Backend support for using dynamic styles. See also Ticket #49.
[rodin/chimara.git] / libchimara / style.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "chimara-glk-private.h"
4 #include "glk.h"
5 #include "style.h"
6 #include "magic.h"
7 #include "stream.h"
8 #include "strio.h"
9
10 extern GPrivate *glk_data_key;
11
12 static gboolean style_accept(GScanner *scanner, GTokenType token);
13 static gboolean style_accept_style_selector(GScanner *scanner, ChimaraGlk *glk);
14 static gboolean style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag);
15 static void style_add_tag_to_textbuffer(gpointer key, gpointer tag, gpointer tag_table);
16 static void style_copy_tag_to_textbuffer(gpointer key, gpointer tag, gpointer target_table);
17 static void text_tag_to_attr_list(GtkTextTag *tag, PangoAttrList *list);
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. However, any value is actually legal; if the 
26  * interpreter does not recognize the style value, it will treat it as 
27  * %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 #define NUM_STYLES 13
42 static const gchar* TAG_NAMES[] = {
43         "normal",
44         "emphasized",
45         "preformatted",
46         "header",
47         "subheader",
48         "alert",
49         "note",
50         "block-quote",
51         "input",
52         "user1",
53         "user2",
54         "hyperlink",
55         "pager"
56 };
57
58 static const gchar* GLK_TAG_NAMES[] = {
59         "glk-normal",
60         "glk-emphasized",
61         "glk-preformatted",
62         "glk-header",
63         "glk-subheader",
64         "glk-alert",
65         "glk-note",
66         "glk-block-quote",
67         "glk-input",
68         "glk-user1",
69         "glk-user2"
70 };
71
72 /* Internal function: mapping from style enum to tag name */
73 static gchar*
74 get_tag_name(glui32 style)
75 {
76         if(style >= NUM_STYLES) {
77                 WARNING("Unsupported style");
78                 return "normal";
79         } else {
80                 return (gchar*) TAG_NAMES[style];
81         }
82 }
83
84 /* Internal function: mapping from glk style enum to tag name */
85 static gchar*
86 get_glk_tag_name(glui32 style)
87 {
88         if(style >= style_NUMSTYLES) {
89                 WARNING("Unsupported style");
90                 return "normal";
91         } else {
92                 return (gchar*) GLK_TAG_NAMES[style];
93         }
94 }
95
96 /** 
97  * glk_set_style_stream:
98  * @str: Output stream to change the style of
99  * @styl: The style to apply
100  *
101  * This changes the style of the stream @str. See glk_set_style().
102  */
103 void
104 glk_set_style_stream(strid_t str, glui32 styl) {
105         if(str->window == NULL)
106                 return;
107
108         flush_window_buffer(str->window);
109         str->style = get_tag_name(styl);
110         str->glk_style = get_glk_tag_name(styl);
111 }
112
113 /* Internal function: call this to initialize the layout of the 'more' prompt. */
114 void
115 style_init_more_prompt(winid_t win)
116 {
117         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
118
119         win->pager_layout = gtk_widget_create_pango_layout(win->widget, "More");
120         pango_layout_set_attributes(win->pager_layout, glk_data->pager_attr_list);
121 }
122
123 /* Internal function: call this to initialize the default styles to a textbuffer. */
124 void
125 style_init_textbuffer(GtkTextBuffer *buffer)
126 {
127         g_return_if_fail(buffer != NULL);
128
129         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
130
131         /* Place the default text tags in the textbuffer's tag table */
132         g_hash_table_foreach(glk_data->styles->text_buffer, style_add_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
133
134         /* Copy the override text tags to the textbuffers's tag table */
135         g_hash_table_foreach(glk_data->glk_styles->text_buffer, style_copy_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
136 }
137
138
139 /* Internal function: call this to initialize the default styles to a textgrid. */
140 void
141 style_init_textgrid(GtkTextBuffer *buffer)
142 {
143         g_return_if_fail(buffer != NULL);
144         
145         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
146
147         /* Place the default text tags in the textbuffer's tag table */
148         g_hash_table_foreach(glk_data->styles->text_grid, style_add_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
149
150         /* Copy the current text tags to the textbuffers's tag table */
151         g_hash_table_foreach(glk_data->glk_styles->text_grid, style_copy_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
152 }
153
154 /* Internal function used to iterate over the default text tag table, applying them to a textbuffer */
155 static void
156 style_add_tag_to_textbuffer(gpointer key, gpointer tag, gpointer tag_table)
157 {
158         g_return_if_fail(key != NULL);
159         g_return_if_fail(tag != NULL);
160         g_return_if_fail(tag_table != NULL);
161
162         gtk_text_tag_table_add(tag_table, tag);
163 }
164
165 /* Internal function used to iterate over a style table, copying it */
166 static void
167 style_copy_tag_to_textbuffer(gpointer key, gpointer tag, gpointer target_table)
168 {
169         g_return_if_fail(key != NULL);
170         g_return_if_fail(tag != NULL);
171         g_return_if_fail(target_table != NULL);
172
173         gtk_text_tag_table_add(target_table, gtk_text_tag_copy( GTK_TEXT_TAG(tag) ));
174 }
175
176 /* Internal function that copies a text tag */
177 GtkTextTag *
178 gtk_text_tag_copy(GtkTextTag *tag)
179 {
180         GtkTextTag *copy;
181
182         g_return_val_if_fail(tag != NULL, NULL);
183
184         copy = gtk_text_tag_new(tag->name);
185         gtk_text_attributes_copy_values(tag->values, copy->values);
186         
187         #define _COPY_FLAG(flag) copy->flag = tag->flag
188                 _COPY_FLAG (bg_color_set);
189                 _COPY_FLAG (bg_color_set);
190                 _COPY_FLAG (bg_stipple_set);
191                 _COPY_FLAG (fg_color_set);
192                 _COPY_FLAG (fg_stipple_set);
193                 _COPY_FLAG (justification_set);
194                 _COPY_FLAG (left_margin_set);
195                 _COPY_FLAG (indent_set);
196                 _COPY_FLAG (rise_set);
197                 _COPY_FLAG (strikethrough_set);
198                 _COPY_FLAG (right_margin_set);
199                 _COPY_FLAG (pixels_above_lines_set);
200                 _COPY_FLAG (pixels_below_lines_set);
201                 _COPY_FLAG (pixels_inside_wrap_set);
202                 _COPY_FLAG (tabs_set);
203                 _COPY_FLAG (underline_set);
204                 _COPY_FLAG (wrap_mode_set);
205                 _COPY_FLAG (bg_full_height_set);
206                 _COPY_FLAG (invisible_set);
207                 _COPY_FLAG (editable_set);
208                 _COPY_FLAG (language_set);
209         #undef _COPY_FLAG
210
211         /* Copy the data that was added manually */
212         gpointer reverse_color = g_object_get_data( G_OBJECT(tag), "reverse_color" );
213
214         if(reverse_color)
215                 g_object_set_data( G_OBJECT(copy), "reverse_color", reverse_color );
216
217         return copy;
218 }
219
220 /* Internal function that constructs the default styles */
221 void
222 style_init(ChimaraGlk *glk)
223 {
224         CHIMARA_GLK_USE_PRIVATE(glk, priv);
225         
226         GHashTable *default_text_grid_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
227         GHashTable *default_text_buffer_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
228         GHashTable *glk_text_grid_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
229         GHashTable *glk_text_buffer_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
230         GtkTextTag *tag;
231
232         PangoFontDescription *default_font_desc = pango_font_description_from_string("Serif");
233         PangoFontDescription *monospace_font_desc = pango_font_description_from_string("Monospace");
234         
235         /* Initialise the default styles for a text grid */
236         tag = gtk_text_tag_new("normal");
237         g_object_set(tag, "font-desc", monospace_font_desc, NULL);
238         g_hash_table_insert(default_text_grid_styles, "normal", tag);
239
240         tag = gtk_text_tag_new("emphasized");
241         g_object_set(tag, "font-desc", monospace_font_desc, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
242         g_hash_table_insert(default_text_grid_styles, "emphasized", tag);
243
244         tag = gtk_text_tag_new("preformatted");
245         g_object_set(tag, "font-desc", monospace_font_desc, NULL);
246         g_hash_table_insert(default_text_grid_styles, "preformatted", tag);
247
248         tag = gtk_text_tag_new("header");
249         g_object_set(tag, "font-desc", monospace_font_desc, "weight", PANGO_WEIGHT_BOLD, NULL);
250         g_hash_table_insert(default_text_grid_styles, "header", tag);
251
252         tag = gtk_text_tag_new("subheader");
253         g_object_set(tag, "font-desc", monospace_font_desc, "weight", PANGO_WEIGHT_BOLD, NULL);
254         g_hash_table_insert(default_text_grid_styles, "subheader", tag);
255
256         tag = gtk_text_tag_new("alert");
257         g_object_set(tag, "font-desc", monospace_font_desc, "foreground", "#aa0000", "weight", PANGO_WEIGHT_BOLD, NULL);
258         g_hash_table_insert(default_text_grid_styles, "alert", tag);
259
260         tag = gtk_text_tag_new("note");
261         g_object_set(tag, "font-desc", monospace_font_desc, "foreground", "#aaaa00", "weight", PANGO_WEIGHT_BOLD, NULL);
262         g_hash_table_insert(default_text_grid_styles, "note", tag);
263
264         tag = gtk_text_tag_new("block-quote");
265         g_object_set(tag, "font-desc", monospace_font_desc, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
266         g_hash_table_insert(default_text_grid_styles, "block-quote", tag);
267
268         tag = gtk_text_tag_new("input");
269         g_object_set(tag, "font-desc", monospace_font_desc, NULL);
270         g_hash_table_insert(default_text_grid_styles, "input", tag);
271
272         tag = gtk_text_tag_new("user1");
273         g_object_set(tag, "font-desc", monospace_font_desc, NULL);
274         g_hash_table_insert(default_text_grid_styles, "user1", tag);
275
276         tag = gtk_text_tag_new("user2");
277         g_object_set(tag, "font-desc", monospace_font_desc, NULL);
278         g_hash_table_insert(default_text_grid_styles, "user2", tag);
279
280         tag = gtk_text_tag_new("hyperlink");
281         g_object_set(tag, "foreground", "#0000ff", "underline", PANGO_UNDERLINE_SINGLE, "underline-set", TRUE, NULL);
282         g_hash_table_insert(default_text_grid_styles, "hyperlink", tag);
283
284         /* Initialise the default styles for a text buffer */
285         tag = gtk_text_tag_new("normal");
286         g_object_set(tag, "font-desc", default_font_desc, NULL);
287         g_hash_table_insert(default_text_buffer_styles, "normal", tag);
288
289         tag = gtk_text_tag_new("emphasized");
290         g_object_set(tag, "font-desc", default_font_desc, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
291         g_hash_table_insert(default_text_buffer_styles, "emphasized", tag);
292
293         tag = gtk_text_tag_new("preformatted");
294         g_object_set(tag, "font-desc", monospace_font_desc, NULL);
295         g_hash_table_insert(default_text_buffer_styles, "preformatted", tag);
296
297         tag = gtk_text_tag_new("header");
298         g_object_set(tag, "font-desc", default_font_desc, "size-points", 18.0, "weight", PANGO_WEIGHT_BOLD, NULL);
299         g_hash_table_insert(default_text_buffer_styles, "header", tag);
300
301         tag = gtk_text_tag_new("subheader");
302         g_object_set(tag, "font-desc", default_font_desc, "size-points", 14.0, "weight", PANGO_WEIGHT_BOLD, NULL);
303         g_hash_table_insert(default_text_buffer_styles, "subheader", tag);
304
305         tag = gtk_text_tag_new("alert");
306         g_object_set(tag, "font-desc", default_font_desc, "foreground", "#aa0000", "weight", PANGO_WEIGHT_BOLD, NULL);
307         g_hash_table_insert(default_text_buffer_styles, "alert", tag);
308
309         tag = gtk_text_tag_new("note");
310         g_object_set(tag, "font-desc", default_font_desc, "foreground", "#aaaa00", "weight", PANGO_WEIGHT_BOLD, NULL);
311         g_hash_table_insert(default_text_buffer_styles, "note", tag);
312
313         tag = gtk_text_tag_new("block-quote");
314         g_object_set(tag, "font-desc", default_font_desc, "justification", GTK_JUSTIFY_CENTER, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
315         g_hash_table_insert(default_text_buffer_styles, "block-quote", tag);
316
317         tag = gtk_text_tag_new("input");
318         g_object_set(tag, "font-desc", default_font_desc, NULL);
319         g_hash_table_insert(default_text_buffer_styles, "input", tag);
320
321         tag = gtk_text_tag_new("user1");
322         g_object_set(tag, "font-desc", default_font_desc, NULL);
323         g_hash_table_insert(default_text_buffer_styles, "user1", tag);
324
325         tag = gtk_text_tag_new("user2");
326         g_object_set(tag, "font-desc", default_font_desc, NULL);
327         g_hash_table_insert(default_text_buffer_styles, "user2", tag);
328
329         tag = gtk_text_tag_new("hyperlink");
330         g_object_set(tag, "font-desc", default_font_desc, "foreground", "#0000ff", "underline", PANGO_UNDERLINE_SINGLE, "underline-set", TRUE, NULL);
331         g_hash_table_insert(default_text_buffer_styles, "hyperlink", tag);
332
333         GtkTextTag *pager_tag = gtk_text_tag_new("pager");
334         g_object_set(pager_tag, "font-desc", default_font_desc, "foreground", "#ffffff", "background", "#000000", NULL);
335         g_hash_table_insert(default_text_buffer_styles, "pager", pager_tag);
336         text_tag_to_attr_list(pager_tag, priv->pager_attr_list);
337
338         pango_font_description_free(default_font_desc);
339         pango_font_description_free(monospace_font_desc);
340         
341         priv->styles->text_grid = default_text_grid_styles;
342         priv->styles->text_buffer = default_text_buffer_styles;
343
344
345         /* Initialize the GLK styles to empty tags */
346         int i;
347         for(i=0; i<style_NUMSTYLES; i++) {
348                 tag = gtk_text_tag_new(GLK_TAG_NAMES[i]);
349                 g_hash_table_insert(glk_text_grid_styles, (gchar*) GLK_TAG_NAMES[i], tag);
350                 g_hash_table_insert(glk_text_buffer_styles, (gchar*) GLK_TAG_NAMES[i], tag);
351         }
352
353         priv->glk_styles->text_grid = glk_text_grid_styles;
354         priv->glk_styles->text_buffer = glk_text_buffer_styles;
355
356 }
357
358 /* Reset style tables to the library's internal defaults */
359 void
360 reset_default_styles(ChimaraGlk *glk)
361 {
362         /* TODO: write this function */
363 }
364
365 /* Copy the default styles to the current styles
366  FIXME: This function is temporary and will not be needed later on */
367 void
368 copy_default_styles_to_current_styles(ChimaraGlk *glk)
369 {
370         /*
371         CHIMARA_GLK_USE_PRIVATE(glk, priv);
372         g_hash_table_foreach(priv->styles->text_grid, style_table_copy, priv->glk_styles->text_grid);
373         g_hash_table_foreach(priv->styles->text_buffer, style_table_copy, priv->glk_styles->text_buffer);
374
375         GtkTextTag *pager_tag = GTK_TEXT_TAG( g_hash_table_lookup(priv->styles->text_buffer, "pager") );
376         text_tag_to_attr_list(pager_tag, priv->pager_attr_list);
377         */
378 }
379
380 /* Create the CSS file scanner */
381 GScanner *
382 create_css_file_scanner(void)
383 {
384         GScanner *scanner = g_scanner_new(NULL);
385         scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z "#";
386         scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z "-_" G_CSET_DIGITS;
387         scanner->config->symbol_2_token = TRUE;
388         scanner->config->cpair_comment_single = NULL;
389         scanner->config->scan_float = FALSE;
390         return scanner;
391 }
392
393 /* Run the scanner over the CSS file, overriding the default styles */
394 void
395 scan_css_file(GScanner *scanner, ChimaraGlk *glk)
396 {
397         CHIMARA_GLK_USE_PRIVATE(glk, priv);
398
399         while( g_scanner_peek_next_token(scanner) != G_TOKEN_EOF) {
400                 if( !style_accept_style_selector(scanner, glk) )
401                         break;
402         }
403
404         g_scanner_destroy(scanner);
405
406         /* Update the pager prompt to the new style */
407         GtkTextTag *pager_tag = GTK_TEXT_TAG( g_hash_table_lookup(priv->styles->text_buffer, "pager") );
408         text_tag_to_attr_list(pager_tag, priv->pager_attr_list);
409 }
410
411 /* Internal function: parses a token */
412 static gboolean
413 style_accept(GScanner *scanner, GTokenType token)
414 {
415         GTokenType next = g_scanner_get_next_token(scanner);
416         if(next != token) {
417                 g_scanner_unexp_token(scanner, token, NULL, NULL, NULL, "CSS Error", 1);
418                 return FALSE;
419         }
420         return TRUE;
421 }
422
423 /* Internal function: parses a style selector */
424 static gboolean
425 style_accept_style_selector(GScanner *scanner, ChimaraGlk *glk)
426 {
427         CHIMARA_GLK_USE_PRIVATE(glk, priv);
428
429         GtkTextTag *current_tag;
430         gchar *field;
431         GTokenType token = g_scanner_get_next_token(scanner);
432         GTokenValue value = g_scanner_cur_value(scanner);
433
434         if(
435                 token != G_TOKEN_IDENTIFIER ||
436                 ( strcmp(value.v_identifier, "buffer") && strcmp(value.v_identifier, "grid") )
437         ) {
438                 g_scanner_error(scanner, "CSS Error: buffer/grid expected");
439                 return FALSE;
440         }
441
442         field = g_strdup(value.v_identifier);
443
444         if( !style_accept(scanner, '.') )
445                 return FALSE;
446
447         token = g_scanner_get_next_token(scanner);
448         value = g_scanner_cur_value(scanner);
449
450         if(token != G_TOKEN_IDENTIFIER) {
451                 g_scanner_error(scanner, "CSS Error: style selector expected");
452                 return FALSE;
453         }
454
455         if( !strcmp(field, "buffer") )
456                 current_tag = g_hash_table_lookup(priv->styles->text_buffer, value.v_identifier);
457         else
458                 current_tag = g_hash_table_lookup(priv->styles->text_grid, value.v_identifier);
459
460         if(current_tag == NULL) {
461                 g_scanner_error(scanner, "CSS Error: invalid style identifier");
462                 return FALSE;
463         }
464
465         if( !style_accept(scanner, '{') )
466                 return FALSE;
467
468         while( g_scanner_peek_next_token(scanner) != '}') {
469                 if( !style_accept_style_hint(scanner, current_tag) )
470                         return FALSE;
471         }
472                 
473         if( !style_accept(scanner, '}') )
474                 return FALSE;
475
476         return TRUE;
477 }
478
479 /* Internal function: parses a style hint */
480 static gboolean
481 style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag)
482 {
483         GTokenType token = g_scanner_get_next_token(scanner);
484         GTokenValue value = g_scanner_cur_value(scanner);
485         gchar *hint;
486
487         if(token != G_TOKEN_IDENTIFIER) {
488                 g_scanner_error(scanner, "CSS Error: style hint expected");
489                 return FALSE;
490         }
491
492         hint = g_strdup(value.v_identifier);
493
494         if( !style_accept(scanner, ':') )
495                 return FALSE;
496
497         token = g_scanner_get_next_token(scanner);
498         value = g_scanner_cur_value(scanner);
499
500         if( !strcmp(hint, "font-family") ) {
501                 if(token != G_TOKEN_STRING) {
502                         g_scanner_error(scanner, "CSS Error: string expected");
503                         return FALSE;
504                 }
505                 g_object_set(current_tag, "family", value.v_string, "family-set", TRUE, NULL);
506         }
507         else if( !strcmp(hint, "font-weight") ) {
508                 if(token != G_TOKEN_IDENTIFIER) {
509                         g_scanner_error(scanner, "CSS Error: bold/normal expected");
510                         return FALSE;
511                 }
512
513                 if( !strcmp(value.v_identifier, "bold") )
514                         g_object_set(current_tag, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
515                 else if( !strcmp(value.v_identifier, "normal") )
516                         g_object_set(current_tag, "weight", PANGO_WEIGHT_NORMAL, "weight-set", TRUE, NULL);
517                 else {
518                         g_scanner_error(scanner, "CSS Error: bold/normal expected");
519                         return FALSE;
520                 }
521         }
522         else if( !strcmp(hint, "font-style") ) {
523                 if(token != G_TOKEN_IDENTIFIER) {
524                         g_scanner_error(scanner, "CSS Error: italic/normal expected");
525                         return FALSE;
526                 }
527
528                 if( !strcmp(value.v_identifier, "italic") )
529                         g_object_set(current_tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
530                 else if( !strcmp(value.v_identifier, "normal") )
531                         g_object_set(current_tag, "style", PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
532                 else {
533                         g_scanner_error(scanner, "CSS Error: italic/normal expected");
534                         return FALSE;
535                 }
536         }
537         else if( !strcmp(hint, "font-size") ) {
538                 if(token == G_TOKEN_INT) 
539                         g_object_set(current_tag, "size-points", (float)value.v_int, "size-set", TRUE, NULL);
540                 else if(token == G_TOKEN_FLOAT)
541                         g_object_set(current_tag, "size-points", value.v_float, "size-set", TRUE, NULL);
542                 else {
543                         g_scanner_error(scanner, "CSS Error: integer or float expected");
544                         return FALSE;
545                 }
546         }
547         else if( !strcmp(hint, "color") ) {
548                 if(token != G_TOKEN_IDENTIFIER) {
549                         g_scanner_error(scanner, "CSS Error: hex color expected");
550                         return FALSE;
551                 }
552
553                 g_object_set(current_tag, "foreground", value.v_identifier, "foreground-set", TRUE, NULL);
554         }
555         else if( !strcmp(hint, "background-color") ) {
556                 if(token != G_TOKEN_IDENTIFIER) {
557                         g_scanner_error(scanner, "CSS Error: hex color expected");
558                         return FALSE;
559                 }
560                 g_object_set(current_tag, "background", value.v_identifier, "background-set", TRUE, NULL);
561         }
562         else if( !strcmp(hint, "text-align") ) {
563                 if(token != G_TOKEN_IDENTIFIER) {
564                         g_scanner_error(scanner, "CSS Error: left/right/center expected");
565                         return FALSE;
566                 }
567                 
568                 if( !strcmp(value.v_identifier, "left") )
569                         g_object_set(current_tag, "justification", GTK_JUSTIFY_LEFT, "justification-set", TRUE, NULL);
570                 else if( !strcmp(value.v_identifier, "right") )
571                         g_object_set(current_tag, "justification", GTK_JUSTIFY_RIGHT, "justification-set", TRUE, NULL);
572                 else if( !strcmp(value.v_identifier, "center") )
573                         g_object_set(current_tag, "justification", GTK_JUSTIFY_CENTER, "justification-set", TRUE, NULL);
574                 else {
575                         g_scanner_error(scanner, "CSS Error: left/right/center expected");
576                         return FALSE;
577                 }
578         }
579         else if( !strcmp(hint, "margin-left") ) {
580                 if(token != G_TOKEN_INT) {
581                         g_scanner_error(scanner, "CSS Error: integer expected");
582                         return FALSE;
583                 }
584                 g_object_set(current_tag, "left-margin", value.v_int, "left-margin-set", TRUE, NULL);
585         }
586         else if( !strcmp(hint, "margin-right") ) {
587                 if(token != G_TOKEN_INT) {
588                         g_scanner_error(scanner, "CSS Error: integer expected");
589                         return FALSE;
590                 }
591                 g_object_set(current_tag, "right-margin", value.v_int, "right-margin-set", TRUE, NULL);
592         }
593         else if( !strcmp(hint, "margin-top") ) {
594                 if(token != G_TOKEN_INT) {
595                         g_scanner_error(scanner, "CSS Error: integer expected");
596                         return FALSE;
597                 }
598                 g_object_set(current_tag, "pixels-above-lines", value.v_int, "pixels-above-lines-set", TRUE, NULL);
599         }
600         else if( !strcmp(hint, "margin-bottom") ) {
601                 if(token != G_TOKEN_INT) {
602                         g_scanner_error(scanner, "CSS Error: integer expected");
603                         return FALSE;
604                 }
605                 g_object_set(current_tag, "pixels-below-lines", value.v_int, "pixels-below-lines-set", TRUE, NULL);
606         }
607                 
608         else {
609                 g_scanner_error(scanner, "CSS Error: invalid style hint %s", hint);
610                 return FALSE;
611         }
612
613         if( !style_accept(scanner, ';') )
614                 return FALSE;
615
616         return TRUE;
617 }
618
619 /* Internal function: parses a glk color to a #hex-value */
620 static void
621 glkcolor_to_hex(glui32 val, gchar *buffer)
622 {
623         g_return_if_fail(buffer != NULL);
624
625         sprintf(buffer, "#%02X%02X%02X",
626                 ((val & 0xff0000) >> 16),
627                 ((val & 0x00ff00) >> 8),
628                 (val & 0x0000ff)
629         );
630 }
631
632 /* Internal function: parses a glk color to a GdkColor */
633 void
634 glkcolor_to_gdkcolor(glui32 val, GdkColor *color)
635 {
636         color->red = 256 * ((val & 0xff0000) >> 16);
637         color->green = 256 * ((val & 0x00ff00) >> 8);
638         color->blue = 256 * (val & 0x0000ff);
639 }
640
641 /* Internal function: parses a GdkColor to a glk color */
642 static glui32
643 gdkcolor_to_glkcolor(GdkColor *color)
644 {
645         g_return_val_if_fail(color != NULL, 0);
646         return (glui32) color->pixel;
647 }
648
649 /* Internal function: changes a GTK tag to correspond with the given style. */
650 static void
651 apply_stylehint_to_tag(GtkTextTag *tag, GtkTextTag *default_tag, glui32 wintype, glui32 hint, glsi32 val)
652 {
653         g_return_if_fail(tag != NULL);
654
655         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
656         GObject *tag_object = G_OBJECT(tag);
657
658         gint reverse_color = GPOINTER_TO_INT( g_object_get_data(tag_object, "reverse-color") );
659
660         int i = 0;
661         gchar color[20];
662         switch(hint) {
663         case stylehint_Indentation:
664                 g_object_set(tag_object, "left-margin", 5*val, "left-margin-set", TRUE, NULL);
665                 g_object_set(tag_object, "right-margin", 5*val, "right-margin-set", TRUE, NULL);
666                 break;
667         
668         case stylehint_ParaIndentation:
669                 g_object_set(tag_object, "indent", 5*val, "indent-set", TRUE, NULL);
670                 break;
671
672         case stylehint_Justification:
673                 switch(val) {
674                         case stylehint_just_LeftFlush:  i = GTK_JUSTIFY_LEFT; break;
675                         case stylehint_just_LeftRight:  i = GTK_JUSTIFY_FILL; break;
676                         case stylehint_just_Centered:   i = GTK_JUSTIFY_CENTER; break;
677                         case stylehint_just_RightFlush: i = GTK_JUSTIFY_RIGHT; break;
678                         default: 
679                                 WARNING("Unknown justification");
680                                 i = GTK_JUSTIFY_LEFT;
681                 }
682                 g_object_set(tag_object, "justification", i, "justification-set", TRUE, NULL);
683                 break;
684
685         case stylehint_Weight:
686                 switch(val) {
687                         case -1: i = PANGO_WEIGHT_LIGHT; break;
688                         case  0: i = PANGO_WEIGHT_NORMAL; break;
689                         case  1: i = PANGO_WEIGHT_BOLD; break;
690                         default: WARNING("Unknown font weight");
691                 }
692                 g_object_set(tag_object, "weight", i, "weight-set", TRUE, NULL);
693                 break;
694
695         case stylehint_Size:
696                 g_object_set(tag_object, "size", 14+(2*val), "size-set", TRUE, NULL);
697                 break;
698
699         case stylehint_Oblique:
700                 g_object_set(tag_object, "style", val ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
701                 break;
702
703         case stylehint_Proportional:
704         {
705                 gchar *font_family;
706                 GtkTextTag *font_tag = g_hash_table_lookup(
707                     wintype == wintype_TextBuffer? glk_data->styles->text_buffer : glk_data->styles->text_grid,
708                     val? "normal" : "preformatted");
709                 g_object_get(font_tag, "family", &font_family, NULL);
710                 g_object_set(tag_object, "family", font_family, "family-set", TRUE, NULL);
711                 g_free(font_family);
712         }
713                 break;
714
715         case stylehint_TextColor:
716                 glkcolor_to_hex(val, color);
717
718                 if(!reverse_color)
719                         g_object_set(tag_object, "foreground", color, "foreground-set", TRUE, NULL);
720                 else
721                         g_object_set(tag_object, "background", color, "background-set", TRUE, NULL);
722
723                 break;
724
725         case stylehint_BackColor:
726                 glkcolor_to_hex(val, color);
727
728                 if(!reverse_color)
729                         g_object_set(tag_object, "background", color, "background-set", TRUE, NULL);
730                 else
731                         g_object_set(tag_object, "foreground", color, "background-set", TRUE, NULL);
732
733                 break;
734
735         case stylehint_ReverseColor:
736                 if(reverse_color != val) {
737                         /* Flip the fore- and background colors */
738                         GdkColor* foreground_color;
739                         GdkColor* background_color;
740                         gint f_set, b_set, df_set, db_set = 0;
741                         g_object_get(tag_object, "foreground-set", &f_set, "background-set", &b_set, NULL);
742                         g_object_get(default_tag, "foreground-set", &df_set, "background-set", &db_set, NULL);
743
744                         if(f_set)
745                                 g_object_get(tag_object, "foreground-gdk", &foreground_color, NULL);
746                         else if(df_set)
747                                 g_object_get(default_tag, "foreground-gdk", &foreground_color, NULL);
748                         if(b_set)
749                                 g_object_get(tag_object, "background-gdk", &background_color, NULL);
750                         else if(db_set)
751                                 g_object_get(default_tag, "background-gdk", &background_color, NULL);
752
753                         if(b_set || db_set)
754                                 g_object_set(tag_object, "foreground-gdk", background_color, NULL);
755                         else
756                                 g_object_set(tag_object, "foreground", "#ffffff", NULL);
757
758                         if(f_set || df_set)
759                                 g_object_set(tag_object, "background-gdk", foreground_color, NULL);
760                         else
761                                 g_object_set(tag_object, "background", "#000000", NULL);
762
763                         g_object_set_data( tag_object, "reverse-color", GINT_TO_POINTER(val != 0) );
764                 }
765                 break;
766
767         default:
768                 WARNING("Unknown style hint");
769         }
770 }
771
772 /* Internal function: queries a text tag for the value of a given style hint */
773 static gint
774 query_tag(GtkTextTag *tag, glui32 wintype, glui32 hint)
775 {
776         gint intval;
777         GdkColor *colval;
778
779         g_return_val_if_fail(tag != NULL, 0);
780
781         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
782
783         switch(hint) {
784         case stylehint_Indentation:
785                 g_object_get(tag, "left_margin", &intval, NULL);
786                 return intval/5;
787         
788         case stylehint_ParaIndentation:
789                 g_object_get(tag, "indent", &intval, NULL);
790                 return intval/5;
791
792         case stylehint_Justification:
793                 g_object_get(tag, "justification", &intval, NULL);
794                 switch(intval) {
795                         case GTK_JUSTIFY_LEFT: return stylehint_just_LeftFlush;
796                         case GTK_JUSTIFY_FILL: return stylehint_just_LeftRight;
797                         case GTK_JUSTIFY_CENTER: return stylehint_just_Centered;
798                         case GTK_JUSTIFY_RIGHT: return stylehint_just_RightFlush;
799                         default: 
800                                 WARNING("Unknown justification");
801                                 return stylehint_just_LeftFlush;
802                 }
803
804         case stylehint_Weight:
805                 g_object_get(tag, "weight", &intval, NULL);
806                 switch(intval) {
807                         case PANGO_WEIGHT_LIGHT: return -1;
808                         case PANGO_WEIGHT_NORMAL: return 0;
809                         case PANGO_WEIGHT_BOLD: return 1;
810                         default: WARNING("Unknown font weight"); return 0;
811                 }
812
813         case stylehint_Size:
814                 g_object_get(tag, "size", &intval, NULL);
815                 return (intval/2)-14;
816
817         case stylehint_Oblique:
818                 g_object_get(tag, "style", &intval , NULL);
819                 return intval == PANGO_STYLE_ITALIC ? 1 : 0;
820
821         case stylehint_Proportional:
822                 /* Use pango_font_family_is_monospace()? */
823         {
824                 gchar *font_family, *query_font_family;
825                 GtkTextTag *font_tag = g_hash_table_lookup(
826                     wintype == wintype_TextBuffer? glk_data->styles->text_buffer : glk_data->styles->text_grid,
827                     "preformatted");
828                 g_object_get(font_tag, "family", &font_family, NULL);
829                 g_object_get(tag, "family", &query_font_family, NULL);
830                 gint retval = strcmp(font_family, query_font_family)? 0 : 1;
831                 g_free(font_family);
832                 g_free(query_font_family);
833                 return retval;
834         }
835
836         case stylehint_TextColor:
837                 g_object_get(tag, "foreground-gdk", &colval, NULL);
838                 return gdkcolor_to_glkcolor(colval);
839
840         case stylehint_BackColor:
841                 g_object_get(tag, "background-gdk", &colval, NULL);
842                 return gdkcolor_to_glkcolor(colval);
843
844         case stylehint_ReverseColor:
845                 return GPOINTER_TO_INT( g_object_get_data(G_OBJECT(tag), "reverse_color") );
846
847         default:
848                 WARNING("Unknown style hint");
849         }
850         
851         return 0;
852 }
853
854 /**
855  * glk_stylehint_set:
856  * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
857  * @styl: The style to set a hint for.
858  * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
859  * @val: The style hint. The meaning of this depends on @hint.
860  *
861  * Sets a hint about the appearance of one style for a particular type of 
862  * window. You can also set @wintype to %wintype_AllTypes, which sets a hint for 
863  * all types of window.
864  * <note><para>
865  *  There is no equivalent constant to set a hint for all styles of a single 
866  *  window type.
867  * </para></note>
868  */
869 void
870 glk_stylehint_set(glui32 wintype, glui32 styl, glui32 hint, glsi32 val)
871 {
872         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
873
874         GtkTextTag *to_change, *default_tag;
875         if(wintype == wintype_TextBuffer || wintype == wintype_AllTypes) {
876                 to_change = g_hash_table_lookup( glk_data->glk_styles->text_buffer, get_glk_tag_name(styl) );
877                 default_tag = g_hash_table_lookup( glk_data->styles->text_buffer, get_tag_name(styl) );
878                 apply_stylehint_to_tag(to_change, default_tag, wintype_TextBuffer, hint, val);
879         }
880
881         if(wintype == wintype_TextGrid || wintype == wintype_AllTypes) {
882                 to_change = g_hash_table_lookup( glk_data->glk_styles->text_grid, get_glk_tag_name(styl) );
883                 default_tag = g_hash_table_lookup( glk_data->styles->text_grid, get_tag_name(styl) );
884                 apply_stylehint_to_tag(to_change, default_tag, wintype_TextGrid, hint, val);
885         }
886 }
887
888 /**
889  * glk_stylehint_clear:
890  * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
891  * @styl: The style to set a hint for.
892  * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
893  *
894  * Clears a hint about the appearance of one style for a particular type of 
895  * window to its default value. You can also set @wintype to %wintype_AllTypes, 
896  * which clears a hint for all types of window.
897  * <note><para>
898  *  There is no equivalent constant to reset a hint for all styles of a single 
899  *  window type.
900  * </para></note>
901  */
902 void
903 glk_stylehint_clear(glui32 wintype, glui32 styl, glui32 hint)
904 {
905         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
906         GtkTextTag *tag;
907
908         switch(wintype) {
909         case wintype_TextBuffer:
910                 tag = g_hash_table_lookup( glk_data->styles->text_buffer, get_glk_tag_name(styl) );
911                 glk_stylehint_set( wintype, styl, hint, query_tag(tag, wintype, hint) );
912                 break;
913         case wintype_TextGrid:
914                 tag = g_hash_table_lookup( glk_data->styles->text_grid, get_glk_tag_name(styl) );
915                 glk_stylehint_set( wintype, styl, hint, query_tag(tag, wintype, hint) );
916         default:
917                 return;
918         }
919 }
920
921 /**
922  * glk_style_distinguish:
923  * @win: The window in which the styles are to be distinguished.
924  * @styl1: The first style to be distinguished from the second style.
925  * @styl2: The second style to be distinguished from the first style.
926  * 
927  * Decides whether two styles are visually distinguishable in the given window.
928  * The exact meaning of this is left for the library to determine.
929  * <note><title>Chimara</title><para>
930  *   Currently, all styles of one window are assumed to be mutually
931  *   distinguishable.
932  * </para></note>
933  * 
934  * Returns: %TRUE (1) if the two styles are visually distinguishable. If they 
935  * are not, it returns %FALSE (0).
936  */
937 glui32
938 glk_style_distinguish(winid_t win, glui32 styl1, glui32 styl2)
939 {
940         /* FIXME */
941         return styl1 != styl2;
942 }
943
944 /**
945  * glk_style_measure:
946  * @win: The window from which to take the style.
947  * @styl: The style to perform the measurement on.
948  * @hint: The stylehint to measure.
949  * @result: Address to write the result to.
950  * 
951  * Tries to test an attribute of one style in the given window @win. The library
952  * may not be able to determine the attribute; if not, this returns %FALSE (0).
953  * If it can, it returns %TRUE (1) and stores the value in the location pointed
954  * at by @result. 
955  * <note><para>
956  *   As usual, it is legal for @result to be %NULL, although fairly pointless.
957  * </para></note>
958  *
959  * The meaning of the value depends on the hint which was tested:
960  * <variablelist>
961  * <varlistentry>
962  *   <term>%stylehint_Indentation, %stylehint_ParaIndentation</term>
963  *   <listitem><para>The indentation and paragraph indentation. These are in a
964  *   metric which is platform-dependent.</para>
965  *   <note><para>Most likely either characters or pixels.</para></note>
966  *   </listitem>
967  * </varlistentry>
968  * <varlistentry>
969  *   <term>%stylehint_Justification</term>
970  *   <listitem><para>One of the constants %stylehint_just_LeftFlush,
971  *   %stylehint_just_LeftRight, %stylehint_just_Centered, or
972  *   %stylehint_just_RightFlush.</para></listitem>
973  * </varlistentry>
974  * <varlistentry>
975  *   <term>%stylehint_Size</term>
976  *   <listitem><para>The font size. Again, this is in a platform-dependent
977  *   metric.</para>
978  *   <note><para>Pixels, points, or simply 1 if the library does not support
979  *   varying font sizes.</para></note>
980  *   </listitem>
981  * </varlistentry>
982  * <varlistentry>
983  *   <term>%stylehint_Weight</term>
984  *   <listitem><para>1 for heavy-weight fonts (boldface), 0 for normal weight,
985  *   and -1 for light-weight fonts.</para></listitem>
986  * </varlistentry>
987  * <varlistentry>
988  *   <term>%stylehint_Oblique</term>
989  *   <listitem><para>1 for oblique fonts (italic), or 0 for normal angle.</para>
990  *   </listitem>
991  * </varlistentry>
992  * <varlistentry>
993  *   <term>%stylehint_Proportional</term>
994  *   <listitem><para>1 for proportional-width fonts, or 0 for fixed-width.
995  *   </para></listitem>
996  * </varlistentry>
997  * <varlistentry>
998  *   <term>%stylehint_TextColor, %stylehint_BackColor</term>
999  *   <listitem><para>These are values from 0x00000000 to 0x00FFFFFF, encoded as
1000  *   described in <link 
1001  *   linkend="chimara-Suggesting-the-Appearance-of-Styles">Suggesting the
1002  *   Appearance of Styles</link>.</para></listitem>
1003  * </varlistentry>
1004  * <varlistentry>
1005  *   <term>%stylehint_ReverseColor</term>
1006  *   <listitem><para>0 for normal printing, 1 if the foreground and background
1007  *   colors are reversed.</para></listitem>
1008  * </varlistentry>
1009  * </variablelist>
1010  * 
1011  * Returns: TRUE upon successul retrieval, otherwise FALSE.
1012  */
1013 glui32
1014 glk_style_measure(winid_t win, glui32 styl, glui32 hint, glui32 *result)
1015 {
1016         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
1017         GtkTextTag *tag;
1018
1019         switch(win->type) {
1020         case wintype_TextBuffer:
1021                 tag = g_hash_table_lookup( glk_data->glk_styles->text_buffer, get_glk_tag_name(styl) );
1022                 if(result)
1023                         *result = query_tag(tag, win->type, hint);
1024                 break;
1025         case wintype_TextGrid:
1026                 tag = g_hash_table_lookup( glk_data->glk_styles->text_grid, get_glk_tag_name(styl) );
1027                 if(result)
1028                         *result = query_tag(tag, win->type, hint);
1029         default:
1030                 return FALSE;
1031         }
1032
1033         return TRUE;
1034 }
1035
1036 /* Internal function returning the current default font for a window type
1037  * This can be used later for size calculations. Only wintype_TextGrid and wintype_TextBuffer are
1038  * supported for now */
1039 PangoFontDescription *
1040 get_current_font(guint32 wintype)
1041 {
1042         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
1043         GtkTextTag *normal;
1044
1045         switch(wintype) {
1046         case wintype_TextGrid:
1047                 normal = g_hash_table_lookup(glk_data->styles->text_grid, "normal");
1048                 break;
1049         case wintype_TextBuffer:
1050                 normal = g_hash_table_lookup(glk_data->styles->text_buffer, "normal");
1051                 break;
1052         default:
1053                 return NULL;
1054         }
1055
1056         PangoFontDescription *font;
1057         g_object_get( G_OBJECT(normal), "font-desc", &font, NULL );
1058
1059         return font;
1060 }
1061
1062 /* Internal function copying the attributes of a text tag to a pango attribute list */
1063 static void
1064 text_tag_to_attr_list(GtkTextTag *tag, PangoAttrList *list)
1065 {
1066         gboolean set;
1067         GdkColor *foreground, *background;
1068         gchar *string;
1069         PangoFontDescription *font_desc;
1070         gboolean strikethrough;
1071         PangoUnderline underline;
1072
1073         g_object_get(tag, "foreground-set", &set, "foreground-gdk", &foreground, NULL);
1074         if(set) {
1075                 pango_attr_list_insert(
1076                         list,
1077                         pango_attr_foreground_new(foreground->red, foreground->green, foreground->blue)
1078                 );
1079         }
1080         g_object_get(tag, "background-set", &set, "background-gdk", &background, NULL);
1081         if(set) {
1082                 pango_attr_list_insert(
1083                         list,
1084                         pango_attr_background_new(background->red, background->green, background->blue)
1085                 );
1086         }
1087         g_object_get(tag, "language-set", &set, "language", &string, NULL);
1088         if(set) {
1089                 pango_attr_list_insert(
1090                         list,
1091                         pango_attr_language_new( pango_language_from_string(string) )
1092                 );
1093         }
1094
1095         /* Font description updates the following properties simultaniously:
1096          * family, style, weight, variant, stretch, size
1097          */
1098         g_object_get(tag, "font-desc", &font_desc, NULL);
1099         pango_attr_list_insert(
1100                 list,
1101                 pango_attr_font_desc_new(font_desc)
1102         );
1103
1104         g_object_get(tag, "strikethrough-set", &set, "strikethrough", &strikethrough, NULL);
1105         if(set) {
1106                 pango_attr_list_insert(
1107                         list,
1108                         pango_attr_strikethrough_new(strikethrough)
1109                 );
1110         }
1111         g_object_get(tag, "underline-set", &set, "underline", &underline, NULL);
1112         if(set) {
1113                 pango_attr_list_insert(
1114                         list,
1115                         pango_attr_underline_new(underline)
1116                 );
1117         }
1118 }