Seigo Nonaka | 667b69a | 2018-08-31 12:28:14 -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 | /** |
| 18 | * @file system_fonts.h |
| 19 | * @brief Provides the system font configurations. |
| 20 | * |
| 21 | * These APIs provides the list of system installed font files with additional metadata about the |
| 22 | * font. |
| 23 | * |
| 24 | * The ASystemFontIterator_open method will give you an iterator which can iterate all system |
| 25 | * installed font files as shown in the following example. |
| 26 | * |
| 27 | * <code> |
| 28 | * ASystemFontIterator* iterator = ASystemFontIterator_open(); |
| 29 | * ASystemFont* font = NULL; |
| 30 | * |
| 31 | * while ((font = ASystemFontIterator_next(iterator)) != nullptr) { |
| 32 | * // Look if the font is your desired one. |
| 33 | * if (ASystemFont_getWeight(font) == 400 && !ASystemFont_isItalic(font) |
| 34 | * && ASystemFont_getLocale(font) == NULL) { |
| 35 | * break; |
| 36 | * } |
| 37 | * ASystemFont_close(font); |
| 38 | * } |
| 39 | * ASystemFontIterator_close(iterator); |
| 40 | * |
| 41 | * int fd = open(ASystemFont_getFontFilePath(font), O_RDONLY); |
| 42 | * int collectionIndex = ASystemFont_getCollectionINdex(font); |
| 43 | * std::vector<std::pair<uint32_t, float>> variationSettings; |
| 44 | * for (size_t i = 0; i < ASystemFont_getAxisCount(font); ++i) { |
| 45 | * variationSettings.push_back(std::make_pair( |
| 46 | * ASystemFont_getAxisTag(font, i), |
| 47 | * ASystemFont_getAxisValue(font, i))); |
| 48 | * } |
| 49 | * ASystemFont_close(font); |
| 50 | * |
| 51 | * // Use this font for your text rendering engine. |
| 52 | * |
| 53 | * </code> |
| 54 | * |
| 55 | * Available since API level 29. |
| 56 | */ |
| 57 | |
| 58 | #ifndef ANDROID_SYSTEM_FONTS_H |
| 59 | #define ANDROID_SYSTEM_FONTS_H |
| 60 | |
| 61 | #include <stdbool.h> |
| 62 | #include <stddef.h> |
| 63 | #include <sys/cdefs.h> |
| 64 | |
| 65 | /****************************************************************** |
| 66 | * |
| 67 | * IMPORTANT NOTICE: |
| 68 | * |
| 69 | * This file is part of Android's set of stable system headers |
| 70 | * exposed by the Android NDK (Native Development Kit). |
| 71 | * |
| 72 | * Third-party source AND binary code relies on the definitions |
| 73 | * here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES. |
| 74 | * |
| 75 | * - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES) |
| 76 | * - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS |
| 77 | * - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY |
| 78 | * - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES |
| 79 | */ |
| 80 | |
| 81 | __BEGIN_DECLS |
| 82 | |
| 83 | #if __ANDROID_API__ >= 29 |
| 84 | |
| 85 | enum { |
| 86 | /** The minimum value fot the font weight value. */ |
| 87 | ASYSTEM_FONT_WEIGHT_MIN = 0, |
| 88 | |
| 89 | /** A font weight value for the thin weight. */ |
| 90 | ASYSTEM_FONT_WEIGHT_THIN = 100, |
| 91 | |
| 92 | /** A font weight value for the extra-light weight. */ |
| 93 | ASYSTEM_FONT_WEIGHT_EXTRA_LIGHT = 200, |
| 94 | |
| 95 | /** A font weight value for the light weight. */ |
| 96 | ASYSTEM_FONT_WEIGHT_LIGHT = 300, |
| 97 | |
| 98 | /** A font weight value for the normal weight. */ |
| 99 | ASYSTEM_FONT_WEIGHT_NORMAL = 400, |
| 100 | |
| 101 | /** A font weight value for the medium weight. */ |
| 102 | ASYSTEM_FONT_WEIGHT_MEDIUM = 500, |
| 103 | |
| 104 | /** A font weight value for the semi-bold weight. */ |
| 105 | ASYSTEM_FONT_WEIGHT_SEMI_BOLD = 600, |
| 106 | |
| 107 | /** A font weight value for the bold weight. */ |
| 108 | ASYSTEM_FONT_WEIGHT_BOLD = 700, |
| 109 | |
| 110 | /** A font weight value for the extra-bold weight. */ |
| 111 | ASYSTEM_FONT_WEIGHT_EXTRA_BOLD = 800, |
| 112 | |
| 113 | /** A font weight value for the black weight. */ |
| 114 | ASYSTEM_FONT_WEIGHT_BLACK = 900, |
| 115 | |
| 116 | /** The maximum value for the font weight value. */ |
| 117 | ASYSTEM_FONT_WEIGHT_MAX = 1000 |
| 118 | }; |
| 119 | |
| 120 | /** |
| 121 | * ASystemFontIterator provides access to the system font configuration. |
| 122 | * |
| 123 | * ASystemFontIterator is an iterator for all available system font settings. |
| 124 | * This iterator is not a thread-safe object. Do not pass this iterator to other threads. |
| 125 | */ |
| 126 | struct ASystemFontIterator; |
| 127 | |
| 128 | /** |
| 129 | * ASystemFont provides information of the single system font configuration. |
| 130 | */ |
| 131 | struct ASystemFont; |
| 132 | |
| 133 | /** |
| 134 | * Create a system font iterator. |
| 135 | * |
| 136 | * Use ASystemFont_close() to close the iterator. |
| 137 | * |
| 138 | * \return a pointer for a newly allocated iterator, nullptr on failure. |
| 139 | */ |
| 140 | ASystemFontIterator* _Nullable ASystemFontIterator_open() __INTRODUCED_IN(29); |
| 141 | |
| 142 | /** |
| 143 | * Close an opened system font iterator, freeing any related resources. |
| 144 | * |
| 145 | * \param a pointer of an iterator for the system fonts. Do nothing if NULL is passed. |
| 146 | */ |
| 147 | void ASystemFontIterator_close(ASystemFontIterator* _Nullable iterator) __INTRODUCED_IN(29); |
| 148 | |
| 149 | /** |
| 150 | * Move to the next system font. |
| 151 | * |
| 152 | * \param iterator an iterator for the system fonts. Passing NULL is not allowed. |
| 153 | * \return true if more system fonts are available, otherwise false. |
| 154 | */ |
| 155 | ASystemFont* _Nullable ASystemFontIterator_next(ASystemFontIterator* _Nonnull iterator) __INTRODUCED_IN(29); |
| 156 | |
| 157 | /** |
| 158 | * Close an ASystemFont returned by ASystemFontIterator_next. |
| 159 | * |
| 160 | * \param font a font returned by ASystemFontIterator_next. Do nothing if NULL is passed. |
| 161 | */ |
| 162 | void ASystemFont_close(ASystemFont* _Nullable font) __INTRODUCED_IN(29); |
| 163 | |
| 164 | |
| 165 | /** |
| 166 | * Return an absolute path to the current font file. |
| 167 | * |
| 168 | * Here is a list of font formats returned by this method: |
| 169 | * <ul> |
| 170 | * <li>OpenType</li> |
| 171 | * <li>OpenType Font Collection</li> |
| 172 | * <li>TrueType</li> |
| 173 | * <li>TrueType Collection</li> |
| 174 | * </ul> |
| 175 | * The file extension could be one of *.otf, *.ttf, *.otc or *.ttc. |
| 176 | * |
| 177 | * The font file returned is guaranteed to be opend with O_RDONLY. |
| 178 | * Note that the returned pointer is valid until ASystemFont_close() is called for the given font. |
| 179 | * |
| 180 | * \param iterator an iterator for the system fonts. Passing NULL is not allowed. |
| 181 | * \return a string of the font file path. |
| 182 | */ |
| 183 | const char* _Nonnull ASystemFont_getFontFilePath(const ASystemFont* _Nonnull font) __INTRODUCED_IN(29); |
| 184 | |
| 185 | /** |
| 186 | * Return a weight value associated with the current font. |
| 187 | * |
| 188 | * The weight values are positive and less than or equal to 1000. |
| 189 | * Here are pairs of the common names and their values. |
| 190 | * <p> |
| 191 | * <table> |
| 192 | * <thead> |
| 193 | * <tr> |
| 194 | * <th align="center">Value</th> |
| 195 | * <th align="center">Name</th> |
| 196 | * <th align="center">NDK Definition</th> |
| 197 | * </tr> |
| 198 | * </thead> |
| 199 | * <tbody> |
| 200 | * <tr> |
| 201 | * <td align="center">100</td> |
| 202 | * <td align="center">Thin</td> |
| 203 | * <td align="center">{@link ASYSTEM_FONT_WEIGHT_THIN}</td> |
| 204 | * </tr> |
| 205 | * <tr> |
| 206 | * <td align="center">200</td> |
| 207 | * <td align="center">Extra Light (Ultra Light)</td> |
| 208 | * <td align="center">{@link ASYSTEM_FONT_WEIGHT_EXTRA_LIGHT}</td> |
| 209 | * </tr> |
| 210 | * <tr> |
| 211 | * <td align="center">300</td> |
| 212 | * <td align="center">Light</td> |
| 213 | * <td align="center">{@link ASYSTEM_FONT_WEIGHT_LIGHT}</td> |
| 214 | * </tr> |
| 215 | * <tr> |
| 216 | * <td align="center">400</td> |
| 217 | * <td align="center">Normal (Regular)</td> |
| 218 | * <td align="center">{@link ASYSTEM_FONT_WEIGHT_NORMAL}</td> |
| 219 | * </tr> |
| 220 | * <tr> |
| 221 | * <td align="center">500</td> |
| 222 | * <td align="center">Medium</td> |
| 223 | * <td align="center">{@link ASYSTEM_FONT_WEIGHT_MEDIUM}</td> |
| 224 | * </tr> |
| 225 | * <tr> |
| 226 | * <td align="center">600</td> |
| 227 | * <td align="center">Semi Bold (Demi Bold)</td> |
| 228 | * <td align="center">{@link ASYSTEM_FONT_WEIGHT_SEMI_BOLD}</td> |
| 229 | * </tr> |
| 230 | * <tr> |
| 231 | * <td align="center">700</td> |
| 232 | * <td align="center">Bold</td> |
| 233 | * <td align="center">{@link ASYSTEM_FONT_WEIGHT_BOLD}</td> |
| 234 | * </tr> |
| 235 | * <tr> |
| 236 | * <td align="center">800</td> |
| 237 | * <td align="center">Extra Bold (Ultra Bold)</td> |
| 238 | * <td align="center">{@link ASYSTEM_FONT_WEIGHT_EXTRA_BOLD}</td> |
| 239 | * </tr> |
| 240 | * <tr> |
| 241 | * <td align="center">900</td> |
| 242 | * <td align="center">Black (Heavy)</td> |
| 243 | * <td align="center">{@link ASYSTEM_FONT_WEIGHT_BLACK}</td> |
| 244 | * </tr> |
| 245 | * </tbody> |
| 246 | * </p> |
| 247 | * Note that the weight value may fall in between above values, e.g. 250 weight. |
| 248 | * |
| 249 | * For more information about font weight, read [OpenType usWeightClass](https://docs.microsoft.com/en-us/typography/opentype/spec/os2#usweightclass) |
| 250 | * |
| 251 | * \param iterator an iterator for the system fonts. Passing NULL is not allowed. |
| 252 | * \return a positive integer less than or equal to {@link ASYSTEM_FONT_MAX_WEIGHT} is returned. |
| 253 | */ |
| 254 | uint16_t ASystemFont_getWeight(const ASystemFont* _Nonnull font) __INTRODUCED_IN(29); |
| 255 | |
| 256 | /** |
| 257 | * Return true if the current font is italic, otherwise returns false. |
| 258 | * |
| 259 | * \param iterator an iterator for the system fonts. Passing NULL is not allowed. |
| 260 | * \return true if italic, otherwise false. |
| 261 | */ |
| 262 | bool ASystemFont_isItalic(const ASystemFont* _Nonnull font) __INTRODUCED_IN(29); |
| 263 | |
| 264 | /** |
| 265 | * Return a IETF BCP47 compliant language tag associated with the current font. |
| 266 | * |
| 267 | * For information about IETF BCP47, read [Locale.forLanguageTag(java.lang.String)](https://developer.android.com/reference/java/util/Locale.html#forLanguageTag(java.lang.String)") |
| 268 | * |
| 269 | * Note that the returned pointer is valid until ASystemFont_close() is called. |
| 270 | * |
| 271 | * \param iterator an iterator for the system fonts. Passing NULL is not allowed. |
| 272 | * \return a IETF BCP47 compliant langauge tag or nullptr if not available. |
| 273 | */ |
| 274 | const char* _Nullable ASystemFont_getLocale(const ASystemFont* _Nonnull font) __INTRODUCED_IN(29); |
| 275 | |
| 276 | /** |
| 277 | * Return a font collection index value associated with the current font. |
| 278 | * |
| 279 | * In case the target font file is a font collection (e.g. .ttc or .otc), this |
| 280 | * returns a non-negative value as an font offset in the collection. This |
| 281 | * always returns 0 if the target font file is a regular font. |
| 282 | * |
| 283 | * \param iterator an iterator for the system fonts. Passing NULL is not allowed. |
| 284 | * \return a font collection index. |
| 285 | */ |
| 286 | size_t ASystemFont_getCollectionIndex(const ASystemFont* _Nonnull font) __INTRODUCED_IN(29); |
| 287 | |
| 288 | /** |
| 289 | * Return a count of font variation settings associated with the current font |
| 290 | * |
| 291 | * The font variation settings are provided as multiple tag-values pairs. |
| 292 | * |
| 293 | * For example, bold italic font may have following font variation settings: |
| 294 | * 'wght' 700, 'slnt' -12 |
| 295 | * In this case, ASystemFont_getAxisCount returns 2 and ASystemFont_getAxisTag |
| 296 | * and ASystemFont_getAxisValue will return following values. |
| 297 | * <code> |
| 298 | * ASystemFont* font = ASystemFontIterator_next(ite); |
| 299 | * |
| 300 | * // Returns the number of axes |
| 301 | * ASystemFont_getAxisCount(font); // Returns 2 |
| 302 | * |
| 303 | * // Returns the tag-value pair for the first axis. |
| 304 | * ASystemFont_getAxisTag(font, 0); // Returns 'wght'(0x77676874) |
| 305 | * ASystemFont_getAxisValue(font, 0); // Returns 700.0 |
| 306 | * |
| 307 | * // Returns the tag-value pair for the second axis. |
| 308 | * ASystemFont_getAxisTag(font, 1); // Returns 'slnt'(0x736c6e74) |
| 309 | * ASystemFont_getAxisValue(font, 1); // Returns -12.0 |
| 310 | * </code> |
| 311 | * |
| 312 | * For more information about font variation settings, read [Font Variations Table](https://docs.microsoft.com/en-us/typography/opentype/spec/fvar) |
| 313 | * |
| 314 | * \param iterator an iterator for the system fonts. Passing NULL is not allowed. |
| 315 | * \return a number of font variation settings. |
| 316 | */ |
| 317 | size_t ASystemFont_getAxisCount(const ASystemFont* _Nonnull font) __INTRODUCED_IN(29); |
| 318 | |
| 319 | |
| 320 | /** |
| 321 | * Return an OpenType axis tag associated with the current font. |
| 322 | * |
| 323 | * See ASystemFont_getAxisCount for more details. |
| 324 | * |
| 325 | * \param iterator an iterator for the system fonts. Passing NULL is not allowed. |
| 326 | * \param an index to the font variation settings. Passing value larger than or |
| 327 | * equal to {@link ASystemFont_getAxisCount} is not allowed. |
| 328 | * \return an OpenType axis tag value for the given font variation setting. |
| 329 | */ |
| 330 | uint32_t ASystemFont_getAxisTag(const ASystemFont* _Nonnull font, uint32_t axisIndex) |
| 331 | __INTRODUCED_IN(29); |
| 332 | |
| 333 | /** |
| 334 | * Return an OpenType axis value associated with the current font. |
| 335 | * |
| 336 | * See ASystemFont_getAxisCount for more details. |
| 337 | * |
| 338 | * \param iterator an iterator for the system fonts. Passing NULL is not allowed. |
| 339 | * \param an index to the font variation settings. Passing value larger than or |
| 340 | * equal to {@link ASYstemFont_getAxisCount} is not allwed. |
| 341 | * \return a float value for the given font variation setting. |
| 342 | */ |
| 343 | float ASystemFont_getAxisValue(const ASystemFont* _Nonnull font, uint32_t axisIndex) |
| 344 | __INTRODUCED_IN(29); |
| 345 | |
| 346 | #endif // __ANDROID_API__ >= 29 |
| 347 | |
| 348 | __END_DECLS |
| 349 | |
| 350 | #endif // ANDROID_SYSTEM_FONTS_H |