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