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