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