FontFamily.h revision 43bb9743ca91a761b9723674ac862660dd2be834
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 <vector> 21#include <string> 22#include <hb.h> 23 24#include <utils/TypeHelpers.h> 25 26#include <minikin/MinikinRefCounted.h> 27#include <minikin/SparseBitSet.h> 28 29namespace android { 30 31class MinikinFont; 32 33// FontLanguage is a compact representation of a bcp-47 language tag. It 34// does not capture all possible information, only what directly affects 35// font rendering. 36class FontLanguage { 37 friend class FontStyle; 38 friend class FontLanguages; 39public: 40 FontLanguage() : mBits(0) { } 41 42 // Parse from string 43 FontLanguage(const char* buf, size_t size); 44 45 bool operator==(const FontLanguage other) const { 46 return mBits != kUnsupportedLanguage && mBits == other.mBits; 47 } 48 operator bool() const { return mBits != 0; } 49 50 bool isUnsupported() const { return mBits == kUnsupportedLanguage; } 51 bool hasEmojiFlag() const { return isUnsupported() ? false : (mBits & kEmojiFlag); } 52 53 std::string getString() const; 54 55 // 0 = no match, 1 = language matches 56 int match(const FontLanguage other) const; 57 58private: 59 explicit FontLanguage(uint32_t bits) : mBits(bits) { } 60 61 uint32_t bits() const { return mBits; } 62 63 static const uint32_t kUnsupportedLanguage = 0xFFFFFFFFu; 64 static const uint32_t kBaseLangMask = 0xFFFFFFu; 65 static const uint32_t kHansFlag = 1u << 24; 66 static const uint32_t kHantFlag = 1u << 25; 67 static const uint32_t kEmojiFlag = 1u << 26; 68 static const uint32_t kScriptMask = kHansFlag | kHantFlag | kEmojiFlag; 69 uint32_t mBits; 70}; 71 72// A list of zero or more instances of FontLanguage, in the order of 73// preference. Used for further resolution of rendering results. 74class FontLanguages { 75public: 76 FontLanguages() { mLangs.clear(); } 77 78 // Parse from string, which is a comma-separated list of languages 79 FontLanguages(const char* buf, size_t size); 80 81 const FontLanguage& operator[](size_t index) const { return mLangs.at(index); } 82 83 size_t size() const { return mLangs.size(); } 84 85private: 86 std::vector<FontLanguage> mLangs; 87}; 88 89// FontStyle represents all style information needed to select an actual font 90// from a collection. The implementation is packed into two 32-bit words 91// so it can be efficiently copied, embedded in other objects, etc. 92class FontStyle { 93public: 94 FontStyle() : FontStyle(0 /* variant */, 4 /* weight */, false /* italic */) {} 95 FontStyle(int weight, bool italic) : FontStyle(0 /* variant */, weight, italic) {} 96 FontStyle(uint32_t langListId) 97 : FontStyle(langListId, 0 /* variant */, 4 /* weight */, false /* italic */) {} 98 99 FontStyle(int variant, int weight, bool italic); 100 FontStyle(uint32_t langListId, int variant, int weight, bool italic); 101 102 int getWeight() const { return bits & kWeightMask; } 103 bool getItalic() const { return (bits & kItalicMask) != 0; } 104 int getVariant() const { return (bits >> kVariantShift) & kVariantMask; } 105 uint32_t getLanguageListId() const { return mLanguageListId; } 106 107 bool operator==(const FontStyle other) const { 108 return bits == other.bits && mLanguageListId == other.mLanguageListId; 109 } 110 111 hash_t hash() const; 112 113 // Looks up a language list from an internal cache and returns its ID. 114 // If the passed language list is not in the cache, registers it and returns newly assigned ID. 115 static uint32_t registerLanguageList(const std::string& languages); 116private: 117 static const uint32_t kWeightMask = (1 << 4) - 1; 118 static const uint32_t kItalicMask = 1 << 4; 119 static const int kVariantShift = 5; 120 static const uint32_t kVariantMask = (1 << 2) - 1; 121 122 static uint32_t pack(int variant, int weight, bool italic); 123 124 uint32_t bits; 125 uint32_t mLanguageListId; 126}; 127 128enum FontVariant { 129 VARIANT_DEFAULT = 0, 130 VARIANT_COMPACT = 1, 131 VARIANT_ELEGANT = 2, 132}; 133 134inline hash_t hash_type(const FontStyle &style) { 135 return style.hash(); 136} 137 138// attributes representing transforms (fake bold, fake italic) to match styles 139class FontFakery { 140public: 141 FontFakery() : mFakeBold(false), mFakeItalic(false) { } 142 FontFakery(bool fakeBold, bool fakeItalic) : mFakeBold(fakeBold), mFakeItalic(fakeItalic) { } 143 // TODO: want to support graded fake bolding 144 bool isFakeBold() { return mFakeBold; } 145 bool isFakeItalic() { return mFakeItalic; } 146private: 147 bool mFakeBold; 148 bool mFakeItalic; 149}; 150 151struct FakedFont { 152 // ownership is the enclosing FontCollection 153 MinikinFont* font; 154 FontFakery fakery; 155}; 156 157class FontFamily : public MinikinRefCounted { 158public: 159 FontFamily() : mHbFont(nullptr) { } 160 161 FontFamily(FontLanguage lang, int variant) : mLang(lang), mVariant(variant), mHbFont(nullptr) { 162 } 163 164 ~FontFamily(); 165 166 // Add font to family, extracting style information from the font 167 bool addFont(MinikinFont* typeface); 168 169 void addFont(MinikinFont* typeface, FontStyle style); 170 FakedFont getClosestMatch(FontStyle style) const; 171 172 FontLanguage lang() const { return mLang; } 173 int variant() const { return mVariant; } 174 175 // API's for enumerating the fonts in a family. These don't guarantee any particular order 176 size_t getNumFonts() const; 177 MinikinFont* getFont(size_t index) const; 178 FontStyle getStyle(size_t index) const; 179 180 // Get Unicode coverage. Lifetime of returned bitset is same as receiver. May return nullptr on 181 // error. 182 const SparseBitSet* getCoverage(); 183 184 // Returns true if the font has a glyph for the code point and variation selector pair. 185 // Caller should acquire a lock before calling the method. 186 bool hasVariationSelector(uint32_t codepoint, uint32_t variationSelector); 187 188 // Purges cached mHbFont. 189 // hb_font_t keeps a reference to hb_face_t which is managed by HbFaceCache. Thus, 190 // it is good to purge hb_font_t once it is no longer necessary. 191 // Caller should acquire a lock before calling the method. 192 void purgeHbFontCache(); 193private: 194 void addFontLocked(MinikinFont* typeface, FontStyle style); 195 196 class Font { 197 public: 198 Font(MinikinFont* typeface, FontStyle style) : 199 typeface(typeface), style(style) { } 200 MinikinFont* typeface; 201 FontStyle style; 202 }; 203 FontLanguage mLang; 204 int mVariant; 205 std::vector<Font> mFonts; 206 207 SparseBitSet mCoverage; 208 bool mCoverageValid; 209 210 hb_font_t* mHbFont; 211}; 212 213} // namespace android 214 215#endif // MINIKIN_FONT_FAMILY_H 216