summaryrefslogtreecommitdiff
path: root/pango/pango-language-set-simple.c
blob: 3f1aa326bd8ec7e93e65e556b541b1fdeaa87420 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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);
}