Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 1 | /* |
| 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 Nonaka | 75b841b | 2018-10-30 11:39:49 -0700 | [diff] [blame^] | 32 | #include <hwui/MinikinSkia.h> |
| 33 | #include <minikin/FontCollection.h> |
| 34 | #include <minikin/LocaleList.h> |
| 35 | #include <minikin/SystemFonts.h> |
| 36 | |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 37 | struct XmlCharDeleter { |
| 38 | void operator()(xmlChar* b) { xmlFree(b); } |
| 39 | }; |
| 40 | |
| 41 | struct XmlDocDeleter { |
| 42 | void operator()(xmlDoc* d) { xmlFreeDoc(d); } |
| 43 | }; |
| 44 | |
| 45 | using XmlCharUniquePtr = std::unique_ptr<xmlChar, XmlCharDeleter>; |
| 46 | using XmlDocUniquePtr = std::unique_ptr<xmlDoc, XmlDocDeleter>; |
| 47 | |
| 48 | struct ASystemFontIterator { |
| 49 | XmlDocUniquePtr mXmlDoc; |
| 50 | xmlNode* mFontNode; |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 51 | |
| 52 | // The OEM customization XML. |
| 53 | XmlDocUniquePtr mCustomizationXmlDoc; |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | struct 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 | |
| 65 | namespace { |
| 66 | |
| 67 | std::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 | |
| 83 | const xmlChar* FAMILY_TAG = BAD_CAST("family"); |
| 84 | const xmlChar* FONT_TAG = BAD_CAST("font"); |
| 85 | |
| 86 | xmlNode* 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 | |
| 95 | xmlNode* 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 Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 104 | void copyFont(const XmlDocUniquePtr& xmlDoc, xmlNode* fontNode, ASystemFont* out, |
| 105 | const std::string& pathPrefix) { |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 106 | const xmlChar* LOCALE_ATTR_NAME = BAD_CAST("lang"); |
| 107 | XmlCharUniquePtr filePathStr( |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 108 | xmlNodeListGetString(xmlDoc.get(), fontNode->xmlChildrenNode, 1)); |
| 109 | out->mFilePath = pathPrefix + xmlTrim( |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 110 | std::string(filePathStr.get(), filePathStr.get() + xmlStrlen(filePathStr.get()))); |
| 111 | |
| 112 | const xmlChar* WEIGHT_ATTR_NAME = BAD_CAST("weight"); |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 113 | XmlCharUniquePtr weightStr(xmlGetProp(fontNode, WEIGHT_ATTR_NAME)); |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 114 | 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 Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 119 | XmlCharUniquePtr styleStr(xmlGetProp(fontNode, STYLE_ATTR_NAME)); |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 120 | out->mItalic = styleStr ? xmlStrEqual(styleStr.get(), ITALIC_ATTR_VALUE) : false; |
| 121 | |
| 122 | const xmlChar* INDEX_ATTR_NAME = BAD_CAST("index"); |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 123 | XmlCharUniquePtr indexStr(xmlGetProp(fontNode, INDEX_ATTR_NAME)); |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 124 | out->mCollectionIndex = indexStr ? |
| 125 | strtol(reinterpret_cast<const char*>(indexStr.get()), nullptr, 10) : 0; |
| 126 | |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 127 | XmlCharUniquePtr localeStr(xmlGetProp(xmlDoc->parent, LOCALE_ATTR_NAME)); |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 128 | 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 Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 135 | for (xmlNode* axis = firstElement(fontNode, AXIS_TAG); axis; |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 136 | 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 | |
| 157 | bool 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 Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 166 | xmlNode* findFirstFontNode(const XmlDocUniquePtr& doc) { |
| 167 | xmlNode* familySet = xmlDocGetRootElement(doc.get()); |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 168 | 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 | |
| 189 | ASystemFontIterator* ASystemFontIterator_open() { |
| 190 | std::unique_ptr<ASystemFontIterator> ite(new ASystemFontIterator()); |
| 191 | ite->mXmlDoc.reset(xmlReadFile("/system/etc/fonts.xml", nullptr, 0)); |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 192 | ite->mCustomizationXmlDoc.reset(xmlReadFile("/product/etc/fonts_customization.xml", nullptr, 0)); |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 193 | return ite.release(); |
| 194 | } |
| 195 | |
| 196 | void ASystemFontIterator_close(ASystemFontIterator* ite) { |
| 197 | delete ite; |
| 198 | } |
| 199 | |
Seigo Nonaka | 75b841b | 2018-10-30 11:39:49 -0700 | [diff] [blame^] | 200 | ASystemFont* 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 Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 235 | xmlNode* findNextFontNode(const XmlDocUniquePtr& xmlDoc, xmlNode* fontNode) { |
| 236 | if (fontNode == nullptr) { |
| 237 | if (!xmlDoc) { |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 238 | return nullptr; // Already at the end. |
| 239 | } else { |
| 240 | // First time to query font. |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 241 | return findFirstFontNode(xmlDoc); |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 242 | } |
| 243 | } else { |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 244 | xmlNode* nextNode = nextSibling(fontNode, FONT_TAG); |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 245 | while (nextNode == nullptr) { |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 246 | xmlNode* family = nextSibling(fontNode->parent, FAMILY_TAG); |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 247 | if (family == nullptr) { |
| 248 | break; |
| 249 | } |
| 250 | nextNode = firstElement(family, FONT_TAG); |
| 251 | } |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 252 | return nextNode; |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 256 | ASystemFont* 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 Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 293 | void ASystemFont_close(ASystemFont* font) { |
| 294 | delete font; |
| 295 | } |
| 296 | |
| 297 | const 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 | |
| 302 | uint16_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 | |
| 307 | bool ASystemFont_isItalic(const ASystemFont* font) { |
| 308 | LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed as font argument"); |
| 309 | return font->mItalic; |
| 310 | } |
| 311 | |
| 312 | const 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 | |
| 317 | size_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 | |
| 322 | size_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 | |
| 327 | uint32_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 | |
| 334 | float 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 | } |