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