1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef GFX_MACFONT_H
7 #define GFX_MACFONT_H
8 
9 #include "mozilla/MemoryReporting.h"
10 #include "gfxFont.h"
11 #include <ApplicationServices/ApplicationServices.h>
12 
13 #include "mozilla/gfx/UnscaledFontMac.h"
14 
15 class MacOSFontEntry;
16 
17 class gfxMacFont : public gfxFont {
18  public:
19   gfxMacFont(const RefPtr<mozilla::gfx::UnscaledFontMac>& aUnscaledFont, MacOSFontEntry* aFontEntry,
20              const gfxFontStyle* aFontStyle);
21 
22   virtual ~gfxMacFont();
23 
GetCGFontRef()24   CGFontRef GetCGFontRef() const { return mCGFont; }
25 
26   /* override Measure to add padding for antialiasing */
27   RunMetrics Measure(const gfxTextRun* aTextRun, uint32_t aStart, uint32_t aEnd,
28                      BoundingBoxType aBoundingBoxType, DrawTarget* aDrawTargetForTightBoundingBox,
29                      Spacing* aSpacing, mozilla::gfx::ShapedTextFlags aOrientation) override;
30 
31   // We need to provide hinted (non-linear) glyph widths if using a font
32   // with embedded color bitmaps (Apple Color Emoji), as Core Text renders
33   // the glyphs with non-linear scaling at small pixel sizes.
ProvidesGlyphWidths()34   bool ProvidesGlyphWidths() const override {
35     return mVariationFont || mFontEntry->HasFontTable(TRUETYPE_TAG('s', 'b', 'i', 'x'));
36   }
37 
38   int32_t GetGlyphWidth(uint16_t aGID) override;
39 
40   bool GetGlyphBounds(uint16_t aGID, gfxRect* aBounds, bool aTight) override;
41 
42   already_AddRefed<mozilla::gfx::ScaledFont> GetScaledFont(
43       mozilla::gfx::DrawTarget* aTarget) override;
44 
45   bool ShouldRoundXOffset(cairo_t* aCairo) const override;
46 
47   void AddSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf,
48                               FontCacheSizes* aSizes) const override;
49   void AddSizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf,
50                               FontCacheSizes* aSizes) const override;
51 
GetType()52   FontType GetType() const override { return FONT_TYPE_MAC; }
53 
54   // Helper to create a CTFont from a CGFont, with optional font descriptor
55   // (for features), and copying any variations that were set on the CGFont.
56   // This is public so that gfxCoreTextShaper can also use it.
57   static CTFontRef CreateCTFontFromCGFontWithVariations(CGFontRef aCGFont, CGFloat aSize,
58                                                         bool aInstalledFont,
59                                                         CTFontDescriptorRef aFontDesc = nullptr);
60 
61  protected:
GetHorizontalMetrics()62   const Metrics& GetHorizontalMetrics() override { return mMetrics; }
63 
64   // override to prefer CoreText shaping with fonts that depend on AAT
65   bool ShapeText(DrawTarget* aDrawTarget, const char16_t* aText, uint32_t aOffset, uint32_t aLength,
66                  Script aScript, nsAtom* aLanguage, bool aVertical, RoundingFlags aRounding,
67                  gfxShapedText* aShapedText) override;
68 
69   void InitMetrics();
70   void InitMetricsFromPlatform();
71 
72   // Get width and glyph ID for a character; uses aConvFactor
73   // to convert font units as returned by CG to actual dimensions
74   gfxFloat GetCharWidth(CFDataRef aCmap, char16_t aUniChar, uint32_t* aGlyphID,
75                         gfxFloat aConvFactor);
76 
77   // a strong reference to the CoreGraphics font
78   CGFontRef mCGFont;
79 
80   // a Core Text font reference, created only if we're using CT to measure
81   // glyph widths; otherwise null.
82   CTFontRef mCTFont;
83 
84   mozilla::UniquePtr<gfxFontShaper> mCoreTextShaper;
85 
86   Metrics mMetrics;
87   nscolor mFontSmoothingBackgroundColor;
88 
89   bool mVariationFont;  // true if font has OpenType variations
90 };
91 
92 #endif /* GFX_MACFONT_H */
93