1 // Parse and represent CSS border definitions (eg. margin, border-image-width)
3 // Copyright (C) 2013 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 "CSSBorder.hxx"
21 #include <boost/lexical_cast.hpp>
22 #include <boost/range.hpp>
23 #include <boost/tokenizer.hpp>
28 //----------------------------------------------------------------------------
29 bool CSSBorder::isValid() const
34 //----------------------------------------------------------------------------
35 bool CSSBorder::isNone() const
44 //----------------------------------------------------------------------------
45 const std::string& CSSBorder::getKeyword() const
50 //----------------------------------------------------------------------------
51 CSSBorder::Offsets CSSBorder::getRelOffsets(const SGRect<int>& dim) const
54 for(int i = 0; i < 4; ++i)
56 ret.val[i] = offsets.val[i];
58 ret.val[i] /= (i & 1) ? dim.height() : dim.width();
63 //----------------------------------------------------------------------------
64 CSSBorder::Offsets CSSBorder::getAbsOffsets(const SGRect<int>& dim) const
67 for(int i = 0; i < 4; ++i)
69 ret.val[i] = offsets.val[i];
71 ret.val[i] *= (i & 1) ? dim.height() : dim.width();
76 //----------------------------------------------------------------------------
77 CSSBorder CSSBorder::parse(const std::string& str)
82 // [<number>'%'?]{1,4} (top[,right[,bottom[,left]]])
84 // Percentages are relative to the size of the image: the width of the
85 // image for the horizontal offsets, the height for vertical offsets.
86 // Numbers represent pixels in the image.
90 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
91 const boost::char_separator<char> del(" \t\n");
93 tokenizer tokens(str.begin(), str.end(), del);
94 for( tokenizer::const_iterator tok = tokens.begin();
95 tok != tokens.end() && c < 4;
98 if( isalpha(*tok->begin()) )
102 bool rel = ret.types.rel[c] = (*tok->rbegin() == '%');
104 // Negative values are not allowed and values bigger than the size of
105 // the image are interpreted as ‘100%’. TODO check max
109 boost::lexical_cast<float>
111 rel ? boost::make_iterator_range(tok->begin(), tok->end() - 1)
121 // When four values are specified, they set the offsets on the top, right,
122 // bottom and left sides in that order.
124 #define CSS_COPY_VAL(dest, src)\
126 ret.offsets.val[dest] = ret.offsets.val[src];\
127 ret.types.rel[dest] = ret.types.rel[src];\
135 // if the right is missing, it is the same as the top.
138 // if the bottom is missing, it is the same as the top
142 // If the left is missing, it is the same as the right
148 if( ret.keyword == "none" )
150 memset(&ret.offsets, 0, sizeof(Offsets));
158 } // namespace simgear