1 | /* "a -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
---|
2 | /* This file is part of the GtkHTML library. |
---|
3 | |
---|
4 | Copyright (C) 2002, Ximian Inc. |
---|
5 | |
---|
6 | This library is free software; you can redistribute it and/or |
---|
7 | modify it under the terms of the GNU Library General Public |
---|
8 | License as published by the Free Software Foundation; either |
---|
9 | version 2 of the License, or (at your option) any later version. |
---|
10 | |
---|
11 | This library 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 | Library General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU Library General Public License |
---|
17 | along with this library; see the file COPYING.LIB. If not, write to |
---|
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
19 | Boston, MA 02111-1307, USA. |
---|
20 | */ |
---|
21 | |
---|
22 | #include <string.h> |
---|
23 | #include <ctype.h> |
---|
24 | #include "htmlstyle.h" |
---|
25 | |
---|
26 | /* Color handling. */ |
---|
27 | gboolean |
---|
28 | html_parse_color (const gchar *text, |
---|
29 | GdkColor *color) |
---|
30 | { |
---|
31 | gchar c [8]; |
---|
32 | gint len = strlen (text); |
---|
33 | |
---|
34 | if (gdk_color_parse (text, color)) |
---|
35 | return TRUE; |
---|
36 | |
---|
37 | c [7] = 0; |
---|
38 | if (*text != '#') { |
---|
39 | c[0] = '#'; |
---|
40 | strncpy (c + 1, text, 6); |
---|
41 | len++; |
---|
42 | } else { |
---|
43 | strncpy (c, text, MIN (7, len)); |
---|
44 | } |
---|
45 | |
---|
46 | if (len < 7) |
---|
47 | memset (c + len, '\0', 7-len); |
---|
48 | |
---|
49 | return gdk_color_parse (c, color); |
---|
50 | } |
---|
51 | |
---|
52 | static HTMLLength * |
---|
53 | parse_length (char *str) { |
---|
54 | char *cur = str; |
---|
55 | HTMLLength *len; |
---|
56 | |
---|
57 | len = g_new0 (HTMLLength, 1); |
---|
58 | |
---|
59 | if (!str) |
---|
60 | return len; |
---|
61 | |
---|
62 | /* g_warning ("begin \"%s\"", *str); */ |
---|
63 | |
---|
64 | while (isspace (*cur)) cur++; |
---|
65 | |
---|
66 | len->val = atoi (cur); |
---|
67 | len->type = HTML_LENGTH_TYPE_PIXELS; |
---|
68 | |
---|
69 | while (isdigit (*cur) || *cur == '-') cur++; |
---|
70 | |
---|
71 | switch (*cur) { |
---|
72 | case '*': |
---|
73 | if (len->val == 0) |
---|
74 | len->val = 1; |
---|
75 | len->type = HTML_LENGTH_TYPE_FRACTION; |
---|
76 | cur++; |
---|
77 | break; |
---|
78 | case '%': |
---|
79 | len->type = HTML_LENGTH_TYPE_PERCENT; |
---|
80 | cur++; |
---|
81 | break; |
---|
82 | } |
---|
83 | |
---|
84 | if (cur <= str) { |
---|
85 | g_free (len); |
---|
86 | return NULL; |
---|
87 | } |
---|
88 | |
---|
89 | /* g_warning ("length len->val=%d, len->type=%d", len->val, len->type); */ |
---|
90 | |
---|
91 | return len; |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | HTMLStyle * |
---|
96 | html_style_new (void) |
---|
97 | { |
---|
98 | HTMLStyle *style = g_new0 (HTMLStyle, 1); |
---|
99 | |
---|
100 | style->display = DISPLAY_NONE; |
---|
101 | |
---|
102 | style->color = NULL; |
---|
103 | style->mask = 0; |
---|
104 | style->settings = 0; |
---|
105 | |
---|
106 | /* BLOCK */ |
---|
107 | style->text_align = HTML_HALIGN_NONE; |
---|
108 | style->clear = HTML_CLEAR_NONE; |
---|
109 | |
---|
110 | style->text_valign = HTML_VALIGN_NONE; |
---|
111 | |
---|
112 | return style; |
---|
113 | } |
---|
114 | |
---|
115 | void |
---|
116 | html_style_free (HTMLStyle *style) |
---|
117 | { |
---|
118 | if (!style) |
---|
119 | return; |
---|
120 | |
---|
121 | g_free (style->face); |
---|
122 | g_free (style->bg_image); |
---|
123 | g_free (style->width); |
---|
124 | g_free (style->height); |
---|
125 | |
---|
126 | if (style->color) |
---|
127 | html_color_unref (style->color); |
---|
128 | |
---|
129 | if (style->bg_color) |
---|
130 | html_color_unref (style->bg_color); |
---|
131 | |
---|
132 | g_free (style); |
---|
133 | } |
---|
134 | |
---|
135 | HTMLStyle * |
---|
136 | html_style_add_color (HTMLStyle *style, HTMLColor *color) |
---|
137 | { |
---|
138 | HTMLColor *old; |
---|
139 | |
---|
140 | if (!style) |
---|
141 | style = html_style_new (); |
---|
142 | |
---|
143 | old = style->color; |
---|
144 | |
---|
145 | style->color = color; |
---|
146 | |
---|
147 | if (color) |
---|
148 | html_color_ref (color); |
---|
149 | |
---|
150 | if (old) |
---|
151 | html_color_unref (old); |
---|
152 | |
---|
153 | return style; |
---|
154 | } |
---|
155 | |
---|
156 | HTMLStyle * |
---|
157 | html_style_unset_decoration (HTMLStyle *style, GtkHTMLFontStyle font_style) |
---|
158 | { |
---|
159 | if (!style) |
---|
160 | style = html_style_new (); |
---|
161 | |
---|
162 | font_style &= ~GTK_HTML_FONT_STYLE_SIZE_MASK; |
---|
163 | style->mask |= font_style; |
---|
164 | style->settings &= ~font_style; |
---|
165 | |
---|
166 | return style; |
---|
167 | } |
---|
168 | |
---|
169 | HTMLStyle * |
---|
170 | html_style_set_decoration (HTMLStyle *style, GtkHTMLFontStyle font_style) |
---|
171 | { |
---|
172 | if (!style) |
---|
173 | style = html_style_new (); |
---|
174 | |
---|
175 | font_style &= ~GTK_HTML_FONT_STYLE_SIZE_MASK; |
---|
176 | style->mask |= font_style; |
---|
177 | style->settings |= font_style; |
---|
178 | |
---|
179 | return style; |
---|
180 | } |
---|
181 | |
---|
182 | HTMLStyle * |
---|
183 | html_style_set_font_size (HTMLStyle *style, GtkHTMLFontStyle font_style) |
---|
184 | { |
---|
185 | if (!style) |
---|
186 | style = html_style_new (); |
---|
187 | |
---|
188 | font_style &= GTK_HTML_FONT_STYLE_SIZE_MASK; |
---|
189 | style->mask |= GTK_HTML_FONT_STYLE_SIZE_MASK; |
---|
190 | style->settings |= font_style; |
---|
191 | |
---|
192 | return style; |
---|
193 | } |
---|
194 | |
---|
195 | HTMLStyle * |
---|
196 | html_style_add_font_face (HTMLStyle *style, const HTMLFontFace *face) |
---|
197 | { |
---|
198 | if (!style) |
---|
199 | style = html_style_new (); |
---|
200 | |
---|
201 | g_free (style->face); |
---|
202 | style->face = g_strdup (face); |
---|
203 | |
---|
204 | return style; |
---|
205 | } |
---|
206 | |
---|
207 | HTMLStyle * |
---|
208 | html_style_add_text_align (HTMLStyle *style, HTMLHAlignType type) |
---|
209 | { |
---|
210 | if (!style) |
---|
211 | style = html_style_new (); |
---|
212 | |
---|
213 | style->text_align = type; |
---|
214 | |
---|
215 | return style; |
---|
216 | } |
---|
217 | |
---|
218 | HTMLStyle * |
---|
219 | html_style_add_text_valign (HTMLStyle *style, HTMLVAlignType type) |
---|
220 | { |
---|
221 | if (!style) |
---|
222 | style = html_style_new (); |
---|
223 | |
---|
224 | style->text_valign = type; |
---|
225 | |
---|
226 | return style; |
---|
227 | } |
---|
228 | |
---|
229 | HTMLStyle * |
---|
230 | html_style_add_background_color (HTMLStyle *style, HTMLColor *color) |
---|
231 | { |
---|
232 | HTMLColor *old; |
---|
233 | |
---|
234 | if (!style) |
---|
235 | style = html_style_new (); |
---|
236 | |
---|
237 | old = style->bg_color; |
---|
238 | |
---|
239 | style->bg_color = color; |
---|
240 | |
---|
241 | if (color) |
---|
242 | html_color_ref (color); |
---|
243 | |
---|
244 | if (old) |
---|
245 | html_color_unref (old); |
---|
246 | |
---|
247 | return style; |
---|
248 | } |
---|
249 | |
---|
250 | HTMLStyle * |
---|
251 | html_style_set_display (HTMLStyle *style, HTMLDisplayType display) |
---|
252 | { |
---|
253 | if (!style) |
---|
254 | style = html_style_new (); |
---|
255 | |
---|
256 | style->display = display; |
---|
257 | |
---|
258 | return style; |
---|
259 | } |
---|
260 | |
---|
261 | HTMLStyle * |
---|
262 | html_style_set_clear (HTMLStyle *style, HTMLClearType clear) |
---|
263 | { |
---|
264 | if (!style) |
---|
265 | style = html_style_new (); |
---|
266 | |
---|
267 | style->clear = clear; |
---|
268 | |
---|
269 | return style; |
---|
270 | } |
---|
271 | |
---|
272 | HTMLStyle * |
---|
273 | html_style_add_width (HTMLStyle *style, char *len) |
---|
274 | { |
---|
275 | if (!style) |
---|
276 | style = html_style_new (); |
---|
277 | |
---|
278 | g_free (style->width); |
---|
279 | |
---|
280 | style->width = parse_length (len); |
---|
281 | |
---|
282 | return style; |
---|
283 | } |
---|
284 | |
---|
285 | HTMLStyle * |
---|
286 | html_style_add_height (HTMLStyle *style, char *len) |
---|
287 | { |
---|
288 | if (!style) |
---|
289 | style = html_style_new (); |
---|
290 | |
---|
291 | g_free (style->height); |
---|
292 | |
---|
293 | style->height = parse_length (len); |
---|
294 | |
---|
295 | return style; |
---|
296 | } |
---|
297 | |
---|
298 | HTMLStyle * |
---|
299 | html_style_add_background_image (HTMLStyle *style, const char *url) |
---|
300 | { |
---|
301 | if (!style) |
---|
302 | style = html_style_new (); |
---|
303 | |
---|
304 | g_free (style->bg_image); |
---|
305 | style->bg_image = g_strdup (url); |
---|
306 | |
---|
307 | return style; |
---|
308 | } |
---|
309 | |
---|
310 | HTMLStyle * |
---|
311 | html_style_add_attribute (HTMLStyle *style, const char *attr) |
---|
312 | { |
---|
313 | gchar **prop; |
---|
314 | |
---|
315 | prop = g_strsplit (attr, ";", 100); |
---|
316 | if (prop) { |
---|
317 | gint i; |
---|
318 | for (i = 0; prop[i]; i++) { |
---|
319 | char *text; |
---|
320 | |
---|
321 | text = g_strstrip (prop[i]); |
---|
322 | if (!strncasecmp ("color: ", text, 7)) { |
---|
323 | GdkColor color; |
---|
324 | |
---|
325 | if (html_parse_color (g_strstrip (text + 7), &color)) { |
---|
326 | HTMLColor *hc = html_color_new_from_gdk_color (&color); |
---|
327 | style = html_style_add_color (style, hc); |
---|
328 | html_color_unref (hc); |
---|
329 | |
---|
330 | } |
---|
331 | } else if (!strncasecmp ("background: ", text, 12)) { |
---|
332 | GdkColor color; |
---|
333 | |
---|
334 | if (html_parse_color (text + 12, &color)) { |
---|
335 | HTMLColor *hc = html_color_new_from_gdk_color (&color); |
---|
336 | style = html_style_add_background_color (style, hc); |
---|
337 | html_color_unref (hc); |
---|
338 | } |
---|
339 | } else if (!strncasecmp ("background-color: ", text, 18)) { |
---|
340 | GdkColor color; |
---|
341 | |
---|
342 | if (html_parse_color (text + 18, &color)) { |
---|
343 | HTMLColor *hc = html_color_new_from_gdk_color (&color); |
---|
344 | style = html_style_add_background_color (style, hc); |
---|
345 | html_color_unref (hc); |
---|
346 | } |
---|
347 | } else if (!strncasecmp ("background-image: ", text, 18)) { |
---|
348 | style = html_style_add_background_image (style, text + 18); |
---|
349 | } else if (!strncasecmp ("white-space: ", text, 13)) { |
---|
350 | /* normal, pre, nowrap, pre-wrap, pre-line, inherit */ |
---|
351 | /* |
---|
352 | if (!strcasecmp ("normal", text + 13)) { |
---|
353 | style = html_style_set_white_space (style, HTML_WHITE_SPACE_NORMAL); |
---|
354 | } else if (!strcasecmp ("pre", text + 13)) { |
---|
355 | style = html_style_set_white_space (style, HTML_WHITE_SPACE_PRE); |
---|
356 | } else if (!strcasecmp ("nowrap", text + 13)) { |
---|
357 | style = html_style_set_white_space (style, HTML_WHITE_SPACE_NOWRAP); |
---|
358 | } else if (!strcasecmp ("pre-wrap", text + 13)) { |
---|
359 | style = html_style_set_white_space (style, HTML_WHITE_SPACE_PRE_WRAP); |
---|
360 | } else if (!strcasecmp ("pre-line", text + 13)) { |
---|
361 | style = html_style_set_white_space (style, HTML_WHITE_SPACE_PRE_LINE); |
---|
362 | } else if (!strcasecmp ("inherit", text + 13)) { |
---|
363 | style = html_style_set_white_space (style, HTML_WHITE_SPACE_INHERIT); |
---|
364 | } |
---|
365 | */ |
---|
366 | } else if (!strncasecmp ("text-decoration: none", text, 21)) { |
---|
367 | style = html_style_unset_decoration (style, ~GTK_HTML_FONT_STYLE_SIZE_MASK); |
---|
368 | } else if (!strncasecmp ("display: ", text, 9)) { |
---|
369 | char *value = text + 9; |
---|
370 | if (!strcasecmp ("block", value)) { |
---|
371 | style = html_style_set_display (style, DISPLAY_BLOCK); |
---|
372 | } else if (!strcasecmp ("inline", value)) { |
---|
373 | style = html_style_set_display (style, DISPLAY_INLINE); |
---|
374 | } else if (!strcasecmp ("none", value)) { |
---|
375 | style = html_style_set_display (style, DISPLAY_NONE); |
---|
376 | } else if (!strcasecmp ("inline-table", value)) { |
---|
377 | style = html_style_set_display (style, DISPLAY_INLINE_TABLE); |
---|
378 | } |
---|
379 | } else if (!strncasecmp ("text-align: center", text, 18)) { |
---|
380 | style = html_style_add_text_align (style, HTML_HALIGN_CENTER); |
---|
381 | } else if (!strncasecmp ("width: ", text, 7)) { |
---|
382 | style = html_style_add_width (style, text + 7); |
---|
383 | } else if (!strncasecmp ("height: ", text, 8)) { |
---|
384 | style = html_style_add_height (style, text + 8); |
---|
385 | } else if (!strncasecmp ("clear: ", text, 7)) { |
---|
386 | char *value = text + 7; |
---|
387 | |
---|
388 | if (!strcasecmp ("left", value)) { |
---|
389 | style = html_style_set_clear (style, HTML_CLEAR_LEFT); |
---|
390 | } else if (!strcasecmp ("right", value)) { |
---|
391 | style = html_style_set_clear (style, HTML_CLEAR_RIGHT); |
---|
392 | } else if (!strcasecmp ("both", value)) { |
---|
393 | style = html_style_set_clear (style, HTML_CLEAR_ALL); |
---|
394 | } else if (!strcasecmp ("inherit", value)) { |
---|
395 | style = html_style_set_clear (style, HTML_CLEAR_INHERIT); |
---|
396 | } else if (!strcasecmp ("none", value)) { |
---|
397 | style = html_style_set_clear (style, HTML_CLEAR_NONE); |
---|
398 | } |
---|
399 | } |
---|
400 | } |
---|
401 | g_strfreev (prop); |
---|
402 | } |
---|
403 | return style; |
---|
404 | } |
---|