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 // #define VERBOSE_DEBUG
18
19 #define LOG_TAG "Minikin"
20 #include <cutils/log.h>
21
22 #include "unicode/unistr.h"
23 #include "unicode/unorm2.h"
24
25 #include "MinikinInternal.h"
26 #include <minikin/FontCollection.h>
27
28 using std::vector;
29
30 namespace android {
31
32 template <typename T>
max(T a,T b)33 static inline T max(T a, T b) {
34 return a>b ? a : b;
35 }
36
37 uint32_t FontCollection::sNextId = 0;
38
FontCollection(const vector<FontFamily * > & typefaces)39 FontCollection::FontCollection(const vector<FontFamily*>& typefaces) :
40 mMaxChar(0) {
41 AutoMutex _l(gMinikinLock);
42 mId = sNextId++;
43 vector<uint32_t> lastChar;
44 size_t nTypefaces = typefaces.size();
45 #ifdef VERBOSE_DEBUG
46 ALOGD("nTypefaces = %d\n", nTypefaces);
47 #endif
48 const FontStyle defaultStyle;
49 for (size_t i = 0; i < nTypefaces; i++) {
50 FontFamily* family = typefaces[i];
51 MinikinFont* typeface = family->getClosestMatch(defaultStyle).font;
52 if (typeface == NULL) {
53 continue;
54 }
55 family->RefLocked();
56 const SparseBitSet* coverage = family->getCoverage();
57 if (coverage == nullptr) {
58 family->UnrefLocked();
59 continue;
60 }
61 mFamilies.push_back(family); // emplace_back would be better
62 mMaxChar = max(mMaxChar, coverage->length());
63 lastChar.push_back(coverage->nextSetBit(0));
64 }
65 nTypefaces = mFamilies.size();
66 LOG_ALWAYS_FATAL_IF(nTypefaces == 0,
67 "Font collection must have at least one valid typeface");
68 size_t nPages = (mMaxChar + kPageMask) >> kLogCharsPerPage;
69 size_t offset = 0;
70 for (size_t i = 0; i < nPages; i++) {
71 Range dummy;
72 mRanges.push_back(dummy);
73 Range* range = &mRanges.back();
74 #ifdef VERBOSE_DEBUG
75 ALOGD("i=%d: range start = %d\n", i, offset);
76 #endif
77 range->start = offset;
78 for (size_t j = 0; j < nTypefaces; j++) {
79 if (lastChar[j] < (i + 1) << kLogCharsPerPage) {
80 FontFamily* family = mFamilies[j];
81 mFamilyVec.push_back(family);
82 offset++;
83 uint32_t nextChar = family->getCoverage()->nextSetBit((i + 1) << kLogCharsPerPage);
84 #ifdef VERBOSE_DEBUG
85 ALOGD("nextChar = %d (j = %d)\n", nextChar, j);
86 #endif
87 lastChar[j] = nextChar;
88 }
89 }
90 range->end = offset;
91 }
92 }
93
~FontCollection()94 FontCollection::~FontCollection() {
95 for (size_t i = 0; i < mFamilies.size(); i++) {
96 mFamilies[i]->UnrefLocked();
97 }
98 }
99
100 // Implement heuristic for choosing best-match font. Here are the rules:
101 // 1. If first font in the collection has the character, it wins.
102 // 2. If a font matches both language and script, it gets a score of 4.
103 // 3. If a font matches just language, it gets a score of 2.
104 // 4. Matching the "compact" or "elegant" variant adds one to the score.
105 // 5. Highest score wins, with ties resolved to the first font.
getFamilyForChar(uint32_t ch,FontLanguage lang,int variant) const106 FontFamily* FontCollection::getFamilyForChar(uint32_t ch,
107 FontLanguage lang, int variant) const {
108 if (ch >= mMaxChar) {
109 return NULL;
110 }
111 const Range& range = mRanges[ch >> kLogCharsPerPage];
112 #ifdef VERBOSE_DEBUG
113 ALOGD("querying range %d:%d\n", range.start, range.end);
114 #endif
115 FontFamily* bestFamily = NULL;
116 int bestScore = -1;
117 for (size_t i = range.start; i < range.end; i++) {
118 FontFamily* family = mFamilyVec[i];
119 if (family->getCoverage()->get(ch)) {
120 // First font family in collection always matches
121 if (mFamilies[0] == family) {
122 return family;
123 }
124 int score = lang.match(family->lang()) * 2;
125 if (family->variant() == 0 || family->variant() == variant) {
126 score++;
127 }
128 if (score > bestScore) {
129 bestScore = score;
130 bestFamily = family;
131 }
132 }
133 }
134 if (bestFamily == NULL && !mFamilyVec.empty()) {
135 UErrorCode errorCode = U_ZERO_ERROR;
136 const UNormalizer2* normalizer = unorm2_getNFDInstance(&errorCode);
137 if (U_SUCCESS(errorCode)) {
138 UChar decomposed[4];
139 int len = unorm2_getRawDecomposition(normalizer, ch, decomposed, 4, &errorCode);
140 if (U_SUCCESS(errorCode) && len > 0) {
141 int off = 0;
142 U16_NEXT_UNSAFE(decomposed, off, ch);
143 return getFamilyForChar(ch, lang, variant);
144 }
145 }
146 bestFamily = mFamilies[0];
147 }
148 return bestFamily;
149 }
150
151 const uint32_t NBSP = 0xa0;
152 const uint32_t ZWJ = 0x200c;
153 const uint32_t ZWNJ = 0x200d;
154 const uint32_t KEYCAP = 0x20e3;
155 const uint32_t HYPHEN = 0x2010;
156 const uint32_t NB_HYPHEN = 0x2011;
157
158 // Characters where we want to continue using existing font run instead of
159 // recomputing the best match in the fallback list.
160 static const uint32_t stickyWhitelist[] = { '!', ',', '-', '.', ':', ';', '?', NBSP, ZWJ, ZWNJ,
161 KEYCAP, HYPHEN, NB_HYPHEN };
162
isStickyWhitelisted(uint32_t c)163 static bool isStickyWhitelisted(uint32_t c) {
164 for (size_t i = 0; i < sizeof(stickyWhitelist) / sizeof(stickyWhitelist[0]); i++) {
165 if (stickyWhitelist[i] == c) return true;
166 }
167 return false;
168 }
169
itemize(const uint16_t * string,size_t string_size,FontStyle style,vector<Run> * result) const170 void FontCollection::itemize(const uint16_t *string, size_t string_size, FontStyle style,
171 vector<Run>* result) const {
172 FontLanguage lang = style.getLanguage();
173 int variant = style.getVariant();
174 FontFamily* lastFamily = NULL;
175 Run* run = NULL;
176 int nShorts;
177 for (size_t i = 0; i < string_size; i += nShorts) {
178 nShorts = 1;
179 uint32_t ch = string[i];
180 // sigh, decode UTF-16 by hand here
181 if ((ch & 0xfc00) == 0xd800) {
182 if ((i + 1) < string_size) {
183 ch = 0x10000 + ((ch & 0x3ff) << 10) + (string[i + 1] & 0x3ff);
184 nShorts = 2;
185 }
186 }
187 // Continue using existing font as long as it has coverage and is whitelisted
188 if (lastFamily == NULL
189 || !(isStickyWhitelisted(ch) && lastFamily->getCoverage()->get(ch))) {
190 FontFamily* family = getFamilyForChar(ch, lang, variant);
191 if (i == 0 || family != lastFamily) {
192 size_t start = i;
193 // Workaround for Emoji keycap until we implement per-cluster font
194 // selection: if keycap is found in a different font that also
195 // supports previous char, attach previous char to the new run.
196 // Only handles non-surrogate characters.
197 // Bug 7557244.
198 if (ch == KEYCAP && i && family && family->getCoverage()->get(string[i - 1])) {
199 run->end--;
200 if (run->start == run->end) {
201 result->pop_back();
202 }
203 start--;
204 }
205 Run dummy;
206 result->push_back(dummy);
207 run = &result->back();
208 if (family == NULL) {
209 run->fakedFont.font = NULL;
210 } else {
211 run->fakedFont = family->getClosestMatch(style);
212 }
213 lastFamily = family;
214 run->start = start;
215 }
216 }
217 run->end = i + nShorts;
218 }
219 }
220
baseFont(FontStyle style)221 MinikinFont* FontCollection::baseFont(FontStyle style) {
222 return baseFontFaked(style).font;
223 }
224
baseFontFaked(FontStyle style)225 FakedFont FontCollection::baseFontFaked(FontStyle style) {
226 if (mFamilies.empty()) {
227 return FakedFont();
228 }
229 return mFamilies[0]->getClosestMatch(style);
230 }
231
getId() const232 uint32_t FontCollection::getId() const {
233 return mId;
234 }
235
236 } // namespace android
237