diff --git a/app/text/gimptextlayout.c b/app/text/gimptextlayout.c index ad30ee901ca716e6011819558083b72cf98d3c0c..38faf72e5425bd1967e504fb715990d486036ab2 100644 --- a/app/text/gimptextlayout.c +++ b/app/text/gimptextlayout.c @@ -20,10 +20,13 @@ #include "config.h" +#include + #include #include #include "libgimpbase/gimpbase.h" +#include "libgimpcolor/gimpcolor.h" #include "libgimpmath/gimpmath.h" #include "text-types.h" @@ -49,7 +52,7 @@ struct _GimpTextLayout static void gimp_text_layout_finalize (GObject *object); static void gimp_text_layout_position (GimpTextLayout *layout); -static void gimp_text_layout_set_attrs (GimpTextLayout *layout); +static void gimp_text_layout_set_markup (GimpTextLayout *layout); static PangoContext * gimp_text_get_pango_context (GimpText *text, gdouble xres, @@ -144,14 +147,7 @@ gimp_text_layout_new (GimpText *text, pango_layout_set_font_description (layout->layout, font_desc); pango_font_description_free (font_desc); - gimp_text_layout_set_attrs (layout); - - if (text->markup) - pango_layout_set_markup (layout->layout, text->markup, -1); - else if (text->text) - pango_layout_set_text (layout->layout, text->text, -1); - else - pango_layout_set_text (layout->layout, NULL, 0); + gimp_text_layout_set_markup (layout); switch (text->justify) { @@ -458,40 +454,113 @@ gimp_text_layout_untransform_distance (GimpTextLayout *layout, } } -static void -gimp_text_layout_set_attrs (GimpTextLayout *layout) +static gboolean +gimp_text_layout_split_markup (const gchar *markup, + gchar **open_tag, + gchar **content, + gchar **close_tag) { - GimpText *text = layout->text; - PangoAttrList *attrs; - PangoAttribute *attr; + gchar *p_open; + gchar *p_close; + + p_open = strstr (markup, ""); + if (! p_open) + return FALSE; + + *open_tag = g_strndup (markup, p_open - markup + strlen ("")); + + p_close = g_strrstr (markup, ""); + if (! p_close) + { + g_free (*open_tag); + return FALSE; + } + + *close_tag = g_strdup (p_close); - attrs = pango_layout_get_attributes (layout->layout); - if (attrs) - pango_attr_list_ref (attrs); + if (p_open + strlen ("") < p_close) + { + *content = g_strndup (p_open + strlen (""), + p_close - p_open - strlen ("")); + } else - attrs = pango_attr_list_new (); + { + *content = g_strdup (""); + } + + return TRUE; +} + +static gchar * +gimp_text_layout_apply_tags (GimpTextLayout *layout, + const gchar *markup) +{ + GimpText *text = layout->text; + gchar *result; - attr = pango_attr_foreground_new (text->color.r * 65535, - text->color.g * 65535, - text->color.b * 65535); + { + guchar r, g, b; - attr->start_index = 0; - attr->end_index = -1; + gimp_rgb_get_uchar (&text->color, &r, &g, &b); - pango_attr_list_insert (attrs, attr); + result = g_strdup_printf ("%s", + r, g, b, markup); + } if (fabs (text->letter_spacing) > 0.1) { - attr = pango_attr_letter_spacing_new (text->letter_spacing * PANGO_SCALE); + gchar *tmp = g_strdup_printf ("%s", + (gint) (text->letter_spacing * PANGO_SCALE), + result); + g_free (result); + result = tmp; + } - attr->start_index = 0; - attr->end_index = -1; + return result; +} + +static void +gimp_text_layout_set_markup (GimpTextLayout *layout) +{ + GimpText *text = layout->text; + gchar *open_tag = NULL; + gchar *content = NULL; + gchar *close_tag = NULL; + gchar *tagged; + gchar *markup; + + if (text->markup) + { + if (! gimp_text_layout_split_markup (text->markup, + &open_tag, &content, &close_tag)) + { + open_tag = g_strdup (""); + content = g_strdup (""); + close_tag = g_strdup (""); + } + } + else + { + open_tag = g_strdup (""); + close_tag = g_strdup (""); - pango_attr_list_insert (attrs, attr); + if (text->text) + content = g_markup_escape_text (text->text, -1); + else + content = g_strdup (""); } - pango_layout_set_attributes (layout->layout, attrs); - pango_attr_list_unref (attrs); + tagged = gimp_text_layout_apply_tags (layout, content); + + g_free (content); + + markup = g_strconcat (open_tag, tagged, close_tag, NULL); + + g_free (open_tag); + g_free (tagged); + g_free (close_tag); + + pango_layout_set_markup (layout->layout, markup, -1); } static void