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