caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 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 | |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 8 | // running create_test_font generates ./tools/test_font_index.inc |
| 9 | // and ./tools/test_font_<generic name>.inc which are read by ./tools/sk_tool_utils_font.cpp |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 10 | |
| 11 | #include "Resources.h" |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame^] | 12 | #include "SkFontStyle.h" |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 13 | #include "SkOSFile.h" |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 14 | #include "SkOSPath.h" |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 15 | #include "SkPaint.h" |
| 16 | #include "SkPath.h" |
| 17 | #include "SkStream.h" |
| 18 | #include "SkTArray.h" |
| 19 | #include "SkTSort.h" |
| 20 | #include "SkTypeface.h" |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 21 | #include "SkUtils.h" |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 24 | #define DEFAULT_FONT_NAME "sans-serif" |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 25 | |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 26 | namespace { |
| 27 | |
| 28 | struct NamedFontStyle { |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 29 | const char* fName; |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 30 | SkFontStyle fStyle; |
| 31 | }; |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame^] | 32 | constexpr NamedFontStyle normal = {"Normal", SkFontStyle::Normal() }; |
| 33 | constexpr NamedFontStyle bold = {"Bold", SkFontStyle::Bold() }; |
| 34 | constexpr NamedFontStyle italic = {"Italic", SkFontStyle::Italic() }; |
| 35 | constexpr NamedFontStyle bolditalic = {"BoldItalic", SkFontStyle::BoldItalic()}; |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 36 | |
| 37 | struct FontDesc { |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame^] | 38 | char const * const fGenericName; |
| 39 | NamedFontStyle const fNamedStyle; |
| 40 | char const * const fFontName; |
| 41 | char const * const fFile; |
| 42 | // fFontIndex is mutable and will be set later. |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 43 | int fFontIndex; |
| 44 | } gFonts[] = { |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 45 | {"monospace", normal, "Liberation Mono", "LiberationMono-Regular.ttf", -1}, |
| 46 | {"monospace", bold, "Liberation Mono", "LiberationMono-Bold.ttf", -1}, |
| 47 | {"monospace", italic, "Liberation Mono", "LiberationMono-Italic.ttf", -1}, |
| 48 | {"monospace", bolditalic, "Liberation Mono", "LiberationMono-BoldItalic.ttf", -1}, |
| 49 | {"sans-serif", normal, "Liberation Sans", "LiberationSans-Regular.ttf", -1}, |
| 50 | {"sans-serif", bold, "Liberation Sans", "LiberationSans-Bold.ttf", -1}, |
| 51 | {"sans-serif", italic, "Liberation Sans", "LiberationSans-Italic.ttf", -1}, |
| 52 | {"sans-serif", bolditalic, "Liberation Sans", "LiberationSans-BoldItalic.ttf", -1}, |
| 53 | {"serif", normal, "Liberation Serif", "LiberationSerif-Regular.ttf", -1}, |
| 54 | {"serif", bold, "Liberation Serif", "LiberationSerif-Bold.ttf", -1}, |
| 55 | {"serif", italic, "Liberation Serif", "LiberationSerif-Italic.ttf", -1}, |
| 56 | {"serif", bolditalic, "Liberation Serif", "LiberationSerif-BoldItalic.ttf", -1}, |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 59 | const int gFontsCount = (int) SK_ARRAY_COUNT(gFonts); |
| 60 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 61 | const char gHeader[] = |
| 62 | "/*\n" |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 63 | " * Copyright 2015 Google Inc.\n" |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 64 | " *\n" |
| 65 | " * Use of this source code is governed by a BSD-style license that can be\n" |
| 66 | " * found in the LICENSE file.\n" |
| 67 | " */\n" |
| 68 | "\n" |
| 69 | "// Auto-generated by "; |
| 70 | |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 71 | } // namespace |
| 72 | |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 73 | static FILE* font_header(const char* family) { |
caryclark | c7a84fa | 2015-01-29 09:59:53 -0800 | [diff] [blame] | 74 | SkString outPath(SkOSPath::Join(".", "tools")); |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 75 | outPath = SkOSPath::Join(outPath.c_str(), "test_font_"); |
| 76 | SkString fam(family); |
| 77 | do { |
| 78 | int dashIndex = fam.find("-"); |
| 79 | if (dashIndex < 0) { |
| 80 | break; |
| 81 | } |
| 82 | fam.writable_str()[dashIndex] = '_'; |
| 83 | } while (true); |
| 84 | outPath.append(fam); |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 85 | outPath.append(".inc"); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 86 | FILE* out = fopen(outPath.c_str(), "w"); |
caryclark | c7a84fa | 2015-01-29 09:59:53 -0800 | [diff] [blame] | 87 | fprintf(out, "%s%s\n\n", gHeader, SkOSPath::Basename(__FILE__).c_str()); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 88 | return out; |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 91 | enum { |
| 92 | kMaxLineLength = 80, |
| 93 | }; |
| 94 | |
| 95 | static ptrdiff_t last_line_length(const SkString& str) { |
| 96 | const char* first = str.c_str(); |
| 97 | const char* last = first + str.size(); |
| 98 | const char* ptr = last; |
| 99 | while (ptr > first && *--ptr != '\n') |
| 100 | ; |
| 101 | return last - ptr - 1; |
| 102 | } |
| 103 | |
| 104 | static void output_fixed(SkScalar num, int emSize, SkString* out) { |
| 105 | int hex = (int) (num * 65536 / emSize); |
| 106 | out->appendf("0x%08x,", hex); |
| 107 | *out += (int) last_line_length(*out) >= kMaxLineLength ? '\n' : ' '; |
| 108 | } |
| 109 | |
| 110 | static void output_scalar(SkScalar num, int emSize, SkString* out) { |
| 111 | num /= emSize; |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 112 | if (num == (int) num) { |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 113 | out->appendS32((int) num); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 114 | } else { |
| 115 | SkString str; |
| 116 | str.printf("%1.6g", num); |
| 117 | int width = (int) str.size(); |
| 118 | const char* cStr = str.c_str(); |
| 119 | while (cStr[width - 1] == '0') { |
| 120 | --width; |
| 121 | } |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 122 | str.remove(width, str.size() - width); |
| 123 | out->appendf("%sf", str.c_str()); |
| 124 | } |
| 125 | *out += ','; |
| 126 | *out += (int) last_line_length(*out) >= kMaxLineLength ? '\n' : ' '; |
| 127 | } |
| 128 | |
| 129 | static int output_points(const SkPoint* pts, int emSize, int count, SkString* ptsOut) { |
| 130 | for (int index = 0; index < count; ++index) { |
| 131 | // SkASSERT(floor(pts[index].fX) == pts[index].fX); |
| 132 | output_scalar(pts[index].fX, emSize, ptsOut); |
| 133 | // SkASSERT(floor(pts[index].fY) == pts[index].fY); |
| 134 | output_scalar(pts[index].fY, emSize, ptsOut); |
| 135 | } |
| 136 | return count; |
| 137 | } |
| 138 | |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 139 | static void output_path_data(const SkPaint& paint, |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 140 | int emSize, SkString* ptsOut, SkTDArray<SkPath::Verb>* verbs, |
| 141 | SkTDArray<unsigned>* charCodes, SkTDArray<SkScalar>* widths) { |
caryclark | 6d0d6cb | 2015-06-11 09:40:44 -0700 | [diff] [blame] | 142 | for (int ch = 0x00; ch < 0x7f; ++ch) { |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 143 | char str[1]; |
| 144 | str[0] = ch; |
| 145 | const char* used = str; |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 146 | SkUnichar index = SkUTF8_NextUnichar(&used); |
| 147 | SkPath path; |
| 148 | paint.getTextPath((const void*) &index, 2, 0, 0, &path); |
| 149 | SkPath::RawIter iter(path); |
| 150 | SkPath::Verb verb; |
| 151 | SkPoint pts[4]; |
| 152 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 153 | *verbs->append() = verb; |
| 154 | switch (verb) { |
| 155 | case SkPath::kMove_Verb: |
| 156 | output_points(&pts[0], emSize, 1, ptsOut); |
| 157 | break; |
| 158 | case SkPath::kLine_Verb: |
| 159 | output_points(&pts[1], emSize, 1, ptsOut); |
| 160 | break; |
| 161 | case SkPath::kQuad_Verb: |
| 162 | output_points(&pts[1], emSize, 2, ptsOut); |
| 163 | break; |
| 164 | case SkPath::kCubic_Verb: |
| 165 | output_points(&pts[1], emSize, 3, ptsOut); |
| 166 | break; |
| 167 | case SkPath::kClose_Verb: |
| 168 | break; |
| 169 | default: |
| 170 | SkDEBUGFAIL("bad verb"); |
| 171 | SkASSERT(0); |
| 172 | } |
| 173 | } |
| 174 | *verbs->append() = SkPath::kDone_Verb; |
| 175 | *charCodes->append() = index; |
| 176 | SkScalar width; |
| 177 | SkDEBUGCODE(int charCount =) paint.getTextWidths((const void*) &index, 2, &width); |
| 178 | SkASSERT(charCount == 1); |
caryclark | 6d0d6cb | 2015-06-11 09:40:44 -0700 | [diff] [blame] | 179 | // SkASSERT(floor(width) == width); // not true for Hiragino Maru Gothic Pro |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 180 | *widths->append() = width; |
caryclark | 6d0d6cb | 2015-06-11 09:40:44 -0700 | [diff] [blame] | 181 | if (!ch) { |
| 182 | ch = 0x1f; // skip the rest of the control codes |
| 183 | } |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 187 | static int offset_str_len(unsigned num) { |
| 188 | if (num == (unsigned) -1) { |
| 189 | return 10; |
| 190 | } |
| 191 | unsigned result = 1; |
| 192 | unsigned ref = 10; |
| 193 | while (ref <= num) { |
| 194 | ++result; |
| 195 | ref *= 10; |
| 196 | } |
| 197 | return result; |
| 198 | } |
| 199 | |
| 200 | static SkString strip_spaces(const SkString& str) { |
| 201 | SkString result; |
| 202 | int count = (int) str.size(); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 203 | for (int index = 0; index < count; ++index) { |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 204 | char c = str[index]; |
| 205 | if (c != ' ' && c != '-') { |
| 206 | result += c; |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 209 | return result; |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 212 | static SkString strip_final(const SkString& str) { |
| 213 | SkString result(str); |
| 214 | if (result.endsWith("\n")) { |
| 215 | result.remove(result.size() - 1, 1); |
| 216 | } |
| 217 | if (result.endsWith(" ")) { |
| 218 | result.remove(result.size() - 1, 1); |
| 219 | } |
| 220 | if (result.endsWith(",")) { |
| 221 | result.remove(result.size() - 1, 1); |
| 222 | } |
| 223 | return result; |
| 224 | } |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 225 | |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 226 | static void output_font(sk_sp<SkTypeface> face, const char* name, NamedFontStyle style, FILE* out) { |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 227 | int emSize = face->getUnitsPerEm() * 2; |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 228 | SkPaint paint; |
| 229 | paint.setAntiAlias(true); |
| 230 | paint.setTextAlign(SkPaint::kLeft_Align); |
| 231 | paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 232 | paint.setTextSize(emSize); |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 233 | paint.setTypeface(std::move(face)); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 234 | SkTDArray<SkPath::Verb> verbs; |
| 235 | SkTDArray<unsigned> charCodes; |
| 236 | SkTDArray<SkScalar> widths; |
| 237 | SkString ptsOut; |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 238 | output_path_data(paint, emSize, &ptsOut, &verbs, &charCodes, &widths); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 239 | SkString fontnameStr(name); |
| 240 | SkString strippedStr = strip_spaces(fontnameStr); |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 241 | strippedStr.appendf("%s", style.fName); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 242 | const char* fontname = strippedStr.c_str(); |
| 243 | fprintf(out, "const SkScalar %sPoints[] = {\n", fontname); |
| 244 | ptsOut = strip_final(ptsOut); |
| 245 | fprintf(out, "%s", ptsOut.c_str()); |
| 246 | fprintf(out, "\n};\n\n"); |
| 247 | fprintf(out, "const unsigned char %sVerbs[] = {\n", fontname); |
| 248 | int verbCount = verbs.count(); |
| 249 | int outChCount = 0; |
| 250 | for (int index = 0; index < verbCount;) { |
| 251 | SkPath::Verb verb = verbs[index]; |
| 252 | SkASSERT(verb >= SkPath::kMove_Verb && verb <= SkPath::kDone_Verb); |
| 253 | SkASSERT((unsigned) verb == (unsigned char) verb); |
| 254 | fprintf(out, "%u", verb); |
| 255 | if (++index < verbCount) { |
| 256 | outChCount += 3; |
| 257 | fprintf(out, "%c", ','); |
| 258 | if (outChCount >= kMaxLineLength) { |
| 259 | outChCount = 0; |
| 260 | fprintf(out, "%c", '\n'); |
| 261 | } else { |
| 262 | fprintf(out, "%c", ' '); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 265 | } |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 266 | fprintf(out, "\n};\n\n"); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 267 | |
caryclark | 6d0d6cb | 2015-06-11 09:40:44 -0700 | [diff] [blame] | 268 | // all fonts are now 0x00, 0x20 - 0xFE |
| 269 | // don't need to generate or output character codes? |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 270 | fprintf(out, "const unsigned %sCharCodes[] = {\n", fontname); |
| 271 | int offsetCount = charCodes.count(); |
| 272 | for (int index = 0; index < offsetCount;) { |
| 273 | unsigned offset = charCodes[index]; |
| 274 | fprintf(out, "%u", offset); |
| 275 | if (++index < offsetCount) { |
| 276 | outChCount += offset_str_len(offset) + 2; |
| 277 | fprintf(out, "%c", ','); |
| 278 | if (outChCount >= kMaxLineLength) { |
| 279 | outChCount = 0; |
| 280 | fprintf(out, "%c", '\n'); |
| 281 | } else { |
| 282 | fprintf(out, "%c", ' '); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | fprintf(out, "\n};\n\n"); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 287 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 288 | SkString widthsStr; |
| 289 | fprintf(out, "const SkFixed %sWidths[] = {\n", fontname); |
| 290 | for (int index = 0; index < offsetCount; ++index) { |
| 291 | output_fixed(widths[index], emSize, &widthsStr); |
| 292 | } |
| 293 | widthsStr = strip_final(widthsStr); |
| 294 | fprintf(out, "%s\n};\n\n", widthsStr.c_str()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 295 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 296 | fprintf(out, "const int %sCharCodesCount = (int) SK_ARRAY_COUNT(%sCharCodes);\n\n", |
| 297 | fontname, fontname); |
| 298 | |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 299 | SkPaint::FontMetrics metrics; |
| 300 | paint.getFontMetrics(&metrics); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 301 | fprintf(out, "const SkPaint::FontMetrics %sMetrics = {\n", fontname); |
| 302 | SkString metricsStr; |
| 303 | metricsStr.printf("0x%08x, ", metrics.fFlags); |
| 304 | output_scalar(metrics.fTop, emSize, &metricsStr); |
| 305 | output_scalar(metrics.fAscent, emSize, &metricsStr); |
| 306 | output_scalar(metrics.fDescent, emSize, &metricsStr); |
| 307 | output_scalar(metrics.fBottom, emSize, &metricsStr); |
| 308 | output_scalar(metrics.fLeading, emSize, &metricsStr); |
| 309 | output_scalar(metrics.fAvgCharWidth, emSize, &metricsStr); |
| 310 | output_scalar(metrics.fMaxCharWidth, emSize, &metricsStr); |
| 311 | output_scalar(metrics.fXMin, emSize, &metricsStr); |
| 312 | output_scalar(metrics.fXMax, emSize, &metricsStr); |
| 313 | output_scalar(metrics.fXHeight, emSize, &metricsStr); |
| 314 | output_scalar(metrics.fCapHeight, emSize, &metricsStr); |
| 315 | output_scalar(metrics.fUnderlineThickness, emSize, &metricsStr); |
| 316 | output_scalar(metrics.fUnderlinePosition, emSize, &metricsStr); |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 317 | output_scalar(metrics.fStrikeoutThickness, emSize, &metricsStr); |
| 318 | output_scalar(metrics.fStrikeoutPosition, emSize, &metricsStr); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 319 | metricsStr = strip_final(metricsStr); |
| 320 | fprintf(out, "%s\n};\n\n", metricsStr.c_str()); |
| 321 | } |
| 322 | |
| 323 | struct FontWritten { |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 324 | const char* fFontName; |
| 325 | NamedFontStyle fNamedStyle; |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 326 | }; |
| 327 | |
| 328 | static SkTDArray<FontWritten> gWritten; |
| 329 | |
| 330 | static int written_index(const FontDesc& fontDesc) { |
| 331 | for (int index = 0; index < gWritten.count(); ++index) { |
| 332 | const FontWritten& writ = gWritten[index]; |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 333 | if (!strcmp(fontDesc.fFontName, writ.fFontName) && |
| 334 | fontDesc.fNamedStyle.fStyle == writ.fNamedStyle.fStyle) |
| 335 | { |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 336 | return index; |
| 337 | } |
| 338 | } |
| 339 | return -1; |
| 340 | } |
| 341 | |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 342 | static void generate_fonts(const char* basepath) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 343 | FILE* out = nullptr; |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 344 | for (int index = 0; index < gFontsCount; ++index) { |
| 345 | FontDesc& fontDesc = gFonts[index]; |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 346 | if ((index & 3) == 0) { |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 347 | out = font_header(fontDesc.fGenericName); |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 348 | } |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 349 | int fontIndex = written_index(fontDesc); |
| 350 | if (fontIndex >= 0) { |
| 351 | fontDesc.fFontIndex = fontIndex; |
| 352 | continue; |
| 353 | } |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 354 | SkString filepath(SkOSPath::Join(basepath, fontDesc.fFile)); |
| 355 | SkASSERTF(sk_exists(filepath.c_str()), "The file %s does not exist.", filepath.c_str()); |
| 356 | sk_sp<SkTypeface> resourceTypeface = SkTypeface::MakeFromFile(filepath.c_str()); |
| 357 | SkASSERTF(resourceTypeface, "The file %s is not a font.", filepath.c_str()); |
| 358 | output_font(std::move(resourceTypeface), fontDesc.fFontName, fontDesc.fNamedStyle, out); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 359 | fontDesc.fFontIndex = gWritten.count(); |
| 360 | FontWritten* writ = gWritten.append(); |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 361 | writ->fFontName = fontDesc.fFontName; |
| 362 | writ->fNamedStyle = fontDesc.fNamedStyle; |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 363 | if ((index & 3) == 3) { |
| 364 | fclose(out); |
| 365 | } |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 369 | static const char* slant_to_string(SkFontStyle::Slant slant) { |
| 370 | switch (slant) { |
| 371 | case SkFontStyle::kUpright_Slant: return "SkFontStyle::kUpright_Slant"; |
| 372 | case SkFontStyle::kItalic_Slant : return "SkFontStyle::kItalic_Slant" ; |
| 373 | case SkFontStyle::kOblique_Slant: return "SkFontStyle::kOblique_Slant"; |
| 374 | default: SK_ABORT("Unknown slant"); return ""; |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 375 | } |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static void generate_index(const char* defaultName) { |
| 379 | FILE* out = font_header("index"); |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 380 | fprintf(out, "static SkTestFontData gTestFonts[] = {\n"); |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 381 | for (const FontWritten& writ : gWritten) { |
| 382 | const char* name = writ.fFontName; |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 383 | SkString strippedStr = strip_spaces(SkString(name)); |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 384 | strippedStr.appendf("%s", writ.fNamedStyle.fName); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 385 | const char* strip = strippedStr.c_str(); |
| 386 | fprintf(out, |
| 387 | " { %sPoints, %sVerbs, %sCharCodes,\n" |
| 388 | " %sCharCodesCount, %sWidths,\n" |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 389 | " %sMetrics, \"Toy %s\", SkFontStyle(%d,%d,%s), nullptr\n" |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 390 | " },\n", |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 391 | strip, strip, strip, strip, strip, strip, name, |
| 392 | writ.fNamedStyle.fStyle.weight(), writ.fNamedStyle.fStyle.width(), |
| 393 | slant_to_string(writ.fNamedStyle.fStyle.slant())); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 394 | } |
| 395 | fprintf(out, "};\n\n"); |
| 396 | fprintf(out, "const int gTestFontsCount = (int) SK_ARRAY_COUNT(gTestFonts);\n\n"); |
| 397 | fprintf(out, |
| 398 | "struct SubFont {\n" |
| 399 | " const char* fName;\n" |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 400 | " SkFontStyle fStyle;\n" |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 401 | " SkTestFontData& fFont;\n" |
| 402 | " const char* fFile;\n" |
| 403 | "};\n\n" |
| 404 | "const SubFont gSubFonts[] = {\n"); |
| 405 | int defaultIndex = -1; |
| 406 | for (int subIndex = 0; subIndex < gFontsCount; subIndex++) { |
| 407 | const FontDesc& desc = gFonts[subIndex]; |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 408 | if (defaultIndex < 0 && !strcmp(defaultName, desc.fGenericName)) { |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 409 | defaultIndex = subIndex; |
| 410 | } |
| 411 | fprintf(out, |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 412 | " { \"%s\", SkFontStyle(%d,%d,%s), gTestFonts[%d], \"%s\" },\n", |
| 413 | desc.fGenericName, |
| 414 | desc.fNamedStyle.fStyle.weight(), desc.fNamedStyle.fStyle.width(), |
| 415 | slant_to_string(desc.fNamedStyle.fStyle.slant()), desc.fFontIndex, desc.fFile); |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 416 | } |
| 417 | for (int subIndex = 0; subIndex < gFontsCount; subIndex++) { |
| 418 | const FontDesc& desc = gFonts[subIndex]; |
| 419 | fprintf(out, |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 420 | " { \"Toy %s\", SkFontStyle(%d,%d,%s), gTestFonts[%d], \"%s\" },\n", |
| 421 | desc.fFontName, |
| 422 | desc.fNamedStyle.fStyle.weight(), desc.fNamedStyle.fStyle.width(), |
| 423 | slant_to_string(desc.fNamedStyle.fStyle.slant()), desc.fFontIndex, desc.fFile); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 424 | } |
| 425 | fprintf(out, "};\n\n"); |
| 426 | fprintf(out, "const int gSubFontsCount = (int) SK_ARRAY_COUNT(gSubFonts);\n\n"); |
| 427 | SkASSERT(defaultIndex >= 0); |
| 428 | fprintf(out, "const int gDefaultFontIndex = %d;\n", defaultIndex); |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 429 | fclose(out); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | int main(int , char * const []) { |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 433 | generate_fonts("/Library/Fonts/"); |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 434 | generate_index(DEFAULT_FONT_NAME); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 435 | return 0; |
| 436 | } |