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 ///////////////////////////////////////////////////////////////////////////////
19 ///////////////////////////////////////////////////////////////////////////////
21 SK_DECLARE_STATIC_MUTEX(gFontConfigInterfaceMutex);
22 static SkFontConfigInterface* gFontConfigInterface;
24 SkFontConfigInterface* SkFontConfigInterface::RefGlobal() {
25 SkAutoMutexAcquire ac(gFontConfigInterfaceMutex);
27 return SkSafeRef(gFontConfigInterface);
30 SkFontConfigInterface* SkFontConfigInterface::SetGlobal(SkFontConfigInterface* fc) {
31 SkAutoMutexAcquire ac(gFontConfigInterfaceMutex);
33 SkRefCnt_SafeAssign(gFontConfigInterface, fc);
37 ///////////////////////////////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////////////
40 // convenience function to create the direct interface if none is installed.
41 extern SkFontConfigInterface* SkCreateDirectFontConfigInterface();
43 static SkFontConfigInterface* RefFCI() {
45 SkFontConfigInterface* fci = SkFontConfigInterface::RefGlobal();
49 fci = SkFontConfigInterface::GetSingletonDirectInterface(&gFontConfigInterfaceMutex);
50 SkFontConfigInterface::SetGlobal(fci);
54 // export this to SkFontMgr_fontconfig.cpp until this file just goes away.
55 SkFontConfigInterface* SkFontHost_fontconfig_ref_global();
56 SkFontConfigInterface* SkFontHost_fontconfig_ref_global() {
60 ///////////////////////////////////////////////////////////////////////////////
63 FindRec(const char* name, SkTypeface::Style style)
64 : fFamilyName(name) // don't need to make a deep copy
67 const char* fFamilyName;
68 SkTypeface::Style fStyle;
71 static bool find_proc(SkTypeface* face, SkTypeface::Style style, void* ctx) {
72 FontConfigTypeface* fci = (FontConfigTypeface*)face;
73 const FindRec* rec = (const FindRec*)ctx;
75 return rec->fStyle == style && fci->isFamilyName(rec->fFamilyName);
78 SkTypeface* FontConfigTypeface::LegacyCreateTypeface(
79 const SkTypeface* familyFace,
80 const char familyName[],
81 SkTypeface::Style style) {
82 SkAutoTUnref<SkFontConfigInterface> fci(RefFCI());
83 if (NULL == fci.get()) {
88 FontConfigTypeface* fct = (FontConfigTypeface*)familyFace;
89 familyName = fct->getFamilyName();
92 FindRec rec(familyName, style);
93 SkTypeface* face = SkTypefaceCache::FindByProcAndRef(find_proc, &rec);
95 // SkDebugf("found cached face <%s> <%s> %p [%d]\n", familyName, ((FontConfigTypeface*)face)->getFamilyName(), face, face->getRefCnt());
99 SkFontConfigInterface::FontIdentity indentity;
100 SkString outFamilyName;
101 SkTypeface::Style outStyle;
103 if (!fci->matchFamilyName(familyName, style,
104 &indentity, &outFamilyName, &outStyle)) {
108 // check if we, in fact, already have this. perhaps fontconfig aliased the
109 // requested name to some other name we actually have...
110 rec.fFamilyName = outFamilyName.c_str();
111 rec.fStyle = outStyle;
112 face = SkTypefaceCache::FindByProcAndRef(find_proc, &rec);
117 face = FontConfigTypeface::Create(outStyle, indentity, outFamilyName);
118 SkTypefaceCache::Add(face, style);
119 // SkDebugf("add face <%s> <%s> %p [%d]\n", familyName, outFamilyName.c_str(), face, face->getRefCnt());
123 ///////////////////////////////////////////////////////////////////////////////
125 SkStream* FontConfigTypeface::onOpenStream(int* ttcIndex) const {
126 SkStream* stream = this->getLocalStream();
128 // should have been provided by CreateFromStream()
131 SkAutoTUnref<SkStream> dupStream(stream->duplicate());
133 return dupStream.detach();
136 // TODO: update interface use, remove the following code in this block.
137 size_t length = stream->getLength();
139 const void* memory = stream->getMemoryBase();
141 return new SkMemoryStream(memory, length, true);
144 SkAutoTMalloc<uint8_t> allocMemory(length);
146 if (length == stream->read(allocMemory.get(), length)) {
147 SkAutoTUnref<SkMemoryStream> copyStream(new SkMemoryStream());
148 copyStream->setMemoryOwned(allocMemory.detach(), length);
149 return copyStream.detach();
155 SkAutoTUnref<SkFontConfigInterface> fci(RefFCI());
156 if (NULL == fci.get()) {
159 stream = fci->openStream(this->getIdentity());
160 *ttcIndex = this->getIdentity().fTTCIndex;
165 void FontConfigTypeface::onGetFamilyName(SkString* familyName) const {
166 *familyName = this->getFamilyName();
169 void FontConfigTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
170 bool* isLocalStream) const {
171 desc->setFamilyName(this->getFamilyName());
172 desc->setFontIndex(this->getIdentity().fTTCIndex);
173 *isLocalStream = SkToBool(this->getLocalStream());