Fixed reversevideo
[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         /* Copy the reverse_color attribute, that was added manually */
157         g_object_set_data( G_OBJECT(copy), "reverse_color", g_object_get_data(G_OBJECT(tag), "reverse_color") );
158
159         return copy;
160 }
161
162 /* Internal function that reads the default styles from a CSS file */
163 void
164 style_init()
165 {
166         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
167         GHashTable *default_text_grid_styles = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_object_unref);
168         GHashTable *default_text_buffer_styles = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_object_unref);
169         GHashTable *current_text_grid_styles = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_object_unref);
170         GHashTable *current_text_buffer_styles = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_object_unref);
171         GtkTextTag *tag;
172
173         /* Create the CSS file scanner */
174         GScanner *scanner = g_scanner_new(NULL);
175
176         int f = open(glk_data->css_file, O_RDONLY);
177         g_return_if_fail(f != -1);
178         g_scanner_input_file(scanner, f);
179         scanner->input_name = glk_data->css_file;
180         scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z "#";
181         scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z "-_" G_CSET_DIGITS;
182         scanner->config->symbol_2_token = TRUE;
183         scanner->config->cpair_comment_single = NULL;
184         scanner->config->scan_float = FALSE;
185
186         /* Initialise the default styles */
187         g_hash_table_insert(default_text_grid_styles, "normal", gtk_text_tag_new("normal"));
188
189         tag = gtk_text_tag_new("emphasized");
190         g_object_set(tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
191         g_hash_table_insert(default_text_grid_styles, "emphasized", tag);
192
193         tag = gtk_text_tag_new("preformatted");
194         g_object_set(tag, "font-desc", glk_data->monospace_font_desc, NULL);
195         g_hash_table_insert(default_text_grid_styles, "preformatted", tag);
196
197         tag = gtk_text_tag_new("header");
198         g_object_set(tag, "size-points", 18.0, "weight", PANGO_WEIGHT_BOLD, NULL);
199         g_hash_table_insert(default_text_grid_styles, "header", tag);
200
201         tag = gtk_text_tag_new("subheader");
202         g_object_set(tag, "size-points", 14.0, "weight", PANGO_WEIGHT_BOLD, NULL);
203         g_hash_table_insert(default_text_grid_styles, "subheader", tag);
204
205         tag = gtk_text_tag_new("alert");
206         g_object_set(tag, "foreground", "#aa0000", "weight", PANGO_WEIGHT_BOLD, NULL);
207         g_hash_table_insert(default_text_grid_styles, "alert", tag);
208
209         tag = gtk_text_tag_new("note");
210         g_object_set(tag, "foreground", "#aaaa00", "weight", PANGO_WEIGHT_BOLD, NULL);
211         g_hash_table_insert(default_text_grid_styles, "note", tag);
212
213         tag = gtk_text_tag_new("block-quote");
214         g_object_set(tag, "justification", GTK_JUSTIFY_CENTER, "style", PANGO_STYLE_ITALIC, NULL);
215         g_hash_table_insert(default_text_grid_styles, "block-quote", tag);
216
217         g_hash_table_insert(default_text_grid_styles, "input", gtk_text_tag_new("input"));
218         g_hash_table_insert(default_text_grid_styles, "user1", gtk_text_tag_new("user1"));
219         g_hash_table_insert(default_text_grid_styles, "user2", gtk_text_tag_new("user2"));
220
221         g_hash_table_foreach(default_text_grid_styles, style_table_copy, default_text_buffer_styles);
222         glk_data->default_styles->text_grid = default_text_grid_styles;
223         glk_data->default_styles->text_buffer = default_text_buffer_styles;
224
225         /* Run the scanner over the CSS file, overriding defaults */
226         while( g_scanner_peek_next_token(scanner) != G_TOKEN_EOF) {
227                 if( !style_accept_style_selector(scanner) )
228                         break;
229         }
230
231         /* Set the current style to a copy of the default style */
232         g_hash_table_foreach(default_text_grid_styles, style_table_copy, current_text_grid_styles);
233         g_hash_table_foreach(default_text_buffer_styles, style_table_copy, current_text_buffer_styles);
234         glk_data->current_styles->text_grid = current_text_grid_styles;
235         glk_data->current_styles->text_buffer = current_text_buffer_styles;
236
237         g_scanner_destroy(scanner);
238
239         glk_data->style_initialized = TRUE;
240 }
241
242 /* Internal function: parses a token */
243 static gboolean
244 style_accept(GScanner *scanner, GTokenType token)
245 {
246         GTokenType next = g_scanner_get_next_token(scanner);
247         if(next != token ) {
248                 g_scanner_unexp_token(scanner, token, NULL, NULL, NULL, "CSS Error", 1);
249                 return FALSE;
250         } else {
251                 return TRUE;
252         }
253 }
254
255 /* Internal function: parses a style selector */
256 static gboolean
257 style_accept_style_selector(GScanner *scanner)
258 {
259         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
260
261         GtkTextTag *current_tag;
262         gchar *field;
263         GTokenType token = g_scanner_get_next_token(scanner);
264         GTokenValue value = g_scanner_cur_value(scanner);
265
266         if(
267                 token != G_TOKEN_IDENTIFIER ||
268                 ( strcmp(value.v_identifier, "buffer") && strcmp(value.v_identifier, "grid") )
269         ) {
270                 g_scanner_error(scanner, "CSS Error: buffer/grid expected");
271                 return FALSE;
272         }
273
274         field = g_strdup(value.v_identifier);
275
276         if( !style_accept(scanner, '.') )
277                 return FALSE;
278
279         token = g_scanner_get_next_token(scanner);
280         value = g_scanner_cur_value(scanner);
281
282         if(token != G_TOKEN_IDENTIFIER) {
283                 g_scanner_error(scanner, "CSS Error: style selector expected");
284                 return FALSE;
285         }
286
287         if( !strcmp(field, "buffer") )
288                 current_tag = g_hash_table_lookup(glk_data->default_styles->text_buffer, value.v_identifier);
289         else
290                 current_tag = g_hash_table_lookup(glk_data->default_styles->text_grid, value.v_identifier);
291
292         if(current_tag == NULL) {
293                 g_scanner_error(scanner, "CSS Error: invalid style identifier");
294                 return FALSE;
295         }
296
297         if( !style_accept(scanner, '{') )
298                 return FALSE;
299
300         while( g_scanner_peek_next_token(scanner) != '}') {
301                 if( !style_accept_style_hint(scanner, current_tag) )
302                         return FALSE;
303         }
304                 
305         if( !style_accept(scanner, '}') )
306                 return FALSE;
307
308         return TRUE;
309 }
310
311 /* Internal function: parses a style hint */
312 static gboolean
313 style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag)
314 {
315         GTokenType token = g_scanner_get_next_token(scanner);
316         GTokenValue value = g_scanner_cur_value(scanner);
317         gchar *hint;
318
319         if(token != G_TOKEN_IDENTIFIER) {
320                 g_scanner_error(scanner, "CSS Error: style hint expected");
321                 return FALSE;
322         }
323
324         hint = g_strdup(value.v_identifier);
325
326         if( !style_accept(scanner, ':') )
327                 return FALSE;
328
329         token = g_scanner_get_next_token(scanner);
330         value = g_scanner_cur_value(scanner);
331
332         if( !strcmp(hint, "font-family") ) {
333                 if(token != G_TOKEN_STRING) {
334                         g_scanner_error(scanner, "CSS Error: string expected");
335                         return FALSE;
336                 }
337                 g_object_set(current_tag, "family", value.v_string, "family-set", TRUE, NULL);
338         }
339         else if( !strcmp(hint, "font-weight") ) {
340                 if(token != G_TOKEN_IDENTIFIER) {
341                         g_scanner_error(scanner, "CSS Error: bold/normal expected");
342                         return FALSE;
343                 }
344
345                 if( !strcmp(value.v_identifier, "bold") )
346                         g_object_set(current_tag, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
347                 else if( !strcmp(value.v_identifier, "normal") )
348                         g_object_set(current_tag, "weight", PANGO_WEIGHT_NORMAL, "weight-set", TRUE, NULL);
349                 else {
350                         g_scanner_error(scanner, "CSS Error: bold/normal expected");
351                         return FALSE;
352                 }
353         }
354         else if( !strcmp(hint, "font-style") ) {
355                 if(token != G_TOKEN_IDENTIFIER) {
356                         g_scanner_error(scanner, "CSS Error: italic/normal expected");
357                         return FALSE;
358                 }
359
360                 if( !strcmp(value.v_identifier, "italic") )
361                         g_object_set(current_tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
362                 else if( !strcmp(value.v_identifier, "normal") )
363                         g_object_set(current_tag, "style", PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
364                 else {
365                         g_scanner_error(scanner, "CSS Error: italic/normal expected");
366                         return FALSE;
367                 }
368         }
369         else if( !strcmp(hint, "font-size") ) {
370                 if(token == G_TOKEN_INT) 
371                         g_object_set(current_tag, "size-points", (float)value.v_int, "size-set", TRUE, NULL);
372                 else if(token == G_TOKEN_FLOAT)
373                         g_object_set(current_tag, "size-points", value.v_float, "size-set", TRUE, NULL);
374                 else {
375                         g_scanner_error(scanner, "CSS Error: integer or float expected");
376                         return FALSE;
377                 }
378         }
379         else if( !strcmp(hint, "color") ) {
380                 if(token != G_TOKEN_IDENTIFIER) {
381                         g_scanner_error(scanner, "CSS Error: hex color expected");
382                         return FALSE;
383                 }
384
385                 g_object_set(current_tag, "foreground", value.v_identifier, "foreground-set", TRUE, NULL);
386         }
387         else if( !strcmp(hint, "background-color") ) {
388                 if(token != G_TOKEN_IDENTIFIER) {
389                         g_scanner_error(scanner, "CSS Error: hex color expected");
390                         return FALSE;
391                 }
392                 g_object_set(current_tag, "background", value.v_identifier, "background-set", TRUE, NULL);
393         }
394         else if( !strcmp(hint, "text-align") ) {
395                 if(token != G_TOKEN_IDENTIFIER) {
396                         g_scanner_error(scanner, "CSS Error: left/right/center expected");
397                         return FALSE;
398                 }
399                 
400                 if( !strcmp(value.v_identifier, "left") )
401                         g_object_set(current_tag, "justification", GTK_JUSTIFY_LEFT, "justification-set", TRUE, NULL);
402                 else if( !strcmp(value.v_identifier, "right") )
403                         g_object_set(current_tag, "justification", GTK_JUSTIFY_RIGHT, "justification-set", TRUE, NULL);
404                 else if( !strcmp(value.v_identifier, "center") )
405                         g_object_set(current_tag, "justification", GTK_JUSTIFY_CENTER, "justification-set", TRUE, NULL);
406                 else {
407                         g_scanner_error(scanner, "CSS Error: left/right/center expected");
408                         return FALSE;
409                 }
410         }
411         else if( !strcmp(hint, "margin-left") ) {
412                 if(token != G_TOKEN_INT) {
413                         g_scanner_error(scanner, "CSS Error: integer expected");
414                         return FALSE;
415                 }
416                 g_object_set(current_tag, "left-margin", value.v_int, "left-margin-set", TRUE, NULL);
417         }
418         else if( !strcmp(hint, "margin-right") ) {
419                 if(token != G_TOKEN_INT) {
420                         g_scanner_error(scanner, "CSS Error: integer expected");
421                         return FALSE;
422                 }
423                 g_object_set(current_tag, "right-margin", value.v_int, "right-margin-set", TRUE, NULL);
424         }
425         else if( !strcmp(hint, "margin-top") ) {
426                 if(token != G_TOKEN_INT) {
427                         g_scanner_error(scanner, "CSS Error: integer expected");
428                         return FALSE;
429                 }
430                 g_object_set(current_tag, "pixels-above-lines", value.v_int, "pixels-above-lines-set", TRUE, NULL);
431         }
432         else if( !strcmp(hint, "margin-bottom") ) {
433                 if(token != G_TOKEN_INT) {
434                         g_scanner_error(scanner, "CSS Error: integer expected");
435                         return FALSE;
436                 }
437                 g_object_set(current_tag, "pixels-below-lines", value.v_int, "pixels-below-lines-set", TRUE, NULL);
438         }
439                 
440         else {
441                 g_scanner_error(scanner, "CSS Error: invalid style hint %s", hint);
442                 return FALSE;
443         }
444
445         if( !style_accept(scanner, ';') )
446                 return FALSE;
447
448         return TRUE;
449 }
450
451 /* Internal function: parses a glk color to a #hex-value */
452 static void
453 color_format(glui32 val, gchar *buffer)
454 {
455         g_return_if_fail(buffer != NULL);
456
457         sprintf(buffer, "#%02X%02X%02X",
458                 ((val & 0xff0000) >> 16),
459                 ((val & 0x00ff00) >> 8),
460                 (val & 0x0000ff)
461         );
462 }
463
464 /* Internal function: parses a GdkColor to a glk color */
465 static glui32
466 color_parse_gdk(GdkColor *color)
467 {
468         g_return_val_if_fail(color != NULL, 0);
469         return (glui32) color->pixel;
470 }
471
472 /* Internal function: changes a GTK tag to correspond with the given style. */
473 static void
474 apply_stylehint_to_tag(GtkTextTag *tag, glui32 hint, glsi32 val)
475 {
476         g_return_if_fail(tag != NULL);
477
478         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
479         GObject *tag_object = G_OBJECT(tag);
480
481         gint reverse_color = GPOINTER_TO_INT( g_object_get_data(tag_object, "reverse-color") );
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                         g_object_set_data( tag_object, "reverse-color", GINT_TO_POINTER(val != 0) );
574                 }
575                 break;
576
577         default:
578                 WARNING("Unknown style hint");
579         }
580 }
581 /*Internal function: queries a text tag for the value of a given style hint */
582 static gint
583 query_tag(GtkTextTag *tag, glui32 hint)
584 {
585         gint intval;
586         GObject *objval;
587         GdkColor *colval;
588
589         g_return_val_if_fail(tag != NULL, 0);
590
591         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
592
593         switch(hint) {
594         case stylehint_Indentation:
595                 g_object_get(tag, "left_margin", &intval, NULL);
596                 return intval/5;
597                 break;
598         
599         case stylehint_ParaIndentation:
600                 g_object_get(tag, "indent", &intval, NULL);
601                 return intval/5;
602                 break;
603
604         case stylehint_Justification:
605                 g_object_get(tag, "justification", &intval, NULL);
606                 switch(intval) {
607                         case GTK_JUSTIFY_LEFT: return stylehint_just_LeftFlush; break;
608                         case GTK_JUSTIFY_FILL: return stylehint_just_LeftRight; break;
609                         case GTK_JUSTIFY_CENTER: return stylehint_just_Centered; break;
610                         case GTK_JUSTIFY_RIGHT: return stylehint_just_RightFlush; break;
611                         default: 
612                                 WARNING("Unknown justification");
613                                 return stylehint_just_LeftFlush;
614                 }
615                 break;
616
617         case stylehint_Weight:
618                 g_object_get(tag, "weight", &intval, NULL);
619                 switch(intval) {
620                         case PANGO_WEIGHT_LIGHT: return -1; break;
621                         case PANGO_WEIGHT_NORMAL: return 0; break;
622                         case PANGO_WEIGHT_BOLD: return 1; break;
623                         default: WARNING("Unknown font weight"); return 0;
624                 }
625                 break;
626
627         case stylehint_Size:
628                 g_object_get(tag, "size", &intval, NULL);
629                 return (intval/2)-14;
630                 break;
631
632         case stylehint_Oblique:
633                 g_object_get(tag, "style", &intval , NULL);
634                 return intval == PANGO_STYLE_ITALIC ? 1 : 0;
635                 break;
636
637         case stylehint_Proportional:
638                 g_object_get(tag, "font-desc", &objval, NULL);
639                 return objval == (GObject *)glk_data->monospace_font_desc ? 0 : 1;
640                 break;
641
642         case stylehint_TextColor:
643                 g_object_get(tag, "foreground-gdk", &colval, NULL);
644                 return color_parse_gdk(colval);
645                 break;
646
647         case stylehint_BackColor:
648                 g_object_get(tag, "background-gdk", &colval, NULL);
649                 return color_parse_gdk(colval);
650                 break;
651
652         case stylehint_ReverseColor:
653                 return GPOINTER_TO_INT( g_object_get_data(G_OBJECT(tag), "reverse_color") );
654                 break;
655
656         default:
657                 WARNING("Unknown style hint");
658         }
659         
660         return 0;
661 }
662
663 /**
664  * glk_stylehint_set:
665  * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
666  * @styl: The style to set a hint for.
667  * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
668  * @val: The style hint. The meaning of this depends on @hint.
669  *
670  * Sets a hint about the appearance of one style for a particular type of 
671  * window. You can also set wintype to %wintype_AllTypes, which sets a hint for 
672  * all types of window.
673  * <note><para>
674  *  There is no equivalent constant to set a hint for all styles of a single 
675  *  window type.
676  * </para></note>
677  */
678 void
679 glk_stylehint_set(glui32 wintype, glui32 styl, glui32 hint, glsi32 val)
680 {
681         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
682
683         if( G_UNLIKELY(!glk_data->style_initialized) ) {
684                 style_init();
685         }
686
687         GtkTextTag *to_change;
688         if(wintype == wintype_TextBuffer || wintype == wintype_AllTypes) {
689                 to_change = g_hash_table_lookup( glk_data->current_styles->text_buffer, get_tag_name(styl) );
690                 apply_stylehint_to_tag(to_change, hint, val);
691         }
692
693         if(wintype == wintype_TextGrid || wintype == wintype_AllTypes) {
694                 to_change = g_hash_table_lookup( glk_data->current_styles->text_grid, get_tag_name(styl) );
695                 apply_stylehint_to_tag(to_change, hint, val);
696         }
697 }
698
699 /**
700  * glk_stylehint_clear:
701  * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
702  * @styl: The style to set a hint for.
703  * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
704  *
705  * Resets a hint about the appearance of one style for a particular type of 
706  * window to it's default value. You can also set wintype to %wintype_AllTypes, which resets a hint for 
707  * all types of window.
708  * <note><para>
709  *  There is no equivalent constant to reset a hint for all styles of a single 
710  *  window type.
711  * </para></note>
712  */
713 void
714 glk_stylehint_clear(glui32 wintype, glui32 styl, glui32 hint)
715 {
716         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
717         GtkTextTag *tag;
718
719         switch(wintype) {
720         case wintype_TextBuffer:
721                 tag = g_hash_table_lookup( glk_data->default_styles->text_buffer, get_tag_name(styl) );
722                 glk_stylehint_set( wintype, styl, hint, query_tag(tag, hint) );
723                 break;
724         case wintype_TextGrid:
725                 tag = g_hash_table_lookup( glk_data->default_styles->text_grid, get_tag_name(styl) );
726                 glk_stylehint_set( wintype, styl, hint, query_tag(tag, hint) );
727         default:
728                 return;
729         }
730 }
731
732 /**
733  * glk_style_distinguish:
734  * @win: The window in which the styles are to be distinguished.
735  * @styl1: The first style to be distinguished from the second style.
736  * @styl2: The second styel to be distinguished from the first style.
737  * 
738  * Returns: TRUE if the two styles are visually distinguishable in the given window.
739  * If they are not, it returns FALSE.
740  */
741 glui32
742 glk_style_distinguish(winid_t win, glui32 styl1, glui32 styl2)
743 {
744         return styl1 != styl2;
745 }
746
747 /**
748  * glk_style_measure:
749  * @win: The window from which to take the style.
750  * @styl: The style to perform the measurement on.
751  * @hint: The stylehint to measure.
752  * @result: Address to write the result to.
753  * 
754  * This function can be used to query the current value of a particular style hint.
755  * Returns: TRUE upon successul retrievel, otherwise FALSE.
756  */
757 glui32
758 glk_style_measure(winid_t win, glui32 styl, glui32 hint, glui32 *result)
759 {
760         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
761         GtkTextTag *tag;
762
763         switch(win->type) {
764         case wintype_TextBuffer:
765                 tag = g_hash_table_lookup( glk_data->current_styles->text_buffer, get_tag_name(styl) );
766                 *result = query_tag(tag, hint);
767                 break;
768         case wintype_TextGrid:
769                 tag = g_hash_table_lookup( glk_data->current_styles->text_grid, get_tag_name(styl) );
770                 *result = query_tag(tag, hint);
771         default:
772                 return FALSE;
773         }
774
775         return TRUE;
776 }