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 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 8 | #include "Resources.h" |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 9 | #include "SkFontMgr.h" |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame^] | 10 | #include "SkMutex.h" |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 11 | #include "SkOSFile.h" |
| 12 | #include "SkTestScalerContext.h" |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 13 | #include "SkUtils.h" |
| 14 | #include "sk_tool_utils.h" |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 15 | |
| 16 | namespace sk_tool_utils { |
| 17 | |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 18 | #include "test_font_monospace.cpp" |
| 19 | #include "test_font_sans_serif.cpp" |
| 20 | #include "test_font_serif.cpp" |
| 21 | #include "test_font_index.cpp" |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 22 | |
caryclark | f53ce80 | 2015-06-15 06:48:30 -0700 | [diff] [blame] | 23 | void release_portable_typefaces() { |
caryclark | c2a4846 | 2014-07-31 06:36:45 -0700 | [diff] [blame] | 24 | for (int index = 0; index < gTestFontsCount; ++index) { |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 25 | SkTestFontData& fontData = gTestFonts[index]; |
| 26 | SkSafeUnref(fontData.fFontCache); |
| 27 | } |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 30 | SK_DECLARE_STATIC_MUTEX(gTestFontMutex); |
| 31 | |
| 32 | SkTypeface* create_font(const char* name, SkTypeface::Style style) { |
| 33 | SkTestFontData* fontData = NULL; |
| 34 | const SubFont* sub; |
| 35 | if (name) { |
caryclark | c2a4846 | 2014-07-31 06:36:45 -0700 | [diff] [blame] | 36 | for (int index = 0; index < gSubFontsCount; ++index) { |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 37 | sub = &gSubFonts[index]; |
| 38 | if (!strcmp(name, sub->fName) && sub->fStyle == style) { |
| 39 | fontData = &sub->fFont; |
| 40 | break; |
| 41 | } |
| 42 | } |
| 43 | if (!fontData) { |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 44 | // Once all legacy callers to portable fonts are converted, replace this with |
| 45 | // SK_CRASH(); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 46 | SkDebugf("missing %s %d\n", name, style); |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 47 | // 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 Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 51 | } |
| 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 Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 66 | } |
| 67 | } |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 68 | return SkNEW_ARGS(SkTestTypeface, (font, SkFontStyle(style))); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | |
| 72 | SkTypeface* resource_font(const char* name, SkTypeface::Style style) { |
| 73 | const char* file = NULL; |
| 74 | if (name) { |
caryclark | c2a4846 | 2014-07-31 06:36:45 -0700 | [diff] [blame] | 75 | for (int index = 0; index < gSubFontsCount; ++index) { |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 76 | 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 | |
| 98 | char const * const gStyleName[] = { |
| 99 | "", |
| 100 | "_Bold", |
| 101 | "_Italic", |
| 102 | "_BoldItalic", |
| 103 | }; |
| 104 | |
| 105 | static 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 | |
| 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 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 | |
| 137 | void 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 | |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 180 | } |