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
8#include "Resources.h"
9#include "SkFontMgr.h"
10#include "SkMutex.h"
11#include "SkOSFile.h"
12#include "SkTestScalerContext.h"
13#include "SkUtils.h"
14#include "sk_tool_utils.h"
15
16namespace sk_tool_utils {
17
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"
22
23void release_portable_typefaces() {
24    for (int index = 0; index < gTestFontsCount; ++index) {
25        SkTestFontData& fontData = gTestFonts[index];
26        SkSafeUnref(fontData.fFontCache);
27    }
28}
29
30SK_DECLARE_STATIC_MUTEX(gTestFontMutex);
31
32SkTypeface* create_font(const char* name, SkTypeface::Style style) {
33    SkTestFontData* fontData = nullptr;
34    const SubFont* sub;
35    if (name) {
36        for (int index = 0; index < gSubFontsCount; ++index) {
37            sub = &gSubFonts[index];
38            if (!strcmp(name, sub->fName) && sub->fStyle == style) {
39                fontData = &sub->fFont;
40                break;
41            }
42        }
43        if (!fontData) {
44            // Once all legacy callers to portable fonts are converted, replace this with
45            // SK_ABORT();
46            SkDebugf("missing %s %d\n", name, style);
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);
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 = new SkTestFont(*fontData);
63            SkDEBUGCODE(font->fDebugName = sub->fName);
64            SkDEBUGCODE(font->fDebugStyle = sub->fStyle);
65            fontData->fFontCache = SkSafeRef(font);
66        }
67    }
68    return new SkTestTypeface(font, SkFontStyle(style));
69}
70
71}
72