summaryrefslogtreecommitdiff
path: root/pango/pangoxft-fontmap.c
blob: 829551655368018b120588f2947a73fc0168cef0 (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* Pango
 * pangoxft-fontmap.c: Xft font handling
 *
 * Copyright (C) 2000-2003 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 <stdlib.h>
#include <string.h>

#include "pangofc-fontmap-private.h"
#include "pangoxft.h"
#include "pangoxft-private.h"

/* For XExtSetCloseDisplay */
#include <X11/Xlibint.h>

typedef struct _PangoXftFamily       PangoXftFamily;
typedef struct _PangoXftFontMapClass PangoXftFontMapClass;

#define PANGO_TYPE_XFT_FONT_MAP              (pango_xft_font_map_get_type ())
#define PANGO_XFT_FONT_MAP(object)           (G_TYPE_CHECK_INSTANCE_CAST ((object), PANGO_TYPE_XFT_FONT_MAP, PangoXftFontMap))
#define PANGO_XFT_IS_FONT_MAP(object)        (G_TYPE_CHECK_INSTANCE_TYPE ((object), PANGO_TYPE_XFT_FONT_MAP))

struct _PangoXftFontMap
{
  PangoFcFontMap parent_instance;

  guint serial;

  Display *display;
  int screen;

  PangoRenderer *renderer;
};

struct _PangoXftFontMapClass
{
  PangoFcFontMapClass parent_class;
};

static guint         pango_xft_font_map_get_serial         (PangoFontMap         *fontmap);
static void          pango_xft_font_map_changed            (PangoFontMap         *fontmap);
static void          pango_xft_font_map_default_substitute (PangoFcFontMap       *fcfontmap,
							    FcPattern            *pattern);
static PangoFcFont * pango_xft_font_map_new_font           (PangoFcFontMap       *fcfontmap,
							    FcPattern            *pattern);
static void          pango_xft_font_map_finalize           (GObject              *object);

G_LOCK_DEFINE_STATIC (fontmaps);
static GSList *fontmaps = NULL; /* MT-safe */

G_DEFINE_TYPE (PangoXftFontMap, pango_xft_font_map, PANGO_TYPE_FC_FONT_MAP)

static void
pango_xft_font_map_class_init (PangoXftFontMapClass *class)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
  PangoFontMapClass *fontmap_class = PANGO_FONT_MAP_CLASS (class);
  PangoFcFontMapClass *fcfontmap_class = PANGO_FC_FONT_MAP_CLASS (class);

  gobject_class->finalize  = pango_xft_font_map_finalize;

  fontmap_class->get_serial = pango_xft_font_map_get_serial;
  fontmap_class->changed = pango_xft_font_map_changed;

  fcfontmap_class->default_substitute = pango_xft_font_map_default_substitute;
  fcfontmap_class->new_font = pango_xft_font_map_new_font;
}

static void
pango_xft_font_map_init (PangoXftFontMap *xftfontmap)
{
  xftfontmap->serial = 1;
}

