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 std::vector<FontVariation>& 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; 62 FakedFont getVariationFamilyAdjustment(FontStyle style) const; 63 localeListId()64 uint32_t localeListId() const { return mLocaleListId; } variant()65 FamilyVariant variant() const { return mVariant; } 66 67 // API's for enumerating the fonts in a family. These don't guarantee any particular order getNumFonts()68 size_t getNumFonts() const { return mFontsCount; } getFont(size_t index)69 const Font* getFont(size_t index) const { return mFonts[index].get(); } getFontRef(size_t index)70 const std::shared_ptr<Font>& getFontRef(size_t index) const { return mFonts[index]; } getStyle(size_t index)71 FontStyle getStyle(size_t index) const { return mFonts[index]->style(); } isColorEmojiFamily()72 bool isColorEmojiFamily() const { return mIsColorEmoji; } getSupportedAxesCount()73 size_t getSupportedAxesCount() const { return mSupportedAxesCount; } getSupportedAxisAt(size_t index)74 AxisTag getSupportedAxisAt(size_t index) const { return mSupportedAxes[index]; } isCustomFallback()75 bool isCustomFallback() const { return mIsCustomFallback; } isDefaultFallback()76 bool isDefaultFallback() const { return mIsDefaultFallback; } 77 78 // Get Unicode coverage. getCoverage()79 const SparseBitSet& getCoverage() const { 80 if (mParent) [[unlikely]] { 81 return mParent->getCoverage(); 82 } else { 83 return mCoverage; 84 } 85 } 86 getCmap14Coverage(uint16_t vsIndex)87 const SparseBitSet& getCmap14Coverage(uint16_t vsIndex) const { 88 if (mParent) [[unlikely]] { 89 return mParent->getCmap14Coverage(vsIndex); 90 } else { 91 return mCmapFmt14Coverage[vsIndex]; 92 } 93 } 94 getParent()95 const std::shared_ptr<FontFamily>& getParent() const { return mParent; } 96 97 // Returns true if the font has a glyph for the code point and variation selector pair. 98 // Caller should acquire a lock before calling the method. 99 bool hasGlyph(uint32_t codepoint, uint32_t variationSelector) const; 100 101 // Returns true if this font family has a variaion sequence table (cmap format 14 subtable). hasVSTable()102 bool hasVSTable() const { return mCmapFmt14CoverageCount != 0; } 103 104 // Creates new FontFamily based on this family while applying font variations. Returns nullptr 105 // if none of variations apply to this family. 106 std::shared_ptr<FontFamily> createFamilyWithVariation( 107 const std::vector<FontVariation>& variations) const; 108 109 private: 110 FontFamily(uint32_t localeListId, FamilyVariant variant, 111 std::vector<std::shared_ptr<Font>>&& fonts, bool isCustomFallback, 112 bool isDefaultFallback, VariationFamilyType varFamilyType); 113 FontFamily(const std::shared_ptr<FontFamily>& parent, 114 const std::vector<FontVariation>& axesOverride); 115 explicit FontFamily(BufferReader* reader, const std::shared_ptr<std::vector<Font>>& fonts); 116 117 void writeTo(BufferWriter* writer, uint32_t* fontIndex) const; 118 119 void computeCoverage(); 120 121 // Note: to minimize padding, small member fields are grouped at the end. 122 std::unique_ptr<std::shared_ptr<Font>[]> mFonts; 123 // mSupportedAxes is sorted. 124 std::unique_ptr<AxisTag[]> mSupportedAxes; 125 // This field is empty if mParent is set. Use mParent's coverage instead. 126 SparseBitSet mCoverage; 127 // This field is empty if mParent is set. Use mParent's coverage instead. 128 std::unique_ptr<SparseBitSet[]> mCmapFmt14Coverage; 129 std::shared_ptr<FontFamily> mParent; 130 std::vector<FontVariation> mVarOverride; 131 uint32_t mLocaleListId; // 4 bytes 132 uint32_t mFontsCount; // 4 bytes 133 // OpenType supports up to 2^16-1 (uint16) axes. 134 // https://docs.microsoft.com/en-us/typography/opentype/spec/fvar 135 uint16_t mSupportedAxesCount; // 2 bytes 136 uint16_t mCmapFmt14CoverageCount; // 2 bytes 137 FamilyVariant mVariant; // 1 byte 138 bool mIsColorEmoji; // 1 byte 139 bool mIsCustomFallback; // 1 byte 140 bool mIsDefaultFallback; // 1 byte 141 VariationFamilyType mVarFamilyType; // 1byte 142 143 MINIKIN_PREVENT_COPY_AND_ASSIGN(FontFamily); 144 }; 145 146 } // namespace minikin 147 148 #endif // MINIKIN_FONT_FAMILY_H 149