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
57 for(int i = 0; i < 4; ++i)
59 ret.val[i] = offsets.val[i];
61 ret.val[i] /= (i & 1) ? dim.width() : dim.height();
67 //----------------------------------------------------------------------------
68 CSSBorder::Offsets CSSBorder::getAbsOffsets(const SGRect<int>& dim) const
74 for(int i = 0; i < 4; ++i)
76 ret.val[i] = offsets.val[i];
78 ret.val[i] *= (i & 1) ? dim.width() : dim.height();
84 //----------------------------------------------------------------------------
85 CSSBorder CSSBorder::parse(const std::string& str)
90 // [<number>'%'?]{1,4} (top[,right[,bottom[,left]]])
92 // Percentages are relative to the size of the image: the width of the
93 // image for the horizontal offsets, the height for vertical offsets.
94 // Numbers represent pixels in the image.
98 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
99 const boost::char_separator<char> del(" \t\n");
101 tokenizer tokens(str.begin(), str.end(), del);
102 for( tokenizer::const_iterator tok = tokens.begin();
103 tok != tokens.end() && c < 4;
106 if( isalpha(*tok->begin()) )
110 bool rel = ret.types.rel[c] = (*tok->rbegin() == '%');
112 // Negative values are not allowed and values bigger than the size of
113 // the image are interpreted as ‘100%’. TODO check max
117 boost::lexical_cast<float>
119 rel ? boost::make_iterator_range(tok->begin(), tok->end() - 1)
129 // When four values are specified, they set the offsets on the top, right,
130 // bottom and left sides in that order.
132 #define CSS_COPY_VAL(dest, src)\
134 ret.offsets.val[dest] = ret.offsets.val[src];\
135 ret.types.rel[dest] = ret.types.rel[src];\
143 // if the right is missing, it is the same as the top.
146 // if the bottom is missing, it is the same as the top
150 // If the left is missing, it is the same as the right
156 if( ret.keyword == "none" )
158 memset(&ret.offsets, 0, sizeof(Offsets));
166 } // namespace simgear