Home | History | Annotate | Download | only in tools
      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 
     16 namespace sk_tool_utils {
     17 
     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"
     22 
     23 void release_portable_typefaces() {
     24     for (int index = 0; index < gTestFontsCount; ++index) {
     25         SkTestFontData& fontData = gTestFonts[index];
     26         fontData.fCachedFont.reset();
     27     }
     28 }
     29 
     30 SK_DECLARE_STATIC_MUTEX(gTestFontMutex);
     31 
     32 sk_sp<SkTypeface> create_font(const char* name, SkFontStyle 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 weight %d, width %d, slant %d\n",
     47                      name, style.weight(), style.width(), style.slant());
     48             // If we called SkTypeface::CreateFromName() here we'd recurse infinitely,
     49             // so we reimplement its core logic here inline without the recursive aspect.
     50             sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
     51             return sk_sp<SkTypeface>(fm->legacyCreateTypeface(name, style));
     52         }
     53     } else {
     54         sub = &gSubFonts[gDefaultFontIndex];
     55         fontData = &sub->fFont;
     56     }
     57     sk_sp<SkTestFont> font;
     58     {
     59         SkAutoMutexAcquire ac(gTestFontMutex);
     60         if (fontData->fCachedFont) {
     61             font = fontData->fCachedFont;
     62         } else {
     63             font = sk_make_sp<SkTestFont>(*fontData);
     64             SkDEBUGCODE(font->fDebugName = sub->fName);
     65             SkDEBUGCODE(font->fDebugStyle = sub->fStyle);
     66             fontData->fCachedFont = font;
     67         }
     68     }
     69     return sk_make_sp<SkTestTypeface>(std::move(font), style);
     70 }
     71 
     72 }
     73