2014-06-23 18:25:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2018-02-20 22:06:07 +00:00
|
|
|
// Running create_test_font generates ./tools/fonts/test_font_index.inc
|
|
|
|
// and ./tools/fonts/test_font_<generic name>.inc which are read by
|
|
|
|
// ./tools/fonts/sk_tool_utils_font.cpp
|
2014-07-31 12:58:44 +00:00
|
|
|
|
2017-07-27 14:45:29 +00:00
|
|
|
#include "SkFontStyle.h"
|
2014-07-31 12:58:44 +00:00
|
|
|
#include "SkOSFile.h"
|
2017-07-17 19:32:25 +00:00
|
|
|
#include "SkOSPath.h"
|
2014-06-23 18:25:00 +00:00
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkTArray.h"
|
|
|
|
#include "SkTSort.h"
|
|
|
|
#include "SkTypeface.h"
|
2018-08-21 15:45:46 +00:00
|
|
|
#include "SkUTF.h"
|
2014-06-23 18:25:00 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2015-06-10 16:31:09 +00:00
|
|
|
#define DEFAULT_FONT_NAME "sans-serif"
|
2014-07-31 12:58:44 +00:00
|
|
|
|
2017-07-17 19:32:25 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct NamedFontStyle {
|
2014-07-31 12:58:44 +00:00
|
|
|
const char* fName;
|
2017-07-17 19:32:25 +00:00
|
|
|
SkFontStyle fStyle;
|
|
|
|
};
|
2017-07-27 14:45:29 +00:00
|
|
|
constexpr NamedFontStyle normal = {"Normal", SkFontStyle::Normal() };
|
|
|
|
constexpr NamedFontStyle bold = {"Bold", SkFontStyle::Bold() };
|
|
|
|
constexpr NamedFontStyle italic = {"Italic", SkFontStyle::Italic() };
|
|
|
|
constexpr NamedFontStyle bolditalic = {"BoldItalic", SkFontStyle::BoldItalic()};
|
2017-07-17 19:32:25 +00:00
|
|
|
|
|
|
|
struct FontDesc {
|
2017-07-27 14:45:29 +00:00
|
|
|
char const * const fGenericName;
|
|
|
|
NamedFontStyle const fNamedStyle;
|
|
|
|
char const * const fFontName;
|
|
|
|
char const * const fFile;
|
|
|
|
// fFontIndex is mutable and will be set later.
|
2014-07-31 12:58:44 +00:00
|
|
|
int fFontIndex;
|
|
|
|
} gFonts[] = {
|
2017-07-17 19:32:25 +00:00
|
|
|
{"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},
|
2014-07-31 12:58:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const int gFontsCount = (int) SK_ARRAY_COUNT(gFonts);
|
|
|
|
|
|
|
|
const char gHeader[] =
|
|
|
|
"/*\n"
|
2015-06-10 16:31:09 +00:00
|
|
|
" * Copyright 2015 Google Inc.\n"
|
2014-07-31 12:58:44 +00:00
|
|
|
" *\n"
|
|
|
|
" * Use of this source code is governed by a BSD-style license that can be\n"
|
|
|
|
" * found in the LICENSE file.\n"
|
|
|
|
" */\n"
|
|
|
|
"\n"
|
|
|
|
"// Auto-generated by ";
|
|
|
|
|
2017-07-17 19:32:25 +00:00
|
|
|
} // namespace
|
|
|
|
|
2015-06-10 16:31:09 +00:00
|
|
|
static FILE* font_header(const char* family) {
|
2015-01-29 17:59:53 +00:00
|
|
|
SkString outPath(SkOSPath::Join(".", "tools"));
|
2018-02-20 22:06:07 +00:00
|
|
|
outPath = SkOSPath::Join(outPath.c_str(), "fonts");
|
2015-06-10 16:31:09 +00:00
|
|
|
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);
|
2017-07-17 19:32:25 +00:00
|
|
|
outPath.append(".inc");
|
2014-07-31 12:58:44 +00:00
|
|
|
FILE* out = fopen(outPath.c_str(), "w");
|
2015-01-29 17:59:53 +00:00
|
|
|
fprintf(out, "%s%s\n\n", gHeader, SkOSPath::Basename(__FILE__).c_str());
|
2014-07-31 12:58:44 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2014-06-23 18:25:00 +00:00
|
|
|
enum {
|
2014-07-31 12:58:44 +00:00
|
|
|
kMaxLineLength = 80,
|
2014-06-23 18:25:00 +00:00
|
|
|
};
|
|
|
|
|
2014-07-31 12:58:44 +00:00
|
|
|
static ptrdiff_t last_line_length(const SkString& str) {
|
|
|
|
const char* first = str.c_str();
|
|
|
|
const char* last = first + str.size();
|
|
|
|
const char* ptr = last;
|
|
|
|
while (ptr > first && *--ptr != '\n')
|
|
|
|
;
|
|
|
|
return last - ptr - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void output_fixed(SkScalar num, int emSize, SkString* out) {
|
|
|
|
int hex = (int) (num * 65536 / emSize);
|
|
|
|
out->appendf("0x%08x,", hex);
|
|
|
|
*out += (int) last_line_length(*out) >= kMaxLineLength ? '\n' : ' ';
|
2014-06-23 18:25:00 +00:00
|
|
|
}
|
|
|
|
|
2014-07-31 12:58:44 +00:00
|
|
|
static void output_scalar(SkScalar num, int emSize, SkString* out) {
|
|
|
|
num /= emSize;
|
2014-06-23 18:25:00 +00:00
|
|
|
if (num == (int) num) {
|
2014-07-31 12:58:44 +00:00
|
|
|
out->appendS32((int) num);
|
2014-06-23 18:25:00 +00:00
|
|
|
} else {
|
|
|
|
SkString str;
|
|
|
|
str.printf("%1.6g", num);
|
|
|
|
int width = (int) str.size();
|
|
|
|
const char* cStr = str.c_str();
|
|
|
|
while (cStr[width - 1] == '0') {
|
|
|
|
--width;
|
|
|
|
}
|
2014-07-31 12:58:44 +00:00
|
|
|
str.remove(width, str.size() - width);
|
|
|
|
out->appendf("%sf", str.c_str());
|
2014-06-23 18:25:00 +00:00
|
|
|
}
|
2014-07-31 12:58:44 +00:00
|
|
|
*out += ',';
|
|
|
|
*out += (int) last_line_length(*out) >= kMaxLineLength ? '\n' : ' ';
|
2014-06-23 18:25:00 +00:00
|
|
|
}
|
|
|
|
|
2014-07-31 12:58:44 +00:00
|
|
|
static int output_points(const SkPoint* pts, int emSize, int count, SkString* ptsOut) {
|
2014-06-23 18:25:00 +00:00
|
|
|
for (int index = 0; index < count; ++index) {
|
2014-07-31 12:58:44 +00:00
|
|
|
// SkASSERT(floor(pts[index].fX) == pts[index].fX);
|
|
|
|
output_scalar(pts[index].fX, emSize, ptsOut);
|
|
|
|
// SkASSERT(floor(pts[index].fY) == pts[index].fY);
|
|
|
|
output_scalar(pts[index].fY, emSize, ptsOut);
|
2014-06-23 18:25:00 +00:00
|
|
|
}
|
2014-07-31 12:58:44 +00:00
|
|
|
return count;
|
2014-06-23 18:25:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-10 16:31:09 +00:00
|
|
|
static void output_path_data(const SkPaint& paint,
|
2014-07-31 12:58:44 +00:00
|
|
|
int emSize, SkString* ptsOut, SkTDArray<SkPath::Verb>* verbs,
|
|
|
|
SkTDArray<unsigned>* charCodes, SkTDArray<SkScalar>* widths) {
|
2018-07-25 20:52:48 +00:00
|
|
|
for (SkUnichar index = 0x00; index < 0x7f; ++index) {
|
|
|
|
uint16_t utf16[2];
|
|
|
|
size_t utf16Bytes = sizeof(uint16_t) * SkUTF::ToUTF16(index, utf16);
|
2014-06-23 18:25:00 +00:00
|
|
|
SkPath path;
|
2018-07-25 20:52:48 +00:00
|
|
|
SkASSERT(paint.getTextEncoding() == SkPaint::kUTF16_TextEncoding);
|
|
|
|
paint.getTextPath(utf16, utf16Bytes, 0, 0, &path);
|
2014-06-23 18:25:00 +00:00
|
|
|
SkPath::RawIter iter(path);
|
2014-07-31 12:58:44 +00:00
|
|
|
SkPath::Verb verb;
|
2014-06-23 18:25:00 +00:00
|
|
|
SkPoint pts[4];
|
|
|
|
while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
|
2014-07-31 12:58:44 +00:00
|
|
|
*verbs->append() = verb;
|
2014-06-23 18:25:00 +00:00
|
|
|
switch (verb) {
|
|
|
|
case SkPath::kMove_Verb:
|
2014-07-31 12:58:44 +00:00
|
|
|
output_points(&pts[0], emSize, 1, ptsOut);
|
|
|
|
break;
|
2014-06-23 18:25:00 +00:00
|
|
|
case SkPath::kLine_Verb:
|
2014-07-31 12:58:44 +00:00
|
|
|
output_points(&pts[1], emSize, 1, ptsOut);
|
2014-06-23 18:25:00 +00:00
|
|
|
break;
|
|
|
|
case SkPath::kQuad_Verb:
|
2014-07-31 12:58:44 +00:00
|
|
|
output_points(&pts[1], emSize, 2, ptsOut);
|
2014-06-23 18:25:00 +00:00
|
|
|
break;
|
|
|
|
case SkPath::kCubic_Verb:
|
2014-07-31 12:58:44 +00:00
|
|
|
output_points(&pts[1], emSize, 3, ptsOut);
|
2014-06-23 18:25:00 +00:00
|
|
|
break;
|
|
|
|
case SkPath::kClose_Verb:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SkDEBUGFAIL("bad verb");
|
2014-07-31 12:58:44 +00:00
|
|
|
SkASSERT(0);
|
2014-06-23 18:25:00 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-31 12:58:44 +00:00
|
|
|
*verbs->append() = SkPath::kDone_Verb;
|
|
|
|
*charCodes->append() = index;
|
2014-06-23 18:25:00 +00:00
|
|
|
SkScalar width;
|
2018-07-25 20:52:48 +00:00
|
|
|
SkDEBUGCODE(int charCount =) paint.getTextWidths(utf16, utf16Bytes, &width);
|
2014-06-23 18:25:00 +00:00
|
|
|
SkASSERT(charCount == 1);
|
2015-06-11 16:40:44 +00:00
|
|
|
// SkASSERT(floor(width) == width); // not true for Hiragino Maru Gothic Pro
|
2014-07-31 12:58:44 +00:00
|
|
|
*widths->append() = width;
|
2018-07-25 20:52:48 +00:00
|
|
|
if (0 == index) {
|
|
|
|
index = 0x1f; // skip the rest of the control codes
|
2015-06-11 16:40:44 +00:00
|
|
|
}
|
2014-07-31 12:58:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int offset_str_len(unsigned num) {
|
|
|
|
if (num == (unsigned) -1) {
|
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
unsigned result = 1;
|
|
|
|
unsigned ref = 10;
|
|
|
|
while (ref <= num) {
|
|
|
|
++result;
|
|
|
|
ref *= 10;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkString strip_spaces(const SkString& str) {
|
|
|
|
SkString result;
|
|
|
|
int count = (int) str.size();
|
|
|
|
for (int index = 0; index < count; ++index) {
|
|
|
|
char c = str[index];
|
|
|
|
if (c != ' ' && c != '-') {
|
|
|
|
result += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkString strip_final(const SkString& str) {
|
|
|
|
SkString result(str);
|
|
|
|
if (result.endsWith("\n")) {
|
|
|
|
result.remove(result.size() - 1, 1);
|
|
|
|
}
|
|
|
|
if (result.endsWith(" ")) {
|
|
|
|
result.remove(result.size() - 1, 1);
|
|
|
|
}
|
|
|
|
if (result.endsWith(",")) {
|
|
|
|
result.remove(result.size() - 1, 1);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-07-17 19:32:25 +00:00
|
|
|
static void output_font(sk_sp<SkTypeface> face, const char* name, NamedFontStyle style, FILE* out) {
|
2014-07-31 12:58:44 +00:00
|
|
|
int emSize = face->getUnitsPerEm() * 2;
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setTextAlign(SkPaint::kLeft_Align);
|
|
|
|
paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
|
|
|
|
paint.setTextSize(emSize);
|
2017-07-17 19:32:25 +00:00
|
|
|
paint.setTypeface(std::move(face));
|
2014-07-31 12:58:44 +00:00
|
|
|
SkTDArray<SkPath::Verb> verbs;
|
|
|
|
SkTDArray<unsigned> charCodes;
|
|
|
|
SkTDArray<SkScalar> widths;
|
|
|
|
SkString ptsOut;
|
2015-06-10 16:31:09 +00:00
|
|
|
output_path_data(paint, emSize, &ptsOut, &verbs, &charCodes, &widths);
|
2014-07-31 12:58:44 +00:00
|
|
|
SkString fontnameStr(name);
|
|
|
|
SkString strippedStr = strip_spaces(fontnameStr);
|
2017-07-17 19:32:25 +00:00
|
|
|
strippedStr.appendf("%s", style.fName);
|
2014-07-31 12:58:44 +00:00
|
|
|
const char* fontname = strippedStr.c_str();
|
|
|
|
fprintf(out, "const SkScalar %sPoints[] = {\n", fontname);
|
|
|
|
ptsOut = strip_final(ptsOut);
|
|
|
|
fprintf(out, "%s", ptsOut.c_str());
|
|
|
|
fprintf(out, "\n};\n\n");
|
|
|
|
fprintf(out, "const unsigned char %sVerbs[] = {\n", fontname);
|
|
|
|
int verbCount = verbs.count();
|
|
|
|
int outChCount = 0;
|
|
|
|
for (int index = 0; index < verbCount;) {
|
|
|
|
SkPath::Verb verb = verbs[index];
|
|
|
|
SkASSERT(verb >= SkPath::kMove_Verb && verb <= SkPath::kDone_Verb);
|
|
|
|
SkASSERT((unsigned) verb == (unsigned char) verb);
|
|
|
|
fprintf(out, "%u", verb);
|
|
|
|
if (++index < verbCount) {
|
|
|
|
outChCount += 3;
|
|
|
|
fprintf(out, "%c", ',');
|
|
|
|
if (outChCount >= kMaxLineLength) {
|
|
|
|
outChCount = 0;
|
|
|
|
fprintf(out, "%c", '\n');
|
|
|
|
} else {
|
|
|
|
fprintf(out, "%c", ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(out, "\n};\n\n");
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-06-11 16:40:44 +00:00
|
|
|
// all fonts are now 0x00, 0x20 - 0xFE
|
|
|
|
// don't need to generate or output character codes?
|
2014-07-31 12:58:44 +00:00
|
|
|
fprintf(out, "const unsigned %sCharCodes[] = {\n", fontname);
|
|
|
|
int offsetCount = charCodes.count();
|
|
|
|
for (int index = 0; index < offsetCount;) {
|
|
|
|
unsigned offset = charCodes[index];
|
|
|
|
fprintf(out, "%u", offset);
|
|
|
|
if (++index < offsetCount) {
|
|
|
|
outChCount += offset_str_len(offset) + 2;
|
|
|
|
fprintf(out, "%c", ',');
|
|
|
|
if (outChCount >= kMaxLineLength) {
|
|
|
|
outChCount = 0;
|
|
|
|
fprintf(out, "%c", '\n');
|
|
|
|
} else {
|
|
|
|
fprintf(out, "%c", ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(out, "\n};\n\n");
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-07-31 12:58:44 +00:00
|
|
|
SkString widthsStr;
|
|
|
|
fprintf(out, "const SkFixed %sWidths[] = {\n", fontname);
|
|
|
|
for (int index = 0; index < offsetCount; ++index) {
|
|
|
|
output_fixed(widths[index], emSize, &widthsStr);
|
2014-06-23 18:25:00 +00:00
|
|
|
}
|
2014-07-31 12:58:44 +00:00
|
|
|
widthsStr = strip_final(widthsStr);
|
|
|
|
fprintf(out, "%s\n};\n\n", widthsStr.c_str());
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-07-31 12:58:44 +00:00
|
|
|
fprintf(out, "const int %sCharCodesCount = (int) SK_ARRAY_COUNT(%sCharCodes);\n\n",
|
|
|
|
fontname, fontname);
|
|
|
|
|
2014-06-23 18:25:00 +00:00
|
|
|
SkPaint::FontMetrics metrics;
|
|
|
|
paint.getFontMetrics(&metrics);
|
2014-07-31 12:58:44 +00:00
|
|
|
fprintf(out, "const SkPaint::FontMetrics %sMetrics = {\n", fontname);
|
|
|
|
SkString metricsStr;
|
|
|
|
metricsStr.printf("0x%08x, ", metrics.fFlags);
|
|
|
|
output_scalar(metrics.fTop, emSize, &metricsStr);
|
|
|
|
output_scalar(metrics.fAscent, emSize, &metricsStr);
|
|
|
|
output_scalar(metrics.fDescent, emSize, &metricsStr);
|
|
|
|
output_scalar(metrics.fBottom, emSize, &metricsStr);
|
|
|
|
output_scalar(metrics.fLeading, emSize, &metricsStr);
|
|
|
|
output_scalar(metrics.fAvgCharWidth, emSize, &metricsStr);
|
|
|
|
output_scalar(metrics.fMaxCharWidth, emSize, &metricsStr);
|
|
|
|
output_scalar(metrics.fXMin, emSize, &metricsStr);
|
|
|
|
output_scalar(metrics.fXMax, emSize, &metricsStr);
|
|
|
|
output_scalar(metrics.fXHeight, emSize, &metricsStr);
|
|
|
|
output_scalar(metrics.fCapHeight, emSize, &metricsStr);
|
|
|
|
output_scalar(metrics.fUnderlineThickness, emSize, &metricsStr);
|
|
|
|
output_scalar(metrics.fUnderlinePosition, emSize, &metricsStr);
|
2017-07-17 19:32:25 +00:00
|
|
|
output_scalar(metrics.fStrikeoutThickness, emSize, &metricsStr);
|
|
|
|
output_scalar(metrics.fStrikeoutPosition, emSize, &metricsStr);
|
2014-07-31 12:58:44 +00:00
|
|
|
metricsStr = strip_final(metricsStr);
|
|
|
|
fprintf(out, "%s\n};\n\n", metricsStr.c_str());
|
2014-06-23 18:25:00 +00:00
|
|
|
}
|
|
|
|
|
2014-07-31 12:58:44 +00:00
|
|
|
struct FontWritten {
|
2017-07-17 19:32:25 +00:00
|
|
|
const char* fFontName;
|
|
|
|
NamedFontStyle fNamedStyle;
|
2014-07-31 12:58:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static SkTDArray<FontWritten> gWritten;
|
|
|
|
|
|
|
|
static int written_index(const FontDesc& fontDesc) {
|
|
|
|
for (int index = 0; index < gWritten.count(); ++index) {
|
|
|
|
const FontWritten& writ = gWritten[index];
|
2017-07-17 19:32:25 +00:00
|
|
|
if (!strcmp(fontDesc.fFontName, writ.fFontName) &&
|
|
|
|
fontDesc.fNamedStyle.fStyle == writ.fNamedStyle.fStyle)
|
|
|
|
{
|
2014-07-31 12:58:44 +00:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
2014-06-23 18:25:00 +00:00
|
|
|
}
|
2014-07-31 12:58:44 +00:00
|
|
|
|
2017-07-17 19:32:25 +00:00
|
|
|
static void generate_fonts(const char* basepath) {
|
2015-08-27 14:41:13 +00:00
|
|
|
FILE* out = nullptr;
|
2014-07-31 12:58:44 +00:00
|
|
|
for (int index = 0; index < gFontsCount; ++index) {
|
|
|
|
FontDesc& fontDesc = gFonts[index];
|
2015-06-10 16:31:09 +00:00
|
|
|
if ((index & 3) == 0) {
|
2017-07-17 19:32:25 +00:00
|
|
|
out = font_header(fontDesc.fGenericName);
|
2015-06-10 16:31:09 +00:00
|
|
|
}
|
2014-07-31 12:58:44 +00:00
|
|
|
int fontIndex = written_index(fontDesc);
|
|
|
|
if (fontIndex >= 0) {
|
|
|
|
fontDesc.fFontIndex = fontIndex;
|
|
|
|
continue;
|
|
|
|
}
|
2017-07-17 19:32:25 +00:00
|
|
|
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);
|
2014-07-31 12:58:44 +00:00
|
|
|
fontDesc.fFontIndex = gWritten.count();
|
|
|
|
FontWritten* writ = gWritten.append();
|
2017-07-17 19:32:25 +00:00
|
|
|
writ->fFontName = fontDesc.fFontName;
|
|
|
|
writ->fNamedStyle = fontDesc.fNamedStyle;
|
2015-06-10 16:31:09 +00:00
|
|
|
if ((index & 3) == 3) {
|
|
|
|
fclose(out);
|
|
|
|
}
|
2014-07-31 12:58:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 19:32:25 +00:00
|
|
|
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 "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-10 16:31:09 +00:00
|
|
|
static void generate_index(const char* defaultName) {
|
|
|
|
FILE* out = font_header("index");
|
|
|
|
fprintf(out, "static SkTestFontData gTestFonts[] = {\n");
|
2017-07-17 19:32:25 +00:00
|
|
|
for (const FontWritten& writ : gWritten) {
|
|
|
|
const char* name = writ.fFontName;
|
2014-07-31 12:58:44 +00:00
|
|
|
SkString strippedStr = strip_spaces(SkString(name));
|
2017-07-17 19:32:25 +00:00
|
|
|
strippedStr.appendf("%s", writ.fNamedStyle.fName);
|
2014-07-31 12:58:44 +00:00
|
|
|
const char* strip = strippedStr.c_str();
|
|
|
|
fprintf(out,
|
|
|
|
" { %sPoints, %sVerbs, %sCharCodes,\n"
|
|
|
|
" %sCharCodesCount, %sWidths,\n"
|
2018-07-10 23:40:15 +00:00
|
|
|
" %sMetrics, \"Toy %s\", SkFontStyle(%d,%d,%s)\n"
|
2014-07-31 12:58:44 +00:00
|
|
|
" },\n",
|
2017-07-17 19:32:25 +00:00
|
|
|
strip, strip, strip, strip, strip, strip, name,
|
|
|
|
writ.fNamedStyle.fStyle.weight(), writ.fNamedStyle.fStyle.width(),
|
|
|
|
slant_to_string(writ.fNamedStyle.fStyle.slant()));
|
2014-07-31 12:58:44 +00:00
|
|
|
}
|
|
|
|
fprintf(out, "};\n\n");
|
|
|
|
fprintf(out, "const int gTestFontsCount = (int) SK_ARRAY_COUNT(gTestFonts);\n\n");
|
|
|
|
fprintf(out,
|
|
|
|
"struct SubFont {\n"
|
2018-07-10 23:40:15 +00:00
|
|
|
" const char* fFamilyName;\n"
|
|
|
|
" const char* fStyleName;\n"
|
2017-07-17 19:32:25 +00:00
|
|
|
" SkFontStyle fStyle;\n"
|
2014-07-31 12:58:44 +00:00
|
|
|
" SkTestFontData& fFont;\n"
|
|
|
|
" const char* fFile;\n"
|
|
|
|
"};\n\n"
|
|
|
|
"const SubFont gSubFonts[] = {\n");
|
|
|
|
int defaultIndex = -1;
|
|
|
|
for (int subIndex = 0; subIndex < gFontsCount; subIndex++) {
|
|
|
|
const FontDesc& desc = gFonts[subIndex];
|
2017-07-17 19:32:25 +00:00
|
|
|
if (defaultIndex < 0 && !strcmp(defaultName, desc.fGenericName)) {
|
2014-07-31 12:58:44 +00:00
|
|
|
defaultIndex = subIndex;
|
|
|
|
}
|
|
|
|
fprintf(out,
|
2018-07-10 23:40:15 +00:00
|
|
|
" { \"%s\", \"%s\", SkFontStyle(%d,%d,%s), gTestFonts[%d], \"%s\" },\n",
|
|
|
|
desc.fGenericName, desc.fNamedStyle.fName,
|
2017-07-17 19:32:25 +00:00
|
|
|
desc.fNamedStyle.fStyle.weight(), desc.fNamedStyle.fStyle.width(),
|
|
|
|
slant_to_string(desc.fNamedStyle.fStyle.slant()), desc.fFontIndex, desc.fFile);
|
2015-06-10 16:31:09 +00:00
|
|
|
}
|
|
|
|
for (int subIndex = 0; subIndex < gFontsCount; subIndex++) {
|
|
|
|
const FontDesc& desc = gFonts[subIndex];
|
|
|
|
fprintf(out,
|
2018-07-10 23:40:15 +00:00
|
|
|
" { \"Toy %s\", \"%s\", SkFontStyle(%d,%d,%s), gTestFonts[%d], \"%s\" },\n",
|
|
|
|
desc.fFontName, desc.fNamedStyle.fName,
|
2017-07-17 19:32:25 +00:00
|
|
|
desc.fNamedStyle.fStyle.weight(), desc.fNamedStyle.fStyle.width(),
|
|
|
|
slant_to_string(desc.fNamedStyle.fStyle.slant()), desc.fFontIndex, desc.fFile);
|
2014-07-31 12:58:44 +00:00
|
|
|
}
|
|
|
|
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);
|
2015-06-10 16:31:09 +00:00
|
|
|
fclose(out);
|
2014-07-31 12:58:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int , char * const []) {
|
2018-07-25 20:52:48 +00:00
|
|
|
#ifdef SK_BUILD_FOR_UNIX
|
|
|
|
generate_fonts("/usr/share/fonts/truetype/liberation/");
|
|
|
|
#else
|
2018-02-20 14:57:58 +00:00
|
|
|
generate_fonts("/Library/Fonts/"); // or /usr/share/fonts/truetype/ttf-liberation/
|
2018-07-25 20:52:48 +00:00
|
|
|
#endif
|
2015-06-10 16:31:09 +00:00
|
|
|
generate_index(DEFAULT_FONT_NAME);
|
2014-07-31 12:58:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|