gfx/2d/ScaledFontMac.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "ScaledFontMac.h"
michael@0 7 #ifdef USE_SKIA
michael@0 8 #include "PathSkia.h"
michael@0 9 #include "skia/SkPaint.h"
michael@0 10 #include "skia/SkPath.h"
michael@0 11 #include "skia/SkTypeface_mac.h"
michael@0 12 #endif
michael@0 13 #include "DrawTargetCG.h"
michael@0 14 #include <vector>
michael@0 15 #include <dlfcn.h>
michael@0 16
michael@0 17 // prototype for private API
michael@0 18 extern "C" {
michael@0 19 CGPathRef CGFontGetGlyphPath(CGFontRef fontRef, CGAffineTransform *textTransform, int unknown, CGGlyph glyph);
michael@0 20 };
michael@0 21
michael@0 22
michael@0 23 namespace mozilla {
michael@0 24 namespace gfx {
michael@0 25
michael@0 26 ScaledFontMac::CTFontDrawGlyphsFuncT* ScaledFontMac::CTFontDrawGlyphsPtr = nullptr;
michael@0 27 bool ScaledFontMac::sSymbolLookupDone = false;
michael@0 28
michael@0 29 ScaledFontMac::ScaledFontMac(CGFontRef aFont, Float aSize)
michael@0 30 : ScaledFontBase(aSize)
michael@0 31 {
michael@0 32 if (!sSymbolLookupDone) {
michael@0 33 CTFontDrawGlyphsPtr =
michael@0 34 (CTFontDrawGlyphsFuncT*)dlsym(RTLD_DEFAULT, "CTFontDrawGlyphs");
michael@0 35 sSymbolLookupDone = true;
michael@0 36 }
michael@0 37
michael@0 38 // XXX: should we be taking a reference
michael@0 39 mFont = CGFontRetain(aFont);
michael@0 40 if (CTFontDrawGlyphsPtr != nullptr) {
michael@0 41 // only create mCTFont if we're going to be using the CTFontDrawGlyphs API
michael@0 42 mCTFont = CTFontCreateWithGraphicsFont(aFont, aSize, nullptr, nullptr);
michael@0 43 } else {
michael@0 44 mCTFont = nullptr;
michael@0 45 }
michael@0 46 }
michael@0 47
michael@0 48 ScaledFontMac::~ScaledFontMac()
michael@0 49 {
michael@0 50 if (mCTFont) {
michael@0 51 CFRelease(mCTFont);
michael@0 52 }
michael@0 53 CGFontRelease(mFont);
michael@0 54 }
michael@0 55
michael@0 56 #ifdef USE_SKIA
michael@0 57 SkTypeface* ScaledFontMac::GetSkTypeface()
michael@0 58 {
michael@0 59 if (!mTypeface) {
michael@0 60 if (mCTFont) {
michael@0 61 mTypeface = SkCreateTypefaceFromCTFont(mCTFont);
michael@0 62 } else {
michael@0 63 CTFontRef fontFace = CTFontCreateWithGraphicsFont(mFont, mSize, nullptr, nullptr);
michael@0 64 mTypeface = SkCreateTypefaceFromCTFont(fontFace);
michael@0 65 CFRelease(fontFace);
michael@0 66 }
michael@0 67 }
michael@0 68 return mTypeface;
michael@0 69 }
michael@0 70 #endif
michael@0 71
michael@0 72 // private API here are the public options on OS X
michael@0 73 // CTFontCreatePathForGlyph
michael@0 74 // ATSUGlyphGetCubicPaths
michael@0 75 // we've used this in cairo sucessfully for some time.
michael@0 76 // Note: cairo dlsyms it. We could do that but maybe it's
michael@0 77 // safe just to use?
michael@0 78
michael@0 79 TemporaryRef<Path>
michael@0 80 ScaledFontMac::GetPathForGlyphs(const GlyphBuffer &aBuffer, const DrawTarget *aTarget)
michael@0 81 {
michael@0 82 if (aTarget->GetType() == BackendType::COREGRAPHICS || aTarget->GetType() == BackendType::COREGRAPHICS_ACCELERATED) {
michael@0 83 CGMutablePathRef path = CGPathCreateMutable();
michael@0 84
michael@0 85 for (unsigned int i = 0; i < aBuffer.mNumGlyphs; i++) {
michael@0 86 // XXX: we could probably fold both of these transforms together to avoid extra work
michael@0 87 CGAffineTransform flip = CGAffineTransformMakeScale(1, -1);
michael@0 88 CGPathRef glyphPath = ::CGFontGetGlyphPath(mFont, &flip, 0, aBuffer.mGlyphs[i].mIndex);
michael@0 89
michael@0 90 CGAffineTransform matrix = CGAffineTransformMake(mSize, 0, 0, mSize,
michael@0 91 aBuffer.mGlyphs[i].mPosition.x,
michael@0 92 aBuffer.mGlyphs[i].mPosition.y);
michael@0 93 CGPathAddPath(path, &matrix, glyphPath);
michael@0 94 CGPathRelease(glyphPath);
michael@0 95 }
michael@0 96 TemporaryRef<Path> ret = new PathCG(path, FillRule::FILL_WINDING);
michael@0 97 CGPathRelease(path);
michael@0 98 return ret;
michael@0 99 } else {
michael@0 100 return ScaledFontBase::GetPathForGlyphs(aBuffer, aTarget);
michael@0 101 }
michael@0 102 }
michael@0 103
michael@0 104 void
michael@0 105 ScaledFontMac::CopyGlyphsToBuilder(const GlyphBuffer &aBuffer, PathBuilder *aBuilder, BackendType aBackendType, const Matrix *aTransformHint)
michael@0 106 {
michael@0 107 if (!(aBackendType == BackendType::COREGRAPHICS || aBackendType == BackendType::COREGRAPHICS_ACCELERATED)) {
michael@0 108 ScaledFontBase::CopyGlyphsToBuilder(aBuffer, aBuilder, aBackendType, aTransformHint);
michael@0 109 return;
michael@0 110 }
michael@0 111
michael@0 112 PathBuilderCG *pathBuilderCG =
michael@0 113 static_cast<PathBuilderCG*>(aBuilder);
michael@0 114 // XXX: check builder type
michael@0 115 for (unsigned int i = 0; i < aBuffer.mNumGlyphs; i++) {
michael@0 116 // XXX: we could probably fold both of these transforms together to avoid extra work
michael@0 117 CGAffineTransform flip = CGAffineTransformMakeScale(1, -1);
michael@0 118 CGPathRef glyphPath = ::CGFontGetGlyphPath(mFont, &flip, 0, aBuffer.mGlyphs[i].mIndex);
michael@0 119
michael@0 120 CGAffineTransform matrix = CGAffineTransformMake(mSize, 0, 0, mSize,
michael@0 121 aBuffer.mGlyphs[i].mPosition.x,
michael@0 122 aBuffer.mGlyphs[i].mPosition.y);
michael@0 123 CGPathAddPath(pathBuilderCG->mCGPath, &matrix, glyphPath);
michael@0 124 CGPathRelease(glyphPath);
michael@0 125 }
michael@0 126 }
michael@0 127
michael@0 128 uint32_t
michael@0 129 CalcTableChecksum(const uint32_t *tableStart, uint32_t length, bool skipChecksumAdjust = false)
michael@0 130 {
michael@0 131 uint32_t sum = 0L;
michael@0 132 const uint32_t *table = tableStart;
michael@0 133 const uint32_t *end = table+((length+3) & ~3) / sizeof(uint32_t);
michael@0 134 while (table < end) {
michael@0 135 if (skipChecksumAdjust && (table - tableStart) == 2) {
michael@0 136 table++;
michael@0 137 } else {
michael@0 138 sum += CFSwapInt32BigToHost(*table++);
michael@0 139 }
michael@0 140 }
michael@0 141 return sum;
michael@0 142 }
michael@0 143
michael@0 144 struct TableRecord {
michael@0 145 uint32_t tag;
michael@0 146 uint32_t checkSum;
michael@0 147 uint32_t offset;
michael@0 148 uint32_t length;
michael@0 149 CFDataRef data;
michael@0 150 };
michael@0 151
michael@0 152 int maxPow2LessThan(int a)
michael@0 153 {
michael@0 154 int x = 1;
michael@0 155 int shift = 0;
michael@0 156 while ((x<<(shift+1)) < a) {
michael@0 157 shift++;
michael@0 158 }
michael@0 159 return shift;
michael@0 160 }
michael@0 161
michael@0 162 struct writeBuf
michael@0 163 {
michael@0 164 writeBuf(int size)
michael@0 165 {
michael@0 166 this->data = new unsigned char [size];
michael@0 167 this->offset = 0;
michael@0 168 }
michael@0 169 ~writeBuf() {
michael@0 170 delete this->data;
michael@0 171 }
michael@0 172
michael@0 173 template <class T>
michael@0 174 void writeElement(T a)
michael@0 175 {
michael@0 176 *reinterpret_cast<T*>(&this->data[this->offset]) = a;
michael@0 177 this->offset += sizeof(T);
michael@0 178 }
michael@0 179
michael@0 180 void writeMem(const void *data, unsigned long length)
michael@0 181 {
michael@0 182 memcpy(&this->data[this->offset], data, length);
michael@0 183 this->offset += length;
michael@0 184 }
michael@0 185
michael@0 186 void align()
michael@0 187 {
michael@0 188 while (this->offset & 3) {
michael@0 189 this->data[this->offset] = 0;
michael@0 190 this->offset++;
michael@0 191 }
michael@0 192 }
michael@0 193
michael@0 194 unsigned char *data;
michael@0 195 int offset;
michael@0 196 };
michael@0 197
michael@0 198 bool
michael@0 199 ScaledFontMac::GetFontFileData(FontFileDataOutput aDataCallback, void *aBaton)
michael@0 200 {
michael@0 201 // We'll reconstruct a TTF font from the tables we can get from the CGFont
michael@0 202 CFArrayRef tags = CGFontCopyTableTags(mFont);
michael@0 203 CFIndex count = CFArrayGetCount(tags);
michael@0 204
michael@0 205 TableRecord *records = new TableRecord[count];
michael@0 206 uint32_t offset = 0;
michael@0 207 offset += sizeof(uint32_t)*3;
michael@0 208 offset += sizeof(uint32_t)*4*count;
michael@0 209 bool CFF = false;
michael@0 210 for (CFIndex i = 0; i<count; i++) {
michael@0 211 uint32_t tag = (uint32_t)(uintptr_t)CFArrayGetValueAtIndex(tags, i);
michael@0 212 if (tag == 0x43464620) // 'CFF '
michael@0 213 CFF = true;
michael@0 214 CFDataRef data = CGFontCopyTableForTag(mFont, tag);
michael@0 215 records[i].tag = tag;
michael@0 216 records[i].offset = offset;
michael@0 217 records[i].data = data;
michael@0 218 records[i].length = CFDataGetLength(data);
michael@0 219 bool skipChecksumAdjust = (tag == 0x68656164); // 'head'
michael@0 220 records[i].checkSum = CalcTableChecksum(reinterpret_cast<const uint32_t*>(CFDataGetBytePtr(data)),
michael@0 221 records[i].length, skipChecksumAdjust);
michael@0 222 offset += records[i].length;
michael@0 223 // 32 bit align the tables
michael@0 224 offset = (offset + 3) & ~3;
michael@0 225 }
michael@0 226 CFRelease(tags);
michael@0 227
michael@0 228 struct writeBuf buf(offset);
michael@0 229 // write header/offset table
michael@0 230 if (CFF) {
michael@0 231 buf.writeElement(CFSwapInt32HostToBig(0x4f54544f));
michael@0 232 } else {
michael@0 233 buf.writeElement(CFSwapInt32HostToBig(0x00010000));
michael@0 234 }
michael@0 235 buf.writeElement(CFSwapInt16HostToBig(count));
michael@0 236 buf.writeElement(CFSwapInt16HostToBig((1<<maxPow2LessThan(count))*16));
michael@0 237 buf.writeElement(CFSwapInt16HostToBig(maxPow2LessThan(count)));
michael@0 238 buf.writeElement(CFSwapInt16HostToBig(count*16-((1<<maxPow2LessThan(count))*16)));
michael@0 239
michael@0 240 // write table record entries
michael@0 241 for (CFIndex i = 0; i<count; i++) {
michael@0 242 buf.writeElement(CFSwapInt32HostToBig(records[i].tag));
michael@0 243 buf.writeElement(CFSwapInt32HostToBig(records[i].checkSum));
michael@0 244 buf.writeElement(CFSwapInt32HostToBig(records[i].offset));
michael@0 245 buf.writeElement(CFSwapInt32HostToBig(records[i].length));
michael@0 246 }
michael@0 247
michael@0 248 // write tables
michael@0 249 int checkSumAdjustmentOffset = 0;
michael@0 250 for (CFIndex i = 0; i<count; i++) {
michael@0 251 if (records[i].tag == 0x68656164) {
michael@0 252 checkSumAdjustmentOffset = buf.offset + 2*4;
michael@0 253 }
michael@0 254 buf.writeMem(CFDataGetBytePtr(records[i].data), CFDataGetLength(records[i].data));
michael@0 255 buf.align();
michael@0 256 CFRelease(records[i].data);
michael@0 257 }
michael@0 258 delete[] records;
michael@0 259
michael@0 260 // clear the checksumAdjust field before checksumming the whole font
michael@0 261 memset(&buf.data[checkSumAdjustmentOffset], 0, sizeof(uint32_t));
michael@0 262 uint32_t fontChecksum = CFSwapInt32HostToBig(0xb1b0afba - CalcTableChecksum(reinterpret_cast<const uint32_t*>(buf.data), offset));
michael@0 263 // set checkSumAdjust to the computed checksum
michael@0 264 memcpy(&buf.data[checkSumAdjustmentOffset], &fontChecksum, sizeof(fontChecksum));
michael@0 265
michael@0 266 // we always use an index of 0
michael@0 267 aDataCallback(buf.data, buf.offset, 0, mSize, aBaton);
michael@0 268
michael@0 269 return true;
michael@0 270
michael@0 271 }
michael@0 272
michael@0 273 }
michael@0 274 }

mercurial