static void
pango_xft_font_map_finalize (GObject *object)
{
  PangoXftFontMap *xftfontmap = PANGO_XFT_FONT_MAP (object);

  if (xftfontmap->renderer)
    g_object_unref (xftfontmap->renderer);

  G_LOCK (fontmaps);
  fontmaps = g_slist_remove (fontmaps, object);
  G_UNLOCK (fontmaps);

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


static guint
pango_xft_font_map_get_serial (PangoFontMap *fontmap)
{
  PangoXftFontMap *xftfontmap = PANGO_XFT_FONT_MAP (fontmap);

  return xftfontmap->serial;
}

static void
pango_xft_font_map_changed (PangoFontMap *fontmap)
{
  PangoXftFontMap *xftfontmap = PANGO_XFT_FONT_MAP (fontmap);

  xftfontmap->serial++;
  if (xftfontmap->serial == 0)
    xftfontmap->serial++;
}

static PangoFontMap *
pango_xft_find_font_map (Display *display,
			 int      screen)
{
  GSList *tmp_list;

  G_LOCK (fontmaps);
  tmp_list = fontmaps;
  while (tmp_list)
    {
      PangoXftFontMap *xftfontmap = tmp_list->data;

      if (xftfontmap->display == display &&
	  xftfontmap->screen == screen)
        {
          G_UNLOCK (fontmaps);
	  return PANGO_FONT_MAP (xftfontmap);
        }

      tmp_list = tmp_list->next;
    }
  G_UNLOCK (fontmaps);

  return NULL;
}

/*
 * Hackery to set up notification when a Display is closed
 */
static GSList *registered_displays; /* MT-safe, protected by fontmaps lock */

static int
close_display_cb (Display   *display,
		  XExtCodes *extcodes G_GNUC_UNUSED)
{
  GSList *tmp_list;

  G_LOCK (fontmaps);
  tmp_list = g_slist_copy (fontmaps);
  G_UNLOCK (fontmaps);

  while (tmp_list)
    {
      PangoXftFontMap *xftfontmap = tmp_list->data;
      tmp_list = tmp_list->next;

      if (xftfontmap->display == display)
	pango_xft_shutdown_display (display, xftfontmap->screen);
    }

  g_slist_free (tmp_list);

  registered_displays = g_slist_remove (registered_displays, display);

  return 0;
}

static void
register_display (Display *display)
{
  XExtCodes *extcodes;
  GSList *tmp_list;

  for (tmp_list = registered_displays; tmp_list; tmp_list = tmp_list->next)
    {
      if (tmp_list->data == display)
	return;
    }

  registered_displays = g_slist_prepend (registered_displays, display);

  extcodes = XAddExtension (display);
  XESetCloseDisplay (display, extcodes->extension, close_display_cb);
}

/**
 * pango_xft_get_font_map:
 * @display: an X display
 * @screen: the screen number of a screen within @display
 *
 * Returns the #PangoXftFontMap for the given display and screen.
 * The fontmap is owned by Pango and will be valid until
 * the display is closed.
 *
 * Return value: (transfer none): a #PangoFontMap object, owned by Pango.
 *
 * Since: 1.2
 **/
PangoFontMap *
pango_xft_get_font_map (Display *display,
			int      screen)
{
  PangoFontMap *fontmap;
  PangoXftFontMap *xftfontmap;

  g_return_val_if_fail (display != NULL, NULL);

  fontmap = pango_xft_find_font_map (display, screen);
  if (fontmap)
    return fontmap;

  xftfontmap = (PangoXftFontMap *)g_object_new (PANGO_TYPE_XFT_FONT_MAP, NULL);

  xftfontmap->display = display;
  xftfontmap->screen = screen;

  G_LOCK (fontmaps);

  register_display (display);

  fontmaps = g_slist_prepend (fontmaps, xftfontmap);

  G_UNLOCK (fontmaps);

  return PANGO_FONT_MAP (xftfontmap);
}

/**
 * pango_xft_shutdown_display:
 * @display: an X display
 * @screen: the screen number of a screen within @display
 *
 * Release any resources that have been cached for the
 * combination of @display and @screen. Note that when the
 * X display is closed, resources are released automatically,
 * without needing to call this function.
 *
 * Since: 1.2
 **/
void
pango_xft_shutdown_display (Display *display,
			    int      screen)
{
  PangoFontMap *fontmap;

  fontmap = pango_xft_find_font_map (display, screen);
  if (fontmap)
    {
      PangoXftFontMap *xftfontmap = PANGO_XFT_FONT_MAP (fontmap);

      G_LOCK (fontmaps);
      fontmaps = g_slist_remove (fontmaps, fontmap);
      G_UNLOCK (fontmaps);
      pango_fc_font_map_shutdown (PANGO_FC_FONT_MAP (fontmap));

      xftfontmap->display = NULL;
      g_object_unref (fontmap);
    }
}

/**
 * pango_xft_set_default_substitute:
 * @display: an X Display
 * @screen: the screen number of a screen within @display
 * @func: function to call to to do final config tweaking
 *        on #FcPattern objects.
 * @data: data to pass to @func
 * @notify: function to call when @data is no longer used.
 *
 * Sets a function that will be called to do final configuration
 * substitution on a #FcPattern before it is used to load
 * the font. This function can be used to do things like set
 * hinting and antialiasing options.
 *
 * Deprecated: 1.46: Use pango_fc_font_map_set_default_substitute()
 * instead.
 *
 * Since: 1.2
 **/
void
pango_xft_set_default_substitute (Display                *display,
				  int                     screen,
				  PangoXftSubstituteFunc  func,
				  gpointer                data,
				  GDestroyNotify          notify)
{
  PangoXftFontMap *xftfontmap = (PangoXftFontMap *)pango_xft_get_font_map (display, screen);

  PangoFcFontMap *fcfontmap = PANGO_FC_FONT_MAP (xftfontmap);
  pango_fc_font_map_set_default_substitute(fcfontmap, func, data, notify);
}

/**
 * pango_xft_substitute_changed:
 * @display: an X Display
 * @screen: the screen number of a screen within @display
 *
 * Call this function any time the results of the
 * default substitution function set with
 * pango_xft_set_default_substitute() change.
 * That is, if your substitution function will return different
 * results for the same input pattern, you must call this function.
 *
 * Deprecated: 1.46: Use pango_fc_font_map_substitute_changed()
 * instead.
 *
 * Since: 1.2
 **/
void
pango_xft_substitute_changed (Display *display,
			      int      screen)
{
  PangoXftFontMap *xftfontmap = (PangoXftFontMap *)pango_xft_get_font_map (display, screen);

  pango_fc_font_map_substitute_changed(PANGO_FC_FONT_MAP (xftfontmap));
}

void
_pango_xft_font_map_get_info (PangoFontMap *fontmap,
			      Display     **display,
			      int          *screen)
{
  PangoXftFontMap *xftfontmap = (PangoXftFontMap *)fontmap;

  if (display)
    *display = xftfontmap->display;
  if (screen)
    *screen = xftfontmap->screen;
}

/**
 * pango_xft_get_context: (skip)
 * @display: an X display.
 * @screen: an X screen.
 *
 * Retrieves a #PangoContext appropriate for rendering with
 * Xft fonts on the given screen of the given display.
 *
 * Return value: the new #PangoContext.
 *
 * Deprecated: 1.22: Use pango_xft_get_font_map() followed by
 * pango_font_map_create_context() instead.
 **/
PangoContext *
pango_xft_get_context (Display *display,
		       int      screen)
{
  g_return_val_if_fail (display != NULL, NULL);

  return pango_font_map_create_context (pango_xft_get_font_map (display, screen));
}

/**
 * _pango_xft_font_map_get_renderer:
 * @fontmap: a #PangoXftFontMap
 *
 * Gets the singleton #PangoXFTRenderer for this fontmap.
 *
 * Return value: the renderer.
 **/
PangoRenderer *
_pango_xft_font_map_get_renderer (PangoXftFontMap *xftfontmap)
{
  if (!xftfontmap->renderer)
    xftfontmap->renderer = pango_xft_renderer_new (xftfontmap->display,
						   xftfontmap->screen);

  return xftfontmap->renderer;
}

static void
pango_xft_font_map_default_substitute (PangoFcFontMap *fcfontmap,
				       FcPattern      *pattern)
{
  PangoXftFontMap *xftfontmap = PANGO_XFT_FONT_MAP (fcfontmap);
  double d;

  FcConfigSubstitute (NULL, pattern, FcMatchPattern);
  if (fcfontmap->substitute_func)
    fcfontmap->substitute_func (pattern, fcfontmap->substitute_data);
  XftDefaultSubstitute (xftfontmap->display, xftfontmap->screen, pattern);
  if (FcPatternGetDouble (pattern, FC_PIXEL_SIZE, 0, &d) == FcResultMatch && d == 0.0)
    {
      FcValue v;
      v.type = FcTypeDouble;
      v.u.d = 1.0;
      FcPatternAdd (pattern, FC_PIXEL_SIZE, v, FcFalse);
    }
}

static PangoFcFont *
pango_xft_font_map_new_font (PangoFcFontMap  *fcfontmap,
			     FcPattern       *pattern)
{
  return (PangoFcFont *)_pango_xft_font_new (PANGO_XFT_FONT_MAP (fcfontmap), pattern);
}