summaryrefslogtreecommitdiff
path: root/pango/pango-generic-family.c
blob: fd555eabff3e9047e164ebe306ee6914feb3d33a (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* Pango
 *
 * Copyright (C) 2022 Matthias Clasen
 *
 * 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-generic-family-private.h"
#include "pango-impl-utils.h"
#include "pango-hbface-private.h"
#include "pango-font-private.h"

/**
 * PangoGenericFamily:
 *
 * An implementation of `PangoFontFamily`
 * that provides faces from other families.
 *
 * `PangoGenericFamily can be used to e.g. assemble a
 * generic 'sans-serif' family from a number of other
 * font families.
 */

/* {{{ PangoFontFamily implementation */

struct _PangoGenericFamilyClass
{
  PangoFontFamilyClass parent_class;
};

G_DEFINE_TYPE (PangoGenericFamily, pango_generic_family, PANGO_TYPE_FONT_FAMILY)

static void
pango_generic_family_init (PangoGenericFamily *self)
{
  self->families = g_ptr_array_new_with_free_func (g_object_unref);
}

static void
pango_generic_family_finalize (GObject *object)
{
  PangoGenericFamily *self = PANGO_GENERIC_FAMILY (object);

  g_free (self->name);
  g_ptr_array_unref (self->families);

  G_OBJECT_CLASS (pango_generic_family_parent_class)->finalize (object);
}

static const char *
pango_generic_family_get_name (PangoFontFamily *family)
{
  PangoGenericFamily *self = PANGO_GENERIC_FAMILY (family);

  return self->name;
}

static gboolean
pango_generic_family_is_monospace (PangoFontFamily *family)
{
  return FALSE;
}

static gboolean
pango_generic_family_is_variable (PangoFontFamily *family)
{
  return FALSE;
}

static void
pango_generic_family_class_init (PangoGenericFamilyClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  PangoFontFamilyClass *family_class = PANGO_FONT_FAMILY_CLASS (class);

  object_class->finalize = pango_generic_family_finalize;

  family_class->get_name = pango_generic_family_get_name;
  family_class->is_monospace = pango_generic_family_is_monospace;
  family_class->is_variable = pango_generic_family_is_variable;
}

/* }}} */
/* {{{ Private API */

/*< private >
 * pango_generic_family_set_font_map:
 * @self: a `PangoGenericFamily`
 * @map: (nullable): a `PangoFontMap`
 *
 * Sets the map of @self.
 */
void
pango_generic_family_set_font_map (PangoGenericFamily *self,
                                   PangoFontMap       *map)
{
  if (self->map)
    g_object_remove_weak_pointer (G_OBJECT (self->map), (gpointer *)&self->map);

  self->map = map;

  if (self->map)
    g_object_add_weak_pointer (G_OBJECT (self->map), (gpointer *)&self->map);
}

/*< private >
 * pango_generic_family_find_face:
 * @family: a `PangoGenericFamily`
 * @description: `PangoFontDescription` to match
 * @language: (nullable): `PangoLanguage` to support
 * @wc: a Unicode character, or 0 to ignore
 *
 * Finds the face that best matches the font description while
 * also supporting the given language and character, from the first
 * family in @family that has any matching faces.
 *
 * Returns: (transfer none) (nullable): the face
 */
PangoFontFace *
pango_generic_family_find_face (PangoGenericFamily   *self,
                                PangoFontDescription *description,
                                PangoLanguage        *language,
                                gunichar              wc)
{
  PangoFontFace *face = NULL;

  for (int i = 0; i < self->families->len; i++)
    {
      PangoHbFamily *family = g_ptr_array_index (self->families, i);

      face = pango_hb_family_find_face (family, description, language, wc);
      if (face)
        break;
    }

  return face;
}

/* }}} */
/* {{{ Public API */

/**
 * pango_generic_family_new:
 * @name: the family name
 *
 * Creates a new `PangoGenericFamily`.
 *
 * A generic family does not contain faces, but will return
 * faces from other families that match a given query.
 *
 * Returns: a newly created `PangoGenericFamily`
 *
 * Since: 1.52
 */
PangoGenericFamily *
pango_generic_family_new (const char *name)
{
  PangoGenericFamily *self;

  g_return_val_if_fail (name != NULL, NULL);

  self = g_object_new (PANGO_TYPE_GENERIC_FAMILY, NULL);

  self->name = g_strdup (name);

  return self;
}

/**
 * pango_generic_family_add_family:
 * @self: a `PangoGenericFamily`
 * @family: (transfer none): a `PangoFontFamily` to add
 *
 * Adds a `PangoFontFamily` to a `PangoGenericFamily`.
 *
 * It is an error to call this function more than
 * once for the same family.
 *
 * Since: 1.52
 */
void
pango_generic_family_add_family (PangoGenericFamily *self,
                                 PangoFontFamily    *family)
{
  g_return_if_fail (PANGO_IS_GENERIC_FAMILY (self));
  g_return_if_fail (PANGO_IS_FONT_FAMILY (family));

  g_ptr_array_add (self->families, g_object_ref (family));
}

/* }}} */

/* vim:set foldmethod=marker expandtab: */