1b13e61e7ebbd4fbf77f24c17bf92a9478602e7e
[rodin/chimara.git] / libchimara / style.c
1 #include "style.h"
2 #include <stdio.h>
3 #include <fcntl.h>
4
5 extern GPrivate *glk_data_key;
6 static gboolean chimara_style_initialized = FALSE;
7
8 static gboolean style_accept(GScanner *scanner, GTokenType token);
9 static gboolean style_accept_style_selector(GScanner *scanner);
10 static gboolean style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag);
11 static void style_add_tag_to_textbuffer(gpointer key, gpointer tag, gpointer tag_table);
12 static void style_table_copy(gpointer key, gpointer tag, gpointer target_table);
13 static GtkTextTag* gtk_text_tag_copy(GtkTextTag *tag);
14
15 /**
16  * glk_set_style:
17  * @styl: The style to apply
18  *
19  * Changes the style of the current output stream. @styl should be one of the
20  * <code>style_</code> constants listed above. However, any value is actually
21  * legal; if the interpreter does not recognize the style value, it will treat
22  * it as %style_Normal.
23  * <note><para>
24  *  This policy allows for the future definition of styles without breaking old
25  *  Glk libraries.
26  * </para></note>
27  */
28 void
29 glk_set_style(glui32 styl)
30 {
31         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
32         g_return_if_fail(glk_data->current_stream != NULL);
33         glk_set_style_stream(glk_data->current_stream, styl);
34 }
35
36 static const gchar* TAG_NAMES[] = {
37         "normal",
38         "emphasized",
39         "preformatted",
40         "header",
41         "subheader",
42         "alert",
43         "note",
44         "block-quote",
45         "input",
46         "user1",
47         "user2"
48 };
49
50 /* Internal function: mapping from style enum to tag name */
51 static gchar*
52 get_tag_name(glui32 style)
53 {
54         if(style >= style_NUMSTYLES) {
55                 WARNING("Unsupported style");
56                 return "normal";
57         } else {
58                 return (gchar*) TAG_NAMES[style];
59         }
60 }
61
62 /** 
63  * glk_set_style_stream:
64  * @str: Output stream to change the style of
65  * @styl: The style to apply
66  *
67  * This changes the style of the stream @str. See glk_set_style().
68  */
69 void
70 glk_set_style_stream(strid_t str, glui32 styl) {
71         str->style = get_tag_name(styl);
72 }
73
74 /* Internal function: call this to initialize the default styles to a textbuffer. */
75 void
76 style_init_textbuffer(GtkTextBuffer *buffer)
77 {
78         g_return_if_fail(buffer != NULL);
79
80         if( G_UNLIKELY(!chimara_style_initialized) ) {
81                 style_init();
82         }
83
84         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
85         g_hash_table_foreach(glk_data->current_styles->text_buffer, style_add_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
86 }
87
88 /* Internal function: call this to initialize the default styles to a textgrid. */
89 void
90 style_init_textgrid(GtkTextBuffer *buffer)
91 {
92         g_return_if_fail(buffer != NULL);
93
94         if( G_UNLIKELY(!chimara_style_initialized) ) {
95                 style_init();
96         }
97
98         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
99         g_hash_table_foreach(glk_data->current_styles->text_grid, style_add_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
100 }
101
102 /* Internal function used to iterate over the default text tag table, applying them to a textbuffer */
103 static void
104 style_add_tag_to_textbuffer(gpointer key, gpointer tag, gpointer tag_table)
105 {
106         gtk_text_tag_table_add( tag_table, gtk_text_tag_copy(tag) );
107 }
108
109 /* Internal function used to iterate over a style table, copying it */
110 static void
111 style_table_copy(gpointer key, gpointer tag, gpointer target_table)
112 {
113         g_return_if_fail(key != NULL);
114         g_return_if_fail(tag != NULL);
115         g_return_if_fail(target_table != NULL);
116
117         g_hash_table_insert(target_table, key, gtk_text_tag_copy( GTK_TEXT_TAG(tag) ));
118 }
119
120 /* Internal function that copies a text tag */
121 static GtkTextTag*
122 gtk_text_tag_copy(GtkTextTag *tag)
123 {
124         GtkTextTag *copy;
125
126         g_return_val_if_fail(tag != NULL, NULL);
127
128         copy = gtk_text_tag_new(tag->name);
129         gtk_text_attributes_copy_values(tag->values, copy->values);
130
131         #define _COPY_FLAG(flag) copy->flag = tag->flag
132                 _COPY_FLAG (bg_color_set);
133                 _COPY_FLAG (bg_color_set);
134                 _COPY_FLAG (bg_stipple_set);
135                 _COPY_FLAG (fg_color_set);
136                 _COPY_FLAG (fg_stipple_set);
137                 _COPY_FLAG (justification_set);
138                 _COPY_FLAG (left_margin_set);
139                 _COPY_FLAG (indent_set);
140                 _COPY_FLAG (rise_set);
141                 _COPY_FLAG (strikethrough_set);
142                 _COPY_FLAG (right_margin_set);
143                 _COPY_FLAG (pixels_above_lines_set);
144                 _COPY_FLAG (pixels_below_lines_set);
145                 _COPY_FLAG (pixels_inside_wrap_set);
146                 _COPY_FLAG (tabs_set);
147                 _COPY_FLAG (underline_set);
148                 _COPY_FLAG (wrap_mode_set);
149                 _COPY_FLAG (bg_full_height_set);
150                 _COPY_FLAG (invisible_set);
151                 _COPY_FLAG (editable_set);
152                 _COPY_FLAG (language_set);
153         #undef _COPY_FLAG
154
155         return copy;
156 }
157     
158 /* Internal function that reads the default styles from a CSS file */
159 void
160 style_init()
161 {
162         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
163         GHashTable *default_text_grid_styles = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_object_unref);
164         GHashTable *default_text_buffer_styles = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_object_unref);
165         GHashTable *current_text_grid_styles = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_object_unref);
166         GHashTable *current_text_buffer_styles = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_object_unref);
167         GtkTextTag *tag;
168
169         /* Create the CSS file scanner */
170         GScanner *scanner = g_scanner_new(NULL);
171
172         int f = open(glk_data->css_file, O_RDONLY);
173         g_return_if_fail(f != -1);
174         g_scanner_input_file(scanner, f);
175         scanner->input_name = glk_data->css_file;
176         scanner->config->cset_identifier_first = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ#";
177         scanner->config->cset_identifier_nth = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_0123456789";
178         scanner->config->symbol_2_token = TRUE;
179         scanner->config->cpair_comment_single = NULL;
180         scanner->config->scan_float = FALSE;
181
182         /* Initialise the default styles */
183         g_hash_table_insert(default_text_grid_styles, "normal", gtk_text_tag_new("normal"));
184
185         tag = gtk_text_tag_new("emphasized");
186         g_object_set(tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
187         g_hash_table_insert(default_text_grid_styles, "emphasized", tag);
188
189         tag = gtk_text_tag_new("preformatted");
190         g_object_set(tag, "font-desc", glk_data->monospace_font_desc, NULL);
191         g_hash_table_insert(default_text_grid_styles, "preformatted", tag);
192
193         tag = gtk_text_tag_new("header");
194         g_object_set(tag, "size-points", 18.0, "weight", PANGO_WEIGHT_BOLD, NULL);
195         g_hash_table_insert(default_text_grid_styles, "header", tag);
196
197         tag = gtk_text_tag_new("subheader");
198         g_object_set(tag, "size-points", 14.0, "weight", PANGO_WEIGHT_BOLD, NULL);
199         g_hash_table_insert(default_text_grid_styles, "subheader", tag);
200
201         tag = gtk_text_tag_new("alert");
202         g_object_set(tag, "foreground", "#aa0000", "weight", PANGO_WEIGHT_BOLD, NULL);
203         g_hash_table_insert(default_text_grid_styles, "alert", tag);
204
205         tag = gtk_text_tag_new("note");
206         g_object_set(tag, "foreground", "#aaaa00", "weight", PANGO_WEIGHT_BOLD, NULL);
207         g_hash_table_insert(default_text_grid_styles, "note", tag);
208
209         tag = gtk_text_tag_new("block-quote");
210         g_object_set(tag, "justification", GTK_JUSTIFY_CENTER, "style", PANGO_STYLE_ITALIC, NULL);
211         g_hash_table_insert(default_text_grid_styles, "block-quote", tag);
212
213         g_hash_table_insert(default_text_grid_styles, "input", gtk_text_tag_new("input"));
214         g_hash_table_insert(default_text_grid_styles, "user1", gtk_text_tag_new("user1"));
215         g_hash_table_insert(default_text_grid_styles, "user2", gtk_text_tag_new("user2"));
216
217         g_hash_table_foreach(default_text_grid_styles, style_table_copy, default_text_buffer_styles);
218         glk_data->default_styles->text_grid = default_text_grid_styles;
219         glk_data->default_styles->text_buffer = default_text_buffer_styles;
220
221         /* Run the scanner over the CSS file, overriding defaults */
222         while( g_scanner_peek_next_token(scanner) != G_TOKEN_EOF) {
223                 if( !style_accept_style_selector(scanner) )
224                         break;
225         }
226
227         /* Set the current style to a copy of the default style */
228         g_hash_table_foreach(default_text_grid_styles, style_table_copy, current_text_grid_styles);
229         g_hash_table_foreach(default_text_buffer_styles, style_table_copy, current_text_buffer_styles);
230         glk_data->current_styles->text_grid = current_text_grid_styles;
231         glk_data->current_styles->text_buffer = current_text_buffer_styles;
232
233         g_scanner_destroy(scanner);
234
235         chimara_style_initialized = TRUE;
236 }
237
238 /* Internal function: parses a token */
239 static gboolean
240 style_accept(GScanner *scanner, GTokenType token)
241 {
242         GTokenType next = g_scanner_get_next_token(scanner);
243         if(next != token ) {
244                 g_scanner_unexp_token(scanner, token, NULL, NULL, NULL, "CSS Error", 1);
245                 return FALSE;
246         } else {
247                 return TRUE;
248         }
249 }
250
251 /* Internal function: parses a style selector */
252 static gboolean
253 style_accept_style_selector(GScanner *scanner)
254 {
255         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
256
257         GtkTextTag *current_tag;
258         gchar *field;
259         GTokenType token = g_scanner_get_next_token(scanner);
260         GTokenValue value = g_scanner_cur_value(scanner);
261
262         if(
263                 token != G_TOKEN_IDENTIFIER ||
264                 ( strcmp(value.v_identifier, "buffer") && strcmp(value.v_identifier, "grid") )
265         ) {
266                 g_scanner_error(scanner, "CSS Error: buffer/grid expected");
267                 return FALSE;
268         }
269
270         field = g_strdup(value.v_identifier);
271
272         if( !style_accept(scanner, '.') )
273                 return FALSE;
274
275         token = g_scanner_get_next_token(scanner);
276         value = g_scanner_cur_value(scanner);
277
278         if(token != G_TOKEN_IDENTIFIER) {
279                 g_scanner_error(scanner, "CSS Error: style selector expected");
280                 return FALSE;
281         }
282
283         if( !strcmp(field, "buffer") )
284                 current_tag = g_hash_table_lookup(glk_data->default_styles->text_buffer, value.v_identifier);
285         else
286                 current_tag = g_hash_table_lookup(glk_data->default_styles->text_grid, value.v_identifier);
287
288         if(current_tag == NULL) {
289                 g_scanner_error(scanner, "CSS Error: invalid style identifier");
290                 return FALSE;
291         }
292
293         if( !style_accept(scanner, '{') )
294                 return FALSE;
295
296         while( g_scanner_peek_next_token(scanner) != '}') {
297                 if( !style_accept_style_hint(scanner, current_tag) )
298                         return FALSE;
299         }
300                 
301         if( !style_accept(scanner, '}') )
302                 return FALSE;
303
304         return TRUE;
305 }
306
307 /* Internal function: parses a style hint */
308 static gboolean
309 style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag)
310 {
311         GTokenType token = g_scanner_get_next_token(scanner);
312         GTokenValue value = g_scanner_cur_value(scanner);
313         gchar *hint;
314
315         if(token != G_TOKEN_IDENTIFIER) {
316                 g_scanner_error(scanner, "CSS Error: style hint expected");
317                 return FALSE;
318         }
319
320         hint = g_strdup(value.v_identifier);
321
322         if( !style_accept(scanner, ':') )
323                 return FALSE;
324
325         token = g_scanner_get_next_token(scanner);
326         value = g_scanner_cur_value(scanner);
327
328         if( !strcmp(hint, "font-family") ) {
329                 if(token != G_TOKEN_STRING) {
330                         g_scanner_error(scanner, "CSS Error: string expected");
331                         return FALSE;
332                 }
333                 g_object_set(current_tag, "family", value.v_string, "family-set", TRUE, NULL);
334         }
335         else if( !strcmp(hint, "font-weight") ) {
336                 if(token != G_TOKEN_IDENTIFIER) {
337                         g_scanner_error(scanner, "CSS Error: bold/normal expected");
338                         return FALSE;
339                 }
340
341                 if( !strcmp(value.v_identifier, "bold") )
342                         g_object_set(current_tag, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
343                 else if( !strcmp(value.v_identifier, "normal") )
344                         g_object_set(current_tag, "weight", PANGO_WEIGHT_NORMAL, "weight-set", TRUE, NULL);
345                 else {
346                         g_scanner_error(scanner, "CSS Error: bold/normal expected");
347                         return FALSE;
348                 }
349         }
350         else if( !strcmp(hint, "font-style") ) {
351                 if(token != G_TOKEN_IDENTIFIER) {
352                         g_scanner_error(scanner, "CSS Error: italic/normal expected");
353                         return FALSE;
354                 }
355
356                 if( !strcmp(value.v_identifier, "italic") )
357                         g_object_set(current_tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
358                 else if( !strcmp(value.v_identifier, "normal") )
359                         g_object_set(current_tag, "style", PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
360                 else {
361                         g_scanner_error(scanner, "CSS Error: italic/normal expected");
362                         return FALSE;
363                 }
364         }
365         else if( !strcmp(hint, "font-size") ) {
366                 if(token == G_TOKEN_INT) 
367                         g_object_set(current_tag, "size-points", (float)value.v_int, "size-set", TRUE, NULL);
368                 else if(token == G_TOKEN_FLOAT)
369                         g_object_set(current_tag, "size-points", value.v_float, "size-set", TRUE, NULL);
370                 else {
371                         g_scanner_error(scanner, "CSS Error: integer or float expected");
372                         return FALSE;
373                 }
374         }
375         else if( !strcmp(hint, "color") ) {
376                 if(token != G_TOKEN_IDENTIFIER) {
377                         g_scanner_error(scanner, "CSS Error: hex color expected");
378                         return FALSE;
379                 }
380
381                 g_object_set(current_tag, "foreground", value.v_identifier, "foreground-set", TRUE, NULL);
382         }
383         else if( !strcmp(hint, "background-color") ) {
384                 if(token != G_TOKEN_IDENTIFIER) {
385                         g_scanner_error(scanner, "CSS Error: hex color expected");
386                         return FALSE;
387                 }
388                 g_object_set(current_tag, "background", value.v_identifier, "background-set", TRUE, NULL);
389         }
390         else if( !strcmp(hint, "text-align") ) {
391                 if(token != G_TOKEN_IDENTIFIER) {
392                         g_scanner_error(scanner, "CSS Error: left/right/center expected");
393                         return FALSE;
394                 }
395                 
396                 if( !strcmp(value.v_identifier, "left") )
397                         g_object_set(current_tag, "justification", GTK_JUSTIFY_LEFT, "justification-set", TRUE, NULL);
398                 else if( !strcmp(value.v_identifier, "right") )
399                         g_object_set(current_tag, "justification", GTK_JUSTIFY_RIGHT, "justification-set", TRUE, NULL);
400                 else if( !strcmp(value.v_identifier, "center") )
401                         g_object_set(current_tag, "justification", GTK_JUSTIFY_CENTER, "justification-set", TRUE, NULL);
402                 else {
403                         g_scanner_error(scanner, "CSS Error: left/right/center expected");
404                         return FALSE;
405                 }
406         }
407         else if( !strcmp(hint, "margin-left") ) {
408                 if(token != G_TOKEN_INT) {
409                         g_scanner_error(scanner, "CSS Error: integer expected");
410                         return FALSE;
411                 }
412                 g_object_set(current_tag, "left-margin", value.v_int, "left-margin-set", TRUE, NULL);
413         }
414         else if( !strcmp(hint, "margin-right") ) {
415                 if(token != G_TOKEN_INT) {
416                         g_scanner_error(scanner, "CSS Error: integer expected");
417                         return FALSE;
418                 }
419                 g_object_set(current_tag, "right-margin", value.v_int, "right-margin-set", TRUE, NULL);
420         }
421         else if( !strcmp(hint, "margin-top") ) {
422                 if(token != G_TOKEN_INT) {
423                         g_scanner_error(scanner, "CSS Error: integer expected");
424                         return FALSE;
425                 }
426                 g_object_set(current_tag, "pixels-above-lines", value.v_int, "pixels-above-lines-set", TRUE, NULL);
427         }
428         else if( !strcmp(hint, "margin-bottom") ) {
429                 if(token != G_TOKEN_INT) {
430                         g_scanner_error(scanner, "CSS Error: integer expected");
431                         return FALSE;
432                 }
433                 g_object_set(current_tag, "pixels-below-lines", value.v_int, "pixels-below-lines-set", TRUE, NULL);
434         }
435                 
436         else {
437                 g_scanner_error(scanner, "CSS Error: invalid style hint %s", hint);
438                 return FALSE;
439         }
440
441         if( !style_accept(scanner, ';') )
442                 return FALSE;
443
444         return TRUE;
445 }
446
447 /* Internal function: parses a glk color to a #hex-value */
448 static void
449 color_format(glui32 val, gchar *buffer)
450 {
451         g_return_if_fail(buffer != NULL);
452
453         sprintf(buffer, "#%02X%02X%02X",
454                 ((val & 0xff0000) >> 16),
455                 ((val & 0x00ff00) >> 8),
456                 (val & 0x0000ff)
457         );
458 }
459
460 /* Internal function: parses a GdkColor to a glk color */
461 static glui32
462 color_parse_gdk(GdkColor *color)
463 {
464         g_return_val_if_fail(color != NULL, 0);
465         return (glui32) color->pixel;
466 }
467
468 /* Internal function: changes a GTK tag to correspond with the given style. */
469 static void
470 apply_stylehint_to_tag(GtkTextTag *tag, glui32 hint, glsi32 val)
471 {
472         g_return_if_fail(tag != NULL);
473
474         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
475         GObject *tag_object = G_OBJECT(tag);
476         gint reverse_color = 0;
477
478         /* FIXME where should we keep track of this?
479         g_object_get(tag, "reverse_color", &reverse_color, NULL);
480         */
481
482         int i = 0;
483         gchar color[20];
484         switch(hint) {
485         case stylehint_Indentation:
486                 g_object_set(tag_object, "left-margin", 5*val, "left-margin-set", TRUE, NULL);
487                 g_object_set(tag_object, "right-margin", 5*val, "right-margin-set", TRUE, NULL);
488                 break;
489         
490         case stylehint_ParaIndentation:
491                 g_object_set(tag_object, "indent", 5*val, "indent-set", TRUE, NULL);
492                 break;
493
494         case stylehint_Justification:
495                 switch(val) {
496                         case stylehint_just_LeftFlush:  i = GTK_JUSTIFY_LEFT; break;
497                         case stylehint_just_LeftRight:  i = GTK_JUSTIFY_FILL; break;
498                         case stylehint_just_Centered:   i = GTK_JUSTIFY_CENTER; break;
499                         case stylehint_just_RightFlush: i = GTK_JUSTIFY_RIGHT; break;
500                         default: 
501                                 WARNING("Unknown justification");
502                                 i = GTK_JUSTIFY_LEFT;
503                 }
504                 g_object_set(tag_object, "justification", i, "justification-set", TRUE, NULL);
505                 break;
506
507         case stylehint_Weight:
508                 switch(val) {
509                         case -1: i = PANGO_WEIGHT_LIGHT; break;
510                         case  0: i = PANGO_WEIGHT_NORMAL; break;
511                         case  1: i = PANGO_WEIGHT_BOLD; break;
512                         default: WARNING("Unknown font weight");
513                 }
514                 g_object_set(tag_object, "weight", i, "weight-set", TRUE, NULL);
515                 break;
516
517         case stylehint_Size:
518                 g_object_set(tag_object, "size", 14+(2*val), "size-set", TRUE, NULL);
519                 break;
520
521         case stylehint_Oblique:
522                 g_object_set(tag_object, "style", val ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
523                 break;
524
525         case stylehint_Proportional:
526                 g_object_set(tag_object, "font-desc", val ? glk_data->default_font_desc : glk_data->monospace_font_desc, NULL);
527                 break;
528
529         case stylehint_TextColor:
530                 color_format(val, color);
531
532                 if(!reverse_color)
533                         g_object_set(tag_object, "foreground", color, "foreground-set", TRUE, NULL);
534                 else
535                         g_object_set(tag_object, "background", color, "background-set", TRUE, NULL);
536
537                 break;
538
539         case stylehint_BackColor:
540                 color_format(val, color);
541
542                 if(!reverse_color)
543                         g_object_set(tag_object, "background", color, "background-set", TRUE, NULL);
544                 else
545                         g_object_set(tag_object, "foreground", color, "background-set", TRUE, NULL);
546
547                 break;
548
549         case stylehint_ReverseColor:
550                 if(reverse_color != val) {
551                         /* Flip the fore- and background colors */
552                         GdkColor* foreground_color;
553                         GdkColor* background_color;
554                         gint f_set, b_set = 0;
555                         g_object_get(tag_object, "foreground-set", &f_set, "background-set", &b_set, NULL);
556
557                         if(f_set)
558                                 g_object_get(tag_object, "foreground-gdk", &foreground_color, NULL);
559                         if(b_set)
560                                 g_object_get(tag_object, "background-gdk", &background_color, NULL);
561
562                         if(b_set)
563                                 g_object_set(tag_object, "foreground-gdk", background_color, NULL);
564                         else
565                                 g_object_set(tag_object, "foreground", "#ffffff", NULL);
566
567                         if(f_set)
568                                 g_object_set(tag_object, "background-gdk", foreground_color, NULL);
569                         else
570                                 g_object_set(tag_object, "background", "#000000", NULL);
571                 }
572                 break;
573
574         default:
575                 WARNING("Unknown style hint");
576         }
577 }
578 /*Internal function: queries a text tag for the value of a given style hint */
579 static gint
580 query_tag(GtkTextTag *tag, glui32 hint)
581 {
582         gint intval;
583         GObject *objval;
584         GdkColor *colval;
585
586         g_return_val_if_fail(tag != NULL, 0);
587
588         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
589
590         switch(hint) {
591         case stylehint_Indentation:
592                 g_object_get(tag, "left_margin", &intval, NULL);
593                 return intval/5;
594                 break;
595         
596         case stylehint_ParaIndentation:
597                 g_object_get(tag, "indent", &intval, NULL);
598                 return intval/5;
599                 break;
600
601         case stylehint_Justification:
602                 g_object_get(tag, "justification", &intval, NULL);
603                 switch(intval) {
604                         case GTK_JUSTIFY_LEFT: return stylehint_just_LeftFlush; break;
605                         case GTK_JUSTIFY_FILL: return stylehint_just_LeftRight; break;
606                         case GTK_JUSTIFY_CENTER: return stylehint_just_Centered; break;
607                         case GTK_JUSTIFY_RIGHT: return stylehint_just_RightFlush; break;
608                         default: 
609                                 WARNING("Unknown justification");
610                                 return stylehint_just_LeftFlush;
611                 }
612                 break;
613
614         case stylehint_Weight:
615                 g_object_get(tag, "weight", &intval, NULL);
616                 switch(intval) {
617                         case PANGO_WEIGHT_LIGHT: return -1; break;
618                         case PANGO_WEIGHT_NORMAL: return 0; break;
619                         case PANGO_WEIGHT_BOLD: return 1; break;
620                         default: WARNING("Unknown font weight"); return 0;
621                 }
622                 break;
623
624         case stylehint_Size:
625                 g_object_get(tag, "size", &intval, NULL);
626                 return (intval/2)-14;
627                 break;
628
629         case stylehint_Oblique:
630                 g_object_get(tag, "style", &intval , NULL);
631                 return intval == PANGO_STYLE_ITALIC ? 1 : 0;
632                 break;
633
634         case stylehint_Proportional:
635                 g_object_get(tag, "font-desc", &objval, NULL);
636                 return objval == (GObject *)glk_data->monospace_font_desc ? 0 : 1;
637                 break;
638
639         case stylehint_TextColor:
640                 g_object_get(tag, "foreground-gdk", &colval, NULL);
641                 return color_parse_gdk(colval);
642                 break;
643
644         case stylehint_BackColor:
645                 g_object_get(tag, "background-gdk", &colval, NULL);
646                 return color_parse_gdk(colval);
647                 break;
648
649         case stylehint_ReverseColor:
650                 /* FIXME: implement this */
651                 return 0;
652                 break;
653
654         default:
655                 WARNING("Unknown style hint");
656         }
657         
658         return 0;
659 }
660
661 /**
662  * glk_stylehint_set:
663  * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
664  * @styl: The style to set a hint for.
665  * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
666  * @val: The style hint. The meaning of this depends on @hint.
667  *
668  * Sets a hint about the appearance of one style for a particular type of 
669  * window. You can also set wintype to %wintype_AllTypes, which sets a hint for 
670  * all types of window.
671  * <note><para>
672  *  There is no equivalent constant to set a hint for all styles of a single 
673  *  window type.
674  * </para></note>
675  */
676 void
677 glk_stylehint_set(glui32 wintype, glui32 styl, glui32 hint, glsi32 val)
678 {
679         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
680
681         if( G_UNLIKELY(!chimara_style_initialized) ) {
682                 style_init();
683         }
684
685         GtkTextTag *to_change;
686         if(wintype == wintype_TextBuffer || wintype == wintype_AllTypes) {
687                 to_change = g_hash_table_lookup( glk_data->current_styles->text_buffer, get_tag_name(styl) );
688                 apply_stylehint_to_tag(to_change, hint, val);
689         }
690
691         if(wintype == wintype_TextGrid || wintype == wintype_AllTypes) {
692                 to_change = g_hash_table_lookup( glk_data->current_styles->text_grid, get_tag_name(styl) );
693                 apply_stylehint_to_tag(to_change, hint, val);
694         }
695 }
696
697 void
698 glk_stylehint_clear(glui32 wintype, glui32 styl, glui32 hint)
699 {
700         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
701         GtkTextTag *tag;
702
703         switch(wintype) {
704         case wintype_TextBuffer:
705                 tag = g_hash_table_lookup( glk_data->default_styles->text_buffer, get_tag_name(styl) );
706                 glk_stylehint_set( wintype, styl, hint, query_tag(tag, hint) );
707                 break;
708         case wintype_TextGrid:
709                 tag = g_hash_table_lookup( glk_data->default_styles->text_grid, get_tag_name(styl) );
710                 glk_stylehint_set( wintype, styl, hint, query_tag(tag, hint) );
711         default:
712                 return;
713         }
714 }
715
716 glui32
717 glk_style_distinguish(winid_t win, glui32 styl1, glui32 styl2)
718 {
719         return styl1 != styl2;
720 }
721
722 glui32
723 glk_style_measure(winid_t win, glui32 styl, glui32 hint, glui32 *result)
724 {
725         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
726         GtkTextTag *tag;
727
728         switch(win->type) {
729         case wintype_TextBuffer:
730                 tag = g_hash_table_lookup( glk_data->current_styles->text_buffer, get_tag_name(styl) );
731                 *result = query_tag(tag, hint);
732                 break;
733         case wintype_TextGrid:
734                 tag = g_hash_table_lookup( glk_data->current_styles->text_grid, get_tag_name(styl) );
735                 *result = query_tag(tag, hint);
736         default:
737                 return FALSE;
738         }
739
740         return TRUE;
741 }