Blender V5.3
vfont_curve.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include <algorithm>
13#include <cmath>
14#include <cstdlib>
15#include <cstring>
16#include <cwctype>
17
18#include "MEM_guardedalloc.h"
19
20#include "BLI_listbase.hh"
21#include "BLI_math_base_safe.hh"
22#include "BLI_math_matrix_c.hh"
23#include "BLI_math_vector.hh"
24#include "BLI_math_vector_c.hh"
25#include "BLI_rect.hh"
26#include "BLI_string_utf8.hh"
27#include "BLI_threads.hh"
28#include "BLI_utildefines.hh"
29
30#include "DNA_curve_types.h"
31#include "DNA_object_types.h"
32#include "DNA_vfont_types.h"
33
34#include "BKE_anim_path.h"
35#include "BKE_curve.hh"
36#include "BKE_object_types.hh"
37#include "BKE_vfont.hh"
38#include "BKE_vfontdata.hh"
39
40namespace blender {
41
48
49/* -------------------------------------------------------------------- */
52
56static void mid_v2v2(float a[2], float b[2])
57{
58 a[0] = b[0] = (a[0] * 0.5) + (b[0] * 0.5f);
59 a[1] = b[1] = (a[1] * 0.5) + (b[1] * 0.5f);
60}
61
62static float vfont_metrics_ascent(const VFontData_Metrics *metrics)
63{
64 return metrics->ascend_ratio * metrics->em_ratio;
65}
66static float vfont_metrics_descent(const VFontData_Metrics *metrics)
67{
68 return metrics->em_ratio - vfont_metrics_ascent(metrics);
69}
70
71static VFont *vfont_from_charinfo(const Curve &cu, const CharInfo *info)
72{
73 switch (int(info->flag & (CU_CHINFO_BOLD | CU_CHINFO_ITALIC))) {
74 case CU_CHINFO_BOLD:
75 return cu.vfontb ? cu.vfontb : cu.vfont;
77 return cu.vfonti ? cu.vfonti : cu.vfont;
79 return cu.vfontbi ? cu.vfontbi : cu.vfont;
80 default:
81 return cu.vfont;
82 }
83}
84
86{
87 if (vfont == nullptr) {
88 return nullptr;
89 }
90
91 /* Lazily initialize the data. */
92 if (!vfont->data) {
93
95
96 if (vfont->data) {
97 /* Check data again, since it might have been already initialized from other thread
98 * (previous check is not accurate or threading,
99 * just prevents unneeded lock if all the data is here for sure). */
101 return vfont->data;
102 }
103
105
107 }
108
109 return vfont->data;
110}
111
118static bool vfont_char_find_no_lock(const VFontData *vfd, char32_t charcode, VChar **r_che)
119{
120 if (VChar **che_p = vfd->characters->lookup_ptr(charcode)) {
121 *r_che = *che_p;
122 return true;
123 }
124 *r_che = nullptr;
125 return false;
126}
127
128static bool vfont_char_find(const VFontData *vfd, char32_t charcode, VChar **r_che)
129{
131 bool che_found = vfont_char_find_no_lock(vfd, charcode, r_che);
133 return che_found;
134}
135
142static VChar *vfont_char_ensure(VFont *vfont, char32_t charcode)
143{
144 VChar *che;
145 if (vfont && vfont->data) {
146 VFontData *vfd = vfont->data;
147 bool che_found = vfont_char_find(vfd, charcode, &che);
148
149 /* The character wasn't in the current curve base so load it. */
150 if (che_found == false) {
152 /* Check it once again, char might have been already load
153 * between previous #BLI_rw_mutex_unlock() and this #BLI_rw_mutex_lock().
154 *
155 * Such a check should not be a bottleneck since it wouldn't
156 * happen often once all the chars are load. */
157 che_found = vfont_char_find_no_lock(vfd, charcode, &che);
158 if (che_found == false) {
159 che = BKE_vfontdata_char_from_freetypefont(vfont, charcode);
160 }
162 }
163 }
164 else {
165 che = nullptr;
166 }
167 return che;
168}
169
171
172/* -------------------------------------------------------------------- */
178
181 const VFontData_Metrics *metrics = nullptr;
182
183 bool initialized = false;
184
186 struct {
188 VChar che[2] = {};
189
191 Nurb nu[2] = {};
193 BezTriple bezt[2][4] = {};
195};
196
201static VChar *vfont_placeholder_ensure(VCharPlaceHolder &che_placeholder, char32_t charcode)
202{
203 const int che_index = (charcode == ' ') ? 0 : 1;
204
205 if (!che_placeholder.initialized) {
206 const VFontData_Metrics *metrics = che_placeholder.metrics;
207
208 const float ascent = vfont_metrics_ascent(metrics);
209
210 const float line_width = 0.05 * metrics->em_ratio;
211
212 /* The rectangle size within the available bounds. */
213 const float2 size_factor = {
214 0.9f,
215 0.9f - (line_width * 2),
216 };
217 const float size_factor_margin_y = ((1.0 - size_factor.x) / 2.0f);
218
219 /* Always initialize all placeholders, only if one is used. */
220 VChar *che;
221
222 /* Space - approximately en width. */
223 che = &che_placeholder.data.che[0];
224 che->width = 0.5f * metrics->em_ratio;
225
226 /* Hollow rectangle - approximately en width. */
227 che = &che_placeholder.data.che[1];
228 che->width = 0.5f * metrics->em_ratio;
229
230 for (int nu_index = 0; nu_index < ARRAY_SIZE(che_placeholder.data.nu); nu_index++) {
231 Nurb *nu = &che_placeholder.data.nu[nu_index];
232 BLI_addtail(&che->nurbsbase, nu);
233
234 /* In this case poly makes more sense, follow the convention for others. */
235 nu->type = CU_BEZIER;
236 nu->resolu = 8;
237
238 nu->bezt = &che_placeholder.data.bezt[nu_index][0];
239 nu->pntsu = 4;
240 nu->pntsv = 1;
241 nu->flagu |= CU_NURB_CYCLIC;
242
243 rctf bounds;
244 bounds.xmin = (che->width * size_factor_margin_y);
245 bounds.xmax = (che->width * 1.0 - size_factor_margin_y);
246 bounds.ymin = 0.0f;
247 bounds.ymax = ascent * size_factor.y;
248
249 if (nu_index == 1) {
250 bounds.xmin += line_width;
251 bounds.xmax -= line_width;
252 bounds.ymin += line_width;
253 bounds.ymax -= line_width;
254 }
255
256 if (nu_index == 0) {
257 ARRAY_SET_ITEMS(nu->bezt[0].vec[1], bounds.xmin, bounds.ymin);
258 ARRAY_SET_ITEMS(nu->bezt[1].vec[1], bounds.xmin, bounds.ymax);
259 ARRAY_SET_ITEMS(nu->bezt[2].vec[1], bounds.xmax, bounds.ymax);
260 ARRAY_SET_ITEMS(nu->bezt[3].vec[1], bounds.xmax, bounds.ymin);
261 }
262 else {
263 /* Holes are meant to use reverse winding, while not essential for Blender.
264 * Do this for the sake of correctness. */
265 ARRAY_SET_ITEMS(nu->bezt[3].vec[1], bounds.xmin, bounds.ymin);
266 ARRAY_SET_ITEMS(nu->bezt[2].vec[1], bounds.xmin, bounds.ymax);
267 ARRAY_SET_ITEMS(nu->bezt[1].vec[1], bounds.xmax, bounds.ymax);
268 ARRAY_SET_ITEMS(nu->bezt[0].vec[1], bounds.xmax, bounds.ymin);
269 }
270
271 for (int bezt_index = 0; bezt_index < 4; bezt_index++) {
272 BezTriple *bezt = &nu->bezt[bezt_index];
273 bezt->radius = 1.0;
274 bezt->h1 = HD_VECT;
275 bezt->h2 = HD_VECT;
276 }
277 }
278 che_placeholder.initialized = true;
279 }
280 return &che_placeholder.data.che[che_index];
281}
282
289 char32_t charcode,
290 VCharPlaceHolder &che_placeholder)
291{
292 VChar *che = nullptr;
293 if (vfd) {
294 /* While the characters are known to exist, locking is needed
295 * as the hash-table may be added to (and resized) by another thread. */
296 vfont_char_find(vfd, charcode, &che);
297 }
298 if (che == nullptr) [[unlikely]] {
299 che = vfont_placeholder_ensure(che_placeholder, charcode);
300 }
301 return che;
302}
303
305
306/* -------------------------------------------------------------------- */
309
318static Nurb *build_underline(const Curve &cu,
319 ListBaseT<Nurb> *nubase,
320 const rctf *rect,
321 const float yofs,
322 const float rotate,
323 const int charidx,
324 const short mat_nr,
325 const float font_size,
326 Nurb *ul_prev_nu)
327{
328 Nurb *nu;
329 BPoint *bp;
330
331 nu = MEM_new<Nurb>("underline_nurb");
332 nu->resolu = cu.resolu;
333 nu->bezt = nullptr;
334 nu->knotsu = nu->knotsv = nullptr;
335 nu->charidx = charidx + 1000;
336 if (mat_nr >= 0) {
337 nu->mat_nr = mat_nr;
338 }
339 nu->pntsu = 4;
340 nu->pntsv = 1;
341 nu->orderu = 4;
342 nu->orderv = 1;
343 nu->flagu = CU_NURB_CYCLIC;
344
345 bp = MEM_new_array_zeroed<BPoint>(4, "underline_bp");
346
347 copy_v4_fl4(bp[0].vec, rect->xmin, (rect->ymax + yofs), 0.0f, 1.0f);
348 copy_v4_fl4(bp[1].vec, rect->xmax, (rect->ymax + yofs), 0.0f, 1.0f);
349 copy_v4_fl4(bp[2].vec, rect->xmax, (rect->ymin + yofs), 0.0f, 1.0f);
350 copy_v4_fl4(bp[3].vec, rect->xmin, (rect->ymin + yofs), 0.0f, 1.0f);
351
352 /* Used by curve extrusion. */
353 bp[0].radius = bp[1].radius = bp[2].radius = bp[3].radius = 1.0f;
354
355 nu->bp = bp;
356 BLI_addtail(nubase, nu);
357
358 if (rotate != 0.0f) {
359 float si = sinf(rotate);
360 float co = cosf(rotate);
361
362 for (int i = nu->pntsu; i > 0; i--) {
363 float *fp = bp->vec;
364
365 float x = fp[0] - rect->xmin;
366 float y = fp[1] - rect->ymin;
367
368 fp[0] = (+co * x + si * y) + rect->xmin;
369 fp[1] = (-si * x + co * y) + rect->ymin;
370
371 bp++;
372 }
373
374 bp = nu->bp;
375 }
376
377 mul_v2_fl(bp[0].vec, font_size);
378 mul_v2_fl(bp[1].vec, font_size);
379 mul_v2_fl(bp[2].vec, font_size);
380 mul_v2_fl(bp[3].vec, font_size);
381
382 if (ul_prev_nu) {
383 /* Weld locations with the previous, adjacent underline. */
384 BPoint *bp_prev = ul_prev_nu->bp;
385 mid_v2v2(bp_prev[1].vec, bp[0].vec); /* Lower line. */
386 mid_v2v2(bp_prev[2].vec, bp[3].vec); /* Upper line. */
387 }
388
389 return nu;
390}
391
392static void vfont_char_build_impl(const Curve &cu,
393 ListBaseT<Nurb> *nubase,
394 const VChar *che,
395 const CharInfo *info,
396 const bool is_smallcaps,
397 const float2 &offset,
398 const float rotate,
399 const int charidx,
400 const float fsize)
401{
402 /* Make a copy at distance `offset` with shear. */
403 float shear = cu.shear;
404 float si = sinf(rotate);
405 float co = cosf(rotate);
406
407 /* Select the glyph data */
408 const Nurb *nu_from_vchar = nullptr;
409 if (che) {
410 nu_from_vchar = che->nurbsbase.first();
411 }
412
413 /* Create the character. */
414 while (nu_from_vchar) {
415 const BezTriple *bezt_from_vchar = nu_from_vchar->bezt;
416 if (bezt_from_vchar) {
417 Nurb *nu = MEM_new<Nurb>("duplichar_nurb");
418 if (nu == nullptr) {
419 break;
420 }
421 *nu = dna::shallow_copy(*nu_from_vchar);
422 nu->resolu = cu.resolu;
423 nu->bp = nullptr;
424 nu->knotsu = nu->knotsv = nullptr;
425 nu->flag = CU_SMOOTH;
426 nu->charidx = charidx;
427 if (info->mat_nr > 0) {
428 nu->mat_nr = info->mat_nr;
429 }
430 else {
431 nu->mat_nr = 0;
432 }
433 int u = nu->pntsu;
434
435 BezTriple *bezt = MEM_new_array_uninitialized<BezTriple>(size_t(u), "duplichar_bezt2");
436 if (bezt == nullptr) {
437 MEM_delete(nu);
438 break;
439 }
440 memcpy(bezt, bezt_from_vchar, u * sizeof(BezTriple));
441 nu->bezt = bezt;
442
443 if (shear != 0.0f) {
444 bezt = nu->bezt;
445
446 for (int i = nu->pntsu; i > 0; i--) {
447 bezt->vec[0][0] += shear * bezt->vec[0][1];
448 bezt->vec[1][0] += shear * bezt->vec[1][1];
449 bezt->vec[2][0] += shear * bezt->vec[2][1];
450 bezt++;
451 }
452 }
453 if (rotate != 0.0f) {
454 bezt = nu->bezt;
455 for (int i = nu->pntsu; i > 0; i--) {
456 float *fp = bezt->vec[0];
457
458 float x = fp[0];
459 fp[0] = co * x + si * fp[1];
460 fp[1] = -si * x + co * fp[1];
461 x = fp[3];
462 fp[3] = co * x + si * fp[4];
463 fp[4] = -si * x + co * fp[4];
464 x = fp[6];
465 fp[6] = co * x + si * fp[7];
466 fp[7] = -si * x + co * fp[7];
467
468 bezt++;
469 }
470 }
471 bezt = nu->bezt;
472
473 if (is_smallcaps) {
474 const float sca = cu.smallcaps_scale;
475 for (int i = nu->pntsu; i > 0; i--) {
476 float *fp = bezt->vec[0];
477 fp[0] *= sca;
478 fp[1] *= sca;
479 fp[3] *= sca;
480 fp[4] *= sca;
481 fp[6] *= sca;
482 fp[7] *= sca;
483 bezt++;
484 }
485 }
486 bezt = nu->bezt;
487
488 for (int i = nu->pntsu; i > 0; i--) {
489 float *fp = bezt->vec[0];
490 fp[0] = (fp[0] + offset.x) * fsize;
491 fp[1] = (fp[1] + offset.y) * fsize;
492 fp[3] = (fp[3] + offset.x) * fsize;
493 fp[4] = (fp[4] + offset.y) * fsize;
494 fp[6] = (fp[6] + offset.x) * fsize;
495 fp[7] = (fp[7] + offset.y) * fsize;
496 bezt++;
497 }
498
499 BLI_addtail(nubase, nu);
500 }
501
502 nu_from_vchar = nu_from_vchar->next;
503 }
504}
505
507 ListBaseT<Nurb> *nubase,
508 uint charcode,
509 const CharInfo *info,
510 const bool is_smallcaps,
511 const float2 &offset,
512 float rotate,
513 int charidx,
514 const float fsize)
515{
517 if (!vfd) {
518 return;
519 }
520 VChar *che = nullptr;
521 /* C0 control characters should not generate geometry. */
522 if (charcode >= 32) {
523 VCharPlaceHolder che_placeholder = {
524 /*metrics*/ &vfd->metrics,
525 };
526 che = vfont_char_find_or_placeholder(vfd, charcode, che_placeholder);
527 }
528 vfont_char_build_impl(cu, nubase, che, info, is_smallcaps, offset, rotate, charidx, fsize);
529}
530
531static float vfont_char_width(const Curve &cu, VChar *che, const bool is_smallcaps)
532{
533 /* The character wasn't found, probably `charcode = 0`, then the width shall be 0 as well. */
534 if (che == nullptr) {
535 return 0.0f;
536 }
537 if (is_smallcaps) {
538 return che->width * cu.smallcaps_scale;
539 }
540
541 return che->width;
542}
543
552static bool vfont_char_is_combining(const char32_t charcode, const float char_width)
553{
554 return (char_width == 0.0f) && (BLI_wcwidth_or_error(charcode) == 0);
555}
556
557static char32_t vfont_char_apply_smallcaps(char32_t charcode, const bool is_smallcaps)
558{
559 if (is_smallcaps) [[unlikely]] {
560 return toupper(charcode);
561 }
562 return charcode;
563}
564
565static void textbox_scale(TextBox *tb_dst, const TextBox *tb_src, float scale)
566{
567 tb_dst->x = tb_src->x * scale;
568 tb_dst->y = tb_src->y * scale;
569 tb_dst->w = tb_src->w * scale;
570 tb_dst->h = tb_src->h * scale;
571}
572
574
575/* -------------------------------------------------------------------- */
584
588 struct {
589 float min;
590 float max;
592 bool ok;
604};
605
607
608/* -------------------------------------------------------------------- */
615
623
624enum {
629};
630
631#define FONT_TO_CURVE_SCALE_ITERATIONS 20
632#define FONT_TO_CURVE_SCALE_THRESHOLD 0.0001f
633
635
636/* -------------------------------------------------------------------- */
639
644
645static void vfont_info_context_init(VFontInfoContext *vfinfo_ctx, const Curve &cu)
646{
647 BLI_assert(!vfinfo_ctx->vfont);
648 BLI_assert(!vfinfo_ctx->vfd);
649
650 vfinfo_ctx->vfont = cu.vfont;
651 vfinfo_ctx->vfd = vfont_data_ensure(vfinfo_ctx->vfont);
652}
653
655 const Curve &cu,
656 const CharInfo *info)
657{
658 VFont *vfont = vfont_from_charinfo(cu, info);
659 if (vfinfo_ctx->vfont != vfont) {
660 vfinfo_ctx->vfont = vfont;
661 vfinfo_ctx->vfd = vfont_data_ensure(vfont);
662 }
663}
664
666
667/* -------------------------------------------------------------------- */
670
686
692 float x_min;
694 float x_max;
699};
700
705static bool vfont_to_curve(Object *ob,
706 const Curve &cu,
707 const eEditFontMode mode,
708 VFontToCurveIter &iter_data,
709 VFontCursor_Params *cursor_params,
710 ListBaseT<Nurb> *r_nubase,
711 const char32_t **r_text,
712 int *r_text_len,
713 bool *r_text_free,
714 CharTrans **r_chartransdata,
715 float *r_font_size_eval)
716{
717 EditFont *ef = cu.editfont;
718 EditFontSelBox *selboxes = nullptr;
719 const CharInfo *info = nullptr, *custrinfo;
720 TextBox tb_scale;
721 VChar *che;
722 CharTrans *chartransdata = nullptr, *ct;
723 TempLineInfo *lineinfo;
724 float linedist;
725 float twidth = 0;
726 int i, slen, j;
727 int curbox;
728 /* These values are only set to the selection range when `selboxes` is non-null. */
729 int selstart = 0, selend = 0;
730 int cnr = 0, lnr = 0, wsnr = 0, wnr = 0;
731 const char32_t *mem = nullptr;
732 bool mem_alloc = false;
733 const float font_size = cu.fsize * iter_data.scale_to_fit;
734 /* Shift down vertically to be 25% below & 75% above baseline (before font scale is applied). */
735 const float font_select_y_offset = 0.25;
736 const bool word_wrap = iter_data.word_wrap;
737 const float2 cu_offset_scale = {
738 safe_divide(cu.xof, font_size),
739 safe_divide(cu.yof, font_size),
740 };
741 int last_line = -1;
742 /* Length of the text disregarding \n breaks. */
743 float current_line_length = 0.0f;
744 float longest_line_length = 0.0f;
745
746 /* Text at the beginning of the last used text-box (use for y-axis alignment).
747 * We over-allocate by one to simplify logic of getting last char. */
748 Array<int> i_textbox_array(cu.totbox + 1, 0);
749
750#define MARGIN_X_MIN (cu_offset_scale.x + tb_scale.x)
751#define MARGIN_Y_MIN (cu_offset_scale.y + tb_scale.y)
752
753 /* NOTE: do calculations including the trailing `\0` of a string
754 * because the cursor can be at that location. */
755
756 BLI_assert(ob == nullptr || ob->type == OB_FONT);
757
758 /* Read-file ensures non-null, must have become null at run-time, this is a bug! */
759 if (!(cu.str && cu.tb && (ef ? ef->textbufinfo : cu.strinfo))) [[unlikely]] {
760 BLI_assert(0);
761 return false;
762 }
763
764 /* Set font data */
765 VFontInfoContext vfinfo_ctx = {nullptr};
766 vfont_info_context_init(&vfinfo_ctx, cu);
767
768 /* This must only be used for calculations which apply to all text,
769 * for character level queries, values from `vfinfo_ctx` must be updated & used.
770 * Note that this can be null. */
771 VFontData_Metrics _vfont_metrics_default_buf;
772 const VFontData_Metrics *metrics;
773 if (vfinfo_ctx.vfd) {
774 metrics = &vfinfo_ctx.vfd->metrics;
775 }
776 else {
777 BKE_vfontdata_metrics_get_defaults(&_vfont_metrics_default_buf);
778 metrics = &_vfont_metrics_default_buf;
779 }
780
781 VCharPlaceHolder che_placeholder = {
782 /*metrics*/ metrics,
783 };
784
785 if (ef) {
786 slen = ef->len;
787 mem = ef->textbuf;
788 custrinfo = ef->textbufinfo;
789 }
790 else {
791 char32_t *mem_tmp;
792 slen = cu.len_char32;
793
794 /* Create unicode string. */
795 mem_tmp = MEM_new_array_uninitialized<char32_t>(size_t(slen) + 1, "convertedmem");
796 if (!mem_tmp) {
797 return false;
798 }
799
800 BLI_str_utf8_as_utf32(mem_tmp, cu.str, slen + 1);
801
802 mem = mem_tmp;
803 mem_alloc = true;
804 custrinfo = cu.strinfo;
805 }
806
807 /* Only manipulate the edit-font if this object is in edit-mode, otherwise it's unnecessary
808 * as well as crashing since manipulating the #EditFont here isn't thread-safe, see: #144970.
809 *
810 * NOTE(@ideasman42): Relying on the objects mode here isn't as fool-proof as I'd like,
811 * however, even in cases where object data is shared between two different objects,
812 * both active in different windows - it's not possible to enter edit on both at the same time.
813 * If problems are found with this method, other checks could be investigated. */
814 if (ef) {
815 if (ob && (ob->mode & OB_MODE_EDIT)) {
816 /* Pass. */
817 }
818 else {
819 /* Other modes manipulate `ef->pos` which must only be done when this object is in edit-mode.
820 * Not when a curve that happens to have edit-mode data is evaluated
821 * (typically a linked duplicate). */
823
824 /* Since all data has been accessed that's needed, set as null since it's
825 * important never to manipulate this data from multiple threads at once. */
826 ef = nullptr;
827 }
828 }
829
830 if (ef != nullptr) {
831 if (ef->selboxes) {
832 MEM_delete(ef->selboxes);
833 }
834
835 if (BKE_vfont_select_get(&cu, &selstart, &selend)) {
836 ef->selboxes_len = (selend - selstart) + 1;
838 }
839 else {
840 ef->selboxes_len = 0;
841 ef->selboxes = nullptr;
842 }
843
844 selboxes = ef->selboxes;
845 }
846
847 /* Calculate the offset and rotation of each char. */
848 ct = chartransdata = MEM_new_array_zeroed<CharTrans>(size_t(slen) + 1, "buildtext");
849
850 /* We assume the worst case: 1 character per line (is freed at end anyway). */
851 lineinfo = MEM_new_array_uninitialized<TempLineInfo>(size_t(slen) * 2 + 1, "lineinfo");
852
853 linedist = cu.linedist;
854
855 curbox = 0;
856 textbox_scale(&tb_scale, &cu.tb[curbox], safe_divide(1.0f, font_size));
857 const bool use_textbox = (tb_scale.w != 0.0f);
858
859 float2 offset{
862 };
863 /* X position of the last base (non-combining) character, for centering combining marks. */
864 float offset_x_base = MARGIN_X_MIN;
865 /* Base character bounds & accumulators for combining mark vertical positioning.
866 * Mirrors the fallback algorithm in BLF (see #blf_glyph_step) and HarfBuzz. */
867 const VChar *che_base = nullptr;
868 float base_ymax_accum = 0.0f;
869 float base_ymin_accum = 0.0f;
870
871 /* `xtrax` is used to implement character "spacing".
872 * Note that this is added (when adding space), and multiplied when subtracting space.
873 *
874 * This may seem strange but is in fact quite logical for variable width fonts:
875 *
876 * - When increasing the space use **addition**:
877 * It makes sense to add the value to keep the "gaps" between the characters even,
878 * otherwise the space between a `W` and an `i` will have a lot of space around the
879 * `W` and very little around the `i`.
880 *
881 * The values are easy to reason about (1em is the font's full height):
882 * - 1.0 is normal (no extra space).
883 * - 2.0 adds 0.5em between each character.
884 * - 3.0 adds 1.0em between each character.
885 * - When decreasing the space use **multiply**:
886 * It makes sense because subtracting space may have little impact on a wide character,
887 * where as for narrow characters, they may overlap completely, or - even move to a
888 * point before the previous character.
889 *
890 * The values are also easy to reason about.
891 * - 0.0 all characters overlap exactly (no advance).
892 * - 0.75 all characters use 75% their usual spacing.
893 * - 1.0 is normal (no change).
894 *
895 * Use the #XTRAX_WITH_CHAR_WIDTH macro to enforce this logic. */
896 const bool xtrax_contract = cu.spacing < 1.0f;
897 const float xtrax = xtrax_contract ? (cu.spacing - 1.0f) : (0.5f * cu.spacing - 0.5f);
898#define XTRAX_WITH_CHAR_WIDTH(twidth) (xtrax_contract ? (xtrax * (twidth)) : xtrax)
899
900 TextBoxBounds_ForCursor *tb_bounds_for_cursor = nullptr;
901 if (cursor_params != nullptr) {
902 if (cu.textoncurve == nullptr && (cu.totbox > 1) && (slen > 0)) {
903 tb_bounds_for_cursor = MEM_new_array<TextBoxBounds_ForCursor>(size_t(cu.totbox),
904 "TextboxBounds_Cursor");
905 for (curbox = 0; curbox < cu.totbox; curbox++) {
906 TextBoxBounds_ForCursor *tb_bounds = &tb_bounds_for_cursor[curbox];
907 tb_bounds->char_index_last = -1;
908 tb_bounds->bounds.xmin = FLT_MAX;
909 tb_bounds->bounds.xmax = -FLT_MAX;
910 tb_bounds->bounds.ymin = FLT_MAX;
911 tb_bounds->bounds.ymax = -FLT_MAX;
912 }
913 }
914 curbox = 0;
915 }
916
917 /* Start assuming the current index is in a space so that initial spaces are ignored. */
918 bool in_space = true;
919
920 i = 0;
921 while (i <= slen) {
922 /* Characters in the list. */
923 info = &custrinfo[i];
924 char32_t charcode = mem[i];
925 if (info->flag & CU_CHINFO_SMALLCAPS) {
926 charcode = towupper(charcode);
927 if (mem[i] != charcode) {
928 BLI_assert(ct == &chartransdata[i]);
929 ct->is_smallcaps = true;
930 }
931 }
932 /* The #vfont_char_apply_smallcaps function can be used from now on. */
933
934 vfont_info_context_update(&vfinfo_ctx, cu, info);
935
936 if (!ELEM(charcode, '\n', '\0')) {
937 che = vfont_char_ensure(vfinfo_ctx.vfont, charcode);
938 if (che == nullptr) {
939 che = vfont_placeholder_ensure(che_placeholder, charcode);
940 }
941 }
942 else {
943 che = nullptr;
944 }
945
946 const bool is_space = ELEM(charcode, ' ', '\n', '\r', '\t');
947 if (is_space && !in_space) {
948 wnr++;
949 }
950 in_space = is_space;
951 ct->wordnr = wnr;
952
953 twidth = vfont_char_width(cu, che, ct->is_smallcaps);
954
955 /* Calculate positions. */
956
957 if ((tb_scale.w != 0.0f) && (ct->do_break == 0)) { /* May need wrapping. */
958 const float x_available = cu_offset_scale.x + tb_scale.w;
959 const float x_used = (offset.x - tb_scale.x) + twidth;
960
961 if (word_wrap == false) {
962 /* When scale to fit is used, don't do any wrapping.
963 *
964 * Floating precision error can cause the text to be slightly larger.
965 * Assert this is a small value as large values indicate incorrect
966 * calculations with scale-to-fit which shouldn't be ignored. See #89241. */
967 if (x_used > x_available) {
968 BLI_assert_msg(compare_ff_relative(x_used, x_available, FLT_EPSILON, 64),
969 "VFontToCurveIter.scale_to_fit not set correctly!");
970 }
971 }
972 else if (x_used > x_available) {
973 // CLOG_WARN(&LOG, "linewidth exceeded: %c%c%c...", mem[i], mem[i+1], mem[i+2]);
974 bool do_break = false;
975 for (j = i; (mem[j] != '\n') && (chartransdata[j].do_break == 0); j--) {
976
977 /* Special case when there are no breaks possible. */
978 if (j == 0) [[unlikely]] {
979 if (i == slen) {
980 /* Use the behavior of zero a height text-box when a break cannot be inserted.
981 *
982 * Typically when a text-box has any height and overflow is set to scale
983 * the text will wrap to fit the width as necessary. When wrapping isn't
984 * possible it's important to use the same code-path as zero-height lines.
985 * Without this exception a single word will not scale-to-fit (see: #95116). */
986 tb_scale.h = 0.0f;
987 }
988 break;
989 }
990
991 if (ELEM(mem[j], ' ', '-')) {
992 ct -= (i - (j - 1));
993 cnr -= (i - (j - 1));
994 if (mem[j] == ' ') {
995 wsnr--;
996 }
997 if (mem[j] == '-') {
998 wsnr++;
999 }
1000 i = j - 1;
1001 offset.x = ct->offset.x;
1002 wnr = ct->wordnr;
1003 BLI_assert(&ct[1] == &chartransdata[i + 1]);
1004 ct[1].do_break = 1;
1005 ct[1].is_wrap = 1;
1006 do_break = true;
1007 break;
1008 }
1009 BLI_assert(chartransdata[j].do_break == 0);
1010 }
1011
1012 if (do_break) {
1013 if (tb_scale.h == 0.0f) {
1014 /* NOTE: If underlined text is truncated away, the extra space is also truncated. */
1015 BLI_assert(&chartransdata[i + 1] == &ct[1]);
1016 ct[1].is_overflow = 1;
1017 }
1018 /* Since a break was added, re-run this loop with `i` at it's new value. */
1019 continue;
1020 }
1021 }
1022 }
1023
1024 if (charcode == '\n' || charcode == 0 || ct->do_break) {
1025 ct->offset = offset;
1026 ct->linenr = lnr;
1027 ct->charnr = cnr;
1028
1029 offset.y -= linedist;
1030
1031 lineinfo[lnr].x_min = (offset.x - XTRAX_WITH_CHAR_WIDTH(twidth)) - tb_scale.x;
1032 lineinfo[lnr].x_max = tb_scale.w;
1033 lineinfo[lnr].char_nr = cnr;
1034 lineinfo[lnr].wspace_nr = wsnr;
1035
1036 if (tb_bounds_for_cursor != nullptr) {
1037 tb_bounds_for_cursor[curbox].char_index_last = i;
1038 }
1039
1040 if ((tb_scale.h != 0.0f) &&
1041 (-(offset.y - tb_scale.y) > (tb_scale.h - linedist) - cu_offset_scale.y))
1042 {
1043 if (cu.totbox > (curbox + 1)) {
1044 curbox++;
1045 i_textbox_array[curbox] = i + 1;
1046
1047 textbox_scale(&tb_scale, &cu.tb[curbox], 1.0f / font_size);
1048
1049 offset.y = MARGIN_Y_MIN;
1050 }
1051 else if (last_line == -1) {
1052 last_line = lnr + 1;
1053 ct->is_overflow = 1;
1054 }
1055 }
1056
1057 current_line_length += offset.x - MARGIN_X_MIN;
1058 if (ct->do_break) {
1059 current_line_length += twidth;
1060 }
1061 else {
1062 longest_line_length = std::max(current_line_length, longest_line_length);
1063 current_line_length = 0.0f;
1064 }
1065
1066 offset.x = MARGIN_X_MIN;
1067 offset_x_base = MARGIN_X_MIN;
1068 che_base = nullptr;
1069 base_ymax_accum = 0.0f;
1070 base_ymin_accum = 0.0f;
1071 lnr++;
1072 cnr = 0;
1073 wsnr = 0;
1074 }
1075 else if (charcode == '\t') { /* Tab character. */
1076 float tabfac;
1077
1078 ct->offset = offset;
1079 ct->linenr = lnr;
1080 ct->charnr = cnr++;
1081
1082 tabfac = (offset.x - MARGIN_X_MIN + 0.01f);
1083 tabfac = 2.0f * ceilf(tabfac / 2.0f);
1084 offset.x = MARGIN_X_MIN + tabfac;
1085 offset_x_base = offset.x;
1086 che_base = nullptr;
1087 base_ymax_accum = 0.0f;
1088 base_ymin_accum = 0.0f;
1089 }
1090 else {
1091 /* Won't have been changed since last assignment, ensure this remains the case. */
1092 BLI_assert(twidth == vfont_char_width(cu, che, ct->is_smallcaps));
1093
1094 if ((che != nullptr) && vfont_char_is_combining(charcode, twidth)) [[unlikely]] {
1095 /* Combining character: center the mark over the previous base character.
1096 * This mirrors the fallback algorithm used by BLF (see #blf_glyph_step)
1097 * and HarfBuzz when GPOS tables are absent. */
1098 const float base_center = (offset_x_base + offset.x) * 0.5f;
1099 ct->offset.x = base_center - BLI_rctf_cent_x(&che->bounds);
1100 ct->offset.y = offset.y;
1101
1102 /* Vertical: reposition above/below marks (follows HarfBuzz `position_mark`). */
1103 if (che_base) {
1104 const float mark_ymin = che->bounds.ymin;
1105 const float mark_ymax = che->bounds.ymax;
1106 const float mark_height = mark_ymax - mark_ymin;
1107 const float base_mid = BLI_rctf_cent_y(&che_base->bounds);
1108 const float mark_mid = (mark_ymin + mark_ymax) * 0.5f;
1109 /* Gap matches HarfBuzz `y_gap = font->y_scale / 16`. */
1110 const float y_gap = metrics->em_ratio / 16.0f;
1111
1112 if (mark_mid > base_mid) {
1113 /* Above mark. */
1114 base_ymax_accum += y_gap;
1115 float offset_y = base_ymax_accum - mark_ymin;
1116 /* Don't shift down "above" marks too much (HarfBuzz dampening). */
1117 if ((y_gap > 0.0f) != (offset_y > 0.0f)) {
1118 const float correction = -offset_y * 0.5f;
1119 base_ymax_accum += correction;
1120 offset_y += correction;
1121 }
1122 base_ymax_accum += mark_height;
1123 ct->offset.y += offset_y;
1124 }
1125 else {
1126 /* Below mark. */
1127 base_ymin_accum -= y_gap;
1128 float offset_y = base_ymin_accum - mark_ymax;
1129 /* Never shift up "below" marks (HarfBuzz dampening). */
1130 if ((y_gap > 0.0f) == (offset_y > 0.0f)) {
1131 base_ymin_accum -= offset_y;
1132 offset_y = 0.0f;
1133 }
1134 base_ymin_accum -= mark_height;
1135 ct->offset.y += offset_y;
1136 }
1137 }
1138
1139 ct->linenr = lnr;
1140 ct->charnr = cnr++;
1141 }
1142 else {
1143 ct->offset = offset;
1144 ct->linenr = lnr;
1145 ct->charnr = cnr++;
1146
1147 EditFontSelBox *sb = nullptr;
1148 if (selboxes && (i >= selstart) && (i <= selend)) {
1149 sb = &selboxes[i - selstart];
1150 sb->y = (offset.y - font_select_y_offset) * font_size - linedist * font_size * 0.1f;
1151 sb->h = linedist * font_size;
1152 sb->w = offset.x * font_size;
1153 }
1154
1155 float wsfac;
1156 if (charcode == ' ') { /* Space character. */
1157 wsfac = cu.wordspace;
1158 wsnr++;
1159 }
1160 else {
1161 wsfac = 1.0f;
1162 }
1163
1164 offset_x_base = offset.x;
1165 che_base = che;
1166 base_ymax_accum = che ? che->bounds.ymax : 0.0f;
1167 base_ymin_accum = che ? che->bounds.ymin : 0.0f;
1168 offset.x += (twidth * wsfac * (1.0f + (info->kern / 40.0f))) +
1169 XTRAX_WITH_CHAR_WIDTH(twidth);
1170
1171 if (sb) {
1172 sb->w = (offset.x * font_size) - sb->w;
1173 }
1174 }
1175 }
1176 ct++;
1177 i++;
1178 }
1179
1180 current_line_length += offset.x + twidth - MARGIN_X_MIN;
1181 longest_line_length = std::max(current_line_length, longest_line_length);
1182
1183 if (ef && selboxes) {
1184 /* Set combined style flags for the selected string. Start with all styles then
1185 * remove one if ANY characters do not have it. Break out if we've removed them all. */
1187 for (int k = selstart; k <= selend && ef->select_char_info_flag; k++) {
1188 info = &custrinfo[k];
1189 ef->select_char_info_flag &= info->flag;
1190 }
1191 }
1192
1193 if (cu.spacemode != CU_ALIGN_X_LEFT) {
1194 ct = chartransdata;
1195
1196 if (cu.spacemode == CU_ALIGN_X_RIGHT) {
1197 TempLineInfo *li;
1198
1199 for (i = 0, li = lineinfo; i < lnr; i++, li++) {
1200 li->x_min = (li->x_max - li->x_min) + cu_offset_scale.x;
1201 }
1202
1203 for (i = 0; i <= slen; i++) {
1204 ct->offset.x += lineinfo[ct->linenr].x_min;
1205 ct++;
1206 }
1207 }
1208 else if (cu.spacemode == CU_ALIGN_X_MIDDLE) {
1209 TempLineInfo *li;
1210
1211 for (i = 0, li = lineinfo; i < lnr; i++, li++) {
1212 li->x_min = ((li->x_max - li->x_min) + cu_offset_scale.x) / 2.0f;
1213 }
1214
1215 for (i = 0; i <= slen; i++) {
1216 ct->offset.x += lineinfo[ct->linenr].x_min;
1217 ct++;
1218 }
1219 }
1220 else if ((cu.spacemode == CU_ALIGN_X_FLUSH) && use_textbox) {
1221 TempLineInfo *li;
1222
1223 for (i = 0, li = lineinfo; i < lnr; i++, li++) {
1224 li->x_min = ((li->x_max - li->x_min) + cu_offset_scale.x);
1225
1226 if (li->char_nr > 1) {
1227 li->x_min /= float(li->char_nr - 1);
1228 }
1229 }
1230 for (i = 0; i <= slen; i++) {
1231 for (j = i; !ELEM(mem[j], '\0', '\n') && (chartransdata[j].do_break == 0) && (j < slen);
1232 j++)
1233 {
1234 /* Pass. */
1235 }
1236
1237 // if ((mem[j] != '\n') && (mem[j])) {
1238 ct->offset.x += ct->charnr * lineinfo[ct->linenr].x_min;
1239 // }
1240 ct++;
1241 }
1242 }
1243 else if ((cu.spacemode == CU_ALIGN_X_JUSTIFY) && use_textbox) {
1244 float curofs = 0.0f;
1245 for (i = 0; i <= slen; i++) {
1246 for (j = i; (mem[j]) && (mem[j] != '\n') && (chartransdata[j].do_break == 0) && (j < slen);
1247 j++)
1248 {
1249 /* Pass. */
1250 }
1251
1252 if ((mem[j] != '\n') && (chartransdata[j].do_break != 0)) {
1253 /* Skip terminator spaces (those that became the wrap break point):
1254 * they were removed from `wspace_nr` so they don't receive slack
1255 * and dividing by `wspace_nr` here would be a divide-by-zero on
1256 * lines whose only space was consumed by the wrap. */
1257 if (mem[i] == ' ' && chartransdata[i].do_break == 0) {
1258 TempLineInfo *li;
1259
1260 li = &lineinfo[ct->linenr];
1261 curofs += ((li->x_max - li->x_min) + cu_offset_scale.x) / float(li->wspace_nr);
1262 }
1263 ct->offset.x += curofs;
1264 }
1265 if (mem[i] == '\n' || chartransdata[i].do_break) {
1266 curofs = 0;
1267 }
1268 ct++;
1269 }
1270 }
1271 }
1272
1273 /* Top-baseline is default, in this case, do nothing. */
1274 if (cu.align_y != CU_ALIGN_Y_TOP_BASELINE) {
1275 if (tb_scale.h != 0.0f) {
1276 /* We need to loop all the text-boxes even the "full" ones.
1277 * This way they all get the same vertical padding. */
1278 for (int tb_index = 0; tb_index < cu.totbox; tb_index++) {
1279 CharTrans *ct_first, *ct_last;
1280 const int i_textbox = i_textbox_array[tb_index];
1281 const int i_textbox_next = i_textbox_array[tb_index + 1];
1282 const bool is_last_filled_textbox = ELEM(i_textbox_next, 0, slen + 1);
1283 int lines;
1284
1285 ct_first = chartransdata + i_textbox;
1286 ct_last = chartransdata + (is_last_filled_textbox ? slen : i_textbox_next - 1);
1287 lines = ct_last->linenr - ct_first->linenr + 1;
1288
1289 if (cu.overflow == CU_OVERFLOW_TRUNCATE) {
1290 /* Ensure overflow doesn't truncate text, before centering vertically
1291 * giving odd/buggy results, see: #66614. */
1292 if ((tb_index == cu.totbox - 1) && (last_line != -1)) {
1293 lines = last_line - ct_first->linenr;
1294 }
1295 }
1296
1297 textbox_scale(&tb_scale, &cu.tb[tb_index], 1.0f / font_size);
1298 /* The initial Y origin of the text-box is hard-coded to 1.0f * text scale. */
1299 const float textbox_y_origin = 1.0f;
1300 float yoff = 0.0f;
1301
1302 switch (cu.align_y) {
1304 break;
1305 case CU_ALIGN_Y_TOP:
1306 yoff = textbox_y_origin - vfont_metrics_ascent(metrics);
1307 break;
1308 case CU_ALIGN_Y_CENTER:
1309 yoff = ((((metrics->em_ratio + (lines - 1) * linedist) * 0.5f) -
1310 vfont_metrics_ascent(metrics)) -
1311 (tb_scale.h * 0.5f) + textbox_y_origin);
1312 break;
1314 yoff = textbox_y_origin + ((lines - 1) * linedist) - tb_scale.h;
1315 break;
1316 case CU_ALIGN_Y_BOTTOM:
1317 yoff = textbox_y_origin + ((lines - 1) * linedist) - tb_scale.h +
1318 vfont_metrics_descent(metrics);
1319 break;
1320 }
1321
1322 for (ct = ct_first; ct <= ct_last; ct++) {
1323 ct->offset.y += yoff;
1324 }
1325
1326 if (is_last_filled_textbox) {
1327 break;
1328 }
1329 }
1330 }
1331 else {
1332 /* Non text-box case handled separately. */
1333 float yoff = 0.0f;
1334
1335 switch (cu.align_y) {
1337 break;
1338 case CU_ALIGN_Y_TOP:
1339 yoff = -vfont_metrics_ascent(metrics);
1340 break;
1341 case CU_ALIGN_Y_CENTER:
1342 yoff = ((metrics->em_ratio + (lnr - 1) * linedist) * 0.5f) -
1343 vfont_metrics_ascent(metrics);
1344 break;
1346 yoff = (lnr - 1) * linedist;
1347 break;
1348 case CU_ALIGN_Y_BOTTOM:
1349 yoff = (lnr - 1) * linedist + vfont_metrics_descent(metrics);
1350 break;
1351 }
1352
1353 ct = chartransdata;
1354 for (i = 0; i <= slen; i++) {
1355 ct->offset.y += yoff;
1356 ct++;
1357 }
1358 }
1359 }
1360 if (tb_bounds_for_cursor != nullptr) {
1361 int char_beg_next = 0;
1362 for (curbox = 0; curbox < cu.totbox; curbox++) {
1363 TextBoxBounds_ForCursor *tb_bounds = &tb_bounds_for_cursor[curbox];
1364 if (tb_bounds->char_index_last == -1) {
1365 continue;
1366 }
1367 const int char_beg = char_beg_next;
1368 const int char_end = tb_bounds->char_index_last;
1369
1370 TempLineInfo *line_beg = &lineinfo[chartransdata[char_beg].linenr];
1371 TempLineInfo *line_end = &lineinfo[chartransdata[char_end].linenr];
1372
1373 int char_idx_offset = char_beg;
1374
1375 rctf *bounds = &tb_bounds->bounds;
1376 /* In a text-box with no curves, `offset.y` only decrements over lines, `ymax` and `ymin`
1377 * can be obtained from any character in the first and last line of the text-box. */
1378 bounds->ymax = chartransdata[char_beg].offset.y;
1379 bounds->ymin = chartransdata[char_end].offset.y;
1380
1381 for (TempLineInfo *line = line_beg; line <= line_end; line++) {
1382 const CharTrans *first_char_line = &chartransdata[char_idx_offset];
1383 const CharTrans *last_char_line = &chartransdata[char_idx_offset + line->char_nr];
1384
1385 bounds->xmin = min_ff(bounds->xmin, first_char_line->offset.x);
1386 bounds->xmax = max_ff(bounds->xmax, last_char_line->offset.x);
1387 char_idx_offset += line->char_nr + 1;
1388 }
1389 /* Move the bounds into a space compatible with `cursor_location`. */
1390 BLI_rctf_mul(bounds, font_size);
1391
1392 char_beg_next = tb_bounds->char_index_last + 1;
1393 }
1394 }
1395
1396 MEM_delete(lineinfo);
1397
1398 /* TEXT ON CURVE */
1399 /* NOTE: Only #OB_CURVES_LEGACY objects could have a path. */
1400 if (cu.textoncurve && cu.textoncurve->type == OB_CURVES_LEGACY) {
1401 BLI_assert(cu.textoncurve->runtime->curve_cache != nullptr);
1402 if (cu.textoncurve->runtime->curve_cache != nullptr &&
1404 {
1405 float distfac, imat[4][4], imat3[3][3], cmat[3][3];
1406 float minx, maxx;
1407 float timeofs, sizefac;
1408
1409 if (ob != nullptr) {
1410 invert_m4_m4(imat, ob->object_to_world().ptr());
1411 }
1412 else {
1413 unit_m4(imat);
1414 }
1415 copy_m3_m4(imat3, imat);
1416
1417 copy_m3_m4(cmat, cu.textoncurve->object_to_world().ptr());
1418 mul_m3_m3m3(cmat, cmat, imat3);
1419 sizefac = normalize_v3(cmat[0]) / font_size;
1420
1421 ct = chartransdata;
1422 minx = maxx = ct->offset.x;
1423 ct++;
1424 for (i = 1; i <= slen; i++, ct++) {
1425 minx = std::min(minx, ct->offset.x);
1426 maxx = std::max(maxx, ct->offset.x);
1427 }
1428
1429 /* We put the x-coordinate exact at the curve, the y is rotated. */
1430
1431 /* Length correction. */
1432 const float chartrans_size_x = maxx - minx;
1433 if (chartrans_size_x != 0.0f) {
1434 const CurveCache *cc = cu.textoncurve->runtime->curve_cache;
1435 const float totdist = BKE_anim_path_get_length(cc);
1436 distfac = (sizefac * totdist) / chartrans_size_x;
1437 distfac = (distfac > 1.0f) ? (1.0f / distfac) : 1.0f;
1438 }
1439 else {
1440 /* Happens when there are no characters, set this value to place the text cursor. */
1441 distfac = 0.0f;
1442 }
1443
1444 timeofs = 0.0f;
1445
1446 if (distfac < 1.0f) {
1447 /* Path longer than text: space-mode is involved. */
1448
1449 if (cu.spacemode == CU_ALIGN_X_RIGHT) {
1450 timeofs = 1.0f - distfac;
1451 }
1452 else if (cu.spacemode == CU_ALIGN_X_MIDDLE) {
1453 timeofs = (1.0f - distfac) / 2.0f;
1454 }
1455 else if (cu.spacemode == CU_ALIGN_X_FLUSH) {
1456 distfac = 1.0f;
1457 }
1458 }
1459
1460 if (chartrans_size_x != 0.0f) {
1461 distfac /= chartrans_size_x;
1462 }
1463
1464 timeofs += distfac * cu.xof; /* Not cyclic. */
1465
1466 ct = chartransdata;
1467 for (i = 0; i <= slen; i++, ct++) {
1468 float ctime, dtime, vec[4], rotvec[3];
1469 float si, co;
1470
1471 /* Rotate around center character. */
1472 info = &custrinfo[i];
1473 BLI_assert(ct == &chartransdata[i]);
1474 const char32_t charcode = vfont_char_apply_smallcaps(mem[i], ct->is_smallcaps);
1475
1476 vfont_info_context_update(&vfinfo_ctx, cu, info);
1477 che = vfont_char_find_or_placeholder(vfinfo_ctx.vfd, charcode, che_placeholder);
1478
1479 twidth = vfont_char_width(cu, che, ct->is_smallcaps);
1480
1481 dtime = distfac * 0.5f * twidth;
1482
1483 ctime = timeofs + distfac * (ct->offset.x - minx);
1484 CLAMP(ctime, 0.0f, 1.0f);
1485
1486 /* Calculate the right loc AND the right rot separately. */
1487 BKE_where_on_path(cu.textoncurve, ctime, vec, nullptr, nullptr, nullptr, nullptr);
1489 cu.textoncurve, ctime + dtime, nullptr, rotvec, nullptr, nullptr, nullptr);
1490
1491 mul_v3_fl(vec, sizefac);
1492
1493 ct->rotate = float(M_PI) - atan2f(rotvec[1], rotvec[0]);
1494
1495 si = sinf(ct->rotate);
1496 co = cosf(ct->rotate);
1497
1498 offset.y = ct->offset.y;
1499
1500 ct->offset = {
1501 vec[0] + si * offset.y,
1502 vec[1] + co * offset.y,
1503 };
1504
1505 if (selboxes && (i >= selstart) && (i <= selend)) {
1506 EditFontSelBox *sb;
1507 sb = &selboxes[i - selstart];
1508 sb->rotate = -ct->rotate;
1509 }
1510 }
1511 }
1512 }
1513
1514 if (selboxes) {
1515 ct = chartransdata;
1516 for (i = 0; i <= selend; i++, ct++) {
1517 if (i >= selstart) {
1518 EditFontSelBox *sb = &selboxes[i - selstart];
1519 sb->x = ct->offset.x;
1520 sb->y = ct->offset.y;
1521 if (ct->rotate != 0.0f) {
1522 sb->x -= sinf(ct->rotate) * font_select_y_offset;
1523 sb->y -= cosf(ct->rotate) * font_select_y_offset;
1524 }
1525 else {
1526 /* Simple downward shift below baseline when not rotated. */
1527 sb->y -= font_select_y_offset;
1528 }
1529 sb->x *= font_size;
1530 sb->y *= font_size;
1531 selboxes[i - selstart].h = font_size;
1532 }
1533 }
1534 }
1535
1537 iter_data.status == VFONT_TO_CURVE_INIT)
1538 {
1539 ct = &chartransdata[ef->pos];
1540
1541 if (ELEM(mode, FO_CURSUP, FO_PAGEUP) && ct->linenr == 0) {
1542 /* Pass. */
1543 }
1544 else if (ELEM(mode, FO_CURSDOWN, FO_PAGEDOWN) && ct->linenr == lnr) {
1545 /* Pass. */
1546 }
1547 else if (mode == FO_LINE_BEGIN) {
1548 /* Line wrap aware line beginning. */
1549 while ((ef->pos > 0) && (chartransdata[ef->pos - 1].linenr == ct->linenr)) {
1550 ef->pos -= 1;
1551 }
1552 }
1553 else if (mode == FO_LINE_END) {
1554 /* Line wrap aware line end. */
1555 while ((ef->pos < slen) && (chartransdata[ef->pos + 1].linenr == ct->linenr)) {
1556 ef->pos += 1;
1557 }
1558 }
1559 else {
1560 switch (mode) {
1561 case FO_CURSUP:
1562 lnr = ct->linenr - 1;
1563 break;
1564 case FO_CURSDOWN:
1565 lnr = ct->linenr + 1;
1566 break;
1567 case FO_PAGEUP:
1568 lnr = ct->linenr - 10;
1569 break;
1570 case FO_PAGEDOWN:
1571 lnr = ct->linenr + 10;
1572 break;
1573 /* Ignored. */
1574 case FO_EDIT:
1575 case FO_CURS:
1576 case FO_DUPLI:
1577 case FO_SELCHANGE:
1578 case FO_LINE_BEGIN:
1579 case FO_LINE_END:
1580 break;
1581 }
1582 cnr = ct->charnr;
1583 /* Seek for char with `lnr` & `cnr`. */
1584 ef->pos = 0;
1585 ct = chartransdata;
1586 for (i = 0; i < slen; i++) {
1587 if (ct->linenr == lnr) {
1588 if ((ct->charnr == cnr) || ((ct + 1)->charnr == 0)) {
1589 break;
1590 }
1591 }
1592 else if (ct->linenr > lnr) {
1593 break;
1594 }
1595 ef->pos++;
1596 ct++;
1597 }
1598 }
1599 }
1600
1601 /* Cursor first. */
1602 if (ef) {
1603 ct = &chartransdata[ef->pos];
1604 const float cursor_width = 0.04f;
1605 const float cursor_half = 0.02f;
1606
1607 /* By default the cursor is exactly between the characters
1608 * and matches the rotation of the character to the right. */
1609 float cursor_left = 0.0f - cursor_half;
1610 float cursor_rotate = ct->rotate;
1611
1612 if (ef->selboxes) {
1613 if (ef->selend >= ef->selstart) {
1614 /* Cursor at right edge of a text selection. Match rotation to the character at the
1615 * end of selection. Cursor is further right to show the selected characters better. */
1616 cursor_rotate = chartransdata[max_ii(0, ef->selend - 1)].rotate;
1617 cursor_left = 0.0f;
1618 }
1619 else {
1620 /* Cursor at the left edge of a text selection. Cursor
1621 * is further left to show the selected characters better. */
1622 cursor_left = 0.0f - cursor_width;
1623 }
1624 }
1625 else if ((ef->pos == ef->len) && (ef->len > 0)) {
1626 /* Nothing selected, but at the end of the string. Match rotation to previous character. */
1627 cursor_rotate = chartransdata[ef->len - 1].rotate;
1628 }
1629
1630 /* We need the rotation to be around the bottom-left corner. So we make
1631 * that the zero point before rotation, rotate, then apply offsets afterward. */
1632
1633 /* Bottom left. */
1634 ef->textcurs[0] = float2(cursor_left, 0.0f - font_select_y_offset);
1635 /* Bottom right. */
1636 ef->textcurs[1] = float2(cursor_left + cursor_width, 0.0f - font_select_y_offset);
1637 /* Top left. */
1638 ef->textcurs[3] = float2(cursor_left, 1.0f - font_select_y_offset);
1639 /* Top right. */
1640 ef->textcurs[2] = float2(cursor_left + cursor_width, 1.0f - font_select_y_offset);
1641
1642 for (int vert = 0; vert < 4; vert++) {
1643 float2 temp_fl;
1644 /* Rotate around the cursor's bottom-left corner. */
1645 rotate_v2_v2fl(temp_fl, &ef->textcurs[vert][0], -cursor_rotate);
1646 ef->textcurs[vert] = font_size * (ct->offset + temp_fl);
1647 }
1648 }
1649
1650 if (mode == FO_SELCHANGE) {
1651 MEM_delete(chartransdata);
1652 chartransdata = nullptr;
1653 }
1654 else if (mode == FO_EDIT) {
1655 /* Make NURBS-data. */
1656 BKE_nurbList_free(r_nubase);
1657
1658 /* Track the previous underline so contiguous underlines can be welded together.
1659 * This is done to prevent overlapping geometry, see: #122540. */
1660 int ul_prev_i = -1;
1661 Nurb *ul_prev_nu = nullptr;
1662
1663 ct = chartransdata;
1664 for (i = 0; i < slen; i++) {
1665
1666 if ((cu.overflow == CU_OVERFLOW_TRUNCATE) && (ob && ob->mode != OB_MODE_EDIT) &&
1667 ct->is_overflow)
1668 {
1669 break;
1670 }
1671
1672 info = &(custrinfo[i]);
1673 const char32_t charcode = vfont_char_apply_smallcaps(mem[i], ct->is_smallcaps);
1674 /* We don't want to see any character for `\n`. */
1675 if (charcode != '\n') {
1676
1677 vfont_info_context_update(&vfinfo_ctx, cu, info);
1678 /* Find the character, the characters has to be in the memory already
1679 * since character checking has been done earlier already. */
1680 che = vfont_char_find_or_placeholder(vfinfo_ctx.vfd, charcode, che_placeholder);
1682 cu, r_nubase, che, info, ct->is_smallcaps, ct->offset, ct->rotate, i, font_size);
1683
1684 if (info->flag & CU_CHINFO_UNDERLINE) {
1685 float ulwidth, uloverlap = 0.0f;
1686 rctf rect;
1687
1688 twidth = vfont_char_width(cu, che, ct->is_smallcaps);
1689
1690 BLI_assert(&ct[1] == &chartransdata[i + 1]);
1691 if ((i < (slen - 1)) && (mem[i + 1] != '\n') &&
1692 ((mem[i + 1] != ' ') || (custrinfo[i + 1].flag & CU_CHINFO_UNDERLINE)) &&
1693 ((ct[1].is_wrap) == 0))
1694 {
1695 uloverlap = XTRAX_WITH_CHAR_WIDTH(twidth);
1696 }
1697 ulwidth = (twidth * (1.0f + (info->kern / 40.0f))) + uloverlap;
1698
1699 rect.xmin = ct->offset.x;
1700 rect.xmax = rect.xmin + ulwidth;
1701
1702 rect.ymin = ct->offset.y;
1703 rect.ymax = rect.ymin - cu.ulheight;
1704
1705 if ((ul_prev_i != -1) &&
1706 /* Skip welding underlines when there are gaps. */
1707 ((ul_prev_i + 1 != i) ||
1708 /* Skip welding on new lines. */
1709 (chartransdata[ul_prev_i].linenr != ct->linenr)))
1710 {
1711 ul_prev_nu = nullptr;
1712 }
1713
1714 ul_prev_nu = build_underline(cu,
1715 r_nubase,
1716 &rect,
1717 cu.ulpos - 0.05f,
1718 ct->rotate,
1719 i,
1720 info->mat_nr,
1721 font_size,
1722 ul_prev_nu);
1723 ul_prev_i = ul_prev_nu ? i : -1;
1724 }
1725 }
1726 ct++;
1727 }
1728 }
1729
1730 if (iter_data.status == VFONT_TO_CURVE_SCALE_ONCE) {
1731 /* That means we were in a final run, just exit. */
1733 iter_data.status = VFONT_TO_CURVE_DONE;
1734 }
1735 else if (cu.overflow == CU_OVERFLOW_NONE) {
1736 /* Pass. */
1737 }
1738 else if ((tb_scale.h == 0.0f) && (tb_scale.w == 0.0f)) {
1739 /* Pass. */
1740 }
1741 else if (cu.overflow == CU_OVERFLOW_SCALE) {
1742 if ((cu.totbox == 1) && ((tb_scale.w == 0.0f) || (tb_scale.h == 0.0f))) {
1743 /* These are special cases, simpler to deal with. */
1744 if (tb_scale.w == 0.0f) {
1745 /* This is a potential vertical overflow.
1746 * Since there is no width limit, all the new lines are from line breaks. */
1747 if ((last_line != -1) && (lnr > last_line)) {
1748 const float total_text_height = lnr * linedist;
1749 iter_data.scale_to_fit = tb_scale.h / total_text_height;
1751 iter_data.word_wrap = false;
1752 }
1753 }
1754 else if (tb_scale.h == 0.0f) {
1755 /* This is a horizontal overflow. */
1756 if (longest_line_length > tb_scale.w) {
1757 /* We make sure longest line before it broke can fit here. */
1758 float scale_to_fit = tb_scale.w / longest_line_length;
1759
1760 iter_data.scale_to_fit = scale_to_fit;
1762 iter_data.word_wrap = false;
1763 }
1764 }
1765 }
1766 else {
1767 /* This is the really complicated case, the best we can do is to iterate over
1768 * this function a few times until we get an acceptable result.
1769 *
1770 * Keep in mind that there is no single number that will make all fit to the end.
1771 * In a way, our ultimate goal is to get the highest scale that still leads to the
1772 * number of extra lines to zero. */
1773 if (iter_data.status == VFONT_TO_CURVE_INIT) {
1774 bool valid = true;
1775
1776 for (int tb_index = 0; tb_index <= curbox; tb_index++) {
1777 TextBox *tb = &cu.tb[tb_index];
1778 if ((tb->w == 0.0f) || (tb->h == 0.0f)) {
1779 valid = false;
1780 break;
1781 }
1782 }
1783
1784 if (valid && (last_line != -1) && (lnr > last_line)) {
1785 const float total_text_height = lnr * linedist;
1786 float scale_to_fit = tb_scale.h / total_text_height;
1787
1788 iter_data.bisect.max = 1.0f;
1789 iter_data.bisect.min = scale_to_fit;
1790
1791 iter_data.status = VFONT_TO_CURVE_BISECT;
1792 }
1793 }
1794 else {
1796 /* Try to get the highest scale that gives us the exactly
1797 * number of lines we need. */
1798 bool valid = false;
1799
1800 if ((last_line != -1) && (lnr > last_line)) {
1801 /* It is overflowing, scale it down. */
1802 iter_data.bisect.max = iter_data.scale_to_fit;
1803 }
1804 else {
1805 /* It fits inside the text-box, scale it up. */
1806 iter_data.bisect.min = iter_data.scale_to_fit;
1807 valid = true;
1808 }
1809
1810 /* Bisecting to try to find the best fit. */
1811 iter_data.scale_to_fit = (iter_data.bisect.max + iter_data.bisect.min) * 0.5f;
1812
1813 /* We iterated enough or got a good enough result. */
1814 if ((!iter_data.iteration--) || ((iter_data.bisect.max - iter_data.bisect.min) <
1816 {
1817 if (valid) {
1818 iter_data.status = VFONT_TO_CURVE_DONE;
1819 }
1820 else {
1821 iter_data.scale_to_fit = iter_data.bisect.min;
1823 }
1824 }
1825 }
1826 }
1827 }
1828
1829 if (cursor_params) {
1830 const float2 &cursor_location = cursor_params->cursor_location;
1831 /* Erasing all text could give `slen = 0`. */
1832 if (slen == 0) {
1833 cursor_params->r_string_offset = -1;
1834 }
1835 else if (cu.textoncurve != nullptr) {
1836
1837 int closest_char = -1;
1838 float closest_dist_sq = FLT_MAX;
1839
1840 for (i = 0; i <= slen; i++) {
1841 const float2 char_location = chartransdata[i].offset * font_size;
1842 const float test_dist_sq = math::distance_squared(cursor_location, char_location);
1843 if (closest_dist_sq > test_dist_sq) {
1844 closest_char = i;
1845 closest_dist_sq = test_dist_sq;
1846 }
1847 }
1848
1849 cursor_params->r_string_offset = closest_char;
1850 }
1851 else {
1852 /* Find the first box closest to `cursor_location`. */
1853 int char_beg = 0;
1854 int char_end = slen;
1855
1856 if (tb_bounds_for_cursor != nullptr) {
1857 /* Search for the closest box. */
1858 int closest_box = -1;
1859 float closest_dist_sq = FLT_MAX;
1860 for (curbox = 0; curbox < cu.totbox; curbox++) {
1861 const TextBoxBounds_ForCursor *tb_bounds = &tb_bounds_for_cursor[curbox];
1862 if (tb_bounds->char_index_last == -1) {
1863 continue;
1864 }
1865 /* The closest point in the box to the `cursor_location`
1866 * by clamping it to the bounding box. */
1867 const float2 cursor_location_clamped = {
1868 clamp_f(cursor_location.x, tb_bounds->bounds.xmin, tb_bounds->bounds.xmax),
1869 clamp_f(cursor_location.y, tb_bounds->bounds.ymin, tb_bounds->bounds.ymax),
1870 };
1871
1872 const float test_dist_sq = math::distance_squared(cursor_location,
1873 cursor_location_clamped);
1874 if (test_dist_sq < closest_dist_sq) {
1875 closest_dist_sq = test_dist_sq;
1876 closest_box = curbox;
1877 }
1878 }
1879 if (closest_box != -1) {
1880 if (closest_box != 0) {
1881 char_beg = tb_bounds_for_cursor[closest_box - 1].char_index_last + 1;
1882 }
1883 char_end = tb_bounds_for_cursor[closest_box].char_index_last;
1884 }
1885 MEM_delete(tb_bounds_for_cursor);
1886 tb_bounds_for_cursor = nullptr; /* Safety only. */
1887 }
1888 const float interline_offset = ((linedist - 0.5f) / 2.0f) * font_size;
1889 /* Loop until find the line where `cursor_location` is over. */
1890 for (i = char_beg; i <= char_end; i++) {
1891 if (cursor_location.y >= ((chartransdata[i].offset.y * font_size) - interline_offset)) {
1892 break;
1893 }
1894 }
1895
1896 i = min_ii(i, char_end);
1897 const short char_line = chartransdata[i].linenr;
1898
1899 /* Loop back until find the first character of the line, this because `cursor_location` can
1900 * be positioned further below the text, so #i can be the last character of the last line. */
1901 for (; i >= char_beg + 1 && chartransdata[i - 1].linenr == char_line; i--) {
1902 /* Pass. */
1903 }
1904 /* Loop until find the first character to the right of `cursor_location`
1905 * (using the character midpoint on the x-axis as a reference). */
1906 for (; i <= char_end && chartransdata[i].linenr == char_line; i++) {
1907 info = &custrinfo[i];
1908 const bool is_smallcaps = chartransdata[i].is_smallcaps;
1909 const char32_t charcode = vfont_char_apply_smallcaps(mem[i], is_smallcaps);
1910
1911 vfont_info_context_update(&vfinfo_ctx, cu, info);
1912 che = vfont_char_find_or_placeholder(vfinfo_ctx.vfd, charcode, che_placeholder);
1913
1914 const float charwidth = vfont_char_width(cu, che, is_smallcaps);
1915 if (vfont_char_is_combining(charcode, charwidth)) [[unlikely]] {
1916 /* Combining character, skip so the cursor won't be between combining characters. */
1917 continue;
1918 }
1919 const float charhalf = (charwidth / 2.0f);
1920 if (cursor_location.x <= ((chartransdata[i].offset.x + charhalf) * font_size)) {
1921 break;
1922 }
1923 }
1924 i = min_ii(i, char_end);
1925
1926 /* If there is no character to the right of the cursor we are on the next line, go back to
1927 * the last character of the previous line. */
1928 if (i > char_beg && chartransdata[i].linenr != char_line) {
1929 i -= 1;
1930 }
1931 cursor_params->r_string_offset = i;
1932 }
1933 /* Must be cleared & freed. */
1934 BLI_assert(tb_bounds_for_cursor == nullptr);
1935 }
1936
1937 /* Scale to fit only works for single text box layouts. */
1939 /* Always cleanup before going to the scale-to-fit repetition. */
1940 if (r_nubase != nullptr) {
1941 BKE_nurbList_free(r_nubase);
1942 }
1943
1944 if (chartransdata != nullptr) {
1945 MEM_delete(chartransdata);
1946 }
1947
1948 if (mem_alloc) {
1949 MEM_delete(mem);
1950 }
1951 return true;
1952 }
1953
1954 if (r_text) {
1955 *r_text = mem;
1956 *r_text_len = slen;
1957 *r_text_free = mem_alloc;
1958 }
1959 else {
1960 if (mem_alloc) {
1961 MEM_delete(mem);
1962 }
1963 }
1964
1965 if (chartransdata) {
1966 if (r_chartransdata) {
1967 *r_chartransdata = chartransdata;
1968 }
1969 else {
1970 MEM_delete(chartransdata);
1971 }
1972 }
1973
1974 /* Store the effective scale, to use for the text-box lines. */
1975 if (ef != nullptr) {
1976 ef->font_size_eval = font_size;
1977 }
1978 if (r_font_size_eval) {
1979 *r_font_size_eval = font_size;
1980 }
1981 return true;
1982
1983#undef MARGIN_X_MIN
1984#undef MARGIN_Y_MIN
1985#undef XTRAX_WITH_CHAR_WIDTH
1986}
1987
1989
1990/* -------------------------------------------------------------------- */
1995
1997 const Curve &cu,
1998 const eEditFontMode mode,
1999 ListBaseT<Nurb> *r_nubase,
2000 const char32_t **r_text,
2001 int *r_text_len,
2002 bool *r_text_free,
2003 CharTrans **r_chartransdata,
2004 float *r_font_size_eval)
2005{
2008 data.scale_to_fit = 1.0f;
2009 data.word_wrap = true;
2010 data.ok = true;
2011 data.status = VFONT_TO_CURVE_INIT;
2012
2013 do {
2014 data.ok &= vfont_to_curve(ob,
2015 cu,
2016 mode,
2017 data,
2018 nullptr,
2019 r_nubase,
2020 r_text,
2021 r_text_len,
2022 r_text_free,
2023 r_chartransdata,
2024 r_font_size_eval);
2026
2027 return data.ok;
2028}
2029
2030int BKE_vfont_cursor_to_text_index(Object *ob, const float2 &cursor_location)
2031{
2032 Curve &cu = *id_cast<Curve *>(ob->data);
2033 ListBaseT<Nurb> *r_nubase = &cu.nurb;
2034
2035 /* TODO: iterating to calculate the scale can be avoided. */
2038 data.scale_to_fit = 1.0f;
2039 data.word_wrap = true;
2040 data.ok = true;
2041 data.status = VFONT_TO_CURVE_INIT;
2042
2043 VFontCursor_Params cursor_params = {};
2044 cursor_params.cursor_location = cursor_location;
2045 cursor_params.r_string_offset = -1;
2046
2047 do {
2048 data.ok &= vfont_to_curve(ob,
2049 cu,
2050 FO_CURS,
2051 data,
2052 &cursor_params,
2053 r_nubase,
2054 nullptr,
2055 nullptr,
2056 nullptr,
2057 nullptr,
2058 nullptr);
2060
2061 return cursor_params.r_string_offset;
2062}
2063
2064#undef FONT_TO_CURVE_SCALE_ITERATIONS
2065#undef FONT_TO_CURVE_SCALE_THRESHOLD
2066
2067bool BKE_vfont_to_curve_nubase(Object *ob, const eEditFontMode mode, ListBaseT<Nurb> *r_nubase)
2068{
2069 BLI_assert(ob->type == OB_FONT);
2070 const Curve &cu = *id_cast<const Curve *>(ob->data);
2071 return BKE_vfont_to_curve_ex(
2072 ob, cu, mode, r_nubase, nullptr, nullptr, nullptr, nullptr, nullptr);
2073}
2074
2076{
2077 Curve &cu = *id_cast<Curve *>(ob->data);
2078 return BKE_vfont_to_curve_ex(
2079 ob, cu, mode, &cu.nurb, nullptr, nullptr, nullptr, nullptr, nullptr);
2080}
2081
2083
2084} // namespace blender
#define FO_CURS_IS_MOTION(mode)
Definition BKE_vfont.hh:96
A structure to represent vector fonts, and to load them from PostScript fonts.
#define M_PI
#define THREAD_LOCK_READ
#define THREAD_LOCK_WRITE
#define BLI_RWLOCK_INITIALIZER
#define CU_CHINFO_STYLE_ALL
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define CLAMP(a, b, c)
#define ARRAY_SET_ITEMS(...)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define FLT_MAX
#define FLT_EPSILON
float3x3 scale(float3x3 mat, float2 scale)
float3x3 rotate(float3x3 mat, AxisAngle rotation)
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
T distance_squared(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
BMesh Iterators.
MINLINE float min_ff(float a, float b)
static bool vfont_to_curve(Object *ob, const Curve &cu, const eEditFontMode mode, VFontToCurveIter &iter_data, VFontCursor_Params *cursor_params, ListBaseT< Nurb > *r_nubase, const char32_t **r_text, int *r_text_len, bool *r_text_free, CharTrans **r_chartransdata, float *r_font_size_eval)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static VChar * vfont_placeholder_ensure(VCharPlaceHolder &che_placeholder, char32_t charcode)
MINLINE int compare_ff_relative(float a, float b, float max_diff, int max_ulps)
@ VFONT_TO_CURVE_DONE
@ VFONT_TO_CURVE_INIT
@ VFONT_TO_CURVE_SCALE_ONCE
@ VFONT_TO_CURVE_BISECT
static VChar * vfont_char_ensure(VFont *vfont, char32_t charcode)
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void copy_m3_m4(float m1[3][3], const float m2[4][4])
static bool vfont_char_find_no_lock(const VFontData *vfd, char32_t charcode, VChar **r_che)
static VFont * vfont_from_charinfo(const Curve &cu, const CharInfo *info)
MINLINE void mul_v2_fl(float r[2], float f)
bool BKE_where_on_path(const struct Object *ob, float ctime, float r_vec[4], float r_dir[3], float r_quat[4], float *r_radius, float *r_weight)
void BKE_nurbList_free(ListBaseT< Nurb > *lb)
Definition curve.cc:619
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
size_t BLI_str_utf8_as_utf32(char32_t *__restrict dst_w, const char *__restrict src_c, size_t dst_w_maxncpy) ATTR_NONNULL(1
static float vfont_metrics_ascent(const VFontData_Metrics *metrics)
MINLINE int max_ii(int a, int b)
MINLINE void copy_v4_fl4(float v[4], float x, float y, float z, float w)
void BKE_vfont_char_build(const Curve &cu, ListBaseT< Nurb > *nubase, unsigned int charcode, const CharInfo *info, bool is_smallcaps, const float2 &offset, float rotate, int charidx, float fsize)
static bool vfont_char_find(const VFontData *vfd, char32_t charcode, VChar **r_che)
static VFontData * vfont_data_ensure(VFont *vfont)
void BLI_rw_mutex_lock(ThreadRWMutex *mutex, int mode)
Definition threads.cc:470
static void vfont_char_build_impl(const Curve &cu, ListBaseT< Nurb > *nubase, const VChar *che, const CharInfo *info, const bool is_smallcaps, const float2 &offset, const float rotate, const int charidx, const float fsize)
static void vfont_info_context_init(VFontInfoContext *vfinfo_ctx, const Curve &cu)
MINLINE void mul_v3_fl(float r[3], float f)
bool BKE_vfont_to_curve_ex(Object *ob, const Curve &cu, eEditFontMode mode, ListBaseT< Nurb > *r_nubase, const char32_t **r_text, int *r_text_len, bool *r_text_free, CharTrans **r_chartransdata, float *r_font_size_eval)
static ThreadRWMutex vfont_rwlock
void BLI_rctf_mul(struct rctf *rect, float factor)
Definition rct.cc:602
VChar * BKE_vfontdata_char_from_freetypefont(VFont *vfont, unsigned int character)
VecBase< float, 2 > float2
static void mid_v2v2(float a[2], float b[2])
void rotate_v2_v2fl(float r[2], const float p[2], float angle)
void BLI_rw_mutex_unlock(ThreadRWMutex *mutex)
Definition threads.cc:480
BMesh const char void * data
static VChar * vfont_char_find_or_placeholder(const VFontData *vfd, char32_t charcode, VCharPlaceHolder &che_placeholder)
static Nurb * build_underline(const Curve &cu, ListBaseT< Nurb > *nubase, const rctf *rect, const float yofs, const float rotate, const int charidx, const short mat_nr, const float font_size, Nurb *ul_prev_nu)
bool BKE_vfont_to_curve(Object *ob, eEditFontMode mode)
@ FO_SELCHANGE
Definition BKE_vfont.hh:92
@ FO_PAGEUP
Definition BKE_vfont.hh:88
@ FO_CURSUP
Definition BKE_vfont.hh:85
@ FO_LINE_END
Definition BKE_vfont.hh:91
@ FO_PAGEDOWN
Definition BKE_vfont.hh:89
@ FO_CURSDOWN
Definition BKE_vfont.hh:86
@ FO_LINE_BEGIN
Definition BKE_vfont.hh:90
static bool vfont_char_is_combining(const char32_t charcode, const float char_width)
void BKE_vfont_data_ensure(VFont *vfont)
Definition vfont.cc:205
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
void unit_m4(float m[4][4])
int BKE_vfont_select_get(const Curve *cu, int *r_start, int *r_end)
Definition vfont.cc:424
MINLINE float normalize_v3(float n[3])
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
static void textbox_scale(TextBox *tb_dst, const TextBox *tb_src, float scale)
MINLINE float max_ff(float a, float b)
BLI_INLINE float BLI_rctf_cent_y(const struct rctf *rct)
Definition BLI_rect.hh:198
static void vfont_info_context_update(VFontInfoContext *vfinfo_ctx, const Curve &cu, const CharInfo *info)
static char32_t vfont_char_apply_smallcaps(char32_t charcode, const bool is_smallcaps)
float BKE_anim_path_get_length(const struct CurveCache *curve_cache)
void mul_m3_m3m3(float R[3][3], const float A[3][3], const float B[3][3])
void BKE_vfontdata_metrics_get_defaults(VFontData_Metrics *metrics)
MINLINE float safe_divide(float a, float b)
MINLINE int min_ii(int a, int b)
pthread_rwlock_t ThreadRWMutex
int BKE_vfont_cursor_to_text_index(Object *ob, const float2 &cursor_location)
bool BKE_vfont_to_curve_nubase(Object *ob, eEditFontMode mode, ListBaseT< Nurb > *r_nubase)
BLI_INLINE float BLI_rctf_cent_x(const struct rctf *rct)
Definition BLI_rect.hh:194
static float vfont_char_width(const Curve &cu, VChar *che, const bool is_smallcaps)
@ CU_ALIGN_Y_TOP_BASELINE
@ CU_ALIGN_Y_BOTTOM_BASELINE
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
MINLINE float clamp_f(float value, float min, float max)
size_t size_t int BLI_wcwidth_or_error(char32_t ucs) ATTR_WARN_UNUSED_RESULT
static float vfont_metrics_descent(const VFontData_Metrics *metrics)
#define sinf
#define cosf
#define ceilf
#define atan2f
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
eBezTriple_Handle h2
eBezTriple_Handle h1
const float * anim_path_accum_length
Definition BKE_curve.hh:53
struct VFont * vfonti
struct VFont * vfont
ListBaseT< Nurb > nurb
eCurveAlignY align_y
struct TextBox * tb
struct EditFont * editfont
eCurveSpaceMode spacemode
eCurveOverflow overflow
struct Object * textoncurve
struct CharInfo * strinfo
struct VFont * vfontb
struct VFont * vfontbi
CharInfo * textbufinfo
Definition BKE_vfont.hh:48
EditFontSelBox * selboxes
Definition BKE_vfont.hh:55
float2 textcurs[4]
Definition BKE_vfont.hh:54
char32_t * textbuf
Definition BKE_vfont.hh:46
BezTriple * bezt
eNurbKnotFlag flagu
struct Nurb * next
bke::ObjectRuntime * runtime
struct blender::VCharPlaceHolder::@315142112317153375343340361115150036337051316052 data
const VFontData_Metrics * metrics
ListBaseT< Nurb > nurbsbase
VFontData_Metrics metrics
Map< uint, struct VChar * > * characters
struct blender::VFontToCurveIter::@071131017331153006171351347221321314224170011034 bisect
struct VFontData * data
#define FONT_TO_CURVE_SCALE_THRESHOLD
#define MARGIN_Y_MIN
#define FONT_TO_CURVE_SCALE_ITERATIONS
#define MARGIN_X_MIN
#define XTRAX_WITH_CHAR_WIDTH(twidth)