blob: adbfa1692b3191057e481591580baeed125b254c [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) {
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();
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());
bungeman11a77c62016-04-12 13:45:06 -070050 return fm->legacyCreateTypeface(name, SkFontStyle::FromOldStyle(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 {
halcanary385fe4d2015-08-26 13:07:48 -070062 font = new SkTestFont(*fontData);
Cary Clark992c7b02014-07-31 08:58:44 -040063 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 }
bungeman11a77c62016-04-12 13:45:06 -070068 return new SkTestTypeface(font, SkFontStyle::FromOldStyle(style));
Cary Clark992c7b02014-07-31 08:58:44 -040069}
70
caryclark5fb6bd42014-06-23 11:25:00 -070071}