Blender V5.3
BLF_tests.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "BLF_api.hh"
6
7#include "BLI_path_utils.hh"
8
9#include "BKE_gtest_base.hh"
10
11#include "testing/testing.h"
12
13namespace blender::tests {
14
15static std::string font_path(std::string font_name)
16{
17 char path[FILE_MAX];
19 path, sizeof(path), tests::flags_test_asset_dir().c_str(), "blenfont", font_name.c_str());
20 return std::string(path);
21}
22
23static int open_font(std::string font_name)
24{
25 BLF_init();
26 return BLF_load(font_path(font_name).c_str());
27}
28
29static void close_font(int id)
30{
31 BLF_unload_id(id);
32 BLF_exit();
33}
34
36
38{
39 const int id = open_font("Ahem.ttf");
40 EXPECT_TRUE(id != -1);
41 close_font(id);
42}
43
44TEST_F(BlfLoadTest, font_is_loaded_path)
45{
46 BLF_init();
47 std::string path = font_path("Ahem.ttf");
48 const int id = BLF_load(path.c_str());
49 EXPECT_TRUE(BLF_is_loaded(path.c_str()));
50 close_font(id);
51}
52
53TEST_F(BlfLoadTest, font_is_loaded_id)
54{
55 const int id = open_font("Ahem.ttf");
57 close_font(id);
58}
59
60TEST_F(BlfLoadTest, display_name_from_file)
61{
62 std::string path = font_path("Ahem.ttf");
63 const char *name = BLF_display_name_from_file(path.c_str());
64 EXPECT_TRUE(STREQ(name, "Ahem Regular"));
65 /* BLF_display_name result must be freed. */
66 MEM_delete(name);
67}
68
69TEST_F(BlfLoadTest, display_name_from_id)
70{
71 const int id = open_font("Ahem.ttf");
72 const char *name = BLF_display_name_from_id(id);
73 EXPECT_TRUE(STREQ(name, "Ahem Regular"));
74 /* BLF_display_name result must be freed. */
75 MEM_delete(name);
76 close_font(id);
77}
78
80{
81 const int id = open_font("Ahem.ttf");
82 const bool has_glyph = BLF_has_glyph(id, 0x0058); /* 'X' */
83 EXPECT_TRUE(has_glyph);
84 close_font(id);
85}
86
88
89TEST_F(BlfMetricsTest, get_vfont_metrics)
90{
91 const int id = open_font("Ahem.ttf");
92 float ascend_ratio = 0.0f;
93 float em_ratio = 0.0f;
94 float scale = 0.0f;
95 const bool has_metrics = BLF_get_vfont_metrics(id, &ascend_ratio, &em_ratio, &scale);
96 EXPECT_TRUE(has_metrics);
97 EXPECT_TRUE(ascend_ratio == 0.8f);
98 EXPECT_TRUE(em_ratio == 1.0f);
99 EXPECT_TRUE(scale == 0.001f);
100 close_font(id);
101}
102
103TEST_F(BlfMetricsTest, default_weight)
104{
105 const int id = open_font("Ahem.ttf");
106 const int weight = BLF_default_weight(id);
107 EXPECT_TRUE(weight == 400);
108 close_font(id);
109}
110
111TEST_F(BlfMetricsTest, has_variable_weight)
112{
113 const int id = open_font("Roboto.ttf");
114 const bool has_variable_weight = BLF_has_variable_weight(id);
115 EXPECT_TRUE(has_variable_weight);
116 close_font(id);
117}
118
119TEST_F(BlfMetricsTest, variable_weight)
120{
121 const int id = open_font("Roboto.ttf");
122 const char sample[] = "MM";
123 BLF_size(id, 100.0f);
124 BLF_character_weight(id, 300);
125 const float width_thin = BLF_width(id, sample, sizeof(sample));
126 BLF_character_weight(id, 600);
127 const float width_wide = BLF_width(id, sample, sizeof(sample));
128 EXPECT_TRUE(width_wide > width_thin);
129 close_font(id);
130}
131
133
135{
136 const int id = open_font("Ahem.ttf");
137 BLF_size(id, 100.0f);
138 const int width = BLF_width_max(id);
139 EXPECT_TRUE(width == 100.0f);
140 close_font(id);
141}
142
144{
145 const int id = open_font("Ahem.ttf");
146 BLF_size(id, 100.0f);
147 const int height = BLF_height_max(id);
148 EXPECT_TRUE(height == 100.0f);
149 close_font(id);
150}
151
153{
154 const int id = open_font("Ahem.ttf");
155 BLF_size(id, 100.0f);
156 const int descender = BLF_descender(id);
157 EXPECT_TRUE(descender == -20);
158 close_font(id);
159}
160
162{
163 const int id = open_font("Ahem.ttf");
164 BLF_size(id, 100.0f);
165 const int ascender = BLF_ascender(id);
166 EXPECT_TRUE(ascender == 80);
167 close_font(id);
168}
169
171{
172 /* Ahem does not have all the characters needed for calculation. */
173 const int id = open_font("Roboto.ttf");
174 BLF_size(id, 100.0f);
175 const float width = BLF_fixed_width(id);
176 EXPECT_TRUE(width == 56.0f);
177 close_font(id);
178}
179
181{
182 /* In this test font, 'X' is exactly one em wide. */
183 const char sample[] = "XX";
184 int id = open_font("Ahem.ttf");
185 BLF_size(id, 100.0f);
186 const float width = BLF_width(id, sample, sizeof(sample));
187 EXPECT_TRUE(width == 200.0f);
188 close_font(id);
189}
190
192{
193 /* In this test font, 'X' is exactly one em high. */
194 const char sample[] = "X";
195 int id = open_font("Ahem.ttf");
196 BLF_size(id, 100.0f);
197 const float width = BLF_height(id, sample, sizeof(sample));
198 EXPECT_TRUE(width == 100.0f);
199 close_font(id);
200}
201
203{
204 /* In this test font, 'X' has advance of exactly one em. */
205 const char sample[] = "X";
206 int id = open_font("Ahem.ttf");
207 BLF_size(id, 100.0f);
208 const float width = BLF_glyph_advance(id, sample);
209 EXPECT_NEAR(width, 100.0f, 0.000001f);
210 close_font(id);
211}
212
214
215TEST_F(BlfWrappingMinimalTest, wrap_overflow_ascii)
216{
217 /* Do not break, even though over the wrap limit. */
218 const char sample[] =
219 "xxxxxxxxxxxxxxxxxxx!\"#$%&\'()*+,-./"
220 "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
221 /* Ahem does not contain all the characters included in above string. */
222 int id = open_font("Roboto.ttf");
223 BLF_size(id, 10.0f);
224 const float width = BLF_width(id, sample, sizeof(sample));
225 Vector<StringRef> wrapped = BLF_string_wrap(id, sample, int(float(width) * 0.05f));
226 EXPECT_TRUE(wrapped.size() == 1);
227 close_font(id);
228}
229
231{
232 /* Must break at the center spaces into two, one space trailing, one leading. */
233 const char sample[] = "x xxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxx ";
234 int id = open_font("Ahem.ttf");
235 BLF_size(id, 10.0f);
236 const float width = BLF_width(id, sample, sizeof(sample));
237 Vector<StringRef> wrapped = BLF_string_wrap(id, sample, int(float(width) * 0.7f));
238 EXPECT_TRUE(wrapped.size() == 2 && wrapped[0].back() == ' ' && wrapped[1].substr(0, 1) != " ");
239 close_font(id);
240}
241
243{
244 /* Must break on every line feed except at end of string. */
245 const char sample[] = "x\nxxxxxxxxxxxxxxxx\n\nxxxxxxxxxxxxxxxxxxx\n";
246 int id = open_font("Ahem.ttf");
247 BLF_size(id, 10.0f);
248 const float width = BLF_width(id, sample, sizeof(sample));
249 Vector<StringRef> wrapped = BLF_string_wrap(id, sample, int(float(width) * 0.7f));
250 EXPECT_TRUE(wrapped.size() == 4 && wrapped[2].is_empty());
251 close_font(id);
252}
253
255{
256 /* Must break at limit. */
257 const char sample[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
258 int id = open_font("Ahem.ttf");
259 BLF_size(id, 10.0f);
260 const float width = BLF_width(id, sample, sizeof(sample));
262 id, sample, int(float(width) * 0.7f), BLFWrapMode::HardLimit);
263 EXPECT_TRUE(wrapped.size() == 2);
264 close_font(id);
265}
266
268
269TEST_F(BlfWrappingPathTest, wrap_path_overflow_ascii)
270{
271 /* Do not break, even though over the wrap limit. */
272 const char sample[] =
273 "xxxxxxxxxxxxxxxxxxx!\"#$\'()*+,"
274 "0123456789:;<>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^`abcdefghijklmnopqrstuvwxyz{|}~";
275 /* Ahem does not contain all the characters included in above string. */
276 int id = open_font("Roboto.ttf");
277 BLF_size(id, 10.0f);
278 const float width = BLF_width(id, sample, sizeof(sample));
280 id, sample, int(float(width) * 0.05f), BLFWrapMode::Path);
281 EXPECT_TRUE(wrapped.size() == 1);
282 close_font(id);
283}
284
286{
287 /* Must break at the center space. */
288 const char sample[] = "x xxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxx ";
289 int id = open_font("Ahem.ttf");
290 BLF_size(id, 10.0f);
291 const float width = BLF_width(id, sample, sizeof(sample));
293 id, sample, int(float(width) * 0.7f), BLFWrapMode::Path);
294 EXPECT_TRUE(wrapped.size() == 2);
295 close_font(id);
296}
297
298TEST_F(BlfWrappingPathTest, wrap_path_separators_underscore)
299{
300 /* Must break at middle underscore. */
301 int id = open_font("Roboto.ttf");
302 /* Ahem does not contain all the characters included in tested string. */
303 BLF_size(id, 10.0f);
304 const char sample[] = "x_xx_xxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxx_";
305 float width = BLF_width(id, sample, sizeof(sample));
307 id, sample, int(float(width) * 0.7f), BLFWrapMode::Path);
308 EXPECT_TRUE(wrapped.size() == 2);
309 close_font(id);
310}
311
312TEST_F(BlfWrappingPathTest, wrap_path_separators_slash)
313{
314 /* Wrap at backslash path separators. */
315 int id = open_font("Roboto.ttf");
316 /* Ahem does not contain all the characters included in tested string. */
317 BLF_size(id, 10.0f);
318 const char sample[] = "xxxxxxxxxx" SEP_STR "xxxxxxxxxxxxxxxxxxxxxx";
319 float width = BLF_width(id, sample, sizeof(sample));
321 id, sample, int(float(width) * 0.7f), BLFWrapMode::Path);
322 EXPECT_TRUE(wrapped.size() == 2);
323 close_font(id);
324}
325
326TEST_F(BlfWrappingPathTest, wrap_path_hardlimit)
327{
328 /* Must break at limit. */
329 const char sample[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
330 int id = open_font("Ahem.ttf");
331 BLF_size(id, 10.0f);
332 const float width = BLF_width(id, sample, sizeof(sample));
334 id, sample, int(float(width) * 0.7f), BLFWrapMode::Path | BLFWrapMode::HardLimit);
335 EXPECT_TRUE(wrapped.size() == 2);
336 close_font(id);
337}
338
340
341TEST_F(BlfWrappingTypographicalTest, wrap_typographical_thinspace)
342{
343 /* Must break at thinspace. */
344 const char sample[] = "xxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
345 int id = open_font("Roboto.ttf");
346 BLF_size(id, 10.0f);
347 const float width = BLF_width(id, sample, sizeof(sample));
349 id, sample, int(float(width) * 0.7f), BLFWrapMode::Typographical);
350 EXPECT_TRUE(wrapped.size() == 2);
351 close_font(id);
352}
353
354TEST_F(BlfWrappingTypographicalTest, wrap_typographical_backslash)
355{
356 /* Optional break at any backslash. */
357 const char sample[] = "xxxxxxxxxxx\\xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
358 int id = open_font("Roboto.ttf");
359 BLF_size(id, 10.0f);
360 const float width = BLF_width(id, sample, sizeof(sample));
362 id, sample, int(float(width) * 0.7f), BLFWrapMode::Typographical);
363 EXPECT_TRUE(wrapped.size() == 2);
364 close_font(id);
365}
366
367TEST_F(BlfWrappingTypographicalTest, wrap_typographical_underscore)
368{
369 /* Optional break at any underscore. */
370 const char sample[] = "xxxxxxxxxxx_xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
371 int id = open_font("Roboto.ttf");
372 BLF_size(id, 10.0f);
373 const float width = BLF_width(id, sample, sizeof(sample));
375 id, sample, int(float(width) * 0.7f), BLFWrapMode::Typographical);
376 EXPECT_TRUE(wrapped.size() == 2);
377 close_font(id);
378}
379
380TEST_F(BlfWrappingTypographicalTest, wrap_typographical_forward_slash)
381{
382 /* Do not break on solidus if previous is a number. */
383 const char sample[] = "xxxxxxxxxxxxxx/xx3/xxxxxxxxxxxxxxxxxxxxx";
384 int id = open_font("Roboto.ttf");
385 BLF_size(id, 10.0f);
386 const float width = BLF_width(id, sample, sizeof(sample));
388 id, sample, int(float(width) * 0.7f), BLFWrapMode::Typographical);
389 EXPECT_TRUE(wrapped.size() == 2 && wrapped[0].back() != '3');
390 close_font(id);
391}
392
393TEST_F(BlfWrappingTypographicalTest, wrap_typographical_dash)
394{
395 /* Do not break on dash, hyphen, em dash if previous is space. */
396 const char sample[] = "xxxxxxxxxxxxxx-xx /xxxxxxxxxxxxxxxxxxxxx";
397 int id = open_font("Roboto.ttf");
398 BLF_size(id, 10.0f);
399 const float width = BLF_width(id, sample, sizeof(sample));
401 id, sample, int(float(width) * 0.7f), BLFWrapMode::Typographical);
402 EXPECT_TRUE(wrapped.size() == 2 && wrapped[0].back() != ' ');
403 close_font(id);
404}
405
406TEST_F(BlfWrappingTypographicalTest, wrap_typographical_CJK)
407{
408 /* Do not break on dash, hyphen, em dash if previous is space. */
409 const char sample[] =
410 "xxxxxxxxxxxxxx"
411 "断"
412 "xxxxxxxxxxxxxxxxxxxxx";
413 int id = open_font("Roboto.ttf");
414 BLF_size(id, 10.0f);
415 const float width = BLF_width(id, sample, sizeof(sample));
417 id, sample, int(float(width) * 0.7f), BLFWrapMode::Typographical);
418 EXPECT_TRUE(wrapped.size() == 2);
419 close_font(id);
420}
421
422TEST_F(BlfWrappingTypographicalTest, wrap_typographical_Tibetan)
423{
424 /* Do not break on dash, hyphen, em dash if previous is space. */
425 const char sample[] =
426 "xxxxxxxxxxxxxx"
427 "།"
428 "xxxxxxxxxxxxxxxxxxxxx";
429 int id = open_font("Roboto.ttf");
430 BLF_size(id, 10.0f);
431 const float width = BLF_width(id, sample, sizeof(sample));
433 id, sample, int(float(width) * 0.7f), BLFWrapMode::Typographical);
434 EXPECT_TRUE(wrapped.size() == 2);
435 close_font(id);
436}
437
438TEST_F(BlfWrappingTypographicalTest, wrap_typographical_hardlimit)
439{
440 /* Must break at limit. */
441 const char sample[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
442 int id = open_font("Ahem.ttf");
443 BLF_size(id, 10.0f);
444 const float width = BLF_width(id, sample, sizeof(sample));
446 id, sample, int(float(width) * 0.7f), BLFWrapMode::Typographical | BLFWrapMode::HardLimit);
447 EXPECT_TRUE(wrapped.size() == 2);
448 close_font(id);
449}
450
451TEST_F(BlfWrappingMinimalTest, wrap_hardlimit_too_narrow_width)
452{
453 /* Must break for each character. */
454 const char sample[] = "aeiouáéíóú ;'/./\n\n1234567890-=";
455 int id = open_font("Ahem.ttf");
456 const Vector<StringRef> expected_wrap = {
457 "a", "e", "i", "o", "u", "á", "é", "í", "ó", "ú", " ", ";", "\'", "/", ".",
458 "/", "", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=",
459 };
460 for (float size : {0.1f, 1.0f, 5.0f, 10.0f}) {
461 BLF_size(id, size);
463 EXPECT_EQ(wrapped, expected_wrap);
464 }
465 close_font(id);
466}
467
468} // namespace blender::tests
#define FILE_MAX
#define BLI_path_join(...)
#define STREQ(a, b)
int64_t size() const
bool is_empty() const
float3x3 scale(float3x3 mat, float2 scale)
#define EXPECT_TRUE(result)
#define EXPECT_NEAR(result, expect, threshold)
static std::string font_path(std::string font_name)
Definition BLF_tests.cc:15
TEST_F(BlfLoadTest, load)
Definition BLF_tests.cc:37
static void close_font(int id)
Definition BLF_tests.cc:29
static int open_font(std::string font_name)
Definition BLF_tests.cc:23
bool BLF_is_loaded_id(int fontid) ATTR_WARN_UNUSED_RESULT
Definition blf.cc:172
int BLF_glyph_advance(int fontid, const char *str)
Definition blf.cc:859
float BLF_width(int fontid, const char *str, size_t str_len, ResultBLF *r_info=nullptr) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2)
Definition blf.cc:835
int BLF_width_max(int fontid) ATTR_WARN_UNUSED_RESULT
Definition blf.cc:894
bool BLF_get_vfont_metrics(int fontid, float *ascend_ratio, float *em_ratio, float *scale)
Definition blf.cc:1145
int bool BLF_is_loaded(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition blf.cc:162
void BLF_character_weight(int fontid, int weight)
Definition blf.cc:349
void BLF_size(int fontid, float size)
Definition blf.cc:445
int BLF_load(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition blf.cc:177
float BLF_height(int fontid, const char *str, size_t str_len, ResultBLF *r_info=nullptr) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2)
Definition blf.cc:870
int BLF_default_weight(int fontid) ATTR_WARN_UNUSED_RESULT
Definition blf.cc:357
Vector< StringRef > BLF_string_wrap(int fontid, StringRef str, const int max_pixel_width, BLFWrapMode mode=BLFWrapMode::Minimal)
Definition blf.cc:1103
float BLF_fixed_width(int fontid) ATTR_WARN_UNUSED_RESULT
Definition blf.cc:848
bool BLF_has_variable_weight(int fontid) ATTR_WARN_UNUSED_RESULT
Definition blf.cc:366
bool BLF_has_glyph(int fontid, unsigned int unicode) ATTR_WARN_UNUSED_RESULT
Definition blf.cc:153
int BLF_ascender(int fontid) ATTR_WARN_UNUSED_RESULT
Definition blf.cc:916
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
int BLF_height_max(int fontid) ATTR_WARN_UNUSED_RESULT
Definition blf.cc:883
int BLF_init()
Definition blf.cc:63
char * BLF_display_name_from_file(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition blf.cc:1115
int BLF_descender(int fontid) ATTR_WARN_UNUSED_RESULT
Definition blf.cc:905
char * BLF_display_name_from_id(int fontid)
Definition blf.cc:1135
bool BLF_unload_id(int fontid)
Definition blf.cc:282
void BLF_exit()
Definition blf.cc:72
#define SEP_STR
Definition unit.cc:41