1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef MOZILLA_GFX_UNSCALEDFONTFREETYPE_H_
8 #define MOZILLA_GFX_UNSCALEDFONTFREETYPE_H_
9 
10 #include <cairo-ft.h>
11 
12 #include "2D.h"
13 
14 namespace mozilla {
15 namespace gfx {
16 
17 class ScaledFontFreeType;
18 class ScaledFontFontconfig;
19 
20 class UnscaledFontFreeType : public UnscaledFont {
21  public:
MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(UnscaledFontFreeType,override)22   MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(UnscaledFontFreeType, override)
23   explicit UnscaledFontFreeType(const RefPtr<SharedFTFace>& aFace)
24       : mFace(aFace), mIndex(0) {}
25   explicit UnscaledFontFreeType(const char* aFile, uint32_t aIndex = 0,
26                                 RefPtr<SharedFTFace> aFace = nullptr)
mFace(std::move (aFace))27       : mFace(std::move(aFace)), mFile(aFile), mIndex(aIndex) {}
28   explicit UnscaledFontFreeType(std::string&& aFile, uint32_t aIndex = 0,
29                                 RefPtr<SharedFTFace> aFace = nullptr)
mFace(std::move (aFace))30       : mFace(std::move(aFace)), mFile(std::move(aFile)), mIndex(aIndex) {}
31 
GetType()32   FontType GetType() const override { return FontType::FREETYPE; }
33 
GetFace()34   const RefPtr<SharedFTFace>& GetFace() const { return mFace; }
GetFile()35   const std::string& GetFile() const { return mFile; }
GetIndex()36   uint32_t GetIndex() const { return mIndex; }
37 
38   bool GetFontFileData(FontFileDataOutput aDataCallback, void* aBaton) override;
39 
40   bool GetFontDescriptor(FontDescriptorOutput aCb, void* aBaton) override;
41 
42   RefPtr<SharedFTFace> InitFace();
43 
44 #ifdef MOZ_WIDGET_ANDROID
45   static already_AddRefed<UnscaledFont> CreateFromFontDescriptor(
46       const uint8_t* aData, uint32_t aDataLength, uint32_t aIndex);
47 
48   already_AddRefed<ScaledFont> CreateScaledFont(
49       Float aGlyphSize, const uint8_t* aInstanceData,
50       uint32_t aInstanceDataLength, const FontVariation* aVariations,
51       uint32_t aNumVariations) override;
52 
53   already_AddRefed<ScaledFont> CreateScaledFontFromWRFont(
54       Float aGlyphSize, const wr::FontInstanceOptions* aOptions,
55       const wr::FontInstancePlatformOptions* aPlatformOptions,
56       const FontVariation* aVariations, uint32_t aNumVariations) override;
57 #endif
58 
59  protected:
60   RefPtr<SharedFTFace> mFace;
61   std::string mFile;
62   uint32_t mIndex;
63 
64   friend class ScaledFontFreeType;
65   friend class ScaledFontFontconfig;
66 
67   static void GetVariationSettingsFromFace(
68       std::vector<FontVariation>* aVariations, FT_Face aFace);
69 
70   static void ApplyVariationsToFace(const FontVariation* aVariations,
71                                     uint32_t aNumVariations, FT_Face aFace);
72 };
73 
74 #ifdef MOZ_WIDGET_GTK
75 class UnscaledFontFontconfig : public UnscaledFontFreeType {
76  public:
MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(UnscaledFontFontconfig,override)77   MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(UnscaledFontFontconfig, override)
78   explicit UnscaledFontFontconfig(const RefPtr<SharedFTFace>& aFace)
79       : UnscaledFontFreeType(aFace) {}
80   explicit UnscaledFontFontconfig(const char* aFile, uint32_t aIndex = 0,
81                                   RefPtr<SharedFTFace> aFace = nullptr)
UnscaledFontFreeType(aFile,aIndex,std::move (aFace))82       : UnscaledFontFreeType(aFile, aIndex, std::move(aFace)) {}
83   explicit UnscaledFontFontconfig(std::string&& aFile, uint32_t aIndex = 0,
84                                   RefPtr<SharedFTFace> aFace = nullptr)
UnscaledFontFreeType(std::move (aFile),aIndex,std::move (aFace))85       : UnscaledFontFreeType(std::move(aFile), aIndex, std::move(aFace)) {}
86 
GetType()87   FontType GetType() const override { return FontType::FONTCONFIG; }
88 
89   static already_AddRefed<UnscaledFont> CreateFromFontDescriptor(
90       const uint8_t* aData, uint32_t aDataLength, uint32_t aIndex);
91 
92   already_AddRefed<ScaledFont> CreateScaledFont(
93       Float aGlyphSize, const uint8_t* aInstanceData,
94       uint32_t aInstanceDataLength, const FontVariation* aVariations,
95       uint32_t aNumVariations) override;
96 
97   already_AddRefed<ScaledFont> CreateScaledFontFromWRFont(
98       Float aGlyphSize, const wr::FontInstanceOptions* aOptions,
99       const wr::FontInstancePlatformOptions* aPlatformOptions,
100       const FontVariation* aVariations, uint32_t aNumVariations) override;
101 };
102 #endif
103 
104 }  // namespace gfx
105 }  // namespace mozilla
106 
107 #endif /* MOZILLA_GFX_UNSCALEDFONTFREETYPE_H_ */
108