Merge branch 'master' of github.com:wmvanvliet/Chimara
[projects/chimara/chimara.git] / libchimara / style.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <math.h>
4 #include "chimara-glk-private.h"
5 #include "glk.h"
6 #include "style.h"
7 #include "magic.h"
8 #include "stream.h"
9 #include "strio.h"
10
11 extern GPrivate *glk_data_key;
12
13 static gboolean style_accept(GScanner *scanner, GTokenType token);
14 static gboolean style_accept_style_selector(GScanner *scanner, ChimaraGlk *glk);
15 static gboolean style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag);
16 static void style_add_tag_to_textbuffer(gpointer key, gpointer tag, gpointer tag_table);
17 static void style_copy_tag_to_textbuffer(gpointer key, gpointer tag, gpointer target_table);
18 static void text_tag_to_attr_list(GtkTextTag *tag, PangoAttrList *list);
19 GtkTextTag* gtk_text_tag_copy(GtkTextTag *tag);
20
21 /**
22  * glk_set_style:
23  * @styl: The style to apply
24  *
25  * Changes the style of the current output stream. @styl should be one of the
26  * <code>style_</code> constants. However, any value is actually legal; if the 
27  * interpreter does not recognize the style value, it will treat it as 
28  * %style_Normal.
29  * <note><para>
30  *  This policy allows for the future definition of styles without breaking old
31  *  Glk libraries.
32  * </para></note>
33  */
34 void
35 glk_set_style(glui32 styl)
36 {
37         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
38         g_return_if_fail(glk_data->current_stream != NULL);
39         glk_set_style_stream(glk_data->current_stream, styl);
40 }
41
42 /* The first 11 tag names must correspond to the first 11 glk tag names as defined below */
43 static const gchar* TAG_NAMES[] = {
44         "normal",
45         "emphasized",
46         "preformatted",
47         "header",
48         "subheader",
49         "alert",
50         "note",
51         "block-quote",
52         "input",
53         "user1",
54         "user2",
55         "hyperlink",
56         "pager",
57         "default"
58 };
59
60 /* The first 11 glk tag names must correspond to the first 11 tag names as defined above */
61 static const gchar* GLK_TAG_NAMES[] = {
62         "glk-normal",
63         "glk-emphasized",
64         "glk-preformatted",
65         "glk-header",
66         "glk-subheader",
67         "glk-alert",
68         "glk-note",
69         "glk-block-quote",
70         "glk-input",
71         "glk-user1",
72         "glk-user2"
73 };
74
75 const gchar**
76 style_get_tag_names()
77 {
78         return TAG_NAMES;
79 }
80
81 /* Internal function: mapping from style enum to tag name */
82 static const gchar*
83 get_tag_name(glui32 style)
84 {
85         if(style >= CHIMARA_NUM_STYLES) {
86                 WARNING("Unsupported style");
87                 return "normal";
88         } else {
89                 return (gchar*) TAG_NAMES[style];
90         }
91 }
92
93 /* Internal function: mapping from glk style enum to tag name */
94 static const gchar*
95 get_glk_tag_name(glui32 style)
96 {
97         if(style >= style_NUMSTYLES) {
98                 WARNING("Unsupported style");
99                 return "normal";
100         } else {
101                 return (gchar*) GLK_TAG_NAMES[style];
102         }
103 }
104
105 /** 
106  * glk_set_style_stream:
107  * @str: Output stream to change the style of
108  * @styl: The style to apply
109  *
110  * This changes the style of the stream @str. See glk_set_style().
111  */
112 void
113 glk_set_style_stream(strid_t str, glui32 styl) {
114         if(str->window == NULL)
115                 return;
116
117         flush_window_buffer(str->window);
118         str->style = (gchar*) get_tag_name(styl);
119         str->glk_style = (gchar*) get_glk_tag_name(styl);
120 }
121
122 /* Internal function: call this to initialize the layout of the 'more' prompt. */
123 void
124 style_init_more_prompt(winid_t win)
125 {
126         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
127
128         win->pager_layout = gtk_widget_create_pango_layout(win->widget, "More");
129         pango_layout_set_attributes(win->pager_layout, glk_data->pager_attr_list);
130 }
131
132 /* Internal function: call this to initialize the default styles to a textbuffer. */
133 void
134 style_init_textbuffer(GtkTextBuffer *buffer)
135 {
136         g_return_if_fail(buffer != NULL);
137
138         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
139
140         /* Place the default text tags in the textbuffer's tag table */
141         g_hash_table_foreach(glk_data->styles->text_buffer, style_add_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
142
143         /* Copy the override text tags to the textbuffers's tag table */
144         g_hash_table_foreach(glk_data->glk_styles->text_buffer, style_copy_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
145 }
146
147
148 /* Internal function: call this to initialize the default styles to a textgrid. */
149 void
150 style_init_textgrid(GtkTextBuffer *buffer)
151 {
152         g_return_if_fail(buffer != NULL);
153         
154         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
155
156         /* Place the default text tags in the textbuffer's tag table */
157         g_hash_table_foreach(glk_data->styles->text_grid, style_add_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
158
159         /* Copy the current text tags to the textbuffers's tag table */
160         g_hash_table_foreach(glk_data->glk_styles->text_grid, style_copy_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
161 }
162
163 /* Internal function used to iterate over the default text tag table, applying them to a textbuffer */
164 static void
165 style_add_tag_to_textbuffer(gpointer key, gpointer tag, gpointer tag_table)
166 {
167         g_return_if_fail(key != NULL);
168         g_return_if_fail(tag != NULL);
169         g_return_if_fail(tag_table != NULL);
170
171         gtk_text_tag_table_add(tag_table, tag);
172 }
173
174 /* Internal function used to iterate over a style table, copying it */
175 static void
176 style_copy_tag_to_textbuffer(gpointer key, gpointer tag, gpointer target_table)
177 {
178         g_return_if_fail(key != NULL);
179         g_return_if_fail(tag != NULL);
180         g_return_if_fail(target_table != NULL);
181
182         gtk_text_tag_table_add(target_table, gtk_text_tag_copy( GTK_TEXT_TAG(tag) ));
183 }
184
185 /* Internal function that copies a text tag */
186 GtkTextTag *
187 gtk_text_tag_copy(GtkTextTag *tag)
188 {
189         GtkTextTag *copy;
190
191         g_return_val_if_fail(tag != NULL, NULL);
192
193         copy = gtk_text_tag_new(tag->name);
194         gtk_text_attributes_copy_values(tag->values, copy->values);
195         
196         #define _COPY_FLAG(flag) copy->flag = tag->flag
197                 _COPY_FLAG (bg_color_set);
198                 _COPY_FLAG (bg_color_set);
199                 _COPY_FLAG (bg_stipple_set);
200                 _COPY_FLAG (fg_color_set);
201                 _COPY_FLAG (fg_stipple_set);
202                 _COPY_FLAG (justification_set);
203                 _COPY_FLAG (left_margin_set);
204                 _COPY_FLAG (indent_set);
205                 _COPY_FLAG (rise_set);
206                 _COPY_FLAG (strikethrough_set);
207                 _COPY_FLAG (right_margin_set);
208                 _COPY_FLAG (pixels_above_lines_set);
209                 _COPY_FLAG (pixels_below_lines_set);
210                 _COPY_FLAG (pixels_inside_wrap_set);
211                 _COPY_FLAG (tabs_set);
212                 _COPY_FLAG (underline_set);
213                 _COPY_FLAG (wrap_mode_set);
214                 _COPY_FLAG (bg_full_height_set);
215                 _COPY_FLAG (invisible_set);
216                 _COPY_FLAG (editable_set);
217                 _COPY_FLAG (language_set);
218                 _COPY_FLAG (scale_set);
219         #undef _COPY_FLAG
220
221         /* Copy the data that was added manually */
222         gpointer reverse_color = g_object_get_data( G_OBJECT(tag), "reverse_color" );
223
224         if(reverse_color)
225                 g_object_set_data( G_OBJECT(copy), "reverse_color", reverse_color );
226
227         return copy;
228 }
229
230 /* Internal function that constructs the default styles */
231 void
232 style_init(ChimaraGlk *glk)
233 {
234         CHIMARA_GLK_USE_PRIVATE(glk, priv);
235         
236         GHashTable *default_text_grid_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
237         GHashTable *default_text_buffer_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
238         GHashTable *glk_text_grid_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
239         GHashTable *glk_text_buffer_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
240         GtkTextTag *tag;
241
242         PangoFontDescription *default_font_desc = pango_font_description_from_string("Serif");
243         PangoFontDescription *monospace_font_desc = pango_font_description_from_string("Monospace");
244         
245         /* Initialise the default styles for a text grid */
246         tag = gtk_text_tag_new("default");
247         g_object_set(tag, "font-desc", monospace_font_desc, NULL);
248         g_hash_table_insert(default_text_grid_styles, "default", tag);
249
250         tag = gtk_text_tag_new("normal");
251         //g_object_set(tag, "font-desc", monospace_font_desc, NULL);
252         g_hash_table_insert(default_text_grid_styles, "normal", tag);
253
254         tag = gtk_text_tag_new("emphasized");
255         //g_object_set(tag, "font-desc", monospace_font_desc, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
256         g_object_set(tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
257         g_hash_table_insert(default_text_grid_styles, "emphasized", tag);
258
259         tag = gtk_text_tag_new("preformatted");
260         g_object_set(tag, "font-desc", monospace_font_desc, NULL);
261         g_hash_table_insert(default_text_grid_styles, "preformatted", tag);
262
263         tag = gtk_text_tag_new("header");
264         //g_object_set(tag, "font-desc", monospace_font_desc, "weight", PANGO_WEIGHT_BOLD, NULL);
265         g_object_set(tag, "weight", PANGO_WEIGHT_BOLD, NULL);
266         g_hash_table_insert(default_text_grid_styles, "header", tag);
267
268         tag = gtk_text_tag_new("subheader");
269         //g_object_set(tag, "font-desc", monospace_font_desc, "weight", PANGO_WEIGHT_BOLD, NULL);
270         g_object_set(tag, "weight", PANGO_WEIGHT_BOLD, NULL);
271         g_hash_table_insert(default_text_grid_styles, "subheader", tag);
272
273         tag = gtk_text_tag_new("alert");
274         //g_object_set(tag, "font-desc", monospace_font_desc, "foreground", "#aa0000", "weight", PANGO_WEIGHT_BOLD, NULL);
275         g_object_set(tag, "foreground", "#aa0000", "weight", PANGO_WEIGHT_BOLD, NULL);
276         g_hash_table_insert(default_text_grid_styles, "alert", tag);
277
278         tag = gtk_text_tag_new("note");
279         //g_object_set(tag, "font-desc", monospace_font_desc, "foreground", "#aaaa00", "weight", PANGO_WEIGHT_BOLD, NULL);
280         g_object_set(tag, "foreground", "#aaaa00", "weight", PANGO_WEIGHT_BOLD, NULL);
281         g_hash_table_insert(default_text_grid_styles, "note", tag);
282
283         tag = gtk_text_tag_new("block-quote");
284         //g_object_set(tag, "font-desc", monospace_font_desc, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
285         g_object_set(tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
286         g_hash_table_insert(default_text_grid_styles, "block-quote", tag);
287
288         tag = gtk_text_tag_new("input");
289         //g_object_set(tag, "font-desc", monospace_font_desc, NULL);
290         g_hash_table_insert(default_text_grid_styles, "input", tag);
291
292         tag = gtk_text_tag_new("user1");
293         //g_object_set(tag, "font-desc", monospace_font_desc, NULL);
294         g_hash_table_insert(default_text_grid_styles, "user1", tag);
295
296         tag = gtk_text_tag_new("user2");
297         //g_object_set(tag, "font-desc", monospace_font_desc, NULL);
298         g_hash_table_insert(default_text_grid_styles, "user2", tag);
299
300         tag = gtk_text_tag_new("hyperlink");
301         g_object_set(tag, "foreground", "#0000ff", "underline", PANGO_UNDERLINE_SINGLE, "underline-set", TRUE, NULL);
302         g_hash_table_insert(default_text_grid_styles, "hyperlink", tag);
303
304         /* Initialise the default styles for a text buffer */
305         tag = gtk_text_tag_new("default");
306         g_object_set(tag, "font-desc", default_font_desc, NULL);
307         g_hash_table_insert(default_text_buffer_styles, "default", tag);
308
309         tag = gtk_text_tag_new("normal");
310         //g_object_set(tag, "font-desc", default_font_desc, NULL);
311         g_hash_table_insert(default_text_buffer_styles, "normal", tag);
312
313         tag = gtk_text_tag_new("emphasized");
314         //g_object_set(tag, "font-desc", default_font_desc, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
315         g_object_set(tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
316         g_hash_table_insert(default_text_buffer_styles, "emphasized", tag);
317
318         tag = gtk_text_tag_new("preformatted");
319         g_object_set(tag, "font-desc", monospace_font_desc, NULL);
320         g_hash_table_insert(default_text_buffer_styles, "preformatted", tag);
321
322         tag = gtk_text_tag_new("header");
323         //g_object_set(tag, "font-desc", default_font_desc, "size-points", 18.0, "weight", PANGO_WEIGHT_BOLD, NULL);
324         g_object_set(tag, "weight", PANGO_WEIGHT_BOLD, NULL);
325         g_hash_table_insert(default_text_buffer_styles, "header", tag);
326
327         tag = gtk_text_tag_new("subheader");
328         //g_object_set(tag, "font-desc", default_font_desc, "size-points", 14.0, "weight", PANGO_WEIGHT_BOLD, NULL);
329         g_object_set(tag, "weight", PANGO_WEIGHT_BOLD, NULL);
330         g_hash_table_insert(default_text_buffer_styles, "subheader", tag);
331
332         tag = gtk_text_tag_new("alert");
333         //g_object_set(tag, "font-desc", default_font_desc, "foreground", "#aa0000", "weight", PANGO_WEIGHT_BOLD, NULL);
334         g_object_set(tag, "foreground", "#aa0000", "weight", PANGO_WEIGHT_BOLD, NULL);
335         g_hash_table_insert(default_text_buffer_styles, "alert", tag);
336
337         tag = gtk_text_tag_new("note");
338         //g_object_set(tag, "font-desc", default_font_desc, "foreground", "#aaaa00", "weight", PANGO_WEIGHT_BOLD, NULL);
339         g_object_set(tag, "foreground", "#aaaa00", "weight", PANGO_WEIGHT_BOLD, NULL);
340         g_hash_table_insert(default_text_buffer_styles, "note", tag);
341
342         tag = gtk_text_tag_new("block-quote");
343         //g_object_set(tag, "font-desc", default_font_desc, "justification", GTK_JUSTIFY_CENTER, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
344         g_object_set(tag, "justification", GTK_JUSTIFY_CENTER, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
345         g_hash_table_insert(default_text_buffer_styles, "block-quote", tag);
346
347         tag = gtk_text_tag_new("input");
348         //g_object_set(tag, "font-desc", default_font_desc, NULL);
349         g_hash_table_insert(default_text_buffer_styles, "input", tag);
350
351         tag = gtk_text_tag_new("user1");
352         //g_object_set(tag, "font-desc", default_font_desc, NULL);
353         g_hash_table_insert(default_text_buffer_styles, "user1", tag);
354
355         tag = gtk_text_tag_new("user2");
356         //g_object_set(tag, "font-desc", default_font_desc, NULL);
357         g_hash_table_insert(default_text_buffer_styles, "user2", tag);
358
359         tag = gtk_text_tag_new("hyperlink");
360         //g_object_set(tag, "font-desc", default_font_desc, "foreground", "#0000ff", "underline", PANGO_UNDERLINE_SINGLE, "underline-set", TRUE, NULL);
361         g_object_set(tag, "foreground", "#0000ff", "underline", PANGO_UNDERLINE_SINGLE, "underline-set", TRUE, NULL);
362         g_hash_table_insert(default_text_buffer_styles, "hyperlink", tag);
363
364         GtkTextTag *pager_tag = gtk_text_tag_new("pager");
365         g_object_set(pager_tag, "font-desc", default_font_desc, "foreground", "#ffffff", "background", "#000000", NULL);
366         g_hash_table_insert(default_text_buffer_styles, "pager", pager_tag);
367         text_tag_to_attr_list(pager_tag, priv->pager_attr_list);
368
369         pango_font_description_free(default_font_desc);
370         pango_font_description_free(monospace_font_desc);
371         
372         priv->styles->text_grid = default_text_grid_styles;
373         priv->styles->text_buffer = default_text_buffer_styles;
374
375
376         /* Initialize the GLK styles to empty tags */
377         int i;
378         for(i=0; i<style_NUMSTYLES; i++) {
379                 tag = gtk_text_tag_new(GLK_TAG_NAMES[i]);
380                 g_hash_table_insert(glk_text_grid_styles, (gchar*) GLK_TAG_NAMES[i], tag);
381                 tag = gtk_text_tag_new(GLK_TAG_NAMES[i]);
382                 g_hash_table_insert(glk_text_buffer_styles, (gchar*) GLK_TAG_NAMES[i], tag);
383         }
384
385         priv->glk_styles->text_grid = glk_text_grid_styles;
386         priv->glk_styles->text_buffer = glk_text_buffer_styles;
387
388 }
389
390 /* Reset style tables to the library's internal defaults */
391 void
392 reset_default_styles(ChimaraGlk *glk)
393 {
394         /* TODO: write this function */
395 }
396
397 /* Copy the default styles to the current styles
398  FIXME: This function is temporary and will not be needed later on */
399 void
400 copy_default_styles_to_current_styles(ChimaraGlk *glk)
401 {
402         /*
403         CHIMARA_GLK_USE_PRIVATE(glk, priv);
404         g_hash_table_foreach(priv->styles->text_grid, style_table_copy, priv->glk_styles->text_grid);
405         g_hash_table_foreach(priv->styles->text_buffer, style_table_copy, priv->glk_styles->text_buffer);
406
407         GtkTextTag *pager_tag = GTK_TEXT_TAG( g_hash_table_lookup(priv->styles->text_buffer, "pager") );
408         text_tag_to_attr_list(pager_tag, priv->pager_attr_list);
409         */
410 }
411
412 /* Create the CSS file scanner */
413 GScanner *
414 create_css_file_scanner(void)
415 {
416         GScanner *scanner = g_scanner_new(NULL);
417         scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z "#";
418         scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z "-_" G_CSET_DIGITS;
419         scanner->config->symbol_2_token = TRUE;
420         scanner->config->cpair_comment_single = NULL;
421         scanner->config->scan_float = FALSE;
422         return scanner;
423 }
424
425 /* Run the scanner over the CSS file, overriding the default styles */
426 void
427 scan_css_file(GScanner *scanner, ChimaraGlk *glk)
428 {
429         CHIMARA_GLK_USE_PRIVATE(glk, priv);
430
431         while( g_scanner_peek_next_token(scanner) != G_TOKEN_EOF) {
432                 if( !style_accept_style_selector(scanner, glk) )
433                         break;
434         }
435
436         g_scanner_destroy(scanner);
437
438         /* Update the pager prompt to the new style */
439         GtkTextTag *pager_tag = GTK_TEXT_TAG( g_hash_table_lookup(priv->styles->text_buffer, "pager") );
440         text_tag_to_attr_list(pager_tag, priv->pager_attr_list);
441 }
442
443 /* Internal function: parses a token */
444 static gboolean
445 style_accept(GScanner *scanner, GTokenType token)
446 {
447         GTokenType next = g_scanner_get_next_token(scanner);
448         if(next != token) {
449                 g_scanner_unexp_token(scanner, token, NULL, NULL, NULL, "CSS Error", 1);
450                 return FALSE;
451         }
452         return TRUE;
453 }
454
455 /* Internal function: parses a style selector */
456 static gboolean
457 style_accept_style_selector(GScanner *scanner, ChimaraGlk *glk)
458 {
459         CHIMARA_GLK_USE_PRIVATE(glk, priv);
460
461         GtkTextTag *current_tag;
462         gchar *field;
463         GTokenType token = g_scanner_get_next_token(scanner);
464         GTokenValue value = g_scanner_cur_value(scanner);
465
466         if(
467                 token != G_TOKEN_IDENTIFIER ||
468                 ( strcmp(value.v_identifier, "buffer") && strcmp(value.v_identifier, "grid") )
469         ) {
470                 g_scanner_error(scanner, "CSS Error: buffer/grid expected");
471                 return FALSE;
472         }
473
474         field = g_strdup(value.v_identifier);
475
476         /* Parse the tag name to change */
477         if( g_scanner_peek_next_token(scanner) == '{') {
478                 style_accept(scanner, '{');
479                 if( !strcmp(field, "buffer") )
480                         current_tag = g_hash_table_lookup(priv->styles->text_buffer, "default");
481                 else
482                         current_tag = g_hash_table_lookup(priv->styles->text_grid, "default");
483         } else {
484                 if( !style_accept(scanner, '.') )
485                         return FALSE;
486
487                 token = g_scanner_get_next_token(scanner);
488                 value = g_scanner_cur_value(scanner);
489
490                 if(token != G_TOKEN_IDENTIFIER) {
491                         g_scanner_error(scanner, "CSS Error: style selector expected");
492                         return FALSE;
493                 }
494
495                 if( !strcmp(field, "buffer") )
496                         current_tag = g_hash_table_lookup(priv->styles->text_buffer, value.v_identifier);
497                 else
498                         current_tag = g_hash_table_lookup(priv->styles->text_grid, value.v_identifier);
499
500                 if(current_tag == NULL) {
501                         g_scanner_error(scanner, "CSS Error: invalid style identifier");
502                         return FALSE;
503                 }
504
505                 if( !style_accept(scanner, '{') )
506                         return FALSE;
507         }
508
509         while( g_scanner_peek_next_token(scanner) != '}') {
510                 if( !style_accept_style_hint(scanner, current_tag) )
511                         return FALSE;
512         }
513                 
514         if( !style_accept(scanner, '}') )
515                 return FALSE;
516
517         return TRUE;
518 }
519
520 /* Internal function: parses a style hint */
521 static gboolean
522 style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag)
523 {
524         GTokenType token = g_scanner_get_next_token(scanner);
525         GTokenValue value = g_scanner_cur_value(scanner);
526         gchar *hint;
527
528         if(token != G_TOKEN_IDENTIFIER) {
529                 g_scanner_error(scanner, "CSS Error: style hint expected");
530                 return FALSE;
531         }
532
533         hint = g_strdup(value.v_identifier);
534
535         if( !style_accept(scanner, ':') )
536                 return FALSE;
537
538         token = g_scanner_get_next_token(scanner);
539         value = g_scanner_cur_value(scanner);
540
541         if( !strcmp(hint, "font-family") ) {
542                 if(token != G_TOKEN_STRING) {
543                         g_scanner_error(scanner, "CSS Error: string expected");
544                         return FALSE;
545                 }
546                 g_object_set(current_tag, "family", value.v_string, "family-set", TRUE, NULL);
547         }
548         else if( !strcmp(hint, "font-weight") ) {
549                 if(token != G_TOKEN_IDENTIFIER) {
550                         g_scanner_error(scanner, "CSS Error: bold/normal expected");
551                         return FALSE;
552                 }
553
554                 if( !strcmp(value.v_identifier, "bold") )
555                         g_object_set(current_tag, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
556                 else if( !strcmp(value.v_identifier, "normal") )
557                         g_object_set(current_tag, "weight", PANGO_WEIGHT_NORMAL, "weight-set", TRUE, NULL);
558                 else {
559                         g_scanner_error(scanner, "CSS Error: bold/normal expected");
560                         return FALSE;
561                 }
562         }
563         else if( !strcmp(hint, "font-style") ) {
564                 if(token != G_TOKEN_IDENTIFIER) {
565                         g_scanner_error(scanner, "CSS Error: italic/normal expected");
566                         return FALSE;
567                 }
568
569                 if( !strcmp(value.v_identifier, "italic") )
570                         g_object_set(current_tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
571                 else if( !strcmp(value.v_identifier, "normal") )
572                         g_object_set(current_tag, "style", PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
573                 else {
574                         g_scanner_error(scanner, "CSS Error: italic/normal expected");
575                         return FALSE;
576                 }
577         }
578         else if( !strcmp(hint, "font-size") ) {
579                 if(token == G_TOKEN_INT) 
580                         g_object_set(current_tag, "size-points", (float)value.v_int, "size-set", TRUE, NULL);
581                 else if(token == G_TOKEN_FLOAT)
582                         g_object_set(current_tag, "size-points", value.v_float, "size-set", TRUE, NULL);
583                 else {
584                         g_scanner_error(scanner, "CSS Error: integer or float expected");
585                         return FALSE;
586                 }
587         }
588         else if( !strcmp(hint, "color") ) {
589                 if(token != G_TOKEN_IDENTIFIER) {
590                         g_scanner_error(scanner, "CSS Error: hex color expected");
591                         return FALSE;
592                 }
593
594                 g_object_set(current_tag, "foreground", value.v_identifier, "foreground-set", TRUE, NULL);
595         }
596         else if( !strcmp(hint, "background-color") ) {
597                 if(token != G_TOKEN_IDENTIFIER) {
598                         g_scanner_error(scanner, "CSS Error: hex color expected");
599                         return FALSE;
600                 }
601                 g_object_set(current_tag, "background", value.v_identifier, "background-set", TRUE, NULL);
602         }
603         else if( !strcmp(hint, "text-align") ) {
604                 if(token != G_TOKEN_IDENTIFIER) {
605                         g_scanner_error(scanner, "CSS Error: left/right/center expected");
606                         return FALSE;
607                 }
608                 
609                 if( !strcmp(value.v_identifier, "left") )
610                         g_object_set(current_tag, "justification", GTK_JUSTIFY_LEFT, "justification-set", TRUE, NULL);
611                 else if( !strcmp(value.v_identifier, "right") )
612                         g_object_set(current_tag, "justification", GTK_JUSTIFY_RIGHT, "justification-set", TRUE, NULL);
613                 else if( !strcmp(value.v_identifier, "center") )
614                         g_object_set(current_tag, "justification", GTK_JUSTIFY_CENTER, "justification-set", TRUE, NULL);
615                 else {
616                         g_scanner_error(scanner, "CSS Error: left/right/center expected");
617                         return FALSE;
618                 }
619         }
620         else if( !strcmp(hint, "margin-left") ) {
621                 if(token != G_TOKEN_INT) {
622                         g_scanner_error(scanner, "CSS Error: integer expected");
623                         return FALSE;
624                 }
625                 g_object_set(current_tag, "left-margin", value.v_int, "left-margin-set", TRUE, NULL);
626         }
627         else if( !strcmp(hint, "margin-right") ) {
628                 if(token != G_TOKEN_INT) {
629                         g_scanner_error(scanner, "CSS Error: integer expected");
630                         return FALSE;
631                 }
632                 g_object_set(current_tag, "right-margin", value.v_int, "right-margin-set", TRUE, NULL);
633         }
634         else if( !strcmp(hint, "margin-top") ) {
635                 if(token != G_TOKEN_INT) {
636                         g_scanner_error(scanner, "CSS Error: integer expected");
637                         return FALSE;
638                 }
639                 g_object_set(current_tag, "pixels-above-lines", value.v_int, "pixels-above-lines-set", TRUE, NULL);
640         }
641         else if( !strcmp(hint, "margin-bottom") ) {
642                 if(token != G_TOKEN_INT) {
643                         g_scanner_error(scanner, "CSS Error: integer expected");
644                         return FALSE;
645                 }
646                 g_object_set(current_tag, "pixels-below-lines", value.v_int, "pixels-below-lines-set", TRUE, NULL);
647         }
648                 
649         else {
650                 g_scanner_error(scanner, "CSS Error: invalid style hint %s", hint);
651                 return FALSE;
652         }
653
654         if( !style_accept(scanner, ';') )
655                 return FALSE;
656
657         return TRUE;
658 }
659
660 /* Internal function: parses a glk color to a #hex-value */
661 static void
662 glkcolor_to_hex(glui32 val, gchar *buffer)
663 {
664         g_return_if_fail(buffer != NULL);
665
666         sprintf(buffer, "#%02X%02X%02X",
667                 ((val & 0xff0000) >> 16),
668                 ((val & 0x00ff00) >> 8),
669                 (val & 0x0000ff)
670         );
671 }
672
673 /* Internal function: parses a glk color to a GdkColor */
674 void
675 glkcolor_to_gdkcolor(glui32 val, GdkColor *color)
676 {
677         color->red = 256 * ((val & 0xff0000) >> 16);
678         color->green = 256 * ((val & 0x00ff00) >> 8);
679         color->blue = 256 * (val & 0x0000ff);
680 }
681
682 /* Internal function: parses a GdkColor to a glk color */
683 static glui32
684 gdkcolor_to_glkcolor(GdkColor *color)
685 {
686         g_return_val_if_fail(color != NULL, 0);
687         return (glui32) color->pixel;
688 }
689
690 /* Internal function: changes a GTK tag to correspond with the given style. */
691 static void
692 apply_stylehint_to_tag(GtkTextTag *tag, GtkTextTag *default_tag, glui32 wintype, glui32 hint, glsi32 val)
693 {
694         g_return_if_fail(tag != NULL);
695
696         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
697         GObject *tag_object = G_OBJECT(tag);
698
699         gint reverse_color = GPOINTER_TO_INT( g_object_get_data(tag_object, "reverse-color") );
700
701         int i = 0;
702         gchar color[20];
703         switch(hint) {
704         case stylehint_Indentation:
705                 g_object_set(tag_object, "left-margin", 5*val, "left-margin-set", TRUE, NULL);
706                 g_object_set(tag_object, "right-margin", 5*val, "right-margin-set", TRUE, NULL);
707                 break;
708         
709         case stylehint_ParaIndentation:
710                 g_object_set(tag_object, "indent", 5*val, "indent-set", TRUE, NULL);
711                 break;
712
713         case stylehint_Justification:
714                 switch(val) {
715                         case stylehint_just_LeftFlush:  i = GTK_JUSTIFY_LEFT; break;
716                         case stylehint_just_LeftRight:  i = GTK_JUSTIFY_FILL; break;
717                         case stylehint_just_Centered:   i = GTK_JUSTIFY_CENTER; break;
718                         case stylehint_just_RightFlush: i = GTK_JUSTIFY_RIGHT; break;
719                         default: 
720                                 WARNING("Unknown justification");
721                                 i = GTK_JUSTIFY_LEFT;
722                 }
723                 g_object_set(tag_object, "justification", i, "justification-set", TRUE, NULL);
724                 break;
725
726         case stylehint_Weight:
727                 switch(val) {
728                         case -1: i = PANGO_WEIGHT_LIGHT; break;
729                         case  0: i = PANGO_WEIGHT_NORMAL; break;
730                         case  1: i = PANGO_WEIGHT_BOLD; break;
731                         default: WARNING("Unknown font weight");
732                 }
733                 g_object_set(tag_object, "weight", i, "weight-set", TRUE, NULL);
734                 break;
735
736         case stylehint_Size:
737                 {
738                         gdouble scale = PANGO_SCALE_MEDIUM;
739                         switch(val) {
740                                 case -3: scale = PANGO_SCALE_XX_SMALL; break;
741                                 case -2: scale = PANGO_SCALE_X_SMALL; break;
742                                 case -1: scale = PANGO_SCALE_SMALL; break;
743                                 case  0: scale = PANGO_SCALE_MEDIUM; break;
744                                 case  1: scale = PANGO_SCALE_LARGE; break;
745                                 case  2: scale = PANGO_SCALE_X_LARGE; break;
746                                 case  3: scale = PANGO_SCALE_XX_LARGE; break;
747                                 default:
748                                         /* We follow Pango's convention of having each magnification
749                                         step be a scaling of 1.2 */
750                                         scale = pow(1.2, (double)val);
751                         }
752                         g_printerr("Setting tag to %f\n", scale);
753                         g_object_set(tag_object, "scale", scale, "scale-set", TRUE, NULL);
754                 }
755                 break;
756
757         case stylehint_Oblique:
758                 g_object_set(tag_object, "style", val ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
759                 break;
760
761         case stylehint_Proportional:
762         {
763                 gchar *font_family;
764                 GtkTextTag *font_tag = g_hash_table_lookup(
765                     wintype == wintype_TextBuffer? glk_data->styles->text_buffer : glk_data->styles->text_grid,
766                     val? "default" : "preformatted");
767                 g_object_get(font_tag, "family", &font_family, NULL);
768                 g_object_set(tag_object, "family", font_family, "family-set", TRUE, NULL);
769                 g_free(font_family);
770         }
771                 break;
772
773         case stylehint_TextColor:
774                 glkcolor_to_hex(val, color);
775
776                 if(!reverse_color)
777                         g_object_set(tag_object, "foreground", color, "foreground-set", TRUE, NULL);
778                 else
779                         g_object_set(tag_object, "background", color, "background-set", TRUE, NULL);
780
781                 break;
782
783         case stylehint_BackColor:
784                 glkcolor_to_hex(val, color);
785
786                 if(!reverse_color)
787                         g_object_set(tag_object, "background", color, "background-set", TRUE, NULL);
788                 else
789                         g_object_set(tag_object, "foreground", color, "background-set", TRUE, NULL);
790
791                 break;
792
793         case stylehint_ReverseColor:
794                 if(reverse_color != val) {
795                         /* Flip the fore- and background colors */
796                         GdkColor* foreground_color;
797                         GdkColor* background_color;
798                         gint f_set, b_set, df_set, db_set = 0;
799                         g_object_get(tag_object, "foreground-set", &f_set, "background-set", &b_set, NULL);
800                         g_object_get(default_tag, "foreground-set", &df_set, "background-set", &db_set, NULL);
801
802                         if(f_set)
803                                 g_object_get(tag_object, "foreground-gdk", &foreground_color, NULL);
804                         else if(df_set)
805                                 g_object_get(default_tag, "foreground-gdk", &foreground_color, NULL);
806                         if(b_set)
807                                 g_object_get(tag_object, "background-gdk", &background_color, NULL);
808                         else if(db_set)
809                                 g_object_get(default_tag, "background-gdk", &background_color, NULL);
810
811                         if(b_set || db_set)
812                                 g_object_set(tag_object, "foreground-gdk", background_color, NULL);
813                         else
814                                 g_object_set(tag_object, "foreground", "#ffffff", NULL);
815
816                         if(f_set || df_set)
817                                 g_object_set(tag_object, "background-gdk", foreground_color, NULL);
818                         else
819                                 g_object_set(tag_object, "background", "#000000", NULL);
820
821                         g_object_set_data( tag_object, "reverse-color", GINT_TO_POINTER(val != 0) );
822                 }
823                 break;
824
825         default:
826                 WARNING("Unknown style hint");
827         }
828 }
829
830 /* Internal function: queries a text tag for the value of a given style hint */
831 static gint
832 query_tag(GtkTextTag *tag, glui32 wintype, glui32 hint)
833 {
834         gint intval;
835         gdouble doubleval;
836         GdkColor *colval;
837
838         g_return_val_if_fail(tag != NULL, 0);
839
840         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
841
842         switch(hint) {
843         case stylehint_Indentation:
844                 g_object_get(tag, "left_margin", &intval, NULL);
845                 return intval/5;
846         
847         case stylehint_ParaIndentation:
848                 g_object_get(tag, "indent", &intval, NULL);
849                 return intval/5;
850
851         case stylehint_Justification:
852                 g_object_get(tag, "justification", &intval, NULL);
853                 switch(intval) {
854                         case GTK_JUSTIFY_LEFT: return stylehint_just_LeftFlush;
855                         case GTK_JUSTIFY_FILL: return stylehint_just_LeftRight;
856                         case GTK_JUSTIFY_CENTER: return stylehint_just_Centered;
857                         case GTK_JUSTIFY_RIGHT: return stylehint_just_RightFlush;
858                         default: 
859                                 WARNING("Unknown justification");
860                                 return stylehint_just_LeftFlush;
861                 }
862
863         case stylehint_Weight:
864                 g_object_get(tag, "weight", &intval, NULL);
865                 switch(intval) {
866                         case PANGO_WEIGHT_LIGHT: return -1;
867                         case PANGO_WEIGHT_NORMAL: return 0;
868                         case PANGO_WEIGHT_BOLD: return 1;
869                         default: WARNING("Unknown font weight"); return 0;
870                 }
871
872         case stylehint_Size:
873                 g_object_get(tag, "scale", &doubleval, NULL);
874                 return (gint)round(log(doubleval) / log(1.2));
875
876         case stylehint_Oblique:
877                 g_object_get(tag, "style", &intval , NULL);
878                 return intval == PANGO_STYLE_ITALIC ? 1 : 0;
879
880         case stylehint_Proportional:
881                 /* Use pango_font_family_is_monospace()? */
882         {
883                 gchar *font_family, *query_font_family;
884                 GtkTextTag *font_tag = g_hash_table_lookup(
885                     wintype == wintype_TextBuffer? glk_data->styles->text_buffer : glk_data->styles->text_grid,
886                     "preformatted");
887                 g_object_get(font_tag, "family", &font_family, NULL);
888                 g_object_get(tag, "family", &query_font_family, NULL);
889                 gint retval = strcmp(font_family, query_font_family)? 0 : 1;
890                 g_free(font_family);
891                 g_free(query_font_family);
892                 return retval;
893         }
894
895         case stylehint_TextColor:
896                 g_object_get(tag, "foreground-gdk", &colval, NULL);
897                 return gdkcolor_to_glkcolor(colval);
898
899         case stylehint_BackColor:
900                 g_object_get(tag, "background-gdk", &colval, NULL);
901                 return gdkcolor_to_glkcolor(colval);
902
903         case stylehint_ReverseColor:
904                 return GPOINTER_TO_INT( g_object_get_data(G_OBJECT(tag), "reverse_color") );
905
906         default:
907                 WARNING("Unknown style hint");
908         }
909         
910         return 0;
911 }
912
913 /**
914  * glk_stylehint_set:
915  * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
916  * @styl: The style to set a hint for.
917  * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
918  * @val: The style hint. The meaning of this depends on @hint.
919  *
920  * Sets a hint about the appearance of one style for a particular type of 
921  * window. You can also set @wintype to %wintype_AllTypes, which sets a hint for 
922  * all types of window.
923  * <note><para>
924  *  There is no equivalent constant to set a hint for all styles of a single 
925  *  window type.
926  * </para></note>
927  */
928 void
929 glk_stylehint_set(glui32 wintype, glui32 styl, glui32 hint, glsi32 val)
930 {
931         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
932
933         GtkTextTag *to_change, *default_tag;
934         if(wintype == wintype_TextBuffer || wintype == wintype_AllTypes) {
935                 to_change = g_hash_table_lookup( glk_data->glk_styles->text_buffer, get_glk_tag_name(styl) );
936                 default_tag = g_hash_table_lookup( glk_data->styles->text_buffer, get_tag_name(styl) );
937                 apply_stylehint_to_tag(to_change, default_tag, wintype_TextBuffer, hint, val);
938         }
939
940         if(wintype == wintype_TextGrid || wintype == wintype_AllTypes) {
941                 to_change = g_hash_table_lookup( glk_data->glk_styles->text_grid, get_glk_tag_name(styl) );
942                 default_tag = g_hash_table_lookup( glk_data->styles->text_grid, get_tag_name(styl) );
943                 apply_stylehint_to_tag(to_change, default_tag, wintype_TextGrid, hint, val);
944         }
945 }
946
947 /**
948  * glk_stylehint_clear:
949  * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
950  * @styl: The style to set a hint for.
951  * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
952  *
953  * Clears a hint about the appearance of one style for a particular type of 
954  * window to its default value. You can also set @wintype to %wintype_AllTypes, 
955  * which clears a hint for all types of window.
956  * <note><para>
957  *  There is no equivalent constant to reset a hint for all styles of a single 
958  *  window type.
959  * </para></note>
960  */
961 void
962 glk_stylehint_clear(glui32 wintype, glui32 styl, glui32 hint)
963 {
964         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
965         GtkTextTag *tag;
966
967         switch(wintype) {
968         case wintype_TextBuffer:
969                 tag = g_hash_table_lookup( glk_data->glk_styles->text_buffer, get_glk_tag_name(styl) );
970                 if(tag) {
971                         glk_stylehint_set( wintype, styl, hint, query_tag(tag, wintype, hint) );
972                 }
973                 break;
974         case wintype_TextGrid:
975                 tag = g_hash_table_lookup( glk_data->glk_styles->text_grid, get_glk_tag_name(styl) );
976                 if(tag) {
977                         glk_stylehint_set( wintype, styl, hint, query_tag(tag, wintype, hint) );
978                 }
979         default:
980                 return;
981         }
982 }
983
984 /**
985  * glk_style_distinguish:
986  * @win: The window in which the styles are to be distinguished.
987  * @styl1: The first style to be distinguished from the second style.
988  * @styl2: The second style to be distinguished from the first style.
989  * 
990  * Decides whether two styles are visually distinguishable in the given window.
991  * The exact meaning of this is left for the library to determine.
992  * <note><title>Chimara</title><para>
993  *   Currently, all styles of one window are assumed to be mutually
994  *   distinguishable.
995  * </para></note>
996  * 
997  * Returns: %TRUE (1) if the two styles are visually distinguishable. If they 
998  * are not, it returns %FALSE (0).
999  */
1000 glui32
1001 glk_style_distinguish(winid_t win, glui32 styl1, glui32 styl2)
1002 {
1003         /* FIXME */
1004         return styl1 != styl2;
1005 }
1006
1007 /**
1008  * glk_style_measure:
1009  * @win: The window from which to take the style.
1010  * @styl: The style to perform the measurement on.
1011  * @hint: The stylehint to measure.
1012  * @result: Address to write the result to.
1013  * 
1014  * Tries to test an attribute of one style in the given window @win. The library
1015  * may not be able to determine the attribute; if not, this returns %FALSE (0).
1016  * If it can, it returns %TRUE (1) and stores the value in the location pointed
1017  * at by @result. 
1018  * <note><para>
1019  *   As usual, it is legal for @result to be %NULL, although fairly pointless.
1020  * </para></note>
1021  *
1022  * The meaning of the value depends on the hint which was tested:
1023  * <variablelist>
1024  * <varlistentry>
1025  *   <term>%stylehint_Indentation, %stylehint_ParaIndentation</term>
1026  *   <listitem><para>The indentation and paragraph indentation. These are in a
1027  *   metric which is platform-dependent.</para>
1028  *   <note><para>Most likely either characters or pixels.</para></note>
1029  *   </listitem>
1030  * </varlistentry>
1031  * <varlistentry>
1032  *   <term>%stylehint_Justification</term>
1033  *   <listitem><para>One of the constants %stylehint_just_LeftFlush,
1034  *   %stylehint_just_LeftRight, %stylehint_just_Centered, or
1035  *   %stylehint_just_RightFlush.</para></listitem>
1036  * </varlistentry>
1037  * <varlistentry>
1038  *   <term>%stylehint_Size</term>
1039  *   <listitem><para>The font size. Again, this is in a platform-dependent
1040  *   metric.</para>
1041  *   <note><para>Pixels, points, or simply 1 if the library does not support
1042  *   varying font sizes.</para></note>
1043  *   </listitem>
1044  * </varlistentry>
1045  * <varlistentry>
1046  *   <term>%stylehint_Weight</term>
1047  *   <listitem><para>1 for heavy-weight fonts (boldface), 0 for normal weight,
1048  *   and -1 for light-weight fonts.</para></listitem>
1049  * </varlistentry>
1050  * <varlistentry>
1051  *   <term>%stylehint_Oblique</term>
1052  *   <listitem><para>1 for oblique fonts (italic), or 0 for normal angle.</para>
1053  *   </listitem>
1054  * </varlistentry>
1055  * <varlistentry>
1056  *   <term>%stylehint_Proportional</term>
1057  *   <listitem><para>1 for proportional-width fonts, or 0 for fixed-width.
1058  *   </para></listitem>
1059  * </varlistentry>
1060  * <varlistentry>
1061  *   <term>%stylehint_TextColor, %stylehint_BackColor</term>
1062  *   <listitem><para>These are values from 0x00000000 to 0x00FFFFFF, encoded as
1063  *   described in <link 
1064  *   linkend="chimara-Suggesting-the-Appearance-of-Styles">Suggesting the
1065  *   Appearance of Styles</link>.</para></listitem>
1066  * </varlistentry>
1067  * <varlistentry>
1068  *   <term>%stylehint_ReverseColor</term>
1069  *   <listitem><para>0 for normal printing, 1 if the foreground and background
1070  *   colors are reversed.</para></listitem>
1071  * </varlistentry>
1072  * </variablelist>
1073  * 
1074  * Returns: TRUE upon successul retrieval, otherwise FALSE.
1075  */
1076 glui32
1077 glk_style_measure(winid_t win, glui32 styl, glui32 hint, glui32 *result)
1078 {
1079         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
1080         GtkTextTag *tag;
1081
1082         switch(win->type) {
1083         case wintype_TextBuffer:
1084                 tag = g_hash_table_lookup( glk_data->glk_styles->text_buffer, get_glk_tag_name(styl) );
1085                 if(result)
1086                         *result = query_tag(tag, win->type, hint);
1087                 break;
1088         case wintype_TextGrid:
1089                 tag = g_hash_table_lookup( glk_data->glk_styles->text_grid, get_glk_tag_name(styl) );
1090                 if(result)
1091                         *result = query_tag(tag, win->type, hint);
1092         default:
1093                 return FALSE;
1094         }
1095
1096         return TRUE;
1097 }
1098
1099 /* Internal function returning the current default font for a window type
1100  * This can be used later for size calculations. Only wintype_TextGrid and wintype_TextBuffer are
1101  * supported for now */
1102 PangoFontDescription *
1103 get_current_font(guint32 wintype)
1104 {
1105         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
1106         GtkTextTag *tag;
1107
1108         switch(wintype) {
1109         case wintype_TextGrid:
1110                 tag = g_hash_table_lookup(glk_data->styles->text_grid, "default");
1111                 break;
1112         case wintype_TextBuffer:
1113                 tag = g_hash_table_lookup(glk_data->styles->text_buffer, "default");
1114                 break;
1115         default:
1116                 return NULL;
1117         }
1118
1119         PangoFontDescription *font;
1120         g_object_get( G_OBJECT(tag), "font-desc", &font, NULL );
1121
1122         return font;
1123 }
1124
1125 /* Internal function copying the attributes of a text tag to a pango attribute list */
1126 static void
1127 text_tag_to_attr_list(GtkTextTag *tag, PangoAttrList *list)
1128 {
1129         gboolean set;
1130         GdkColor *foreground, *background;
1131         gchar *string;
1132         PangoFontDescription *font_desc;
1133         gboolean strikethrough;
1134         PangoUnderline underline;
1135
1136         g_object_get(tag, "foreground-set", &set, "foreground-gdk", &foreground, NULL);
1137         if(set) {
1138                 pango_attr_list_insert(
1139                         list,
1140                         pango_attr_foreground_new(foreground->red, foreground->green, foreground->blue)
1141                 );
1142         }
1143         g_object_get(tag, "background-set", &set, "background-gdk", &background, NULL);
1144         if(set) {
1145                 pango_attr_list_insert(
1146                         list,
1147                         pango_attr_background_new(background->red, background->green, background->blue)
1148                 );
1149         }
1150         g_object_get(tag, "language-set", &set, "language", &string, NULL);
1151         if(set) {
1152                 pango_attr_list_insert(
1153                         list,
1154                         pango_attr_language_new( pango_language_from_string(string) )
1155                 );
1156         }
1157
1158         /* Font description updates the following properties simultaniously:
1159          * family, style, weight, variant, stretch, size
1160          */
1161         g_object_get(tag, "font-desc", &font_desc, NULL);
1162         pango_attr_list_insert(
1163                 list,
1164                 pango_attr_font_desc_new(font_desc)
1165         );
1166
1167         g_object_get(tag, "strikethrough-set", &set, "strikethrough", &strikethrough, NULL);
1168         if(set) {
1169                 pango_attr_list_insert(
1170                         list,
1171                         pango_attr_strikethrough_new(strikethrough)
1172                 );
1173         }
1174         g_object_get(tag, "underline-set", &set, "underline", &underline, NULL);
1175         if(set) {
1176                 pango_attr_list_insert(
1177                         list,
1178                         pango_attr_underline_new(underline)
1179                 );
1180         }
1181 }
1182
1183 /* Update pager and reverse video tags */
1184 void
1185 style_update(ChimaraGlk *glk)
1186 {
1187         CHIMARA_GLK_USE_PRIVATE(glk, priv);
1188
1189         GtkTextTag *pager_tag = GTK_TEXT_TAG( g_hash_table_lookup(priv->styles->text_buffer, "pager") );
1190         text_tag_to_attr_list(pager_tag, priv->pager_attr_list);
1191 }