[gtksourceview] Allow #rgba() colors in named colors



commit a622c34673f629a8281faf471397edcd6cd7baa6
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Aug 16 18:28:25 2015 +0200

    Allow #rgba() colors in named colors
    
    Fix bug https://bugzilla.gnome.org/show_bug.cgi?id=740910
    and add a unit test

 gtksourceview/gtksourcestylescheme.c |   37 +++++++---
 tests/Makefile.am                    |    7 ++-
 tests/styles/test.xml                |   13 +++
 tests/test-stylescheme.c             |  140 ++++++++++++++++++++++++++++++++++
 4 files changed, 186 insertions(+), 11 deletions(-)
---
diff --git a/gtksourceview/gtksourcestylescheme.c b/gtksourceview/gtksourcestylescheme.c
index a40c68e..544ed80 100644
--- a/gtksourceview/gtksourcestylescheme.c
+++ b/gtksourceview/gtksourcestylescheme.c
@@ -373,6 +373,29 @@ gtk_source_style_scheme_get_filename (GtkSourceStyleScheme *scheme)
 }
 
 /*
+ * Try to parse a color string.
+ * If the color can be parsed, return the offset in the string
+ * with the real start of the color (either the string itself, or after
+ * the initial '#' character).
+ */
+static const gchar *
+color_parse (const gchar *color,
+             GdkRGBA     *rgba)
+{
+       if ((*color == '#') && gdk_rgba_parse (rgba, color + 1))
+       {
+               return color + 1;
+       }
+
+       if (gdk_rgba_parse (rgba, color))
+       {
+               return color;
+       }
+
+       return NULL;
+}
+
+/*
  * get_color_by_name:
  * @scheme: a #GtkSourceStyleScheme.
  * @name: color name to find.
@@ -396,15 +419,9 @@ get_color_by_name (GtkSourceStyleScheme *scheme,
        {
                GdkRGBA dummy;
 
-               if (gdk_rgba_parse (&dummy, name + 1))
-               {
-                       color = name + 1;
-               }
-               else if (gdk_rgba_parse (&dummy, name))
-               {
-                       color = name;
-               }
-               else
+               color = color_parse (name, &dummy);
+
+               if (color == NULL)
                {
                        g_warning ("could not parse color '%s'", name);
                }
@@ -582,7 +599,7 @@ get_color (GtkSourceStyle *style,
 
                if (style->mask & mask)
                {
-                       if (color == NULL || !gdk_rgba_parse (dest, color))
+                       if (color == NULL || !color_parse (color, dest))
                        {
                                g_warning ("%s: invalid color '%s'", G_STRLOC,
                                           color != NULL ? color : "(null)");
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 3029aab..344fe06 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -114,6 +114,9 @@ test_region_SOURCES = test-region.c
 UNIT_TEST_PROGS += test-search-context
 test_search_context_SOURCES = test-search-context.c
 
+UNIT_TEST_PROGS += test-stylescheme
+test_stylescheme_SOURCES = test-stylescheme.c
+
 UNIT_TEST_PROGS += test-styleschememanager
 test_styleschememanager_SOURCES = test-styleschememanager.c
 
@@ -135,6 +138,7 @@ EXTRA_DIST =                                \
        language-specs/test-full.lang   \
        setup-file-saver.sh             \
        styles/classic.xml              \
+       styles/test.xml                 \
        syntax-highlighting             \
        test-completion.gresource.xml   \
        test-completion.ui              \
@@ -155,7 +159,8 @@ instteslang_DATA =                  \
 
 insttesstyledir = ${libexecdir}/installed-tests/$(PACKAGE)/styles
 insttesstyle_DATA =                    \
-       styles/classic.xml
+       styles/classic.xml              \
+       styles/test.xml
 
 %.test: %$(EXEEXT) Makefile
        $(AM_V_GEN) (echo '[Test]' > $  tmp; \
diff --git a/tests/styles/test.xml b/tests/styles/test.xml
new file mode 100644
index 0000000..c615878
--- /dev/null
+++ b/tests/styles/test.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<style-scheme id="test" name="Test" version="1.0">
+  <author>Paolo Borelli</author>
+  <author>John Doe</author>
+  <description>Test color scheme</description>
+
+  <color name="namedrgba" value="#rgba(100, 0, 0, 0.5)"/>
+
+  <style name="current-line" background="namedrgba"/>
+  <style name="background-pattern" background="#rgba(100, 0, 0, 0.5)"/>
+
+  <style name="def:comment" foreground="#FF0000"/>
+</style-scheme>
diff --git a/tests/test-stylescheme.c b/tests/test-stylescheme.c
new file mode 100644
index 0000000..8d4782b
--- /dev/null
+++ b/tests/test-stylescheme.c
@@ -0,0 +1,140 @@
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <gtk/gtk.h>
+#include <gtksourceview/gtksource.h>
+
+typedef struct _TestFixture TestFixture;
+
+struct _TestFixture {
+       GtkSourceStyleSchemeManager *manager;
+};
+
+/* If we are running from the source dir (e.g. during make check)
+ * we override the path to read from the data dir
+ */
+static void
+test_fixture_setup (TestFixture   *fixture,
+                    gconstpointer  data)
+{
+       gchar *dir;
+       gchar **style_dirs;
+
+       dir = g_build_filename (TOP_SRCDIR, "data", "styles", NULL);
+
+       fixture->manager = gtk_source_style_scheme_manager_get_default ();
+
+       if (g_file_test (dir, G_FILE_TEST_IS_DIR))
+       {
+               style_dirs = g_new0 (gchar *, 3);
+               style_dirs[0] = dir;
+               style_dirs[1] = g_test_build_filename (G_TEST_DIST, "styles", NULL);
+       }
+       else
+       {
+               const gchar * const *current;
+               int i;
+
+               g_free (dir);
+
+               current = gtk_source_style_scheme_manager_get_search_path (fixture->manager);
+               style_dirs = g_new0 (gchar *, g_strv_length ((gchar **)current) + 2);
+               for (i = 0; current[i] != NULL; i++)
+               {
+                       style_dirs[i] = g_strdup(current[i]);
+               }
+               style_dirs[i] = g_test_build_filename (G_TEST_DIST, "styles", NULL);
+       }
+
+       gtk_source_style_scheme_manager_set_search_path (fixture->manager, style_dirs);
+       g_strfreev (style_dirs);
+}
+
+static void
+test_fixture_teardown (TestFixture   *fixture,
+                       gconstpointer  data)
+{
+}
+
+static void
+compare_strv (const gchar **strv,
+             const gchar **expected_strv)
+{
+       if (expected_strv != NULL)
+       {
+               guint n, i;
+
+               n = g_strv_length ((gchar **) expected_strv);
+               for (i = 0; i < n; i++)
+               {
+                       g_assert_cmpstr (strv[i], ==, expected_strv[i]);
+               }
+       }
+       else
+       {
+               g_assert (strv == NULL);
+       }
+}
+
+static void
+check_scheme (GtkSourceStyleScheme  *scheme,
+              const gchar           *expected_id,
+              const gchar           *expected_name,
+              const gchar           *expected_description,
+              const gchar          **expected_authors,
+              const gchar           *style_id)
+{
+       GtkSourceStyle *style;
+
+       g_assert_cmpstr (gtk_source_style_scheme_get_id (scheme), ==, expected_id);
+       g_assert_cmpstr (gtk_source_style_scheme_get_name (scheme), ==, expected_name);
+       g_assert_cmpstr (gtk_source_style_scheme_get_description (scheme), ==, expected_description);
+       compare_strv ((const gchar **)gtk_source_style_scheme_get_authors (scheme), expected_authors);
+
+       style = gtk_source_style_scheme_get_style (scheme, style_id);
+       g_assert (GTK_SOURCE_IS_STYLE (style));
+}
+
+static void
+test_scheme_properties (TestFixture   *fixture,
+                        gconstpointer  data)
+{
+       GtkSourceStyleScheme *scheme;
+       const gchar *authors[] = { "Paolo Borelli", "John Doe", NULL};
+
+       scheme = gtk_source_style_scheme_manager_get_scheme (fixture->manager, "test");
+       check_scheme (scheme, "test", "Test", "Test color scheme", authors, "def:comment");
+}
+
+static void
+test_named_color_alpha (TestFixture   *fixture,
+                        gconstpointer  data)
+{
+       GtkSourceStyleScheme *scheme;
+       GdkRGBA color1, color2;
+       gboolean res;
+
+       scheme = gtk_source_style_scheme_manager_get_scheme (fixture->manager, "test");
+
+       /* Use these two semi private methods to compare a named color and a normal one */
+       res = _gtk_source_style_scheme_get_current_line_color (scheme, &color1);
+       g_assert (res);
+
+       res = _gtk_source_style_scheme_get_background_pattern_color (scheme, &color2);
+       g_assert (res);
+
+       g_assert (gdk_rgba_equal (&color1, &color2));
+}
+
+int
+main (int argc, char** argv)
+{
+       gtk_test_init (&argc, &argv);
+
+       g_test_add ("/StyleScheme/scheme-properties", TestFixture, NULL, test_fixture_setup, 
test_scheme_properties, test_fixture_teardown);
+       g_test_add ("/StyleScheme/named-colors-alpha", TestFixture, NULL, test_fixture_setup, 
test_named_color_alpha, test_fixture_teardown);
+
+       return g_test_run();
+}


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