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