blob: 7b95fb04e07dea734d39fb65e997bac378e60e3d [file] [log] [blame]
caryclark5fb6bd42014-06-23 11:25:00 -07001/*
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
Cary Clark992c7b02014-07-31 08:58:44 -04008#include "Resources.h"
caryclark83ca6282015-06-10 09:31:09 -07009#include "SkFontMgr.h"
mtklein1b249332015-07-07 12:21:21 -070010#include "SkMutex.h"
Cary Clark992c7b02014-07-31 08:58:44 -040011#include "SkOSFile.h"
12#include "SkTestScalerContext.h"
Cary Clark992c7b02014-07-31 08:58:44 -040013#include "SkUtils.h"
14#include "sk_tool_utils.h"
caryclark5fb6bd42014-06-23 11:25:00 -070015
16namespace sk_tool_utils {
17
caryclark83ca6282015-06-10 09:31:09 -070018#include "test_font_monospace.cpp"
19#include "test_font_sans_serif.cpp"
20#include "test_font_serif.cpp"
21#include "test_font_index.cpp"
caryclark5fb6bd42014-06-23 11:25:00 -070022
caryclarkf53ce802015-06-15 06:48:30 -070023void release_portable_typefaces() {
caryclarkc2a48462014-07-31 06:36:45 -070024 for (int index = 0; index < gTestFontsCount; ++index) {
Cary Clark992c7b02014-07-31 08:58:44 -040025 SkTestFontData& fontData = gTestFonts[index];
26 SkSafeUnref(fontData.fFontCache);
27 }
caryclark5fb6bd42014-06-23 11:25:00 -070028}
29
Cary Clark992c7b02014-07-31 08:58:44 -040030SK_DECLARE_STATIC_MUTEX(gTestFontMutex);
31
32SkTypeface* create_font(const char* name, SkTypeface::Style style) {
33 SkTestFontData* fontData = NULL;
34 const SubFont* sub;
35 if (name) {
caryclarkc2a48462014-07-31 06:36:45 -070036 for (int index = 0; index < gSubFontsCount; ++index) {
Cary Clark992c7b02014-07-31 08:58:44 -040037 sub = &gSubFonts[index];
38 if (!strcmp(name, sub->fName) && sub->fStyle == style) {
39 fontData = &sub->fFont;
40 break;
41 }
42 }
43 if (!fontData) {
caryclark83ca6282015-06-10 09:31:09 -070044 // Once all legacy callers to portable fonts are converted, replace this with
45 // SK_CRASH();
Cary Clark992c7b02014-07-31 08:58:44 -040046 SkDebugf("missing %s %d\n", name, style);
caryclark83ca6282015-06-10 09:31:09 -070047 // If we called SkTypeface::CreateFromName() here we'd recurse infinitely,
48 // so we reimplement its core logic here inline without the recursive aspect.
49 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
50 return fm->legacyCreateTypeface(name, style);
Cary Clark992c7b02014-07-31 08:58:44 -040051 }
52 } else {
53 sub = &gSubFonts[gDefaultFontIndex];
54 fontData = &sub->fFont;
55 }
56 SkTestFont* font;
57 {
58 SkAutoMutexAcquire ac(gTestFontMutex);
59 if (fontData->fFontCache) {
60 font = SkSafeRef(fontData->fFontCache);
61 } else {
62 font = SkNEW_ARGS(SkTestFont, (*fontData));
63 SkDEBUGCODE(font->fDebugName = sub->fName);
64 SkDEBUGCODE(font->fDebugStyle = sub->fStyle);
65 fontData->fFontCache = SkSafeRef(font);
Cary Clark992c7b02014-07-31 08:58:44 -040066 }
67 }
bungemana4c4a2d2014-10-20 13:33:19 -070068 return SkNEW_ARGS(SkTestTypeface, (font, SkFontStyle(style)));
Cary Clark992c7b02014-07-31 08:58:44 -040069}
70
71
72SkTypeface* resource_font(const char* name, SkTypeface::Style style) {
73 const char* file = NULL;
74 if (name) {
caryclarkc2a48462014-07-31 06:36:45 -070075 for (int index = 0; index < gSubFontsCount; ++index) {
Cary Clark992c7b02014-07-31 08:58:44 -040076 const SubFont& sub = gSubFonts[index];
77 if (!strcmp(name, sub.fName) && sub.fStyle == style) {
78 file = sub.fFile;
79 break;
80 }
81 }
82 if (!file) {
83 return SkTypeface::CreateFromName(name, style);
84 }
85 } else {
86 file = gSubFonts[gDefaultFontIndex].fFile;
87 }
88 SkString filepath(GetResourcePath(file));
89 if (sk_exists(filepath.c_str())) {
90 return SkTypeface::CreateFromFile(filepath.c_str());
91 }
92 return SkTypeface::CreateFromName(name, style);
93}
94
95#ifdef SK_DEBUG
96#include <stdio.h>
97
98char const * const gStyleName[] = {
99 "",
100 "_Bold",
101 "_Italic",
102 "_BoldItalic",
103};
104
105static SkString strip_spaces(const char* str) {
106 SkString result;
107 int count = (int) strlen(str);
108 for (int index = 0; index < count; ++index) {
109 char c = str[index];
110 if (c != ' ' && c != '-') {
111 result += c;
112 }
113 }
114 return result;
115}
116
117const 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
127static FILE* font_header() {
128 SkString pathStr(GetResourcePath());
129 pathStr = SkOSPath::Join(pathStr.c_str(), "..");
130 pathStr = SkOSPath::Join(pathStr.c_str(), "tools");
131 pathStr = SkOSPath::Join(pathStr.c_str(), "test_font_data_chars.cpp");
132 FILE* out = fopen(pathStr.c_str(), "w");
133 fprintf(out, "%s%s\n\n", gHeader, SkOSPath::Basename(__FILE__).c_str());
134 return out;
135}
136
137void report_used_chars() {
138 FILE* out = font_header();
139 for (int index = 0; index < gTestFontsCount; ++index) {
140 SkTestFontData& fontData = gTestFonts[index];
141 SkTestFont* font = fontData.fFontCache;
142 if (!font) {
143 continue;
144 }
145 SkString name(strip_spaces(font->debugFontName()));
146 fprintf(out, "const char g%s%s[] =\n", name.c_str(), gStyleName[font->fDebugStyle]);
147 SkString used(" \"");
148 for (int c = ' '; c <= '~'; ++c) {
149 int bitOffset = c - ' ';
150 if (font->fDebugBits[bitOffset >> 3] & (1 << (bitOffset & 7))) {
151 if (c == '"' || c == '\\') {
152 used += '\\';
153 }
154 used += c;
155 }
156 }
157 if (used.size() > 1) {
158 fprintf(out, "%s\"", used.c_str());
159 }
160 int oIndex = 0;
161 while (font->fDebugOverage[oIndex]) {
162 uint16_t uni = font->fDebugOverage[oIndex];
163 size_t byteCount = SkUTF16_ToUTF8(&uni, 1, NULL);
164 SkAutoSTMalloc<10, char> utf8(byteCount);
165 SkUTF16_ToUTF8(&uni, 1, utf8);
166 for (unsigned byteIndex = 0; byteIndex < byteCount; ++byteIndex) {
167 char unibyte = utf8[byteIndex];
168 fprintf(out, " \"\\x%02X\"", (unsigned char) unibyte);
169 }
170 if (++oIndex >= (int) sizeof(font->fDebugOverage)) {
171 break;
172 }
173 }
174 fprintf(out, ";\n");
175 }
176 fclose(out);
177}
178#endif
179
caryclark5fb6bd42014-06-23 11:25:00 -0700180}