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 <unordered_set> 23 #include <vector> 24 25 #include "minikin/FamilyVariant.h" 26 #include "minikin/Font.h" 27 #include "minikin/FontStyle.h" 28 #include "minikin/HbUtils.h" 29 #include "minikin/Macros.h" 30 #include "minikin/SparseBitSet.h" 31 32 namespace minikin { 33 34 class FontFamily { 35 public: 36 explicit FontFamily(std::vector<std::shared_ptr<Font>>&& fonts); 37 FontFamily(FamilyVariant variant, std::vector<std::shared_ptr<Font>>&& fonts); 38 FontFamily(uint32_t localeListId, FamilyVariant variant, 39 std::vector<std::shared_ptr<Font>>&& fonts, bool isCustomFallback); 40 41 template <Font::TypefaceReader typefaceReader> readFrom(BufferReader * reader)42 static std::shared_ptr<FontFamily> readFrom(BufferReader* reader) { 43 uint32_t localeListId = readLocaleListInternal(reader); 44 uint32_t fontsCount = reader->read<uint32_t>(); 45 std::vector<std::shared_ptr<Font>> fonts; 46 fonts.reserve(fontsCount); 47 for (uint32_t i = 0; i < fontsCount; i++) { 48 fonts.emplace_back(Font::readFrom<typefaceReader>(reader, localeListId)); 49 } 50 return readFromInternal(reader, std::move(fonts), localeListId); 51 } 52 53 template <Font::TypefaceWriter typefaceWriter> writeTo(BufferWriter * writer)54 void writeTo(BufferWriter* writer) const { 55 writeLocaleListInternal(writer); 56 writer->write<uint32_t>(mFonts.size()); 57 for (const std::shared_ptr<Font>& font : mFonts) { 58 font->writeTo<typefaceWriter>(writer); 59 } 60 writeToInternal(writer); 61 } 62 63 FakedFont getClosestMatch(FontStyle style) const; 64 localeListId()65 uint32_t localeListId() const { return mLocaleListId; } variant()66 FamilyVariant variant() const { return mVariant; } 67 68 // API's for enumerating the fonts in a family. These don't guarantee any particular order getNumFonts()69 size_t getNumFonts() const { return mFonts.size(); } getFont(size_t index)70 const Font* getFont(size_t index) const { return mFonts[index].get(); } getFontRef(size_t index)71 const std::shared_ptr<Font>& getFontRef(size_t index) const { return mFonts[index]; } getStyle(size_t index)72 FontStyle getStyle(size_t index) const { return mFonts[index]->style(); } isColorEmojiFamily()73 bool isColorEmojiFamily() const { return mIsColorEmoji; } supportedAxes()74 const std::unordered_set<AxisTag>& supportedAxes() const { return mSupportedAxes; } isCustomFallback()75 bool isCustomFallback() const { return mIsCustomFallback; } 76 77 // Get Unicode coverage. getCoverage()78 const SparseBitSet& getCoverage() const { return mCoverage; } 79 80 // Returns true if the font has a glyph for the code point and variation selector pair. 81 // Caller should acquire a lock before calling the method. 82 bool hasGlyph(uint32_t codepoint, uint32_t variationSelector) const; 83 84 // Returns true if this font family has a variaion sequence table (cmap format 14 subtable). hasVSTable()85 bool hasVSTable() const { return !mCmapFmt14Coverage.empty(); } 86 87 // Creates new FontFamily based on this family while applying font variations. Returns nullptr 88 // if none of variations apply to this family. 89 std::shared_ptr<FontFamily> createFamilyWithVariation( 90 const std::vector<FontVariation>& variations) const; 91 92 private: 93 FontFamily(uint32_t localeListId, FamilyVariant variant, 94 std::vector<std::shared_ptr<Font>>&& fonts, 95 std::unordered_set<AxisTag>&& supportedAxes, bool isColorEmoji, 96 bool isCustomFallback, SparseBitSet&& coverage, 97 std::vector<std::unique_ptr<SparseBitSet>>&& cmapFmt14Coverage); 98 99 static uint32_t readLocaleListInternal(BufferReader* reader); 100 static std::shared_ptr<FontFamily> readFromInternal(BufferReader* reader, 101 std::vector<std::shared_ptr<Font>>&& fonts, 102 uint32_t localeListId); 103 void writeLocaleListInternal(BufferWriter* writer) const; 104 void writeToInternal(BufferWriter* writer) const; 105 106 void computeCoverage(); 107 108 uint32_t mLocaleListId; 109 FamilyVariant mVariant; 110 std::vector<std::shared_ptr<Font>> mFonts; 111 std::unordered_set<AxisTag> mSupportedAxes; 112 bool mIsColorEmoji; 113 bool mIsCustomFallback; 114 115 SparseBitSet mCoverage; 116 std::vector<std::unique_ptr<SparseBitSet>> mCmapFmt14Coverage; 117 118 MINIKIN_PREVENT_COPY_AND_ASSIGN(FontFamily); 119 }; 120 121 } // namespace minikin 122 123 #endif // MINIKIN_FONT_FAMILY_H 124