2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 // running create_test_font generates ./tools/test_font_data.cpp
9 // which is read by ./tools/sk_tool_utils_font.cpp
11 #include "Resources.h"
18 #include "SkTypeface.h"
22 // the folllowing include is generated by running dm with
23 // --portableFonts --reportUsedChars
24 #include "test_font_data_chars.cpp"
26 #define DEFAULT_FONT_NAME "Liberation Sans"
28 static struct FontDesc {
30 SkTypeface::Style fStyle;
33 const char* fCharsUsed;
36 {"Courier New", SkTypeface::kNormal, "Courier New", "Courier New.ttf",
38 {"Courier New", SkTypeface::kBold, "Courier New", "Courier New Bold.ttf",
40 {"Courier New", SkTypeface::kItalic, "Courier New", "Courier New Italic.ttf",
42 {"Courier New", SkTypeface::kBoldItalic, "Courier New", "Courier New Bold Italic.ttf",
43 gCourierNew_BoldItalic},
44 {"Helvetica", SkTypeface::kNormal, "Liberation Sans", "LiberationSans-Regular.ttf",
46 {"Helvetica", SkTypeface::kBold, "Liberation Sans", "LiberationSans-Bold.ttf",
47 gLiberationSans_Bold},
48 {"Helvetica", SkTypeface::kItalic, "Liberation Sans", "LiberationSans-Italic.ttf",
49 gLiberationSans_Italic},
50 {"Helvetica", SkTypeface::kBoldItalic, "Liberation Sans", "LiberationSans-BoldItalic.ttf",
51 gLiberationSans_BoldItalic},
52 {"Hiragino Maru Gothic Pro", SkTypeface::kNormal, "Hiragino Maru Gothic Pro", "Pro W4.otf",
53 gHiraginoMaruGothicPro},
54 {"Liberation Sans", SkTypeface::kNormal, "Liberation Sans", "LiberationSans-Regular.ttf",
56 {"Liberation Sans", SkTypeface::kBold, "Liberation Sans", "LiberationSans-Bold.ttf",
57 gLiberationSans_Bold},
58 {"Liberation Sans", SkTypeface::kItalic, "Liberation Sans", "LiberationSans-Italic.ttf",
59 gLiberationSans_Italic},
60 {"Liberation Sans", SkTypeface::kBoldItalic, "Liberation Sans", "LiberationSans-BoldItalic.ttf",
61 gLiberationSans_BoldItalic},
62 {"monospace", SkTypeface::kNormal, "Courier New", "Courier New.ttf",
64 {"monospace", SkTypeface::kBold, "Courier New", "Courier New Bold.ttf",
66 {"monospace", SkTypeface::kItalic, "Courier New", "Courier New Italic.ttf",
68 {"monospace", SkTypeface::kBoldItalic, "Courier New", "Courier New Bold Italic.ttf",
69 gCourierNew_BoldItalic},
70 {"Papyrus", SkTypeface::kNormal, "Papyrus", "Papyrus.ttc",
72 {"sans-serif", SkTypeface::kNormal, "Liberation Sans", "LiberationSans-Regular.ttf",
74 {"sans-serif", SkTypeface::kBold, "Liberation Sans", "LiberationSans-Bold.ttf",
75 gLiberationSans_Bold},
76 {"sans-serif", SkTypeface::kItalic, "Liberation Sans", "LiberationSans-Italic.ttf",
77 gLiberationSans_Italic},
78 {"sans-serif", SkTypeface::kBoldItalic, "Liberation Sans", "LiberationSans-BoldItalic.ttf",
79 gLiberationSans_BoldItalic},
80 {"serif", SkTypeface::kNormal, "Times New Roman", "Times New Roman.ttf",
82 {"serif", SkTypeface::kBold, "Times New Roman", "Times New Roman Bold.ttf",
84 {"serif", SkTypeface::kItalic, "Times New Roman", "Times New Roman Italic.ttf",
85 gTimesNewRoman_Italic},
86 {"serif", SkTypeface::kBoldItalic, "Times New Roman", "Times New Roman Bold Italic.ttf",
87 gTimesNewRoman_BoldItalic},
88 {"Times", SkTypeface::kNormal, "Times New Roman", "Times New Roman.ttf",
90 {"Times", SkTypeface::kBold, "Times New Roman", "Times New Roman Bold.ttf",
92 {"Times", SkTypeface::kItalic, "Times New Roman", "Times New Roman Italic.ttf",
93 gTimesNewRoman_Italic},
94 {"Times", SkTypeface::kBoldItalic, "Times New Roman", "Times New Roman Bold Italic.ttf",
95 gTimesNewRoman_BoldItalic},
96 {"Times New Roman", SkTypeface::kNormal, "Times New Roman", "Times New Roman.ttf",
98 {"Times New Roman", SkTypeface::kBold, "Times New Roman", "Times New Roman Bold.ttf",
100 {"Times New Roman", SkTypeface::kItalic, "Times New Roman", "Times New Roman Italic.ttf",
101 gTimesNewRoman_Italic},
102 {"Times New Roman", SkTypeface::kBoldItalic, "Times New Roman", "Times New Roman Bold Italic.ttf",
103 gTimesNewRoman_BoldItalic},
104 {"Times Roman", SkTypeface::kNormal, "Liberation Sans", "LiberationSans-Regular.ttf",
108 const int gFontsCount = (int) SK_ARRAY_COUNT(gFonts);
110 const char* gStyleName[] = {
117 const char gHeader[] =
119 " * Copyright 2014 Google Inc.\n"
121 " * Use of this source code is governed by a BSD-style license that can be\n"
122 " * found in the LICENSE file.\n"
125 "// Auto-generated by ";
127 static FILE* font_header() {
128 SkString outPath(SkOSPath::SkPathJoin(".", "tools"));
129 outPath = SkOSPath::SkPathJoin(outPath.c_str(), "test_font_data.cpp");
130 FILE* out = fopen(outPath.c_str(), "w");
131 fprintf(out, "%s%s\n\n", gHeader, SkOSPath::SkBasename(__FILE__).c_str());
139 static ptrdiff_t last_line_length(const SkString& str) {
140 const char* first = str.c_str();
141 const char* last = first + str.size();
142 const char* ptr = last;
143 while (ptr > first && *--ptr != '\n')
145 return last - ptr - 1;
148 static void output_fixed(SkScalar num, int emSize, SkString* out) {
149 int hex = (int) (num * 65536 / emSize);
150 out->appendf("0x%08x,", hex);
151 *out += (int) last_line_length(*out) >= kMaxLineLength ? '\n' : ' ';
154 static void output_scalar(SkScalar num, int emSize, SkString* out) {
156 if (num == (int) num) {
157 out->appendS32((int) num);
160 str.printf("%1.6g", num);
161 int width = (int) str.size();
162 const char* cStr = str.c_str();
163 while (cStr[width - 1] == '0') {
166 str.remove(width, str.size() - width);
167 out->appendf("%sf", str.c_str());
170 *out += (int) last_line_length(*out) >= kMaxLineLength ? '\n' : ' ';
173 static int output_points(const SkPoint* pts, int emSize, int count, SkString* ptsOut) {
174 for (int index = 0; index < count; ++index) {
175 // SkASSERT(floor(pts[index].fX) == pts[index].fX);
176 output_scalar(pts[index].fX, emSize, ptsOut);
177 // SkASSERT(floor(pts[index].fY) == pts[index].fY);
178 output_scalar(pts[index].fY, emSize, ptsOut);
183 static void output_path_data(const SkPaint& paint, const char* used,
184 int emSize, SkString* ptsOut, SkTDArray<SkPath::Verb>* verbs,
185 SkTDArray<unsigned>* charCodes, SkTDArray<SkScalar>* widths) {
187 SkUnichar index = SkUTF8_NextUnichar(&used);
189 paint.getTextPath((const void*) &index, 2, 0, 0, &path);
190 SkPath::RawIter iter(path);
193 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
194 *verbs->append() = verb;
196 case SkPath::kMove_Verb:
197 output_points(&pts[0], emSize, 1, ptsOut);
199 case SkPath::kLine_Verb:
200 output_points(&pts[1], emSize, 1, ptsOut);
202 case SkPath::kQuad_Verb:
203 output_points(&pts[1], emSize, 2, ptsOut);
205 case SkPath::kCubic_Verb:
206 output_points(&pts[1], emSize, 3, ptsOut);
208 case SkPath::kClose_Verb:
211 SkDEBUGFAIL("bad verb");
215 *verbs->append() = SkPath::kDone_Verb;
216 *charCodes->append() = index;
218 SkDEBUGCODE(int charCount =) paint.getTextWidths((const void*) &index, 2, &width);
219 SkASSERT(charCount == 1);
220 // SkASSERT(floor(width) == width); // not true for Hiragino Maru Gothic Pro
221 *widths->append() = width;
225 static int offset_str_len(unsigned num) {
226 if (num == (unsigned) -1) {
238 static SkString strip_spaces(const SkString& str) {
240 int count = (int) str.size();
241 for (int index = 0; index < count; ++index) {
243 if (c != ' ' && c != '-') {
250 static SkString strip_final(const SkString& str) {
251 SkString result(str);
252 if (result.endsWith("\n")) {
253 result.remove(result.size() - 1, 1);
255 if (result.endsWith(" ")) {
256 result.remove(result.size() - 1, 1);
258 if (result.endsWith(",")) {
259 result.remove(result.size() - 1, 1);
264 static void output_font(SkTypeface* face, const char* name, SkTypeface::Style style,
265 const char* used, FILE* out) {
266 int emSize = face->getUnitsPerEm() * 2;
268 paint.setAntiAlias(true);
269 paint.setTextAlign(SkPaint::kLeft_Align);
270 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
271 paint.setTextSize(emSize);
272 SkSafeUnref(paint.setTypeface(face));
273 SkTDArray<SkPath::Verb> verbs;
274 SkTDArray<unsigned> charCodes;
275 SkTDArray<SkScalar> widths;
277 output_path_data(paint, used, emSize, &ptsOut, &verbs, &charCodes, &widths);
278 SkString fontnameStr(name);
279 SkString strippedStr = strip_spaces(fontnameStr);
280 strippedStr.appendf("%s", gStyleName[style]);
281 const char* fontname = strippedStr.c_str();
282 fprintf(out, "const SkScalar %sPoints[] = {\n", fontname);
283 ptsOut = strip_final(ptsOut);
284 fprintf(out, "%s", ptsOut.c_str());
285 fprintf(out, "\n};\n\n");
286 fprintf(out, "const unsigned char %sVerbs[] = {\n", fontname);
287 int verbCount = verbs.count();
289 for (int index = 0; index < verbCount;) {
290 SkPath::Verb verb = verbs[index];
291 SkASSERT(verb >= SkPath::kMove_Verb && verb <= SkPath::kDone_Verb);
292 SkASSERT((unsigned) verb == (unsigned char) verb);
293 fprintf(out, "%u", verb);
294 if (++index < verbCount) {
296 fprintf(out, "%c", ',');
297 if (outChCount >= kMaxLineLength) {
299 fprintf(out, "%c", '\n');
301 fprintf(out, "%c", ' ');
305 fprintf(out, "\n};\n\n");
307 fprintf(out, "const unsigned %sCharCodes[] = {\n", fontname);
308 int offsetCount = charCodes.count();
309 for (int index = 0; index < offsetCount;) {
310 unsigned offset = charCodes[index];
311 fprintf(out, "%u", offset);
312 if (++index < offsetCount) {
313 outChCount += offset_str_len(offset) + 2;
314 fprintf(out, "%c", ',');
315 if (outChCount >= kMaxLineLength) {
317 fprintf(out, "%c", '\n');
319 fprintf(out, "%c", ' ');
323 fprintf(out, "\n};\n\n");
326 fprintf(out, "const SkFixed %sWidths[] = {\n", fontname);
327 for (int index = 0; index < offsetCount; ++index) {
328 output_fixed(widths[index], emSize, &widthsStr);
330 widthsStr = strip_final(widthsStr);
331 fprintf(out, "%s\n};\n\n", widthsStr.c_str());
333 fprintf(out, "const int %sCharCodesCount = (int) SK_ARRAY_COUNT(%sCharCodes);\n\n",
336 SkPaint::FontMetrics metrics;
337 paint.getFontMetrics(&metrics);
338 fprintf(out, "const SkPaint::FontMetrics %sMetrics = {\n", fontname);
340 metricsStr.printf("0x%08x, ", metrics.fFlags);
341 output_scalar(metrics.fTop, emSize, &metricsStr);
342 output_scalar(metrics.fAscent, emSize, &metricsStr);
343 output_scalar(metrics.fDescent, emSize, &metricsStr);
344 output_scalar(metrics.fBottom, emSize, &metricsStr);
345 output_scalar(metrics.fLeading, emSize, &metricsStr);
346 output_scalar(metrics.fAvgCharWidth, emSize, &metricsStr);
347 output_scalar(metrics.fMaxCharWidth, emSize, &metricsStr);
348 output_scalar(metrics.fXMin, emSize, &metricsStr);
349 output_scalar(metrics.fXMax, emSize, &metricsStr);
350 output_scalar(metrics.fXHeight, emSize, &metricsStr);
351 output_scalar(metrics.fCapHeight, emSize, &metricsStr);
352 output_scalar(metrics.fUnderlineThickness, emSize, &metricsStr);
353 output_scalar(metrics.fUnderlinePosition, emSize, &metricsStr);
354 metricsStr = strip_final(metricsStr);
355 fprintf(out, "%s\n};\n\n", metricsStr.c_str());
360 SkTypeface::Style fStyle;
363 static SkTDArray<FontWritten> gWritten;
365 static int written_index(const FontDesc& fontDesc) {
366 for (int index = 0; index < gWritten.count(); ++index) {
367 const FontWritten& writ = gWritten[index];
368 if (!strcmp(fontDesc.fFont, writ.fName) && fontDesc.fStyle == writ.fStyle) {
375 static void generate_fonts(FILE* out) {
376 for (int index = 0; index < gFontsCount; ++index) {
377 FontDesc& fontDesc = gFonts[index];
378 int fontIndex = written_index(fontDesc);
379 if (fontIndex >= 0) {
380 fontDesc.fFontIndex = fontIndex;
383 SkTypeface* systemTypeface = SkTypeface::CreateFromName(fontDesc.fFont, fontDesc.fStyle);
384 SkASSERT(systemTypeface);
385 SkString filepath(GetResourcePath(fontDesc.fFile));
386 SkASSERT(sk_exists(filepath.c_str()));
387 SkTypeface* resourceTypeface = SkTypeface::CreateFromFile(filepath.c_str());
388 SkASSERT(resourceTypeface);
389 output_font(resourceTypeface, fontDesc.fFont, fontDesc.fStyle, fontDesc.fCharsUsed, out);
390 fontDesc.fFontIndex = gWritten.count();
391 FontWritten* writ = gWritten.append();
392 writ->fName = fontDesc.fFont;
393 writ->fStyle = fontDesc.fStyle;
397 static void generate_index(const char* defaultName, FILE* out) {
398 int fontCount = gWritten.count();
400 "static SkTestFontData gTestFonts[] = {\n");
402 for (fontIndex = 0; fontIndex < fontCount; ++fontIndex) {
403 const FontWritten& writ = gWritten[fontIndex];
404 const char* name = writ.fName;
405 SkString strippedStr = strip_spaces(SkString(name));
406 strippedStr.appendf("%s", gStyleName[writ.fStyle]);
407 const char* strip = strippedStr.c_str();
409 " { %sPoints, %sVerbs, %sCharCodes,\n"
410 " %sCharCodesCount, %sWidths,\n"
411 " %sMetrics, \"%s\", SkTypeface::%s, NULL\n"
413 strip, strip, strip, strip, strip, strip, name, gStyleName[writ.fStyle]);
415 fprintf(out, "};\n\n");
416 fprintf(out, "const int gTestFontsCount = (int) SK_ARRAY_COUNT(gTestFonts);\n\n");
419 " const char* fName;\n"
420 " SkTypeface::Style fStyle;\n"
421 " SkTestFontData& fFont;\n"
422 " const char* fFile;\n"
424 "const SubFont gSubFonts[] = {\n");
425 int defaultIndex = -1;
426 for (int subIndex = 0; subIndex < gFontsCount; subIndex++) {
427 const FontDesc& desc = gFonts[subIndex];
428 if (!strcmp(defaultName, desc.fName)) {
429 defaultIndex = subIndex;
432 " { \"%s\", SkTypeface::%s, gTestFonts[%d], \"%s\"},\n", desc.fName,
433 gStyleName[desc.fStyle], desc.fFontIndex, desc.fFile);
435 fprintf(out, "};\n\n");
436 fprintf(out, "const int gSubFontsCount = (int) SK_ARRAY_COUNT(gSubFonts);\n\n");
437 SkASSERT(defaultIndex >= 0);
438 fprintf(out, "const int gDefaultFontIndex = %d;\n", defaultIndex);
441 int main(int , char * const []) {
442 #ifndef SK_BUILD_FOR_MAC
443 #error "use fonts installed on Mac"
445 FILE* out = font_header();
447 generate_index(DEFAULT_FONT_NAME, out);