]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssnumbervalue.c
css: Huge refactoring to avoid computing wrong values
[~andy/gtk] / gtk / gtkcssnumbervalue.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2011 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "config.h"
19
20 #include "gtkcssnumbervalueprivate.h"
21
22 #include "gtkcssenumvalueprivate.h"
23 #include "gtkstylepropertyprivate.h"
24
25 struct _GtkCssValue {
26   GTK_CSS_VALUE_BASE
27   GtkCssUnit unit;
28   double value;
29 };
30
31 static void
32 gtk_css_value_number_free (GtkCssValue *value)
33 {
34   g_slice_free (GtkCssValue, value);
35 }
36
37 static GtkCssValue *
38 gtk_css_value_number_compute (GtkCssValue             *number,
39                               guint                    property_id,
40                               GtkStyleProviderPrivate *provider,
41                               GtkCssComputedValues    *values,
42                               GtkCssComputedValues    *parent_values,
43                               GtkCssDependencies      *dependencies)
44 {
45   GtkBorderStyle border_style;
46
47   /* I don't like this special case being here in this generic code path, but no idea where else to put it. */
48   switch (property_id)
49     {
50       case GTK_CSS_PROPERTY_BORDER_TOP_WIDTH:
51         border_style = _gtk_css_border_style_value_get (_gtk_css_computed_values_get_value (values, GTK_CSS_PROPERTY_BORDER_TOP_STYLE));
52         if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
53           return _gtk_css_number_value_new (0, GTK_CSS_PX);
54         break;
55       case GTK_CSS_PROPERTY_BORDER_RIGHT_WIDTH:
56         border_style = _gtk_css_border_style_value_get (_gtk_css_computed_values_get_value (values, GTK_CSS_PROPERTY_BORDER_RIGHT_STYLE));
57         if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
58           return _gtk_css_number_value_new (0, GTK_CSS_PX);
59         break;
60       case GTK_CSS_PROPERTY_BORDER_BOTTOM_WIDTH:
61         border_style = _gtk_css_border_style_value_get (_gtk_css_computed_values_get_value (values, GTK_CSS_PROPERTY_BORDER_BOTTOM_STYLE));
62         if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
63           return _gtk_css_number_value_new (0, GTK_CSS_PX);
64         break;
65       case GTK_CSS_PROPERTY_BORDER_LEFT_WIDTH:
66         border_style = _gtk_css_border_style_value_get (_gtk_css_computed_values_get_value (values, GTK_CSS_PROPERTY_BORDER_LEFT_STYLE));
67         if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
68           return _gtk_css_number_value_new (0, GTK_CSS_PX);
69         break;
70       case GTK_CSS_PROPERTY_OUTLINE_WIDTH:
71         border_style = _gtk_css_border_style_value_get (_gtk_css_computed_values_get_value (values, GTK_CSS_PROPERTY_OUTLINE_STYLE));
72         if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
73           return _gtk_css_number_value_new (0, GTK_CSS_PX);
74         break;
75       default:
76         break;
77     }
78
79   switch (number->unit)
80     {
81     default:
82       g_assert_not_reached();
83       /* fall through */
84     case GTK_CSS_PERCENT:
85     case GTK_CSS_NUMBER:
86     case GTK_CSS_PX:
87     case GTK_CSS_DEG:
88     case GTK_CSS_S:
89       return _gtk_css_value_ref (number);
90     case GTK_CSS_PT:
91       return _gtk_css_number_value_new (number->value * 96.0 / 72.0,
92                                         GTK_CSS_PX);
93     case GTK_CSS_PC:
94       return _gtk_css_number_value_new (number->value * 96.0 / 72.0 * 12.0,
95                                         GTK_CSS_PX);
96       break;
97     case GTK_CSS_IN:
98       return _gtk_css_number_value_new (number->value * 96.0,
99                                         GTK_CSS_PX);
100       break;
101     case GTK_CSS_CM:
102       return _gtk_css_number_value_new (number->value * 96.0 * 0.39370078740157477,
103                                         GTK_CSS_PX);
104       break;
105     case GTK_CSS_MM:
106       return _gtk_css_number_value_new (number->value * 96.0 * 0.039370078740157477,
107                                         GTK_CSS_PX);
108       break;
109     case GTK_CSS_EM:
110       *dependencies = GTK_CSS_DEPENDS_ON_FONT_SIZE;
111       return _gtk_css_number_value_new (number->value *
112                                         _gtk_css_number_value_get (_gtk_css_computed_values_get_value (values, GTK_CSS_PROPERTY_FONT_SIZE), 100),
113                                         GTK_CSS_PX);
114       break;
115     case GTK_CSS_EX:
116       /* for now we pretend ex is half of em */
117       *dependencies = GTK_CSS_DEPENDS_ON_FONT_SIZE;
118       return _gtk_css_number_value_new (number->value * 0.5 * 
119                                         _gtk_css_number_value_get (_gtk_css_computed_values_get_value (values, GTK_CSS_PROPERTY_FONT_SIZE), 100),
120                                         GTK_CSS_PX);
121     case GTK_CSS_RAD:
122       return _gtk_css_number_value_new (number->value * 360.0 / (2 * G_PI),
123                                         GTK_CSS_DEG);
124     case GTK_CSS_GRAD:
125       return _gtk_css_number_value_new (number->value * 360.0 / 400.0,
126                                         GTK_CSS_DEG);
127     case GTK_CSS_TURN:
128       return _gtk_css_number_value_new (number->value * 360.0,
129                                         GTK_CSS_DEG);
130     case GTK_CSS_MS:
131       return _gtk_css_number_value_new (number->value / 1000.0,
132                                         GTK_CSS_S);
133     }
134 }
135
136 static gboolean
137 gtk_css_value_number_equal (const GtkCssValue *number1,
138                             const GtkCssValue *number2)
139 {
140   return number1->unit == number2->unit &&
141          number1->value == number2->value;
142 }
143
144 static GtkCssValue *
145 gtk_css_value_number_transition (GtkCssValue *start,
146                                  GtkCssValue *end,
147                                  guint        property_id,
148                                  double       progress)
149 {
150   /* FIXME: This needs to be supported at least for percentages,
151    * but for that we kinda need to support calc(5px + 50%) */
152   if (start->unit != end->unit)
153     return NULL;
154
155   return _gtk_css_number_value_new (start->value + (end->value - start->value) * progress,
156                                     start->unit);
157 }
158
159 static void
160 gtk_css_value_number_print (const GtkCssValue *number,
161                             GString           *string)
162 {
163   char buf[G_ASCII_DTOSTR_BUF_SIZE];
164
165   const char *names[] = {
166     /* [GTK_CSS_NUMBER] = */ "",
167     /* [GTK_CSS_PERCENT] = */ "%",
168     /* [GTK_CSS_PX] = */ "px",
169     /* [GTK_CSS_PT] = */ "pt",
170     /* [GTK_CSS_EM] = */ "em",
171     /* [GTK_CSS_EX] = */ "ex",
172     /* [GTK_CSS_PC] = */ "pc",
173     /* [GTK_CSS_IN] = */ "in",
174     /* [GTK_CSS_CM] = */ "cm",
175     /* [GTK_CSS_MM] = */ "mm",
176     /* [GTK_CSS_RAD] = */ "rad",
177     /* [GTK_CSS_DEG] = */ "deg",
178     /* [GTK_CSS_GRAD] = */ "grad",
179     /* [GTK_CSS_TURN] = */ "turn",
180     /* [GTK_CSS_S] = */ "s",
181     /* [GTK_CSS_MS] = */ "ms",
182   };
183
184   g_ascii_dtostr (buf, sizeof (buf), number->value);
185   g_string_append (string, buf);
186   if (number->value != 0.0)
187     g_string_append (string, names[number->unit]);
188 }
189
190 static const GtkCssValueClass GTK_CSS_VALUE_NUMBER = {
191   gtk_css_value_number_free,
192   gtk_css_value_number_compute,
193   gtk_css_value_number_equal,
194   gtk_css_value_number_transition,
195   gtk_css_value_number_print
196 };
197
198 GtkCssValue *
199 _gtk_css_number_value_new (double     value,
200                            GtkCssUnit unit)
201 {
202   static GtkCssValue zero_singleton = { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_NUMBER, 0 };
203   static GtkCssValue px_singletons[] = {
204     { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 0 },
205     { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 1 },
206     { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 2 },
207     { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 3 },
208     { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 4 },
209   };
210   GtkCssValue *result;
211
212   if (unit == GTK_CSS_NUMBER && value == 0)
213     return _gtk_css_value_ref (&zero_singleton);
214
215   if (unit == GTK_CSS_PX &&
216       (value == 0 ||
217        value == 1 ||
218        value == 2 ||
219        value == 3 ||
220        value == 4))
221     {
222       return _gtk_css_value_ref (&px_singletons[(int) value]);
223     }
224
225   result = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_NUMBER);
226   result->unit = unit;
227   result->value = value;
228
229   return result;
230 }
231
232 GtkCssUnit
233 _gtk_css_number_value_get_unit (const GtkCssValue *value)
234 {
235   g_return_val_if_fail (value->class == &GTK_CSS_VALUE_NUMBER, GTK_CSS_NUMBER);
236
237   return value->unit;
238 }
239
240 double
241 _gtk_css_number_value_get (const GtkCssValue *number,
242                            double             one_hundred_percent)
243 {
244   g_return_val_if_fail (number != NULL, 0.0);
245   g_return_val_if_fail (number->class == &GTK_CSS_VALUE_NUMBER, 0.0);
246
247   if (number->unit == GTK_CSS_PERCENT)
248     return number->value * one_hundred_percent / 100;
249   else
250     return number->value;
251 }
252