1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2009 Blender Foundation.
* All rights reserved.
*/
/** \file
* \ingroup edinterface
*/
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MEM_guardedalloc.h"
#include "DNA_userdef_types.h"
#include "BLI_listbase.h"
#include "BLI_rect.h"
#include "BLI_string.h"
#include "BLI_utildefines.h"
#include "BKE_global.h"
#include "BLF_api.h"
#ifdef WITH_INTERNATIONAL
# include "BLT_translation.h"
#endif
#include "UI_interface.h"
#include "ED_datafiles.h"
#include "interface_intern.h"
#ifdef WIN32
# include "BLI_math_base.h" /* M_PI */
#endif
/* style + theme + layout-engine = UI */
/**
* This is a complete set of layout rules, the 'state' of the Layout
* Engine. Multiple styles are possible, defined via C or Python. Styles
* get a name, and will typically get activated per region type, like
* "Header", or "Listview" or "Toolbar". Properties of Style definitions
* are:
*
* - default column properties, internal spacing, aligning, min/max width
* - button alignment rules (for groups)
* - label placement rules
* - internal labeling or external labeling default
* - default minimum widths for buttons/labels (in amount of characters)
* - font types, styles and relative sizes for Panel titles, labels, etc.
*/
/* ********************************************** */
static uiStyle *ui_style_new(ListBase *styles, const char *name, short uifont_id)
{
uiStyle *style = MEM_callocN(sizeof(uiStyle), "new style");
BLI_addtail(styles, style);
BLI_strncpy(style->name, name, MAX_STYLE_NAME);
style->panelzoom = 1.0; /* unused */
style->paneltitle.uifont_id = uifont_id;
style->paneltitle.points = UI_DEFAULT_TITLE_POINTS;
style->paneltitle.shadow = 3;
style->paneltitle.shadx = 0;
style->paneltitle.shady = -1;
style->paneltitle.shadowalpha = 0.5f;
style->paneltitle.shadowcolor = 0.0f;
style->grouplabel.uifont_id = uifont_id;
style->grouplabel.points = UI_DEFAULT_TITLE_POINTS;
style->grouplabel.shadow = 3;
style->grouplabel.shadx = 0;
style->grouplabel.shady = -1;
style->grouplabel.shadowalpha = 0.5f;
style->grouplabel.shadowcolor = 0.0f;
style->widgetlabel.uifont_id = uifont_id;
style->widgetlabel.points = UI_DEFAULT_TEXT_POINTS;
style->widgetlabel.shadow = 3;
style->widgetlabel.shadx = 0;
style->widgetlabel.shady = -1;
style->widgetlabel.shadowalpha = 0.5f;
style->widgetlabel.shadowcolor = 0.0f;
style->widget.uifont_id = uifont_id;
style->widget.points = UI_DEFAULT_TEXT_POINTS;
style->widget.shadow = 1;
style->widget.shady = -1;
style->widget.shadowalpha = 0.5f;
style->widget.shadowcolor = 0.0f;
style->columnspace = 8;
style->templatespace = 5;
style->boxspace = 5;
style->buttonspacex = 8;
style->buttonspacey = 2;
style->panelspace = 8;
style->panelouter = 4;
return style;
}
static uiFont *uifont_to_blfont(int id)
{
uiFont *font = U.uifonts.first;
for (; font; font = font->next) {
if (font->uifont_id == id) {
return font;
}
}
return U.uifonts.first;
}
/* *************** draw ************************ */
void UI_fontstyle_draw_ex(const uiFontStyle *fs,
const rcti *rect,
const char *str,
const uchar col[4],
const struct uiFontStyleDraw_Params *fs_params,
size_t len,
int *r_xofs,
int *r_yofs,
struct ResultBLF *r_info)
{
int xofs = 0, yofs;
int font_flag = BLF_CLIPPING;
UI_fontstyle_set(fs);
/* set the flag */
if (fs->shadow) {
font_flag |= BLF_SHADOW;
const float shadow_color[4] = {
fs->shadowcolor, fs->shadowcolor, fs->shadowcolor, fs->shadowalpha};
BLF_shadow(fs->uifont_id, fs->shadow, shadow_color);
BLF_shadow_offset(fs->uifont_id, fs->shadx, fs->shady);
}
if (fs_params->word_wrap == 1) {
font_flag |= BLF_WORD_WRAP;
}
if (fs->bold) {
font_flag |= BLF_BOLD;
}
if (fs->italic) {
font_flag |= BLF_ITALIC;
}
BLF_enable(fs->uifont_id, font_flag);
if (fs_params->word_wrap == 1) {
/* draw from boundbox top */
yofs = BLI_rcti_size_y(rect) - BLF_height_max(fs->uifont_id);
}
else {
/* draw from boundbox center */
const float height = BLF_ascender(fs->uifont_id) + BLF_descender(fs->uifont_id);
yofs = ceil(0.5f * (BLI_rcti_size_y(rect) - height));
}
if (fs_params->align == UI_STYLE_TEXT_CENTER) {
xofs = floor(0.5f * (BLI_rcti_size_x(rect) - BLF_width(fs->uifont_id, str, len)));
}
else if (fs_params->align == UI_STYLE_TEXT_RIGHT) {
xofs = BLI_rcti_size_x(rect) - BLF_width(fs->uifont_id, str, len);
}
yofs = MAX2(0, yofs);
xofs = MAX2(0, xofs);
BLF_clipping(fs->uifont_id, rect->xmin, rect->ymin, rect->xmax, rect->ymax);
BLF_position(fs->uifont_id, rect->xmin + xofs, rect->ymin + yofs, 0.0f);
BLF_color4ubv(fs->uifont_id, col);
BLF_draw_ex(fs->uifont_id, str, len, r_info);
BLF_disable(fs->uifont_id, font_flag);
if (r_xofs) {
*r_xofs = xofs;
}
if (r_yofs) {
*r_yofs = yofs;
}
}
void UI_fontstyle_draw(const uiFontStyle *fs,
const rcti *rect,
const char *str,
const uchar col[4],
const struct uiFontStyleDraw_Params *fs_params)
{
int xofs, yofs;
UI_fontstyle_draw_ex(fs, rect, str, col, fs_params, BLF_DRAW_STR_DUMMY_MAX, &xofs, &yofs, NULL);
}
/* drawn same as above, but at 90 degree angle */
void UI_fontstyle_draw_rotated(const uiFontStyle *fs,
const rcti *rect,
const char *str,
const uchar col[4])
{
float height;
int xofs, yofs;
float angle;
rcti txtrect;
UI_fontstyle_set(fs);
height = BLF_ascender(fs->uifont_id) + BLF_descender(fs->uifont_id);
/* becomes x-offset when rotated */
xofs = ceil(0.5f * (BLI_rcti_size_y(rect) - height));
/* ignore UI_STYLE, always aligned to top */
/* Rotate counter-clockwise for now (assumes left-to-right language). */
xofs += height;
yofs = BLF_width(fs->uifont_id, str, BLF_DRAW_STR_DUMMY_MAX) + 5;
angle = M_PI_2;
/* translate rect to vertical */
txtrect.xmin = rect->xmin - BLI_rcti_size_y(rect);
txtrect.ymin = rect->ymin - BLI_rcti_size_x(rect);
txtrect.xmax = rect->xmin;
txtrect.ymax = rect->ymin;
/* clip is very strict, so we give it some space */
/* clipping is done without rotation, so make rect big enough to contain both positions */
BLF_clipping(fs->uifont_id,
txtrect.xmin - 1,
txtrect.ymin - yofs - xofs - 4,
rect->xmax + 1,
rect->ymax + 4);
BLF_enable(fs->uifont_id, BLF_CLIPPING);
BLF_position(fs->uifont_id, txtrect.xmin + xofs, txtrect.ymax - yofs, 0.0f);
BLF_enable(fs->uifont_id, BLF_ROTATION);
BLF_rotation(fs->uifont_id, angle);
BLF_color4ubv(fs->uifont_id, col);
if (fs->shadow) {
BLF_enable(fs->uifont_id, BLF_SHADOW);
const float shadow_color[4] = {
fs->shadowcolor, fs->shadowcolor, fs->shadowcolor, fs->shadowalpha};
BLF_shadow(fs->uifont_id, fs->shadow, shadow_color);
BLF_shadow_offset(fs->uifont_id, fs->shadx, fs->shady);
}
BLF_draw(fs->uifont_id, str, BLF_DRAW_STR_DUMMY_MAX);
BLF_disable(fs->uifont_id, BLF_ROTATION);
BLF_disable(fs->uifont_id, BLF_CLIPPING);
if (fs->shadow) {
BLF_disable(fs->uifont_id, BLF_SHADOW);
}
}
/**
* Similar to #UI_fontstyle_draw
* but ignore alignment, shadow & no clipping rect.
*
* For drawing on-screen labels.
*/
void UI_fontstyle_draw_simple(
const uiFontStyle *fs, float x, float y, const char *str, const uchar col[4])
{
UI_fontstyle_set(fs);
BLF_position(fs->uifont_id, x, y, 0.0f);
BLF_color4ubv(fs->uifont_id, col);
BLF_draw(fs->uifont_id, str, BLF_DRAW_STR_DUMMY_MAX);
}
/**
* Same as #UI_fontstyle_draw but draw a colored backdrop.
*/
void UI_fontstyle_draw_simple_backdrop(const uiFontStyle *fs,
float x,
float y,
const char *str,
const float col_fg[4],
const float col_bg[4])
{
UI_fontstyle_set(fs);
{
const float width = BLF_width(fs->uifont_id, str, BLF_DRAW_STR_DUMMY_MAX);
const float height = BLF_height_max(fs->uifont_id);
const float decent = BLF_descender(fs->uifont_id);
const float margin = height / 4.0f;
UI_draw_roundbox_corner_set(UI_CNR_ALL);
UI_draw_roundbox_4fv(
&(const rctf){
.xmin = x - margin,
.xmax = x + width + margin,
.ymin = (y + decent) - margin,
.ymax = (y + decent) + height + margin,
},
true,
margin,
col_bg);
}
BLF_position(fs->uifont_id, x, y, 0.0f);
BLF_color4fv(fs->uifont_id, col_fg);
BLF_draw(fs->uifont_id, str, BLF_DRAW_STR_DUMMY_MAX);
}
/* ************** helpers ************************ */
/* XXX: read a style configure */
const uiStyle *UI_style_get(void)
{
#if 0
uiStyle *style = NULL;
/* offset is two struct uiStyle pointers */
style = BLI_findstring(&U.uistyles, "Unifont Style", sizeof(style) * 2);
return (style != NULL) ? style : U.uistyles.first;
#else
return U.uistyles.first;
#endif
}
/* for drawing, scaled with DPI setting */
const uiStyle *UI_style_get_dpi(void)
{
const uiStyle *style = UI_style_get();
static uiStyle _style;
_style = *style;
_style.paneltitle.shadx = (short)(UI_DPI_FAC * _style.paneltitle.shadx);
_style.paneltitle.shady = (short)(UI_DPI_FAC * _style.paneltitle.shady);
_style.grouplabel.shadx = (short)(UI_DPI_FAC * _style.grouplabel.shadx);
_style.grouplabel.shady = (short)(UI_DPI_FAC * _style.grouplabel.shady);
_style.widgetlabel.shadx = (short)(UI_DPI_FAC * _style.widgetlabel.shadx);
_style.widgetlabel.shady = (short)(UI_DPI_FAC * _style.widgetlabel.shady);
_style.columnspace = (short)(UI_DPI_FAC * _style.columnspace);
_style.templatespace = (short)(UI_DPI_FAC * _style.templatespace);
_style.boxspace = (short)(UI_DPI_FAC * _style.boxspace);
_style.buttonspacex = (short)(UI_DPI_FAC * _style.buttonspacex);
_style.buttonspacey = (short)(UI_DPI_FAC * _style.buttonspacey);
_style.panelspace = (short)(UI_DPI_FAC * _style.panelspace);
_style.panelouter = (short)(UI_DPI_FAC * _style.panelouter);
return &_style;
}
int UI_fontstyle_string_width(const uiFontStyle *fs, const char *str)
{
UI_fontstyle_set(fs);
return (int)BLF_width(fs->uifont_id, str, BLF_DRAW_STR_DUMMY_MAX);
}
/**
* Return the width of `str` with the spacing & kerning of `fs` with `aspect`
* (representing #uiBlock.aspect) applied.
*
* When calculating text width, the UI layout logic calculate widths without scale,
* only applying scale when drawing. This causes problems for fonts since kerning at
* smaller sizes often makes them wider than a scaled down version of the larger text.
* Resolve this by calculating the text at the on-screen size,
* returning the result scaled back to 1:1. See T92361.
*/
int UI_fontstyle_string_width_with_block_aspect(const uiFontStyle *fs,
const char *str,
const float aspect)
{
uiFontStyle fs_buf;
if (aspect != 1.0f) {
fs_buf = *fs;
ui_fontscale(&fs_buf.points, aspect);
fs = &fs_buf;
}
int width = UI_fontstyle_string_width(fs, str);
if (aspect != 1.0f) {
width = (int)ceilf(width * aspect);
}
return width;
}
int UI_fontstyle_height_max(const uiFontStyle *fs)
{
UI_fontstyle_set(fs);
return BLF_height_max(fs->uifont_id);
}
/* ************** init exit ************************ */
/* called on each startup.blend read */
/* reading without uifont will create one */
void uiStyleInit(void)
{
uiStyle *style = U.uistyles.first;
/* recover from uninitialized dpi */
if (U.dpi == 0) {
U.dpi = 72;
}
CLAMP(U.dpi, 48, 144);
LISTBASE_FOREACH (uiFont *, font, &U.uifonts) {
BLF_unload_id(font->blf_id);
}
if (blf_mono_font != -1) {
BLF_unload_id(blf_mono_font);
blf_mono_font = -1;
}
if (blf_mono_font_render != -1) {
BLF_unload_id(blf_mono_font_render);
blf_mono_font_render = -1;
}
uiFont *font_first = U.uifonts.first;
/* default builtin */
if (font_first == NULL) {
font_first = MEM_callocN(sizeof(uiFont), "ui font");
BLI_addtail(&U.uifonts, font_first);
}
if (U.font_path_ui[0]) {
BLI_strncpy(font_first->filename, U.font_path_ui, sizeof(font_first->filename));
font_first->uifont_id = UIFONT_CUSTOM1;
}
else {
BLI_strncpy(font_first->filename, "default", sizeof(font_first->filename));
font_first->uifont_id = UIFONT_DEFAULT;
}
LISTBASE_FOREACH (uiFont *, font, &U.uifonts) {
const bool unique = false;
if (font->uifont_id == UIFONT_DEFAULT) {
font->blf_id = BLF_load_default(unique);
}
else {
font->blf_id = BLF_load(font->filename);
if (font->blf_id == -1) {
font->blf_id = BLF_load_default(unique);
}
}
BLF_default_set(font->blf_id);
if (font->blf_id == -1) {
if (G.debug & G_DEBUG) {
printf("%s: error, no fonts available\n", __func__);
}
}
else {
/* ? just for speed to initialize?
* Yes, this build the glyph cache and create
* the texture.
*/
BLF_size(font->blf_id, 11 * U.pixelsize, U.dpi);
BLF_size(font->blf_id, 12 * U.pixelsize, U.dpi);
BLF_size(font->blf_id, 14 * U.pixelsize, U.dpi);
}
}
if (style == NULL) {
ui_style_new(&U.uistyles, "Default Style", UIFONT_DEFAULT);
}
/* XXX, this should be moved into a style,
* but for now best only load the monospaced font once. */
BLI_assert(blf_mono_font == -1);
/* Use unique font loading to avoid thread safety issues with mono font
* used for render metadata stamp in threads. */
if (U.font_path_ui_mono[0]) {
blf_mono_font = BLF_load_unique(U.font_path_ui_mono);
}
if (blf_mono_font == -1) {
const bool unique = true;
blf_mono_font = BLF_load_mono_default(unique);
}
BLF_size(blf_mono_font, 12 * U.pixelsize, 72);
/* Set default flags based on UI preferences (not render fonts) */
{
const int flag_disable = (BLF_MONOCHROME | BLF_HINTING_NONE | BLF_HINTING_SLIGHT |
BLF_HINTING_FULL);
int flag_enable = 0;
if (U.text_render & USER_TEXT_HINTING_NONE) {
flag_enable |= BLF_HINTING_NONE;
}
else if (U.text_render & USER_TEXT_HINTING_SLIGHT) {
flag_enable |= BLF_HINTING_SLIGHT;
}
else if (U.text_render & USER_TEXT_HINTING_FULL) {
flag_enable |= BLF_HINTING_FULL;
}
if (U.text_render & USER_TEXT_DISABLE_AA) {
flag_enable |= BLF_MONOCHROME;
}
LISTBASE_FOREACH (uiFont *, font, &U.uifonts) {
if (font->blf_id != -1) {
BLF_disable(font->blf_id, flag_disable);
BLF_enable(font->blf_id, flag_enable);
}
}
if (blf_mono_font != -1) {
BLF_disable(blf_mono_font, flag_disable);
BLF_enable(blf_mono_font, flag_enable);
}
}
/**
* Second for rendering else we get threading problems,
*
* \note This isn't good that the render font depends on the preferences,
* keep for now though, since without this there is no way to display many unicode chars.
*/
if (blf_mono_font_render == -1) {
const bool unique = true;
blf_mono_font_render = BLF_load_mono_default(unique);
}
BLF_size(blf_mono_font_render, 12 * U.pixelsize, 72);
}
void UI_fontstyle_set(const uiFontStyle *fs)
{
uiFont *font = uifont_to_blfont(fs->uifont_id);
BLF_size(font->blf_id, fs->points * U.pixelsize, U.dpi);
}
|