skia2/tools/sk_tool_utils_font.cpp
mboc ee6a9919a3 SkTypeface::MakeFromName to take SkFontStyle.
SkTypeface::MakeFromName currently takes SkTypeface::Style,
which is quite limited. This starts the transition to this
function taking SkFontStyle instead.

GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1818043002

TBR=reed
He said it sounded like a good idea.

Review-Url: https://codereview.chromium.org/1818043002
2016-05-31 11:42:37 -07:00

72 lines
2.2 KiB
C++

/*
* Copyright 2014 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "Resources.h"
#include "SkFontMgr.h"
#include "SkMutex.h"
#include "SkOSFile.h"
#include "SkTestScalerContext.h"
#include "SkUtils.h"
#include "sk_tool_utils.h"
namespace sk_tool_utils {
#include "test_font_monospace.cpp"
#include "test_font_sans_serif.cpp"
#include "test_font_serif.cpp"
#include "test_font_index.cpp"
void release_portable_typefaces() {
for (int index = 0; index < gTestFontsCount; ++index) {
SkTestFontData& fontData = gTestFonts[index];
SkSafeUnref(fontData.fFontCache);
}
}
SK_DECLARE_STATIC_MUTEX(gTestFontMutex);
sk_sp<SkTypeface> create_font(const char* name, SkFontStyle style) {
SkTestFontData* fontData = nullptr;
const SubFont* sub;
if (name) {
for (int index = 0; index < gSubFontsCount; ++index) {
sub = &gSubFonts[index];
if (!strcmp(name, sub->fName) && sub->fStyle == style) {
fontData = &sub->fFont;
break;
}
}
if (!fontData) {
// Once all legacy callers to portable fonts are converted, replace this with
// SK_ABORT();
SkDebugf("missing %s %d\n", name, style);
// If we called SkTypeface::CreateFromName() here we'd recurse infinitely,
// so we reimplement its core logic here inline without the recursive aspect.
SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
return sk_sp<SkTypeface>(fm->legacyCreateTypeface(name, style));
}
} else {
sub = &gSubFonts[gDefaultFontIndex];
fontData = &sub->fFont;
}
SkTestFont* font;
{
SkAutoMutexAcquire ac(gTestFontMutex);
if (fontData->fFontCache) {
font = SkSafeRef(fontData->fFontCache);
} else {
font = new SkTestFont(*fontData);
SkDEBUGCODE(font->fDebugName = sub->fName);
SkDEBUGCODE(font->fDebugStyle = sub->fStyle);
fontData->fFontCache = SkSafeRef(font);
}
}
return sk_make_sp<SkTestTypeface>(font, style);
}
}