2 * Copyright 2008 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkFontConfigInterface.h"
9 #include "SkFontConfigTypeface.h"
10 #include "SkFontDescriptor.h"
11 #include "SkFontHost.h"
12 #include "SkFontHost_FreeType_common.h"
13 #include "SkFontStream.h"
15 #include "SkTypeface.h"
16 #include "SkTypefaceCache.h"
18 // Defined in SkFontHost_FreeType.cpp
19 bool find_name_and_attributes(SkStream* stream, SkString* name,
20 SkTypeface::Style* style, bool* isFixedWidth);
22 ///////////////////////////////////////////////////////////////////////////////
23 ///////////////////////////////////////////////////////////////////////////////
25 SK_DECLARE_STATIC_MUTEX(gFontConfigInterfaceMutex);
26 static SkFontConfigInterface* gFontConfigInterface;
28 SkFontConfigInterface* SkFontConfigInterface::RefGlobal() {
29 SkAutoMutexAcquire ac(gFontConfigInterfaceMutex);
31 return SkSafeRef(gFontConfigInterface);
34 SkFontConfigInterface* SkFontConfigInterface::SetGlobal(SkFontConfigInterface* fc) {
35 SkAutoMutexAcquire ac(gFontConfigInterfaceMutex);
37 SkRefCnt_SafeAssign(gFontConfigInterface, fc);
41 ///////////////////////////////////////////////////////////////////////////////
42 ///////////////////////////////////////////////////////////////////////////////
44 // convenience function to create the direct interface if none is installed.
45 extern SkFontConfigInterface* SkCreateDirectFontConfigInterface();
47 static SkFontConfigInterface* RefFCI() {
49 SkFontConfigInterface* fci = SkFontConfigInterface::RefGlobal();
53 fci = SkFontConfigInterface::GetSingletonDirectInterface();
54 SkFontConfigInterface::SetGlobal(fci);
58 // export this to SkFontMgr_fontconfig.cpp until this file just goes away.
59 SkFontConfigInterface* SkFontHost_fontconfig_ref_global();
60 SkFontConfigInterface* SkFontHost_fontconfig_ref_global() {
64 ///////////////////////////////////////////////////////////////////////////////
67 FindRec(const char* name, SkTypeface::Style style)
68 : fFamilyName(name) // don't need to make a deep copy
71 const char* fFamilyName;
72 SkTypeface::Style fStyle;
75 static bool find_proc(SkTypeface* face, SkTypeface::Style style, void* ctx) {
76 FontConfigTypeface* fci = (FontConfigTypeface*)face;
77 const FindRec* rec = (const FindRec*)ctx;
79 return rec->fStyle == style && fci->isFamilyName(rec->fFamilyName);
82 SkTypeface* FontConfigTypeface::LegacyCreateTypeface(
83 const SkTypeface* familyFace,
84 const char familyName[],
85 SkTypeface::Style style) {
86 SkAutoTUnref<SkFontConfigInterface> fci(RefFCI());
87 if (NULL == fci.get()) {
92 FontConfigTypeface* fct = (FontConfigTypeface*)familyFace;
93 familyName = fct->getFamilyName();
96 FindRec rec(familyName, style);
97 SkTypeface* face = SkTypefaceCache::FindByProcAndRef(find_proc, &rec);
99 // SkDebugf("found cached face <%s> <%s> %p [%d]\n", familyName, ((FontConfigTypeface*)face)->getFamilyName(), face, face->getRefCnt());
103 SkFontConfigInterface::FontIdentity indentity;
104 SkString outFamilyName;
105 SkTypeface::Style outStyle;
107 if (!fci->matchFamilyName(familyName, style,
108 &indentity, &outFamilyName, &outStyle)) {
112 // check if we, in fact, already have this. perhaps fontconfig aliased the
113 // requested name to some other name we actually have...
114 rec.fFamilyName = outFamilyName.c_str();
115 rec.fStyle = outStyle;
116 face = SkTypefaceCache::FindByProcAndRef(find_proc, &rec);
121 face = SkNEW_ARGS(FontConfigTypeface, (outStyle, indentity, outFamilyName));
122 SkTypefaceCache::Add(face, style);
123 // SkDebugf("add face <%s> <%s> %p [%d]\n", familyName, outFamilyName.c_str(), face, face->getRefCnt());
127 #ifdef SK_FONTHOST_DOES_NOT_USE_FONTMGR
129 SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
130 const char familyName[],
131 SkTypeface::Style style) {
132 return FontConfigTypeface::LegacyCreateTypeface(familyFace, familyName,
136 SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
140 const size_t length = stream->getLength();
144 if (length >= 1024 * 1024 * 1024) {
145 return NULL; // don't accept too large fonts (>= 1GB) for safety.
148 // ask freetype for reported style and if it is a fixed width font
149 SkTypeface::Style style = SkTypeface::kNormal;
150 bool isFixedWidth = false;
151 if (!find_name_and_attributes(stream, NULL, &style, &isFixedWidth)) {
155 SkTypeface* face = SkNEW_ARGS(FontConfigTypeface, (style, isFixedWidth, stream));
159 SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) {
160 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
161 return stream.get() ? CreateTypefaceFromStream(stream) : NULL;
166 ///////////////////////////////////////////////////////////////////////////////
168 SkStream* FontConfigTypeface::onOpenStream(int* ttcIndex) const {
169 SkStream* stream = this->getLocalStream();
171 // should have been provided by CreateFromStream()
174 SkAutoTUnref<SkStream> dupStream(stream->duplicate());
176 return dupStream.detach();
179 // TODO: update interface use, remove the following code in this block.
180 size_t length = stream->getLength();
182 const void* memory = stream->getMemoryBase();
183 if (NULL != memory) {
184 return new SkMemoryStream(memory, length, true);
187 SkAutoTMalloc<uint8_t> allocMemory(length);
189 if (length == stream->read(allocMemory.get(), length)) {
190 SkAutoTUnref<SkMemoryStream> copyStream(new SkMemoryStream());
191 copyStream->setMemoryOwned(allocMemory.detach(), length);
192 return copyStream.detach();
198 SkAutoTUnref<SkFontConfigInterface> fci(RefFCI());
199 if (NULL == fci.get()) {
202 stream = fci->openStream(this->getIdentity());
203 *ttcIndex = this->getIdentity().fTTCIndex;
208 void FontConfigTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
209 bool* isLocalStream) const {
210 desc->setFamilyName(this->getFamilyName());
211 *isLocalStream = SkToBool(this->getLocalStream());