blob: 60ff95e1235219308b11aa48542cb087eb9b0754 [file] [log] [blame]
Seigo Nonakacb88a652019-03-29 14:22:49 -07001/*
2 * Copyright (C) 2019 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/**
Dan Albertc0416092019-04-16 13:20:26 -070018 * @addtogroup Font
Dan Alberte4a1cab2019-06-14 14:01:20 -070019 * @{
Dan Albertc0416092019-04-16 13:20:26 -070020 */
21
22/**
Seigo Nonakacb88a652019-03-29 14:22:49 -070023 * @file font_matcher.h
24 * @brief Provides the font matching logic with various inputs.
25 *
26 * You can use this class for deciding what font is to be used for drawing text.
27 *
28 * A matcher is created from text style, locales and UI compatibility. The match function for
29 * matcher object can be called multiple times until close function is called.
30 *
31 * Even if no font can render the given text, the match function will return a non-null result for
32 * drawing Tofu character.
33 *
34 * Examples:
35 * \code{.cpp}
36 * // Simple font query for the ASCII character.
37 * std::vector<uint16_t> text = { 'A' };
38 * AFontMatcher* matcher = AFontMatcher_create("sans-serif");
gfan7fed43d2021-04-06 18:50:06 -070039 * AFont* font = AFontMatcher_match(text.data(), text.length(), &runLength);
Seigo Nonakacb88a652019-03-29 14:22:49 -070040 * // runLength will be 1 and the font will points a valid font file.
41 * AFontMatcher_destroy(matcher);
42 *
43 * // Querying font for CJK characters
44 * std::vector<uint16_t> text = { 0x9AA8 };
45 * AFontMatcher* matcher = AFontMatcher_create("sans-serif");
46 * AFontMatcher_setLocales(matcher, "zh-CN,ja-JP");
gfan7fed43d2021-04-06 18:50:06 -070047 * AFont* font = AFontMatcher_match(text.data(), text.length(), &runLength);
Seigo Nonakacb88a652019-03-29 14:22:49 -070048 * // runLength will be 1 and the font will points a Simplified Chinese font.
49 * AFontMatcher_setLocales(matcher, "ja-JP,zh-CN");
gfan7fed43d2021-04-06 18:50:06 -070050 * AFont* font = AFontMatcher_match(text.data(), text.length(), &runLength);
Seigo Nonakacb88a652019-03-29 14:22:49 -070051 * // runLength will be 1 and the font will points a Japanese font.
52 * AFontMatcher_destroy(matcher);
53 *
54 * // Querying font for text/color emoji
55 * std::vector<uint16_t> text = { 0xD83D, 0xDC68, 0x200D, 0x2764, 0xFE0F, 0x200D, 0xD83D, 0xDC68 };
56 * AFontMatcher* matcher = AFontMatcher_create("sans-serif");
gfan7fed43d2021-04-06 18:50:06 -070057 * AFont* font = AFontMatcher_match(text.data(), text.length(), &runLength);
Seigo Nonakacb88a652019-03-29 14:22:49 -070058 * // runLength will be 8 and the font will points a color emoji font.
59 * AFontMatcher_destroy(matcher);
60 *
61 * // Mixture of multiple script of characters.
62 * // 0x05D0 is a Hebrew character and 0x0E01 is a Thai character.
63 * std::vector<uint16_t> text = { 0x05D0, 0x0E01 };
64 * AFontMatcher* matcher = AFontMatcher_create("sans-serif");
gfan7fed43d2021-04-06 18:50:06 -070065 * AFont* font = AFontMatcher_match(text.data(), text.length(), &runLength);
Seigo Nonakacb88a652019-03-29 14:22:49 -070066 * // runLength will be 1 and the font will points a Hebrew font.
67 * AFontMatcher_destroy(matcher);
68 * \endcode
69 *
70 * Available since API level 29.
71 */
72
73#ifndef ANDROID_FONT_MATCHER_H
74#define ANDROID_FONT_MATCHER_H
75
76#include <stdbool.h>
77#include <stddef.h>
Ben Wagnerffcf5b12022-10-26 21:30:12 +000078#include <stdint.h>
Seigo Nonakacb88a652019-03-29 14:22:49 -070079#include <sys/cdefs.h>
80
81#include <android/font.h>
82
83/******************************************************************
84 *
85 * IMPORTANT NOTICE:
86 *
87 * This file is part of Android's set of stable system headers
88 * exposed by the Android NDK (Native Development Kit).
89 *
90 * Third-party source AND binary code relies on the definitions
91 * here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES.
92 *
93 * - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES)
94 * - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS
95 * - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY
96 * - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES
97 */
98
99__BEGIN_DECLS
100
Seigo Nonakacb88a652019-03-29 14:22:49 -0700101enum {
102 /** A family variant value for the system default variant. */
103 AFAMILY_VARIANT_DEFAULT = 0,
104
105 /**
106 * A family variant value for the compact font family variant.
107 *
108 * The compact font family has Latin-based vertical metrics.
109 */
110 AFAMILY_VARIANT_COMPACT = 1,
111
112 /**
113 * A family variant value for the elegant font family variant.
114 *
115 * The elegant font family may have larger vertical metrics than Latin font.
116 */
117 AFAMILY_VARIANT_ELEGANT = 2,
118};
119
Ben Wagner25580412022-10-31 19:18:31 +0000120struct AFontMatcher;
Seigo Nonakacb88a652019-03-29 14:22:49 -0700121/**
122 * AFontMatcher performs match operation on given parameters and available font files.
123 * This matcher is not a thread-safe object. Do not pass this matcher to other threads.
124 */
Ben Wagner25580412022-10-31 19:18:31 +0000125typedef struct AFontMatcher AFontMatcher;
Seigo Nonakacb88a652019-03-29 14:22:49 -0700126
127/**
128 * Select the best font from given parameters.
129 *
130 */
131
132/**
Elliott Hughes3d70e532019-10-29 08:59:39 -0700133 * Creates a new AFontMatcher object.
134 *
135 * Available since API level 29.
Seigo Nonakacb88a652019-03-29 14:22:49 -0700136 */
137AFontMatcher* _Nonnull AFontMatcher_create() __INTRODUCED_IN(29);
138
139/**
140 * Destroy the matcher object.
141 *
Elliott Hughes3d70e532019-10-29 08:59:39 -0700142 * Available since API level 29.
143 *
Seigo Nonakacb88a652019-03-29 14:22:49 -0700144 * \param matcher a matcher object. Passing NULL is not allowed.
145 */
146void AFontMatcher_destroy(AFontMatcher* _Nonnull matcher) __INTRODUCED_IN(29);
147
148/**
149 * Set font style to matcher.
150 *
gfan7fed43d2021-04-06 18:50:06 -0700151 * If this function is not called, the matcher performs with {@link AFONT_WEIGHT_NORMAL}
Seigo Nonakacb88a652019-03-29 14:22:49 -0700152 * with non-italic style.
153 *
Elliott Hughes3d70e532019-10-29 08:59:39 -0700154 * Available since API level 29.
155 *
Seigo Nonakacb88a652019-03-29 14:22:49 -0700156 * \param matcher a matcher object. Passing NULL is not allowed.
157 * \param weight a font weight value. Only from 0 to 1000 value is valid
158 * \param italic true if italic, otherwise false.
159 */
160void AFontMatcher_setStyle(
161 AFontMatcher* _Nonnull matcher,
162 uint16_t weight,
163 bool italic) __INTRODUCED_IN(29);
164
165/**
166 * Set font locales to matcher.
167 *
168 * If this function is not called, the matcher performs with empty locale list.
169 *
Elliott Hughes3d70e532019-10-29 08:59:39 -0700170 * Available since API level 29.
171 *
Seigo Nonakacb88a652019-03-29 14:22:49 -0700172 * \param matcher a matcher object. Passing NULL is not allowed.
173 * \param languageTags a null character terminated comma separated IETF BCP47 compliant language
174 * tags.
175 */
176void AFontMatcher_setLocales(
177 AFontMatcher* _Nonnull matcher,
178 const char* _Nonnull languageTags) __INTRODUCED_IN(29);
179
180/**
181 * Set family variant to matcher.
182 *
183 * If this function is not called, the matcher performs with {@link AFAMILY_VARIANT_DEFAULT}.
184 *
Elliott Hughes3d70e532019-10-29 08:59:39 -0700185 * Available since API level 29.
186 *
Seigo Nonakacb88a652019-03-29 14:22:49 -0700187 * \param matcher a matcher object. Passing NULL is not allowed.
188 * \param familyVariant Must be one of {@link AFAMILY_VARIANT_DEFAULT},
189 * {@link AFAMILY_VARIANT_COMPACT} or {@link AFAMILY_VARIANT_ELEGANT} is valid.
190 */
191void AFontMatcher_setFamilyVariant(
192 AFontMatcher* _Nonnull matcher,
193 uint32_t familyVariant) __INTRODUCED_IN(29);
194
195/**
196 * Performs the matching from the generic font family for the text and select one font.
197 *
198 * For more information about generic font families, read [W3C spec](https://www.w3.org/TR/css-fonts-4/#generic-font-families)
199 *
200 * Even if no font can render the given text, this function will return a non-null result for
201 * drawing Tofu character.
202 *
Elliott Hughes3d70e532019-10-29 08:59:39 -0700203 * Available since API level 29.
204 *
Seigo Nonakacb88a652019-03-29 14:22:49 -0700205 * \param matcher a matcher object. Passing NULL is not allowed.
206 * \param familyName a null character terminated font family name
207 * \param text a UTF-16 encoded text buffer to be rendered. Do not pass empty string.
208 * \param textLength a length of the given text buffer. This must not be zero.
209 * \param runLengthOut if not null, the font run length will be filled.
210 * \return a font to be used for given text and params. You need to release the returned font by
gfan7fed43d2021-04-06 18:50:06 -0700211 * AFont_close when it is no longer needed.
Seigo Nonakacb88a652019-03-29 14:22:49 -0700212 */
213AFont* _Nonnull AFontMatcher_match(
214 const AFontMatcher* _Nonnull matcher,
215 const char* _Nonnull familyName,
216 const uint16_t* _Nonnull text,
217 const uint32_t textLength,
218 uint32_t* _Nullable runLengthOut) __INTRODUCED_IN(29);
219
Seigo Nonakacb88a652019-03-29 14:22:49 -0700220__END_DECLS
221
222#endif // ANDROID_FONT_MATCHER_H
Dan Albertc0416092019-04-16 13:20:26 -0700223
224/** @} */