[pango/pango2: 102/168] Add PangoLanguageSet




commit 615645009f6f30fd766aef05768c90d30f2a14da
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Dec 25 17:43:31 2021 -0500

    Add PangoLanguageSet
    
    PangoLanguageSet is an internal type used to
    represent languages that are supported by a
    font, similar to FcLangSet.

 pango/meson.build                         |   2 +
 pango/pango-language-set-private.h        |  44 +++++++++++
 pango/pango-language-set-simple-private.h |  32 ++++++++
 pango/pango-language-set-simple.c         | 125 ++++++++++++++++++++++++++++++
 pango/pango-language-set.c                |  87 +++++++++++++++++++++
 5 files changed, 290 insertions(+)
---
diff --git a/pango/meson.build b/pango/meson.build
index b81b71f1..4e7a9dae 100644
--- a/pango/meson.build
+++ b/pango/meson.build
@@ -21,6 +21,8 @@ pango_sources = [
   'pango-gravity.c',
   'pango-item.c',
   'pango-language.c',
+  'pango-language-set.c',
+  'pango-language-set-simple.c',
   'pango-layout.c',
   'pango-markup.c',
   'pango-matrix.c',
diff --git a/pango/pango-language-set-private.h b/pango/pango-language-set-private.h
new file mode 100644
index 00000000..7eb32b5c
--- /dev/null
+++ b/pango/pango-language-set-private.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2022 Red Hat, Inc.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include "pango-language.h"
+
+
+#define PANGO_TYPE_LANGUAGE_SET (pango_language_set_get_type ())
+
+G_DECLARE_DERIVABLE_TYPE (PangoLanguageSet, pango_language_set, PANGO, LANGUAGE_SET, GObject)
+
+struct _PangoLanguageSetClass
+{
+  GObjectClass parent_class;
+
+  gboolean               (* matches_language)   (PangoLanguageSet       *set,
+                                                 PangoLanguage          *language);
+
+  PangoLanguage **      (* get_languages)       (PangoLanguageSet       *set);
+};
+
+gboolean                pango_language_set_matches_language     (PangoLanguageSet *set,
+                                                                 PangoLanguage    *language);
+
+PangoLanguage **        pango_language_set_get_languages        (PangoLanguageSet *set);
+
+char *                  pango_language_set_to_string            (PangoLanguageSet *set);
diff --git a/pango/pango-language-set-simple-private.h b/pango/pango-language-set-simple-private.h
new file mode 100644
index 00000000..25d719da
--- /dev/null
+++ b/pango/pango-language-set-simple-private.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2022 Red Hat, Inc.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include "pango-language-set-private.h"
+
+
+#define PANGO_TYPE_LANGUAGE_SET_SIMPLE (pango_language_set_simple_get_type ())
+
+G_DECLARE_FINAL_TYPE (PangoLanguageSetSimple, pango_language_set_simple, PANGO, LANGUAGE_SET_SIMPLE, 
PangoLanguageSet)
+
+PangoLanguageSetSimple *pango_language_set_simple_new           (void);
+
+void                    pango_language_set_simple_add_language  (PangoLanguageSetSimple *self,
+                                                                 PangoLanguage          *language);
diff --git a/pango/pango-language-set-simple.c b/pango/pango-language-set-simple.c
new file mode 100644
index 00000000..3f1aa326
--- /dev/null
+++ b/pango/pango-language-set-simple.c
@@ -0,0 +1,125 @@
+/* Pango
+ *
+ * Copyright (C) 2021 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#include <gio/gio.h>
+
+#include "pango-language-set-simple-private.h"
+
+struct _PangoLanguageSetSimple
+{
+  PangoLanguageSet parent_instance;
+
+  GHashTable *languages;
+  PangoLanguage **list;
+};
+
+G_DEFINE_TYPE (PangoLanguageSetSimple, pango_language_set_simple, PANGO_TYPE_LANGUAGE_SET)
+
+static void
+pango_language_set_simple_init (PangoLanguageSetSimple *self)
+{
+  self->languages = g_hash_table_new (g_direct_hash, g_direct_equal);
+}
+
+static void
+pango_language_set_simple_finalize (GObject *object)
+{
+  PangoLanguageSetSimple *self = PANGO_LANGUAGE_SET_SIMPLE (object);
+
+  g_hash_table_unref (self->languages);
+  g_free (self->list);
+
+  G_OBJECT_CLASS (pango_language_set_simple_parent_class)->finalize (object);
+}
+
+static gboolean
+pango_language_set_simple_matches_language (PangoLanguageSet *set,
+                                            PangoLanguage    *language)
+{
+  PangoLanguageSetSimple *self = PANGO_LANGUAGE_SET_SIMPLE (set);
+  const char *s;
+
+  if (g_hash_table_contains (self->languages, language))
+    return TRUE;
+
+  if (language == pango_language_from_string ("c"))
+    return TRUE;
+
+  s = pango_language_to_string (language);
+  if (strchr (s, '-'))
+    {
+      char buf[10];
+
+      for (int i = 0; i < 10; i++)
+        {
+          buf[i] = s[i];
+          if (buf[i] == '-')
+            {
+              buf[i] = '\0';
+              break;
+            }
+        }
+      buf[9] = '\0';
+
+      if (g_hash_table_contains (self->languages, pango_language_from_string (buf)))
+        return TRUE;
+    }
+
+  return FALSE;
+}
+
+static PangoLanguage **
+pango_language_set_simple_get_languages (PangoLanguageSet *set)
+{
+  PangoLanguageSetSimple *self = PANGO_LANGUAGE_SET_SIMPLE (set);
+
+  if (!self->list)
+    self->list = (PangoLanguage **) g_hash_table_get_keys_as_array (self->languages, NULL);
+
+  return self->list;
+}
+
+static void
+pango_language_set_simple_class_init (PangoLanguageSetSimpleClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+  PangoLanguageSetClass *language_set_class = PANGO_LANGUAGE_SET_CLASS (class);
+
+  object_class->finalize = pango_language_set_simple_finalize;
+  language_set_class->matches_language = pango_language_set_simple_matches_language;
+  language_set_class->get_languages = pango_language_set_simple_get_languages;
+}
+
+PangoLanguageSetSimple *
+pango_language_set_simple_new (void)
+{
+  return g_object_new (PANGO_TYPE_LANGUAGE_SET_SIMPLE, NULL);
+}
+
+void
+pango_language_set_simple_add_language (PangoLanguageSetSimple *self,
+                                        PangoLanguage          *language)
+{
+  g_return_if_fail (self->list == NULL);
+
+  g_hash_table_add (self->languages, language);
+}
diff --git a/pango/pango-language-set.c b/pango/pango-language-set.c
new file mode 100644
index 00000000..b0dc87a8
--- /dev/null
+++ b/pango/pango-language-set.c
@@ -0,0 +1,87 @@
+/* Pango
+ *
+ * Copyright (C) 2021 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#include <gio/gio.h>
+
+#include "pango-language-set-private.h"
+
+
+G_DEFINE_ABSTRACT_TYPE (PangoLanguageSet, pango_language_set, G_TYPE_OBJECT)
+
+static void
+pango_language_set_init (PangoLanguageSet *set)
+{
+}
+
+static gboolean
+pango_language_set_default_matches_language (PangoLanguageSet *set,
+                                             PangoLanguage    *language)
+{
+  return FALSE;
+}
+
+static PangoLanguage **
+pango_language_set_default_get_languages (PangoLanguageSet *set)
+{
+  return NULL;
+}
+
+static void
+pango_language_set_class_init (PangoLanguageSetClass *class)
+{
+  PangoLanguageSetClass *language_set_class = PANGO_LANGUAGE_SET_CLASS (class);
+
+  language_set_class->matches_language = pango_language_set_default_matches_language;
+  language_set_class->get_languages = pango_language_set_default_get_languages;
+}
+
+gboolean
+pango_language_set_matches_language (PangoLanguageSet *set,
+                                     PangoLanguage    *language)
+{
+  return PANGO_LANGUAGE_SET_GET_CLASS (set)->matches_language (set, language);
+}
+
+PangoLanguage **
+pango_language_set_get_languages (PangoLanguageSet *set)
+{
+  return PANGO_LANGUAGE_SET_GET_CLASS (set)->get_languages (set);
+}
+
+char *
+pango_language_set_to_string (PangoLanguageSet *set)
+{
+  PangoLanguage **languages;
+  GString *s;
+
+  s = g_string_new ("");
+
+  languages = pango_language_set_get_languages (set);
+  for (int i = 0; languages[i]; i++)
+    {
+      if (s->len > 0)
+        g_string_append (s, "|");
+      g_string_append (s, pango_language_to_string (languages[i]));
+    }
+
+  return g_string_free (s, FALSE);
+}


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