blob: 302cbd11da4b8f9b5237b972d7731b6336870595 [file] [log] [blame]
Seigo Nonaka50692ca2018-08-31 12:27:15 -07001/*
2 * Copyright (C) 2018 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#include <jni.h>
18
Seigo Nonakab3a7bce2019-03-29 14:24:57 -070019#include <android/font.h>
20#include <android/font_matcher.h>
Seigo Nonaka50692ca2018-08-31 12:27:15 -070021#include <android/system_fonts.h>
22
23#include <memory>
24#include <string>
25#include <vector>
26
27#include <errno.h>
28#include <fcntl.h>
29#include <libxml/tree.h>
30#include <log/log.h>
31#include <sys/stat.h>
32#include <unistd.h>
33
Seigo Nonaka75b841b2018-10-30 11:39:49 -070034#include <hwui/MinikinSkia.h>
35#include <minikin/FontCollection.h>
36#include <minikin/LocaleList.h>
37#include <minikin/SystemFonts.h>
38
Seigo Nonaka50692ca2018-08-31 12:27:15 -070039struct XmlCharDeleter {
40 void operator()(xmlChar* b) { xmlFree(b); }
41};
42
43struct XmlDocDeleter {
44 void operator()(xmlDoc* d) { xmlFreeDoc(d); }
45};
46
47using XmlCharUniquePtr = std::unique_ptr<xmlChar, XmlCharDeleter>;
48using XmlDocUniquePtr = std::unique_ptr<xmlDoc, XmlDocDeleter>;
49
50struct ASystemFontIterator {
51 XmlDocUniquePtr mXmlDoc;
52 xmlNode* mFontNode;
Seigo Nonaka36758982018-10-01 19:06:11 -070053
54 // The OEM customization XML.
55 XmlDocUniquePtr mCustomizationXmlDoc;
Seigo Nonaka50692ca2018-08-31 12:27:15 -070056};
57
Seigo Nonakab3a7bce2019-03-29 14:24:57 -070058struct AFont {
Seigo Nonaka50692ca2018-08-31 12:27:15 -070059 std::string mFilePath;
60 std::unique_ptr<std::string> mLocale;
61 uint16_t mWeight;
62 bool mItalic;
63 uint32_t mCollectionIndex;
64 std::vector<std::pair<uint32_t, float>> mAxes;
65};
66
Seigo Nonakab3a7bce2019-03-29 14:24:57 -070067struct AFontMatcher {
68 minikin::FontStyle mFontStyle;
69 uint32_t mLocaleListId = 0; // 0 is reserved for empty locale ID.
70 bool mFamilyVariant = AFAMILY_VARIANT_DEFAULT;
71};
72
73static_assert(static_cast<uint32_t>(AFAMILY_VARIANT_DEFAULT) ==
74 static_cast<uint32_t>(minikin::FamilyVariant::DEFAULT));
75static_assert(static_cast<uint32_t>(AFAMILY_VARIANT_COMPACT) ==
76 static_cast<uint32_t>(minikin::FamilyVariant::COMPACT));
77static_assert(static_cast<uint32_t>(AFAMILY_VARIANT_ELEGANT) ==
78 static_cast<uint32_t>(minikin::FamilyVariant::ELEGANT));
79
Seigo Nonaka50692ca2018-08-31 12:27:15 -070080namespace {
81
82std::string xmlTrim(const std::string& in) {
83 if (in.empty()) {
84 return in;
85 }
86 const char XML_SPACES[] = "\u0020\u000D\u000A\u0009";
87 const size_t start = in.find_first_not_of(XML_SPACES); // inclusive
88 if (start == std::string::npos) {
89 return "";
90 }
91 const size_t end = in.find_last_not_of(XML_SPACES); // inclusive
92 if (end == std::string::npos) {
93 return "";
94 }
95 return in.substr(start, end - start + 1 /* +1 since end is inclusive */);
96}
97
98const xmlChar* FAMILY_TAG = BAD_CAST("family");
99const xmlChar* FONT_TAG = BAD_CAST("font");
100
101xmlNode* firstElement(xmlNode* node, const xmlChar* tag) {
102 for (xmlNode* child = node->children; child; child = child->next) {
103 if (xmlStrEqual(child->name, tag)) {
104 return child;
105 }
106 }
107 return nullptr;
108}
109
110xmlNode* nextSibling(xmlNode* node, const xmlChar* tag) {
111 while ((node = node->next) != nullptr) {
112 if (xmlStrEqual(node->name, tag)) {
113 return node;
114 }
115 }
116 return nullptr;
117}
118
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700119void copyFont(const XmlDocUniquePtr& xmlDoc, xmlNode* fontNode, AFont* out,
Seigo Nonaka36758982018-10-01 19:06:11 -0700120 const std::string& pathPrefix) {
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700121 const xmlChar* LOCALE_ATTR_NAME = BAD_CAST("lang");
122 XmlCharUniquePtr filePathStr(
Seigo Nonaka36758982018-10-01 19:06:11 -0700123 xmlNodeListGetString(xmlDoc.get(), fontNode->xmlChildrenNode, 1));
124 out->mFilePath = pathPrefix + xmlTrim(
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700125 std::string(filePathStr.get(), filePathStr.get() + xmlStrlen(filePathStr.get())));
126
127 const xmlChar* WEIGHT_ATTR_NAME = BAD_CAST("weight");
Seigo Nonaka36758982018-10-01 19:06:11 -0700128 XmlCharUniquePtr weightStr(xmlGetProp(fontNode, WEIGHT_ATTR_NAME));
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700129 out->mWeight = weightStr ?
130 strtol(reinterpret_cast<const char*>(weightStr.get()), nullptr, 10) : 400;
131
132 const xmlChar* STYLE_ATTR_NAME = BAD_CAST("style");
133 const xmlChar* ITALIC_ATTR_VALUE = BAD_CAST("italic");
Seigo Nonaka36758982018-10-01 19:06:11 -0700134 XmlCharUniquePtr styleStr(xmlGetProp(fontNode, STYLE_ATTR_NAME));
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700135 out->mItalic = styleStr ? xmlStrEqual(styleStr.get(), ITALIC_ATTR_VALUE) : false;
136
137 const xmlChar* INDEX_ATTR_NAME = BAD_CAST("index");
Seigo Nonaka36758982018-10-01 19:06:11 -0700138 XmlCharUniquePtr indexStr(xmlGetProp(fontNode, INDEX_ATTR_NAME));
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700139 out->mCollectionIndex = indexStr ?
140 strtol(reinterpret_cast<const char*>(indexStr.get()), nullptr, 10) : 0;
141
Seigo Nonaka36758982018-10-01 19:06:11 -0700142 XmlCharUniquePtr localeStr(xmlGetProp(xmlDoc->parent, LOCALE_ATTR_NAME));
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700143 out->mLocale.reset(
144 localeStr ? new std::string(reinterpret_cast<const char*>(localeStr.get())) : nullptr);
145
146 const xmlChar* TAG_ATTR_NAME = BAD_CAST("tag");
147 const xmlChar* STYLEVALUE_ATTR_NAME = BAD_CAST("stylevalue");
148 const xmlChar* AXIS_TAG = BAD_CAST("axis");
149 out->mAxes.clear();
Seigo Nonaka36758982018-10-01 19:06:11 -0700150 for (xmlNode* axis = firstElement(fontNode, AXIS_TAG); axis;
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700151 axis = nextSibling(axis, AXIS_TAG)) {
152 XmlCharUniquePtr tagStr(xmlGetProp(axis, TAG_ATTR_NAME));
153 if (!tagStr || xmlStrlen(tagStr.get()) != 4) {
154 continue; // Tag value must be 4 char string
155 }
156
157 XmlCharUniquePtr styleValueStr(xmlGetProp(axis, STYLEVALUE_ATTR_NAME));
158 if (!styleValueStr) {
159 continue;
160 }
161
162 uint32_t tag =
163 static_cast<uint32_t>(tagStr.get()[0] << 24) |
164 static_cast<uint32_t>(tagStr.get()[1] << 16) |
165 static_cast<uint32_t>(tagStr.get()[2] << 8) |
166 static_cast<uint32_t>(tagStr.get()[3]);
167 float styleValue = strtod(reinterpret_cast<const char*>(styleValueStr.get()), nullptr);
168 out->mAxes.push_back(std::make_pair(tag, styleValue));
169 }
170}
171
172bool isFontFileAvailable(const std::string& filePath) {
173 std::string fullPath = filePath;
174 struct stat st = {};
175 if (stat(fullPath.c_str(), &st) != 0) {
176 return false;
177 }
178 return S_ISREG(st.st_mode);
179}
180
Seigo Nonaka36758982018-10-01 19:06:11 -0700181xmlNode* findFirstFontNode(const XmlDocUniquePtr& doc) {
182 xmlNode* familySet = xmlDocGetRootElement(doc.get());
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700183 if (familySet == nullptr) {
184 return nullptr;
185 }
186 xmlNode* family = firstElement(familySet, FAMILY_TAG);
187 if (family == nullptr) {
188 return nullptr;
189 }
190
191 xmlNode* font = firstElement(family, FONT_TAG);
192 while (font == nullptr) {
193 family = nextSibling(family, FAMILY_TAG);
194 if (family == nullptr) {
195 return nullptr;
196 }
197 font = firstElement(family, FONT_TAG);
198 }
199 return font;
200}
201
202} // namespace
203
204ASystemFontIterator* ASystemFontIterator_open() {
205 std::unique_ptr<ASystemFontIterator> ite(new ASystemFontIterator());
206 ite->mXmlDoc.reset(xmlReadFile("/system/etc/fonts.xml", nullptr, 0));
Seigo Nonaka36758982018-10-01 19:06:11 -0700207 ite->mCustomizationXmlDoc.reset(xmlReadFile("/product/etc/fonts_customization.xml", nullptr, 0));
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700208 return ite.release();
209}
210
211void ASystemFontIterator_close(ASystemFontIterator* ite) {
212 delete ite;
213}
214
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700215AFontMatcher* _Nonnull AFontMatcher_create() {
216 return new AFontMatcher();
217}
218
219void AFontMatcher_destroy(AFontMatcher* matcher) {
220 delete matcher;
221}
222
223void AFontMatcher_setStyle(
224 AFontMatcher* _Nonnull matcher,
Seigo Nonaka75b841b2018-10-30 11:39:49 -0700225 uint16_t weight,
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700226 bool italic) {
227 matcher->mFontStyle = minikin::FontStyle(
228 weight, static_cast<minikin::FontStyle::Slant>(italic));
229}
230
231void AFontMatcher_setLocales(
232 AFontMatcher* _Nonnull matcher,
233 const char* _Nonnull languageTags) {
234 matcher->mLocaleListId = minikin::registerLocaleList(languageTags);
235}
236
237void AFontMatcher_setFamilyVariant(AFontMatcher* _Nonnull matcher, uint32_t familyVariant) {
238 matcher->mFamilyVariant = familyVariant;
239}
240
241AFont* _Nonnull AFontMatcher_match(
242 const AFontMatcher* _Nonnull matcher,
243 const char* _Nonnull familyName,
Seigo Nonaka75b841b2018-10-30 11:39:49 -0700244 const uint16_t* _Nonnull text,
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700245 const uint32_t textLength,
Seigo Nonaka75b841b2018-10-30 11:39:49 -0700246 uint32_t* _Nullable runLength) {
247 std::shared_ptr<minikin::FontCollection> fc =
248 minikin::SystemFonts::findFontCollection(familyName);
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700249 std::vector<minikin::FontCollection::Run> runs = fc->itemize(
250 minikin::U16StringPiece(text, textLength),
251 matcher->mFontStyle,
252 matcher->mLocaleListId,
253 static_cast<minikin::FamilyVariant>(matcher->mFamilyVariant));
Seigo Nonaka75b841b2018-10-30 11:39:49 -0700254
255 const minikin::Font* font = runs[0].fakedFont.font;
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700256 std::unique_ptr<AFont> result = std::make_unique<AFont>();
Seigo Nonaka75b841b2018-10-30 11:39:49 -0700257 const android::MinikinFontSkia* minikinFontSkia =
258 reinterpret_cast<android::MinikinFontSkia*>(font->typeface().get());
259 result->mFilePath = minikinFontSkia->getFilePath();
260 result->mWeight = font->style().weight();
261 result->mItalic = font->style().slant() == minikin::FontStyle::Slant::ITALIC;
262 result->mCollectionIndex = minikinFontSkia->GetFontIndex();
263 const std::vector<minikin::FontVariation>& axes = minikinFontSkia->GetAxes();
264 result->mAxes.reserve(axes.size());
265 for (auto axis : axes) {
266 result->mAxes.push_back(std::make_pair(axis.axisTag, axis.value));
267 }
268 if (runLength != nullptr) {
269 *runLength = runs[0].end;
270 }
271 return result.release();
272}
273
Seigo Nonaka36758982018-10-01 19:06:11 -0700274xmlNode* findNextFontNode(const XmlDocUniquePtr& xmlDoc, xmlNode* fontNode) {
275 if (fontNode == nullptr) {
276 if (!xmlDoc) {
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700277 return nullptr; // Already at the end.
278 } else {
279 // First time to query font.
Seigo Nonaka36758982018-10-01 19:06:11 -0700280 return findFirstFontNode(xmlDoc);
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700281 }
282 } else {
Seigo Nonaka36758982018-10-01 19:06:11 -0700283 xmlNode* nextNode = nextSibling(fontNode, FONT_TAG);
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700284 while (nextNode == nullptr) {
Seigo Nonaka36758982018-10-01 19:06:11 -0700285 xmlNode* family = nextSibling(fontNode->parent, FAMILY_TAG);
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700286 if (family == nullptr) {
287 break;
288 }
289 nextNode = firstElement(family, FONT_TAG);
290 }
Seigo Nonaka36758982018-10-01 19:06:11 -0700291 return nextNode;
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700292 }
293}
294
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700295AFont* ASystemFontIterator_next(ASystemFontIterator* ite) {
Seigo Nonaka36758982018-10-01 19:06:11 -0700296 LOG_ALWAYS_FATAL_IF(ite == nullptr, "nullptr has passed as iterator argument");
297 if (ite->mXmlDoc) {
298 ite->mFontNode = findNextFontNode(ite->mXmlDoc, ite->mFontNode);
299 if (ite->mFontNode == nullptr) {
300 // Reached end of the XML file. Continue OEM customization.
301 ite->mXmlDoc.reset();
302 ite->mFontNode = nullptr;
303 } else {
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700304 std::unique_ptr<AFont> font = std::make_unique<AFont>();
Seigo Nonaka36758982018-10-01 19:06:11 -0700305 copyFont(ite->mXmlDoc, ite->mFontNode, font.get(), "/system/fonts/");
306 if (!isFontFileAvailable(font->mFilePath)) {
307 return ASystemFontIterator_next(ite);
308 }
309 return font.release();
310 }
311 }
312 if (ite->mCustomizationXmlDoc) {
313 // TODO: Filter only customizationType="new-named-family"
314 ite->mFontNode = findNextFontNode(ite->mCustomizationXmlDoc, ite->mFontNode);
315 if (ite->mFontNode == nullptr) {
316 // Reached end of the XML file. Finishing
317 ite->mCustomizationXmlDoc.reset();
318 ite->mFontNode = nullptr;
319 return nullptr;
320 } else {
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700321 std::unique_ptr<AFont> font = std::make_unique<AFont>();
Seigo Nonaka36758982018-10-01 19:06:11 -0700322 copyFont(ite->mCustomizationXmlDoc, ite->mFontNode, font.get(), "/product/fonts/");
323 if (!isFontFileAvailable(font->mFilePath)) {
324 return ASystemFontIterator_next(ite);
325 }
326 return font.release();
327 }
328 }
329 return nullptr;
330}
331
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700332void AFont_close(AFont* font) {
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700333 delete font;
334}
335
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700336const char* AFont_getFontFilePath(const AFont* font) {
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700337 LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed as font argument");
338 return font->mFilePath.c_str();
339}
340
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700341uint16_t AFont_getWeight(const AFont* font) {
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700342 LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed as font argument");
343 return font->mWeight;
344}
345
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700346bool AFont_isItalic(const AFont* font) {
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700347 LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed as font argument");
348 return font->mItalic;
349}
350
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700351const char* AFont_getLocale(const AFont* font) {
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700352 LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed to font argument");
353 return font->mLocale ? nullptr : font->mLocale->c_str();
354}
355
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700356size_t AFont_getCollectionIndex(const AFont* font) {
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700357 LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed to font argument");
358 return font->mCollectionIndex;
359}
360
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700361size_t AFont_getAxisCount(const AFont* font) {
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700362 LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed to font argument");
363 return font->mAxes.size();
364}
365
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700366uint32_t AFont_getAxisTag(const AFont* font, uint32_t axisIndex) {
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700367 LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed to font argument");
368 LOG_ALWAYS_FATAL_IF(axisIndex >= font->mAxes.size(),
369 "given axis index is out of bounds. (< %zd", font->mAxes.size());
370 return font->mAxes[axisIndex].first;
371}
372
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700373float AFont_getAxisValue(const AFont* font, uint32_t axisIndex) {
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700374 LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed to font argument");
375 LOG_ALWAYS_FATAL_IF(axisIndex >= font->mAxes.size(),
376 "given axis index is out of bounds. (< %zd", font->mAxes.size());
377 return font->mAxes[axisIndex].second;
378}