Index: tools/create_test_font.cpp |
diff --git a/tools/create_test_font.cpp b/tools/create_test_font.cpp |
index 5e4f6bc9fc439512073b2fd5ec86d3b2d291ce4b..42527d97cce78828a6a9eef449b5626f2ced15c5 100644 |
--- a/tools/create_test_font.cpp |
+++ b/tools/create_test_font.cpp |
@@ -19,104 +19,41 @@ |
#include "SkUtils.h" |
#include <stdio.h> |
-// the folllowing include is generated by running dm with |
-// --portableFonts --reportUsedChars |
-#include "test_font_data_chars.cpp" |
- |
-#define DEFAULT_FONT_NAME "Liberation Sans" |
+#define DEFAULT_FONT_NAME "sans-serif" |
static struct FontDesc { |
const char* fName; |
SkTypeface::Style fStyle; |
const char* fFont; |
const char* fFile; |
- const char* fCharsUsed; |
int fFontIndex; |
} gFonts[] = { |
- {"Courier New", SkTypeface::kNormal, "Courier New", "Courier New.ttf", |
- gCourierNew, -1}, |
- {"Courier New", SkTypeface::kBold, "Courier New", "Courier New Bold.ttf", |
- gCourierNew_Bold, -1}, |
- {"Courier New", SkTypeface::kItalic, "Courier New", "Courier New Italic.ttf", |
- gCourierNew_Italic, -1}, |
- {"Courier New", SkTypeface::kBoldItalic, "Courier New", "Courier New Bold Italic.ttf", |
- gCourierNew_BoldItalic, -1}, |
- {"Helvetica", SkTypeface::kNormal, "Liberation Sans", "LiberationSans-Regular.ttf", |
- gLiberationSans, -1}, |
- {"Helvetica", SkTypeface::kBold, "Liberation Sans", "LiberationSans-Bold.ttf", |
- gLiberationSans_Bold, -1}, |
- {"Helvetica", SkTypeface::kItalic, "Liberation Sans", "LiberationSans-Italic.ttf", |
- gLiberationSans_Italic, -1}, |
- {"Helvetica", SkTypeface::kBoldItalic, "Liberation Sans", "LiberationSans-BoldItalic.ttf", |
- gLiberationSans_BoldItalic, -1}, |
- {"Hiragino Maru Gothic Pro", SkTypeface::kNormal, "Hiragino Maru Gothic Pro", "Pro W4.otf", |
- gHiraginoMaruGothicPro, -1}, |
- {"Liberation Sans", SkTypeface::kNormal, "Liberation Sans", "LiberationSans-Regular.ttf", |
- gLiberationSans, -1}, |
- {"Liberation Sans", SkTypeface::kBold, "Liberation Sans", "LiberationSans-Bold.ttf", |
- gLiberationSans_Bold, -1}, |
- {"Liberation Sans", SkTypeface::kItalic, "Liberation Sans", "LiberationSans-Italic.ttf", |
- gLiberationSans_Italic, -1}, |
- {"Liberation Sans", SkTypeface::kBoldItalic, "Liberation Sans", "LiberationSans-BoldItalic.ttf", |
- gLiberationSans_BoldItalic, -1}, |
- {"monospace", SkTypeface::kNormal, "Courier New", "Courier New.ttf", |
- gCourierNew, -1}, |
- {"monospace", SkTypeface::kBold, "Courier New", "Courier New Bold.ttf", |
- gCourierNew_Bold, -1}, |
- {"monospace", SkTypeface::kItalic, "Courier New", "Courier New Italic.ttf", |
- gCourierNew_Italic, -1}, |
- {"monospace", SkTypeface::kBoldItalic, "Courier New", "Courier New Bold Italic.ttf", |
- gCourierNew_BoldItalic, -1}, |
- {"Papyrus", SkTypeface::kNormal, "Papyrus", "Papyrus.ttc", |
- gPapyrus, -1}, |
- {"sans-serif", SkTypeface::kNormal, "Liberation Sans", "LiberationSans-Regular.ttf", |
- gLiberationSans, -1}, |
- {"sans-serif", SkTypeface::kBold, "Liberation Sans", "LiberationSans-Bold.ttf", |
- gLiberationSans_Bold, -1}, |
- {"sans-serif", SkTypeface::kItalic, "Liberation Sans", "LiberationSans-Italic.ttf", |
- gLiberationSans_Italic, -1}, |
- {"sans-serif", SkTypeface::kBoldItalic, "Liberation Sans", "LiberationSans-BoldItalic.ttf", |
- gLiberationSans_BoldItalic, -1}, |
- {"serif", SkTypeface::kNormal, "Times New Roman", "Times New Roman.ttf", |
- gTimesNewRoman, -1}, |
- {"serif", SkTypeface::kBold, "Times New Roman", "Times New Roman Bold.ttf", |
- gTimesNewRoman_Bold, -1}, |
- {"serif", SkTypeface::kItalic, "Times New Roman", "Times New Roman Italic.ttf", |
- gTimesNewRoman_Italic, -1}, |
- {"serif", SkTypeface::kBoldItalic, "Times New Roman", "Times New Roman Bold Italic.ttf", |
- gTimesNewRoman_BoldItalic, -1}, |
- {"Times", SkTypeface::kNormal, "Times New Roman", "Times New Roman.ttf", |
- gTimesNewRoman, -1}, |
- {"Times", SkTypeface::kBold, "Times New Roman", "Times New Roman Bold.ttf", |
- gTimesNewRoman_Bold, -1}, |
- {"Times", SkTypeface::kItalic, "Times New Roman", "Times New Roman Italic.ttf", |
- gTimesNewRoman_Italic, -1}, |
- {"Times", SkTypeface::kBoldItalic, "Times New Roman", "Times New Roman Bold Italic.ttf", |
- gTimesNewRoman_BoldItalic, -1}, |
- {"Times New Roman", SkTypeface::kNormal, "Times New Roman", "Times New Roman.ttf", |
- gTimesNewRoman, -1}, |
- {"Times New Roman", SkTypeface::kBold, "Times New Roman", "Times New Roman Bold.ttf", |
- gTimesNewRoman_Bold, -1}, |
- {"Times New Roman", SkTypeface::kItalic, "Times New Roman", "Times New Roman Italic.ttf", |
- gTimesNewRoman_Italic, -1}, |
- {"Times New Roman", SkTypeface::kBoldItalic, "Times New Roman", "Times New Roman Bold Italic.ttf", |
- gTimesNewRoman_BoldItalic, -1}, |
- {"Times Roman", SkTypeface::kNormal, "Liberation Sans", "LiberationSans-Regular.ttf", |
- gLiberationSans, -1}, |
+ {"monospace", SkTypeface::kNormal, "Liberation Mono", "LiberationMono-Regular.ttf", -1}, |
+ {"monospace", SkTypeface::kBold, "Liberation Mono", "LiberationMono-Bold.ttf", -1}, |
+ {"monospace", SkTypeface::kItalic, "Liberation Mono", "LiberationMono-Italic.ttf", -1}, |
+ {"monospace", SkTypeface::kBoldItalic, "Liberation Mono", "LiberationMono-BoldItalic.ttf", -1}, |
+ {"sans-serif", SkTypeface::kNormal, "Liberation Sans", "LiberationSans-Regular.ttf", -1}, |
+ {"sans-serif", SkTypeface::kBold, "Liberation Sans", "LiberationSans-Bold.ttf", -1}, |
+ {"sans-serif", SkTypeface::kItalic, "Liberation Sans", "LiberationSans-Italic.ttf", -1}, |
+ {"sans-serif", SkTypeface::kBoldItalic, "Liberation Sans", "LiberationSans-BoldItalic.ttf", -1}, |
+ {"serif", SkTypeface::kNormal, "Liberation Serif", "LiberationSerif-Regular.ttf", -1}, |
+ {"serif", SkTypeface::kBold, "Liberation Serif", "LiberationSerif-Bold.ttf", -1}, |
+ {"serif", SkTypeface::kItalic, "Liberation Serif", "LiberationSerif-Italic.ttf", -1}, |
+ {"serif", SkTypeface::kBoldItalic, "Liberation Serif", "LiberationSerif-BoldItalic.ttf", -1}, |
}; |
const int gFontsCount = (int) SK_ARRAY_COUNT(gFonts); |
const char* gStyleName[] = { |
- "kNormal", |
- "kBold", |
- "kItalic", |
- "kBoldItalic", |
+ "Normal", |
+ "Bold", |
+ "Italic", |
+ "BoldItalic", |
}; |
const char gHeader[] = |
"/*\n" |
-" * Copyright 2014 Google Inc.\n" |
+" * Copyright 2015 Google Inc.\n" |
" *\n" |
" * Use of this source code is governed by a BSD-style license that can be\n" |
" * found in the LICENSE file.\n" |
@@ -124,9 +61,19 @@ const char gHeader[] = |
"\n" |
"// Auto-generated by "; |
-static FILE* font_header() { |
+static FILE* font_header(const char* family) { |
SkString outPath(SkOSPath::Join(".", "tools")); |
- outPath = SkOSPath::Join(outPath.c_str(), "test_font_data.cpp"); |
+ outPath = SkOSPath::Join(outPath.c_str(), "test_font_"); |
+ SkString fam(family); |
+ do { |
+ int dashIndex = fam.find("-"); |
+ if (dashIndex < 0) { |
+ break; |
+ } |
+ fam.writable_str()[dashIndex] = '_'; |
+ } while (true); |
+ outPath.append(fam); |
+ outPath.append(".cpp"); |
FILE* out = fopen(outPath.c_str(), "w"); |
fprintf(out, "%s%s\n\n", gHeader, SkOSPath::Basename(__FILE__).c_str()); |
return out; |
@@ -180,10 +127,13 @@ static int output_points(const SkPoint* pts, int emSize, int count, SkString* pt |
return count; |
} |
-static void output_path_data(const SkPaint& paint, const char* used, |
+static void output_path_data(const SkPaint& paint, |
int emSize, SkString* ptsOut, SkTDArray<SkPath::Verb>* verbs, |
SkTDArray<unsigned>* charCodes, SkTDArray<SkScalar>* widths) { |
- while (*used) { |
+ for (char ch = 0x20; ch < 0x7F; ++ch) { |
+ char str[1]; |
+ str[0] = ch; |
+ const char* used = str; |
SkUnichar index = SkUTF8_NextUnichar(&used); |
SkPath path; |
paint.getTextPath((const void*) &index, 2, 0, 0, &path); |
@@ -261,8 +211,7 @@ static SkString strip_final(const SkString& str) { |
return result; |
} |
-static void output_font(SkTypeface* face, const char* name, SkTypeface::Style style, |
- const char* used, FILE* out) { |
+static void output_font(SkTypeface* face, const char* name, SkTypeface::Style style, FILE* out) { |
int emSize = face->getUnitsPerEm() * 2; |
SkPaint paint; |
paint.setAntiAlias(true); |
@@ -274,7 +223,7 @@ static void output_font(SkTypeface* face, const char* name, SkTypeface::Style st |
SkTDArray<unsigned> charCodes; |
SkTDArray<SkScalar> widths; |
SkString ptsOut; |
- output_path_data(paint, used, emSize, &ptsOut, &verbs, &charCodes, &widths); |
+ output_path_data(paint, emSize, &ptsOut, &verbs, &charCodes, &widths); |
SkString fontnameStr(name); |
SkString strippedStr = strip_spaces(fontnameStr); |
strippedStr.appendf("%s", gStyleName[style]); |
@@ -304,6 +253,8 @@ static void output_font(SkTypeface* face, const char* name, SkTypeface::Style st |
} |
fprintf(out, "\n};\n\n"); |
+ // FIXME: all fonts are now 0x20 - 0xFE |
+ // don't need to generate or output character codes |
fprintf(out, "const unsigned %sCharCodes[] = {\n", fontname); |
int offsetCount = charCodes.count(); |
for (int index = 0; index < offsetCount;) { |
@@ -372,9 +323,13 @@ static int written_index(const FontDesc& fontDesc) { |
return -1; |
} |
-static void generate_fonts(FILE* out) { |
+static void generate_fonts() { |
+ FILE* out = NULL; |
for (int index = 0; index < gFontsCount; ++index) { |
FontDesc& fontDesc = gFonts[index]; |
+ if ((index & 3) == 0) { |
+ out = font_header(fontDesc.fName); |
+ } |
int fontIndex = written_index(fontDesc); |
if (fontIndex >= 0) { |
fontDesc.fFontIndex = fontIndex; |
@@ -382,23 +337,48 @@ static void generate_fonts(FILE* out) { |
} |
SkTypeface* systemTypeface = SkTypeface::CreateFromName(fontDesc.fFont, fontDesc.fStyle); |
SkASSERT(systemTypeface); |
- SkString filepath(GetResourcePath(fontDesc.fFile)); |
+ SkString filepath("/Library/Fonts/"); |
+ filepath.append(fontDesc.fFile); |
SkASSERT(sk_exists(filepath.c_str())); |
SkTypeface* resourceTypeface = SkTypeface::CreateFromFile(filepath.c_str()); |
SkASSERT(resourceTypeface); |
- output_font(resourceTypeface, fontDesc.fFont, fontDesc.fStyle, fontDesc.fCharsUsed, out); |
+ output_font(resourceTypeface, fontDesc.fFont, fontDesc.fStyle, out); |
fontDesc.fFontIndex = gWritten.count(); |
FontWritten* writ = gWritten.append(); |
writ->fName = fontDesc.fFont; |
writ->fStyle = fontDesc.fStyle; |
+ if ((index & 3) == 3) { |
+ fclose(out); |
+ } |
} |
} |
-static void generate_index(const char* defaultName, FILE* out) { |
+static void generate_index(const char* defaultName) { |
int fontCount = gWritten.count(); |
- fprintf(out, |
- "static SkTestFontData gTestFonts[] = {\n"); |
+ FILE* out = font_header("index"); |
int fontIndex; |
+#if 0 |
+ // currently generated files are inlined one after the other. |
+ // if the inlining is undesirable, generate externs using the code below |
+ // (additional code required to add include files) |
+ for (fontIndex = 0; fontIndex < fontCount; ++fontIndex) { |
+ const FontWritten& writ = gWritten[fontIndex]; |
+ const char* name = writ.fName; |
+ SkString strippedStr = strip_spaces(SkString(name)); |
+ strippedStr.appendf("%s", gStyleName[writ.fStyle]); |
+ const char* strip = strippedStr.c_str(); |
+ fprintf(out, |
+ "extern const SkScalar %sPoints[];\n" |
+ "extern const unsigned char %sVerbs[];\n" |
+ "extern const unsigned %sCharCodes[];\n" |
+ "extern const int %sCharCodesCount;\n" |
+ "extern const SkFixed %sWidths[];\n" |
+ "extern const SkPaint::FontMetrics %sMetrics;\n", |
+ strip, strip, strip, strip, strip, strip); |
+ } |
+ fprintf(out, "\n"); |
+#endif |
+ fprintf(out, "static SkTestFontData gTestFonts[] = {\n"); |
for (fontIndex = 0; fontIndex < fontCount; ++fontIndex) { |
const FontWritten& writ = gWritten[fontIndex]; |
const char* name = writ.fName; |
@@ -408,7 +388,7 @@ static void generate_index(const char* defaultName, FILE* out) { |
fprintf(out, |
" { %sPoints, %sVerbs, %sCharCodes,\n" |
" %sCharCodesCount, %sWidths,\n" |
- " %sMetrics, \"%s\", SkTypeface::%s, NULL\n" |
+ " %sMetrics, \"Toy %s\", SkTypeface::k%s, NULL\n" |
" },\n", |
strip, strip, strip, strip, strip, strip, name, gStyleName[writ.fStyle]); |
} |
@@ -425,26 +405,31 @@ static void generate_index(const char* defaultName, FILE* out) { |
int defaultIndex = -1; |
for (int subIndex = 0; subIndex < gFontsCount; subIndex++) { |
const FontDesc& desc = gFonts[subIndex]; |
- if (!strcmp(defaultName, desc.fName)) { |
+ if (defaultIndex < 0 && !strcmp(defaultName, desc.fName)) { |
defaultIndex = subIndex; |
} |
fprintf(out, |
- " { \"%s\", SkTypeface::%s, gTestFonts[%d], \"%s\"},\n", desc.fName, |
+ " { \"%s\", SkTypeface::k%s, gTestFonts[%d], \"%s\" },\n", desc.fName, |
+ gStyleName[desc.fStyle], desc.fFontIndex, desc.fFile); |
+ } |
+ for (int subIndex = 0; subIndex < gFontsCount; subIndex++) { |
+ const FontDesc& desc = gFonts[subIndex]; |
+ fprintf(out, |
+ " { \"Toy %s\", SkTypeface::k%s, gTestFonts[%d], \"%s\" },\n", desc.fFont, |
gStyleName[desc.fStyle], desc.fFontIndex, desc.fFile); |
} |
fprintf(out, "};\n\n"); |
fprintf(out, "const int gSubFontsCount = (int) SK_ARRAY_COUNT(gSubFonts);\n\n"); |
SkASSERT(defaultIndex >= 0); |
fprintf(out, "const int gDefaultFontIndex = %d;\n", defaultIndex); |
+ fclose(out); |
} |
int main(int , char * const []) { |
#ifndef SK_BUILD_FOR_MAC |
#error "use fonts installed on Mac" |
#endif |
- FILE* out = font_header(); |
- generate_fonts(out); |
- generate_index(DEFAULT_FONT_NAME, out); |
- fclose(out); |
+ generate_fonts(); |
+ generate_index(DEFAULT_FONT_NAME); |
return 0; |
} |