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