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