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