gfx/2d/ScaledFontDWrite.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 "ScaledFontDWrite.h"
michael@0 7 #include "PathD2D.h"
michael@0 8 #include "DrawTargetD2D.h"
michael@0 9 #include "Logging.h"
michael@0 10
michael@0 11 #include <vector>
michael@0 12
michael@0 13 namespace mozilla {
michael@0 14 namespace gfx {
michael@0 15
michael@0 16 struct ffReferenceKey
michael@0 17 {
michael@0 18 uint8_t *mData;
michael@0 19 uint32_t mSize;
michael@0 20 };
michael@0 21
michael@0 22 class DWriteFontFileLoader : public IDWriteFontFileLoader
michael@0 23 {
michael@0 24 public:
michael@0 25 DWriteFontFileLoader()
michael@0 26 {
michael@0 27 }
michael@0 28
michael@0 29 // IUnknown interface
michael@0 30 IFACEMETHOD(QueryInterface)(IID const& iid, OUT void** ppObject)
michael@0 31 {
michael@0 32 if (iid == __uuidof(IDWriteFontFileLoader)) {
michael@0 33 *ppObject = static_cast<IDWriteFontFileLoader*>(this);
michael@0 34 return S_OK;
michael@0 35 } else if (iid == __uuidof(IUnknown)) {
michael@0 36 *ppObject = static_cast<IUnknown*>(this);
michael@0 37 return S_OK;
michael@0 38 } else {
michael@0 39 return E_NOINTERFACE;
michael@0 40 }
michael@0 41 }
michael@0 42
michael@0 43 IFACEMETHOD_(ULONG, AddRef)()
michael@0 44 {
michael@0 45 return 1;
michael@0 46 }
michael@0 47
michael@0 48 IFACEMETHOD_(ULONG, Release)()
michael@0 49 {
michael@0 50 return 1;
michael@0 51 }
michael@0 52
michael@0 53 // IDWriteFontFileLoader methods
michael@0 54 /**
michael@0 55 * Important! Note the key here -has- to be a pointer to an
michael@0 56 * ffReferenceKey object.
michael@0 57 */
michael@0 58 virtual HRESULT STDMETHODCALLTYPE
michael@0 59 CreateStreamFromKey(void const* fontFileReferenceKey,
michael@0 60 UINT32 fontFileReferenceKeySize,
michael@0 61 OUT IDWriteFontFileStream** fontFileStream);
michael@0 62
michael@0 63 /**
michael@0 64 * Gets the singleton loader instance. Note that when using this font
michael@0 65 * loader, the key must be a pointer to an FallibleTArray<uint8_t>. This
michael@0 66 * array will be empty when the function returns.
michael@0 67 */
michael@0 68 static IDWriteFontFileLoader* Instance()
michael@0 69 {
michael@0 70 if (!mInstance) {
michael@0 71 mInstance = new DWriteFontFileLoader();
michael@0 72 DrawTargetD2D::GetDWriteFactory()->
michael@0 73 RegisterFontFileLoader(mInstance);
michael@0 74 }
michael@0 75 return mInstance;
michael@0 76 }
michael@0 77
michael@0 78 private:
michael@0 79 static IDWriteFontFileLoader* mInstance;
michael@0 80 };
michael@0 81
michael@0 82 class DWriteFontFileStream : public IDWriteFontFileStream
michael@0 83 {
michael@0 84 public:
michael@0 85 /**
michael@0 86 * Used by the FontFileLoader to create a new font stream,
michael@0 87 * this font stream is created from data in memory. The memory
michael@0 88 * passed may be released after object creation, it will be
michael@0 89 * copied internally.
michael@0 90 *
michael@0 91 * @param aData Font data
michael@0 92 */
michael@0 93 DWriteFontFileStream(uint8_t *aData, uint32_t aSize);
michael@0 94 ~DWriteFontFileStream();
michael@0 95
michael@0 96 // IUnknown interface
michael@0 97 IFACEMETHOD(QueryInterface)(IID const& iid, OUT void** ppObject)
michael@0 98 {
michael@0 99 if (iid == __uuidof(IDWriteFontFileStream)) {
michael@0 100 *ppObject = static_cast<IDWriteFontFileStream*>(this);
michael@0 101 return S_OK;
michael@0 102 } else if (iid == __uuidof(IUnknown)) {
michael@0 103 *ppObject = static_cast<IUnknown*>(this);
michael@0 104 return S_OK;
michael@0 105 } else {
michael@0 106 return E_NOINTERFACE;
michael@0 107 }
michael@0 108 }
michael@0 109
michael@0 110 IFACEMETHOD_(ULONG, AddRef)()
michael@0 111 {
michael@0 112 ++mRefCnt;
michael@0 113 return mRefCnt;
michael@0 114 }
michael@0 115
michael@0 116 IFACEMETHOD_(ULONG, Release)()
michael@0 117 {
michael@0 118 --mRefCnt;
michael@0 119 if (mRefCnt == 0) {
michael@0 120 delete this;
michael@0 121 return 0;
michael@0 122 }
michael@0 123 return mRefCnt;
michael@0 124 }
michael@0 125
michael@0 126 // IDWriteFontFileStream methods
michael@0 127 virtual HRESULT STDMETHODCALLTYPE ReadFileFragment(void const** fragmentStart,
michael@0 128 UINT64 fileOffset,
michael@0 129 UINT64 fragmentSize,
michael@0 130 OUT void** fragmentContext);
michael@0 131
michael@0 132 virtual void STDMETHODCALLTYPE ReleaseFileFragment(void* fragmentContext);
michael@0 133
michael@0 134 virtual HRESULT STDMETHODCALLTYPE GetFileSize(OUT UINT64* fileSize);
michael@0 135
michael@0 136 virtual HRESULT STDMETHODCALLTYPE GetLastWriteTime(OUT UINT64* lastWriteTime);
michael@0 137
michael@0 138 private:
michael@0 139 std::vector<uint8_t> mData;
michael@0 140 uint32_t mRefCnt;
michael@0 141 };
michael@0 142
michael@0 143 static BYTE
michael@0 144 GetSystemTextQuality()
michael@0 145 {
michael@0 146 BOOL font_smoothing;
michael@0 147 UINT smoothing_type;
michael@0 148
michael@0 149 if (!SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, &font_smoothing, 0)) {
michael@0 150 return DEFAULT_QUALITY;
michael@0 151 }
michael@0 152
michael@0 153 if (font_smoothing) {
michael@0 154 if (!SystemParametersInfo(SPI_GETFONTSMOOTHINGTYPE,
michael@0 155 0, &smoothing_type, 0)) {
michael@0 156 return DEFAULT_QUALITY;
michael@0 157 }
michael@0 158
michael@0 159 if (smoothing_type == FE_FONTSMOOTHINGCLEARTYPE) {
michael@0 160 return CLEARTYPE_QUALITY;
michael@0 161 }
michael@0 162
michael@0 163 return ANTIALIASED_QUALITY;
michael@0 164 }
michael@0 165
michael@0 166 return DEFAULT_QUALITY;
michael@0 167 }
michael@0 168
michael@0 169 #define GASP_TAG 0x70736167
michael@0 170 #define GASP_DOGRAY 0x2
michael@0 171
michael@0 172 static inline unsigned short
michael@0 173 readShort(const char *aBuf)
michael@0 174 {
michael@0 175 return (*aBuf << 8) | *(aBuf + 1);
michael@0 176 }
michael@0 177
michael@0 178 static bool
michael@0 179 DoGrayscale(IDWriteFontFace *aDWFace, Float ppem)
michael@0 180 {
michael@0 181 void *tableContext;
michael@0 182 char *tableData;
michael@0 183 UINT32 tableSize;
michael@0 184 BOOL exists;
michael@0 185 aDWFace->TryGetFontTable(GASP_TAG, (const void**)&tableData, &tableSize, &tableContext, &exists);
michael@0 186
michael@0 187 if (exists) {
michael@0 188 if (tableSize < 4) {
michael@0 189 aDWFace->ReleaseFontTable(tableContext);
michael@0 190 return true;
michael@0 191 }
michael@0 192 struct gaspRange {
michael@0 193 unsigned short maxPPEM; // Stored big-endian
michael@0 194 unsigned short behavior; // Stored big-endian
michael@0 195 };
michael@0 196 unsigned short numRanges = readShort(tableData + 2);
michael@0 197 if (tableSize < (UINT)4 + numRanges * 4) {
michael@0 198 aDWFace->ReleaseFontTable(tableContext);
michael@0 199 return true;
michael@0 200 }
michael@0 201 gaspRange *ranges = (gaspRange *)(tableData + 4);
michael@0 202 for (int i = 0; i < numRanges; i++) {
michael@0 203 if (readShort((char*)&ranges[i].maxPPEM) > ppem) {
michael@0 204 if (!(readShort((char*)&ranges[i].behavior) & GASP_DOGRAY)) {
michael@0 205 aDWFace->ReleaseFontTable(tableContext);
michael@0 206 return false;
michael@0 207 }
michael@0 208 break;
michael@0 209 }
michael@0 210 }
michael@0 211 aDWFace->ReleaseFontTable(tableContext);
michael@0 212 }
michael@0 213 return true;
michael@0 214 }
michael@0 215
michael@0 216 IDWriteFontFileLoader* DWriteFontFileLoader::mInstance = nullptr;
michael@0 217
michael@0 218 HRESULT STDMETHODCALLTYPE
michael@0 219 DWriteFontFileLoader::CreateStreamFromKey(const void *fontFileReferenceKey,
michael@0 220 UINT32 fontFileReferenceKeySize,
michael@0 221 IDWriteFontFileStream **fontFileStream)
michael@0 222 {
michael@0 223 if (!fontFileReferenceKey || !fontFileStream) {
michael@0 224 return E_POINTER;
michael@0 225 }
michael@0 226
michael@0 227 const ffReferenceKey *key = static_cast<const ffReferenceKey*>(fontFileReferenceKey);
michael@0 228 *fontFileStream =
michael@0 229 new DWriteFontFileStream(key->mData, key->mSize);
michael@0 230
michael@0 231 if (!*fontFileStream) {
michael@0 232 return E_OUTOFMEMORY;
michael@0 233 }
michael@0 234 (*fontFileStream)->AddRef();
michael@0 235 return S_OK;
michael@0 236 }
michael@0 237
michael@0 238 DWriteFontFileStream::DWriteFontFileStream(uint8_t *aData, uint32_t aSize)
michael@0 239 : mRefCnt(0)
michael@0 240 {
michael@0 241 mData.resize(aSize);
michael@0 242 memcpy(&mData.front(), aData, aSize);
michael@0 243 }
michael@0 244
michael@0 245 DWriteFontFileStream::~DWriteFontFileStream()
michael@0 246 {
michael@0 247 }
michael@0 248
michael@0 249 HRESULT STDMETHODCALLTYPE
michael@0 250 DWriteFontFileStream::GetFileSize(UINT64 *fileSize)
michael@0 251 {
michael@0 252 *fileSize = mData.size();
michael@0 253 return S_OK;
michael@0 254 }
michael@0 255
michael@0 256 HRESULT STDMETHODCALLTYPE
michael@0 257 DWriteFontFileStream::GetLastWriteTime(UINT64 *lastWriteTime)
michael@0 258 {
michael@0 259 return E_NOTIMPL;
michael@0 260 }
michael@0 261
michael@0 262 HRESULT STDMETHODCALLTYPE
michael@0 263 DWriteFontFileStream::ReadFileFragment(const void **fragmentStart,
michael@0 264 UINT64 fileOffset,
michael@0 265 UINT64 fragmentSize,
michael@0 266 void **fragmentContext)
michael@0 267 {
michael@0 268 // We are required to do bounds checking.
michael@0 269 if (fileOffset + fragmentSize > mData.size()) {
michael@0 270 return E_FAIL;
michael@0 271 }
michael@0 272
michael@0 273 // truncate the 64 bit fileOffset to size_t sized index into mData
michael@0 274 size_t index = static_cast<size_t>(fileOffset);
michael@0 275
michael@0 276 // We should be alive for the duration of this.
michael@0 277 *fragmentStart = &mData[index];
michael@0 278 *fragmentContext = nullptr;
michael@0 279 return S_OK;
michael@0 280 }
michael@0 281
michael@0 282 void STDMETHODCALLTYPE
michael@0 283 DWriteFontFileStream::ReleaseFileFragment(void *fragmentContext)
michael@0 284 {
michael@0 285 }
michael@0 286
michael@0 287 ScaledFontDWrite::ScaledFontDWrite(uint8_t *aData, uint32_t aSize,
michael@0 288 uint32_t aIndex, Float aGlyphSize)
michael@0 289 : ScaledFontBase(aGlyphSize)
michael@0 290 {
michael@0 291 IDWriteFactory *factory = DrawTargetD2D::GetDWriteFactory();
michael@0 292
michael@0 293 ffReferenceKey key;
michael@0 294 key.mData = aData;
michael@0 295 key.mSize = aSize;
michael@0 296
michael@0 297 RefPtr<IDWriteFontFile> fontFile;
michael@0 298 if (FAILED(factory->CreateCustomFontFileReference(&key, sizeof(ffReferenceKey), DWriteFontFileLoader::Instance(), byRef(fontFile)))) {
michael@0 299 gfxWarning() << "Failed to load font file from data!";
michael@0 300 return;
michael@0 301 }
michael@0 302
michael@0 303 IDWriteFontFile *ff = fontFile;
michael@0 304 if (FAILED(factory->CreateFontFace(DWRITE_FONT_FACE_TYPE_TRUETYPE, 1, &ff, aIndex, DWRITE_FONT_SIMULATIONS_NONE, byRef(mFontFace)))) {
michael@0 305 gfxWarning() << "Failed to create font face from font file data!";
michael@0 306 }
michael@0 307 }
michael@0 308
michael@0 309 TemporaryRef<Path>
michael@0 310 ScaledFontDWrite::GetPathForGlyphs(const GlyphBuffer &aBuffer, const DrawTarget *aTarget)
michael@0 311 {
michael@0 312 if (aTarget->GetType() != BackendType::DIRECT2D) {
michael@0 313 return ScaledFontBase::GetPathForGlyphs(aBuffer, aTarget);
michael@0 314 }
michael@0 315
michael@0 316 RefPtr<PathBuilder> pathBuilder = aTarget->CreatePathBuilder();
michael@0 317
michael@0 318 PathBuilderD2D *pathBuilderD2D =
michael@0 319 static_cast<PathBuilderD2D*>(pathBuilder.get());
michael@0 320
michael@0 321 CopyGlyphsToSink(aBuffer, pathBuilderD2D->GetSink());
michael@0 322
michael@0 323 return pathBuilder->Finish();
michael@0 324 }
michael@0 325
michael@0 326 void
michael@0 327 ScaledFontDWrite::CopyGlyphsToBuilder(const GlyphBuffer &aBuffer, PathBuilder *aBuilder, BackendType aBackendType, const Matrix *aTransformHint)
michael@0 328 {
michael@0 329 if (aBackendType != BackendType::DIRECT2D) {
michael@0 330 ScaledFontBase::CopyGlyphsToBuilder(aBuffer, aBuilder, aBackendType, aTransformHint);
michael@0 331 return;
michael@0 332 }
michael@0 333
michael@0 334 PathBuilderD2D *pathBuilderD2D =
michael@0 335 static_cast<PathBuilderD2D*>(aBuilder);
michael@0 336
michael@0 337 CopyGlyphsToSink(aBuffer, pathBuilderD2D->GetSink());
michael@0 338 }
michael@0 339
michael@0 340 void
michael@0 341 ScaledFontDWrite::CopyGlyphsToSink(const GlyphBuffer &aBuffer, ID2D1GeometrySink *aSink)
michael@0 342 {
michael@0 343 std::vector<UINT16> indices;
michael@0 344 std::vector<FLOAT> advances;
michael@0 345 std::vector<DWRITE_GLYPH_OFFSET> offsets;
michael@0 346 indices.resize(aBuffer.mNumGlyphs);
michael@0 347 advances.resize(aBuffer.mNumGlyphs);
michael@0 348 offsets.resize(aBuffer.mNumGlyphs);
michael@0 349
michael@0 350 memset(&advances.front(), 0, sizeof(FLOAT) * aBuffer.mNumGlyphs);
michael@0 351 for (unsigned int i = 0; i < aBuffer.mNumGlyphs; i++) {
michael@0 352 indices[i] = aBuffer.mGlyphs[i].mIndex;
michael@0 353 offsets[i].advanceOffset = aBuffer.mGlyphs[i].mPosition.x;
michael@0 354 offsets[i].ascenderOffset = -aBuffer.mGlyphs[i].mPosition.y;
michael@0 355 }
michael@0 356
michael@0 357 mFontFace->GetGlyphRunOutline(mSize, &indices.front(), &advances.front(),
michael@0 358 &offsets.front(), aBuffer.mNumGlyphs,
michael@0 359 FALSE, FALSE, aSink);
michael@0 360 }
michael@0 361
michael@0 362 bool
michael@0 363 ScaledFontDWrite::GetFontFileData(FontFileDataOutput aDataCallback, void *aBaton)
michael@0 364 {
michael@0 365 UINT32 fileCount = 0;
michael@0 366 mFontFace->GetFiles(&fileCount, nullptr);
michael@0 367
michael@0 368 if (fileCount > 1) {
michael@0 369 MOZ_ASSERT(false);
michael@0 370 return false;
michael@0 371 }
michael@0 372
michael@0 373 RefPtr<IDWriteFontFile> file;
michael@0 374 mFontFace->GetFiles(&fileCount, byRef(file));
michael@0 375
michael@0 376 const void *referenceKey;
michael@0 377 UINT32 refKeySize;
michael@0 378 // XXX - This can currently crash for webfonts, as when we get the reference
michael@0 379 // key out of the file, that can be an invalid reference key for the loader
michael@0 380 // we use it with. The fix to this is not obvious but it will probably
michael@0 381 // have to happen inside thebes.
michael@0 382 file->GetReferenceKey(&referenceKey, &refKeySize);
michael@0 383
michael@0 384 RefPtr<IDWriteFontFileLoader> loader;
michael@0 385 file->GetLoader(byRef(loader));
michael@0 386
michael@0 387 RefPtr<IDWriteFontFileStream> stream;
michael@0 388 loader->CreateStreamFromKey(referenceKey, refKeySize, byRef(stream));
michael@0 389
michael@0 390 UINT64 fileSize64;
michael@0 391 stream->GetFileSize(&fileSize64);
michael@0 392 if (fileSize64 > UINT32_MAX) {
michael@0 393 MOZ_ASSERT(false);
michael@0 394 return false;
michael@0 395 }
michael@0 396
michael@0 397 uint32_t fileSize = static_cast<uint32_t>(fileSize64);
michael@0 398 const void *fragmentStart;
michael@0 399 void *context;
michael@0 400 stream->ReadFileFragment(&fragmentStart, 0, fileSize, &context);
michael@0 401
michael@0 402 aDataCallback((uint8_t*)fragmentStart, fileSize, mFontFace->GetIndex(), mSize, aBaton);
michael@0 403
michael@0 404 stream->ReleaseFileFragment(context);
michael@0 405
michael@0 406 return true;
michael@0 407 }
michael@0 408
michael@0 409 AntialiasMode
michael@0 410 ScaledFontDWrite::GetDefaultAAMode()
michael@0 411 {
michael@0 412 AntialiasMode defaultMode = AntialiasMode::SUBPIXEL;
michael@0 413
michael@0 414 switch (GetSystemTextQuality()) {
michael@0 415 case CLEARTYPE_QUALITY:
michael@0 416 defaultMode = AntialiasMode::SUBPIXEL;
michael@0 417 break;
michael@0 418 case ANTIALIASED_QUALITY:
michael@0 419 defaultMode = AntialiasMode::GRAY;
michael@0 420 break;
michael@0 421 case DEFAULT_QUALITY:
michael@0 422 defaultMode = AntialiasMode::NONE;
michael@0 423 break;
michael@0 424 }
michael@0 425
michael@0 426 if (defaultMode == AntialiasMode::GRAY) {
michael@0 427 if (!DoGrayscale(mFontFace, mSize)) {
michael@0 428 defaultMode = AntialiasMode::NONE;
michael@0 429 }
michael@0 430 }
michael@0 431 return defaultMode;
michael@0 432 }
michael@0 433
michael@0 434 }
michael@0 435 }

mercurial