Add strikeout font metrics.
This also updates create_test_font so that it can be built, compiles,
and uses SkFontStyle instead of SkTypeface::Style.
BUG=b/63669723
Change-Id: I6eb0f851853f4721cf8e5052255b5b6750c3257f
Reviewed-on: https://skia-review.googlesource.com/24740
Reviewed-by: Cary Clark <caryclark@google.com>
Reviewed-by: Mike Reed <reed@google.com>
Reviewed-by: Mike Klein <mtklein@chromium.org>
Commit-Queue: Ben Wagner <bungeman@google.com>
diff --git a/tools/create_test_font.cpp b/tools/create_test_font.cpp
index 5183981..721e16b 100644
--- a/tools/create_test_font.cpp
+++ b/tools/create_test_font.cpp
@@ -5,11 +5,12 @@
* found in the LICENSE file.
*/
-// running create_test_font generates ./tools/test_font_data.cpp
-// which is read by ./tools/sk_tool_utils_font.cpp
+// running create_test_font generates ./tools/test_font_index.inc
+// and ./tools/test_font_<generic name>.inc which are read by ./tools/sk_tool_utils_font.cpp
#include "Resources.h"
#include "SkOSFile.h"
+#include "SkOSPath.h"
#include "SkPaint.h"
#include "SkPath.h"
#include "SkStream.h"
@@ -21,36 +22,40 @@
#define DEFAULT_FONT_NAME "sans-serif"
-static struct FontDesc {
+namespace {
+
+struct NamedFontStyle {
const char* fName;
- SkTypeface::Style fStyle;
- const char* fFont;
+ SkFontStyle fStyle;
+};
+NamedFontStyle normal = {"Normal", SkFontStyle(SkFontStyle::kNormal_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant)};
+NamedFontStyle bold = {"Bold", SkFontStyle(SkFontStyle::kBold_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant)};
+NamedFontStyle italic = {"Italic", SkFontStyle(SkFontStyle::kNormal_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kItalic_Slant )};
+NamedFontStyle bolditalic = {"BoldItalic", SkFontStyle(SkFontStyle::kBold_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kItalic_Slant )};
+
+struct FontDesc {
+ const char* fGenericName;
+ NamedFontStyle fNamedStyle;
+ const char* fFontName;
const char* fFile;
int fFontIndex;
} gFonts[] = {
- {"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},
+ {"monospace", normal, "Liberation Mono", "LiberationMono-Regular.ttf", -1},
+ {"monospace", bold, "Liberation Mono", "LiberationMono-Bold.ttf", -1},
+ {"monospace", italic, "Liberation Mono", "LiberationMono-Italic.ttf", -1},
+ {"monospace", bolditalic, "Liberation Mono", "LiberationMono-BoldItalic.ttf", -1},
+ {"sans-serif", normal, "Liberation Sans", "LiberationSans-Regular.ttf", -1},
+ {"sans-serif", bold, "Liberation Sans", "LiberationSans-Bold.ttf", -1},
+ {"sans-serif", italic, "Liberation Sans", "LiberationSans-Italic.ttf", -1},
+ {"sans-serif", bolditalic, "Liberation Sans", "LiberationSans-BoldItalic.ttf", -1},
+ {"serif", normal, "Liberation Serif", "LiberationSerif-Regular.ttf", -1},
+ {"serif", bold, "Liberation Serif", "LiberationSerif-Bold.ttf", -1},
+ {"serif", italic, "Liberation Serif", "LiberationSerif-Italic.ttf", -1},
+ {"serif", bolditalic, "Liberation Serif", "LiberationSerif-BoldItalic.ttf", -1},
};
const int gFontsCount = (int) SK_ARRAY_COUNT(gFonts);
-const char* gStyleName[] = {
- "Normal",
- "Bold",
- "Italic",
- "BoldItalic",
-};
-
const char gHeader[] =
"/*\n"
" * Copyright 2015 Google Inc.\n"
@@ -61,6 +66,8 @@
"\n"
"// Auto-generated by ";
+} // namespace
+
static FILE* font_header(const char* family) {
SkString outPath(SkOSPath::Join(".", "tools"));
outPath = SkOSPath::Join(outPath.c_str(), "test_font_");
@@ -73,7 +80,7 @@
fam.writable_str()[dashIndex] = '_';
} while (true);
outPath.append(fam);
- outPath.append(".cpp");
+ outPath.append(".inc");
FILE* out = fopen(outPath.c_str(), "w");
fprintf(out, "%s%s\n\n", gHeader, SkOSPath::Basename(__FILE__).c_str());
return out;
@@ -214,14 +221,14 @@
return result;
}
-static void output_font(SkTypeface* face, const char* name, SkTypeface::Style style, FILE* out) {
+static void output_font(sk_sp<SkTypeface> face, const char* name, NamedFontStyle style, FILE* out) {
int emSize = face->getUnitsPerEm() * 2;
SkPaint paint;
paint.setAntiAlias(true);
paint.setTextAlign(SkPaint::kLeft_Align);
paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
paint.setTextSize(emSize);
- SkSafeUnref(paint.setTypeface(face));
+ paint.setTypeface(std::move(face));
SkTDArray<SkPath::Verb> verbs;
SkTDArray<unsigned> charCodes;
SkTDArray<SkScalar> widths;
@@ -229,7 +236,7 @@
output_path_data(paint, emSize, &ptsOut, &verbs, &charCodes, &widths);
SkString fontnameStr(name);
SkString strippedStr = strip_spaces(fontnameStr);
- strippedStr.appendf("%s", gStyleName[style]);
+ strippedStr.appendf("%s", style.fName);
const char* fontname = strippedStr.c_str();
fprintf(out, "const SkScalar %sPoints[] = {\n", fontname);
ptsOut = strip_final(ptsOut);
@@ -305,13 +312,15 @@
output_scalar(metrics.fCapHeight, emSize, &metricsStr);
output_scalar(metrics.fUnderlineThickness, emSize, &metricsStr);
output_scalar(metrics.fUnderlinePosition, emSize, &metricsStr);
+ output_scalar(metrics.fStrikeoutThickness, emSize, &metricsStr);
+ output_scalar(metrics.fStrikeoutPosition, emSize, &metricsStr);
metricsStr = strip_final(metricsStr);
fprintf(out, "%s\n};\n\n", metricsStr.c_str());
}
struct FontWritten {
- const char* fName;
- SkTypeface::Style fStyle;
+ const char* fFontName;
+ NamedFontStyle fNamedStyle;
};
static SkTDArray<FontWritten> gWritten;
@@ -319,88 +328,74 @@
static int written_index(const FontDesc& fontDesc) {
for (int index = 0; index < gWritten.count(); ++index) {
const FontWritten& writ = gWritten[index];
- if (!strcmp(fontDesc.fFont, writ.fName) && fontDesc.fStyle == writ.fStyle) {
+ if (!strcmp(fontDesc.fFontName, writ.fFontName) &&
+ fontDesc.fNamedStyle.fStyle == writ.fNamedStyle.fStyle)
+ {
return index;
}
}
return -1;
}
-static void generate_fonts() {
+static void generate_fonts(const char* basepath) {
FILE* out = nullptr;
for (int index = 0; index < gFontsCount; ++index) {
FontDesc& fontDesc = gFonts[index];
if ((index & 3) == 0) {
- out = font_header(fontDesc.fName);
+ out = font_header(fontDesc.fGenericName);
}
int fontIndex = written_index(fontDesc);
if (fontIndex >= 0) {
fontDesc.fFontIndex = fontIndex;
continue;
}
- SkTypeface* systemTypeface = SkTypeface::CreateFromName(fontDesc.fFont, fontDesc.fStyle);
- SkASSERT(systemTypeface);
- 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, out);
+ SkString filepath(SkOSPath::Join(basepath, fontDesc.fFile));
+ SkASSERTF(sk_exists(filepath.c_str()), "The file %s does not exist.", filepath.c_str());
+ sk_sp<SkTypeface> resourceTypeface = SkTypeface::MakeFromFile(filepath.c_str());
+ SkASSERTF(resourceTypeface, "The file %s is not a font.", filepath.c_str());
+ output_font(std::move(resourceTypeface), fontDesc.fFontName, fontDesc.fNamedStyle, out);
fontDesc.fFontIndex = gWritten.count();
FontWritten* writ = gWritten.append();
- writ->fName = fontDesc.fFont;
- writ->fStyle = fontDesc.fStyle;
+ writ->fFontName = fontDesc.fFontName;
+ writ->fNamedStyle = fontDesc.fNamedStyle;
if ((index & 3) == 3) {
fclose(out);
}
}
}
-static void generate_index(const char* defaultName) {
- int fontCount = gWritten.count();
- 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);
+static const char* slant_to_string(SkFontStyle::Slant slant) {
+ switch (slant) {
+ case SkFontStyle::kUpright_Slant: return "SkFontStyle::kUpright_Slant";
+ case SkFontStyle::kItalic_Slant : return "SkFontStyle::kItalic_Slant" ;
+ case SkFontStyle::kOblique_Slant: return "SkFontStyle::kOblique_Slant";
+ default: SK_ABORT("Unknown slant"); return "";
}
- fprintf(out, "\n");
-#endif
+}
+
+static void generate_index(const char* defaultName) {
+ FILE* out = font_header("index");
fprintf(out, "static SkTestFontData gTestFonts[] = {\n");
- for (fontIndex = 0; fontIndex < fontCount; ++fontIndex) {
- const FontWritten& writ = gWritten[fontIndex];
- const char* name = writ.fName;
+ for (const FontWritten& writ : gWritten) {
+ const char* name = writ.fFontName;
SkString strippedStr = strip_spaces(SkString(name));
- strippedStr.appendf("%s", gStyleName[writ.fStyle]);
+ strippedStr.appendf("%s", writ.fNamedStyle.fName);
const char* strip = strippedStr.c_str();
fprintf(out,
" { %sPoints, %sVerbs, %sCharCodes,\n"
" %sCharCodesCount, %sWidths,\n"
- " %sMetrics, \"Toy %s\", SkTypeface::k%s, nullptr\n"
+ " %sMetrics, \"Toy %s\", SkFontStyle(%d,%d,%s), nullptr\n"
" },\n",
- strip, strip, strip, strip, strip, strip, name, gStyleName[writ.fStyle]);
+ strip, strip, strip, strip, strip, strip, name,
+ writ.fNamedStyle.fStyle.weight(), writ.fNamedStyle.fStyle.width(),
+ slant_to_string(writ.fNamedStyle.fStyle.slant()));
}
fprintf(out, "};\n\n");
fprintf(out, "const int gTestFontsCount = (int) SK_ARRAY_COUNT(gTestFonts);\n\n");
fprintf(out,
"struct SubFont {\n"
" const char* fName;\n"
- " SkTypeface::Style fStyle;\n"
+ " SkFontStyle fStyle;\n"
" SkTestFontData& fFont;\n"
" const char* fFile;\n"
"};\n\n"
@@ -408,18 +403,22 @@
int defaultIndex = -1;
for (int subIndex = 0; subIndex < gFontsCount; subIndex++) {
const FontDesc& desc = gFonts[subIndex];
- if (defaultIndex < 0 && !strcmp(defaultName, desc.fName)) {
+ if (defaultIndex < 0 && !strcmp(defaultName, desc.fGenericName)) {
defaultIndex = subIndex;
}
fprintf(out,
- " { \"%s\", SkTypeface::k%s, gTestFonts[%d], \"%s\" },\n", desc.fName,
- gStyleName[desc.fStyle], desc.fFontIndex, desc.fFile);
+ " { \"%s\", SkFontStyle(%d,%d,%s), gTestFonts[%d], \"%s\" },\n",
+ desc.fGenericName,
+ desc.fNamedStyle.fStyle.weight(), desc.fNamedStyle.fStyle.width(),
+ slant_to_string(desc.fNamedStyle.fStyle.slant()), 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);
+ " { \"Toy %s\", SkFontStyle(%d,%d,%s), gTestFonts[%d], \"%s\" },\n",
+ desc.fFontName,
+ desc.fNamedStyle.fStyle.weight(), desc.fNamedStyle.fStyle.width(),
+ slant_to_string(desc.fNamedStyle.fStyle.slant()), desc.fFontIndex, desc.fFile);
}
fprintf(out, "};\n\n");
fprintf(out, "const int gSubFontsCount = (int) SK_ARRAY_COUNT(gSubFonts);\n\n");
@@ -429,10 +428,7 @@
}
int main(int , char * const []) {
-#ifndef SK_BUILD_FOR_MAC
- #error "use fonts installed on Mac"
-#endif
- generate_fonts();
+ generate_fonts("/Library/Fonts/");
generate_index(DEFAULT_FONT_NAME);
return 0;
}