blob: 63b03287499582428a3e53c2abb418b7d8aafcd2 [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
120/**
121 * AFontMatcher performs match operation on given parameters and available font files.
122 * This matcher is not a thread-safe object. Do not pass this matcher to other threads.
123 */
124struct AFontMatcher;
125
126/**
127 * Select the best font from given parameters.
128 *
129 */
130
131/**
Elliott Hughes3d70e532019-10-29 08:59:39 -0700132 * Creates a new AFontMatcher object.
133 *
134 * Available since API level 29.
Seigo Nonakacb88a652019-03-29 14:22:49 -0700135 */
136AFontMatcher* _Nonnull AFontMatcher_create() __INTRODUCED_IN(29);
137
138/**
139 * Destroy the matcher object.
140 *
Elliott Hughes3d70e532019-10-29 08:59:39 -0700141 * Available since API level 29.
142 *
Seigo Nonakacb88a652019-03-29 14:22:49 -0700143 * \param matcher a matcher object. Passing NULL is not allowed.
144 */
145void AFontMatcher_destroy(AFontMatcher* _Nonnull matcher) __INTRODUCED_IN(29);
146
147/**
148 * Set font style to matcher.
149 *
gfan7fed43d2021-04-06 18:50:06 -0700150 * If this function is not called, the matcher performs with {@link AFONT_WEIGHT_NORMAL}
Seigo Nonakacb88a652019-03-29 14:22:49 -0700151 * with non-italic style.
152 *
Elliott Hughes3d70e532019-10-29 08:59:39 -0700153 * Available since API level 29.
154 *
Seigo Nonakacb88a652019-03-29 14:22:49 -0700155 * \param matcher a matcher object. Passing NULL is not allowed.
156 * \param weight a font weight value. Only from 0 to 1000 value is valid
157 * \param italic true if italic, otherwise false.
158 */
159void AFontMatcher_setStyle(
160 AFontMatcher* _Nonnull matcher,
161 uint16_t weight,
162 bool italic) __INTRODUCED_IN(29);
163
164/**
165 * Set font locales to matcher.
166 *
167 * If this function is not called, the matcher performs with empty locale list.
168 *
Elliott Hughes3d70e532019-10-29 08:59:39 -0700169 * Available since API level 29.
170 *
Seigo Nonakacb88a652019-03-29 14:22:49 -0700171 * \param matcher a matcher object. Passing NULL is not allowed.
172 * \param languageTags a null character terminated comma separated IETF BCP47 compliant language
173 * tags.
174 */
175void AFontMatcher_setLocales(
176 AFontMatcher* _Nonnull matcher,
177 const char* _Nonnull languageTags) __INTRODUCED_IN(29);
178
179/**
180 * Set family variant to matcher.
181 *
182 * If this function is not called, the matcher performs with {@link AFAMILY_VARIANT_DEFAULT}.
183 *
Elliott Hughes3d70e532019-10-29 08:59:39 -0700184 * Available since API level 29.
185 *
Seigo Nonakacb88a652019-03-29 14:22:49 -0700186 * \param matcher a matcher object. Passing NULL is not allowed.
187 * \param familyVariant Must be one of {@link AFAMILY_VARIANT_DEFAULT},
188 * {@link AFAMILY_VARIANT_COMPACT} or {@link AFAMILY_VARIANT_ELEGANT} is valid.
189 */
190void AFontMatcher_setFamilyVariant(
191 AFontMatcher* _Nonnull matcher,
192 uint32_t familyVariant) __INTRODUCED_IN(29);
193
194/**
195 * Performs the matching from the generic font family for the text and select one font.
196 *
197 * For more information about generic font families, read [W3C spec](https://www.w3.org/TR/css-fonts-4/#generic-font-families)
198 *
199 * Even if no font can render the given text, this function will return a non-null result for
200 * drawing Tofu character.
201 *
Elliott Hughes3d70e532019-10-29 08:59:39 -0700202 * Available since API level 29.
203 *
Seigo Nonakacb88a652019-03-29 14:22:49 -0700204 * \param matcher a matcher object. Passing NULL is not allowed.
205 * \param familyName a null character terminated font family name
206 * \param text a UTF-16 encoded text buffer to be rendered. Do not pass empty string.
207 * \param textLength a length of the given text buffer. This must not be zero.
208 * \param runLengthOut if not null, the font run length will be filled.
209 * \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 -0700210 * AFont_close when it is no longer needed.
Seigo Nonakacb88a652019-03-29 14:22:49 -0700211 */
212AFont* _Nonnull AFontMatcher_match(
213 const AFontMatcher* _Nonnull matcher,
214 const char* _Nonnull familyName,
215 const uint16_t* _Nonnull text,
216 const uint32_t textLength,
217 uint32_t* _Nullable runLengthOut) __INTRODUCED_IN(29);
218
Seigo Nonakacb88a652019-03-29 14:22:49 -0700219__END_DECLS
220
221#endif // ANDROID_FONT_MATCHER_H
Dan Albertc0416092019-04-16 13:20:26 -0700222
223/** @} */