Debian Codegraph @

podofo/test/unit/FontTest.cpp

raw symbols SCIP
1 /**
2 * Copyright (C) 2009 by Dominik Seichter <[email protected]>
3 * Copyright (C) 2021 by Francesco Pretto <[email protected]>
4 *
5 * Licensed under GNU Library General Public 2.0 or later.
6 * Some rights reserved. See COPYING, AUTHORS.
7 */
8
9 #include <PdfTest.h>
10
11 #include <podofo/private/FreetypePrivate.h>
12
13 using namespace std;
14 using namespace PoDoFo;
15
16 #ifdef PODOFO_HAVE_FONTCONFIG
17
18 #include <fontconfig/fontconfig.h>
19
20 static bool getFontInfo(FcPattern* font, string& fontFamily, string& fontPath,
21 PdfFontStyle& style);
22 static void testSingleFont(FcPattern* font);
23
24 TEST_CASE("TestFonts")
25 {
26 // Get all installed fonts
27 auto pattern = FcPatternCreate();
28 auto objectSet = FcObjectSetBuild(FC_FAMILY, FC_STYLE, FC_FILE, FC_SLANT, FC_WEIGHT, nullptr);
29 auto fontSet = FcFontList(nullptr, pattern, objectSet);
30
31 FcObjectSetDestroy(objectSet);
32 FcPatternDestroy(pattern);
33
34 if (fontSet == nullptr)
35 {
36 INFO("Unable to search for fonts");
37 return;
38 }
39
40 INFO(utls::Format("Testing {} fonts", fontSet->nfont));
41 for (int i = 0; i < fontSet->nfont; i++)
42 testSingleFont(fontSet->fonts[i]);
43
44 FcFontSetDestroy(fontSet);
45 }
46
47 TEST_CASE("TestCreateFontExtract")
48 {
49 PdfMemDocument doc;
50 auto& page = doc.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4));
51
52 // Play a bit with font path caching
53 auto fontPath1 = TestUtils::GetTestInputFilePath("Fonts", "LiberationSans-Regular.ttf");
54 auto fontPath2 = TestUtils::GetTestInputFilePath("Fonts", "..", "Fonts", "LiberationSans-Regular.ttf");
55 auto fontRef = &doc.GetFonts().GetOrCreateFont(fontPath1);
56 auto& font = doc.GetFonts().GetOrCreateFont(fontPath2);
57 PdfFont* fontFromBuffer;
58
59 {
60 charbuff fontbuffer;
61 utls::ReadTo(fontbuffer, TestUtils::GetTestInputFilePath("Fonts", "LiberationSans-Regular.ttf"));
62 fontFromBuffer = &doc.GetFonts().GetOrCreateFontFromBuffer(fontbuffer);
63 }
64
65 // The matched fonts should be the same one
66 REQUIRE(&font == fontRef);
67
68 {
69 PdfPainter painter;
70 painter.SetCanvas(page);
71
72 painter.TextState.SetFont(font, 30.0);
73 painter.DrawText("ěščř", 100, 600);
74
75 painter.TextState.SetFont(*fontFromBuffer, 30.0);
76 painter.DrawText("ěščř buffer", 100, 500);
77 painter.FinishDrawing();
78 }
79
80 auto outputpath = TestUtils::GetTestOutputFilePath("TestCreateFontExtract.pdf");
81
82 try
83 {
84 FileStreamDevice stream(outputpath, FileMode::Create);
85 doc.Save(stream);
86 }
87 catch (const PdfError& error)
88 {
89 // Don't continue further the test in this case
90 if (error.GetCode() == PdfErrorCode::UnsupportedFontFormat)
91 return;
92
93 throw;
94 }
95
96 // FIXME: The test crash if we tried to extract
97 // text directly on the original "doc" page
98
99 PdfMemDocument doc2;
100 doc2.Load(outputpath);
101
102 vector<PdfTextEntry> entries;
103 doc2.GetPages().GetPageAt(0).ExtractTextTo(entries);
104
105 REQUIRE(entries[0].Text == "ěščř");
106 REQUIRE(entries[0].X == 100);
107 REQUIRE(entries[0].Y == 600);
108
109 REQUIRE(entries[1].Text == "ěščř buffer");
110 REQUIRE(entries[1].X == 100);
111 REQUIRE(entries[1].Y == 500);
112 }
113
114 void testSingleFont(FcPattern* font)
115 {
116 PdfMemDocument doc;
117 string fontFamily;
118 string fontPath;
119 PdfFontStyle style;
120 PdfFontConfigSearchParams fcParams;
121 auto& fcWrapper = PdfFontManager::GetFontConfigWrapper();
122
123 if (getFontInfo(font, fontFamily, fontPath, style))
124 {
125 unsigned faceIndex;
126 fcParams.Style = style;
127 fontPath = fcWrapper.SearchFontPath(fontFamily, fcParams, faceIndex);
128 if (fontPath.length() != 0)
129 {
130 PdfFontSearchParams params;
131 params.Style = style;
132 (void)doc.GetFonts().SearchFont(fontFamily, params);
133 INFO(utls::Format("Font failed: {}", fontPath));
134 }
135 }
136 }
137
138 bool getFontInfo(FcPattern* font, string& fontFamily, string& fontPath,
139 PdfFontStyle& style)
140 {
141 FcChar8* family = nullptr;
142 FcChar8* path = nullptr;
143 int slant;
144 int weight;
145 style = PdfFontStyle::Regular;
146
147 if (FcPatternGetString(font, FC_FAMILY, 0, &family) == FcResultMatch)
148 {
149 fontFamily = reinterpret_cast<char*>(family);
150 if (FcPatternGetString(font, FC_FILE, 0, &path) == FcResultMatch)
151 {
152 fontPath = reinterpret_cast<char*>(path);
153
154 if (FcPatternGetInteger(font, FC_SLANT, 0, &slant) == FcResultMatch)
155 {
156 if (slant == FC_SLANT_ITALIC || slant == FC_SLANT_OBLIQUE)
157 style |= PdfFontStyle::Italic;
158
159 if (FcPatternGetInteger(font, FC_WEIGHT, 0, &weight) == FcResultMatch)
160 {
161 if (weight >= FC_WEIGHT_BOLD)
162 style |= PdfFontStyle::Bold;
163
164 return true;
165 }
166 }
167 //free( file );
168 }
169 //free( family );
170 }
171
172 return false;
173 }
174
175 #endif // PODOFO_HAVE_FONTCONFIG