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 | |
benjaminwagner | cb571e1 | 2016-07-27 11:12:12 -0700 | [diff] [blame] | 18 | #include "test_font_monospace.inc" |
| 19 | #include "test_font_sans_serif.inc" |
| 20 | #include "test_font_serif.inc" |
| 21 | #include "test_font_index.inc" |
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]; |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 26 | fontData.fCachedFont.reset(); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 27 | } |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 28 | } |
| 29 | |
reed | 086eea9 | 2016-05-04 17:12:46 -0700 | [diff] [blame] | 30 | SK_DECLARE_STATIC_MUTEX(gTestFontMutex); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 31 | |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 32 | sk_sp<SkTypeface> create_font(const char* name, SkFontStyle 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(); |
Mike Klein | 8f514c7 | 2016-09-27 09:10:15 -0400 | [diff] [blame] | 46 | SkDebugf("missing %s weight %d, width %d, slant %d\n", |
| 47 | name, style.weight(), style.width(), style.slant()); |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 48 | // If we called SkTypeface::CreateFromName() here we'd recurse infinitely, |
| 49 | // so we reimplement its core logic here inline without the recursive aspect. |
Hal Canary | 1b612a8 | 2016-11-03 16:26:13 -0400 | [diff] [blame] | 50 | sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault()); |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 51 | return fm->legacyMakeTypeface(name, style); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 52 | } |
| 53 | } else { |
| 54 | sub = &gSubFonts[gDefaultFontIndex]; |
| 55 | fontData = &sub->fFont; |
| 56 | } |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 57 | sk_sp<SkTestFont> font; |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 58 | { |
| 59 | SkAutoMutexAcquire ac(gTestFontMutex); |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 60 | if (fontData->fCachedFont) { |
| 61 | font = fontData->fCachedFont; |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 62 | } else { |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 63 | font = sk_make_sp<SkTestFont>(*fontData); |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 64 | fontData->fCachedFont = font; |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 65 | } |
| 66 | } |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 67 | return sk_make_sp<SkTestTypeface>(std::move(font), style); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 68 | } |
| 69 | |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 70 | } |