summaryrefslogtreecommitdiff
path: root/pango2/pango-font-family.c
blob: 26940b528e865006f3102bc643c44b66b86a564d (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* Pango2
 *
 * Copyright (C) 1999 Red Hat Software
 *
 * 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-font-family-private.h"
#include "pango-font-face.h"
#include "pango-font.h"

/**
 * Pango2FontFamily:
 *
 * A `Pango2FontFamily` is used to represent a family of related
 * font faces.
 *
 * The font faces in a family share a common design, but differ in
 * slant, weight, width or other aspects.
 *
 * `Pango2FontFamily` implements the [iface@Gio.ListModel] interface,
 * to provide a list of font faces.
 */

/* {{{ GListModel implementation */

static GType
pango2_font_family_get_item_type (GListModel *list)
{
  return PANGO2_TYPE_FONT_FACE;
}

static guint
pango2_font_family_get_n_items (GListModel *list)
{
  g_assert_not_reached ();
  return 0;
}

static gpointer
pango2_font_family_get_item (GListModel *list,
                             guint       position)
{
  g_assert_not_reached ();
  return NULL;
}

static void
pango2_font_family_list_model_init (GListModelInterface *iface)
{
  iface->get_item_type = pango2_font_family_get_item_type;
  iface->get_n_items = pango2_font_family_get_n_items;
  iface->get_item = pango2_font_family_get_item;
}

/* }}} */
/* {{{ Pango2FontFamily implementation */

enum {
  PROP_NAME = 1,
  PROP_ITEM_TYPE,
  PROP_N_ITEMS,
  N_PROPERTIES
};

static GParamSpec *properties[N_PROPERTIES] = { NULL, };

G_DEFINE_ABSTRACT_TYPE_WITH_CODE (Pango2FontFamily, pango2_font_family, G_TYPE_OBJECT,
                                  G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, pango2_font_family_list_model_init))

static Pango2FontFace *
pango2_font_family_real_get_face (Pango2FontFamily *family,
                                  const char       *name)
{
  Pango2FontFace *face;

  face = NULL;

  for (int i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (family)); i++)
    {
      Pango2FontFace *f = g_list_model_get_item (G_LIST_MODEL (family), i);
      g_object_unref (f);
      if (name == NULL ||
          strcmp (name, pango2_font_face_get_name (f)) == 0)
        {
          face = f;
          break;
        }
    }

  return face;
}

static void
pango2_font_family_finalize (GObject *object)
{
  Pango2FontFamily *family = PANGO2_FONT_FAMILY (object);

  g_free (family->name);
  if (family->map)
    g_object_remove_weak_pointer (G_OBJECT (family->map), (gpointer *)&family->map);

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

static void
pango2_font_family_get_property (GObject    *object,
                                 guint       property_id,
                                 GValue     *value,
                                 GParamSpec *pspec)
{
  Pango2FontFamily *family = PANGO2_FONT_FAMILY (object);

  switch (property_id)
    {
    case PROP_NAME:
      g_value_set_string (value, family->name);
      break;

    case PROP_ITEM_TYPE:
      g_value_set_gtype (value, PANGO2_TYPE_FONT);
      break;

    case PROP_N_ITEMS:
      g_value_set_uint (value, pango2_font_family_get_n_items (G_LIST_MODEL (object)));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
pango2_font_family_class_init (Pango2FontFamilyClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);

  object_class->finalize = pango2_font_family_finalize;
  object_class->get_property = pango2_font_family_get_property;

  class->get_face = pango2_font_family_real_get_face;

  /**
   * Pango2FontFamily:name: (attributes org.gtk.Property.get=pango2_font_family_get_name)
   *
   * The name of the family.
   */
  properties[PROP_NAME] =
      g_param_spec_string ("name", NULL, NULL,
                           NULL,
                           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  /**
   * Pango2FontFamily:item-type:
   *
   * The type of objects that the family contains.
   */
  properties[PROP_ITEM_TYPE] =
      g_param_spec_gtype ("item-type", NULL, NULL,
                          PANGO2_TYPE_FONT_FACE,
                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  /**
   * Pango2FontFamily:n-items:
   *
   * The number of faces contained in the family.
   */
  properties[PROP_N_ITEMS] =
      g_param_spec_uint ("n-items", NULL, NULL,
                         0, G_MAXUINT, 0,
                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (object_class, N_PROPERTIES, properties);
}

static void
pango2_font_family_init (Pango2FontFamily *family G_GNUC_UNUSED)
{
}

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

/**
 * pango2_font_family_get_font_map:
 * @family: a `Pango2FontFamily`
 *
 * Returns the `Pango2FontMap that @family belongs to.
 *
 * Note that the family maintains a *weak* reference to
 * the font map, so if all references to font map are
 * dropped, the font map will be finalized even if there
 * are fonts created with the font map that are still alive.
 * In that case this function will return %NULL.
 *
 * It is the responsibility of the user to ensure that the
 * font map is kept alive. In most uses this is not an issue
 * as a `Pango2Context` holds a reference to the font map.

 *
 * Return value: (transfer none) (nullable): the `Pango2FontMap
 */
Pango2FontMap *
pango2_font_family_get_font_map (Pango2FontFamily *family)
{
  return family->map;
}

/**
 * pango2_font_family_get_name:
 * @family: a `Pango2FontFamily`
 *
 * Gets the name of the family.
 *
 * The name is unique among all fonts for the font backend and can
 * be used in a `Pango2FontDescription` to specify that a face from
 * this family is desired.
 *
 * Return value: the name of the family. This string is owned
 *   by the family object and must not be modified or freed.
 */
const char *
pango2_font_family_get_name (Pango2FontFamily *family)
{
  g_return_val_if_fail (PANGO2_IS_FONT_FAMILY (family), NULL);

  return family->name;
}

/**
 * pango2_font_family_get_face:
 * @family: a `Pango2FontFamily`
 * @name: (nullable): the name of a face. If the name is `NULL`,
 *   the family's default face (fontconfig calls it "Regular")
 *   will be returned.
 *
 * Gets the `Pango2FontFace` of the family with the given name.
 *
 * Returns: (transfer none) (nullable): the `Pango2FontFace`,
 *   or `NULL` if no face with the given name exists.
 */
Pango2FontFace *
pango2_font_family_get_face (Pango2FontFamily *family,
                             const char       *name)
{
  g_return_val_if_fail (PANGO2_IS_FONT_FAMILY (family), NULL);

  return PANGO2_FONT_FAMILY_GET_CLASS (family)->get_face (family, name);
}

/* }}} */

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