summaryrefslogtreecommitdiff
path: root/cogl-pango/cogl-pango-fontmap.c
blob: 0f475f95f870165cd90179721ffc2a0e4db72a77 (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
/*
 * Clutter.
 *
 * An OpenGL based 'interactive canvas' library.
 *
 * Authored By Matthew Allum  <mallum@openedhand.com>
 *
 * Copyright (C) 2008 OpenedHand
 *
 * 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 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/>.
 */

/**
 * SECTION:cogl-pango
 * @short_description: COGL-based text rendering using Pango
 *
 * FIXME
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

/* This is needed to get the Pango headers to export stuff needed to
   subclass */
#ifndef PANGO_ENABLE_BACKEND
#define PANGO_ENABLE_BACKEND 1
#endif

#include <pango/pango-fontmap.h>
#include <pango/pangocairo.h>
#include <pango/pango-renderer.h>

#include "cogl-pango.h"
#include "cogl-pango-private.h"
#include "cogl-util.h"
#include "cogl/cogl-context-private.h"

static GQuark cogl_pango_font_map_get_priv_key (void) G_GNUC_CONST;

typedef struct _CoglPangoFontMapPriv
{
  CoglContext *ctx;
  PangoRenderer *renderer;
} CoglPangoFontMapPriv;

static void
free_priv (gpointer data)
{
  CoglPangoFontMapPriv *priv = data;

  cogl_object_unref (priv->ctx);
  cogl_object_unref (priv->renderer);

  g_free (priv);
}

PangoFontMap *
cogl_pango_font_map_new (void)
{
  PangoFontMap *fm = pango_cairo_font_map_new ();
  CoglPangoFontMapPriv *priv = g_new0 (CoglPangoFontMapPriv, 1);

  _COGL_GET_CONTEXT (context, NULL);

  priv->ctx = cogl_object_ref (context);

  /* XXX: The public pango api doesn't let us sub-class
   * PangoCairoFontMap so we attach our own private data using qdata
   * for now. */
  g_object_set_qdata_full (G_OBJECT (fm),
                           cogl_pango_font_map_get_priv_key (),
                           priv,
                           free_priv);

  return fm;
}

PangoContext *
cogl_pango_font_map_create_context (CoglPangoFontMap *fm)
{
  _COGL_RETURN_VAL_IF_FAIL (COGL_PANGO_IS_FONT_MAP (fm), NULL);

  /* We can just directly use the pango context from the Cairo font
     map */
  return pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fm));
}

static CoglPangoFontMapPriv *
_cogl_pango_font_map_get_priv (CoglPangoFontMap *fm)
{
  return g_object_get_qdata (G_OBJECT (fm),
			     cogl_pango_font_map_get_priv_key ());
}

PangoRenderer *
_cogl_pango_font_map_get_renderer (CoglPangoFontMap *fm)
{
  CoglPangoFontMapPriv *priv = _cogl_pango_font_map_get_priv (fm);
  if (G_UNLIKELY (!priv->renderer))
    priv->renderer = _cogl_pango_renderer_new (priv->ctx);
  return priv->renderer;
}

PangoRenderer *
cogl_pango_font_map_get_renderer (CoglPangoFontMap *fm)
{
  return _cogl_pango_font_map_get_renderer (fm);
}

CoglContext *
_cogl_pango_font_map_get_cogl_context (CoglPangoFontMap *fm)
{
  CoglPangoFontMapPriv *priv = _cogl_pango_font_map_get_priv (fm);
  return priv->ctx;
}

void
cogl_pango_font_map_set_resolution (CoglPangoFontMap *font_map,
				    double            dpi)
{
  _COGL_RETURN_IF_FAIL (COGL_PANGO_IS_FONT_MAP (font_map));

  pango_cairo_font_map_set_resolution (PANGO_CAIRO_FONT_MAP (font_map), dpi);
}

void
cogl_pango_font_map_clear_glyph_cache (CoglPangoFontMap *fm)
{
  PangoRenderer *renderer = _cogl_pango_font_map_get_renderer (fm);

  _cogl_pango_renderer_clear_glyph_cache (COGL_PANGO_RENDERER (renderer));
}

void
cogl_pango_font_map_set_use_mipmapping (CoglPangoFontMap *fm,
                                        CoglBool          value)
{
  PangoRenderer *renderer = _cogl_pango_font_map_get_renderer (fm);

  _cogl_pango_renderer_set_use_mipmapping (COGL_PANGO_RENDERER (renderer),
                                           value);
}

CoglBool
cogl_pango_font_map_get_use_mipmapping (CoglPangoFontMap *fm)
{
  PangoRenderer *renderer = _cogl_pango_font_map_get_renderer (fm);

  return
    _cogl_pango_renderer_get_use_mipmapping (COGL_PANGO_RENDERER (renderer));
}

static GQuark
cogl_pango_font_map_get_priv_key (void)
{
  static GQuark priv_key = 0;

  if (G_UNLIKELY (priv_key == 0))
      priv_key = g_quark_from_static_string ("CoglPangoFontMap");

  return priv_key;
}