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