• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2008 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 "SkFontConfigInterface.h"
9 #include "SkFontConfigTypeface.h"
10 #include "SkFontDescriptor.h"
11 #include "SkFontHost_FreeType_common.h"
12 #include "SkFontStream.h"
13 #include "SkStream.h"
14 #include "SkTypeface.h"
15 #include "SkTypefaceCache.h"
16 
17 ///////////////////////////////////////////////////////////////////////////////
18 ///////////////////////////////////////////////////////////////////////////////
19 
20 SK_DECLARE_STATIC_MUTEX(gFontConfigInterfaceMutex);
21 static SkFontConfigInterface* gFontConfigInterface;
22 
RefGlobal()23 SkFontConfigInterface* SkFontConfigInterface::RefGlobal() {
24     SkAutoMutexAcquire ac(gFontConfigInterfaceMutex);
25 
26     return SkSafeRef(gFontConfigInterface);
27 }
28 
SetGlobal(SkFontConfigInterface * fc)29 SkFontConfigInterface* SkFontConfigInterface::SetGlobal(SkFontConfigInterface* fc) {
30     SkAutoMutexAcquire ac(gFontConfigInterfaceMutex);
31 
32     SkRefCnt_SafeAssign(gFontConfigInterface, fc);
33     return fc;
34 }
35 
36 ///////////////////////////////////////////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////////////////
38 
39 // convenience function to create the direct interface if none is installed.
40 extern SkFontConfigInterface* SkCreateDirectFontConfigInterface();
41 
RefFCI()42 static SkFontConfigInterface* RefFCI() {
43     for (;;) {
44         SkFontConfigInterface* fci = SkFontConfigInterface::RefGlobal();
45         if (fci) {
46             return fci;
47         }
48         fci = SkFontConfigInterface::GetSingletonDirectInterface(&gFontConfigInterfaceMutex);
49         SkFontConfigInterface::SetGlobal(fci);
50     }
51 }
52 
53 // export this to SkFontMgr_fontconfig.cpp until this file just goes away.
54 SkFontConfigInterface* SkFontHost_fontconfig_ref_global();
SkFontHost_fontconfig_ref_global()55 SkFontConfigInterface* SkFontHost_fontconfig_ref_global() {
56     return RefFCI();
57 }
58 
59 ///////////////////////////////////////////////////////////////////////////////
60 
61 struct NameStyle {
NameStyleNameStyle62     NameStyle(const char* name, const SkFontStyle& style)
63         : fFamilyName(name)  // don't need to make a deep copy
64         , fStyle(style) {}
65 
66     const char* fFamilyName;
67     SkFontStyle fStyle;
68 };
69 
find_by_NameStyle(SkTypeface * cachedTypeface,const SkFontStyle & cachedStyle,void * ctx)70 static bool find_by_NameStyle(SkTypeface* cachedTypeface,
71                               const SkFontStyle& cachedStyle,
72                               void* ctx)
73 {
74     FontConfigTypeface* cachedFCTypeface = static_cast<FontConfigTypeface*>(cachedTypeface);
75     const NameStyle* nameStyle = static_cast<const NameStyle*>(ctx);
76 
77     return nameStyle->fStyle == cachedStyle &&
78            cachedFCTypeface->isFamilyName(nameStyle->fFamilyName);
79 }
80 
find_by_FontIdentity(SkTypeface * cachedTypeface,const SkFontStyle &,void * ctx)81 static bool find_by_FontIdentity(SkTypeface* cachedTypeface, const SkFontStyle&, void* ctx) {
82     typedef SkFontConfigInterface::FontIdentity FontIdentity;
83     FontConfigTypeface* cachedFCTypeface = static_cast<FontConfigTypeface*>(cachedTypeface);
84     FontIdentity* indentity = static_cast<FontIdentity*>(ctx);
85 
86     return cachedFCTypeface->getIdentity() == *indentity;
87 }
88 
LegacyCreateTypeface(const char familyName[],SkTypeface::Style style)89 SkTypeface* FontConfigTypeface::LegacyCreateTypeface(const char familyName[],
90                                                      SkTypeface::Style style)
91 {
92     SkAutoTUnref<SkFontConfigInterface> fci(RefFCI());
93     if (NULL == fci.get()) {
94         return NULL;
95     }
96 
97     // Check if requested NameStyle is in the NameStyle cache.
98     SkFontStyle requestedStyle(style);
99     NameStyle nameStyle(familyName, requestedStyle);
100     SkTypeface* face = SkTypefaceCache::FindByProcAndRef(find_by_NameStyle, &nameStyle);
101     if (face) {
102         //SkDebugf("found cached face <%s> <%s> %p [%d]\n",
103         //         familyName, ((FontConfigTypeface*)face)->getFamilyName(),
104         //         face, face->getRefCnt());
105         return face;
106     }
107 
108     SkFontConfigInterface::FontIdentity indentity;
109     SkString outFamilyName;
110     SkTypeface::Style outStyle;
111     if (!fci->matchFamilyName(familyName, style, &indentity, &outFamilyName, &outStyle)) {
112         return NULL;
113     }
114 
115     // Check if a typeface with this FontIdentity is already in the FontIdentity cache.
116     face = SkTypefaceCache::FindByProcAndRef(find_by_FontIdentity, &indentity);
117     if (!face) {
118         face = FontConfigTypeface::Create(SkFontStyle(outStyle), indentity, outFamilyName);
119         // Add this FontIdentity to the FontIdentity cache.
120         SkTypefaceCache::Add(face, requestedStyle);
121     }
122     // TODO: Ensure requested NameStyle and resolved NameStyle are both in the NameStyle cache.
123 
124     //SkDebugf("add face <%s> <%s> %p [%d]\n",
125     //         familyName, outFamilyName.c_str(),
126     //         face, face->getRefCnt());
127     return face;
128 }
129 
130 ///////////////////////////////////////////////////////////////////////////////
131 
onOpenStream(int * ttcIndex) const132 SkStreamAsset* FontConfigTypeface::onOpenStream(int* ttcIndex) const {
133     SkStreamAsset* stream = this->getLocalStream();
134     if (stream) {
135         // TODO: should have been provided by CreateFromStream()
136         *ttcIndex = 0;
137         return stream->duplicate();
138     }
139 
140     SkAutoTUnref<SkFontConfigInterface> fci(RefFCI());
141     if (NULL == fci.get()) {
142         return NULL;
143     }
144 
145     *ttcIndex = this->getIdentity().fTTCIndex;
146     return fci->openStream(this->getIdentity());
147 }
148 
onGetFamilyName(SkString * familyName) const149 void FontConfigTypeface::onGetFamilyName(SkString* familyName) const {
150     *familyName = fFamilyName;
151 }
152 
onGetFontDescriptor(SkFontDescriptor * desc,bool * isLocalStream) const153 void FontConfigTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
154                                              bool* isLocalStream) const {
155     SkString name;
156     this->getFamilyName(&name);
157     desc->setFamilyName(name.c_str());
158     desc->setFontIndex(this->getIdentity().fTTCIndex);
159     *isLocalStream = SkToBool(this->getLocalStream());
160 }
161