1 // vim: ts=2 sw=2 et
2 /*
3 * These tests are of limited usefulness. In fact, you might even say that
4 * they're not really tests at all. But I felt that it would be useful to have
5 * some basic usage of most functions just to verify that things compile and
6 * work generally
7 */
8
9 #include <cfloat>
10 #include <stdexcept>
11 #include <boost/test/unit_test.hpp>
12 #include <boost/test/test_tools.hpp>
13 #include <boost/test/floating_point_comparison.hpp>
14 using namespace boost::unit_test;
15 #include <cairomm/fontface.h>
16 #include <cairomm/scaledfont.h>
17 #include <cairomm/surface.h>
18 #include <cairomm/context.h>
19
20 #include <cairo-features.h>
21 #ifdef CAIRO_HAS_WIN32_FONT
22 #include <windows.h>
23 #include <cairomm/win32_font.h>
24 #endif // CAIRO_HAS_WIN32_FONT
25
26 void
test_create_toy()27 test_create_toy ()
28 {
29 auto toy =
30 Cairo::ToyFontFace::create("sans",
31 Cairo::FONT_SLANT_ITALIC,
32 Cairo::FONT_WEIGHT_NORMAL);
33 BOOST_CHECK (toy);
34 BOOST_CHECK_EQUAL (CAIRO_STATUS_SUCCESS, toy->get_status());
35 }
36
test_toy_getters()37 void test_toy_getters ()
38 {
39 auto toy =
40 Cairo::ToyFontFace::create("sans",
41 Cairo::FONT_SLANT_ITALIC,
42 Cairo::FONT_WEIGHT_NORMAL);
43 BOOST_CHECK_EQUAL ("sans", toy->get_family());
44 BOOST_CHECK_EQUAL (Cairo::FONT_SLANT_ITALIC, toy->get_slant());
45 BOOST_CHECK_EQUAL (Cairo::FONT_WEIGHT_NORMAL, toy->get_weight());
46 BOOST_CHECK_EQUAL (Cairo::FONT_TYPE_TOY, toy->get_type());
47 }
48
49 #ifdef CAIRO_HAS_FT_FONT
test_ft_font_face()50 void test_ft_font_face()
51 {
52 auto invalid = FcPatternCreate();
53 Cairo::RefPtr<Cairo::FtFontFace> invalid_face;
54 BOOST_CHECK_THROW(invalid_face = Cairo::FtFontFace::create(invalid), std::bad_alloc);
55
56 // basically taken from the cairo test case -- we don't care what font we're
57 // using so just create an empty pattern and do the minimal substitution to
58 // get a valid pattern
59 auto pattern = FcPatternCreate();
60 FcConfigSubstitute (NULL, pattern, FcMatchPattern);
61 FcDefaultSubstitute (pattern);
62 FcResult result;
63 auto resolved = FcFontMatch (NULL, pattern, &result);
64 auto face = Cairo::FtFontFace::create(resolved);
65 BOOST_CHECK(face);
66
67 // FIXME: test creating from a FT_Face
68 }
69 #endif // CAIRO_HAS_FT_FONT
70
71 #ifdef CAIRO_HAS_WIN32_FONT
test_win32_font_face()72 void test_win32_font_face()
73 {
74 LOGFONTW lf;
75 lf.lfHeight = 10;
76 lf.lfWidth = 0;
77 lf.lfEscapement = 0;
78 lf.lfOrientation = 0;
79 lf.lfWeight = FW_NORMAL;
80 lf.lfItalic = FALSE;
81 lf.lfUnderline = FALSE;
82 lf.lfStrikeOut = FALSE;
83 lf.lfCharSet = ANSI_CHARSET;
84 lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
85 lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
86 lf.lfQuality = DEFAULT_QUALITY;
87 lf.lfPitchAndFamily = DEFAULT_PITCH;
88 wcscpy(lf.lfFaceName, L"Courier New");
89
90 Cairo::RefPtr<Cairo::Win32FontFace> fc(Cairo::Win32FontFace::create(&lf));
91 BOOST_CHECK(fc);
92 Cairo::RefPtr<Cairo::ImageSurface> sfc(Cairo::ImageSurface::create(Cairo::FORMAT_RGB24, 100, 100));
93 Cairo::RefPtr<Cairo::Context> cr(Cairo::Context::create(sfc));
94 cr->translate(0.0, 50.0);
95 cr->set_source_rgb(1.0, 1.0, 1.0);
96 BOOST_CHECK_NO_THROW(cr->set_font_face(fc));
97 BOOST_CHECK_NO_THROW(cr->show_text("Hello, World!"));
98 }
99 #endif // CAIRO_HAS_WIN32_FONT
100
101
102 test_suite*
init_unit_test_suite(int argc,char * argv[])103 init_unit_test_suite(int argc, char* argv[])
104 {
105 // compile even with -Werror
106 if (argc && argv) {}
107
108 test_suite* test= BOOST_TEST_SUITE( "Cairo::FontFace Tests" );
109
110 test->add (BOOST_TEST_CASE (&test_create_toy));
111 test->add (BOOST_TEST_CASE (&test_toy_getters));
112 #ifdef CAIRO_HAS_FT_FONT
113 test->add (BOOST_TEST_CASE (&test_ft_font_face));
114 #endif // CAIRO_HAS_FT_FONT
115 #ifdef CAIRO_HAS_WIN32_FONT
116 test->add (BOOST_TEST_CASE (&test_win32_font_face));
117 #endif // CAIRO_HAS_WIN32_FONT
118
119 return test;
120 }
121