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) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 33 | SkTestFontData* fontData = nullptr; |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 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 |
djsollen | f2b340f | 2016-01-29 08:51:04 -0800 | [diff] [blame] | 45 | // SK_ABORT(); |
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 { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 62 | font = new SkTestFont(*fontData); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 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 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 68 | return new SkTestTypeface(font, SkFontStyle(style)); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 69 | } |
| 70 | |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 71 | } |