summaryrefslogtreecommitdiff
path: root/tests/test-user-font.cc
blob: 656bcfcc144e6e372175772f7d3513018af44313 (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
// vim: ts=2 sw=2 et
/*
 * These tests are of limited usefulness.  In fact, you might even say that
 * they're not really tests at all.  But I felt that it would be useful to have
 * some basic usage of most functions just to verify that things compile and
 * work generally
 */

#include <cfloat>
#include <stdexcept>
#include <boost/test/unit_test.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/floating_point_comparison.hpp>
using namespace boost::unit_test;
#include <cairomm/fontface.h>
#include <cairomm/scaledfont.h>
#include <cairomm/surface.h>
#include <cairomm/context.h>

using namespace Cairo;

// little utility helper classes
struct TestSetup
{
  TestSetup()
  {
    surface = ImageSurface::create(Cairo::FORMAT_ARGB32, 100, 100);
    cr = Cairo::Context::create(surface);
  }

  RefPtr<Context> cr;
  RefPtr<Surface> surface;
};

// a no-op-render user font base class
class NullRenderUserFont : public UserFontFace
{
public:
  ErrorStatus
    render_glyph(const RefPtr<ScaledFont>& /*scaled_font*/,
                 unsigned long /*glyph*/,
                 const RefPtr<Context>& /*cr*/,
                 TextExtents& /*metrics*/)
    { ++count_render_glyph; return CAIRO_STATUS_SUCCESS; }

  int count_render_glyph;

protected:
  NullRenderUserFont() : UserFontFace(), count_render_glyph(0) {}
};

/******************************
 * test_implement_text
 ******************************/
class ImplTextUserFont: public NullRenderUserFont
{
public:
  static RefPtr<ImplTextUserFont> create() { return RefPtr<ImplTextUserFont>(new ImplTextUserFont());};
  ErrorStatus text_to_glyphs(const RefPtr<ScaledFont>& /*scaled_font*/,
                                     const std::string& /*utf8*/,
                                     std::vector<Glyph>& glyphs,
                                     std::vector<TextCluster>& /*clusters*/,
                                     TextClusterFlags& /*cluster_flags*/) override
  {
    ++count_text_to_glyphs;
    // return an arbitrary glyph
    Glyph g = {84, 0, 0};
    glyphs.push_back(g);
    return CAIRO_STATUS_SUCCESS;
  }
  int count_text_to_glyphs;

protected:
  ImplTextUserFont() : count_text_to_glyphs(0) {}
};

void test_implement_text()
{
  TestSetup setup;
  RefPtr<ImplTextUserFont> font = ImplTextUserFont::create();
  setup.cr->set_font_face(font);
  setup.cr->show_text("hello");
  BOOST_REQUIRE(font->count_text_to_glyphs > 0);
  BOOST_REQUIRE(font->count_render_glyph > 0);
}

/******************************
 * test_implement_unicode
 ******************************/
class ImplUnicodeUserFont: public NullRenderUserFont
{
public:
  static RefPtr<ImplUnicodeUserFont> create() { return RefPtr<ImplUnicodeUserFont>(new ImplUnicodeUserFont());};
  ErrorStatus unicode_to_glyph(const RefPtr<ScaledFont>& /*scaled_font*/,
                                       unsigned long /*unicode*/,
                                       unsigned long& /*glyph*/) override
  { ++count_unicode_to_glyph;  return CAIRO_STATUS_SUCCESS;}
  int count_unicode_to_glyph;

protected:
  ImplUnicodeUserFont() : NullRenderUserFont(), count_unicode_to_glyph(0) {}
};

void test_implement_unicode()
{
  TestSetup setup;
  RefPtr<ImplTextUserFont> font = ImplTextUserFont::create();
  setup.cr->set_font_face(font);
  setup.cr->show_text("hello");
  BOOST_REQUIRE(font->count_text_to_glyphs > 0);
  BOOST_REQUIRE(font->count_render_glyph > 0);
}

/******************************
 * test_implement_both
 ******************************/
class ImplBothUserFont: public NullRenderUserFont
{
public:
  static RefPtr<ImplBothUserFont> create() { return RefPtr<ImplBothUserFont>(new ImplBothUserFont());};
  ErrorStatus unicode_to_glyph(const RefPtr<ScaledFont>& /*scaled_font*/,
                                       unsigned long /*unicode*/,
                                       unsigned long& /*glyph*/) override
  { ++count_unicode_to_glyph;  return CAIRO_STATUS_SUCCESS;}
  int count_unicode_to_glyph;

  ErrorStatus text_to_glyphs(const RefPtr<ScaledFont>& /*scaled_font*/,
                                     const std::string& /*utf8*/,
                                     std::vector<Glyph>& glyphs,
                                     std::vector<TextCluster>& /*clusters*/,
                                     TextClusterFlags& /*cluster_flags*/) override
  {
    ++count_text_to_glyphs;
    // return an arbitrary glyph
    Glyph g = {84, 0, 0};
    glyphs.push_back(g);
    return CAIRO_STATUS_SUCCESS;
  }
  int count_text_to_glyphs;

protected:
  ImplBothUserFont() : NullRenderUserFont(), count_unicode_to_glyph(0),
  count_text_to_glyphs(0) {}
};

void test_implement_both()
{
  TestSetup setup;
  RefPtr<ImplBothUserFont> font = ImplBothUserFont::create();
  setup.cr->set_font_face(font);
  setup.cr->show_text("hello");
  // text_to_glyphs should take precedence
  BOOST_REQUIRE(font->count_text_to_glyphs > 0);
  BOOST_REQUIRE(font->count_unicode_to_glyph == 0);
  BOOST_REQUIRE(font->count_render_glyph > 0);
}

/******************************
 * test_implement_neither
 ******************************/
class ImplNeitherUserFont: public NullRenderUserFont
{
public:
  static RefPtr<ImplNeitherUserFont> create() { return RefPtr<ImplNeitherUserFont>(new ImplNeitherUserFont());};

protected:
  ImplNeitherUserFont() : NullRenderUserFont() {}
};

void test_implement_neither()
{
  TestSetup setup;
  RefPtr<ImplNeitherUserFont> font = ImplNeitherUserFont::create();
  setup.cr->set_font_face(font);
  setup.cr->show_text("hello");
  BOOST_REQUIRE(font->count_render_glyph > 0);
}

/******************************
 * test_implement_init
 ******************************/
class ImplInitUserFont: public NullRenderUserFont
{
public:
  static RefPtr<ImplInitUserFont> create() { return RefPtr<ImplInitUserFont>(new ImplInitUserFont());};
  ErrorStatus init(const RefPtr<ScaledFont>& /*scaled_font*/,
                           const RefPtr<Context>& /*cr*/,
                           FontExtents& /*extents*/) override
  {++count_init; return CAIRO_STATUS_SUCCESS;}

  int count_init;

protected:
  ImplInitUserFont() : NullRenderUserFont(), count_init(0) {}
};

void test_implement_init()
{
  TestSetup setup;
  RefPtr<ImplInitUserFont> font = ImplInitUserFont::create();
  setup.cr->set_font_face(font);
  setup.cr->show_text("hello");
  BOOST_REQUIRE(font->count_init > 0);
  BOOST_REQUIRE(font->count_render_glyph > 0);
}

class ExceptionUserFont : public UserFontFace
{
public:
  static RefPtr<ExceptionUserFont> create(int flags) { return RefPtr<ExceptionUserFont>(new ExceptionUserFont(flags));};

  ErrorStatus
  render_glyph(const RefPtr<ScaledFont>& /*scaled_font*/,
               unsigned long /*glyph*/,
               const RefPtr<Context>& /*cr*/,
               TextExtents& /*metrics*/)
  {
    count_render_glyph++;
    if (m_flags & FLAG_RENDER)
      throw std::logic_error("render-glyph exception");
    return CAIRO_STATUS_SUCCESS;
  }

  ErrorStatus
  unicode_to_glyph(const RefPtr<ScaledFont>& /*scaled_font*/,
                                       unsigned long unicode,
                                       unsigned long& glyph)
  {
    count_unicode_to_glyph++;
    if (m_flags & FLAG_UNICODE)
      throw std::logic_error("unicode-to-glyph exception");

    glyph = unicode;
    return CAIRO_STATUS_SUCCESS;
  }

  ErrorStatus
  init(const RefPtr<ScaledFont>& /*scaled_font*/,
                         const RefPtr<Context>& /*cr*/,
                         FontExtents& /*extents*/) override
  {
    count_init++;
    if (m_flags & FLAG_INIT)
      throw std::logic_error("init exception");
    return CAIRO_STATUS_SUCCESS;
  }

  int count_render_glyph;
  int count_text_to_glyphs;
  int count_unicode_to_glyph;
  int count_init;
  int m_flags;

  static const int FLAG_INIT = 1 << 0;
  static const int FLAG_UNICODE = 1 << 1;
  static const int FLAG_RENDER = 1 << 2;

protected:
  ExceptionUserFont(int flags) : UserFontFace(), count_render_glyph(0),
  count_text_to_glyphs(0), count_unicode_to_glyph(0), count_init(0),
  m_flags(flags) {}
};

void test_user_font_exception()
{
  Cairo::RefPtr<ExceptionUserFont> font =
    ExceptionUserFont::create(ExceptionUserFont::FLAG_INIT);
  BOOST_CHECK(font);

  // the init() callback will throw an exception, if this isn't handled in the
  // callback wrapper, the program will abort since an exception can't unwind
  // through C code.  However, due to the exception being thrown, the create()
  // function will fail and throw a new exception.  So if the executable doesn't
  // abort, we should get an exception here.
  Cairo::RefPtr<Cairo::ScaledFont> scaled_font;
  BOOST_CHECK_THROW (scaled_font = Cairo::ScaledFont::create(font,
                                                             Cairo::scaling_matrix(10, 10),
                                                             Cairo::identity_matrix(),
                                                             Cairo::FontOptions()),
                     Cairo::logic_error);
  BOOST_CHECK (font->count_init > 0);

  // now test when an exception is thrown in unicode_to_glyph
  font = ExceptionUserFont::create(ExceptionUserFont::FLAG_UNICODE);
  BOOST_CHECK_NO_THROW (scaled_font = Cairo::ScaledFont::create(font,
                                                                Cairo::scaling_matrix(10, 10),
                                                                Cairo::identity_matrix(),
                                                                Cairo::FontOptions()));
  TestSetup setup;
  setup.cr->set_font_face(font);
  // this call should throw an exception since the callback wrapper will return
  // an error status (that will be translated into an exception) but the test
  // shouldn't abort since the callback exceptions are handled by the callback
  // wrapper
  BOOST_REQUIRE_EQUAL (CAIRO_STATUS_SUCCESS, font->get_status());
  BOOST_CHECK_THROW(setup.cr->show_text("Hello, world"), Cairo::logic_error);
  BOOST_CHECK(font->count_unicode_to_glyph > 0);
  BOOST_CHECK_EQUAL(font->count_render_glyph, 0);

  // now test when an exception is thrown in render_glyph
  font = ExceptionUserFont::create(ExceptionUserFont::FLAG_RENDER);
  BOOST_CHECK_NO_THROW (scaled_font = Cairo::ScaledFont::create(font,
                                                                Cairo::scaling_matrix(10, 10),
                                                                Cairo::identity_matrix(),
                                                                Cairo::FontOptions()));
  // need a new setup since the old cr is now in an error state, so attemtping
  // to use it will throw an exception
  TestSetup setup2;
  BOOST_CHECK_NO_THROW(setup2.cr->set_font_face(font));
  BOOST_REQUIRE_EQUAL (CAIRO_STATUS_SUCCESS, font->get_status());
  BOOST_CHECK_THROW(setup2.cr->show_text("Hello, world"), Cairo::logic_error);
  BOOST_CHECK (font->count_unicode_to_glyph > 0);
  BOOST_CHECK (font->count_render_glyph > 0);
}


test_suite*
init_unit_test_suite(int argc, char* argv[])
{
  // compile even with -Werror
  if (argc && argv) {}

  test_suite* test= BOOST_TEST_SUITE( "Cairo::UserFontFace Tests" );

  test->add (BOOST_TEST_CASE (&test_implement_text));
  test->add (BOOST_TEST_CASE (&test_implement_unicode));
  test->add (BOOST_TEST_CASE (&test_implement_both));
  test->add (BOOST_TEST_CASE (&test_implement_neither));
  test->add (BOOST_TEST_CASE (&test_implement_init));
  test->add (BOOST_TEST_CASE (&test_user_font_exception));

  return test;
}