+#ifdef DEBUG_STYLES
+ g_printf("glk_style_measure(win->rock=%d, styl=%d, hint=%d, result=...)\n", win->rock, styl, hint);
+#endif
+
+ ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
+ GtkTextTag *tag;
+
+ switch(win->type) {
+ case wintype_TextBuffer:
+ tag = g_hash_table_lookup( glk_data->glk_styles->text_buffer, get_glk_tag_name(styl) );
+ if(result)
+ *result = query_tag(tag, win->type, hint);
+ break;
+ case wintype_TextGrid:
+ tag = g_hash_table_lookup( glk_data->glk_styles->text_grid, get_glk_tag_name(styl) );
+ if(result)
+ *result = query_tag(tag, win->type, hint);
+ default:
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+/* Internal function returning the current default font for a window type
+ * This can be used later for size calculations. Only wintype_TextGrid and wintype_TextBuffer are
+ * supported for now */
+PangoFontDescription *
+get_current_font(guint32 wintype)
+{
+ ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
+ GtkTextTag *tag;
+
+ switch(wintype) {
+ case wintype_TextGrid:
+ tag = g_hash_table_lookup(glk_data->styles->text_grid, "default");
+ return pango_font_description_from_string("Monospace");
+ break;
+ case wintype_TextBuffer:
+ tag = g_hash_table_lookup(glk_data->styles->text_buffer, "default");
+ return pango_font_description_from_string("Serif");
+ break;
+ default:
+ return NULL;
+ }
+
+ PangoFontDescription *font;
+ return font;
+}
+
+/* Internal function copying the attributes of a text tag to a pango attribute list */
+static void
+text_tag_to_attr_list(GtkTextTag *tag, PangoAttrList *list)
+{
+ gboolean set;
+ GdkColor *foreground, *background;
+ gchar *string;
+ PangoFontDescription *font_desc;
+ gboolean strikethrough;
+ PangoUnderline underline;
+
+ g_object_get(tag, "foreground-set", &set, "foreground-gdk", &foreground, NULL);
+ if(set) {
+ pango_attr_list_insert(
+ list,
+ pango_attr_foreground_new(foreground->red, foreground->green, foreground->blue)
+ );
+ }
+ g_object_get(tag, "background-set", &set, "background-gdk", &background, NULL);
+ if(set) {
+ pango_attr_list_insert(
+ list,
+ pango_attr_background_new(background->red, background->green, background->blue)
+ );
+ }
+ g_object_get(tag, "language-set", &set, "language", &string, NULL);
+ if(set) {
+ pango_attr_list_insert(
+ list,
+ pango_attr_language_new( pango_language_from_string(string) )
+ );
+ }
+
+ /* Font description updates the following properties simultaniously:
+ * family, style, weight, variant, stretch, size.
+ * FIXME: Except it doesn't really.
+ */
+ g_object_get(tag, "font-desc", &font_desc, NULL);
+ pango_attr_list_insert(
+ list,
+ pango_attr_font_desc_new(font_desc)
+ );
+
+ g_object_get(tag, "strikethrough-set", &set, "strikethrough", &strikethrough, NULL);
+ if(set) {
+ pango_attr_list_insert(
+ list,
+ pango_attr_strikethrough_new(strikethrough)
+ );
+ }
+ g_object_get(tag, "underline-set", &set, "underline", &underline, NULL);
+ if(set) {
+ pango_attr_list_insert(
+ list,
+ pango_attr_underline_new(underline)
+ );
+ }
+}
+
+/* Update pager tag */
+void
+style_update(ChimaraGlk *glk)
+{
+ CHIMARA_GLK_USE_PRIVATE(glk, priv);
+
+ GtkTextTag *pager_tag = GTK_TEXT_TAG( g_hash_table_lookup(priv->styles->text_buffer, "pager") );
+ text_tag_to_attr_list(pager_tag, priv->pager_attr_list);
+}
+
+/* Determine the current colors used to render the text for a given stream.
+ * This can be set in a number of places */
+static void
+style_cascade_colors(GtkTextTag *tag, GtkTextTag *glk_tag, GtkTextTag *default_tag, GdkColor **foreground, GdkColor **background)
+{
+ gboolean foreground_set = FALSE;
+ gboolean background_set = FALSE;
+ gint reverse_color;
+
+ // Default color
+ reverse_color = GPOINTER_TO_INT( g_object_get_data(G_OBJECT(default_tag), "reverse-color") );
+ g_object_get(default_tag, "foreground-set", &foreground_set, "background-set", &background_set, NULL);
+ if(foreground_set)
+ g_object_get(default_tag, "foreground-gdk", reverse_color ? background:foreground, NULL);
+ if(background_set)
+ g_object_get(default_tag, "background-gdk", reverse_color ? foreground:background, NULL);
+
+ // Player defined color
+ reverse_color = GPOINTER_TO_INT( g_object_get_data(G_OBJECT(tag), "reverse-color") );
+ g_object_get(tag, "foreground-set", &foreground_set, "background-set", &background_set, NULL);
+ if(foreground_set)
+ g_object_get(tag, "foreground-gdk", reverse_color ? background:foreground, NULL);
+ if(background_set)
+ g_object_get(tag, "background-gdk", reverse_color ? foreground:background, NULL);
+
+ // GLK defined color
+ reverse_color = GPOINTER_TO_INT( g_object_get_data(G_OBJECT(glk_tag), "reverse-color") );
+ g_object_get(glk_tag, "foreground-set", &foreground_set, "background-set", &background_set, NULL);
+ if(foreground_set)
+ g_object_get(glk_tag, "foreground-gdk", reverse_color ? background:foreground, NULL);
+ if(background_set)
+ g_object_get(glk_tag, "background-gdk", reverse_color ? foreground:background, NULL);
+
+}
+
+/* Determine the current colors used to render the text for a given stream.
+ * This can be set in a number of places */
+void
+style_stream_colors(strid_t str, GdkColor **foreground, GdkColor **background)
+{
+ VALID_STREAM(str, return);
+ g_return_if_fail(str->window != NULL);
+ g_return_if_fail(str->window->type != wintype_TextBuffer || str->window->type != wintype_TextGrid);
+
+ GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(str->window->widget) );
+ GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
+ GtkTextTag* default_tag = gtk_text_tag_table_lookup(tags, "default");
+ GtkTextTag* tag = gtk_text_tag_table_lookup(tags, str->style);
+ GtkTextTag* glk_tag = gtk_text_tag_table_lookup(tags, str->glk_style);
+
+ style_cascade_colors(tag, glk_tag, default_tag, foreground, background);
+
+ gboolean foreground_set, background_set;
+
+ // Windows can have zcolors defined
+ if(str->window->zcolor) {
+ g_object_get(str->window->zcolor, "foreground-set", &foreground_set, "background-set", &background_set, NULL);
+ if(foreground_set)
+ g_object_get(str->window->zcolor, "foreground-gdk", foreground, NULL);
+ if(background_set)
+ g_object_get(str->window->zcolor, "background-gdk", background, NULL);
+ }