[gtksourceview] stylescheme: add support for "weight" attribute



commit 9d7960ce18748b76d46102ea78651f3822f361d6
Author: Christian Hergert <chergert redhat com>
Date:   Mon Jan 17 11:17:30 2022 -0800

    stylescheme: add support for "weight" attribute
    
    With this commit you can now use an optional weight="thin" or any other
    supported PangoWeight nick in styles. Additionally, you may provide an
    integer value for the weight such as "600".

 docs/style-reference.md                |  8 ++++
 gtksourceview/gtksourcestyle-private.h |  4 +-
 gtksourceview/gtksourcestyle.c         | 54 +++++++++++++++++++++++---
 gtksourceview/gtksourcestylescheme.c   | 70 +++++++++++++++++++++++++++++++++-
 4 files changed, 128 insertions(+), 8 deletions(-)
---
diff --git a/docs/style-reference.md b/docs/style-reference.md
index 4ab8a932..e3cce236 100644
--- a/docs/style-reference.md
+++ b/docs/style-reference.md
@@ -83,6 +83,14 @@ Background color.
 
 - `bold`: `true` or `false`
 
+- `weight`
+
+The weight of the matched text block. The default value is "normal" but
+you may use any of the [enum@Pango.Weight] nicks or optionally a numeric
+value equivalent to the weights of CSS.
+
+Setting this value will override any setting of "bold".
+
 - `underline`
 
 Accepts the values supported by [enum@Pango.Underline] ("none", "single",
diff --git a/gtksourceview/gtksourcestyle-private.h b/gtksourceview/gtksourcestyle-private.h
index 01f87e8c..79220def 100644
--- a/gtksourceview/gtksourcestyle-private.h
+++ b/gtksourceview/gtksourcestyle-private.h
@@ -41,6 +41,7 @@ enum
        GTK_SOURCE_STYLE_USE_STRIKETHROUGH   = 1 << 6, /*< nick=use_strikethrough >*/
        GTK_SOURCE_STYLE_USE_SCALE           = 1 << 7, /*< nick=use_scale >*/
        GTK_SOURCE_STYLE_USE_UNDERLINE_COLOR = 1 << 8, /*< nick=use_underline_color >*/
+       GTK_SOURCE_STYLE_USE_WEIGHT          = 1 << 9, /*< nick=use_weight >*/
 };
 
 struct _GtkSourceStyle
@@ -57,11 +58,12 @@ struct _GtkSourceStyle
        const gchar *underline_color;
 
        PangoUnderline underline;
+       PangoWeight weight;
 
        guint italic : 1;
        guint bold : 1;
        guint strikethrough : 1;
-       guint mask : 12;
+       guint mask : 16;
 };
 
 G_END_DECLS
