blob: 4d3d1d66ffda796a92b6f1d9c8e5b5129e13fb76 [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
19#include <android/system_fonts.h>
20
21#include <memory>
22#include <string>
23#include <vector>
24
25#include <errno.h>
26#include <fcntl.h>
27#include <libxml/tree.h>
28#include <log/log.h>
29#include <sys/stat.h>
30#include <unistd.h>
31
Seigo Nonaka75b841b2018-10-30 11:39:49 -070032#include <hwui/MinikinSkia.h>
33#include <minikin/FontCollection.h>
34#include <minikin/LocaleList.h>
35#include <minikin/SystemFonts.h>
36
Seigo Nonaka50692ca2018-08-31 12:27:15 -070037struct XmlCharDeleter {
38 void operator()(xmlChar* b) { xmlFree(b); }
39};
40
41struct XmlDocDeleter {
42 void operator()(xmlDoc* d) { xmlFreeDoc(d); }
43};
44
45using XmlCharUniquePtr = std::unique_ptr<xmlChar, XmlCharDeleter>;
46using XmlDocUniquePtr = std::unique_ptr<xmlDoc, XmlDocDeleter>;
47
48struct ASystemFontIterator {
49 XmlDocUniquePtr mXmlDoc;
50 xmlNode* mFontNode;
Seigo Nonaka36758982018-10-01 19:06:11 -070051
52 // The OEM customization XML.
53 XmlDocUniquePtr mCustomizationXmlDoc;
Seigo Nonaka50692ca2018-08-31 12:27:15 -070054};
55
56struct ASystemFont {
57 std::string mFilePath;
58 std::unique_ptr<std::string> mLocale;
59 uint16_t mWeight;
60 bool mItalic;
61 uint32_t mCollectionIndex;
62 std::vector<std::pair<uint32_t, float>> mAxes;
63};
64
65namespace {
66
67std::string xmlTrim(const std::string& in) {
68 if (in.empty()) {
69 return in;
70 }
71 const char XML_SPACES[] = "\u0020\u000D\u000A\u0009";
72 const size_t start = in.find_first_not_of(XML_SPACES); // inclusive
73 if (start == std::string::npos) {
74 return "";
75 }
76 const size_t end = in.find_last_not_of(XML_SPACES); // inclusive
77 if (end == std::string::npos) {
78 return "";
79 }
80 return in.substr(start, end - start + 1 /* +1 since end is inclusive */);
81}
82
83const xmlChar* FAMILY_TAG = BAD_CAST("family");
84const xmlChar* FONT_TAG = BAD_CAST("font");
85
86xmlNode* firstElement(xmlNode* node, const xmlChar* tag) {
87 for (xmlNode* child = node->children; child; child = child->next) {
88 if (xmlStrEqual(child->name, tag)) {
89 return child;
90 }
91 }
92 return nullptr;
93}
94
95xmlNode* nextSibling(xmlNode* node, const xmlChar* tag) {
96 while ((node = node->next) != nullptr) {
97 if (xmlStrEqual(node->name, tag)) {
98 return node;
99 }
100 }
101 return nullptr;
102}
103
Seigo Nonaka36758982018-10-01 19:06:11 -0700104void copyFont(const XmlDocUniquePtr& xmlDoc, xmlNode* fontNode, ASystemFont* out,
105 const std::string& pathPrefix) {
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700106 const xmlChar* LOCALE_ATTR_NAME = BAD_CAST("lang");
107 XmlCharUniquePtr filePathStr(
Seigo Nonaka36758982018-10-01 19:06:11 -0700108 xmlNodeListGetString(xmlDoc.get(), fontNode->xmlChildrenNode, 1));
109 out->mFilePath = pathPrefix + xmlTrim(
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700110 std::string(filePathStr.get(), filePathStr.get() + xmlStrlen(filePathStr.get())));
111
112 const xmlChar* WEIGHT_ATTR_NAME = BAD_CAST("weight");
Seigo Nonaka36758982018-10-01 19:06:11 -0700113 XmlCharUniquePtr weightStr(xmlGetProp(fontNode, WEIGHT_ATTR_NAME));
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700114 out->mWeight = weightStr ?
115 strtol(reinterpret_cast<const char*>(weightStr.get()), nullptr, 10) : 400;
116
117 const xmlChar* STYLE_ATTR_NAME = BAD_CAST("style");
118 const xmlChar* ITALIC_ATTR_VALUE = BAD_CAST("italic");
Seigo Nonaka36758982018-10-01 19:06:11 -0700119 XmlCharUniquePtr styleStr(xmlGetProp(fontNode, STYLE_ATTR_NAME));
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700120 out->mItalic = styleStr ? xmlStrEqual(styleStr.get(), ITALIC_ATTR_VALUE) : false;
121
122 const xmlChar* INDEX_ATTR_NAME = BAD_CAST("index");
Seigo Nonaka36758982018-10-01 19:06:11 -0700123 XmlCharUniquePtr indexStr(xmlGetProp(fontNode, INDEX_ATTR_NAME));
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700124 out->mCollectionIndex = indexStr ?
125 strtol(reinterpret_cast<const char*>(indexStr.get()), nullptr, 10) : 0;
126
Seigo Nonaka36758982018-10-01 19:06:11 -0700127 XmlCharUniquePtr localeStr(xmlGetProp(xmlDoc->parent, LOCALE_ATTR_NAME));
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700128 out->mLocale.reset(
129 localeStr ? new std::string(reinterpret_cast<const char*>(localeStr.get())) : nullptr);
130
131 const xmlChar* TAG_ATTR_NAME = BAD_CAST("tag");
132 const xmlChar* STYLEVALUE_ATTR_NAME = BAD_CAST("stylevalue");
133 const xmlChar* AXIS_TAG = BAD_CAST("axis");
134 out->mAxes.clear();
Seigo Nonaka36758982018-10-01 19:06:11 -0700135 for (xmlNode* axis = firstElement(fontNode, AXIS_TAG); axis;
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700136 axis = nextSibling(axis, AXIS_TAG)) {
137 XmlCharUniquePtr tagStr(xmlGetProp(axis, TAG_ATTR_NAME));
138 if (!tagStr || xmlStrlen(tagStr.get()) != 4) {
139 continue; // Tag value must be 4 char string
140 }
141
142 XmlCharUniquePtr styleValueStr(xmlGetProp(axis, STYLEVALUE_ATTR_NAME));
143 if (!styleValueStr) {
144 continue;
145 }
146
147 uint32_t tag =
148 static_cast<uint32_t>(tagStr.get()[0] << 24) |
149 static_cast<uint32_t>(tagStr.get()[1] << 16) |
150 static_cast<uint32_t>(tagStr.get()[2] << 8) |
151 static_cast<uint32_t>(tagStr.get()[3]);
152 float styleValue = strtod(reinterpret_cast<const char*>(styleValueStr.get()), nullptr);
153 out->mAxes.push_back(std::make_pair(tag, styleValue));
154 }
155}
156
157bool isFontFileAvailable(const std::string& filePath) {
158 std::string fullPath = filePath;
159 struct stat st = {};
160 if (stat(fullPath.c_str(), &st) != 0) {
161 return false;
162 }
163 return S_ISREG(st.st_mode);
164}
165
Seigo Nonaka36758982018-10-01 19:06:11 -0700166xmlNode* findFirstFontNode(const XmlDocUniquePtr& doc) {
167 xmlNode* familySet = xmlDocGetRootElement(doc.get());
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700168 if (familySet == nullptr) {
169 return nullptr;
170 }
171 xmlNode* family = firstElement(familySet, FAMILY_TAG);
172 if (family == nullptr) {
173 return nullptr;
174 }
175
176 xmlNode* font = firstElement(family, FONT_TAG);
177 while (font == nullptr) {
178 family = nextSibling(family, FAMILY_TAG);
179 if (family == nullptr) {
180 return nullptr;
181 }
182 font = firstElement(family, FONT_TAG);
183 }
184 return font;
185}
186
187} // namespace
188
189ASystemFontIterator* ASystemFontIterator_open() {
190 std::unique_ptr<ASystemFontIterator> ite(new ASystemFontIterator());
191 ite->mXmlDoc.reset(xmlReadFile("/system/etc/fonts.xml", nullptr, 0));
Seigo Nonaka36758982018-10-01 19:06:11 -0700192 ite->mCustomizationXmlDoc.reset(xmlReadFile("/product/etc/fonts_customization.xml", nullptr, 0));
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700193 return ite.release();
194}
195
196void ASystemFontIterator_close(ASystemFontIterator* ite) {
197 delete ite;
198}
199
Seigo Nonaka75b841b2018-10-30 11:39:49 -0700200ASystemFont* ASystemFont_matchFamilyStyleCharacter(
201 const char* _Nonnull familyName,
202 uint16_t weight,
203 bool italic,
204 const char* _Nonnull languageTags,
205 const uint16_t* _Nonnull text,
206 uint32_t textLength,
207 uint32_t* _Nullable runLength) {
208 std::shared_ptr<minikin::FontCollection> fc =
209 minikin::SystemFonts::findFontCollection(familyName);
210 std::vector<minikin::FontCollection::Run> runs =
211 fc->itemize(minikin::U16StringPiece(text, textLength),
212 minikin::FontStyle(weight, static_cast<minikin::FontStyle::Slant>(italic)),
213 minikin::registerLocaleList(languageTags),
214 minikin::FamilyVariant::DEFAULT);
215
216 const minikin::Font* font = runs[0].fakedFont.font;
217 std::unique_ptr<ASystemFont> result = std::make_unique<ASystemFont>();
218 const android::MinikinFontSkia* minikinFontSkia =
219 reinterpret_cast<android::MinikinFontSkia*>(font->typeface().get());
220 result->mFilePath = minikinFontSkia->getFilePath();
221 result->mWeight = font->style().weight();
222 result->mItalic = font->style().slant() == minikin::FontStyle::Slant::ITALIC;
223 result->mCollectionIndex = minikinFontSkia->GetFontIndex();
224 const std::vector<minikin::FontVariation>& axes = minikinFontSkia->GetAxes();
225 result->mAxes.reserve(axes.size());
226 for (auto axis : axes) {
227 result->mAxes.push_back(std::make_pair(axis.axisTag, axis.value));
228 }
229 if (runLength != nullptr) {
230 *runLength = runs[0].end;
231 }
232 return result.release();
233}
234
Seigo Nonaka36758982018-10-01 19:06:11 -0700235xmlNode* findNextFontNode(const XmlDocUniquePtr& xmlDoc, xmlNode* fontNode) {
236 if (fontNode == nullptr) {
237 if (!xmlDoc) {
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700238 return nullptr; // Already at the end.
239 } else {
240 // First time to query font.
Seigo Nonaka36758982018-10-01 19:06:11 -0700241 return findFirstFontNode(xmlDoc);
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700242 }
243 } else {
Seigo Nonaka36758982018-10-01 19:06:11 -0700244 xmlNode* nextNode = nextSibling(fontNode, FONT_TAG);
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700245 while (nextNode == nullptr) {
Seigo Nonaka36758982018-10-01 19:06:11 -0700246 xmlNode* family = nextSibling(fontNode->parent, FAMILY_TAG);
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700247 if (family == nullptr) {
248 break;
249 }
250 nextNode = firstElement(family, FONT_TAG);
251 }
Seigo Nonaka36758982018-10-01 19:06:11 -0700252 return nextNode;
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700253 }
254}
255
Seigo Nonaka36758982018-10-01 19:06:11 -0700256ASystemFont* ASystemFontIterator_next(ASystemFontIterator* ite) {
257 LOG_ALWAYS_FATAL_IF(ite == nullptr, "nullptr has passed as iterator argument");
258 if (ite->mXmlDoc) {
259 ite->mFontNode = findNextFontNode(ite->mXmlDoc, ite->mFontNode);
260 if (ite->mFontNode == nullptr) {
261 // Reached end of the XML file. Continue OEM customization.
262 ite->mXmlDoc.reset();
263 ite->mFontNode = nullptr;
264 } else {
265 std::unique_ptr<ASystemFont> font = std::make_unique<ASystemFont>();
266 copyFont(ite->mXmlDoc, ite->mFontNode, font.get(), "/system/fonts/");
267 if (!isFontFileAvailable(font->mFilePath)) {
268 return ASystemFontIterator_next(ite);
269 }
270 return font.release();
271 }
272 }
273 if (ite->mCustomizationXmlDoc) {
274 // TODO: Filter only customizationType="new-named-family"
275 ite->mFontNode = findNextFontNode(ite->mCustomizationXmlDoc, ite->mFontNode);
276 if (ite->mFontNode == nullptr) {
277 // Reached end of the XML file. Finishing
278 ite->mCustomizationXmlDoc.reset();
279 ite->mFontNode = nullptr;
280 return nullptr;
281 } else {
282 std::unique_ptr<ASystemFont> font = std::make_unique<ASystemFont>();
283 copyFont(ite->mCustomizationXmlDoc, ite->mFontNode, font.get(), "/product/fonts/");
284 if (!isFontFileAvailable(font->mFilePath)) {
285 return ASystemFontIterator_next(ite);
286 }
287 return font.release();
288 }
289 }
290 return nullptr;
291}
292
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700293void ASystemFont_close(ASystemFont* font) {
294 delete font;
295}
296
297const char* ASystemFont_getFontFilePath(const ASystemFont* font) {
298 LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed as font argument");
299 return font->mFilePath.c_str();
300}
301
302uint16_t ASystemFont_getWeight(const ASystemFont* font) {
303 LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed as font argument");
304 return font->mWeight;
305}
306
307bool ASystemFont_isItalic(const ASystemFont* font) {
308 LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed as font argument");
309 return font->mItalic;
310}
311
312const char* ASystemFont_getLocale(const ASystemFont* font) {
313 LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed to font argument");
314 return font->mLocale ? nullptr : font->mLocale->c_str();
315}
316
317size_t ASystemFont_getCollectionIndex(const ASystemFont* font) {
318 LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed to font argument");
319 return font->mCollectionIndex;
320}
321
322size_t ASystemFont_getAxisCount(const ASystemFont* font) {
323 LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed to font argument");
324 return font->mAxes.size();
325}
326
327uint32_t ASystemFont_getAxisTag(const ASystemFont* font, uint32_t axisIndex) {
328 LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed to font argument");
329 LOG_ALWAYS_FATAL_IF(axisIndex >= font->mAxes.size(),
330 "given axis index is out of bounds. (< %zd", font->mAxes.size());
331 return font->mAxes[axisIndex].first;
332}
333
334float ASystemFont_getAxisValue(const ASystemFont* font, uint32_t axisIndex) {
335 LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed to font argument");
336 LOG_ALWAYS_FATAL_IF(axisIndex >= font->mAxes.size(),
337 "given axis index is out of bounds. (< %zd", font->mAxes.size());
338 return font->mAxes[axisIndex].second;
339}