4 #include "chimara-glk-private.h"
11 extern GPrivate *glk_data_key;
13 static gboolean style_accept(GScanner *scanner, GTokenType token);
14 static gboolean style_accept_style_selector(GScanner *scanner, ChimaraGlk *glk);
15 static gboolean style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag);
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 static void style_cascade_colors(GtkTextTag *tag, GtkTextTag *glk_tag, GtkTextTag *default_tag, GdkColor **foreground, GdkColor **background);
23 * @styl: The style to apply
25 * Changes the style of the current output stream. @styl should be one of the
26 * <code>style_</code> constants. However, any value is actually legal; if the
27 * interpreter does not recognize the style value, it will treat it as
30 * This policy allows for the future definition of styles without breaking old
35 glk_set_style(glui32 styl)
37 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
38 g_return_if_fail(glk_data->current_stream != NULL);
39 glk_set_style_stream(glk_data->current_stream, styl);
42 /* The first 11 tag names must correspond to the first 11 glk tag names as defined below */
43 static const gchar* TAG_NAMES[] = {
60 /* The first 11 glk tag names must correspond to the first 11 tag names as defined above */
61 static const gchar* GLK_TAG_NAMES[] = {
81 /* Internal function: mapping from style enum to tag name */
83 get_tag_name(glui32 style)
85 if(style >= CHIMARA_NUM_STYLES) {
86 WARNING("Unsupported style");
89 return (gchar*) TAG_NAMES[style];
93 /* Internal function: mapping from glk style enum to tag name */
95 get_glk_tag_name(glui32 style)
97 if(style >= style_NUMSTYLES) {
98 WARNING("Unsupported style");
101 return (gchar*) GLK_TAG_NAMES[style];
106 * glk_set_style_stream:
107 * @str: Output stream to change the style of
108 * @styl: The style to apply
110 * This changes the style of the stream @str. See glk_set_style().
113 glk_set_style_stream(strid_t str, glui32 styl) {
115 g_printf("glk_set_style(str->rock=%d, styl=%d)\n", str->rock, styl);
118 if(str->window == NULL)
121 flush_window_buffer(str->window);
122 str->style = (gchar*) get_tag_name(styl);
123 str->glk_style = (gchar*) get_glk_tag_name(styl);
126 /* Internal function: call this to initialize the layout of the 'more' prompt. */
128 style_init_more_prompt(winid_t win)
130 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
132 win->pager_layout = gtk_widget_create_pango_layout(win->widget, "More");
133 pango_layout_set_attributes(win->pager_layout, glk_data->pager_attr_list);
136 /* Internal function: call this to initialize the default styles to a textbuffer. */
138 style_init_textbuffer(GtkTextBuffer *buffer)
140 g_return_if_fail(buffer != NULL);
142 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
144 /* Place the default text tags in the textbuffer's tag table */
145 g_hash_table_foreach(glk_data->styles->text_buffer, style_copy_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
147 /* Copy the override text tags to the textbuffers's tag table */
148 g_hash_table_foreach(glk_data->glk_styles->text_buffer, style_copy_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
152 /* Internal function: call this to initialize the default styles to a textgrid. */
154 style_init_textgrid(GtkTextBuffer *buffer)
156 g_return_if_fail(buffer != NULL);
158 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
160 /* Place the default text tags in the textbuffer's tag table */
161 g_hash_table_foreach(glk_data->styles->text_grid, style_copy_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
163 /* Copy the current text tags to the textbuffers's tag table */
164 g_hash_table_foreach(glk_data->glk_styles->text_grid, style_copy_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
167 /* Internal function used to iterate over a style table, copying it */
169 style_copy_tag_to_textbuffer(gpointer key, gpointer tag, gpointer target_table)
171 g_return_if_fail(key != NULL);
172 g_return_if_fail(tag != NULL);
173 g_return_if_fail(target_table != NULL);
175 gtk_text_tag_table_add(target_table, gtk_text_tag_copy( GTK_TEXT_TAG(tag) ));
178 /* Internal function that copies a text tag */
180 gtk_text_tag_copy(GtkTextTag *tag)
184 g_return_val_if_fail(tag != NULL, NULL);
186 copy = gtk_text_tag_new(tag->name);
187 gtk_text_attributes_copy_values(tag->values, copy->values);
189 #define _COPY_FLAG(flag) copy->flag = tag->flag
190 _COPY_FLAG (bg_color_set);
191 _COPY_FLAG (bg_color_set);
192 _COPY_FLAG (bg_stipple_set);
193 _COPY_FLAG (fg_color_set);
194 _COPY_FLAG (fg_stipple_set);
195 _COPY_FLAG (justification_set);
196 _COPY_FLAG (left_margin_set);
197 _COPY_FLAG (indent_set);
198 _COPY_FLAG (rise_set);
199 _COPY_FLAG (strikethrough_set);
200 _COPY_FLAG (right_margin_set);
201 _COPY_FLAG (pixels_above_lines_set);
202 _COPY_FLAG (pixels_below_lines_set);
203 _COPY_FLAG (pixels_inside_wrap_set);
204 _COPY_FLAG (tabs_set);
205 _COPY_FLAG (underline_set);
206 _COPY_FLAG (wrap_mode_set);
207 _COPY_FLAG (bg_full_height_set);
208 _COPY_FLAG (invisible_set);
209 _COPY_FLAG (editable_set);
210 _COPY_FLAG (language_set);
211 _COPY_FLAG (scale_set);
214 /* Copy the data that was added manually */
215 gpointer reverse_color = g_object_get_data( G_OBJECT(tag), "reverse-color" );
218 g_object_set_data( G_OBJECT(copy), "reverse-color", reverse_color );
224 /* Internal function that constructs the default styles */
226 style_init(ChimaraGlk *glk)
228 CHIMARA_GLK_USE_PRIVATE(glk, priv);
230 GHashTable *default_text_grid_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
231 GHashTable *default_text_buffer_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
232 GHashTable *glk_text_grid_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
233 GHashTable *glk_text_buffer_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
236 /* Initialise the default styles for a text grid */
237 tag = gtk_text_tag_new("default");
238 g_object_set(tag, "family", "Monospace", NULL);
239 g_hash_table_insert(default_text_grid_styles, "default", tag);
241 tag = gtk_text_tag_new("normal");
242 g_hash_table_insert(default_text_grid_styles, "normal", tag);
244 tag = gtk_text_tag_new("emphasized");
245 g_object_set(tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
246 g_hash_table_insert(default_text_grid_styles, "emphasized", tag);
248 tag = gtk_text_tag_new("preformatted");
249 g_hash_table_insert(default_text_grid_styles, "preformatted", tag);
251 tag = gtk_text_tag_new("header");
252 g_object_set(tag, "weight", PANGO_WEIGHT_BOLD, NULL);
253 g_hash_table_insert(default_text_grid_styles, "header", tag);
255 tag = gtk_text_tag_new("subheader");
256 g_object_set(tag, "weight", PANGO_WEIGHT_BOLD, NULL);
257 g_hash_table_insert(default_text_grid_styles, "subheader", tag);
259 tag = gtk_text_tag_new("alert");
260 g_object_set(tag, "foreground", "#aa0000", "weight", PANGO_WEIGHT_BOLD, NULL);
261 g_hash_table_insert(default_text_grid_styles, "alert", tag);
263 tag = gtk_text_tag_new("note");
264 g_object_set(tag, "foreground", "#aaaa00", "weight", PANGO_WEIGHT_BOLD, NULL);
265 g_hash_table_insert(default_text_grid_styles, "note", tag);
267 tag = gtk_text_tag_new("block-quote");
268 g_object_set(tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
269 g_hash_table_insert(default_text_grid_styles, "block-quote", tag);
271 tag = gtk_text_tag_new("input");
272 g_hash_table_insert(default_text_grid_styles, "input", tag);
274 tag = gtk_text_tag_new("user1");
275 g_hash_table_insert(default_text_grid_styles, "user1", tag);
277 tag = gtk_text_tag_new("user2");
278 g_hash_table_insert(default_text_grid_styles, "user2", tag);
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);
284 /* Initialise the default styles for a text buffer */
285 tag = gtk_text_tag_new("default");
286 g_object_set(tag, "family", "Serif", NULL);
287 g_hash_table_insert(default_text_buffer_styles, "default", tag);
289 tag = gtk_text_tag_new("normal");
290 g_hash_table_insert(default_text_buffer_styles, "normal", tag);
292 tag = gtk_text_tag_new("emphasized");
293 g_object_set(tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
294 g_hash_table_insert(default_text_buffer_styles, "emphasized", tag);
296 tag = gtk_text_tag_new("preformatted");
297 g_object_set(tag, "family", "Monospace", NULL);
298 g_hash_table_insert(default_text_buffer_styles, "preformatted", tag);
300 tag = gtk_text_tag_new("header");
301 g_object_set(tag, "weight", PANGO_WEIGHT_BOLD, NULL);
302 g_hash_table_insert(default_text_buffer_styles, "header", tag);
304 tag = gtk_text_tag_new("subheader");
305 g_object_set(tag, "weight", PANGO_WEIGHT_BOLD, NULL);
306 g_hash_table_insert(default_text_buffer_styles, "subheader", tag);
308 tag = gtk_text_tag_new("alert");
309 g_object_set(tag, "foreground", "#aa0000", "weight", PANGO_WEIGHT_BOLD, NULL);
310 g_hash_table_insert(default_text_buffer_styles, "alert", tag);
312 tag = gtk_text_tag_new("note");
313 g_object_set(tag, "foreground", "#aaaa00", "weight", PANGO_WEIGHT_BOLD, NULL);
314 g_hash_table_insert(default_text_buffer_styles, "note", tag);
316 tag = gtk_text_tag_new("block-quote");
317 g_object_set(tag, "justification", GTK_JUSTIFY_CENTER, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
318 g_hash_table_insert(default_text_buffer_styles, "block-quote", tag);
320 tag = gtk_text_tag_new("input");
321 g_hash_table_insert(default_text_buffer_styles, "input", tag);
323 tag = gtk_text_tag_new("user1");
324 g_hash_table_insert(default_text_buffer_styles, "user1", tag);
326 tag = gtk_text_tag_new("user2");
327 g_hash_table_insert(default_text_buffer_styles, "user2", tag);
329 tag = gtk_text_tag_new("hyperlink");
330 g_object_set(tag, "foreground", "#0000ff", "underline", PANGO_UNDERLINE_SINGLE, "underline-set", TRUE, NULL);
331 g_hash_table_insert(default_text_buffer_styles, "hyperlink", tag);
333 GtkTextTag *pager_tag = gtk_text_tag_new("pager");
334 g_object_set(pager_tag, "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);
338 priv->styles->text_grid = default_text_grid_styles;
339 priv->styles->text_buffer = default_text_buffer_styles;
342 /* Initialize the GLK styles to empty tags */
344 for(i=0; i<style_NUMSTYLES; i++) {
345 tag = gtk_text_tag_new(GLK_TAG_NAMES[i]);
346 g_hash_table_insert(glk_text_grid_styles, (gchar*) GLK_TAG_NAMES[i], tag);
347 tag = gtk_text_tag_new(GLK_TAG_NAMES[i]);
348 g_hash_table_insert(glk_text_buffer_styles, (gchar*) GLK_TAG_NAMES[i], tag);
351 priv->glk_styles->text_grid = glk_text_grid_styles;
352 priv->glk_styles->text_buffer = glk_text_buffer_styles;
356 /* Reset style tables to the library's internal defaults */
358 reset_default_styles(ChimaraGlk *glk)
360 /* TODO: write this function */
363 /* Create the CSS file scanner */
365 create_css_file_scanner(void)
367 GScanner *scanner = g_scanner_new(NULL);
368 scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z "#";
369 scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z "-_" G_CSET_DIGITS;
370 scanner->config->symbol_2_token = TRUE;
371 scanner->config->cpair_comment_single = NULL;
372 scanner->config->scan_float = FALSE;
376 /* Run the scanner over the CSS file, overriding the default styles */
378 scan_css_file(GScanner *scanner, ChimaraGlk *glk)
380 CHIMARA_GLK_USE_PRIVATE(glk, priv);
382 while( g_scanner_peek_next_token(scanner) != G_TOKEN_EOF) {
383 if( !style_accept_style_selector(scanner, glk) )
387 g_scanner_destroy(scanner);
389 /* Update the pager prompt to the new style */
390 GtkTextTag *pager_tag = GTK_TEXT_TAG( g_hash_table_lookup(priv->styles->text_buffer, "pager") );
391 text_tag_to_attr_list(pager_tag, priv->pager_attr_list);
394 /* Internal function: parses a token */
396 style_accept(GScanner *scanner, GTokenType token)
398 GTokenType next = g_scanner_get_next_token(scanner);
400 g_scanner_unexp_token(scanner, token, NULL, NULL, NULL, "CSS Error", 1);
406 /* Internal function: parses a style selector */
408 style_accept_style_selector(GScanner *scanner, ChimaraGlk *glk)
410 CHIMARA_GLK_USE_PRIVATE(glk, priv);
412 GtkTextTag *current_tag;
414 GTokenType token = g_scanner_get_next_token(scanner);
415 GTokenValue value = g_scanner_cur_value(scanner);
418 token != G_TOKEN_IDENTIFIER ||
419 ( strcmp(value.v_identifier, "buffer") && strcmp(value.v_identifier, "grid") )
421 g_scanner_error(scanner, "CSS Error: buffer/grid expected");
425 field = g_strdup(value.v_identifier);
427 /* Parse the tag name to change */
428 if( g_scanner_peek_next_token(scanner) == '{') {
429 style_accept(scanner, '{');
430 if( !strcmp(field, "buffer") )
431 current_tag = g_hash_table_lookup(priv->styles->text_buffer, "default");
433 current_tag = g_hash_table_lookup(priv->styles->text_grid, "default");
435 if( !style_accept(scanner, '.') )
438 token = g_scanner_get_next_token(scanner);
439 value = g_scanner_cur_value(scanner);
441 if(token != G_TOKEN_IDENTIFIER) {
442 g_scanner_error(scanner, "CSS Error: style selector expected");
446 if( !strcmp(field, "buffer") )
447 current_tag = g_hash_table_lookup(priv->styles->text_buffer, value.v_identifier);
449 current_tag = g_hash_table_lookup(priv->styles->text_grid, value.v_identifier);
451 if(current_tag == NULL) {
452 g_scanner_error(scanner, "CSS Error: invalid style identifier");
456 if( !style_accept(scanner, '{') )
460 while( g_scanner_peek_next_token(scanner) != '}') {
461 if( !style_accept_style_hint(scanner, current_tag) )
465 if( !style_accept(scanner, '}') )
471 /* Internal function: parses a style hint */
473 style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag)
475 GTokenType token = g_scanner_get_next_token(scanner);
476 GTokenValue value = g_scanner_cur_value(scanner);
479 if(token != G_TOKEN_IDENTIFIER) {
480 g_scanner_error(scanner, "CSS Error: style hint expected");
484 hint = g_strdup(value.v_identifier);
486 if( !style_accept(scanner, ':') )
489 token = g_scanner_get_next_token(scanner);
490 value = g_scanner_cur_value(scanner);
492 if( !strcmp(hint, "font-family") ) {
493 if(token != G_TOKEN_STRING) {
494 g_scanner_error(scanner, "CSS Error: string expected");
497 g_object_set(current_tag, "family", value.v_string, "family-set", TRUE, NULL);
499 else if( !strcmp(hint, "font-weight") ) {
500 if(token != G_TOKEN_IDENTIFIER) {
501 g_scanner_error(scanner, "CSS Error: bold/normal expected");
505 if( !strcmp(value.v_identifier, "bold") )
506 g_object_set(current_tag, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
507 else if( !strcmp(value.v_identifier, "normal") )
508 g_object_set(current_tag, "weight", PANGO_WEIGHT_NORMAL, "weight-set", TRUE, NULL);
510 g_scanner_error(scanner, "CSS Error: bold/normal expected");
514 else if( !strcmp(hint, "font-style") ) {
515 if(token != G_TOKEN_IDENTIFIER) {
516 g_scanner_error(scanner, "CSS Error: italic/normal expected");
520 if( !strcmp(value.v_identifier, "italic") )
521 g_object_set(current_tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
522 else if( !strcmp(value.v_identifier, "normal") )
523 g_object_set(current_tag, "style", PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
525 g_scanner_error(scanner, "CSS Error: italic/normal expected");
529 else if( !strcmp(hint, "font-size") ) {
530 if(token == G_TOKEN_INT)
531 g_object_set(current_tag, "size-points", (float)value.v_int, "size-set", TRUE, NULL);
532 else if(token == G_TOKEN_FLOAT)
533 g_object_set(current_tag, "size-points", value.v_float, "size-set", TRUE, NULL);
535 g_scanner_error(scanner, "CSS Error: integer or float expected");
539 else if( !strcmp(hint, "color") ) {
540 if(token != G_TOKEN_IDENTIFIER) {
541 g_scanner_error(scanner, "CSS Error: hex color expected");
545 g_object_set(current_tag, "foreground", value.v_identifier, "foreground-set", TRUE, NULL);
547 else if( !strcmp(hint, "background-color") ) {
548 if(token != G_TOKEN_IDENTIFIER) {
549 g_scanner_error(scanner, "CSS Error: hex color expected");
552 g_object_set(current_tag, "background", value.v_identifier, "background-set", TRUE, NULL);
554 else if( !strcmp(hint, "text-align") ) {
555 if(token != G_TOKEN_IDENTIFIER) {
556 g_scanner_error(scanner, "CSS Error: left/right/center expected");
560 if( !strcmp(value.v_identifier, "left") )
561 g_object_set(current_tag, "justification", GTK_JUSTIFY_LEFT, "justification-set", TRUE, NULL);
562 else if( !strcmp(value.v_identifier, "right") )
563 g_object_set(current_tag, "justification", GTK_JUSTIFY_RIGHT, "justification-set", TRUE, NULL);
564 else if( !strcmp(value.v_identifier, "center") )
565 g_object_set(current_tag, "justification", GTK_JUSTIFY_CENTER, "justification-set", TRUE, NULL);
567 g_scanner_error(scanner, "CSS Error: left/right/center expected");
571 else if( !strcmp(hint, "margin-left") ) {
572 if(token != G_TOKEN_INT) {
573 g_scanner_error(scanner, "CSS Error: integer expected");
576 g_object_set(current_tag, "left-margin", value.v_int, "left-margin-set", TRUE, NULL);
578 else if( !strcmp(hint, "margin-right") ) {
579 if(token != G_TOKEN_INT) {
580 g_scanner_error(scanner, "CSS Error: integer expected");
583 g_object_set(current_tag, "right-margin", value.v_int, "right-margin-set", TRUE, NULL);
585 else if( !strcmp(hint, "margin-top") ) {
586 if(token != G_TOKEN_INT) {
587 g_scanner_error(scanner, "CSS Error: integer expected");
590 g_object_set(current_tag, "pixels-above-lines", value.v_int, "pixels-above-lines-set", TRUE, NULL);
592 else if( !strcmp(hint, "margin-bottom") ) {
593 if(token != G_TOKEN_INT) {
594 g_scanner_error(scanner, "CSS Error: integer expected");
597 g_object_set(current_tag, "pixels-below-lines", value.v_int, "pixels-below-lines-set", TRUE, NULL);
601 g_scanner_error(scanner, "CSS Error: invalid style hint %s", hint);
605 if( !style_accept(scanner, ';') )
611 /* Internal function: parses a glk color to a GdkColor */
613 glkcolor_to_gdkcolor(glui32 val, GdkColor *color)
615 color->red = 256 * ((val & 0xff0000) >> 16);
616 color->green = 256 * ((val & 0x00ff00) >> 8);
617 color->blue = 256 * (val & 0x0000ff);
620 /* Internal function: parses a glk color to a hex string */
622 glkcolor_to_hex(glui32 val)
624 return g_strdup_printf("%04X%04X%04X",
625 256 * ((val & 0xff0000) >> 16),
626 256 * ((val & 0x00ff00) >> 8),
627 256 * (val & 0x0000ff)
631 /* Internal function: parses a gdk color to a hex string */
633 gdkcolor_to_hex(GdkColor *color)
635 return g_strdup_printf("%04X%04X%04X",
636 color->red, color->green, color->blue
640 /* Internal function: parses a GdkColor to a glk color */
642 gdkcolor_to_glkcolor(GdkColor *color)
644 g_return_val_if_fail(color != NULL, 0);
645 return (glui32) color->pixel;
648 /* Internal function: changes a GTK tag to correspond with the given style. */
650 apply_stylehint_to_tag(GtkTextTag *tag, glui32 wintype, glui32 styl, glui32 hint, glsi32 val)
652 g_return_if_fail(tag != NULL);
654 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
655 GObject *tag_object = G_OBJECT(tag);
657 gint reverse_color = GPOINTER_TO_INT( g_object_get_data(tag_object, "reverse-color") );
662 case stylehint_Indentation:
663 g_object_set(tag_object, "left-margin", 5*val, "left-margin-set", TRUE, NULL);
664 g_object_set(tag_object, "right-margin", 5*val, "right-margin-set", TRUE, NULL);
667 case stylehint_ParaIndentation:
668 g_object_set(tag_object, "indent", 5*val, "indent-set", TRUE, NULL);
671 case stylehint_Justification:
673 case stylehint_just_LeftFlush: i = GTK_JUSTIFY_LEFT; break;
674 case stylehint_just_LeftRight: i = GTK_JUSTIFY_FILL; break;
675 case stylehint_just_Centered: i = GTK_JUSTIFY_CENTER; break;
676 case stylehint_just_RightFlush: i = GTK_JUSTIFY_RIGHT; break;
678 WARNING("Unknown justification");
679 i = GTK_JUSTIFY_LEFT;
681 g_object_set(tag_object, "justification", i, "justification-set", TRUE, NULL);
684 case stylehint_Weight:
686 case -1: i = PANGO_WEIGHT_LIGHT; break;
687 case 0: i = PANGO_WEIGHT_NORMAL; break;
688 case 1: i = PANGO_WEIGHT_BOLD; break;
689 default: WARNING("Unknown font weight");
691 g_object_set(tag_object, "weight", i, "weight-set", TRUE, NULL);
696 gdouble scale = PANGO_SCALE_MEDIUM;
698 case -3: scale = PANGO_SCALE_XX_SMALL; break;
699 case -2: scale = PANGO_SCALE_X_SMALL; break;
700 case -1: scale = PANGO_SCALE_SMALL; break;
701 case 0: scale = PANGO_SCALE_MEDIUM; break;
702 case 1: scale = PANGO_SCALE_LARGE; break;
703 case 2: scale = PANGO_SCALE_X_LARGE; break;
704 case 3: scale = PANGO_SCALE_XX_LARGE; break;
706 /* We follow Pango's convention of having each magnification
707 step be a scaling of 1.2 */
708 scale = pow(1.2, (double)val);
710 g_object_set(tag_object, "scale", scale, "scale-set", TRUE, NULL);
714 case stylehint_Oblique:
715 g_object_set(tag_object, "style", val ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
718 case stylehint_Proportional:
721 GtkTextTag *font_tag = g_hash_table_lookup(
722 wintype == wintype_TextBuffer? glk_data->styles->text_buffer : glk_data->styles->text_grid,
723 val? "default" : "preformatted");
724 g_object_get(font_tag, "family", &font_family, NULL);
725 g_object_set(tag_object, "family", font_family, "family-set", TRUE, NULL);
730 case stylehint_TextColor:
731 glkcolor_to_gdkcolor(val, &color);
734 g_object_set(tag_object, "foreground-gdk", &color, "foreground-set", TRUE, NULL);
736 g_object_set(tag_object, "background-gdk", &color, "background-set", TRUE, NULL);
740 case stylehint_BackColor:
741 glkcolor_to_gdkcolor(val, &color);
744 g_object_set(tag_object, "background-gdk", &color, "background-set", TRUE, NULL);
746 g_object_set(tag_object, "foreground-gdk", &color, "background-set", TRUE, NULL);
750 case stylehint_ReverseColor:
752 // Determine the current colors
754 // If all fails, use black/white
755 // FIXME: Use system theme here
756 GdkColor foreground, background;
757 gdk_color_parse("black", &foreground);
758 gdk_color_parse("white", &background);
759 GdkColor *current_foreground = &foreground;
760 GdkColor *current_background = &background;
762 if(wintype == wintype_TextBuffer) {
763 GtkTextTag* default_tag = g_hash_table_lookup(glk_data->styles->text_buffer, "default");
764 GtkTextTag* base_tag = g_hash_table_lookup(glk_data->styles->text_buffer, get_tag_name(styl));
765 style_cascade_colors(base_tag, tag, default_tag, ¤t_foreground, ¤t_background);
767 else if(wintype == wintype_TextGrid) {
768 GtkTextTag* default_tag = g_hash_table_lookup(glk_data->styles->text_grid, "default");
769 GtkTextTag* base_tag = g_hash_table_lookup(glk_data->styles->text_grid, get_tag_name(styl));
770 style_cascade_colors(base_tag, tag, default_tag, ¤t_foreground, ¤t_background);
774 /* Flip the fore- and background colors */
775 GdkColor *temp = current_foreground;
776 current_foreground = current_background;
777 current_background = temp;
781 "foreground-gdk", current_foreground,
782 "foreground-set", TRUE,
783 "background-gdk", current_background,
784 "background-set", TRUE,
788 g_object_set_data( tag_object, "reverse-color", GINT_TO_POINTER(val != 0) );
793 WARNING("Unknown style hint");
797 /* Internal function: queries a text tag for the value of a given style hint */
799 query_tag(GtkTextTag *tag, glui32 wintype, glui32 hint)
805 g_return_val_if_fail(tag != NULL, 0);
807 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
810 case stylehint_Indentation:
811 g_object_get(tag, "left_margin", &intval, NULL);
814 case stylehint_ParaIndentation:
815 g_object_get(tag, "indent", &intval, NULL);
818 case stylehint_Justification:
819 g_object_get(tag, "justification", &intval, NULL);
821 case GTK_JUSTIFY_LEFT: return stylehint_just_LeftFlush;
822 case GTK_JUSTIFY_FILL: return stylehint_just_LeftRight;
823 case GTK_JUSTIFY_CENTER: return stylehint_just_Centered;
824 case GTK_JUSTIFY_RIGHT: return stylehint_just_RightFlush;
826 WARNING("Unknown justification");
827 return stylehint_just_LeftFlush;
830 case stylehint_Weight:
831 g_object_get(tag, "weight", &intval, NULL);
833 case PANGO_WEIGHT_LIGHT: return -1;
834 case PANGO_WEIGHT_NORMAL: return 0;
835 case PANGO_WEIGHT_BOLD: return 1;
836 default: WARNING("Unknown font weight"); return 0;
840 g_object_get(tag, "scale", &doubleval, NULL);
841 return (gint)round(log(doubleval) / log(1.2));
843 case stylehint_Oblique:
844 g_object_get(tag, "style", &intval , NULL);
845 return intval == PANGO_STYLE_ITALIC ? 1 : 0;
847 case stylehint_Proportional:
848 /* Use pango_font_family_is_monospace()? */
850 gchar *font_family, *query_font_family;
851 GtkTextTag *font_tag = g_hash_table_lookup(
852 wintype == wintype_TextBuffer? glk_data->styles->text_buffer : glk_data->styles->text_grid,
854 g_object_get(font_tag, "family", &font_family, NULL);
855 g_object_get(tag, "family", &query_font_family, NULL);
856 gint retval = strcmp(font_family, query_font_family)? 0 : 1;
858 g_free(query_font_family);
862 case stylehint_TextColor:
863 g_object_get(tag, "foreground-gdk", &colval, NULL);
864 return gdkcolor_to_glkcolor(colval);
866 case stylehint_BackColor:
867 g_object_get(tag, "background-gdk", &colval, NULL);
868 return gdkcolor_to_glkcolor(colval);
870 case stylehint_ReverseColor:
871 return GPOINTER_TO_INT( g_object_get_data(G_OBJECT(tag), "reverse_color") );
874 WARNING("Unknown style hint");
882 * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
883 * @styl: The style to set a hint for.
884 * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
885 * @val: The style hint. The meaning of this depends on @hint.
887 * Sets a hint about the appearance of one style for a particular type of
888 * window. You can also set @wintype to %wintype_AllTypes, which sets a hint for
889 * all types of window.
891 * There is no equivalent constant to set a hint for all styles of a single
896 glk_stylehint_set(glui32 wintype, glui32 styl, glui32 hint, glsi32 val)
899 g_printf("glk_stylehint_set(wintype=%d, styl=%d, hint=%d, val=%d)\n", wintype, styl, hint, val);
902 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
904 GtkTextTag *to_change;
905 if(wintype == wintype_TextBuffer || wintype == wintype_AllTypes) {
906 to_change = g_hash_table_lookup( glk_data->glk_styles->text_buffer, get_glk_tag_name(styl) );
907 apply_stylehint_to_tag(to_change, wintype_TextBuffer, styl, hint, val);
910 if(wintype == wintype_TextGrid || wintype == wintype_AllTypes) {
911 to_change = g_hash_table_lookup( glk_data->glk_styles->text_grid, get_glk_tag_name(styl) );
912 apply_stylehint_to_tag(to_change, wintype_TextGrid, styl, hint, val);
917 * glk_stylehint_clear:
918 * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
919 * @styl: The style to set a hint for.
920 * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
922 * Clears a hint about the appearance of one style for a particular type of
923 * window to its default value. You can also set @wintype to %wintype_AllTypes,
924 * which clears a hint for all types of window.
926 * There is no equivalent constant to reset a hint for all styles of a single
931 glk_stylehint_clear(glui32 wintype, glui32 styl, glui32 hint)
934 g_printf("glk_stylehint_clear(wintype=%d, styl=%d, hint=%d)\n", wintype, styl, hint);
937 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
941 case wintype_TextBuffer:
942 tag = g_hash_table_lookup( glk_data->glk_styles->text_buffer, get_glk_tag_name(styl) );
944 glk_stylehint_set( wintype, styl, hint, query_tag(tag, wintype, hint) );
947 case wintype_TextGrid:
948 tag = g_hash_table_lookup( glk_data->glk_styles->text_grid, get_glk_tag_name(styl) );
950 glk_stylehint_set( wintype, styl, hint, query_tag(tag, wintype, hint) );
958 * glk_style_distinguish:
959 * @win: The window in which the styles are to be distinguished.
960 * @styl1: The first style to be distinguished from the second style.
961 * @styl2: The second style to be distinguished from the first style.
963 * Decides whether two styles are visually distinguishable in the given window.
964 * The exact meaning of this is left for the library to determine.
965 * <note><title>Chimara</title><para>
966 * Currently, all styles of one window are assumed to be mutually
970 * Returns: %TRUE (1) if the two styles are visually distinguishable. If they
971 * are not, it returns %FALSE (0).
974 glk_style_distinguish(winid_t win, glui32 styl1, glui32 styl2)
977 g_printf("glk_style_distinguish(win->rock=%d, styl1=%d, styl2=%d)\n", win->rock, styl1, styl2);
981 return styl1 != styl2;
986 * @win: The window from which to take the style.
987 * @styl: The style to perform the measurement on.
988 * @hint: The stylehint to measure.
989 * @result: Address to write the result to.
991 * Tries to test an attribute of one style in the given window @win. The library
992 * may not be able to determine the attribute; if not, this returns %FALSE (0).
993 * If it can, it returns %TRUE (1) and stores the value in the location pointed
996 * As usual, it is legal for @result to be %NULL, although fairly pointless.
999 * The meaning of the value depends on the hint which was tested:
1002 * <term>%stylehint_Indentation, %stylehint_ParaIndentation</term>
1003 * <listitem><para>The indentation and paragraph indentation. These are in a
1004 * metric which is platform-dependent.</para>
1005 * <note><para>Most likely either characters or pixels.</para></note>
1009 * <term>%stylehint_Justification</term>
1010 * <listitem><para>One of the constants %stylehint_just_LeftFlush,
1011 * %stylehint_just_LeftRight, %stylehint_just_Centered, or
1012 * %stylehint_just_RightFlush.</para></listitem>
1015 * <term>%stylehint_Size</term>
1016 * <listitem><para>The font size. Again, this is in a platform-dependent
1018 * <note><para>Pixels, points, or simply 1 if the library does not support
1019 * varying font sizes.</para></note>
1023 * <term>%stylehint_Weight</term>
1024 * <listitem><para>1 for heavy-weight fonts (boldface), 0 for normal weight,
1025 * and -1 for light-weight fonts.</para></listitem>
1028 * <term>%stylehint_Oblique</term>
1029 * <listitem><para>1 for oblique fonts (italic), or 0 for normal angle.</para>
1033 * <term>%stylehint_Proportional</term>
1034 * <listitem><para>1 for proportional-width fonts, or 0 for fixed-width.
1035 * </para></listitem>
1038 * <term>%stylehint_TextColor, %stylehint_BackColor</term>
1039 * <listitem><para>These are values from 0x00000000 to 0x00FFFFFF, encoded as
1040 * described in <link
1041 * linkend="chimara-Suggesting-the-Appearance-of-Styles">Suggesting the
1042 * Appearance of Styles</link>.</para></listitem>
1045 * <term>%stylehint_ReverseColor</term>
1046 * <listitem><para>0 for normal printing, 1 if the foreground and background
1047 * colors are reversed.</para></listitem>
1050 * Signed values, such as the %stylehint_Weight value, are cast to
1051 * <type>glui32</type>. They may be cast to <type>glsi32</type> to be dealt with
1052 * in a more natural context.
1054 * Returns: TRUE upon successul retrieval, otherwise FALSE.
1057 glk_style_measure(winid_t win, glui32 styl, glui32 hint, glui32 *result)
1060 g_printf("glk_style_measure(win->rock=%d, styl=%d, hint=%d, result=...)\n", win->rock, styl, hint);
1063 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
1067 case wintype_TextBuffer:
1068 tag = g_hash_table_lookup( glk_data->glk_styles->text_buffer, get_glk_tag_name(styl) );
1070 *result = query_tag(tag, win->type, hint);
1072 case wintype_TextGrid:
1073 tag = g_hash_table_lookup( glk_data->glk_styles->text_grid, get_glk_tag_name(styl) );
1075 *result = query_tag(tag, win->type, hint);
1083 /* Internal function returning the current default font for a window type
1084 * This can be used later for size calculations. Only wintype_TextGrid and wintype_TextBuffer are
1085 * supported for now */
1086 PangoFontDescription *
1087 get_current_font(guint32 wintype)
1089 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
1093 case wintype_TextGrid:
1094 tag = g_hash_table_lookup(glk_data->styles->text_grid, "default");
1095 return pango_font_description_from_string("Monospace");
1097 case wintype_TextBuffer:
1098 tag = g_hash_table_lookup(glk_data->styles->text_buffer, "default");
1099 return pango_font_description_from_string("Serif");
1105 PangoFontDescription *font;
1109 /* Internal function copying the attributes of a text tag to a pango attribute list */
1111 text_tag_to_attr_list(GtkTextTag *tag, PangoAttrList *list)
1114 GdkColor *foreground, *background;
1116 PangoFontDescription *font_desc;
1117 gboolean strikethrough;
1118 PangoUnderline underline;
1120 g_object_get(tag, "foreground-set", &set, "foreground-gdk", &foreground, NULL);
1122 pango_attr_list_insert(
1124 pango_attr_foreground_new(foreground->red, foreground->green, foreground->blue)
1127 g_object_get(tag, "background-set", &set, "background-gdk", &background, NULL);
1129 pango_attr_list_insert(
1131 pango_attr_background_new(background->red, background->green, background->blue)
1134 g_object_get(tag, "language-set", &set, "language", &string, NULL);
1136 pango_attr_list_insert(
1138 pango_attr_language_new( pango_language_from_string(string) )
1142 /* Font description updates the following properties simultaniously:
1143 * family, style, weight, variant, stretch, size.
1144 * FIXME: Except it doesn't really.
1146 g_object_get(tag, "font-desc", &font_desc, NULL);
1147 pango_attr_list_insert(
1149 pango_attr_font_desc_new(font_desc)
1152 g_object_get(tag, "strikethrough-set", &set, "strikethrough", &strikethrough, NULL);
1154 pango_attr_list_insert(
1156 pango_attr_strikethrough_new(strikethrough)
1159 g_object_get(tag, "underline-set", &set, "underline", &underline, NULL);
1161 pango_attr_list_insert(
1163 pango_attr_underline_new(underline)
1168 /* Update pager and reverse video tags */
1170 style_update(ChimaraGlk *glk)
1172 CHIMARA_GLK_USE_PRIVATE(glk, priv);
1174 GtkTextTag *pager_tag = GTK_TEXT_TAG( g_hash_table_lookup(priv->styles->text_buffer, "pager") );
1175 text_tag_to_attr_list(pager_tag, priv->pager_attr_list);
1178 /* Determine the current colors used to render the text for a given stream.
1179 * This can be set in a number of places */
1181 style_cascade_colors(GtkTextTag *tag, GtkTextTag *glk_tag, GtkTextTag *default_tag, GdkColor **foreground, GdkColor **background)
1183 gboolean foreground_set = FALSE;
1184 gboolean background_set = FALSE;
1188 reverse_color = GPOINTER_TO_INT( g_object_get_data(G_OBJECT(default_tag), "reverse-color") );
1189 g_object_get(default_tag, "foreground-set", &foreground_set, "background-set", &background_set, NULL);
1191 g_object_get(default_tag, "foreground-gdk", reverse_color ? background:foreground, NULL);
1193 g_object_get(default_tag, "background-gdk", reverse_color ? foreground:background, NULL);
1195 // Player defined color
1196 reverse_color = GPOINTER_TO_INT( g_object_get_data(G_OBJECT(tag), "reverse-color") );
1197 g_object_get(tag, "foreground-set", &foreground_set, "background-set", &background_set, NULL);
1199 g_object_get(tag, "foreground-gdk", reverse_color ? background:foreground, NULL);
1201 g_object_get(tag, "background-gdk", reverse_color ? foreground:background, NULL);
1203 // GLK defined color
1204 reverse_color = GPOINTER_TO_INT( g_object_get_data(G_OBJECT(glk_tag), "reverse-color") );
1205 g_object_get(glk_tag, "foreground-set", &foreground_set, "background-set", &background_set, NULL);
1207 g_object_get(glk_tag, "foreground-gdk", reverse_color ? background:foreground, NULL);
1209 g_object_get(glk_tag, "background-gdk", reverse_color ? foreground:background, NULL);
1213 /* Determine the current colors used to render the text for a given stream.
1214 * This can be set in a number of places */
1216 style_stream_colors(strid_t str, GdkColor **foreground, GdkColor **background)
1218 VALID_STREAM(str, return);
1219 g_return_if_fail(str->window != NULL);
1220 g_return_if_fail(str->window->type != wintype_TextBuffer || str->window->type != wintype_TextGrid);
1222 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(str->window->widget) );
1223 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
1224 GtkTextTag* default_tag = gtk_text_tag_table_lookup(tags, "default");
1225 GtkTextTag* tag = gtk_text_tag_table_lookup(tags, str->style);
1226 GtkTextTag* glk_tag = gtk_text_tag_table_lookup(tags, str->glk_style);
1228 style_cascade_colors(tag, glk_tag, default_tag, foreground, background);
1230 gboolean foreground_set, background_set;
1232 // Windows can have zcolors defined
1233 if(str->window->zcolor) {
1234 g_object_get(str->window->zcolor, "foreground-set", &foreground_set, "background-set", &background_set, NULL);
1236 g_object_get(str->window->zcolor, "foreground-gdk", foreground, NULL);
1238 g_object_get(str->window->zcolor, "background-gdk", background, NULL);