summaryrefslogtreecommitdiff
path: root/cairomm/fontface.cc
blob: 2d305bfd0624c3a8cb7b11a5e44b6671e6f84e3b (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
/* Copyright (C) 2005 The cairomm Development Team
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include <iostream>
#include <cairomm/context.h>
#include <cairomm/fontface.h>
#include <cairomm/scaledfont.h>
#include <cairomm/private.h>

namespace Cairo
{

const cairo_user_data_key_t USER_DATA_KEY_DEFAULT_TEXT_TO_GLYPHS =  {0};

FontFace::FontFace(cairo_font_face_t* cobject, bool has_reference)
: m_cobject(0)
{
  if(has_reference)
    m_cobject = cobject;
  else
    m_cobject = cairo_font_face_reference(cobject);
}

FontFace::~FontFace()
{
  if(m_cobject)
    cairo_font_face_destroy(m_cobject);
}

void FontFace::reference() const
{
 cairo_font_face_reference(m_cobject);
}

void FontFace::unreference() const
{
  cairo_font_face_destroy(m_cobject);
}

/*
void* FontFace::get_user_data(const cairo_user_data_key_t *key)
{
  void* result = cairo_font_face_get_user_data(m_cobject, key);
  check_object_status_and_throw_exception(*this);
  return result;
}

void FontFace::set_user_data(const cairo_user_data_key_t* key, void *user_data, cairo_destroy_func_t destroy)
{
  const ErrorStatus status = (ErrorStatus)cairo_font_face_set_user_data(m_cobject, key, user_data, destroy);
  check_status_and_throw_exception(status);
}
*/

FontType FontFace::get_type() const
{
  cairo_font_type_t font_type = cairo_font_face_get_type(m_cobject);
  check_object_status_and_throw_exception(*this);
  return static_cast<FontType>(font_type);
}

// 'Toy' fonts
RefPtr<ToyFontFace>
ToyFontFace::create(const std::string& family, FontSlant slant, FontWeight weight)
{
  return RefPtr<ToyFontFace>(new ToyFontFace(family, slant, weight));
}

ToyFontFace::ToyFontFace(const std::string& family, FontSlant slant, FontWeight weight) :
  FontFace(cairo_toy_font_face_create (family.c_str(),
                                       static_cast<cairo_font_slant_t>(slant),
           	                            static_cast<cairo_font_weight_t>(weight)),
           true /* has reference*/)
{
  check_status_and_throw_exception(cairo_font_face_status(m_cobject));
}

std::string ToyFontFace::get_family() const
{
  return std::string(cairo_toy_font_face_get_family(m_cobject));
}

FontSlant ToyFontFace::get_slant() const
{
  return FontSlant(cairo_toy_font_face_get_slant(m_cobject));
}

FontWeight ToyFontFace::get_weight() const
{
  return FontWeight(cairo_toy_font_face_get_weight(m_cobject));
}


//*************************//
// UserFont Implementation //
//*************************//

static const cairo_user_data_key_t user_font_key = {0};

static void
log_uncaught_exception(const char* message = 0)
{
  std::cerr << "*** cairomm: Uncaught exception in UserFont callback";
  if(message)
    std::cerr << ": " << message;

  std::cerr << std::endl;
}

cairo_status_t
UserFontFace::init_cb(cairo_scaled_font_t* scaled_font,
                      cairo_t *cr,
                      cairo_font_extents_t* metrics)
{
  cairo_font_face_t* face = cairo_scaled_font_get_font_face(scaled_font);
  // we've stored a pointer to the wrapper object in the C object's user_data
  UserFontFace* instance =
    static_cast<UserFontFace*>(cairo_font_face_get_user_data(face,
                                                             &user_font_key));

  if(instance)
  {
    try
    {
      return instance->init(RefPtr<ScaledFont>(new ScaledFont(scaled_font)),
                            RefPtr<Context>(new Context(cr)),
                            static_cast<FontExtents&>(*metrics));
    }
    catch(const std::exception& ex)
    {
      log_uncaught_exception(ex.what());
    }
    catch( ... )
    {
      log_uncaught_exception();
    }
  }

  // this should never happen
  return CAIRO_STATUS_USER_FONT_ERROR;
}

ErrorStatus
UserFontFace::init(const RefPtr<ScaledFont>& /*scaled_font*/,
                   const RefPtr<Context>& /*cr*/,
                   FontExtents& extents)
{
  // fallback behavior is to set up the default text extents as described in the
  // cairo API documentation
  extents.ascent = 1.0;
  extents.descent = 0.0;
  extents.height = 1.0;
  extents.max_x_advance = 1.0;
  extents.max_y_advance = 0.0;
  return CAIRO_STATUS_SUCCESS;
}

cairo_status_t
UserFontFace::unicode_to_glyph_cb(cairo_scaled_font_t *scaled_font,
                                  unsigned long        unicode,
                                  unsigned long       *glyph)
{
  cairo_font_face_t* face = cairo_scaled_font_get_font_face(scaled_font);
  // we've stored a pointer to the wrapper object in the C object's user_data
  UserFontFace* instance =
    static_cast<UserFontFace*>(cairo_font_face_get_user_data(face,
                                                             &user_font_key));
  if(instance)
  {
    try
    {
      return instance->unicode_to_glyph(RefPtr<ScaledFont>(new ScaledFont(scaled_font)),
                                        unicode, *glyph);
    }
    catch(const std::exception& ex)
    {
      log_uncaught_exception(ex.what());
    }
    catch( ... )
    {
      log_uncaught_exception();
    }
  }

  // this should never happen
  return CAIRO_STATUS_USER_FONT_ERROR;
}

ErrorStatus
UserFontFace::unicode_to_glyph(const RefPtr<ScaledFont>& /*scaled_font*/,
                               unsigned long unicode,
                               unsigned long& glyph)
{
  // fallback behavior is just to map 1:1
  glyph = unicode;
  return CAIRO_STATUS_SUCCESS;
}

cairo_status_t
UserFontFace::text_to_glyphs_cb(cairo_scaled_font_t *scaled_font,
                                const char *utf8,
                                int utf8_len,
                                cairo_glyph_t **glyphs,
                                int *num_glyphs,
                                cairo_text_cluster_t **clusters,
                                int *num_clusters,
                                cairo_text_cluster_flags_t *cluster_flags)
{
  cairo_font_face_t* face = cairo_scaled_font_get_font_face(scaled_font);
  // we've stored a pointer to the wrapper object in the C object's user_data
  UserFontFace* instance =
    static_cast<UserFontFace*>(cairo_font_face_get_user_data(face,
                                                             &user_font_key));

  if(instance)
  {
    try
    {
      std::vector<Glyph> glyph_v;
      std::vector<TextCluster> cluster_v;
      const std::string utf8_str(utf8, utf8 + utf8_len);
      TextClusterFlags local_flags = static_cast<TextClusterFlags>(0);

      ErrorStatus status =
        instance->text_to_glyphs(RefPtr<ScaledFont>(new
                                                    ScaledFont(scaled_font)),
                                 utf8_str, glyph_v, cluster_v, local_flags);

      // NOTE: see explanation in text_to_glyphs()
      if (cairo_font_face_get_user_data(face, &USER_DATA_KEY_DEFAULT_TEXT_TO_GLYPHS))
      {
        *num_glyphs = -1;
        return status;
      }

      // TODO: we re-allocate a new array and pass it back to the caller since
      // cairo will free the the returned array.  It sucks to do this excessive
      // allocation and copying, I don't see much alternative besides just
      // presenting a plain-C API.  If cairo didn't free the list, we could
      // possibly just pass back a .data() pointer or something...
      if(num_glyphs && glyphs)
      {
        *num_glyphs = glyph_v.size();
        if(!glyph_v.empty())
        {
          *glyphs = cairo_glyph_allocate(glyph_v.size());
          std::copy(glyph_v.begin(), glyph_v.end(), *glyphs);
        }
      }
      else
        return CAIRO_STATUS_USER_FONT_ERROR;

      // TODO: same for clusters
      if(num_clusters && clusters)
      {
        *num_clusters = cluster_v.size();
        if(!cluster_v.empty())
        {
          *clusters = cairo_text_cluster_allocate(cluster_v.size());
          std::copy(cluster_v.begin(), cluster_v.end(), *clusters);
        }
      }

      if(cluster_flags)
        *cluster_flags = static_cast<cairo_text_cluster_flags_t>(local_flags);

      return status;
    }
    catch(const std::exception& ex)
    {
      log_uncaught_exception(ex.what());
    }
    catch( ... )
    {
      log_uncaught_exception();
    }
  }

  // this should never happen
  return CAIRO_STATUS_USER_FONT_ERROR;
}

ErrorStatus
UserFontFace::text_to_glyphs(const RefPtr<ScaledFont>& /*scaled_font*/,
                             const std::string& /*utf8*/,
                             std::vector<Glyph>& /*glyphs*/,
                             std::vector<TextCluster>& /*clusters*/,
                             TextClusterFlags& /*cluster_flags*/)
{
  // this is a big hack to make up for the fact that we can't easily pass back a
  // negative value for the size of the glyph array, which is a special value in
  // the C API that means: "ignore this function and call unicode_to_glyph
  // instead".  If this default virtual function is ever called, it will set a
  // bool value in the user_data, which we can read back in the
  // text_to_glyphs_cb and used as a signal to return -1 for the num_glyphs
  // parameter.
  static bool first_time = true;
  if (first_time) {
    cairo_font_face_set_user_data(cobj(), &USER_DATA_KEY_DEFAULT_TEXT_TO_GLYPHS, reinterpret_cast<void*>(true), NULL);
    first_time = false;
  }
  return CAIRO_STATUS_SUCCESS;
}

cairo_status_t
UserFontFace::render_glyph_cb(cairo_scaled_font_t  *scaled_font,
                              unsigned long         glyph,
                              cairo_t              *cr,
                              cairo_text_extents_t *metrics)
{
  cairo_font_face_t* face = cairo_scaled_font_get_font_face(scaled_font);
  // we've stored a pointer to the wrapper object in the C object's user_data
  UserFontFace* instance =
    static_cast<UserFontFace*>(cairo_font_face_get_user_data(face,
                                                             &user_font_key));
  if(instance)
  {
    try
    {
      return instance->render_glyph(RefPtr<ScaledFont>(new ScaledFont(scaled_font)),
                                    glyph, RefPtr<Context>(new Context(cr)),
                                    static_cast<TextExtents&>(*metrics));
    }
    catch(const std::exception& ex)
    {
      log_uncaught_exception(ex.what());
    }
    catch( ... )
    {
      log_uncaught_exception();
    }
  }

  // this should never happen
  return CAIRO_STATUS_USER_FONT_ERROR;
}

// no default implementation for UserFontFace::render_glyph(), user must
// implement it

UserFontFace::UserFontFace()
  : FontFace(cairo_user_font_face_create(), true /* has reference */)
{
  check_status_and_throw_exception(cairo_font_face_status(m_cobject));

  // store a pointer to the wrapper class in the user-data, so that when one of
  // the callback functions gets called (which has to be a plain-C function so
  // can't be a class member), we can get a reference to the wrapper class
  cairo_font_face_set_user_data(m_cobject, &user_font_key,
                                 (void*)this, (cairo_destroy_func_t) NULL);
  cairo_user_font_face_set_init_func(cobj(), init_cb);
  cairo_user_font_face_set_render_glyph_func(cobj(), render_glyph_cb);
  cairo_user_font_face_set_unicode_to_glyph_func(cobj(), unicode_to_glyph_cb);
  cairo_user_font_face_set_text_to_glyphs_func(cobj(), text_to_glyphs_cb);
}

UserFontFace::~UserFontFace()
{
}

#ifdef CAIRO_HAS_FT_FONT

RefPtr<FtFontFace>
FtFontFace::create(FT_Face face, int load_flags)
{
  return RefPtr<FtFontFace>(new FtFontFace(face, load_flags));
}

FtFontFace::FtFontFace(FT_Face face, int load_flags) :
  FontFace(cairo_ft_font_face_create_for_ft_face (face, load_flags),
           true /* has reference*/)
{
  check_status_and_throw_exception(cairo_font_face_status(m_cobject));
}

RefPtr<FtFontFace>
FtFontFace::create(FcPattern* pattern)
{
  return RefPtr<FtFontFace>(new FtFontFace(pattern));
}

FtFontFace::FtFontFace(FcPattern* pattern) :
  FontFace(cairo_ft_font_face_create_for_pattern (pattern),
           true /* has reference*/)
{
  check_status_and_throw_exception(cairo_font_face_status(m_cobject));
}

#endif // CAIRO_HAS_FT_FONT

} //namespace Cairo

// vim: ts=2 sw=2 et