1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINIKIN_FONT_FAMILY_H 18 #define MINIKIN_FONT_FAMILY_H 19 20 #include <memory> 21 #include <string> 22 #include <vector> 23 24 #include "minikin/FamilyVariant.h" 25 #include "minikin/Font.h" 26 #include "minikin/FontStyle.h" 27 #include "minikin/HbUtils.h" 28 #include "minikin/Macros.h" 29 #include "minikin/SparseBitSet.h" 30 31 namespace minikin { 32 33 enum VariationFamilyType : uint8_t { 34 None = 0, 35 SingleFont_wghtOnly = 1, 36 SingleFont_wght_ital = 2, 37 TwoFont_wght = 3, 38 }; 39 40 class FontFamily { 41 public: 42 static std::shared_ptr<FontFamily> create(std::vector<std::shared_ptr<Font>>&& fonts); 43 static std::shared_ptr<FontFamily> create(FamilyVariant variant, 44 std::vector<std::shared_ptr<Font>>&& fonts); 45 static std::shared_ptr<FontFamily> create(uint32_t localeListId, FamilyVariant variant, 46 std::vector<std::shared_ptr<Font>>&& fonts, 47 bool isCustomFallback, bool isDefaultFallback, 48 VariationFamilyType varFamilyType); 49 50 // Create FontFamily with axes override. 51 static std::shared_ptr<FontFamily> create(const std::shared_ptr<FontFamily>& parent, 52 const VariationSettings& axesOverride); 53 54 FontFamily(FontFamily&&) = default; 55 FontFamily& operator=(FontFamily&&) = default; 56 57 static std::vector<std::shared_ptr<FontFamily>> readVector(BufferReader* reader); 58 static void writeVector(BufferWriter* writer, 59 const std::vector<std::shared_ptr<FontFamily>>& families); 60 61 FakedFont getClosestMatch(FontStyle style, const VariationSettings& axes) const; getClosestMatch(FontStyle style)62 FakedFont getClosestMatch(FontStyle style) const { 63 return getClosestMatch(style, VariationSettings()); 64 } 65 FakedFont getVariationFamilyAdjustment(FontStyle style) const; 66 localeListId()67 uint32_t localeListId() const { return mLocaleListId; } variant()68 FamilyVariant variant() const { return mVariant; } 69 70 // API's for enumerating the fonts in a family. These don't guarantee any particular order getNumFonts()71 size_t getNumFonts() const { return mFontsCount; } getFont(size_t index)72 const Font* getFont(size_t index) const { return mFonts[index].get(); } getFontRef(size_t index)73 const std::shared_ptr<Font>& getFontRef(size_t index) const { return mFonts[index]; } getStyle(size_t index)74 FontStyle getStyle(size_t index) const { return mFonts[index]->style(); } isColorEmojiFamily()75 bool isColorEmojiFamily() const { return mIsColorEmoji; } getSupportedAxesCount()76 size_t getSupportedAxesCount() const { return mSupportedAxesCount; } getSupportedAxisAt(size_t index)77 AxisTag getSupportedAxisAt(size_t index) const { return mSupportedAxes[index]; } isCustomFallback()78 bool isCustomFallback() const { return mIsCustomFallback; } isDefaultFallback()79 bool isDefaultFallback() const { return mIsDefaultFallback; } 80 81 // Get Unicode coverage. getCoverage()82 const SparseBitSet& getCoverage() const { 83 if (mParent) [[unlikely]] { 84 return mParent->getCoverage(); 85 } else { 86 return mCoverage; 87 } 88 } 89 getCmap14Coverage(uint16_t vsIndex)90 const SparseBitSet& getCmap14Coverage(uint16_t vsIndex) const { 91 if (mParent) [[unlikely]] { 92 return mParent->getCmap14Coverage(vsIndex); 93 } else { 94 return mCmapFmt14Coverage[vsIndex]; 95 } 96 } 97 getParent()98 const std::shared_ptr<FontFamily>& getParent() const { return mParent; } 99 100 // Returns true if the font has a glyph for the code point and variation selector pair. 101 // Caller should acquire a lock before calling the method. 102 bool hasGlyph(uint32_t codepoint, uint32_t variationSelector) const; 103 104 // Returns true if this font family has a variaion sequence table (cmap format 14 subtable). hasVSTable()105 bool hasVSTable() const { return mCmapFmt14CoverageCount != 0; } 106 107 // Creates new FontFamily based on this family while applying font variations. Returns nullptr 108 // if none of variations apply to this family. 109 std::shared_ptr<FontFamily> createFamilyWithVariation( 110 const VariationSettings& variations) const; 111 112 private: 113 FontFamily(uint32_t localeListId, FamilyVariant variant, 114 std::vector<std::shared_ptr<Font>>&& fonts, bool isCustomFallback, 115 bool isDefaultFallback, VariationFamilyType varFamilyType); 116 FontFamily(const std::shared_ptr<FontFamily>& parent, const VariationSettings& axesOverride); 117 explicit FontFamily(BufferReader* reader, const std::shared_ptr<std::vector<Font>>& fonts); 118 119 void writeTo(BufferWriter* writer, uint32_t* fontIndex) const; 120 121 void computeCoverage(); 122 123 // Note: to minimize padding, small member fields are grouped at the end. 124 std::unique_ptr<std::shared_ptr<Font>[]> mFonts; 125 // mSupportedAxes is sorted. 126 std::unique_ptr<AxisTag[]> mSupportedAxes; 127 // This field is empty if mParent is set. Use mParent's coverage instead. 128 SparseBitSet mCoverage; 129 // This field is empty if mParent is set. Use mParent's coverage instead. 130 std::unique_ptr<SparseBitSet[]> mCmapFmt14Coverage; 131 std::shared_ptr<FontFamily> mParent; 132 VariationSettings mVarOverride; 133 uint32_t mLocaleListId; // 4 bytes 134 uint32_t mFontsCount; // 4 bytes 135 // OpenType supports up to 2^16-1 (uint16) axes. 136 // https://docs.microsoft.com/en-us/typography/opentype/spec/fvar 137 uint16_t mSupportedAxesCount; // 2 bytes 138 uint16_t mCmapFmt14CoverageCount; // 2 bytes 139 FamilyVariant mVariant; // 1 byte 140 bool mIsColorEmoji; // 1 byte 141 bool mIsCustomFallback; // 1 byte 142 bool mIsDefaultFallback; // 1 byte 143 VariationFamilyType mVarFamilyType; // 1byte 144 145 bool mIsVariationFamily; 146 MINIKIN_PREVENT_COPY_AND_ASSIGN(FontFamily); 147 }; 148 149 } // namespace minikin 150 151 #endif // MINIKIN_FONT_FAMILY_H 152