COLRFonts.h (4364B)
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 COLR_FONTS_H 7 #define COLR_FONTS_H 8 9 #include "mozilla/gfx/2D.h" 10 #include "nsAtom.h" 11 #include "nsTArray.h" 12 #include "nsTHashtable.h" 13 14 struct hb_blob_t; 15 struct hb_face_t; 16 struct hb_font_t; 17 18 namespace mozilla { 19 20 namespace layout { 21 class TextDrawTarget; 22 } 23 24 namespace gfx { 25 26 class FontPaletteValueSet { 27 public: 28 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FontPaletteValueSet) 29 30 struct OverrideColor { 31 uint32_t mIndex = 0; 32 sRGBColor mColor; 33 }; 34 35 struct PaletteValues { 36 enum { kLight = -1, kDark = -2 }; 37 int32_t mBasePalette = 0; // 0-based index, or kLight/kDark constants 38 nsTArray<OverrideColor> mOverrides; 39 }; 40 41 const PaletteValues* Lookup(nsAtom* aName, const nsACString& aFamily) const; 42 43 PaletteValues* Insert(nsAtom* aName, const nsACString& aFamily); 44 45 private: 46 ~FontPaletteValueSet() = default; 47 48 struct PaletteHashKey { 49 RefPtr<nsAtom> mName; 50 nsCString mFamily; 51 52 PaletteHashKey() = delete; 53 PaletteHashKey(nsAtom* aName, const nsACString& aFamily) 54 : mName(aName), mFamily(aFamily) {} 55 PaletteHashKey(const PaletteHashKey& aKey) = default; 56 }; 57 58 class HashEntry : public PLDHashEntryHdr { 59 public: 60 using KeyType = const PaletteHashKey&; 61 using KeyTypePointer = const PaletteHashKey*; 62 63 HashEntry() = delete; 64 explicit HashEntry(KeyTypePointer aKey) : mKey(*aKey) {} 65 HashEntry(HashEntry&& other) noexcept 66 : PLDHashEntryHdr(std::move(other)), 67 mKey(other.mKey), 68 mValue(std::move(other.mValue)) { 69 NS_ERROR("Should not be called"); 70 } 71 ~HashEntry() = default; 72 73 bool KeyEquals(const KeyTypePointer aKey) const { 74 return mKey.mName == aKey->mName && mKey.mFamily.Equals(aKey->mFamily); 75 } 76 static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; } 77 static PLDHashNumber HashKey(const KeyTypePointer aKey) { 78 return aKey->mName->hash() + 79 HashString(aKey->mFamily.get(), aKey->mFamily.Length()); 80 } 81 enum { ALLOW_MEMMOVE = true }; 82 83 PaletteHashKey mKey; 84 PaletteValues mValue; 85 }; 86 87 nsTHashtable<HashEntry> mValues; 88 }; 89 90 class COLRFonts { 91 public: 92 static bool ValidateColorGlyphs(hb_blob_t* aCOLR, hb_blob_t* aCPAL); 93 94 // COLRv0: color glyph is represented as a simple list of colored layers. 95 // (This is used only as an opaque pointer; the internal type is private.) 96 struct GlyphLayers; 97 98 static const GlyphLayers* GetGlyphLayers(hb_blob_t* aCOLR, uint32_t aGlyphId); 99 100 static bool PaintGlyphLayers( 101 hb_blob_t* aCOLR, hb_face_t* aFace, const GlyphLayers* aLayers, 102 DrawTarget* aDrawTarget, layout::TextDrawTarget* aTextDrawer, 103 ScaledFont* aScaledFont, DrawOptions aDrawOptions, const Point& aPoint, 104 const sRGBColor& aCurrentColor, const nsTArray<sRGBColor>* aColors); 105 106 // COLRv1 support: color glyph is represented by a directed acyclic graph of 107 // paint records. 108 // (This is used only as an opaque pointer; the internal type is private.) 109 struct GlyphPaintGraph; 110 111 static const GlyphPaintGraph* GetGlyphPaintGraph(hb_blob_t* aCOLR, 112 uint32_t aGlyphId); 113 114 static bool PaintGlyphGraph( 115 hb_blob_t* aCOLR, hb_font_t* aFont, const GlyphPaintGraph* aPaintGraph, 116 DrawTarget* aDrawTarget, layout::TextDrawTarget* aTextDrawer, 117 ScaledFont* aScaledFont, DrawOptions aDrawOptions, const Point& aPoint, 118 const sRGBColor& aCurrentColor, const nsTArray<sRGBColor>* aColors, 119 uint32_t aGlyphId, float aFontUnitsToPixels); 120 121 static Rect GetColorGlyphBounds(hb_blob_t* aCOLR, hb_font_t* aFont, 122 uint32_t aGlyphId, DrawTarget* aDrawTarget, 123 ScaledFont* aScaledFont, 124 float aFontUnitsToPixels); 125 126 static uint16_t GetColrTableVersion(hb_blob_t* aCOLR); 127 128 static nsTArray<sRGBColor> CreateColorPalette( 129 hb_face_t* aFace, const FontPaletteValueSet* aPaletteValueSet, 130 nsAtom* aFontPalette, const nsACString& aFamilyName); 131 }; 132 133 } // namespace gfx 134 135 } // namespace mozilla 136 137 #endif // COLR_FONTS_H