OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
| 8 // running create_test_font generates ./tools/test_font_data.cpp |
| 9 // which is read by ./tools/sk_tool_utils_font.cpp |
| 10 |
| 11 #include "Resources.h" |
| 12 #include "SkOSFile.h" |
8 #include "SkPaint.h" | 13 #include "SkPaint.h" |
9 #include "SkPath.h" | 14 #include "SkPath.h" |
10 #include "SkStream.h" | 15 #include "SkStream.h" |
11 #include "SkTArray.h" | 16 #include "SkTArray.h" |
12 #include "SkTSort.h" | 17 #include "SkTSort.h" |
13 #include "SkTypeface.h" | 18 #include "SkTypeface.h" |
| 19 #include "SkUtils.h" |
14 #include <stdio.h> | 20 #include <stdio.h> |
15 | 21 |
16 enum { | 22 // the folllowing include is generated by running dm with |
17 kEmSize = 4096 | 23 // --portableFonts --reportUsedChars |
| 24 #include "test_font_data_chars.cpp" |
| 25 |
| 26 #define DEFAULT_FONT_NAME "Liberation Sans" |
| 27 |
| 28 static struct FontDesc { |
| 29 const char* fName; |
| 30 SkTypeface::Style fStyle; |
| 31 const char* fFont; |
| 32 const char* fFile; |
| 33 const char* fCharsUsed; |
| 34 int fFontIndex; |
| 35 } gFonts[] = { |
| 36 {"Courier New", SkTypeface::kNormal, "Courier New", "Courier New.ttf", |
| 37 gCourierNew}, |
| 38 {"Courier New", SkTypeface::kBold, "Courier New", "Courier New Bold.tt
f", |
| 39 gCourierNew_Bold}, |
| 40 {"Courier New", SkTypeface::kItalic, "Courier New", "Courier New Italic.
ttf", |
| 41 gCourierNew_Italic}, |
| 42 {"Courier New", SkTypeface::kBoldItalic, "Courier New", "Courier New Bold It
alic.ttf", |
| 43 gCourierNew_BoldItalic}, |
| 44 {"Helvetica", SkTypeface::kNormal, "Liberation Sans", "LiberationSans-Regu
lar.ttf", |
| 45 gLiberationSans}, |
| 46 {"Helvetica", SkTypeface::kBold, "Liberation Sans", "LiberationSans-Bold
.ttf", |
| 47 gLiberationSans_Bold}, |
| 48 {"Helvetica", SkTypeface::kItalic, "Liberation Sans", "LiberationSans-Ital
ic.ttf", |
| 49 gLiberationSans_Italic}, |
| 50 {"Helvetica", SkTypeface::kBoldItalic, "Liberation Sans", "LiberationSans-B
oldItalic.ttf", |
| 51 gLiberationSans_BoldItalic}, |
| 52 {"Hiragino Maru Gothic Pro", SkTypeface::kNormal, "Hiragino Maru Gothic Pro"
, "Pro W4.otf", |
| 53 gHiraginoMaruGothicPro}, |
| 54 {"Liberation Sans", SkTypeface::kNormal, "Liberation Sans", "LiberationSans-
Regular.ttf", |
| 55 gLiberationSans}, |
| 56 {"Liberation Sans", SkTypeface::kBold, "Liberation Sans", "LiberationSans-
Bold.ttf", |
| 57 gLiberationSans_Bold}, |
| 58 {"Liberation Sans", SkTypeface::kItalic, "Liberation Sans", "LiberationSans-
Italic.ttf", |
| 59 gLiberationSans_Italic}, |
| 60 {"Liberation Sans", SkTypeface::kBoldItalic, "Liberation Sans", "LiberationS
ans-BoldItalic.ttf", |
| 61 gLiberationSans_BoldItalic}, |
| 62 {"monospace", SkTypeface::kNormal, "Courier New", "Courier New.ttf", |
| 63 gCourierNew}, |
| 64 {"monospace", SkTypeface::kBold, "Courier New", "Courier New Bold.tt
f", |
| 65 gCourierNew_Bold}, |
| 66 {"monospace", SkTypeface::kItalic, "Courier New", "Courier New Italic.
ttf", |
| 67 gCourierNew_Italic}, |
| 68 {"monospace", SkTypeface::kBoldItalic, "Courier New", "Courier New Bold It
alic.ttf", |
| 69 gCourierNew_BoldItalic}, |
| 70 {"Papyrus", SkTypeface::kNormal, "Papyrus", "Papyrus.ttc", |
| 71 gPapyrus}, |
| 72 {"sans-serif", SkTypeface::kNormal, "Liberation Sans", "LiberationSans-Regu
lar.ttf", |
| 73 gLiberationSans}, |
| 74 {"sans-serif", SkTypeface::kBold, "Liberation Sans", "LiberationSans-Bold
.ttf", |
| 75 gLiberationSans_Bold}, |
| 76 {"sans-serif", SkTypeface::kItalic, "Liberation Sans", "LiberationSans-Ital
ic.ttf", |
| 77 gLiberationSans_Italic}, |
| 78 {"sans-serif", SkTypeface::kBoldItalic, "Liberation Sans", "LiberationSans-B
oldItalic.ttf", |
| 79 gLiberationSans_BoldItalic}, |
| 80 {"serif", SkTypeface::kNormal, "Times New Roman", "Times New Roman.ttf
", |
| 81 gTimesNewRoman}, |
| 82 {"serif", SkTypeface::kBold, "Times New Roman", "Times New Roman Bol
d.ttf", |
| 83 gTimesNewRoman_Bold}, |
| 84 {"serif", SkTypeface::kItalic, "Times New Roman", "Times New Roman Ita
lic.ttf", |
| 85 gTimesNewRoman_Italic}, |
| 86 {"serif", SkTypeface::kBoldItalic, "Times New Roman", "Times New Roman Bol
d Italic.ttf", |
| 87 gTimesNewRoman_BoldItalic}, |
| 88 {"Times", SkTypeface::kNormal, "Times New Roman", "Times New Roman.ttf
", |
| 89 gTimesNewRoman}, |
| 90 {"Times", SkTypeface::kBold, "Times New Roman", "Times New Roman Bol
d.ttf", |
| 91 gTimesNewRoman_Bold}, |
| 92 {"Times", SkTypeface::kItalic, "Times New Roman", "Times New Roman Ita
lic.ttf", |
| 93 gTimesNewRoman_Italic}, |
| 94 {"Times", SkTypeface::kBoldItalic, "Times New Roman", "Times New Roman Bol
d Italic.ttf", |
| 95 gTimesNewRoman_BoldItalic}, |
| 96 {"Times New Roman", SkTypeface::kNormal, "Times New Roman", "Times New Roman
.ttf", |
| 97 gTimesNewRoman}, |
| 98 {"Times New Roman", SkTypeface::kBold, "Times New Roman", "Times New Roman
Bold.ttf", |
| 99 gTimesNewRoman_Bold}, |
| 100 {"Times New Roman", SkTypeface::kItalic, "Times New Roman", "Times New Roman
Italic.ttf", |
| 101 gTimesNewRoman_Italic}, |
| 102 {"Times New Roman", SkTypeface::kBoldItalic, "Times New Roman", "Times New R
oman Bold Italic.ttf", |
| 103 gTimesNewRoman_BoldItalic}, |
| 104 {"Times Roman", SkTypeface::kNormal, "Liberation Sans", "LiberationSans-Regu
lar.ttf", |
| 105 gLiberationSans}, |
18 }; | 106 }; |
19 | 107 |
20 static void output_fixed(FILE* out, SkScalar num) { | 108 const int gFontsCount = (int) SK_ARRAY_COUNT(gFonts); |
21 SkASSERT(floor(num) == num); | 109 |
22 int hex = (int) num * (65536 / kEmSize); | 110 const char* gStyleName[] = { |
23 fprintf(out, "0x%08x", hex); | 111 "kNormal", |
| 112 "kBold", |
| 113 "kItalic", |
| 114 "kBoldItalic", |
| 115 }; |
| 116 |
| 117 const char gHeader[] = |
| 118 "/*\n" |
| 119 " * Copyright 2014 Google Inc.\n" |
| 120 " *\n" |
| 121 " * Use of this source code is governed by a BSD-style license that can be\n" |
| 122 " * found in the LICENSE file.\n" |
| 123 " */\n" |
| 124 "\n" |
| 125 "// Auto-generated by "; |
| 126 |
| 127 static FILE* font_header() { |
| 128 SkString outPath(SkOSPath::SkPathJoin(".", "tools")); |
| 129 outPath = SkOSPath::SkPathJoin(outPath.c_str(), "test_font_data.cpp"); |
| 130 FILE* out = fopen(outPath.c_str(), "w"); |
| 131 fprintf(out, "%s%s\n\n", gHeader, SkOSPath::SkBasename(__FILE__).c_str()); |
| 132 return out; |
24 } | 133 } |
25 | 134 |
26 static void output_scalar(FILE* out, SkScalar num) { | 135 enum { |
27 num /= kEmSize; | 136 kMaxLineLength = 80, |
| 137 }; |
| 138 |
| 139 static ptrdiff_t last_line_length(const SkString& str) { |
| 140 const char* first = str.c_str(); |
| 141 const char* last = first + str.size(); |
| 142 const char* ptr = last; |
| 143 while (ptr > first && *--ptr != '\n') |
| 144 ; |
| 145 return last - ptr - 1; |
| 146 } |
| 147 |
| 148 static void output_fixed(SkScalar num, int emSize, SkString* out) { |
| 149 int hex = (int) (num * 65536 / emSize); |
| 150 out->appendf("0x%08x,", hex); |
| 151 *out += (int) last_line_length(*out) >= kMaxLineLength ? '\n' : ' '; |
| 152 } |
| 153 |
| 154 static void output_scalar(SkScalar num, int emSize, SkString* out) { |
| 155 num /= emSize; |
28 if (num == (int) num) { | 156 if (num == (int) num) { |
29 fprintf(out, "%d", (int) num); | 157 out->appendS32((int) num); |
30 } else { | 158 } else { |
31 SkString str; | 159 SkString str; |
32 str.printf("%1.6g", num); | 160 str.printf("%1.6g", num); |
33 int width = (int) str.size(); | 161 int width = (int) str.size(); |
34 const char* cStr = str.c_str(); | 162 const char* cStr = str.c_str(); |
35 while (cStr[width - 1] == '0') { | 163 while (cStr[width - 1] == '0') { |
36 --width; | 164 --width; |
37 } | 165 } |
38 str.resize(width); | 166 str.remove(width, str.size() - width); |
39 fprintf(out, "%sf", str.c_str()); | 167 out->appendf("%sf", str.c_str()); |
40 } | 168 } |
41 } | 169 *out += ','; |
42 | 170 *out += (int) last_line_length(*out) >= kMaxLineLength ? '\n' : ' '; |
43 static void output_points(FILE* out, const SkPoint* pts, int count) { | 171 } |
| 172 |
| 173 static int output_points(const SkPoint* pts, int emSize, int count, SkString* pt
sOut) { |
44 for (int index = 0; index < count; ++index) { | 174 for (int index = 0; index < count; ++index) { |
45 SkASSERT(floor(pts[index].fX) == pts[index].fX); | 175 // SkASSERT(floor(pts[index].fX) == pts[index].fX); |
46 output_scalar(out, pts[index].fX); | 176 output_scalar(pts[index].fX, emSize, ptsOut); |
47 fprintf(out, ", "); | 177 // SkASSERT(floor(pts[index].fY) == pts[index].fY); |
48 SkASSERT(floor(pts[index].fY) == pts[index].fY); | 178 output_scalar(pts[index].fY, emSize, ptsOut); |
49 output_scalar(out, pts[index].fY); | 179 } |
50 if (index + 1 < count) { | 180 return count; |
51 fprintf(out, ", "); | 181 } |
52 } | 182 |
53 } | 183 static void output_path_data(const SkPaint& paint, const char* used, |
54 fprintf(out, ");\n"); | 184 int emSize, SkString* ptsOut, SkTDArray<SkPath::Verb>* verbs, |
55 } | 185 SkTDArray<unsigned>* charCodes, SkTDArray<SkScalar>* widths) { |
56 | 186 while (*used) { |
57 const char header[] = | 187 SkUnichar index = SkUTF8_NextUnichar(&used); |
58 "/*\n" | 188 SkPath path; |
59 " * Copyright 2014 Google Inc.\n" | 189 paint.getTextPath((const void*) &index, 2, 0, 0, &path); |
60 " *\n" | 190 SkPath::RawIter iter(path); |
61 " * Use of this source code is governed by a BSD-style license that can be\n" | 191 SkPath::Verb verb; |
62 " * found in the LICENSE file.\n" | 192 SkPoint pts[4]; |
63 " */\n" | 193 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
64 "\n" | 194 *verbs->append() = verb; |
65 "// this was auto-generated by TestFontCreator.cpp\n" | 195 switch (verb) { |
66 "\n" | 196 case SkPath::kMove_Verb: |
67 "#include \"SkPaint.h\"\n" | 197 output_points(&pts[0], emSize, 1, ptsOut); |
68 "#include \"SkPath.h\"\n" | 198 break; |
69 "#include \"SkTDArray.h\"\n" | 199 case SkPath::kLine_Verb: |
70 "\n" | 200 output_points(&pts[1], emSize, 1, ptsOut); |
71 "namespace sk_tool_utils {\n" | 201 break; |
72 "\n" | 202 case SkPath::kQuad_Verb: |
73 "SkPaint::FontMetrics create_font(SkTDArray<SkPath*>& pathArray,\n" | 203 output_points(&pts[1], emSize, 2, ptsOut); |
74 " SkTDArray<SkFixed>& widthArray) {"; | 204 break; |
75 | 205 case SkPath::kCubic_Verb: |
76 int tool_main(int argc, char** argv); | 206 output_points(&pts[1], emSize, 3, ptsOut); |
77 int tool_main(int argc, char** argv) { | 207 break; |
| 208 case SkPath::kClose_Verb: |
| 209 break; |
| 210 default: |
| 211 SkDEBUGFAIL("bad verb"); |
| 212 SkASSERT(0); |
| 213 } |
| 214 } |
| 215 *verbs->append() = SkPath::kDone_Verb; |
| 216 *charCodes->append() = index; |
| 217 SkScalar width; |
| 218 SkDEBUGCODE(int charCount =) paint.getTextWidths((const void*) &index, 2
, &width); |
| 219 SkASSERT(charCount == 1); |
| 220 // SkASSERT(floor(width) == width); // not true for Hiragino Maru Gothic
Pro |
| 221 *widths->append() = width; |
| 222 } |
| 223 } |
| 224 |
| 225 static int offset_str_len(unsigned num) { |
| 226 if (num == (unsigned) -1) { |
| 227 return 10; |
| 228 } |
| 229 unsigned result = 1; |
| 230 unsigned ref = 10; |
| 231 while (ref <= num) { |
| 232 ++result; |
| 233 ref *= 10; |
| 234 } |
| 235 return result; |
| 236 } |
| 237 |
| 238 static SkString strip_spaces(const SkString& str) { |
| 239 SkString result; |
| 240 int count = (int) str.size(); |
| 241 for (int index = 0; index < count; ++index) { |
| 242 char c = str[index]; |
| 243 if (c != ' ' && c != '-') { |
| 244 result += c; |
| 245 } |
| 246 } |
| 247 return result; |
| 248 } |
| 249 |
| 250 static SkString strip_final(const SkString& str) { |
| 251 SkString result(str); |
| 252 if (result.endsWith("\n")) { |
| 253 result.remove(result.size() - 1, 1); |
| 254 } |
| 255 if (result.endsWith(" ")) { |
| 256 result.remove(result.size() - 1, 1); |
| 257 } |
| 258 if (result.endsWith(",")) { |
| 259 result.remove(result.size() - 1, 1); |
| 260 } |
| 261 return result; |
| 262 } |
| 263 |
| 264 static void output_font(SkTypeface* face, const char* name, SkTypeface::Style st
yle, |
| 265 const char* used, FILE* out) { |
| 266 int emSize = face->getUnitsPerEm() * 2; |
78 SkPaint paint; | 267 SkPaint paint; |
79 paint.setAntiAlias(true); | 268 paint.setAntiAlias(true); |
80 paint.setTextAlign(SkPaint::kLeft_Align); | 269 paint.setTextAlign(SkPaint::kLeft_Align); |
81 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); | 270 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); |
82 paint.setTextSize(kEmSize); | 271 paint.setTextSize(emSize); |
83 SkString filename("resources/DroidSans.ttf"); | 272 SkSafeUnref(paint.setTypeface(face)); |
84 SkAutoTUnref<SkFILEStream> stream(new SkFILEStream(filename.c_str())); | 273 SkTDArray<SkPath::Verb> verbs; |
85 if (!stream->isValid()) { | 274 SkTDArray<unsigned> charCodes; |
86 SkDebugf("Could not find resources/Roboto-Regular.ttf.\n"); | 275 SkTDArray<SkScalar> widths; |
87 return 1; | 276 SkString ptsOut; |
88 } | 277 output_path_data(paint, used, emSize, &ptsOut, &verbs, &charCodes, &widths); |
89 SkTypeface* typeface = SkTypeface::CreateFromStream(stream); | 278 SkString fontnameStr(name); |
90 SkSafeUnref(paint.setTypeface(typeface)); | 279 SkString strippedStr = strip_spaces(fontnameStr); |
91 FILE* out = fopen("./out/sk_tool_utils_font.cpp", "w"); | 280 strippedStr.appendf("%s", gStyleName[style]); |
92 fprintf(out, "%s\n", header); | 281 const char* fontname = strippedStr.c_str(); |
93 fprintf(out, " SkPath* path;\n"); | 282 fprintf(out, "const SkScalar %sPoints[] = {\n", fontname); |
94 for (unsigned short index = 0x20; index < 0x7F; ++index) { | 283 ptsOut = strip_final(ptsOut); |
95 SkPath path; | 284 fprintf(out, "%s", ptsOut.c_str()); |
96 paint.getTextPath((const void*) &index, 2, 0, 0, &path); | 285 fprintf(out, "\n};\n\n"); |
97 SkPath::RawIter iter(path); | 286 fprintf(out, "const unsigned char %sVerbs[] = {\n", fontname); |
98 uint8_t verb; | 287 int verbCount = verbs.count(); |
99 SkPoint pts[4]; | 288 int outChCount = 0; |
100 fprintf(out, " path = SkNEW(SkPath); //"); | 289 for (int index = 0; index < verbCount;) { |
101 if (index == '\\') { | 290 SkPath::Verb verb = verbs[index]; |
102 fprintf(out, " blackslash\n"); | 291 SkASSERT(verb >= SkPath::kMove_Verb && verb <= SkPath::kDone_Verb); |
103 } else if (index == ' ') { | 292 SkASSERT((unsigned) verb == (unsigned char) verb); |
104 fprintf(out, " space\n"); | 293 fprintf(out, "%u", verb); |
105 } else { | 294 if (++index < verbCount) { |
106 fprintf(out, " %c\n", (char) index); | 295 outChCount += 3; |
107 } | 296 fprintf(out, "%c", ','); |
108 fprintf(out, " *pathArray.append() = path;\n"); | 297 if (outChCount >= kMaxLineLength) { |
109 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { | 298 outChCount = 0; |
110 switch (verb) { | 299 fprintf(out, "%c", '\n'); |
111 case SkPath::kMove_Verb: | 300 } else { |
112 fprintf(out, " path->moveTo("); | 301 fprintf(out, "%c", ' '); |
113 output_points(out, &pts[0], 1); | |
114 continue; | |
115 case SkPath::kLine_Verb: | |
116 fprintf(out, " path->lineTo("); | |
117 output_points(out, &pts[1], 1); | |
118 break; | |
119 case SkPath::kQuad_Verb: | |
120 fprintf(out, " path->quadTo("); | |
121 output_points(out, &pts[1], 2); | |
122 break; | |
123 case SkPath::kCubic_Verb: | |
124 fprintf(out, " path->cubicTo("); | |
125 output_points(out, &pts[1], 3); | |
126 break; | |
127 case SkPath::kClose_Verb: | |
128 fprintf(out, " path->close();\n"); | |
129 break; | |
130 default: | |
131 SkDEBUGFAIL("bad verb"); | |
132 return 1; | |
133 } | 302 } |
134 } | 303 } |
135 SkScalar width; | 304 } |
136 SkDEBUGCODE(int charCount =) paint.getTextWidths((const void*) &index, 2
, &width); | 305 fprintf(out, "\n};\n\n"); |
137 SkASSERT(charCount == 1); | 306 |
138 fprintf(out, " *widthArray.append() = "); | 307 fprintf(out, "const unsigned %sCharCodes[] = {\n", fontname); |
139 output_fixed(out, width); | 308 int offsetCount = charCodes.count(); |
140 fprintf(out, ";\n\n"); | 309 for (int index = 0; index < offsetCount;) { |
141 } | 310 unsigned offset = charCodes[index]; |
| 311 fprintf(out, "%u", offset); |
| 312 if (++index < offsetCount) { |
| 313 outChCount += offset_str_len(offset) + 2; |
| 314 fprintf(out, "%c", ','); |
| 315 if (outChCount >= kMaxLineLength) { |
| 316 outChCount = 0; |
| 317 fprintf(out, "%c", '\n'); |
| 318 } else { |
| 319 fprintf(out, "%c", ' '); |
| 320 } |
| 321 } |
| 322 } |
| 323 fprintf(out, "\n};\n\n"); |
| 324 |
| 325 SkString widthsStr; |
| 326 fprintf(out, "const SkFixed %sWidths[] = {\n", fontname); |
| 327 for (int index = 0; index < offsetCount; ++index) { |
| 328 output_fixed(widths[index], emSize, &widthsStr); |
| 329 } |
| 330 widthsStr = strip_final(widthsStr); |
| 331 fprintf(out, "%s\n};\n\n", widthsStr.c_str()); |
| 332 |
| 333 fprintf(out, "const int %sCharCodesCount = (int) SK_ARRAY_COUNT(%sCharCodes)
;\n\n", |
| 334 fontname, fontname); |
| 335 |
142 SkPaint::FontMetrics metrics; | 336 SkPaint::FontMetrics metrics; |
143 paint.getFontMetrics(&metrics); | 337 paint.getFontMetrics(&metrics); |
144 fprintf(out, " SkPaint::FontMetrics metrics = {\n"); | 338 fprintf(out, "const SkPaint::FontMetrics %sMetrics = {\n", fontname); |
145 fprintf(out, " 0x%08x, ", metrics.fFlags); | 339 SkString metricsStr; |
146 output_scalar(out, metrics.fTop); fprintf(out, ", "); | 340 metricsStr.printf("0x%08x, ", metrics.fFlags); |
147 output_scalar(out, metrics.fAscent); fprintf(out, ", "); | 341 output_scalar(metrics.fTop, emSize, &metricsStr); |
148 output_scalar(out, metrics.fDescent); fprintf(out, ", "); | 342 output_scalar(metrics.fAscent, emSize, &metricsStr); |
149 output_scalar(out, metrics.fBottom); fprintf(out, ", "); | 343 output_scalar(metrics.fDescent, emSize, &metricsStr); |
150 output_scalar(out, metrics.fLeading); fprintf(out, ",\n "); | 344 output_scalar(metrics.fBottom, emSize, &metricsStr); |
151 output_scalar(out, metrics.fAvgCharWidth); fprintf(out, ", "); | 345 output_scalar(metrics.fLeading, emSize, &metricsStr); |
152 output_scalar(out, metrics.fMaxCharWidth); fprintf(out, ", "); | 346 output_scalar(metrics.fAvgCharWidth, emSize, &metricsStr); |
153 output_scalar(out, metrics.fXMin); fprintf(out, ", "); | 347 output_scalar(metrics.fMaxCharWidth, emSize, &metricsStr); |
154 output_scalar(out, metrics.fXMax); fprintf(out, ",\n "); | 348 output_scalar(metrics.fXMin, emSize, &metricsStr); |
155 output_scalar(out, metrics.fXHeight); fprintf(out, ", "); | 349 output_scalar(metrics.fXMax, emSize, &metricsStr); |
156 output_scalar(out, metrics.fCapHeight); fprintf(out, ", "); | 350 output_scalar(metrics.fXHeight, emSize, &metricsStr); |
157 output_scalar(out, metrics.fUnderlineThickness); fprintf(out, ", "); | 351 output_scalar(metrics.fCapHeight, emSize, &metricsStr); |
158 output_scalar(out, metrics.fUnderlinePosition); fprintf(out, "\n };\n"); | 352 output_scalar(metrics.fUnderlineThickness, emSize, &metricsStr); |
159 fprintf(out, " return metrics;\n"); | 353 output_scalar(metrics.fUnderlinePosition, emSize, &metricsStr); |
160 fprintf(out, "}\n\n}\n"); | 354 metricsStr = strip_final(metricsStr); |
| 355 fprintf(out, "%s\n};\n\n", metricsStr.c_str()); |
| 356 } |
| 357 |
| 358 struct FontWritten { |
| 359 const char* fName; |
| 360 SkTypeface::Style fStyle; |
| 361 }; |
| 362 |
| 363 static SkTDArray<FontWritten> gWritten; |
| 364 |
| 365 static int written_index(const FontDesc& fontDesc) { |
| 366 for (int index = 0; index < gWritten.count(); ++index) { |
| 367 const FontWritten& writ = gWritten[index]; |
| 368 if (!strcmp(fontDesc.fFont, writ.fName) && fontDesc.fStyle == writ.fStyl
e) { |
| 369 return index; |
| 370 } |
| 371 } |
| 372 return -1; |
| 373 } |
| 374 |
| 375 static void generate_fonts(FILE* out) { |
| 376 for (int index = 0; index < gFontsCount; ++index) { |
| 377 FontDesc& fontDesc = gFonts[index]; |
| 378 int fontIndex = written_index(fontDesc); |
| 379 if (fontIndex >= 0) { |
| 380 fontDesc.fFontIndex = fontIndex; |
| 381 continue; |
| 382 } |
| 383 SkTypeface* systemTypeface = SkTypeface::CreateFromName(fontDesc.fFont,
fontDesc.fStyle); |
| 384 SkASSERT(systemTypeface); |
| 385 SkString filepath(GetResourcePath(fontDesc.fFile)); |
| 386 SkASSERT(sk_exists(filepath.c_str())); |
| 387 SkTypeface* resourceTypeface = SkTypeface::CreateFromFile(filepath.c_str
()); |
| 388 SkASSERT(resourceTypeface); |
| 389 output_font(resourceTypeface, fontDesc.fFont, fontDesc.fStyle, fontDesc.
fCharsUsed, out); |
| 390 fontDesc.fFontIndex = gWritten.count(); |
| 391 FontWritten* writ = gWritten.append(); |
| 392 writ->fName = fontDesc.fFont; |
| 393 writ->fStyle = fontDesc.fStyle; |
| 394 } |
| 395 } |
| 396 |
| 397 static void generate_index(const char* defaultName, FILE* out) { |
| 398 int fontCount = gWritten.count(); |
| 399 fprintf(out, |
| 400 "static SkTestFontData gTestFonts[] = {\n"); |
| 401 int fontIndex; |
| 402 for (fontIndex = 0; fontIndex < fontCount; ++fontIndex) { |
| 403 const FontWritten& writ = gWritten[fontIndex]; |
| 404 const char* name = writ.fName; |
| 405 SkString strippedStr = strip_spaces(SkString(name)); |
| 406 strippedStr.appendf("%s", gStyleName[writ.fStyle]); |
| 407 const char* strip = strippedStr.c_str(); |
| 408 fprintf(out, |
| 409 " { %sPoints, %sVerbs, %sCharCodes,\n" |
| 410 " %sCharCodesCount, %sWidths,\n" |
| 411 " %sMetrics, \"%s\", SkTypeface::%s, NULL\n" |
| 412 " },\n", |
| 413 strip, strip, strip, strip, strip, strip, name, gStyleName[writ.
fStyle]); |
| 414 } |
| 415 fprintf(out, "};\n\n"); |
| 416 fprintf(out, "const int gTestFontsCount = (int) SK_ARRAY_COUNT(gTestFonts);\
n\n"); |
| 417 fprintf(out, |
| 418 "struct SubFont {\n" |
| 419 " const char* fName;\n" |
| 420 " SkTypeface::Style fStyle;\n" |
| 421 " SkTestFontData& fFont;\n" |
| 422 " const char* fFile;\n" |
| 423 "};\n\n" |
| 424 "const SubFont gSubFonts[] = {\n"); |
| 425 int defaultIndex = -1; |
| 426 for (int subIndex = 0; subIndex < gFontsCount; subIndex++) { |
| 427 const FontDesc& desc = gFonts[subIndex]; |
| 428 if (!strcmp(defaultName, desc.fName)) { |
| 429 defaultIndex = subIndex; |
| 430 } |
| 431 fprintf(out, |
| 432 " { \"%s\", SkTypeface::%s, gTestFonts[%d], \"%s\"},\n", desc
.fName, |
| 433 gStyleName[desc.fStyle], desc.fFontIndex, desc.fFile); |
| 434 } |
| 435 fprintf(out, "};\n\n"); |
| 436 fprintf(out, "const int gSubFontsCount = (int) SK_ARRAY_COUNT(gSubFonts);\n\
n"); |
| 437 SkASSERT(defaultIndex >= 0); |
| 438 fprintf(out, "const int gDefaultFontIndex = %d;\n", defaultIndex); |
| 439 } |
| 440 |
| 441 int main(int , char * const []) { |
| 442 #ifndef SK_BUILD_FOR_MAC |
| 443 #error "use fonts installed on Mac" |
| 444 #endif |
| 445 FILE* out = font_header(); |
| 446 generate_fonts(out); |
| 447 generate_index(DEFAULT_FONT_NAME, out); |
161 fclose(out); | 448 fclose(out); |
162 return 0; | 449 return 0; |
163 } | 450 } |
164 | |
165 #if !defined SK_BUILD_FOR_IOS | |
166 int main(int argc, char * const argv[]) { | |
167 return tool_main(argc, (char**) argv); | |
168 } | |
169 #endif | |
OLD | NEW |