1 #include <boost/test/unit_test.hpp>
2 #include <boost/test/test_tools.hpp>
3 #include <boost/test/floating_point_comparison.hpp>
4 using namespace boost::unit_test;
5 #include <cairomm/scaledfont.h>
6 #include <iostream>
7 
8 using namespace Cairo;
9 
test_construction()10 void test_construction()
11 {
12   auto face = ToyFontFace::create("sans", FONT_SLANT_NORMAL, FONT_WEIGHT_NORMAL);
13   Matrix identity;
14   cairo_matrix_init_identity(&identity);
15   auto font = ScaledFont::create(face, identity, identity, FontOptions());
16   BOOST_REQUIRE(font);
17 
18   // now use the default argument for font_options
19   font = ScaledFont::create(face, identity, identity);
20   BOOST_REQUIRE(font);
21 }
22 
test_text_to_glyphs()23 void test_text_to_glyphs()
24 {
25   auto face = ToyFontFace::create("sans", FONT_SLANT_NORMAL, FONT_WEIGHT_NORMAL);
26   Matrix identity;
27   cairo_matrix_init_identity(&identity);
28   auto font = ScaledFont::create(face, identity, identity, FontOptions());
29   BOOST_REQUIRE(font);
30 
31   std::vector<Glyph> glyphs;
32   std::vector<TextCluster> clusters;
33   TextClusterFlags flags;
34   font->text_to_glyphs(0, 0, "foo", glyphs, clusters, flags);
35 
36   BOOST_CHECK_EQUAL(3, glyphs.size());
37   BOOST_CHECK_EQUAL(3, clusters.size());
38 }
39 
test_scale_matrix()40 void test_scale_matrix()
41 {
42   auto face = ToyFontFace::create("sans", FONT_SLANT_NORMAL, FONT_WEIGHT_NORMAL);
43   Matrix m;
44   cairo_matrix_init_scale(&m, 2.0, 4.0);
45   auto font = ScaledFont::create(face, m, m, FontOptions());
46   BOOST_REQUIRE(font);
47 
48   Matrix result;
49   font->get_scale_matrix(result);
50   // no real test, just excercising the functionality
51 }
52 
test_get_font_face()53 void test_get_font_face()
54 {
55   // this is to test for a bug where we were accidentally freeing the resulting
56   // font face from a call to ScaledFont::get_font_face() when we didn't hold a
57   // reference to it
58   auto face = ToyFontFace::create("sans", FONT_SLANT_NORMAL, FONT_WEIGHT_NORMAL);
59   Matrix identity;
60   cairo_matrix_init_identity(&identity);
61   auto font = ScaledFont::create(face, identity, identity, FontOptions());
62   BOOST_REQUIRE(font);
63   const int refcount = cairo_font_face_get_reference_count(face->cobj());
64   {
65     auto got_face = font->get_font_face();
66   } // scope ensure that the font face is destroyed
67   // after creating and destroying the FontFace in get_font_face, our reference
68   // count should be the same
69   BOOST_REQUIRE_EQUAL(cairo_font_face_get_reference_count(face->cobj()), refcount);
70 }
71 
72 #ifdef CAIRO_HAS_FT_FONT
test_ft_scaled_font()73 void test_ft_scaled_font()
74 {
75   auto invalid = FcPatternCreate();
76   Cairo::RefPtr<Cairo::FtFontFace> invalid_face;
77   BOOST_CHECK_THROW(invalid_face = Cairo::FtFontFace::create(invalid), std::bad_alloc);
78 
79   // basically taken from the cairo test case -- we don't care what font we're
80   // using so just create an empty pattern and do the minimal substitution to
81   // get a valid pattern
82   auto pattern = FcPatternCreate();
83   FcConfigSubstitute (NULL, pattern, FcMatchPattern);
84   FcDefaultSubstitute (pattern);
85   FcResult result;
86   auto resolved = FcFontMatch (NULL, pattern, &result);
87   auto face = Cairo::FtFontFace::create(resolved);
88   BOOST_CHECK(face);
89 
90   cairo_scaled_font_t* c_scaled_font = nullptr;
91   int refcount = 0;
92   {
93     auto scaled_font =
94       FtScaledFont::create(face,
95                            Cairo::identity_matrix(),
96                            Cairo::identity_matrix(),
97                            FontOptions());
98     c_scaled_font = scaled_font->cobj();
99     refcount = cairo_scaled_font_get_reference_count(c_scaled_font);
100   }
101   // make sure that the base destructor is called
102   BOOST_CHECK_EQUAL(cairo_scaled_font_get_reference_count(c_scaled_font), refcount -1);
103 }
104 #endif // CAIRO_HAS_FT_FONT
105 
106 
107 test_suite*
init_unit_test_suite(int argc,char * argv[])108 init_unit_test_suite(int argc, char* argv[])
109 {
110   // compile even with -Werror
111   if (argc && argv) {}
112 
113   test_suite* test= BOOST_TEST_SUITE( "Cairo::ScaledFont Tests" );
114 
115   test->add(BOOST_TEST_CASE(&test_construction));
116   test->add(BOOST_TEST_CASE(&test_text_to_glyphs));
117   test->add(BOOST_TEST_CASE(&test_scale_matrix));
118   test->add(BOOST_TEST_CASE(&test_get_font_face));
119 #ifdef CAIRO_HAS_FT_FONT
120   test->add(BOOST_TEST_CASE(&test_ft_scaled_font));
121 #endif // CAIRO_HAS_FT_FONT
122 
123   return test;
124 }
125