1 // Parse and represent SVG preserveAspectRatio attribute
3 // Copyright (C) 2014 Thomas Geymayer <tomgey@gmail.com>
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "SVGpreserveAspectRatio.hxx"
21 #include <simgear/debug/logstream.hxx>
22 #include <simgear/misc/strutils.hxx>
24 #include <boost/tokenizer.hpp>
29 //----------------------------------------------------------------------------
30 SVGpreserveAspectRatio::SVGpreserveAspectRatio():
38 //----------------------------------------------------------------------------
39 SVGpreserveAspectRatio::Align SVGpreserveAspectRatio::alignX() const
44 //----------------------------------------------------------------------------
45 SVGpreserveAspectRatio::Align SVGpreserveAspectRatio::alignY() const
50 //----------------------------------------------------------------------------
51 bool SVGpreserveAspectRatio::scaleToFill() const
53 return (_align_x == ALIGN_NONE) && (_align_y == ALIGN_NONE);
56 //----------------------------------------------------------------------------
57 bool SVGpreserveAspectRatio::scaleToFit() const
59 return !scaleToFill() && _meet;
62 //----------------------------------------------------------------------------
63 bool SVGpreserveAspectRatio::scaleToCrop() const
65 return !scaleToFill() && !_meet;
68 //----------------------------------------------------------------------------
69 bool SVGpreserveAspectRatio::meet() const
74 //----------------------------------------------------------------------------
76 SVGpreserveAspectRatio::operator==(const SVGpreserveAspectRatio& rhs) const
78 return (_align_x == rhs._align_x)
79 && (_align_y == rhs._align_y)
80 && (_meet == rhs._meet || scaleToFill());
83 //----------------------------------------------------------------------------
84 SVGpreserveAspectRatio SVGpreserveAspectRatio::parse(const std::string& str)
86 SVGpreserveAspectRatio ret;
94 } parse_state = PARSE_defer;
96 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
97 const boost::char_separator<char> del(" \t\n");
99 tokenizer tokens(str.begin(), str.end(), del);
100 for( tokenizer::const_iterator tok = tokens.begin();
102 && parse_state != PARSE_error;
105 const std::string& cur_tok = tok.current_token();
107 switch( parse_state )
110 if( cur_tok == "defer" )
114 "SVGpreserveAspectRatio: 'defer' is ignored." );
115 parse_state = PARSE_align;
120 if( cur_tok == "none" )
122 ret._align_x = ALIGN_NONE;
123 ret._align_y = ALIGN_NONE;
125 else if( cur_tok.length() == 8 )
127 if( strutils::starts_with(cur_tok, "xMin") )
128 ret._align_x = ALIGN_MIN;
129 else if( strutils::starts_with(cur_tok, "xMid") )
130 ret._align_x = ALIGN_MID;
131 else if( strutils::starts_with(cur_tok, "xMax") )
132 ret._align_x = ALIGN_MAX;
135 parse_state = PARSE_error;
139 if( strutils::ends_with(cur_tok, "YMin") )
140 ret._align_y = ALIGN_MIN;
141 else if( strutils::ends_with(cur_tok, "YMid") )
142 ret._align_y = ALIGN_MID;
143 else if( strutils::ends_with(cur_tok, "YMax") )
144 ret._align_y = ALIGN_MAX;
147 parse_state = PARSE_error;
153 parse_state = PARSE_error;
156 parse_state = PARSE_meetOrSlice;
158 case PARSE_meetOrSlice:
159 if( cur_tok == "meet" )
161 else if( cur_tok == "slice" )
165 parse_state = PARSE_error;
168 parse_state = PARSE_done;
173 "SVGpreserveAspectRatio: Ignoring superfluous token"
174 " '" << cur_tok << "'" );
181 if( parse_state == PARSE_error )
185 "SVGpreserveAspectRatio: Failed to parse: '" << str << "'" );
186 return SVGpreserveAspectRatio();
192 } // namespace simgear