gfx/skia/trunk/src/ports/SkFontHost_fontconfig.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 * Copyright 2008 Google Inc.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #include "SkFontConfigInterface.h"
michael@0 9 #include "SkFontConfigTypeface.h"
michael@0 10 #include "SkFontDescriptor.h"
michael@0 11 #include "SkFontHost.h"
michael@0 12 #include "SkFontHost_FreeType_common.h"
michael@0 13 #include "SkFontStream.h"
michael@0 14 #include "SkStream.h"
michael@0 15 #include "SkTypeface.h"
michael@0 16 #include "SkTypefaceCache.h"
michael@0 17
michael@0 18 // Defined in SkFontHost_FreeType.cpp
michael@0 19 bool find_name_and_attributes(SkStream* stream, SkString* name,
michael@0 20 SkTypeface::Style* style, bool* isFixedWidth);
michael@0 21
michael@0 22 ///////////////////////////////////////////////////////////////////////////////
michael@0 23 ///////////////////////////////////////////////////////////////////////////////
michael@0 24
michael@0 25 SK_DECLARE_STATIC_MUTEX(gFontConfigInterfaceMutex);
michael@0 26 static SkFontConfigInterface* gFontConfigInterface;
michael@0 27
michael@0 28 SkFontConfigInterface* SkFontConfigInterface::RefGlobal() {
michael@0 29 SkAutoMutexAcquire ac(gFontConfigInterfaceMutex);
michael@0 30
michael@0 31 return SkSafeRef(gFontConfigInterface);
michael@0 32 }
michael@0 33
michael@0 34 SkFontConfigInterface* SkFontConfigInterface::SetGlobal(SkFontConfigInterface* fc) {
michael@0 35 SkAutoMutexAcquire ac(gFontConfigInterfaceMutex);
michael@0 36
michael@0 37 SkRefCnt_SafeAssign(gFontConfigInterface, fc);
michael@0 38 return fc;
michael@0 39 }
michael@0 40
michael@0 41 ///////////////////////////////////////////////////////////////////////////////
michael@0 42 ///////////////////////////////////////////////////////////////////////////////
michael@0 43
michael@0 44 // convenience function to create the direct interface if none is installed.
michael@0 45 extern SkFontConfigInterface* SkCreateDirectFontConfigInterface();
michael@0 46
michael@0 47 static SkFontConfigInterface* RefFCI() {
michael@0 48 for (;;) {
michael@0 49 SkFontConfigInterface* fci = SkFontConfigInterface::RefGlobal();
michael@0 50 if (fci) {
michael@0 51 return fci;
michael@0 52 }
michael@0 53 fci = SkFontConfigInterface::GetSingletonDirectInterface();
michael@0 54 SkFontConfigInterface::SetGlobal(fci);
michael@0 55 }
michael@0 56 }
michael@0 57
michael@0 58 // export this to SkFontMgr_fontconfig.cpp until this file just goes away.
michael@0 59 SkFontConfigInterface* SkFontHost_fontconfig_ref_global();
michael@0 60 SkFontConfigInterface* SkFontHost_fontconfig_ref_global() {
michael@0 61 return RefFCI();
michael@0 62 }
michael@0 63
michael@0 64 ///////////////////////////////////////////////////////////////////////////////
michael@0 65
michael@0 66 struct FindRec {
michael@0 67 FindRec(const char* name, SkTypeface::Style style)
michael@0 68 : fFamilyName(name) // don't need to make a deep copy
michael@0 69 , fStyle(style) {}
michael@0 70
michael@0 71 const char* fFamilyName;
michael@0 72 SkTypeface::Style fStyle;
michael@0 73 };
michael@0 74
michael@0 75 static bool find_proc(SkTypeface* face, SkTypeface::Style style, void* ctx) {
michael@0 76 FontConfigTypeface* fci = (FontConfigTypeface*)face;
michael@0 77 const FindRec* rec = (const FindRec*)ctx;
michael@0 78
michael@0 79 return rec->fStyle == style && fci->isFamilyName(rec->fFamilyName);
michael@0 80 }
michael@0 81
michael@0 82 SkTypeface* FontConfigTypeface::LegacyCreateTypeface(
michael@0 83 const SkTypeface* familyFace,
michael@0 84 const char familyName[],
michael@0 85 SkTypeface::Style style) {
michael@0 86 SkAutoTUnref<SkFontConfigInterface> fci(RefFCI());
michael@0 87 if (NULL == fci.get()) {
michael@0 88 return NULL;
michael@0 89 }
michael@0 90
michael@0 91 if (familyFace) {
michael@0 92 FontConfigTypeface* fct = (FontConfigTypeface*)familyFace;
michael@0 93 familyName = fct->getFamilyName();
michael@0 94 }
michael@0 95
michael@0 96 FindRec rec(familyName, style);
michael@0 97 SkTypeface* face = SkTypefaceCache::FindByProcAndRef(find_proc, &rec);
michael@0 98 if (face) {
michael@0 99 // SkDebugf("found cached face <%s> <%s> %p [%d]\n", familyName, ((FontConfigTypeface*)face)->getFamilyName(), face, face->getRefCnt());
michael@0 100 return face;
michael@0 101 }
michael@0 102
michael@0 103 SkFontConfigInterface::FontIdentity indentity;
michael@0 104 SkString outFamilyName;
michael@0 105 SkTypeface::Style outStyle;
michael@0 106
michael@0 107 if (!fci->matchFamilyName(familyName, style,
michael@0 108 &indentity, &outFamilyName, &outStyle)) {
michael@0 109 return NULL;
michael@0 110 }
michael@0 111
michael@0 112 // check if we, in fact, already have this. perhaps fontconfig aliased the
michael@0 113 // requested name to some other name we actually have...
michael@0 114 rec.fFamilyName = outFamilyName.c_str();
michael@0 115 rec.fStyle = outStyle;
michael@0 116 face = SkTypefaceCache::FindByProcAndRef(find_proc, &rec);
michael@0 117 if (face) {
michael@0 118 return face;
michael@0 119 }
michael@0 120
michael@0 121 face = SkNEW_ARGS(FontConfigTypeface, (outStyle, indentity, outFamilyName));
michael@0 122 SkTypefaceCache::Add(face, style);
michael@0 123 // SkDebugf("add face <%s> <%s> %p [%d]\n", familyName, outFamilyName.c_str(), face, face->getRefCnt());
michael@0 124 return face;
michael@0 125 }
michael@0 126
michael@0 127 #ifdef SK_FONTHOST_DOES_NOT_USE_FONTMGR
michael@0 128
michael@0 129 SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
michael@0 130 const char familyName[],
michael@0 131 SkTypeface::Style style) {
michael@0 132 return FontConfigTypeface::LegacyCreateTypeface(familyFace, familyName,
michael@0 133 style);
michael@0 134 }
michael@0 135
michael@0 136 SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
michael@0 137 if (!stream) {
michael@0 138 return NULL;
michael@0 139 }
michael@0 140 const size_t length = stream->getLength();
michael@0 141 if (!length) {
michael@0 142 return NULL;
michael@0 143 }
michael@0 144 if (length >= 1024 * 1024 * 1024) {
michael@0 145 return NULL; // don't accept too large fonts (>= 1GB) for safety.
michael@0 146 }
michael@0 147
michael@0 148 // ask freetype for reported style and if it is a fixed width font
michael@0 149 SkTypeface::Style style = SkTypeface::kNormal;
michael@0 150 bool isFixedWidth = false;
michael@0 151 if (!find_name_and_attributes(stream, NULL, &style, &isFixedWidth)) {
michael@0 152 return NULL;
michael@0 153 }
michael@0 154
michael@0 155 SkTypeface* face = SkNEW_ARGS(FontConfigTypeface, (style, isFixedWidth, stream));
michael@0 156 return face;
michael@0 157 }
michael@0 158
michael@0 159 SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) {
michael@0 160 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
michael@0 161 return stream.get() ? CreateTypefaceFromStream(stream) : NULL;
michael@0 162 }
michael@0 163
michael@0 164 #endif
michael@0 165
michael@0 166 ///////////////////////////////////////////////////////////////////////////////
michael@0 167
michael@0 168 SkStream* FontConfigTypeface::onOpenStream(int* ttcIndex) const {
michael@0 169 SkStream* stream = this->getLocalStream();
michael@0 170 if (stream) {
michael@0 171 // should have been provided by CreateFromStream()
michael@0 172 *ttcIndex = 0;
michael@0 173
michael@0 174 SkAutoTUnref<SkStream> dupStream(stream->duplicate());
michael@0 175 if (dupStream) {
michael@0 176 return dupStream.detach();
michael@0 177 }
michael@0 178
michael@0 179 // TODO: update interface use, remove the following code in this block.
michael@0 180 size_t length = stream->getLength();
michael@0 181
michael@0 182 const void* memory = stream->getMemoryBase();
michael@0 183 if (NULL != memory) {
michael@0 184 return new SkMemoryStream(memory, length, true);
michael@0 185 }
michael@0 186
michael@0 187 SkAutoTMalloc<uint8_t> allocMemory(length);
michael@0 188 stream->rewind();
michael@0 189 if (length == stream->read(allocMemory.get(), length)) {
michael@0 190 SkAutoTUnref<SkMemoryStream> copyStream(new SkMemoryStream());
michael@0 191 copyStream->setMemoryOwned(allocMemory.detach(), length);
michael@0 192 return copyStream.detach();
michael@0 193 }
michael@0 194
michael@0 195 stream->rewind();
michael@0 196 stream->ref();
michael@0 197 } else {
michael@0 198 SkAutoTUnref<SkFontConfigInterface> fci(RefFCI());
michael@0 199 if (NULL == fci.get()) {
michael@0 200 return NULL;
michael@0 201 }
michael@0 202 stream = fci->openStream(this->getIdentity());
michael@0 203 *ttcIndex = this->getIdentity().fTTCIndex;
michael@0 204 }
michael@0 205 return stream;
michael@0 206 }
michael@0 207
michael@0 208 void FontConfigTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
michael@0 209 bool* isLocalStream) const {
michael@0 210 desc->setFamilyName(this->getFamilyName());
michael@0 211 *isLocalStream = SkToBool(this->getLocalStream());
michael@0 212 }

mercurial