1 | /* |
---|
2 | rsvg-css.c: Parse CSS basic data types. |
---|
3 | |
---|
4 | Copyright (C) 2000 Eazel, Inc. |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or |
---|
7 | modify it under the terms of the GNU General Public License as |
---|
8 | published by the Free Software Foundation; either version 2 of the |
---|
9 | License, or (at your option) any later version. |
---|
10 | |
---|
11 | This program is distributed in the hope that it will be useful, |
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
14 | General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU General Public |
---|
17 | License along with this program; if not, write to the |
---|
18 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
19 | Boston, MA 02111-1307, USA. |
---|
20 | |
---|
21 | Author: Raph Levien <raph@artofcode.com> |
---|
22 | */ |
---|
23 | |
---|
24 | #include <string.h> |
---|
25 | #include <stdlib.h> |
---|
26 | #include <math.h> |
---|
27 | |
---|
28 | #include <glib.h> |
---|
29 | |
---|
30 | #include "rsvg-css.h" |
---|
31 | |
---|
32 | /** |
---|
33 | * rsvg_css_parse_length: Parse CSS2 length to a pixel value. |
---|
34 | * @str: Original string. |
---|
35 | * @fixed: Where to store boolean value of whether length is fixed. |
---|
36 | * |
---|
37 | * Parses a CSS2 length into a pixel value. |
---|
38 | * |
---|
39 | * Returns: returns the length. |
---|
40 | **/ |
---|
41 | double |
---|
42 | rsvg_css_parse_length (const char *str, gint *fixed) |
---|
43 | { |
---|
44 | char *p; |
---|
45 | |
---|
46 | /* |
---|
47 | * The supported CSS length unit specifiers are: |
---|
48 | * em, ex, px, pt, pc, cm, mm, in, and percentages. |
---|
49 | */ |
---|
50 | |
---|
51 | *fixed = FALSE; |
---|
52 | |
---|
53 | p = strstr (str, "px"); |
---|
54 | if (p != NULL) |
---|
55 | { |
---|
56 | *fixed = TRUE; |
---|
57 | return atof (str); |
---|
58 | } |
---|
59 | p = strstr (str, "in"); |
---|
60 | if (p != NULL) |
---|
61 | { |
---|
62 | *fixed = TRUE; |
---|
63 | /* return svg->pixels_per_inch * atof (str); */ |
---|
64 | } |
---|
65 | p = strstr (str, "%"); |
---|
66 | if (p != NULL) |
---|
67 | { |
---|
68 | return 0.01 * atof (str); |
---|
69 | } |
---|
70 | return atof (str); |
---|
71 | } |
---|
72 | |
---|
73 | gboolean |
---|
74 | rsvg_css_param_match (const char *str, const char *param_name) |
---|
75 | { |
---|
76 | int i; |
---|
77 | |
---|
78 | for (i = 0; str[i] != '\0' && str[i] != ':'; i++) |
---|
79 | if (param_name[i] != str[i]) |
---|
80 | return FALSE; |
---|
81 | return str[i] == ':' && param_name[i] == '\0'; |
---|
82 | } |
---|
83 | |
---|
84 | int |
---|
85 | rsvg_css_param_arg_offset (const char *str) |
---|
86 | { |
---|
87 | int i; |
---|
88 | |
---|
89 | for (i = 0; str[i] != '\0' && str[i] != ':'; i++); |
---|
90 | if (str[i] != '\0') i++; |
---|
91 | for (; str[i] == ' '; i++); |
---|
92 | return i; |
---|
93 | } |
---|
94 | |
---|
95 | /* Parse a CSS2 color, returning rgb */ |
---|
96 | guint32 |
---|
97 | rsvg_css_parse_color (const char *str) |
---|
98 | { |
---|
99 | gint val = 0; |
---|
100 | static GHashTable *colors = NULL; |
---|
101 | |
---|
102 | /* todo: better failure detection */ |
---|
103 | |
---|
104 | /* |
---|
105 | * todo: handle the rgb (r, g, b) and rgb ( r%, g%, b%), syntax |
---|
106 | * defined in http://www.w3.org/TR/REC-CSS2/syndata.html#color-units |
---|
107 | */ |
---|
108 | #ifdef VERBOSE |
---|
109 | g_print ("color = %s\n", str); |
---|
110 | #endif |
---|
111 | if (str[0] == '#') |
---|
112 | { |
---|
113 | int i; |
---|
114 | for (i = 1; str[i]; i++) |
---|
115 | { |
---|
116 | int hexval; |
---|
117 | if (str[i] >= '0' && str[i] <= '9') |
---|
118 | hexval = str[i] - '0'; |
---|
119 | else if (str[i] >= 'A' && str[i] <= 'F') |
---|
120 | hexval = str[i] - 'A' + 10; |
---|
121 | else if (str[i] >= 'a' && str[i] <= 'f') |
---|
122 | hexval = str[i] - 'a' + 10; |
---|
123 | else |
---|
124 | break; |
---|
125 | val = (val << 4) + hexval; |
---|
126 | } |
---|
127 | /* handle #rgb case */ |
---|
128 | if (i == 4) |
---|
129 | { |
---|
130 | val = ((val & 0xf00) << 8) | |
---|
131 | ((val & 0x0f0) << 4) | |
---|
132 | (val & 0x00f); |
---|
133 | val |= val << 4; |
---|
134 | } |
---|
135 | #ifdef VERBOSE |
---|
136 | printf ("val = %x\n", val); |
---|
137 | #endif |
---|
138 | } |
---|
139 | else |
---|
140 | { |
---|
141 | GString * string; |
---|
142 | if (!colors) |
---|
143 | { |
---|
144 | colors = g_hash_table_new (g_str_hash, g_str_equal); |
---|
145 | |
---|
146 | g_hash_table_insert (colors, "black", GINT_TO_POINTER (0x000000)); |
---|
147 | g_hash_table_insert (colors, "silver", GINT_TO_POINTER (0xc0c0c0)); |
---|
148 | g_hash_table_insert (colors, "gray", GINT_TO_POINTER (0x808080)); |
---|
149 | g_hash_table_insert (colors, "white", GINT_TO_POINTER (0xFFFFFF)); |
---|
150 | g_hash_table_insert (colors, "maroon", GINT_TO_POINTER (0x800000)); |
---|
151 | g_hash_table_insert (colors, "red", GINT_TO_POINTER (0xFF0000)); |
---|
152 | g_hash_table_insert (colors, "purple", GINT_TO_POINTER (0x800080)); |
---|
153 | g_hash_table_insert (colors, "fuchsia", GINT_TO_POINTER (0xFF00FF)); |
---|
154 | g_hash_table_insert (colors, "green", GINT_TO_POINTER (0x008000)); |
---|
155 | g_hash_table_insert (colors, "lime", GINT_TO_POINTER (0x00FF00)); |
---|
156 | g_hash_table_insert (colors, "olive", GINT_TO_POINTER (0x808000)); |
---|
157 | g_hash_table_insert (colors, "yellow", GINT_TO_POINTER (0xFFFF00)); |
---|
158 | g_hash_table_insert (colors, "navy", GINT_TO_POINTER (0x000080)); |
---|
159 | g_hash_table_insert (colors, "blue", GINT_TO_POINTER (0x0000FF)); |
---|
160 | g_hash_table_insert (colors, "teal", GINT_TO_POINTER (0x008080)); |
---|
161 | g_hash_table_insert (colors, "aqua", GINT_TO_POINTER (0x00FFFF)); |
---|
162 | } |
---|
163 | |
---|
164 | string = g_string_down (g_string_new (str)); |
---|
165 | |
---|
166 | /* this will default to black on a failed lookup */ |
---|
167 | val = GPOINTER_TO_INT (g_hash_table_lookup (colors, string->str)); |
---|
168 | } |
---|
169 | |
---|
170 | return val; |
---|
171 | } |
---|
172 | |
---|
173 | guint |
---|
174 | rsvg_css_parse_opacity (const char *str) |
---|
175 | { |
---|
176 | char *end_ptr; |
---|
177 | double opacity; |
---|
178 | |
---|
179 | opacity = strtod (str, &end_ptr); |
---|
180 | |
---|
181 | if (end_ptr[0] == '%') |
---|
182 | opacity *= 0.01; |
---|
183 | |
---|
184 | return floor (opacity * 255 + 0.5); |
---|
185 | } |
---|
186 | |
---|
187 | double |
---|
188 | rsvg_css_parse_fontsize (const char *str) |
---|
189 | { |
---|
190 | char *end_ptr; |
---|
191 | double size; |
---|
192 | |
---|
193 | /* todo: handle absolute-size and relative-size tags and proper units */ |
---|
194 | size = strtod (str, &end_ptr); |
---|
195 | |
---|
196 | if (end_ptr[0] == '%') |
---|
197 | size = (36 * size * 0.01); /* todo: egregious hack */ |
---|
198 | |
---|
199 | return size; |
---|
200 | } |
---|
201 | |
---|