gfx/thebes/gfxDWriteFontList.h

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: 4 -*-
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 #ifndef GFX_DWRITEFONTLIST_H
michael@0 7 #define GFX_DWRITEFONTLIST_H
michael@0 8
michael@0 9 #include "mozilla/MemoryReporting.h"
michael@0 10 #include "gfxDWriteCommon.h"
michael@0 11
michael@0 12 #include "gfxFont.h"
michael@0 13 #include "gfxUserFontSet.h"
michael@0 14 #include "cairo-win32.h"
michael@0 15
michael@0 16 #include "gfxPlatformFontList.h"
michael@0 17 #include "gfxPlatform.h"
michael@0 18 #include <algorithm>
michael@0 19
michael@0 20
michael@0 21 /**
michael@0 22 * gfxDWriteFontFamily is a class that describes one of the fonts on the
michael@0 23 * users system. It holds each gfxDWriteFontEntry (maps more directly to
michael@0 24 * a font face) which holds font type, charset info and character map info.
michael@0 25 */
michael@0 26 class gfxDWriteFontEntry;
michael@0 27
michael@0 28 /**
michael@0 29 * \brief Class representing directwrite font family.
michael@0 30 */
michael@0 31 class gfxDWriteFontFamily : public gfxFontFamily
michael@0 32 {
michael@0 33 public:
michael@0 34 /**
michael@0 35 * Constructs a new DWriteFont Family.
michael@0 36 *
michael@0 37 * \param aName Name identifying the family
michael@0 38 * \param aFamily IDWriteFontFamily object representing the directwrite
michael@0 39 * family object.
michael@0 40 */
michael@0 41 gfxDWriteFontFamily(const nsAString& aName,
michael@0 42 IDWriteFontFamily *aFamily)
michael@0 43 : gfxFontFamily(aName), mDWFamily(aFamily), mForceGDIClassic(false) {}
michael@0 44 virtual ~gfxDWriteFontFamily();
michael@0 45
michael@0 46 virtual void FindStyleVariations(FontInfoData *aFontInfoData = nullptr);
michael@0 47
michael@0 48 virtual void LocalizedName(nsAString& aLocalizedName);
michael@0 49
michael@0 50 virtual void ReadFaceNames(gfxPlatformFontList *aPlatformFontList,
michael@0 51 bool aNeedFullnamePostscriptNames);
michael@0 52
michael@0 53 void SetForceGDIClassic(bool aForce) { mForceGDIClassic = aForce; }
michael@0 54
michael@0 55 virtual void AddSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf,
michael@0 56 FontListSizes* aSizes) const;
michael@0 57 virtual void AddSizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf,
michael@0 58 FontListSizes* aSizes) const;
michael@0 59
michael@0 60 protected:
michael@0 61 /** This font family's directwrite fontfamily object */
michael@0 62 nsRefPtr<IDWriteFontFamily> mDWFamily;
michael@0 63 bool mForceGDIClassic;
michael@0 64 };
michael@0 65
michael@0 66 /**
michael@0 67 * \brief Class representing DirectWrite FontEntry (a unique font style/family)
michael@0 68 */
michael@0 69 class gfxDWriteFontEntry : public gfxFontEntry
michael@0 70 {
michael@0 71 public:
michael@0 72 /**
michael@0 73 * Constructs a font entry.
michael@0 74 *
michael@0 75 * \param aFaceName The name of the corresponding font face.
michael@0 76 * \param aFont DirectWrite font object
michael@0 77 */
michael@0 78 gfxDWriteFontEntry(const nsAString& aFaceName,
michael@0 79 IDWriteFont *aFont)
michael@0 80 : gfxFontEntry(aFaceName), mFont(aFont), mFontFile(nullptr),
michael@0 81 mForceGDIClassic(false)
michael@0 82 {
michael@0 83 mItalic = (aFont->GetStyle() == DWRITE_FONT_STYLE_ITALIC ||
michael@0 84 aFont->GetStyle() == DWRITE_FONT_STYLE_OBLIQUE);
michael@0 85 mStretch = FontStretchFromDWriteStretch(aFont->GetStretch());
michael@0 86 uint16_t weight = NS_ROUNDUP(aFont->GetWeight() - 50, 100);
michael@0 87
michael@0 88 weight = std::max<uint16_t>(100, weight);
michael@0 89 weight = std::min<uint16_t>(900, weight);
michael@0 90 mWeight = weight;
michael@0 91
michael@0 92 mIsCJK = UNINITIALIZED_VALUE;
michael@0 93 }
michael@0 94
michael@0 95 /**
michael@0 96 * Constructs a font entry using a font. But with custom font values.
michael@0 97 * This is used for creating correct font entries for @font-face with local
michael@0 98 * font source.
michael@0 99 *
michael@0 100 * \param aFaceName The name of the corresponding font face.
michael@0 101 * \param aFont DirectWrite font object
michael@0 102 * \param aWeight Weight of the font
michael@0 103 * \param aStretch Stretch of the font
michael@0 104 * \param aItalic True if italic
michael@0 105 */
michael@0 106 gfxDWriteFontEntry(const nsAString& aFaceName,
michael@0 107 IDWriteFont *aFont,
michael@0 108 uint16_t aWeight,
michael@0 109 int16_t aStretch,
michael@0 110 bool aItalic)
michael@0 111 : gfxFontEntry(aFaceName), mFont(aFont), mFontFile(nullptr),
michael@0 112 mForceGDIClassic(false)
michael@0 113 {
michael@0 114 mWeight = aWeight;
michael@0 115 mStretch = aStretch;
michael@0 116 mItalic = aItalic;
michael@0 117 mIsUserFont = true;
michael@0 118 mIsLocalUserFont = true;
michael@0 119 mIsCJK = UNINITIALIZED_VALUE;
michael@0 120 }
michael@0 121
michael@0 122 /**
michael@0 123 * Constructs a font entry using a font file.
michael@0 124 *
michael@0 125 * \param aFaceName The name of the corresponding font face.
michael@0 126 * \param aFontFile DirectWrite fontfile object
michael@0 127 * \param aWeight Weight of the font
michael@0 128 * \param aStretch Stretch of the font
michael@0 129 * \param aItalic True if italic
michael@0 130 */
michael@0 131 gfxDWriteFontEntry(const nsAString& aFaceName,
michael@0 132 IDWriteFontFile *aFontFile,
michael@0 133 uint16_t aWeight,
michael@0 134 int16_t aStretch,
michael@0 135 bool aItalic)
michael@0 136 : gfxFontEntry(aFaceName), mFont(nullptr), mFontFile(aFontFile),
michael@0 137 mForceGDIClassic(false)
michael@0 138 {
michael@0 139 mWeight = aWeight;
michael@0 140 mStretch = aStretch;
michael@0 141 mItalic = aItalic;
michael@0 142 mIsUserFont = true;
michael@0 143 mIsCJK = UNINITIALIZED_VALUE;
michael@0 144 }
michael@0 145
michael@0 146 virtual ~gfxDWriteFontEntry();
michael@0 147
michael@0 148 virtual bool IsSymbolFont();
michael@0 149
michael@0 150 virtual hb_blob_t* GetFontTable(uint32_t aTableTag) MOZ_OVERRIDE;
michael@0 151
michael@0 152 nsresult ReadCMAP(FontInfoData *aFontInfoData = nullptr);
michael@0 153
michael@0 154 bool IsCJKFont();
michael@0 155
michael@0 156 void SetForceGDIClassic(bool aForce) { mForceGDIClassic = aForce; }
michael@0 157 bool GetForceGDIClassic() { return mForceGDIClassic; }
michael@0 158
michael@0 159 virtual void AddSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf,
michael@0 160 FontListSizes* aSizes) const;
michael@0 161 virtual void AddSizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf,
michael@0 162 FontListSizes* aSizes) const;
michael@0 163
michael@0 164 protected:
michael@0 165 friend class gfxDWriteFont;
michael@0 166 friend class gfxDWriteFontList;
michael@0 167
michael@0 168 virtual nsresult CopyFontTable(uint32_t aTableTag,
michael@0 169 FallibleTArray<uint8_t>& aBuffer) MOZ_OVERRIDE;
michael@0 170
michael@0 171 virtual gfxFont *CreateFontInstance(const gfxFontStyle *aFontStyle,
michael@0 172 bool aNeedsBold);
michael@0 173
michael@0 174 nsresult CreateFontFace(
michael@0 175 IDWriteFontFace **aFontFace,
michael@0 176 DWRITE_FONT_SIMULATIONS aSimulations = DWRITE_FONT_SIMULATIONS_NONE);
michael@0 177
michael@0 178 static bool InitLogFont(IDWriteFont *aFont, LOGFONTW *aLogFont);
michael@0 179
michael@0 180 /**
michael@0 181 * A fontentry only needs to have either of these. If it has both only
michael@0 182 * the IDWriteFont will be used.
michael@0 183 */
michael@0 184 nsRefPtr<IDWriteFont> mFont;
michael@0 185 nsRefPtr<IDWriteFontFile> mFontFile;
michael@0 186
michael@0 187 // font face corresponding to the mFont/mFontFile *without* any DWrite
michael@0 188 // style simulations applied
michael@0 189 nsRefPtr<IDWriteFontFace> mFontFace;
michael@0 190
michael@0 191 DWRITE_FONT_FACE_TYPE mFaceType;
michael@0 192
michael@0 193 int8_t mIsCJK;
michael@0 194 bool mForceGDIClassic;
michael@0 195 };
michael@0 196
michael@0 197 // custom text renderer used to determine the fallback font for a given char
michael@0 198 class FontFallbackRenderer MOZ_FINAL : public IDWriteTextRenderer
michael@0 199 {
michael@0 200 public:
michael@0 201 FontFallbackRenderer(IDWriteFactory *aFactory)
michael@0 202 : mRefCount(0)
michael@0 203 {
michael@0 204 HRESULT hr = S_OK;
michael@0 205
michael@0 206 hr = aFactory->GetSystemFontCollection(getter_AddRefs(mSystemFonts));
michael@0 207 NS_ASSERTION(SUCCEEDED(hr), "GetSystemFontCollection failed!");
michael@0 208 }
michael@0 209
michael@0 210 ~FontFallbackRenderer()
michael@0 211 {}
michael@0 212
michael@0 213 // IDWriteTextRenderer methods
michael@0 214 IFACEMETHOD(DrawGlyphRun)(
michael@0 215 void* clientDrawingContext,
michael@0 216 FLOAT baselineOriginX,
michael@0 217 FLOAT baselineOriginY,
michael@0 218 DWRITE_MEASURING_MODE measuringMode,
michael@0 219 DWRITE_GLYPH_RUN const* glyphRun,
michael@0 220 DWRITE_GLYPH_RUN_DESCRIPTION const* glyphRunDescription,
michael@0 221 IUnknown* clientDrawingEffect
michael@0 222 );
michael@0 223
michael@0 224 IFACEMETHOD(DrawUnderline)(
michael@0 225 void* clientDrawingContext,
michael@0 226 FLOAT baselineOriginX,
michael@0 227 FLOAT baselineOriginY,
michael@0 228 DWRITE_UNDERLINE const* underline,
michael@0 229 IUnknown* clientDrawingEffect
michael@0 230 )
michael@0 231 {
michael@0 232 return E_NOTIMPL;
michael@0 233 }
michael@0 234
michael@0 235
michael@0 236 IFACEMETHOD(DrawStrikethrough)(
michael@0 237 void* clientDrawingContext,
michael@0 238 FLOAT baselineOriginX,
michael@0 239 FLOAT baselineOriginY,
michael@0 240 DWRITE_STRIKETHROUGH const* strikethrough,
michael@0 241 IUnknown* clientDrawingEffect
michael@0 242 )
michael@0 243 {
michael@0 244 return E_NOTIMPL;
michael@0 245 }
michael@0 246
michael@0 247
michael@0 248 IFACEMETHOD(DrawInlineObject)(
michael@0 249 void* clientDrawingContext,
michael@0 250 FLOAT originX,
michael@0 251 FLOAT originY,
michael@0 252 IDWriteInlineObject* inlineObject,
michael@0 253 BOOL isSideways,
michael@0 254 BOOL isRightToLeft,
michael@0 255 IUnknown* clientDrawingEffect
michael@0 256 )
michael@0 257 {
michael@0 258 return E_NOTIMPL;
michael@0 259 }
michael@0 260
michael@0 261 // IDWritePixelSnapping methods
michael@0 262
michael@0 263 IFACEMETHOD(IsPixelSnappingDisabled)(
michael@0 264 void* clientDrawingContext,
michael@0 265 BOOL* isDisabled
michael@0 266 )
michael@0 267 {
michael@0 268 *isDisabled = FALSE;
michael@0 269 return S_OK;
michael@0 270 }
michael@0 271
michael@0 272 IFACEMETHOD(GetCurrentTransform)(
michael@0 273 void* clientDrawingContext,
michael@0 274 DWRITE_MATRIX* transform
michael@0 275 )
michael@0 276 {
michael@0 277 const DWRITE_MATRIX ident = {1.0, 0.0, 0.0, 1.0, 0.0, 0.0};
michael@0 278 *transform = ident;
michael@0 279 return S_OK;
michael@0 280 }
michael@0 281
michael@0 282 IFACEMETHOD(GetPixelsPerDip)(
michael@0 283 void* clientDrawingContext,
michael@0 284 FLOAT* pixelsPerDip
michael@0 285 )
michael@0 286 {
michael@0 287 *pixelsPerDip = 1.0f;
michael@0 288 return S_OK;
michael@0 289 }
michael@0 290
michael@0 291 // IUnknown methods
michael@0 292
michael@0 293 IFACEMETHOD_(unsigned long, AddRef) ()
michael@0 294 {
michael@0 295 return InterlockedIncrement(&mRefCount);
michael@0 296 }
michael@0 297
michael@0 298 IFACEMETHOD_(unsigned long, Release) ()
michael@0 299 {
michael@0 300 unsigned long newCount = InterlockedDecrement(&mRefCount);
michael@0 301 if (newCount == 0)
michael@0 302 {
michael@0 303 delete this;
michael@0 304 return 0;
michael@0 305 }
michael@0 306
michael@0 307 return newCount;
michael@0 308 }
michael@0 309
michael@0 310 IFACEMETHOD(QueryInterface) (IID const& riid, void** ppvObject)
michael@0 311 {
michael@0 312 if (__uuidof(IDWriteTextRenderer) == riid) {
michael@0 313 *ppvObject = this;
michael@0 314 } else if (__uuidof(IDWritePixelSnapping) == riid) {
michael@0 315 *ppvObject = this;
michael@0 316 } else if (__uuidof(IUnknown) == riid) {
michael@0 317 *ppvObject = this;
michael@0 318 } else {
michael@0 319 *ppvObject = nullptr;
michael@0 320 return E_FAIL;
michael@0 321 }
michael@0 322
michael@0 323 this->AddRef();
michael@0 324 return S_OK;
michael@0 325 }
michael@0 326
michael@0 327 const nsString& FallbackFamilyName() { return mFamilyName; }
michael@0 328
michael@0 329 protected:
michael@0 330 long mRefCount;
michael@0 331 nsRefPtr<IDWriteFontCollection> mSystemFonts;
michael@0 332 nsString mFamilyName;
michael@0 333 };
michael@0 334
michael@0 335
michael@0 336
michael@0 337 class gfxDWriteFontList : public gfxPlatformFontList {
michael@0 338 public:
michael@0 339 gfxDWriteFontList();
michael@0 340
michael@0 341 static gfxDWriteFontList* PlatformFontList() {
michael@0 342 return static_cast<gfxDWriteFontList*>(sPlatformFontList);
michael@0 343 }
michael@0 344
michael@0 345 // initialize font lists
michael@0 346 virtual nsresult InitFontList();
michael@0 347
michael@0 348 virtual gfxFontFamily* GetDefaultFont(const gfxFontStyle* aStyle);
michael@0 349
michael@0 350 virtual gfxFontEntry* LookupLocalFont(const gfxProxyFontEntry *aProxyEntry,
michael@0 351 const nsAString& aFontName);
michael@0 352
michael@0 353 virtual gfxFontEntry* MakePlatformFont(const gfxProxyFontEntry *aProxyEntry,
michael@0 354 const uint8_t *aFontData,
michael@0 355 uint32_t aLength);
michael@0 356
michael@0 357 virtual bool ResolveFontName(const nsAString& aFontName,
michael@0 358 nsAString& aResolvedFontName);
michael@0 359
michael@0 360 bool GetStandardFamilyName(const nsAString& aFontName,
michael@0 361 nsAString& aFamilyName);
michael@0 362
michael@0 363 IDWriteGdiInterop *GetGDIInterop() { return mGDIInterop; }
michael@0 364 bool UseGDIFontTableAccess() { return mGDIFontTableAccess; }
michael@0 365
michael@0 366 virtual gfxFontFamily* FindFamily(const nsAString& aFamily);
michael@0 367
michael@0 368 virtual void GetFontFamilyList(nsTArray<nsRefPtr<gfxFontFamily> >& aFamilyArray);
michael@0 369
michael@0 370 gfxFloat GetForceGDIClassicMaxFontSize() { return mForceGDIClassicMaxFontSize; }
michael@0 371
michael@0 372 virtual void AddSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf,
michael@0 373 FontListSizes* aSizes) const;
michael@0 374 virtual void AddSizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf,
michael@0 375 FontListSizes* aSizes) const;
michael@0 376
michael@0 377 private:
michael@0 378 friend class gfxDWriteFontFamily;
michael@0 379
michael@0 380 nsresult GetFontSubstitutes();
michael@0 381
michael@0 382 void GetDirectWriteSubstitutes();
michael@0 383
michael@0 384 // search fonts system-wide for a given character, null otherwise
michael@0 385 virtual gfxFontEntry* GlobalFontFallback(const uint32_t aCh,
michael@0 386 int32_t aRunScript,
michael@0 387 const gfxFontStyle* aMatchStyle,
michael@0 388 uint32_t& aCmapCount,
michael@0 389 gfxFontFamily** aMatchedFamily);
michael@0 390
michael@0 391 virtual bool UsesSystemFallback() { return true; }
michael@0 392
michael@0 393 /**
michael@0 394 * Fonts listed in the registry as substitutes but for which no actual
michael@0 395 * font family is found.
michael@0 396 */
michael@0 397 nsTArray<nsString> mNonExistingFonts;
michael@0 398
michael@0 399 typedef nsRefPtrHashtable<nsStringHashKey, gfxFontFamily> FontTable;
michael@0 400
michael@0 401 /**
michael@0 402 * Table of font substitutes, we grab this from the registry to get
michael@0 403 * alternative font names.
michael@0 404 */
michael@0 405 FontTable mFontSubstitutes;
michael@0 406
michael@0 407 bool mInitialized;
michael@0 408 virtual nsresult DelayedInitFontList();
michael@0 409
michael@0 410 virtual already_AddRefed<FontInfoData> CreateFontInfoData();
michael@0 411
michael@0 412 gfxFloat mForceGDIClassicMaxFontSize;
michael@0 413
michael@0 414 // whether to use GDI font table access routines
michael@0 415 bool mGDIFontTableAccess;
michael@0 416 nsRefPtr<IDWriteGdiInterop> mGDIInterop;
michael@0 417
michael@0 418 nsRefPtr<FontFallbackRenderer> mFallbackRenderer;
michael@0 419 nsRefPtr<IDWriteTextFormat> mFallbackFormat;
michael@0 420 };
michael@0 421
michael@0 422
michael@0 423 #endif /* GFX_DWRITEFONTLIST_H */

mercurial