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