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 class FontFamily { 34 public: 35 static std::shared_ptr<FontFamily> create(std::vector<std::shared_ptr<Font>>&& fonts); 36 static std::shared_ptr<FontFamily> create(FamilyVariant variant, 37 std::vector<std::shared_ptr<Font>>&& fonts); 38 static std::shared_ptr<FontFamily> create(uint32_t localeListId, FamilyVariant variant, 39 std::vector<std::shared_ptr<Font>>&& fonts, 40 bool isCustomFallback, bool isDefaultFallback); 41 42 FontFamily(FontFamily&&) = default; 43 FontFamily& operator=(FontFamily&&) = default; 44 45 static std::vector<std::shared_ptr<FontFamily>> readVector(BufferReader* reader); 46 static void writeVector(BufferWriter* writer, 47 const std::vector<std::shared_ptr<FontFamily>>& families); 48 49 FakedFont getClosestMatch(FontStyle style) const; 50 localeListId()51 uint32_t localeListId() const { return mLocaleListId; } variant()52 FamilyVariant variant() const { return mVariant; } 53 54 // API's for enumerating the fonts in a family. These don't guarantee any particular order getNumFonts()55 size_t getNumFonts() const { return mFontsCount; } getFont(size_t index)56 const Font* getFont(size_t index) const { return mFonts[index].get(); } getFontRef(size_t index)57 const std::shared_ptr<Font>& getFontRef(size_t index) const { return mFonts[index]; } getStyle(size_t index)58 FontStyle getStyle(size_t index) const { return mFonts[index]->style(); } isColorEmojiFamily()59 bool isColorEmojiFamily() const { return mIsColorEmoji; } getSupportedAxesCount()60 size_t getSupportedAxesCount() const { return mSupportedAxesCount; } getSupportedAxisAt(size_t index)61 AxisTag getSupportedAxisAt(size_t index) const { return mSupportedAxes[index]; } isCustomFallback()62 bool isCustomFallback() const { return mIsCustomFallback; } isDefaultFallback()63 bool isDefaultFallback() const { return mIsDefaultFallback; } 64 65 // Get Unicode coverage. getCoverage()66 const SparseBitSet& getCoverage() const { return mCoverage; } 67 68 // Returns true if the font has a glyph for the code point and variation selector pair. 69 // Caller should acquire a lock before calling the method. 70 bool hasGlyph(uint32_t codepoint, uint32_t variationSelector) const; 71 72 // Returns true if this font family has a variaion sequence table (cmap format 14 subtable). hasVSTable()73 bool hasVSTable() const { return mCmapFmt14CoverageCount != 0; } 74 75 // Creates new FontFamily based on this family while applying font variations. Returns nullptr 76 // if none of variations apply to this family. 77 std::shared_ptr<FontFamily> createFamilyWithVariation( 78 const std::vector<FontVariation>& variations) const; 79 80 private: 81 FontFamily(uint32_t localeListId, FamilyVariant variant, 82 std::vector<std::shared_ptr<Font>>&& fonts, bool isCustomFallback, 83 bool isDefaultFallback); 84 explicit FontFamily(BufferReader* reader, const std::shared_ptr<std::vector<Font>>& fonts); 85 86 void writeTo(BufferWriter* writer, uint32_t* fontIndex) const; 87 88 void computeCoverage(); 89 90 // Note: to minimize padding, small member fields are grouped at the end. 91 std::unique_ptr<std::shared_ptr<Font>[]> mFonts; 92 // mSupportedAxes is sorted. 93 std::unique_ptr<AxisTag[]> mSupportedAxes; 94 SparseBitSet mCoverage; 95 std::unique_ptr<SparseBitSet[]> mCmapFmt14Coverage; 96 uint32_t mLocaleListId; // 4 bytes 97 uint32_t mFontsCount; // 4 bytes 98 // OpenType supports up to 2^16-1 (uint16) axes. 99 // https://docs.microsoft.com/en-us/typography/opentype/spec/fvar 100 uint16_t mSupportedAxesCount; // 2 bytes 101 uint16_t mCmapFmt14CoverageCount; // 2 bytes 102 FamilyVariant mVariant; // 1 byte 103 bool mIsColorEmoji; // 1 byte 104 bool mIsCustomFallback; // 1 byte 105 bool mIsDefaultFallback; // 1 byte 106 107 MINIKIN_PREVENT_COPY_AND_ASSIGN(FontFamily); 108 }; 109 110 } // namespace minikin 111 112 #endif // MINIKIN_FONT_FAMILY_H 113