]> git.mxchange.org Git - simgear.git/blob - simgear/misc/CSSBorder.cxx
HTTP: Rename urlretrieve/urlload to save/load.
[simgear.git] / simgear / misc / CSSBorder.cxx
1 // Parse and represent CSS border definitions (eg. margin, border-image-width)
2 //
3 // Copyright (C) 2013  Thomas Geymayer <tomgey@gmail.com>
4 //
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.
9 //
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.
14 //
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
18
19 #include "CSSBorder.hxx"
20
21 #include <boost/lexical_cast.hpp>
22 #include <boost/range.hpp>
23 #include <boost/tokenizer.hpp>
24
25 namespace simgear
26 {
27
28   //----------------------------------------------------------------------------
29   bool CSSBorder::isValid() const
30   {
31     return valid;
32   }
33
34   //----------------------------------------------------------------------------
35   bool CSSBorder::isNone() const
36   {
37     return !valid
38         || (  offsets.t == 0
39            && offsets.r == 0
40            && offsets.b == 0
41            && offsets.l == 0 );
42   }
43
44   //----------------------------------------------------------------------------
45   const std::string& CSSBorder::getKeyword() const
46   {
47     return keyword;
48   }
49
50   //----------------------------------------------------------------------------
51   CSSBorder::Offsets CSSBorder::getRelOffsets(const SGRect<int>& dim) const
52   {
53     Offsets ret = {{0}};
54     if( !valid )
55       return ret;
56
57     for(int i = 0; i < 4; ++i)
58     {
59       ret.val[i] = offsets.val[i];
60       if( !types.rel[i] )
61         ret.val[i] /= (i & 1) ? dim.width() : dim.height();
62     }
63
64     return ret;
65   }
66
67   //----------------------------------------------------------------------------
68   CSSBorder::Offsets CSSBorder::getAbsOffsets(const SGRect<int>& dim) const
69   {
70     Offsets ret = {{0}};
71     if( !valid )
72       return ret;
73
74     for(int i = 0; i < 4; ++i)
75     {
76       ret.val[i] = offsets.val[i];
77       if( types.rel[i] )
78         ret.val[i] *= (i & 1) ? dim.width() : dim.height();
79     }
80
81     return ret;
82   }
83
84   //----------------------------------------------------------------------------
85   CSSBorder CSSBorder::parse(const std::string& str)
86   {
87     if( str.empty() )
88       return CSSBorder();
89
90     // [<number>'%'?]{1,4} (top[,right[,bottom[,left]]])
91     //
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.
95     int c = 0;
96     CSSBorder ret;
97
98     typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
99     const boost::char_separator<char> del(" \t\n");
100
101     tokenizer tokens(str.begin(), str.end(), del);
102     for( tokenizer::const_iterator tok = tokens.begin();
103          tok != tokens.end() && c < 4;
104          ++tok )
105     {
106       if( isalpha(*tok->begin()) )
107         ret.keyword = *tok;
108       else
109       {
110         bool rel = ret.types.rel[c] = (*tok->rbegin() == '%');
111         ret.offsets.val[c] =
112           // Negative values are not allowed and values bigger than the size of
113           // the image are interpreted as ‘100%’. TODO check max
114           std::max
115           (
116             0.f,
117             boost::lexical_cast<float>
118             (
119               rel ? boost::make_iterator_range(tok->begin(), tok->end() - 1)
120                   : *tok
121             )
122             /
123             (rel ? 100 : 1)
124           );
125         ++c;
126       }
127     }
128
129     // When four values are specified, they set the offsets on the top, right,
130     // bottom and left sides in that order.
131
132 #define CSS_COPY_VAL(dest, src)\
133   {\
134     ret.offsets.val[dest] = ret.offsets.val[src];\
135     ret.types.rel[dest] = ret.types.rel[src];\
136   }
137
138     if( c < 4 )
139     {
140       if( c < 3 )
141       {
142         if( c < 2 )
143           // if the right is missing, it is the same as the top.
144           CSS_COPY_VAL(1, 0);
145
146         // if the bottom is missing, it is the same as the top
147         CSS_COPY_VAL(2, 0);
148       }
149
150       // If the left is missing, it is the same as the right
151       CSS_COPY_VAL(3, 1);
152     }
153
154 #undef CSS_COPY_VAL
155
156     if( ret.keyword == "none" )
157     {
158       memset(&ret.offsets, 0, sizeof(Offsets));
159       ret.keyword.clear();
160     }
161
162     ret.valid = true;
163     return ret;
164   }
165
166 } // namespace simgear