7147d53f9bc7ea5e1b19d53b4574579ee0365501
[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 void
529 glkcolor_to_gdkcolor(glui32 val, GdkColor *color)
530 {
531         color->red = 256 * ((val & 0xff0000) >> 16);
532         color->green = 256 * ((val & 0x00ff00) >> 8);
533         color->blue = 256 * (val & 0x0000ff);
534 }
535
536 /* Internal function: parses a GdkColor to a glk color */
537 static glui32
538 gdkcolor_to_glkcolor(GdkColor *color)
539 {
540         g_return_val_if_fail(color != NULL, 0);
541         return (glui32) color->pixel;
542 }
543
544 /* Internal function: changes a GTK tag to correspond with the given style. */
545 static void
546 apply_stylehint_to_tag(GtkTextTag *tag, glui32 hint, glsi32 val)
547 {
548         g_return_if_fail(tag != NULL);
549
550         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
551         GObject *tag_object = G_OBJECT(tag);
552
553         gint reverse_color = GPOINTER_TO_INT( g_object_get_data(tag_object, "reverse-color") );
554
555         int i = 0;
556         gchar color[20];
557         switch(hint) {
558         case stylehint_Indentation:
559                 g_object_set(tag_object, "left-margin", 5*val, "left-margin-set", TRUE, NULL);
560                 g_object_set(tag_object, "right-margin", 5*val, "right-margin-set", TRUE, NULL);
561                 break;
562         
563         case stylehint_ParaIndentation:
564                 g_object_set(tag_object, "indent", 5*val, "indent-set", TRUE, NULL);
565                 break;
566
567         case stylehint_Justification:
568                 switch(val) {
569                         case stylehint_just_LeftFlush:  i = GTK_JUSTIFY_LEFT; break;
570                         case stylehint_just_LeftRight:  i = GTK_JUSTIFY_FILL; break;
571                         case stylehint_just_Centered:   i = GTK_JUSTIFY_CENTER; break;
572                         case stylehint_just_RightFlush: i = GTK_JUSTIFY_RIGHT; break;
573                         default: 
574                                 WARNING("Unknown justification");
575                                 i = GTK_JUSTIFY_LEFT;
576                 }
577                 g_object_set(tag_object, "justification", i, "justification-set", TRUE, NULL);
578                 break;
579
580         case stylehint_Weight:
581                 switch(val) {
582                         case -1: i = PANGO_WEIGHT_LIGHT; break;
583                         case  0: i = PANGO_WEIGHT_NORMAL; break;
584                         case  1: i = PANGO_WEIGHT_BOLD; break;
585                         default: WARNING("Unknown font weight");
586                 }
587                 g_object_set(tag_object, "weight", i, "weight-set", TRUE, NULL);
588                 break;
589
590         case stylehint_Size:
591                 g_object_set(tag_object, "size", 14+(2*val), "size-set", TRUE, NULL);
592                 break;
593
594         case stylehint_Oblique:
595                 g_object_set(tag_object, "style", val ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
596                 break;
597
598         case stylehint_Proportional:
599                 g_object_set(tag_object, "font-desc", val ? glk_data->default_font_desc : glk_data->monospace_font_desc, NULL);
600                 break;
601
602         case stylehint_TextColor:
603                 glkcolor_to_hex(val, color);
604
605                 if(!reverse_color)
606                         g_object_set(tag_object, "foreground", color, "foreground-set", TRUE, NULL);
607                 else
608                         g_object_set(tag_object, "background", color, "background-set", TRUE, NULL);
609
610                 break;
611
612         case stylehint_BackColor:
613                 glkcolor_to_hex(val, color);
614
615                 if(!reverse_color)
616                         g_object_set(tag_object, "background", color, "background-set", TRUE, NULL);
617                 else
618                         g_object_set(tag_object, "foreground", color, "background-set", TRUE, NULL);
619
620                 break;
621
622         case stylehint_ReverseColor:
623                 if(reverse_color != val) {
624                         /* Flip the fore- and background colors */
625                         GdkColor* foreground_color;
626                         GdkColor* background_color;
627                         gint f_set, b_set = 0;
628                         g_object_get(tag_object, "foreground-set", &f_set, "background-set", &b_set, NULL);
629
630                         if(f_set)
631                                 g_object_get(tag_object, "foreground-gdk", &foreground_color, NULL);
632                         if(b_set)
633                                 g_object_get(tag_object, "background-gdk", &background_color, NULL);
634
635                         if(b_set)
636                                 g_object_set(tag_object, "foreground-gdk", background_color, NULL);
637                         else
638                                 g_object_set(tag_object, "foreground", "#ffffff", NULL);
639
640                         if(f_set)
641                                 g_object_set(tag_object, "background-gdk", foreground_color, NULL);
642                         else
643                                 g_object_set(tag_object, "background", "#000000", NULL);
644
645                         g_object_set_data( tag_object, "reverse-color", GINT_TO_POINTER(val != 0) );
646                 }
647                 break;
648
649         default:
650                 WARNING("Unknown style hint");
651         }
652 }
653 /*Internal function: queries a text tag for the value of a given style hint */
654 static gint
655 query_tag(GtkTextTag *tag, glui32 hint)
656 {
657         gint intval;
658         GObject *objval;
659         GdkColor *colval;
660
661         g_return_val_if_fail(tag != NULL, 0);
662
663         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
664
665         switch(hint) {
666         case stylehint_Indentation:
667                 g_object_get(tag, "left_margin", &intval, NULL);
668                 return intval/5;
669                 break;
670         
671         case stylehint_ParaIndentation:
672                 g_object_get(tag, "indent", &intval, NULL);
673                 return intval/5;
674                 break;
675
676         case stylehint_Justification:
677                 g_object_get(tag, "justification", &intval, NULL);
678                 switch(intval) {
679                         case GTK_JUSTIFY_LEFT: return stylehint_just_LeftFlush; break;
680                         case GTK_JUSTIFY_FILL: return stylehint_just_LeftRight; break;
681                         case GTK_JUSTIFY_CENTER: return stylehint_just_Centered; break;
682                         case GTK_JUSTIFY_RIGHT: return stylehint_just_RightFlush; break;
683                         default: 
684                                 WARNING("Unknown justification");
685                                 return stylehint_just_LeftFlush;
686                 }
687                 break;
688
689         case stylehint_Weight:
690                 g_object_get(tag, "weight", &intval, NULL);
691                 switch(intval) {
692                         case PANGO_WEIGHT_LIGHT: return -1; break;
693                         case PANGO_WEIGHT_NORMAL: return 0; break;
694                         case PANGO_WEIGHT_BOLD: return 1; break;
695                         default: WARNING("Unknown font weight"); return 0;
696                 }
697                 break;
698
699         case stylehint_Size:
700                 g_object_get(tag, "size", &intval, NULL);
701                 return (intval/2)-14;
702                 break;
703
704         case stylehint_Oblique:
705                 g_object_get(tag, "style", &intval , NULL);
706                 return intval == PANGO_STYLE_ITALIC ? 1 : 0;
707                 break;
708
709         case stylehint_Proportional:
710                 g_object_get(tag, "font-desc", &objval, NULL);
711                 return objval == (GObject *)glk_data->monospace_font_desc ? 0 : 1;
712                 break;
713
714         case stylehint_TextColor:
715                 g_object_get(tag, "foreground-gdk", &colval, NULL);
716                 return gdkcolor_to_glkcolor(colval);
717                 break;
718
719         case stylehint_BackColor:
720                 g_object_get(tag, "background-gdk", &colval, NULL);
721                 return gdkcolor_to_glkcolor(colval);
722                 break;
723
724         case stylehint_ReverseColor:
725                 return GPOINTER_TO_INT( g_object_get_data(G_OBJECT(tag), "reverse_color") );
726                 break;
727
728         default:
729                 WARNING("Unknown style hint");
730         }
731         
732         return 0;
733 }
734
735 /**
736  * glk_stylehint_set:
737  * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
738  * @styl: The style to set a hint for.
739  * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
740  * @val: The style hint. The meaning of this depends on @hint.
741  *
742  * Sets a hint about the appearance of one style for a particular type of 
743  * window. You can also set wintype to %wintype_AllTypes, which sets a hint for 
744  * all types of window.
745  * <note><para>
746  *  There is no equivalent constant to set a hint for all styles of a single 
747  *  window type.
748  * </para></note>
749  */
750 void
751 glk_stylehint_set(glui32 wintype, glui32 styl, glui32 hint, glsi32 val)
752 {
753         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
754
755         if( G_UNLIKELY(!glk_data->style_initialized) ) {
756                 style_init();
757         }
758
759         GtkTextTag *to_change;
760         if(wintype == wintype_TextBuffer || wintype == wintype_AllTypes) {
761                 to_change = g_hash_table_lookup( glk_data->current_styles->text_buffer, get_tag_name(styl) );
762                 apply_stylehint_to_tag(to_change, hint, val);
763         }
764
765         if(wintype == wintype_TextGrid || wintype == wintype_AllTypes) {
766                 to_change = g_hash_table_lookup( glk_data->current_styles->text_grid, get_tag_name(styl) );
767                 apply_stylehint_to_tag(to_change, hint, val);
768         }
769 }
770
771 /**
772  * glk_stylehint_clear:
773  * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
774  * @styl: The style to set a hint for.
775  * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
776  *
777  * Resets a hint about the appearance of one style for a particular type of 
778  * window to it's default value. You can also set wintype to %wintype_AllTypes, which resets a hint for 
779  * all types of window.
780  * <note><para>
781  *  There is no equivalent constant to reset a hint for all styles of a single 
782  *  window type.
783  * </para></note>
784  */
785 void
786 glk_stylehint_clear(glui32 wintype, glui32 styl, glui32 hint)
787 {
788         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
789         GtkTextTag *tag;
790
791         switch(wintype) {
792         case wintype_TextBuffer:
793                 tag = g_hash_table_lookup( glk_data->default_styles->text_buffer, get_tag_name(styl) );
794                 glk_stylehint_set( wintype, styl, hint, query_tag(tag, hint) );
795                 break;
796         case wintype_TextGrid:
797                 tag = g_hash_table_lookup( glk_data->default_styles->text_grid, get_tag_name(styl) );
798                 glk_stylehint_set( wintype, styl, hint, query_tag(tag, hint) );
799         default:
800                 return;
801         }
802 }
803
804 /**
805  * glk_style_distinguish:
806  * @win: The window in which the styles are to be distinguished.
807  * @styl1: The first style to be distinguished from the second style.
808  * @styl2: The second styel to be distinguished from the first style.
809  * 
810  * Returns: TRUE if the two styles are visually distinguishable in the given window.
811  * If they are not, it returns FALSE.
812  */
813 glui32
814 glk_style_distinguish(winid_t win, glui32 styl1, glui32 styl2)
815 {
816         return styl1 != styl2;
817 }
818
819 /**
820  * glk_style_measure:
821  * @win: The window from which to take the style.
822  * @styl: The style to perform the measurement on.
823  * @hint: The stylehint to measure.
824  * @result: Address to write the result to.
825  * 
826  * This function can be used to query the current value of a particular style hint.
827  * Returns: TRUE upon successul retrievel, otherwise FALSE.
828  */
829 glui32
830 glk_style_measure(winid_t win, glui32 styl, glui32 hint, glui32 *result)
831 {
832         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
833         GtkTextTag *tag;
834
835         switch(win->type) {
836         case wintype_TextBuffer:
837                 tag = g_hash_table_lookup( glk_data->current_styles->text_buffer, get_tag_name(styl) );
838                 *result = query_tag(tag, hint);
839                 break;
840         case wintype_TextGrid:
841                 tag = g_hash_table_lookup( glk_data->current_styles->text_grid, get_tag_name(styl) );
842                 *result = query_tag(tag, hint);
843         default:
844                 return FALSE;
845         }
846
847         return TRUE;
848 }
849
850 /* Internal function returning the current default font for a window type
851  * This can be used later for size calculations. Only wintype_TextGrid and wintype_TextBuffer are
852  * supported for now */
853 PangoFontDescription*
854 get_current_font(guint32 wintype)
855 {
856         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
857         GtkTextTag *normal;
858
859         if( G_UNLIKELY(!glk_data->style_initialized) ) {
860                 style_init();
861         }
862
863         switch(wintype) {
864         case wintype_TextGrid:
865                 normal = g_hash_table_lookup(glk_data->current_styles->text_grid, "normal");
866                 break;
867         case wintype_TextBuffer:
868                 normal = g_hash_table_lookup(glk_data->current_styles->text_buffer, "normal");
869                 break;
870         default:
871                 return NULL;
872         }
873
874         PangoFontDescription *font;
875         g_object_get( G_OBJECT(normal), "font-desc", &font, NULL );
876
877         return font;
878 }