blob: 8e90a6572478804f8973488ae1d9ddd786ca7084 [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
Seigo Nonaka01709c72019-10-24 18:50:51 -070019#define LOG_TAG "SystemFont"
20
Seigo Nonakab3a7bce2019-03-29 14:24:57 -070021#include <android/font.h>
22#include <android/font_matcher.h>
Seigo Nonaka50692ca2018-08-31 12:27:15 -070023#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 Nonaka75b841b2018-10-30 11:39:49 -070036#include <hwui/MinikinSkia.h>
37#include <minikin/FontCollection.h>
38#include <minikin/LocaleList.h>
39#include <minikin/SystemFonts.h>
40
Seigo Nonaka50692ca2018-08-31 12:27:15 -070041struct XmlCharDeleter {
42 void operator()(xmlChar* b) { xmlFree(b); }
43};
44
45struct XmlDocDeleter {
46 void operator()(xmlDoc* d) { xmlFreeDoc(d); }
47};
48
49using XmlCharUniquePtr = std::unique_ptr<xmlChar, XmlCharDeleter>;
50using XmlDocUniquePtr = std::unique_ptr<xmlDoc, XmlDocDeleter>;
51
Seigo Nonaka01709c72019-10-24 18:50:51 -070052struct ParserState {
53 xmlNode* mFontNode = nullptr;
54 XmlCharUniquePtr mLocale;
55};
56
Seigo Nonakab3a7bce2019-03-29 14:24:57 -070057struct AFont {
Seigo Nonaka50692ca2018-08-31 12:27:15 -070058 std::string mFilePath;
Seigo Nonakab950a1f2021-04-14 19:15:16 -070059 std::optional<std::string> mLocale;
Seigo Nonaka50692ca2018-08-31 12:27:15 -070060 uint16_t mWeight;
61 bool mItalic;
62 uint32_t mCollectionIndex;
63 std::vector<std::pair<uint32_t, float>> mAxes;
Seigo Nonakab950a1f2021-04-14 19:15:16 -070064
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 Nonakab950a1f2021-04-14 19:15:16 -070069};
70
71struct 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
89struct 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 Nonaka50692ca2018-08-31 12:27:15 -070099};
100
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700101struct 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
107static_assert(static_cast<uint32_t>(AFAMILY_VARIANT_DEFAULT) ==
108 static_cast<uint32_t>(minikin::FamilyVariant::DEFAULT));
109static_assert(static_cast<uint32_t>(AFAMILY_VARIANT_COMPACT) ==
110 static_cast<uint32_t>(minikin::FamilyVariant::COMPACT));
111static_assert(static_cast<uint32_t>(AFAMILY_VARIANT_ELEGANT) ==
112 static_cast<uint32_t>(minikin::FamilyVariant::ELEGANT));
113
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700114namespace {
115
116std::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
132const xmlChar* FAMILY_TAG = BAD_CAST("family");
133const xmlChar* FONT_TAG = BAD_CAST("font");
Seigo Nonaka01709c72019-10-24 18:50:51 -0700134const xmlChar* LOCALE_ATTR_NAME = BAD_CAST("lang");
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700135
136xmlNode* 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
145xmlNode* 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 Nonaka01709c72019-10-24 18:50:51 -0700154void copyFont(const XmlDocUniquePtr& xmlDoc, const ParserState& state, AFont* out,
Seigo Nonaka36758982018-10-01 19:06:11 -0700155 const std::string& pathPrefix) {
Seigo Nonaka01709c72019-10-24 18:50:51 -0700156 xmlNode* fontNode = state.mFontNode;
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700157 XmlCharUniquePtr filePathStr(
Seigo Nonaka36758982018-10-01 19:06:11 -0700158 xmlNodeListGetString(xmlDoc.get(), fontNode->xmlChildrenNode, 1));
159 out->mFilePath = pathPrefix + xmlTrim(
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700160 std::string(filePathStr.get(), filePathStr.get() + xmlStrlen(filePathStr.get())));
161
162 const xmlChar* WEIGHT_ATTR_NAME = BAD_CAST("weight");
Seigo Nonaka36758982018-10-01 19:06:11 -0700163 XmlCharUniquePtr weightStr(xmlGetProp(fontNode, WEIGHT_ATTR_NAME));
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700164 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 Nonaka36758982018-10-01 19:06:11 -0700169 XmlCharUniquePtr styleStr(xmlGetProp(fontNode, STYLE_ATTR_NAME));
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700170 out->mItalic = styleStr ? xmlStrEqual(styleStr.get(), ITALIC_ATTR_VALUE) : false;
171
172 const xmlChar* INDEX_ATTR_NAME = BAD_CAST("index");
Seigo Nonaka36758982018-10-01 19:06:11 -0700173 XmlCharUniquePtr indexStr(xmlGetProp(fontNode, INDEX_ATTR_NAME));
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700174 out->mCollectionIndex = indexStr ?
175 strtol(reinterpret_cast<const char*>(indexStr.get()), nullptr, 10) : 0;
176
Seigo Nonakab950a1f2021-04-14 19:15:16 -0700177 if (state.mLocale) {
178 out->mLocale.emplace(reinterpret_cast<const char*>(state.mLocale.get()));
179 }
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700180
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 Nonaka36758982018-10-01 19:06:11 -0700185 for (xmlNode* axis = firstElement(fontNode, AXIS_TAG); axis;
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700186 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
207bool 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 Nonaka01709c72019-10-24 18:50:51 -0700216bool findFirstFontNode(const XmlDocUniquePtr& doc, ParserState* state) {
Seigo Nonaka36758982018-10-01 19:06:11 -0700217 xmlNode* familySet = xmlDocGetRootElement(doc.get());
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700218 if (familySet == nullptr) {
Seigo Nonaka01709c72019-10-24 18:50:51 -0700219 return false;
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700220 }
221 xmlNode* family = firstElement(familySet, FAMILY_TAG);
222 if (family == nullptr) {
Seigo Nonaka01709c72019-10-24 18:50:51 -0700223 return false;
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700224 }
Seigo Nonaka01709c72019-10-24 18:50:51 -0700225 state->mLocale.reset(xmlGetProp(family, LOCALE_ATTR_NAME));
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700226
227 xmlNode* font = firstElement(family, FONT_TAG);
228 while (font == nullptr) {
229 family = nextSibling(family, FAMILY_TAG);
230 if (family == nullptr) {
Seigo Nonaka01709c72019-10-24 18:50:51 -0700231 return false;
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700232 }
233 font = firstElement(family, FONT_TAG);
234 }
Seigo Nonaka01709c72019-10-24 18:50:51 -0700235 state->mFontNode = font;
236 return font != nullptr;
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700237}
238
239} // namespace
240
241ASystemFontIterator* ASystemFontIterator_open() {
242 std::unique_ptr<ASystemFontIterator> ite(new ASystemFontIterator());
Seigo Nonakab950a1f2021-04-14 19:15:16 -0700243
244 std::unordered_set<AFont, FontHasher> fonts;
245 minikin::SystemFonts::getFontMap(
246 [&fonts](const std::vector<std::shared_ptr<minikin::FontCollection>>& collections) {
247 for (const auto& fc : collections) {
248 for (const auto& family : fc->getFamilies()) {
249 for (uint32_t i = 0; i < family->getNumFonts(); ++i) {
250 const minikin::Font* font = family->getFont(i);
251
252 std::optional<std::string> locale;
253 uint32_t localeId = font->getLocaleListId();
254 if (localeId != minikin::kEmptyLocaleListId) {
255 locale.emplace(minikin::getLocaleString(localeId));
256 }
257 std::vector<std::pair<uint32_t, float>> axes;
258 for (const auto& [tag, value] : font->typeface()->GetAxes()) {
259 axes.push_back(std::make_pair(tag, value));
260 }
261
262 fonts.insert(
263 {font->typeface()->GetFontPath(), std::move(locale),
264 font->style().weight(),
265 font->style().slant() == minikin::FontStyle::Slant::ITALIC,
266 static_cast<uint32_t>(font->typeface()->GetFontIndex()),
267 axes});
268 }
269 }
270 }
271 });
272
273 if (fonts.empty()) {
274 ite->mXmlDoc.reset(xmlReadFile("/system/etc/fonts.xml", nullptr, 0));
275 ite->mCustomizationXmlDoc.reset(
276 xmlReadFile("/product/etc/fonts_customization.xml", nullptr, 0));
277 } else {
278 ite->index = 0;
279 ite->fonts.assign(fonts.begin(), fonts.end());
280 }
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700281 return ite.release();
282}
283
284void ASystemFontIterator_close(ASystemFontIterator* ite) {
285 delete ite;
286}
287
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700288AFontMatcher* _Nonnull AFontMatcher_create() {
289 return new AFontMatcher();
290}
291
292void AFontMatcher_destroy(AFontMatcher* matcher) {
293 delete matcher;
294}
295
296void AFontMatcher_setStyle(
297 AFontMatcher* _Nonnull matcher,
Seigo Nonaka75b841b2018-10-30 11:39:49 -0700298 uint16_t weight,
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700299 bool italic) {
300 matcher->mFontStyle = minikin::FontStyle(
301 weight, static_cast<minikin::FontStyle::Slant>(italic));
302}
303
304void AFontMatcher_setLocales(
305 AFontMatcher* _Nonnull matcher,
306 const char* _Nonnull languageTags) {
307 matcher->mLocaleListId = minikin::registerLocaleList(languageTags);
308}
309
310void AFontMatcher_setFamilyVariant(AFontMatcher* _Nonnull matcher, uint32_t familyVariant) {
311 matcher->mFamilyVariant = familyVariant;
312}
313
314AFont* _Nonnull AFontMatcher_match(
315 const AFontMatcher* _Nonnull matcher,
316 const char* _Nonnull familyName,
Seigo Nonaka75b841b2018-10-30 11:39:49 -0700317 const uint16_t* _Nonnull text,
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700318 const uint32_t textLength,
Seigo Nonaka75b841b2018-10-30 11:39:49 -0700319 uint32_t* _Nullable runLength) {
320 std::shared_ptr<minikin::FontCollection> fc =
321 minikin::SystemFonts::findFontCollection(familyName);
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700322 std::vector<minikin::FontCollection::Run> runs = fc->itemize(
323 minikin::U16StringPiece(text, textLength),
324 matcher->mFontStyle,
325 matcher->mLocaleListId,
Seigo Nonakaddc87732019-04-05 15:20:19 -0700326 static_cast<minikin::FamilyVariant>(matcher->mFamilyVariant),
327 1 /* maxRun */);
Seigo Nonaka75b841b2018-10-30 11:39:49 -0700328
Seigo Nonakade1e0192021-05-10 00:37:40 -0700329 const std::shared_ptr<minikin::Font>& font =
330 fc->getBestFont(minikin::U16StringPiece(text, textLength), runs[0], matcher->mFontStyle)
331 .font;
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700332 std::unique_ptr<AFont> result = std::make_unique<AFont>();
Seigo Nonaka75b841b2018-10-30 11:39:49 -0700333 const android::MinikinFontSkia* minikinFontSkia =
334 reinterpret_cast<android::MinikinFontSkia*>(font->typeface().get());
335 result->mFilePath = minikinFontSkia->getFilePath();
336 result->mWeight = font->style().weight();
337 result->mItalic = font->style().slant() == minikin::FontStyle::Slant::ITALIC;
338 result->mCollectionIndex = minikinFontSkia->GetFontIndex();
339 const std::vector<minikin::FontVariation>& axes = minikinFontSkia->GetAxes();
340 result->mAxes.reserve(axes.size());
341 for (auto axis : axes) {
342 result->mAxes.push_back(std::make_pair(axis.axisTag, axis.value));
343 }
344 if (runLength != nullptr) {
345 *runLength = runs[0].end;
346 }
347 return result.release();
348}
349
Seigo Nonaka01709c72019-10-24 18:50:51 -0700350bool findNextFontNode(const XmlDocUniquePtr& xmlDoc, ParserState* state) {
351 if (state->mFontNode == nullptr) {
Seigo Nonaka36758982018-10-01 19:06:11 -0700352 if (!xmlDoc) {
Seigo Nonaka01709c72019-10-24 18:50:51 -0700353 return false; // Already at the end.
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700354 } else {
355 // First time to query font.
Seigo Nonaka01709c72019-10-24 18:50:51 -0700356 return findFirstFontNode(xmlDoc, state);
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700357 }
358 } else {
Seigo Nonaka01709c72019-10-24 18:50:51 -0700359 xmlNode* nextNode = nextSibling(state->mFontNode, FONT_TAG);
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700360 while (nextNode == nullptr) {
Seigo Nonaka01709c72019-10-24 18:50:51 -0700361 xmlNode* family = nextSibling(state->mFontNode->parent, FAMILY_TAG);
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700362 if (family == nullptr) {
363 break;
364 }
Seigo Nonaka01709c72019-10-24 18:50:51 -0700365 state->mLocale.reset(xmlGetProp(family, LOCALE_ATTR_NAME));
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700366 nextNode = firstElement(family, FONT_TAG);
367 }
Seigo Nonaka01709c72019-10-24 18:50:51 -0700368 state->mFontNode = nextNode;
369 return nextNode != nullptr;
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700370 }
371}
372
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700373AFont* ASystemFontIterator_next(ASystemFontIterator* ite) {
Seigo Nonaka36758982018-10-01 19:06:11 -0700374 LOG_ALWAYS_FATAL_IF(ite == nullptr, "nullptr has passed as iterator argument");
Seigo Nonakab950a1f2021-04-14 19:15:16 -0700375 if (!ite->fonts.empty()) {
376 if (ite->index >= ite->fonts.size()) {
377 return nullptr;
378 }
379 return new AFont(ite->fonts[ite->index++]);
380 }
381
Seigo Nonaka36758982018-10-01 19:06:11 -0700382 if (ite->mXmlDoc) {
Seigo Nonaka01709c72019-10-24 18:50:51 -0700383 if (!findNextFontNode(ite->mXmlDoc, &ite->state)) {
Seigo Nonaka36758982018-10-01 19:06:11 -0700384 // Reached end of the XML file. Continue OEM customization.
385 ite->mXmlDoc.reset();
Seigo Nonaka36758982018-10-01 19:06:11 -0700386 } else {
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700387 std::unique_ptr<AFont> font = std::make_unique<AFont>();
Seigo Nonaka01709c72019-10-24 18:50:51 -0700388 copyFont(ite->mXmlDoc, ite->state, font.get(), "/system/fonts/");
Seigo Nonaka36758982018-10-01 19:06:11 -0700389 if (!isFontFileAvailable(font->mFilePath)) {
390 return ASystemFontIterator_next(ite);
391 }
392 return font.release();
393 }
394 }
395 if (ite->mCustomizationXmlDoc) {
396 // TODO: Filter only customizationType="new-named-family"
Seigo Nonaka01709c72019-10-24 18:50:51 -0700397 if (!findNextFontNode(ite->mCustomizationXmlDoc, &ite->state)) {
Seigo Nonaka36758982018-10-01 19:06:11 -0700398 // Reached end of the XML file. Finishing
399 ite->mCustomizationXmlDoc.reset();
Seigo Nonaka36758982018-10-01 19:06:11 -0700400 return nullptr;
401 } else {
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700402 std::unique_ptr<AFont> font = std::make_unique<AFont>();
Seigo Nonaka01709c72019-10-24 18:50:51 -0700403 copyFont(ite->mCustomizationXmlDoc, ite->state, font.get(), "/product/fonts/");
Seigo Nonaka36758982018-10-01 19:06:11 -0700404 if (!isFontFileAvailable(font->mFilePath)) {
405 return ASystemFontIterator_next(ite);
406 }
407 return font.release();
408 }
409 }
410 return nullptr;
411}
412
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700413void AFont_close(AFont* font) {
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700414 delete font;
415}
416
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700417const char* AFont_getFontFilePath(const AFont* font) {
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700418 LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed as font argument");
419 return font->mFilePath.c_str();
420}
421
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700422uint16_t AFont_getWeight(const AFont* font) {
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700423 LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed as font argument");
424 return font->mWeight;
425}
426
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700427bool AFont_isItalic(const AFont* font) {
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700428 LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed as font argument");
429 return font->mItalic;
430}
431
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700432const char* AFont_getLocale(const AFont* font) {
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700433 LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed to font argument");
Seigo Nonaka01709c72019-10-24 18:50:51 -0700434 return font->mLocale ? font->mLocale->c_str() : nullptr;
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700435}
436
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700437size_t AFont_getCollectionIndex(const AFont* font) {
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700438 LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed to font argument");
439 return font->mCollectionIndex;
440}
441
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700442size_t AFont_getAxisCount(const AFont* font) {
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700443 LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed to font argument");
444 return font->mAxes.size();
445}
446
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700447uint32_t AFont_getAxisTag(const AFont* font, uint32_t axisIndex) {
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700448 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].first;
452}
453
Seigo Nonakab3a7bce2019-03-29 14:24:57 -0700454float AFont_getAxisValue(const AFont* font, uint32_t axisIndex) {
Seigo Nonaka50692ca2018-08-31 12:27:15 -0700455 LOG_ALWAYS_FATAL_IF(font == nullptr, "nullptr has passed to font argument");
456 LOG_ALWAYS_FATAL_IF(axisIndex >= font->mAxes.size(),
457 "given axis index is out of bounds. (< %zd", font->mAxes.size());
458 return font->mAxes[axisIndex].second;
459}