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 | |
Seigo Nonaka | 01709c7 | 2019-10-24 18:50:51 -0700 | [diff] [blame] | 19 | #define LOG_TAG "SystemFont" |
| 20 | |
Seigo Nonaka | b3a7bce | 2019-03-29 14:24:57 -0700 | [diff] [blame] | 21 | #include <android/font.h> |
| 22 | #include <android/font_matcher.h> |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 23 | #include <android/system_fonts.h> |
| 24 | |
| 25 | #include <memory> |
| 26 | #include <string> |
| 27 | #include <vector> |
| 28 | |
| 29 | #include <errno.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <libxml/tree.h> |
| 32 | #include <log/log.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <unistd.h> |
| 35 | |
Seigo Nonaka | 75b841b | 2018-10-30 11:39:49 -0700 | [diff] [blame] | 36 | #include <hwui/MinikinSkia.h> |
| 37 | #include <minikin/FontCollection.h> |
| 38 | #include <minikin/LocaleList.h> |
| 39 | #include <minikin/SystemFonts.h> |
| 40 | |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 41 | struct XmlCharDeleter { |
| 42 | void operator()(xmlChar* b) { xmlFree(b); } |
| 43 | }; |
| 44 | |
| 45 | struct XmlDocDeleter { |
| 46 | void operator()(xmlDoc* d) { xmlFreeDoc(d); } |
| 47 | }; |
| 48 | |
| 49 | using XmlCharUniquePtr = std::unique_ptr<xmlChar, XmlCharDeleter>; |
| 50 | using XmlDocUniquePtr = std::unique_ptr<xmlDoc, XmlDocDeleter>; |
| 51 | |
Seigo Nonaka | 01709c7 | 2019-10-24 18:50:51 -0700 | [diff] [blame] | 52 | struct ParserState { |
| 53 | xmlNode* mFontNode = nullptr; |
| 54 | XmlCharUniquePtr mLocale; |
| 55 | }; |
| 56 | |
Seigo Nonaka | b3a7bce | 2019-03-29 14:24:57 -0700 | [diff] [blame] | 57 | struct AFont { |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 58 | std::string mFilePath; |
Seigo Nonaka | b950a1f | 2021-04-14 19:15:16 -0700 | [diff] [blame^] | 59 | std::optional<std::string> mLocale; |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 60 | uint16_t mWeight; |
| 61 | bool mItalic; |
| 62 | uint32_t mCollectionIndex; |
| 63 | std::vector<std::pair<uint32_t, float>> mAxes; |
Seigo Nonaka | b950a1f | 2021-04-14 19:15:16 -0700 | [diff] [blame^] | 64 | |
| 65 | bool operator==(const AFont& o) const { |
| 66 | return mFilePath == o.mFilePath && mLocale == o.mLocale && mWeight == o.mWeight && |
| 67 | mItalic == o.mItalic && mCollectionIndex == o.mCollectionIndex && mAxes == o.mAxes; |
| 68 | } |
| 69 | |
| 70 | AFont() = default; |
| 71 | AFont(const AFont&) = default; |
| 72 | }; |
| 73 | |
| 74 | struct FontHasher { |
| 75 | std::size_t operator()(const AFont& font) const { |
| 76 | std::size_t r = std::hash<std::string>{}(font.mFilePath); |
| 77 | if (font.mLocale) { |
| 78 | r = combine(r, std::hash<std::string>{}(*font.mLocale)); |
| 79 | } |
| 80 | r = combine(r, std::hash<uint16_t>{}(font.mWeight)); |
| 81 | r = combine(r, std::hash<uint32_t>{}(font.mCollectionIndex)); |
| 82 | for (const auto& [tag, value] : font.mAxes) { |
| 83 | r = combine(r, std::hash<uint32_t>{}(tag)); |
| 84 | r = combine(r, std::hash<float>{}(value)); |
| 85 | } |
| 86 | return r; |
| 87 | } |
| 88 | |
| 89 | std::size_t combine(std::size_t l, std::size_t r) const { return l ^ (r << 1); } |
| 90 | }; |
| 91 | |
| 92 | struct ASystemFontIterator { |
| 93 | std::vector<AFont> fonts; |
| 94 | uint32_t index; |
| 95 | |
| 96 | XmlDocUniquePtr mXmlDoc; |
| 97 | |
| 98 | ParserState state; |
| 99 | |
| 100 | // The OEM customization XML. |
| 101 | XmlDocUniquePtr mCustomizationXmlDoc; |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
Seigo Nonaka | b3a7bce | 2019-03-29 14:24:57 -0700 | [diff] [blame] | 104 | struct AFontMatcher { |
| 105 | minikin::FontStyle mFontStyle; |
| 106 | uint32_t mLocaleListId = 0; // 0 is reserved for empty locale ID. |
| 107 | bool mFamilyVariant = AFAMILY_VARIANT_DEFAULT; |
| 108 | }; |
| 109 | |
| 110 | static_assert(static_cast<uint32_t>(AFAMILY_VARIANT_DEFAULT) == |
| 111 | static_cast<uint32_t>(minikin::FamilyVariant::DEFAULT)); |
| 112 | static_assert(static_cast<uint32_t>(AFAMILY_VARIANT_COMPACT) == |
| 113 | static_cast<uint32_t>(minikin::FamilyVariant::COMPACT)); |
| 114 | static_assert(static_cast<uint32_t>(AFAMILY_VARIANT_ELEGANT) == |
| 115 | static_cast<uint32_t>(minikin::FamilyVariant::ELEGANT)); |
| 116 | |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 117 | namespace { |
| 118 | |
| 119 | std::string xmlTrim(const std::string& in) { |
| 120 | if (in.empty()) { |
| 121 | return in; |
| 122 | } |
| 123 | const char XML_SPACES[] = "\u0020\u000D\u000A\u0009"; |
| 124 | const size_t start = in.find_first_not_of(XML_SPACES); // inclusive |
| 125 | if (start == std::string::npos) { |
| 126 | return ""; |
| 127 | } |
| 128 | const size_t end = in.find_last_not_of(XML_SPACES); // inclusive |
| 129 | if (end == std::string::npos) { |
| 130 | return ""; |
| 131 | } |
| 132 | return in.substr(start, end - start + 1 /* +1 since end is inclusive */); |
| 133 | } |
| 134 | |
| 135 | const xmlChar* FAMILY_TAG = BAD_CAST("family"); |
| 136 | const xmlChar* FONT_TAG = BAD_CAST("font"); |
Seigo Nonaka | 01709c7 | 2019-10-24 18:50:51 -0700 | [diff] [blame] | 137 | const xmlChar* LOCALE_ATTR_NAME = BAD_CAST("lang"); |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 138 | |
| 139 | xmlNode* firstElement(xmlNode* node, const xmlChar* tag) { |
| 140 | for (xmlNode* child = node->children; child; child = child->next) { |
| 141 | if (xmlStrEqual(child->name, tag)) { |
| 142 | return child; |
| 143 | } |
| 144 | } |
| 145 | return nullptr; |
| 146 | } |
| 147 | |
| 148 | xmlNode* nextSibling(xmlNode* node, const xmlChar* tag) { |
| 149 | while ((node = node->next) != nullptr) { |
| 150 | if (xmlStrEqual(node->name, tag)) { |
| 151 | return node; |
| 152 | } |
| 153 | } |
| 154 | return nullptr; |
| 155 | } |
| 156 | |
Seigo Nonaka | 01709c7 | 2019-10-24 18:50:51 -0700 | [diff] [blame] | 157 | void copyFont(const XmlDocUniquePtr& xmlDoc, const ParserState& state, AFont* out, |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 158 | const std::string& pathPrefix) { |
Seigo Nonaka | 01709c7 | 2019-10-24 18:50:51 -0700 | [diff] [blame] | 159 | xmlNode* fontNode = state.mFontNode; |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 160 | XmlCharUniquePtr filePathStr( |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 161 | xmlNodeListGetString(xmlDoc.get(), fontNode->xmlChildrenNode, 1)); |
| 162 | out->mFilePath = pathPrefix + xmlTrim( |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 163 | std::string(filePathStr.get(), filePathStr.get() + xmlStrlen(filePathStr.get()))); |
| 164 | |
| 165 | const xmlChar* WEIGHT_ATTR_NAME = BAD_CAST("weight"); |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 166 | XmlCharUniquePtr weightStr(xmlGetProp(fontNode, WEIGHT_ATTR_NAME)); |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 167 | out->mWeight = weightStr ? |
| 168 | strtol(reinterpret_cast<const char*>(weightStr.get()), nullptr, 10) : 400; |
| 169 | |
| 170 | const xmlChar* STYLE_ATTR_NAME = BAD_CAST("style"); |
| 171 | const xmlChar* ITALIC_ATTR_VALUE = BAD_CAST("italic"); |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 172 | XmlCharUniquePtr styleStr(xmlGetProp(fontNode, STYLE_ATTR_NAME)); |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 173 | out->mItalic = styleStr ? xmlStrEqual(styleStr.get(), ITALIC_ATTR_VALUE) : false; |
| 174 | |
| 175 | const xmlChar* INDEX_ATTR_NAME = BAD_CAST("index"); |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 176 | XmlCharUniquePtr indexStr(xmlGetProp(fontNode, INDEX_ATTR_NAME)); |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 177 | out->mCollectionIndex = indexStr ? |
| 178 | strtol(reinterpret_cast<const char*>(indexStr.get()), nullptr, 10) : 0; |
| 179 | |
Seigo Nonaka | b950a1f | 2021-04-14 19:15:16 -0700 | [diff] [blame^] | 180 | if (state.mLocale) { |
| 181 | out->mLocale.emplace(reinterpret_cast<const char*>(state.mLocale.get())); |
| 182 | } |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 183 | |
| 184 | const xmlChar* TAG_ATTR_NAME = BAD_CAST("tag"); |
| 185 | const xmlChar* STYLEVALUE_ATTR_NAME = BAD_CAST("stylevalue"); |
| 186 | const xmlChar* AXIS_TAG = BAD_CAST("axis"); |
| 187 | out->mAxes.clear(); |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 188 | for (xmlNode* axis = firstElement(fontNode, AXIS_TAG); axis; |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 189 | axis = nextSibling(axis, AXIS_TAG)) { |
| 190 | XmlCharUniquePtr tagStr(xmlGetProp(axis, TAG_ATTR_NAME)); |
| 191 | if (!tagStr || xmlStrlen(tagStr.get()) != 4) { |
| 192 | continue; // Tag value must be 4 char string |
| 193 | } |
| 194 | |
| 195 | XmlCharUniquePtr styleValueStr(xmlGetProp(axis, STYLEVALUE_ATTR_NAME)); |
| 196 | if (!styleValueStr) { |
| 197 | continue; |
| 198 | } |
| 199 | |
| 200 | uint32_t tag = |
| 201 | static_cast<uint32_t>(tagStr.get()[0] << 24) | |
| 202 | static_cast<uint32_t>(tagStr.get()[1] << 16) | |
| 203 | static_cast<uint32_t>(tagStr.get()[2] << 8) | |
| 204 | static_cast<uint32_t>(tagStr.get()[3]); |
| 205 | float styleValue = strtod(reinterpret_cast<const char*>(styleValueStr.get()), nullptr); |
| 206 | out->mAxes.push_back(std::make_pair(tag, styleValue)); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | bool isFontFileAvailable(const std::string& filePath) { |
| 211 | std::string fullPath = filePath; |
| 212 | struct stat st = {}; |
| 213 | if (stat(fullPath.c_str(), &st) != 0) { |
| 214 | return false; |
| 215 | } |
| 216 | return S_ISREG(st.st_mode); |
| 217 | } |
| 218 | |
Seigo Nonaka | 01709c7 | 2019-10-24 18:50:51 -0700 | [diff] [blame] | 219 | bool findFirstFontNode(const XmlDocUniquePtr& doc, ParserState* state) { |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 220 | xmlNode* familySet = xmlDocGetRootElement(doc.get()); |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 221 | if (familySet == nullptr) { |
Seigo Nonaka | 01709c7 | 2019-10-24 18:50:51 -0700 | [diff] [blame] | 222 | return false; |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 223 | } |
| 224 | xmlNode* family = firstElement(familySet, FAMILY_TAG); |
| 225 | if (family == nullptr) { |
Seigo Nonaka | 01709c7 | 2019-10-24 18:50:51 -0700 | [diff] [blame] | 226 | return false; |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 227 | } |
Seigo Nonaka | 01709c7 | 2019-10-24 18:50:51 -0700 | [diff] [blame] | 228 | state->mLocale.reset(xmlGetProp(family, LOCALE_ATTR_NAME)); |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 229 | |
| 230 | xmlNode* font = firstElement(family, FONT_TAG); |
| 231 | while (font == nullptr) { |
| 232 | family = nextSibling(family, FAMILY_TAG); |
| 233 | if (family == nullptr) { |
Seigo Nonaka | 01709c7 | 2019-10-24 18:50:51 -0700 | [diff] [blame] | 234 | return false; |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 235 | } |
| 236 | font = firstElement(family, FONT_TAG); |
| 237 | } |
Seigo Nonaka | 01709c7 | 2019-10-24 18:50:51 -0700 | [diff] [blame] | 238 | state->mFontNode = font; |
| 239 | return font != nullptr; |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | } // namespace |
| 243 | |
| 244 | ASystemFontIterator* ASystemFontIterator_open() { |
| 245 | std::unique_ptr<ASystemFontIterator> ite(new ASystemFontIterator()); |
Seigo Nonaka | b950a1f | 2021-04-14 19:15:16 -0700 | [diff] [blame^] | 246 | |
| 247 | std::unordered_set<AFont, FontHasher> fonts; |
| 248 | minikin::SystemFonts::getFontMap( |
| 249 | [&fonts](const std::vector<std::shared_ptr<minikin::FontCollection>>& collections) { |
| 250 | for (const auto& fc : collections) { |
| 251 | for (const auto& family : fc->getFamilies()) { |
| 252 | for (uint32_t i = 0; i < family->getNumFonts(); ++i) { |
| 253 | const minikin::Font* font = family->getFont(i); |
| 254 | |
| 255 | std::optional<std::string> locale; |
| 256 | uint32_t localeId = font->getLocaleListId(); |
| 257 | if (localeId != minikin::kEmptyLocaleListId) { |
| 258 | locale.emplace(minikin::getLocaleString(localeId)); |
| 259 | } |
| 260 | std::vector<std::pair<uint32_t, float>> axes; |
| 261 | for (const auto& [tag, value] : font->typeface()->GetAxes()) { |
| 262 | axes.push_back(std::make_pair(tag, value)); |
| 263 | } |
| 264 | |
| 265 | fonts.insert( |
| 266 | {font->typeface()->GetFontPath(), std::move(locale), |
| 267 | font->style().weight(), |
| 268 | font->style().slant() == minikin::FontStyle::Slant::ITALIC, |
| 269 | static_cast<uint32_t>(font->typeface()->GetFontIndex()), |
| 270 | axes}); |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | }); |
| 275 | |
| 276 | if (fonts.empty()) { |
| 277 | ite->mXmlDoc.reset(xmlReadFile("/system/etc/fonts.xml", nullptr, 0)); |
| 278 | ite->mCustomizationXmlDoc.reset( |
| 279 | xmlReadFile("/product/etc/fonts_customization.xml", nullptr, 0)); |
| 280 | } else { |
| 281 | ite->index = 0; |
| 282 | ite->fonts.assign(fonts.begin(), fonts.end()); |
| 283 | } |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 284 | return ite.release(); |
| 285 | } |
| 286 | |
| 287 | void ASystemFontIterator_close(ASystemFontIterator* ite) { |
| 288 | delete ite; |
| 289 | } |
| 290 | |
Seigo Nonaka | b3a7bce | 2019-03-29 14:24:57 -0700 | [diff] [blame] | 291 | AFontMatcher* _Nonnull AFontMatcher_create() { |
| 292 | return new AFontMatcher(); |
| 293 | } |
| 294 | |
| 295 | void AFontMatcher_destroy(AFontMatcher* matcher) { |
| 296 | delete matcher; |
| 297 | } |
| 298 | |
| 299 | void AFontMatcher_setStyle( |
| 300 | AFontMatcher* _Nonnull matcher, |
Seigo Nonaka | 75b841b | 2018-10-30 11:39:49 -0700 | [diff] [blame] | 301 | uint16_t weight, |
Seigo Nonaka | b3a7bce | 2019-03-29 14:24:57 -0700 | [diff] [blame] | 302 | bool italic) { |
| 303 | matcher->mFontStyle = minikin::FontStyle( |
| 304 | weight, static_cast<minikin::FontStyle::Slant>(italic)); |
| 305 | } |
| 306 | |
| 307 | void AFontMatcher_setLocales( |
| 308 | AFontMatcher* _Nonnull matcher, |
| 309 | const char* _Nonnull languageTags) { |
| 310 | matcher->mLocaleListId = minikin::registerLocaleList(languageTags); |
| 311 | } |
| 312 | |
| 313 | void AFontMatcher_setFamilyVariant(AFontMatcher* _Nonnull matcher, uint32_t familyVariant) { |
| 314 | matcher->mFamilyVariant = familyVariant; |
| 315 | } |
| 316 | |
| 317 | AFont* _Nonnull AFontMatcher_match( |
| 318 | const AFontMatcher* _Nonnull matcher, |
| 319 | const char* _Nonnull familyName, |
Seigo Nonaka | 75b841b | 2018-10-30 11:39:49 -0700 | [diff] [blame] | 320 | const uint16_t* _Nonnull text, |
Seigo Nonaka | b3a7bce | 2019-03-29 14:24:57 -0700 | [diff] [blame] | 321 | const uint32_t textLength, |
Seigo Nonaka | 75b841b | 2018-10-30 11:39:49 -0700 | [diff] [blame] | 322 | uint32_t* _Nullable runLength) { |
| 323 | std::shared_ptr<minikin::FontCollection> fc = |
| 324 | minikin::SystemFonts::findFontCollection(familyName); |
Seigo Nonaka | b3a7bce | 2019-03-29 14:24:57 -0700 | [diff] [blame] | 325 | std::vector<minikin::FontCollection::Run> runs = fc->itemize( |
| 326 | minikin::U16StringPiece(text, textLength), |
| 327 | matcher->mFontStyle, |
| 328 | matcher->mLocaleListId, |
Seigo Nonaka | ddc8773 | 2019-04-05 15:20:19 -0700 | [diff] [blame] | 329 | static_cast<minikin::FamilyVariant>(matcher->mFamilyVariant), |
| 330 | 1 /* maxRun */); |
Seigo Nonaka | 75b841b | 2018-10-30 11:39:49 -0700 | [diff] [blame] | 331 | |
Seigo Nonaka | 0e5f67a | 2021-02-03 11:27:09 -0800 | [diff] [blame] | 332 | const std::shared_ptr<minikin::Font>& font = runs[0].fakedFont.font; |
Seigo Nonaka | b3a7bce | 2019-03-29 14:24:57 -0700 | [diff] [blame] | 333 | std::unique_ptr<AFont> result = std::make_unique<AFont>(); |
Seigo Nonaka | 75b841b | 2018-10-30 11:39:49 -0700 | [diff] [blame] | 334 | const android::MinikinFontSkia* minikinFontSkia = |
| 335 | reinterpret_cast<android::MinikinFontSkia*>(font->typeface().get()); |
| 336 | result->mFilePath = minikinFontSkia->getFilePath(); |
| 337 | result->mWeight = font->style().weight(); |
| 338 | result->mItalic = font->style().slant() == minikin::FontStyle::Slant::ITALIC; |
| 339 | result->mCollectionIndex = minikinFontSkia->GetFontIndex(); |
| 340 | const std::vector<minikin::FontVariation>& axes = minikinFontSkia->GetAxes(); |
| 341 | result->mAxes.reserve(axes.size()); |
| 342 | for (auto axis : axes) { |
| 343 | result->mAxes.push_back(std::make_pair(axis.axisTag, axis.value)); |
| 344 | } |
| 345 | if (runLength != nullptr) { |
| 346 | *runLength = runs[0].end; |
| 347 | } |
| 348 | return result.release(); |
| 349 | } |
| 350 | |
Seigo Nonaka | 01709c7 | 2019-10-24 18:50:51 -0700 | [diff] [blame] | 351 | bool findNextFontNode(const XmlDocUniquePtr& xmlDoc, ParserState* state) { |
| 352 | if (state->mFontNode == nullptr) { |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 353 | if (!xmlDoc) { |
Seigo Nonaka | 01709c7 | 2019-10-24 18:50:51 -0700 | [diff] [blame] | 354 | return false; // Already at the end. |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 355 | } else { |
| 356 | // First time to query font. |
Seigo Nonaka | 01709c7 | 2019-10-24 18:50:51 -0700 | [diff] [blame] | 357 | return findFirstFontNode(xmlDoc, state); |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 358 | } |
| 359 | } else { |
Seigo Nonaka | 01709c7 | 2019-10-24 18:50:51 -0700 | [diff] [blame] | 360 | xmlNode* nextNode = nextSibling(state->mFontNode, FONT_TAG); |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 361 | while (nextNode == nullptr) { |
Seigo Nonaka | 01709c7 | 2019-10-24 18:50:51 -0700 | [diff] [blame] | 362 | xmlNode* family = nextSibling(state->mFontNode->parent, FAMILY_TAG); |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 363 | if (family == nullptr) { |
| 364 | break; |
| 365 | } |
Seigo Nonaka | 01709c7 | 2019-10-24 18:50:51 -0700 | [diff] [blame] | 366 | state->mLocale.reset(xmlGetProp(family, LOCALE_ATTR_NAME)); |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 367 | nextNode = firstElement(family, FONT_TAG); |
| 368 | } |
Seigo Nonaka | 01709c7 | 2019-10-24 18:50:51 -0700 | [diff] [blame] | 369 | state->mFontNode = nextNode; |
| 370 | return nextNode != nullptr; |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
Seigo Nonaka | b3a7bce | 2019-03-29 14:24:57 -0700 | [diff] [blame] | 374 | AFont* ASystemFontIterator_next(ASystemFontIterator* ite) { |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 375 | LOG_ALWAYS_FATAL_IF(ite == nullptr, "nullptr has passed as iterator argument"); |
Seigo Nonaka | b950a1f | 2021-04-14 19:15:16 -0700 | [diff] [blame^] | 376 | if (!ite->fonts.empty()) { |
| 377 | if (ite->index >= ite->fonts.size()) { |
| 378 | return nullptr; |
| 379 | } |
| 380 | return new AFont(ite->fonts[ite->index++]); |
| 381 | } |
| 382 | |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 383 | if (ite->mXmlDoc) { |
Seigo Nonaka | 01709c7 | 2019-10-24 18:50:51 -0700 | [diff] [blame] | 384 | if (!findNextFontNode(ite->mXmlDoc, &ite->state)) { |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 385 | // Reached end of the XML file. Continue OEM customization. |
| 386 | ite->mXmlDoc.reset(); |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 387 | } else { |
Seigo Nonaka | b3a7bce | 2019-03-29 14:24:57 -0700 | [diff] [blame] | 388 | std::unique_ptr<AFont> font = std::make_unique<AFont>(); |
Seigo Nonaka | 01709c7 | 2019-10-24 18:50:51 -0700 | [diff] [blame] | 389 | copyFont(ite->mXmlDoc, ite->state, font.get(), "/system/fonts/"); |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 390 | if (!isFontFileAvailable(font->mFilePath)) { |
| 391 | return ASystemFontIterator_next(ite); |
| 392 | } |
| 393 | return font.release(); |
| 394 | } |
| 395 | } |
| 396 | if (ite->mCustomizationXmlDoc) { |
| 397 | // TODO: Filter only customizationType="new-named-family" |
Seigo Nonaka | 01709c7 | 2019-10-24 18:50:51 -0700 | [diff] [blame] | 398 | if (!findNextFontNode(ite->mCustomizationXmlDoc, &ite->state)) { |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 399 | // Reached end of the XML file. Finishing |
| 400 | ite->mCustomizationXmlDoc.reset(); |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 401 | return nullptr; |
| 402 | } else { |
Seigo Nonaka | b3a7bce | 2019-03-29 14:24:57 -0700 | [diff] [blame] | 403 | std::unique_ptr<AFont> font = std::make_unique<AFont>(); |
Seigo Nonaka | 01709c7 | 2019-10-24 18:50:51 -0700 | [diff] [blame] | 404 | copyFont(ite->mCustomizationXmlDoc, ite->state, font.get(), "/product/fonts/"); |
Seigo Nonaka | 3675898 | 2018-10-01 19:06:11 -0700 | [diff] [blame] | 405 | if (!isFontFileAvailable(font->mFilePath)) { |
| 406 | return ASystemFontIterator_next(ite); |
| 407 | } |
| 408 | return font.release(); |
| 409 | } |
| 410 | } |
| 411 | return nullptr; |
| 412 | } |
| 413 | |
Seigo Nonaka | b3a7bce | 2019-03-29 14:24:57 -0700 | [diff] [blame] | 414 | void AFont_close(AFont* font) { |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 415 | delete font; |
| 416 | } |
| 417 | |
Seigo Nonaka | b3a7bce | 2019-03-29 14:24:57 -0700 | [diff] [blame] | 418 | const char* AFont_getFontFilePath(const AFont* font) { |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 419 | LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed as font argument"); |
| 420 | return font->mFilePath.c_str(); |
| 421 | } |
| 422 | |
Seigo Nonaka | b3a7bce | 2019-03-29 14:24:57 -0700 | [diff] [blame] | 423 | uint16_t AFont_getWeight(const AFont* font) { |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 424 | LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed as font argument"); |
| 425 | return font->mWeight; |
| 426 | } |
| 427 | |
Seigo Nonaka | b3a7bce | 2019-03-29 14:24:57 -0700 | [diff] [blame] | 428 | bool AFont_isItalic(const AFont* font) { |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 429 | LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed as font argument"); |
| 430 | return font->mItalic; |
| 431 | } |
| 432 | |
Seigo Nonaka | b3a7bce | 2019-03-29 14:24:57 -0700 | [diff] [blame] | 433 | const char* AFont_getLocale(const AFont* font) { |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 434 | LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed to font argument"); |
Seigo Nonaka | 01709c7 | 2019-10-24 18:50:51 -0700 | [diff] [blame] | 435 | return font->mLocale ? font->mLocale->c_str() : nullptr; |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Seigo Nonaka | b3a7bce | 2019-03-29 14:24:57 -0700 | [diff] [blame] | 438 | size_t AFont_getCollectionIndex(const AFont* font) { |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 439 | LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed to font argument"); |
| 440 | return font->mCollectionIndex; |
| 441 | } |
| 442 | |
Seigo Nonaka | b3a7bce | 2019-03-29 14:24:57 -0700 | [diff] [blame] | 443 | size_t AFont_getAxisCount(const AFont* font) { |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 444 | LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed to font argument"); |
| 445 | return font->mAxes.size(); |
| 446 | } |
| 447 | |
Seigo Nonaka | b3a7bce | 2019-03-29 14:24:57 -0700 | [diff] [blame] | 448 | uint32_t AFont_getAxisTag(const AFont* font, uint32_t axisIndex) { |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 449 | LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed to font argument"); |
| 450 | LOG_ALWAYS_FATAL_IF(axisIndex >= font->mAxes.size(), |
| 451 | "given axis index is out of bounds. (< %zd", font->mAxes.size()); |
| 452 | return font->mAxes[axisIndex].first; |
| 453 | } |
| 454 | |
Seigo Nonaka | b3a7bce | 2019-03-29 14:24:57 -0700 | [diff] [blame] | 455 | float AFont_getAxisValue(const AFont* font, uint32_t axisIndex) { |
Seigo Nonaka | 50692ca | 2018-08-31 12:27:15 -0700 | [diff] [blame] | 456 | LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed to font argument"); |
| 457 | LOG_ALWAYS_FATAL_IF(axisIndex >= font->mAxes.size(), |
| 458 | "given axis index is out of bounds. (< %zd", font->mAxes.size()); |
| 459 | return font->mAxes[axisIndex].second; |
| 460 | } |