xref: /aosp_15_r20/external/skia/tools/fonts/create_test_font.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2014 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 // Running create_test_font generates ./tools/fonts/test_font_index.inc
9 // and ./tools/fonts/test_font_<generic name>.inc which are read by
10 // ./tools/fonts/TestFontMgr.cpp
11 
12 #include "include/core/SkFont.h"
13 #include "include/core/SkFontMetrics.h"
14 #include "include/core/SkFontMgr.h"
15 #include "include/core/SkFontStyle.h"
16 #include "include/core/SkPath.h"
17 #include "include/core/SkSpan.h"
18 #include "include/core/SkStream.h"
19 #include "include/core/SkTypeface.h"
20 #include "include/private/base/SkAssert.h"
21 #include "include/private/base/SkTDArray.h"
22 #include "src/base/SkUTF.h"
23 #include "src/core/SkOSFile.h"
24 #include "src/core/SkPathPriv.h"
25 #include "src/utils/SkOSPath.h"
26 
27 #include <stdio.h>
28 
29 #if defined(SK_FONTMGR_CORETEXT_AVAILABLE)
30 #include "include/ports/SkFontMgr_mac_ct.h"
31 #endif
32 
33 #if defined(SK_FONTMGR_FONTCONFIG_AVAILABLE)
34 #include "include/ports/SkFontMgr_fontconfig.h"
35 #include "include/ports/SkFontScanner_FreeType.h"
36 #endif
37 
38 #if defined(SK_FONTMGR_FREETYPE_EMPTY_AVAILABLE)
39 #include "include/ports/SkFontMgr_empty.h"
40 #endif
41 
42 namespace {
43 
44 struct NamedFontStyle {
45     char const * const fName;
46     char const * const fIdentifierName;
47     SkFontStyle const fStyle;
48 };
49 
50 struct FontDesc {
51     NamedFontStyle const fNamedStyle;
52     char const * const fFile;
53 };
54 
55 struct FontFamilyDesc {
56     char const * const fGenericName;
57     char const * const fFamilyName;
58     char const * const fIdentifierName;
59     SkSpan<const FontDesc> const fFonts;
60 };
61 
62 } // namespace
63 
font_header(const char * family)64 static FILE* font_header(const char* family) {
65     SkString outPath(SkOSPath::Join(".", "tools"));
66     outPath = SkOSPath::Join(outPath.c_str(), "fonts");
67     outPath = SkOSPath::Join(outPath.c_str(), "test_font_");
68     SkString fam(family);
69     do {
70         int dashIndex = fam.find("-");
71         if (dashIndex < 0) {
72             break;
73         }
74         fam.data()[dashIndex] = '_';
75     } while (true);
76     outPath.append(fam);
77     outPath.append(".inc");
78     FILE* out = fopen(outPath.c_str(), "w");
79 
80     static const char kHeader[] =
81         "/*\n"
82         " * Copyright 2015 Google Inc.\n"
83         " *\n"
84         " * Use of this source code is governed by a BSD-style license that can be\n"
85         " * found in the LICENSE file.\n"
86         " */\n"
87         "\n"
88         "// Auto-generated by ";
89     fprintf(out, "%s%s\n\n", kHeader, SkOSPath::Basename(__FILE__).c_str());
90     return out;
91 }
92 
93 enum {
94     kMaxLineLength = 80,
95 };
96 
last_line_length(const SkString & str)97 static ptrdiff_t last_line_length(const SkString& str) {
98     const char* first = str.c_str();
99     const char* last = first + str.size();
100     const char* ptr = last;
101     while (ptr > first && *--ptr != '\n')
102         ;
103     return last - ptr - 1;
104 }
105 
output_fixed(SkScalar num,int emSize,SkString * out)106 static void output_fixed(SkScalar num, int emSize, SkString* out) {
107     uint32_t hex = (uint32_t)(num * 65536 / emSize);
108     out->appendf("0x%08x,", hex);
109     *out += (int) last_line_length(*out) >= kMaxLineLength ? '\n' : ' ';
110 }
111 
output_scalar(SkScalar num,int emSize,SkString * out)112 static void output_scalar(SkScalar num, int emSize, SkString* out) {
113     num /= emSize;
114     if (num == (int) num) {
115        out->appendS32((int) num);
116     } else {
117         SkString str;
118         str.printf("%1.6g", num);
119         int width = (int) str.size();
120         const char* cStr = str.c_str();
121         while (cStr[width - 1] == '0') {
122             --width;
123         }
124         str.remove(width, str.size() - width);
125         out->appendf("%sf", str.c_str());
126     }
127     *out += ',';
128     *out += (int) last_line_length(*out) >= kMaxLineLength ? '\n' : ' ';
129 }
130 
output_points(const SkPoint * pts,int emSize,int count,SkString * ptsOut)131 static int output_points(const SkPoint* pts, int emSize, int count, SkString* ptsOut) {
132     for (int index = 0; index < count; ++index) {
133         output_scalar(pts[index].fX, emSize, ptsOut);
134         output_scalar(pts[index].fY, emSize, ptsOut);
135     }
136     return count;
137 }
138 
output_path_data(const SkFont & font,int emSize,SkString * ptsOut,SkTDArray<SkPath::Verb> * verbs,SkTDArray<unsigned> * charCodes,SkTDArray<SkScalar> * widths)139 static void output_path_data(const SkFont& font,
140         int emSize, SkString* ptsOut, SkTDArray<SkPath::Verb>* verbs,
141         SkTDArray<unsigned>* charCodes, SkTDArray<SkScalar>* widths) {
142     for (SkUnichar index = 0x00; index < 0x7f; ++index) {
143         uint16_t glyphID = font.unicharToGlyph(index);
144         SkPath path;
145         font.getPath(glyphID, &path);
146         for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) {
147             *verbs->append() = (SkPath::Verb)verb;
148             switch (verb) {
149                 case SkPathVerb::kMove:
150                     output_points(&pts[0], emSize, 1, ptsOut);
151                     break;
152                 case SkPathVerb::kLine:
153                     output_points(&pts[1], emSize, 1, ptsOut);
154                     break;
155                 case SkPathVerb::kQuad:
156                     output_points(&pts[1], emSize, 2, ptsOut);
157                     break;
158                 case SkPathVerb::kCubic:
159                     output_points(&pts[1], emSize, 3, ptsOut);
160                     break;
161                 case SkPathVerb::kClose:
162                     break;
163                 default:
164                     SkDEBUGFAIL("bad verb");
165                     SkASSERT(0);
166             }
167         }
168         *verbs->append() = SkPath::kDone_Verb;
169         *charCodes->append() = index;
170         SkScalar width;
171         font.getWidths(&glyphID, 1, &width);
172      // SkASSERT(floor(width) == width);  // not true for Hiragino Maru Gothic Pro
173         *widths->append() = width;
174         if (0 == index) {
175             index = 0x1f;  // skip the rest of the control codes
176         }
177     }
178 }
179 
offset_str_len(unsigned num)180 static int offset_str_len(unsigned num) {
181     if (num == (unsigned) -1) {
182         return 10;
183     }
184     unsigned result = 1;
185     unsigned ref = 10;
186     while (ref <= num) {
187         ++result;
188         ref *= 10;
189     }
190     return result;
191 }
192 
strip_final(const SkString & str)193 static SkString strip_final(const SkString& str) {
194     SkString result(str);
195     if (result.endsWith("\n")) {
196         result.remove(result.size() - 1, 1);
197     }
198     if (result.endsWith(" ")) {
199         result.remove(result.size() - 1, 1);
200     }
201     if (result.endsWith(",")) {
202         result.remove(result.size() - 1, 1);
203     }
204     return result;
205 }
206 
output_font(sk_sp<SkTypeface> face,const char * identifier,FILE * out)207 static void output_font(sk_sp<SkTypeface> face, const char* identifier, FILE* out) {
208     const int emSize = face->getUnitsPerEm() * 2;
209     SkFont font;
210     font.setEdging(SkFont::Edging::kAntiAlias);
211     font.setSize(emSize);
212     font.setTypeface(std::move(face));
213 
214     SkTDArray<SkPath::Verb> verbs;
215     SkTDArray<unsigned> charCodes;
216     SkTDArray<SkScalar> widths;
217     SkString ptsOut;
218     output_path_data(font, emSize, &ptsOut, &verbs, &charCodes, &widths);
219     fprintf(out, "const SkScalar %sPoints[] = {\n", identifier);
220     ptsOut = strip_final(ptsOut);
221     fprintf(out, "%s", ptsOut.c_str());
222     fprintf(out, "\n};\n\n");
223     fprintf(out, "const unsigned char %sVerbs[] = {\n", identifier);
224     int verbCount = verbs.size();
225     int outChCount = 0;
226     for (int index = 0; index < verbCount;) {
227         SkPath::Verb verb = verbs[index];
228         SkASSERT(verb >= SkPath::kMove_Verb && verb <= SkPath::kDone_Verb);
229         SkASSERT(SkTFitsIn<uint8_t>(verb));
230         fprintf(out, "%u", verb);
231         if (++index < verbCount) {
232             outChCount += 3;
233             fprintf(out, "%c", ',');
234             if (outChCount >= kMaxLineLength) {
235                 outChCount = 0;
236                 fprintf(out, "%c", '\n');
237             } else {
238                 fprintf(out, "%c", ' ');
239             }
240         }
241     }
242     fprintf(out, "\n};\n\n");
243 
244     // all fonts are now 0x00, 0x20 - 0xFE
245     // don't need to generate or output character codes?
246     fprintf(out, "const SkUnichar %sCharCodes[] = {\n", identifier);
247     int offsetCount = charCodes.size();
248     for (int index = 0; index < offsetCount;) {
249         unsigned offset = charCodes[index];
250         fprintf(out, "%u", offset);
251         if (++index < offsetCount) {
252             outChCount += offset_str_len(offset) + 2;
253             fprintf(out, "%c", ',');
254             if (outChCount >= kMaxLineLength) {
255                 outChCount = 0;
256                 fprintf(out, "%c", '\n');
257             } else {
258                 fprintf(out, "%c", ' ');
259             }
260         }
261     }
262     fprintf(out, "\n};\n\n");
263 
264     SkString widthsStr;
265     fprintf(out, "const SkFixed %sWidths[] = {\n", identifier);
266     for (int index = 0; index < offsetCount; ++index) {
267         output_fixed(widths[index], emSize, &widthsStr);
268     }
269     widthsStr = strip_final(widthsStr);
270     fprintf(out, "%s\n};\n\n", widthsStr.c_str());
271 
272     fprintf(out, "const size_t %sCharCodesCount = std::size(%sCharCodes);\n\n",
273             identifier, identifier);
274 
275     SkFontMetrics metrics;
276     font.getMetrics(&metrics);
277     fprintf(out, "const SkFontMetrics %sMetrics = {\n", identifier);
278     SkString metricsStr;
279     metricsStr.printf("0x%08x, ", metrics.fFlags);
280     output_scalar(metrics.fTop, emSize, &metricsStr);
281     output_scalar(metrics.fAscent, emSize, &metricsStr);
282     output_scalar(metrics.fDescent, emSize, &metricsStr);
283     output_scalar(metrics.fBottom, emSize, &metricsStr);
284     output_scalar(metrics.fLeading, emSize, &metricsStr);
285     output_scalar(metrics.fAvgCharWidth, emSize, &metricsStr);
286     output_scalar(metrics.fMaxCharWidth, emSize, &metricsStr);
287     output_scalar(metrics.fXMin, emSize, &metricsStr);
288     output_scalar(metrics.fXMax, emSize, &metricsStr);
289     output_scalar(metrics.fXHeight, emSize, &metricsStr);
290     output_scalar(metrics.fCapHeight, emSize, &metricsStr);
291     output_scalar(metrics.fUnderlineThickness, emSize, &metricsStr);
292     output_scalar(metrics.fUnderlinePosition, emSize, &metricsStr);
293     output_scalar(metrics.fStrikeoutThickness, emSize, &metricsStr);
294     output_scalar(metrics.fStrikeoutPosition, emSize, &metricsStr);
295     metricsStr = strip_final(metricsStr);
296     fprintf(out, "%s\n};\n\n", metricsStr.c_str());
297 }
298 
identifier(const FontFamilyDesc & family,const FontDesc & font)299 static SkString identifier(const FontFamilyDesc& family, const FontDesc& font) {
300     SkString id(family.fIdentifierName);
301     id.append(font.fNamedStyle.fIdentifierName);
302     return id;
303 }
304 
generate_fonts(const char * basepath,const SkSpan<const FontFamilyDesc> & families,sk_sp<const SkFontMgr> mgr)305 static void generate_fonts(const char* basepath,
306                            const SkSpan<const FontFamilyDesc>& families,
307                            sk_sp<const SkFontMgr> mgr) {
308     SkASSERT_RELEASE(mgr);
309     FILE* out = nullptr;
310     for (const FontFamilyDesc& family : families) {
311         out = font_header(family.fGenericName);
312         for (const FontDesc& font : family.fFonts) {
313             SkString filepath(SkOSPath::Join(basepath, font.fFile));
314             SkASSERTF(sk_exists(filepath.c_str()), "The file %s does not exist.", filepath.c_str());
315             sk_sp<SkTypeface> resourceTypeface = mgr->makeFromFile(filepath.c_str(), 0);
316             SkASSERTF(resourceTypeface, "The file %s is not a font.", filepath.c_str());
317             output_font(std::move(resourceTypeface), identifier(family, font).c_str(), out);
318         }
319         fclose(out);
320     }
321 }
322 
slant_to_string(SkFontStyle::Slant slant)323 static const char* slant_to_string(SkFontStyle::Slant slant) {
324     switch (slant) {
325         case SkFontStyle::kUpright_Slant: return "SkFontStyle::kUpright_Slant";
326         case SkFontStyle::kItalic_Slant : return "SkFontStyle::kItalic_Slant" ;
327         case SkFontStyle::kOblique_Slant: return "SkFontStyle::kOblique_Slant";
328         default: SK_ABORT("Unknown slant");
329     }
330 }
331 
generate_index(const SkSpan<const FontFamilyDesc> & families,const FontDesc * defaultFont)332 static void generate_index(const SkSpan<const FontFamilyDesc>& families,
333                            const FontDesc* defaultFont)
334 {
335     FILE* out = font_header("index");
336     fprintf(out, "static SkTestFontData gTestFonts[] = {\n");
337     for (const FontFamilyDesc& family : families) {
338         for (const FontDesc& font : family.fFonts) {
339             SkString identifierStr = identifier(family, font);
340             const char* identifier = identifierStr.c_str();
341             const SkFontStyle& style = font.fNamedStyle.fStyle;
342             fprintf(out,
343                     "    {    %sPoints, %sVerbs,\n"
344                     "         %sCharCodes, %sCharCodesCount, %sWidths,\n"
345                     "         %sMetrics, \"Toy %s\", SkFontStyle(%d,%d,%s)\n"
346                     "    },\n",
347                     identifier, identifier,
348                     identifier, identifier, identifier,
349                     identifier, family.fFamilyName,
350                     style.weight(), style.width(), slant_to_string(style.slant()));
351         }
352     }
353     fprintf(out, "};\n\n");
354     fprintf(out,
355             "struct SubFont {\n"
356             "    const char* fFamilyName;\n"
357             "    const char* fStyleName;\n"
358             "    SkFontStyle fStyle;\n"
359             "    SkTestFontData& fFont;\n"
360             "    const char* fFile;\n"
361             "};\n\n"
362             "const SubFont gSubFonts[] = {\n");
363     int defaultIndex = -1;
364     int testFontsIndex = 0;
365     for (const FontFamilyDesc& family : families) {
366         for (const FontDesc& font : family.fFonts) {
367             if (&font == defaultFont) {
368                 defaultIndex = testFontsIndex;
369             }
370             const SkFontStyle& style = font.fNamedStyle.fStyle;
371             fprintf(out,
372                     "    { \"%s\", \"%s\", SkFontStyle(%d,%d,%s), gTestFonts[%d], \"%s\" },\n",
373                     family.fGenericName, font.fNamedStyle.fName,
374                     style.weight(), style.width(), slant_to_string(style.slant()),
375                     testFontsIndex, font.fFile);
376             testFontsIndex++;
377         }
378     }
379     testFontsIndex = 0;
380     for (const FontFamilyDesc& family : families) {
381         for (const FontDesc& font : family.fFonts) {
382             fprintf(out,
383                     "    { \"Toy %s\", \"%s\", SkFontStyle(%d,%d,%s), gTestFonts[%d], \"%s\" },\n",
384                     family.fFamilyName, font.fNamedStyle.fName,
385                     font.fNamedStyle.fStyle.weight(), font.fNamedStyle.fStyle.width(),
386                     slant_to_string(font.fNamedStyle.fStyle.slant()), testFontsIndex, font.fFile);
387             testFontsIndex++;
388         }
389     }
390     fprintf(out, "};\n\n");
391     SkASSERT(defaultIndex >= 0);
392     fprintf(out, "const size_t gDefaultFontIndex = %d;\n", defaultIndex);
393     fclose(out);
394 }
395 
main(int,char * const[])396 int main(int , char * const []) {
397     constexpr NamedFontStyle normal     = {"Normal",      "Normal",     SkFontStyle::Normal()    };
398     constexpr NamedFontStyle bold       = {"Bold",        "Bold",       SkFontStyle::Bold()      };
399     constexpr NamedFontStyle italic     = {"Italic",      "Italic",     SkFontStyle::Italic()    };
400     constexpr NamedFontStyle bolditalic = {"Bold Italic", "BoldItalic", SkFontStyle::BoldItalic()};
401 
402     static constexpr FontDesc kMonoFonts[] = {
403         {normal,     "LiberationMono-Regular.ttf"},
404         {bold,       "LiberationMono-Bold.ttf"},
405         {italic,     "LiberationMono-Italic.ttf"},
406         {bolditalic, "LiberationMono-BoldItalic.ttf"},
407     };
408 
409     static constexpr FontDesc kSansFonts[] = {
410         {normal,     "LiberationSans-Regular.ttf"},
411         {bold,       "LiberationSans-Bold.ttf"},
412         {italic,     "LiberationSans-Italic.ttf"},
413         {bolditalic, "LiberationSans-BoldItalic.ttf"},
414     };
415 
416     static constexpr FontDesc kSerifFonts[] = {
417         {normal,     "LiberationSerif-Regular.ttf"},
418         {bold,       "LiberationSerif-Bold.ttf"},
419         {italic,     "LiberationSerif-Italic.ttf"},
420         {bolditalic, "LiberationSerif-BoldItalic.ttf"},
421     };
422 
423     static constexpr FontFamilyDesc kFamiliesData[] = {
424         {"monospace",  "Liberation Mono",  "LiberationMono",  kMonoFonts},
425         {"sans-serif", "Liberation Sans",  "LiberationSans",  kSansFonts},
426         {"serif",      "Liberation Serif", "LiberationSerif", kSerifFonts},
427     };
428 
429     static constexpr SkSpan<const FontFamilyDesc> kFamilies(kFamiliesData);
430 
431     sk_sp<SkFontMgr> mgr;
432 #if defined(SK_FONTMGR_FONTCONFIG_AVAILABLE)
433     mgr = SkFontMgr_New_FontConfig(nullptr, SkFontScanner_Make_FreeType());
434 #elif defined(SK_FONTMGR_CORETEXT_AVAILABLE)
435     mgr = SkFontMgr_New_CoreText(nullptr);
436 #elif defined(SK_FONTMGR_FREETYPE_EMPTY_AVAILABLE)
437     mgr = SkFontMgr_New_Custom_Empty();
438 #else
439     SkDEBUGFAIL("Unsupported FontMgr");
440 #endif
441 
442 #if defined(SK_BUILD_FOR_UNIX)
443 #define SK_FONT_FOLDER "/usr/share/fonts/truetype/liberation/"
444 #elif defined(SK_BUILD_FOR_MAC)
445 #define SK_FONT_FOLDER "/Library/Fonts/"
446 #else
447 #error "Unsupported OS"
448 #endif
449 
450     generate_fonts(SK_FONT_FOLDER, kFamilies, mgr);
451     generate_index(kFamilies, &kFamilies[1].fFonts[0]);
452     return 0;
453 }
454