blob: bdd88d07f9a045f7fec1e70b3d11f1e263a8ff48 [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
benjaminwagnercb571e12016-07-27 11:12:12 -070018#include "test_font_monospace.inc"
19#include "test_font_sans_serif.inc"
20#include "test_font_serif.inc"
21#include "test_font_index.inc"
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];
bungeman7cfd46a2016-10-20 16:06:52 -040026 fontData.fCachedFont.reset();
Cary Clark992c7b02014-07-31 08:58:44 -040027 }
caryclark5fb6bd42014-06-23 11:25:00 -070028}
29
reed086eea92016-05-04 17:12:46 -070030SK_DECLARE_STATIC_MUTEX(gTestFontMutex);
Cary Clark992c7b02014-07-31 08:58:44 -040031
mbocee6a9912016-05-31 11:42:36 -070032sk_sp<SkTypeface> create_font(const char* name, SkFontStyle style) {
halcanary96fcdcc2015-08-27 07:41:13 -070033 SkTestFontData* fontData = nullptr;
Cary Clark992c7b02014-07-31 08:58:44 -040034 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
djsollenf2b340f2016-01-29 08:51:04 -080045 // SK_ABORT();
Mike Klein8f514c72016-09-27 09:10:15 -040046 SkDebugf("missing %s weight %d, width %d, slant %d\n",
47 name, style.weight(), style.width(), style.slant());
caryclark83ca6282015-06-10 09:31:09 -070048 // If we called SkTypeface::CreateFromName() here we'd recurse infinitely,
49 // so we reimplement its core logic here inline without the recursive aspect.
Hal Canary1b612a82016-11-03 16:26:13 -040050 sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
mbocee6a9912016-05-31 11:42:36 -070051 return sk_sp<SkTypeface>(fm->legacyCreateTypeface(name, style));
Cary Clark992c7b02014-07-31 08:58:44 -040052 }
53 } else {
54 sub = &gSubFonts[gDefaultFontIndex];
55 fontData = &sub->fFont;
56 }
bungeman7cfd46a2016-10-20 16:06:52 -040057 sk_sp<SkTestFont> font;
Cary Clark992c7b02014-07-31 08:58:44 -040058 {
59 SkAutoMutexAcquire ac(gTestFontMutex);
bungeman7cfd46a2016-10-20 16:06:52 -040060 if (fontData->fCachedFont) {
61 font = fontData->fCachedFont;
Cary Clark992c7b02014-07-31 08:58:44 -040062 } else {
bungeman7cfd46a2016-10-20 16:06:52 -040063 font = sk_make_sp<SkTestFont>(*fontData);
Cary Clark992c7b02014-07-31 08:58:44 -040064 SkDEBUGCODE(font->fDebugName = sub->fName);
65 SkDEBUGCODE(font->fDebugStyle = sub->fStyle);
bungeman7cfd46a2016-10-20 16:06:52 -040066 fontData->fCachedFont = font;
Cary Clark992c7b02014-07-31 08:58:44 -040067 }
68 }
bungeman7cfd46a2016-10-20 16:06:52 -040069 return sk_make_sp<SkTestTypeface>(std::move(font), style);
Cary Clark992c7b02014-07-31 08:58:44 -040070}
71
caryclark5fb6bd42014-06-23 11:25:00 -070072}