diff --git a/gtksourceview/gtksourcestyle.c b/gtksourceview/gtksourcestyle.c
index 53cd902e..0660397e 100644
--- a/gtksourceview/gtksourcestyle.c
+++ b/gtksourceview/gtksourcestyle.c
@@ -24,7 +24,7 @@
 
 /**
  * GtkSourceStyle:
- * 
+ *
  * Represents a style.
  *
  * The `GtkSourceStyle` structure is used to describe text attributes
@@ -62,7 +62,10 @@ enum
        PROP_SCALE,
        PROP_SCALE_SET,
        PROP_UNDERLINE_COLOR,
-       PROP_UNDERLINE_COLOR_SET
+       PROP_UNDERLINE_COLOR_SET,
+       PROP_WEIGHT,
+       PROP_WEIGHT_SET,
+       N_PROPS
 };
 
 static void
@@ -142,6 +145,15 @@ gtk_source_style_class_init (GtkSourceStyleClass *klass)
                                                              NULL,
                                                              G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
 
+       g_object_class_install_property (object_class,
+                                        PROP_WEIGHT,
+                                        g_param_spec_enum ("weight",
+                                                           "Weight",
+                                                           "Text weight",
+                                                           PANGO_TYPE_WEIGHT,
+                                                           PANGO_WEIGHT_NORMAL,
+                                                           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
+
        g_object_class_install_property (object_class,
                                         PROP_UNDERLINE_COLOR,
                                         g_param_spec_string ("underline-color",
@@ -214,6 +226,14 @@ gtk_source_style_class_init (GtkSourceStyleClass *klass)
                                                               FALSE,
                                                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
 
+       g_object_class_install_property (object_class,
+                                        PROP_WEIGHT_SET,
+                                        g_param_spec_boolean ("weight-set",
+                                                              "Weight set",
+                                                              "Whether weight attribute is set",
+                                                              FALSE,
+                                                              G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
+
        g_object_class_install_property (object_class,
                                         PROP_UNDERLINE_COLOR_SET,
                                         g_param_spec_boolean ("underline-color-set",
@@ -226,9 +246,6 @@ gtk_source_style_class_init (GtkSourceStyleClass *klass)
 static void
 gtk_source_style_init (GtkSourceStyle *style)
 {
-       style->foreground = NULL;
-       style->background = NULL;
-       style->line_background = NULL;
 }
 
 #define SET_MASK(style,name) (style)->mask |= (GTK_SOURCE_STYLE_USE_##name)
@@ -332,6 +349,11 @@ gtk_source_style_set_property (GObject      *object,
                        }
                        break;
 
+               case PROP_WEIGHT:
+                       style->weight = (PangoWeight) g_value_get_enum (value);
+                       SET_MASK (style, WEIGHT);
+                       break;
+
                case PROP_UNDERLINE_COLOR:
                        string = g_value_get_string (value);
                        if (string != NULL)
@@ -382,6 +404,10 @@ gtk_source_style_set_property (GObject      *object,
                        MODIFY_MASK (style, value, UNDERLINE_COLOR);
                        break;
 
+               case PROP_WEIGHT_SET:
+                       MODIFY_MASK (style, value, WEIGHT);
+                       break;
+
                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                        break;
@@ -434,6 +460,10 @@ gtk_source_style_get_property (GObject    *object,
                        g_value_set_string (value, style->underline_color);
                        break;
 
+               case PROP_WEIGHT:
+                       g_value_set_enum (value, style->weight);
+                       break;
+
                case PROP_FOREGROUND_SET:
                        GET_MASK (style, value, FOREGROUND);
                        break;
@@ -470,6 +500,10 @@ gtk_source_style_get_property (GObject    *object,
                        GET_MASK (style, value, UNDERLINE_COLOR);
                        break;
 
+               case PROP_WEIGHT_SET:
+                       GET_MASK (style, value, WEIGHT);
+                       break;
+
                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                        break;
@@ -505,6 +539,7 @@ gtk_source_style_copy (const GtkSourceStyle *style)
        copy->strikethrough = style->strikethrough;
        copy->mask = style->mask;
        copy->scale = style->scale;
+       copy->weight = style->weight;
 
        return copy;
 }
@@ -651,6 +686,15 @@ gtk_source_style_apply (const GtkSourceStyle *style,
                        g_object_set (tag, "scale-set", FALSE, NULL);
                }
 
+               if (style->mask & GTK_SOURCE_STYLE_USE_WEIGHT)
+               {
+                       g_object_set (tag, "weight", style->weight, NULL);
+               }
+               else
+               {
+                       g_object_set (tag, "weight-set", FALSE, NULL);
+               }
+
                g_object_thaw_notify (G_OBJECT (tag));
        }
        else
diff --git a/gtksourceview/gtksourcestylescheme.c b/gtksourceview/gtksourcestylescheme.c
index 64417c0a..a0fbe065 100644
--- a/gtksourceview/gtksourcestylescheme.c
+++ b/gtksourceview/gtksourcestylescheme.c
@@ -1029,13 +1029,35 @@ generate_css_style (GtkSourceStyleScheme *scheme)
 }
 
 static gboolean
-parse_bool (char *value)
+parse_bool (const char *value)
 {
        return (g_ascii_strcasecmp (value, "true") == 0 ||
                g_ascii_strcasecmp (value, "yes") == 0 ||
                g_ascii_strcasecmp (value, "1") == 0);
 }
 
+static gboolean
+parse_int (int        *out_value,
+           const char *str)
+{
+       gint64 v;
+
+       if (str == NULL)
+               return FALSE;
+
+       if (str[0] < '0' || str[0] > '9')
+               return FALSE;
+
+       v = g_ascii_strtoll (str, NULL, 10);
+
+       if (v < G_MININT || v > G_MAXINT)
+               return FALSE;
+
+       *out_value = v;
+
+       return TRUE;
+}
+
 static void
 get_bool (xmlNode    *node,
           const char *propname,
@@ -1074,6 +1096,7 @@ parse_style (GtkSourceStyleScheme  *scheme,
        xmlChar *underline = NULL;
        xmlChar *underline_color = NULL;
        xmlChar *scale = NULL;
+       xmlChar *weight = NULL;
        xmlChar *tmp;
 
        tmp = xmlGetProp (node, BAD_CAST "name");
@@ -1112,6 +1135,7 @@ parse_style (GtkSourceStyleScheme  *scheme,
        underline = xmlGetProp (node, BAD_CAST "underline");
        underline_color = xmlGetProp (node, BAD_CAST "underline-color");
        scale = xmlGetProp (node, BAD_CAST "scale");
+       weight = xmlGetProp (node, BAD_CAST "weight");
 
        if (use_style)
        {
@@ -1121,7 +1145,8 @@ parse_style (GtkSourceStyleScheme  *scheme,
                    mask != 0 ||
                    underline != NULL ||
                    underline_color != NULL ||
-                   scale != NULL)
+                   scale != NULL ||
+                   weight != NULL)
                {
                        g_set_error (error, ERROR_QUARK, 0,
                                     "in style '%s': style attributes used along with use-style",
@@ -1134,6 +1159,7 @@ parse_style (GtkSourceStyleScheme  *scheme,
                        xmlFree (underline);
                        xmlFree (underline_color);
                        xmlFree (scale);
+                       xmlFree (weight);
                        return FALSE;
                }
 
@@ -1210,6 +1236,45 @@ parse_style (GtkSourceStyleScheme  *scheme,
                        result->scale = g_intern_string ((char*) scale);
                        result->mask |= GTK_SOURCE_STYLE_USE_SCALE;
                }
+
+               if (weight != NULL)
+               {
+                       GEnumClass *enum_class;
+                       GEnumValue *enum_value;
+                       char *weight_lowercase;
+                       int v;
+
+                       enum_class = G_ENUM_CLASS (g_type_class_ref (PANGO_TYPE_WEIGHT));
+
+                       weight_lowercase = g_ascii_strdown ((char*) weight, -1);
+                       enum_value = g_enum_get_value_by_nick (enum_class, weight_lowercase);
+                       g_free (weight_lowercase);
+
+                       if (enum_value != NULL)
+                       {
+                               result->weight = enum_value->value;
+                               result->mask |= GTK_SOURCE_STYLE_USE_WEIGHT;
+                               result->mask &= ~GTK_SOURCE_STYLE_USE_BOLD;
+                       }
+                       else if (parse_int (&v, weight_lowercase))
+                       {
+                               result->weight = v;
+                               result->mask |= GTK_SOURCE_STYLE_USE_WEIGHT;
+                               result->mask &= ~GTK_SOURCE_STYLE_USE_BOLD;
+                       }
+                       else
+                       {
+                               static gboolean has_warned;
+
+                               if (!has_warned)
+                               {
+                                       g_warning ("Failed to parse style attribute weight=\"%s\"", weight);
+                                       has_warned = TRUE;
+                               }
+                       }
+
+                       g_type_class_unref (enum_class);
+               }
        }
 
        *style_p = result;
@@ -1221,6 +1286,7 @@ parse_style (GtkSourceStyleScheme  *scheme,
        xmlFree (underline);
        xmlFree (underline_color);
        xmlFree (scale);
+       xmlFree (weight);
 
        return TRUE;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]