FontFamily.h revision 198b46f1fea3f47ef8eb6317799c0d77aaec52f6
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// FontStyle represents all style information needed to select an actual font 34// from a collection. The implementation is packed into two 32-bit words 35// so it can be efficiently copied, embedded in other objects, etc. 36class FontStyle { 37public: 38 FontStyle() : FontStyle(0 /* variant */, 4 /* weight */, false /* italic */) {} 39 FontStyle(int weight, bool italic) : FontStyle(0 /* variant */, weight, italic) {} 40 FontStyle(uint32_t langListId) 41 : FontStyle(langListId, 0 /* variant */, 4 /* weight */, false /* italic */) {} 42 43 FontStyle(int variant, int weight, bool italic); 44 FontStyle(uint32_t langListId, int variant, int weight, bool italic); 45 46 int getWeight() const { return bits & kWeightMask; } 47 bool getItalic() const { return (bits & kItalicMask) != 0; } 48 int getVariant() const { return (bits >> kVariantShift) & kVariantMask; } 49 uint32_t getLanguageListId() const { return mLanguageListId; } 50 51 bool operator==(const FontStyle other) const { 52 return bits == other.bits && mLanguageListId == other.mLanguageListId; 53 } 54 55 hash_t hash() const; 56 57 // Looks up a language list from an internal cache and returns its ID. 58 // If the passed language list is not in the cache, registers it and returns newly assigned ID. 59 static uint32_t registerLanguageList(const std::string& languages); 60private: 61 static const uint32_t kWeightMask = (1 << 4) - 1; 62 static const uint32_t kItalicMask = 1 << 4; 63 static const int kVariantShift = 5; 64 static const uint32_t kVariantMask = (1 << 2) - 1; 65 66 static uint32_t pack(int variant, int weight, bool italic); 67 68 uint32_t bits; 69 uint32_t mLanguageListId; 70}; 71 72enum FontVariant { 73 VARIANT_DEFAULT = 0, 74 VARIANT_COMPACT = 1, 75 VARIANT_ELEGANT = 2, 76}; 77 78inline hash_t hash_type(const FontStyle &style) { 79 return style.hash(); 80} 81 82// attributes representing transforms (fake bold, fake italic) to match styles 83class FontFakery { 84public: 85 FontFakery() : mFakeBold(false), mFakeItalic(false) { } 86 FontFakery(bool fakeBold, bool fakeItalic) : mFakeBold(fakeBold), mFakeItalic(fakeItalic) { } 87 // TODO: want to support graded fake bolding 88 bool isFakeBold() { return mFakeBold; } 89 bool isFakeItalic() { return mFakeItalic; } 90private: 91 bool mFakeBold; 92 bool mFakeItalic; 93}; 94 95struct FakedFont { 96 // ownership is the enclosing FontCollection 97 MinikinFont* font; 98 FontFakery fakery; 99}; 100 101class FontFamily : public MinikinRefCounted { 102public: 103 FontFamily() : mHbFont(nullptr) { } 104 105 FontFamily(int variant); 106 107 FontFamily(uint32_t langId, int variant) : mLangId(langId), mVariant(variant), mHbFont(nullptr) { 108 } 109 110 ~FontFamily(); 111 112 // Add font to family, extracting style information from the font 113 bool addFont(MinikinFont* typeface); 114 115 void addFont(MinikinFont* typeface, FontStyle style); 116 FakedFont getClosestMatch(FontStyle style) const; 117 118 uint32_t langId() const { return mLangId; } 119 int variant() const { return mVariant; } 120 121 // API's for enumerating the fonts in a family. These don't guarantee any particular order 122 size_t getNumFonts() const; 123 MinikinFont* getFont(size_t index) const; 124 FontStyle getStyle(size_t index) const; 125 126 // Get Unicode coverage. Lifetime of returned bitset is same as receiver. May return nullptr on 127 // error. 128 const SparseBitSet* getCoverage(); 129 130 // Returns true if the font has a glyph for the code point and variation selector pair. 131 // Caller should acquire a lock before calling the method. 132 bool hasVariationSelector(uint32_t codepoint, uint32_t variationSelector); 133 134 // Purges cached mHbFont. 135 // hb_font_t keeps a reference to hb_face_t which is managed by HbFaceCache. Thus, 136 // it is good to purge hb_font_t once it is no longer necessary. 137 // Caller should acquire a lock before calling the method. 138 void purgeHbFontCache(); 139private: 140 void addFontLocked(MinikinFont* typeface, FontStyle style); 141 142 class Font { 143 public: 144 Font(MinikinFont* typeface, FontStyle style) : 145 typeface(typeface), style(style) { } 146 MinikinFont* typeface; 147 FontStyle style; 148 }; 149 uint32_t mLangId; 150 int mVariant; 151 std::vector<Font> mFonts; 152 153 SparseBitSet mCoverage; 154 bool mCoverageValid; 155 156 hb_font_t* mHbFont; 157}; 158 159} // namespace android 160 161#endif // MINIKIN_FONT_FAMILY_H 162