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 #ifdef SK_FONTHOST_DOES_NOT_USE_FONTMGR
125 SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
126 const char familyName[],
127 SkTypeface::Style style) {
128 return FontConfigTypeface::LegacyCreateTypeface(familyFace, familyName,
132 SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
136 const size_t length = stream->getLength();
140 if (length >= 1024 * 1024 * 1024) {
141 return NULL; // don't accept too large fonts (>= 1GB) for safety.
144 // ask freetype for reported style and if it is a fixed width font
145 SkTypeface::Style style = SkTypeface::kNormal;
146 bool isFixedWidth = false;
147 if (!SkTypeface_FreeType::ScanFont(stream, 0, NULL, &style, &isFixedWidth)) {
151 SkTypeface* face = SkNEW_ARGS(FontConfigTypeface, (style, isFixedWidth, stream));
155 SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) {
156 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
157 return stream.get() ? CreateTypefaceFromStream(stream) : NULL;
162 ///////////////////////////////////////////////////////////////////////////////
164 SkStream* FontConfigTypeface::onOpenStream(int* ttcIndex) const {
165 SkStream* stream = this->getLocalStream();
167 // should have been provided by CreateFromStream()
170 SkAutoTUnref<SkStream> dupStream(stream->duplicate());
172 return dupStream.detach();
175 // TODO: update interface use, remove the following code in this block.
176 size_t length = stream->getLength();
178 const void* memory = stream->getMemoryBase();
179 if (NULL != memory) {
180 return new SkMemoryStream(memory, length, true);
183 SkAutoTMalloc<uint8_t> allocMemory(length);
185 if (length == stream->read(allocMemory.get(), length)) {
186 SkAutoTUnref<SkMemoryStream> copyStream(new SkMemoryStream());
187 copyStream->setMemoryOwned(allocMemory.detach(), length);
188 return copyStream.detach();
194 SkAutoTUnref<SkFontConfigInterface> fci(RefFCI());
195 if (NULL == fci.get()) {
198 stream = fci->openStream(this->getIdentity());
199 *ttcIndex = this->getIdentity().fTTCIndex;
204 void FontConfigTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
205 bool* isLocalStream) const {
206 desc->setFamilyName(this->getFamilyName());
207 *isLocalStream = SkToBool(this->getLocalStream());