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