1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 *
16 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17 * All rights reserved.
18 */
19
20 /** \file
21 * \ingroup bke
22 */
23
24 #include <math.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <wctype.h>
29
30 #include "CLG_log.h"
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_ghash.h"
35 #include "BLI_listbase.h"
36 #include "BLI_math.h"
37 #include "BLI_path_util.h"
38 #include "BLI_string.h"
39 #include "BLI_string_utf8.h"
40 #include "BLI_threads.h"
41 #include "BLI_utildefines.h"
42 #include "BLI_vfontdata.h"
43
44 #include "BLT_translation.h"
45
46 #include "DNA_curve_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_packedFile_types.h"
49 #include "DNA_vfont_types.h"
50
51 #include "BKE_anim_path.h"
52 #include "BKE_curve.h"
53 #include "BKE_font.h"
54 #include "BKE_global.h"
55 #include "BKE_idtype.h"
56 #include "BKE_lib_id.h"
57 #include "BKE_main.h"
58 #include "BKE_packedFile.h"
59
60 #include "BLO_read_write.h"
61
62 static CLG_LogRef LOG = {"bke.data_transfer"};
63 static ThreadRWMutex vfont_rwlock = BLI_RWLOCK_INITIALIZER;
64
65 /**************************** Prototypes **************************/
66
67 static PackedFile *get_builtin_packedfile(void);
68
69 /****************************** VFont Datablock ************************/
70
vfont_init_data(ID * id)71 static void vfont_init_data(ID *id)
72 {
73 VFont *vfont = (VFont *)id;
74 PackedFile *pf = get_builtin_packedfile();
75
76 if (pf) {
77 VFontData *vfd;
78
79 vfd = BLI_vfontdata_from_freetypefont(pf);
80 if (vfd) {
81 vfont->data = vfd;
82
83 BLI_strncpy(vfont->filepath, FO_BUILTIN_NAME, sizeof(vfont->filepath));
84 }
85
86 /* Free the packed file */
87 BKE_packedfile_free(pf);
88 }
89 }
90
vfont_copy_data(Main * UNUSED (bmain),ID * id_dst,const ID * UNUSED (id_src),const int flag)91 static void vfont_copy_data(Main *UNUSED(bmain),
92 ID *id_dst,
93 const ID *UNUSED(id_src),
94 const int flag)
95 {
96 VFont *vfont_dst = (VFont *)id_dst;
97
98 /* We never handle usercount here for own data. */
99 const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT;
100
101 /* Just to be sure, should not have any value actually after reading time. */
102 vfont_dst->temp_pf = NULL;
103
104 if (vfont_dst->packedfile) {
105 vfont_dst->packedfile = BKE_packedfile_duplicate(vfont_dst->packedfile);
106 }
107
108 if (vfont_dst->data) {
109 vfont_dst->data = BLI_vfontdata_copy(vfont_dst->data, flag_subdata);
110 }
111 }
112
113 /** Free (or release) any data used by this font (does not free the font itself). */
vfont_free_data(ID * id)114 static void vfont_free_data(ID *id)
115 {
116 VFont *vfont = (VFont *)id;
117 BKE_vfont_free_data(vfont);
118
119 if (vfont->packedfile) {
120 BKE_packedfile_free(vfont->packedfile);
121 vfont->packedfile = NULL;
122 }
123 }
124
vfont_blend_write(BlendWriter * writer,ID * id,const void * id_address)125 static void vfont_blend_write(BlendWriter *writer, ID *id, const void *id_address)
126 {
127 VFont *vf = (VFont *)id;
128 if (vf->id.us > 0 || BLO_write_is_undo(writer)) {
129 /* Clean up, important in undo case to reduce false detection of changed datablocks. */
130 vf->data = NULL;
131 vf->temp_pf = NULL;
132
133 /* write LibData */
134 BLO_write_id_struct(writer, VFont, id_address, &vf->id);
135 BKE_id_blend_write(writer, &vf->id);
136
137 /* direct data */
138 BKE_packedfile_blend_write(writer, vf->packedfile);
139 }
140 }
141
vfont_blend_read_data(BlendDataReader * reader,ID * id)142 static void vfont_blend_read_data(BlendDataReader *reader, ID *id)
143 {
144 VFont *vf = (VFont *)id;
145 vf->data = NULL;
146 vf->temp_pf = NULL;
147 BKE_packedfile_blend_read(reader, &vf->packedfile);
148 }
149
150 IDTypeInfo IDType_ID_VF = {
151 .id_code = ID_VF,
152 .id_filter = FILTER_ID_VF,
153 .main_listbase_index = INDEX_ID_VF,
154 .struct_size = sizeof(VFont),
155 .name = "Font",
156 .name_plural = "fonts",
157 .translation_context = BLT_I18NCONTEXT_ID_VFONT,
158 .flags = IDTYPE_FLAGS_NO_ANIMDATA,
159
160 .init_data = vfont_init_data,
161 .copy_data = vfont_copy_data,
162 .free_data = vfont_free_data,
163 .make_local = NULL,
164 .foreach_id = NULL,
165 .foreach_cache = NULL,
166
167 .blend_write = vfont_blend_write,
168 .blend_read_data = vfont_blend_read_data,
169 .blend_read_lib = NULL,
170 .blend_read_expand = NULL,
171 };
172
173 /***************************** VFont *******************************/
174
175 /* The vfont code */
BKE_vfont_free_data(struct VFont * vfont)176 void BKE_vfont_free_data(struct VFont *vfont)
177 {
178 if (vfont->data) {
179 if (vfont->data->characters) {
180 GHashIterator gh_iter;
181 GHASH_ITER (gh_iter, vfont->data->characters) {
182 VChar *che = BLI_ghashIterator_getValue(&gh_iter);
183
184 while (che->nurbsbase.first) {
185 Nurb *nu = che->nurbsbase.first;
186 if (nu->bezt) {
187 MEM_freeN(nu->bezt);
188 }
189 BLI_freelinkN(&che->nurbsbase, nu);
190 }
191
192 MEM_freeN(che);
193 }
194
195 BLI_ghash_free(vfont->data->characters, NULL, NULL);
196 }
197
198 MEM_freeN(vfont->data);
199 vfont->data = NULL;
200 }
201
202 if (vfont->temp_pf) {
203 BKE_packedfile_free(vfont->temp_pf); /* NULL when the font file can't be found on disk */
204 vfont->temp_pf = NULL;
205 }
206 }
207
208 static void *builtin_font_data = NULL;
209 static int builtin_font_size = 0;
210
BKE_vfont_is_builtin(struct VFont * vfont)211 bool BKE_vfont_is_builtin(struct VFont *vfont)
212 {
213 return STREQ(vfont->filepath, FO_BUILTIN_NAME);
214 }
215
BKE_vfont_builtin_register(void * mem,int size)216 void BKE_vfont_builtin_register(void *mem, int size)
217 {
218 builtin_font_data = mem;
219 builtin_font_size = size;
220 }
221
get_builtin_packedfile(void)222 static PackedFile *get_builtin_packedfile(void)
223 {
224 if (!builtin_font_data) {
225 CLOG_ERROR(&LOG, "Internal error, builtin font not loaded");
226
227 return NULL;
228 }
229
230 void *mem = MEM_mallocN(builtin_font_size, "vfd_builtin");
231
232 memcpy(mem, builtin_font_data, builtin_font_size);
233
234 return BKE_packedfile_new_from_memory(mem, builtin_font_size);
235 }
236
vfont_get_data(VFont * vfont)237 static VFontData *vfont_get_data(VFont *vfont)
238 {
239 if (vfont == NULL) {
240 return NULL;
241 }
242
243 /* And then set the data */
244 if (!vfont->data) {
245 PackedFile *pf;
246
247 BLI_rw_mutex_lock(&vfont_rwlock, THREAD_LOCK_WRITE);
248
249 if (vfont->data) {
250 /* Check data again, since it might have been already
251 * initialized from other thread (previous check is
252 * not accurate or threading, just prevents unneeded
253 * lock if all the data is here for sure).
254 */
255 BLI_rw_mutex_unlock(&vfont_rwlock);
256 return vfont->data;
257 }
258
259 if (BKE_vfont_is_builtin(vfont)) {
260 pf = get_builtin_packedfile();
261 }
262 else {
263 if (vfont->packedfile) {
264 pf = vfont->packedfile;
265
266 /* We need to copy a tmp font to memory unless it is already there */
267 if (vfont->temp_pf == NULL) {
268 vfont->temp_pf = BKE_packedfile_duplicate(pf);
269 }
270 }
271 else {
272 pf = BKE_packedfile_new(NULL, vfont->filepath, ID_BLEND_PATH_FROM_GLOBAL(&vfont->id));
273
274 if (vfont->temp_pf == NULL) {
275 vfont->temp_pf = BKE_packedfile_new(
276 NULL, vfont->filepath, ID_BLEND_PATH_FROM_GLOBAL(&vfont->id));
277 }
278 }
279 if (!pf) {
280 CLOG_WARN(&LOG, "Font file doesn't exist: %s", vfont->filepath);
281
282 /* DON'T DO THIS
283 * missing file shouldn't modify path! - campbell */
284 #if 0
285 strcpy(vfont->filepath, FO_BUILTIN_NAME);
286 #endif
287 pf = get_builtin_packedfile();
288 }
289 }
290
291 if (pf) {
292 vfont->data = BLI_vfontdata_from_freetypefont(pf);
293 if (pf != vfont->packedfile) {
294 BKE_packedfile_free(pf);
295 }
296 }
297
298 BLI_rw_mutex_unlock(&vfont_rwlock);
299 }
300
301 return vfont->data;
302 }
303
BKE_vfont_load(Main * bmain,const char * filepath)304 VFont *BKE_vfont_load(Main *bmain, const char *filepath)
305 {
306 char filename[FILE_MAXFILE];
307 VFont *vfont = NULL;
308 PackedFile *pf;
309 bool is_builtin;
310
311 if (STREQ(filepath, FO_BUILTIN_NAME)) {
312 BLI_strncpy(filename, filepath, sizeof(filename));
313
314 pf = get_builtin_packedfile();
315 is_builtin = true;
316 }
317 else {
318 BLI_split_file_part(filepath, filename, sizeof(filename));
319 pf = BKE_packedfile_new(NULL, filepath, BKE_main_blendfile_path(bmain));
320
321 is_builtin = false;
322 }
323
324 if (pf) {
325 VFontData *vfd;
326
327 vfd = BLI_vfontdata_from_freetypefont(pf);
328 if (vfd) {
329 vfont = BKE_libblock_alloc(bmain, ID_VF, filename, 0);
330 vfont->data = vfd;
331
332 /* if there's a font name, use it for the ID name */
333 if (vfd->name[0] != '\0') {
334 BLI_strncpy(vfont->id.name + 2, vfd->name, sizeof(vfont->id.name) - 2);
335 }
336 BLI_strncpy(vfont->filepath, filepath, sizeof(vfont->filepath));
337
338 /* if autopack is on store the packedfile in de font structure */
339 if (!is_builtin && (G.fileflags & G_FILE_AUTOPACK)) {
340 vfont->packedfile = pf;
341 }
342
343 /* Do not add FO_BUILTIN_NAME to temporary listbase */
344 if (!STREQ(filename, FO_BUILTIN_NAME)) {
345 vfont->temp_pf = BKE_packedfile_new(NULL, filepath, BKE_main_blendfile_path(bmain));
346 }
347 }
348
349 /* Free the packed file */
350 if (!vfont || vfont->packedfile != pf) {
351 BKE_packedfile_free(pf);
352 }
353 }
354
355 return vfont;
356 }
357
BKE_vfont_load_exists_ex(struct Main * bmain,const char * filepath,bool * r_exists)358 VFont *BKE_vfont_load_exists_ex(struct Main *bmain, const char *filepath, bool *r_exists)
359 {
360 VFont *vfont;
361 char str[FILE_MAX], strtest[FILE_MAX];
362
363 BLI_strncpy(str, filepath, sizeof(str));
364 BLI_path_abs(str, BKE_main_blendfile_path(bmain));
365
366 /* first search an identical filepath */
367 for (vfont = bmain->fonts.first; vfont; vfont = vfont->id.next) {
368 BLI_strncpy(strtest, vfont->filepath, sizeof(vfont->filepath));
369 BLI_path_abs(strtest, ID_BLEND_PATH(bmain, &vfont->id));
370
371 if (BLI_path_cmp(strtest, str) == 0) {
372 id_us_plus(&vfont->id); /* officially should not, it doesn't link here! */
373 if (r_exists) {
374 *r_exists = true;
375 }
376 return vfont;
377 }
378 }
379
380 if (r_exists) {
381 *r_exists = false;
382 }
383 return BKE_vfont_load(bmain, filepath);
384 }
385
BKE_vfont_load_exists(struct Main * bmain,const char * filepath)386 VFont *BKE_vfont_load_exists(struct Main *bmain, const char *filepath)
387 {
388 return BKE_vfont_load_exists_ex(bmain, filepath, NULL);
389 }
390
which_vfont(Curve * cu,CharInfo * info)391 static VFont *which_vfont(Curve *cu, CharInfo *info)
392 {
393 switch (info->flag & (CU_CHINFO_BOLD | CU_CHINFO_ITALIC)) {
394 case CU_CHINFO_BOLD:
395 return cu->vfontb ? cu->vfontb : cu->vfont;
396 case CU_CHINFO_ITALIC:
397 return cu->vfonti ? cu->vfonti : cu->vfont;
398 case (CU_CHINFO_BOLD | CU_CHINFO_ITALIC):
399 return cu->vfontbi ? cu->vfontbi : cu->vfont;
400 default:
401 return cu->vfont;
402 }
403 }
404
BKE_vfont_builtin_get(void)405 VFont *BKE_vfont_builtin_get(void)
406 {
407 VFont *vfont;
408
409 for (vfont = G_MAIN->fonts.first; vfont; vfont = vfont->id.next) {
410 if (BKE_vfont_is_builtin(vfont)) {
411 return vfont;
412 }
413 }
414
415 return BKE_vfont_load(G_MAIN, FO_BUILTIN_NAME);
416 }
417
find_vfont_char(VFontData * vfd,unsigned int character)418 static VChar *find_vfont_char(VFontData *vfd, unsigned int character)
419 {
420 return BLI_ghash_lookup(vfd->characters, POINTER_FROM_UINT(character));
421 }
422
build_underline(Curve * cu,ListBase * nubase,const rctf * rect,float yofs,float rot,int charidx,short mat_nr,const float font_size)423 static void build_underline(Curve *cu,
424 ListBase *nubase,
425 const rctf *rect,
426 float yofs,
427 float rot,
428 int charidx,
429 short mat_nr,
430 const float font_size)
431 {
432 Nurb *nu2;
433 BPoint *bp;
434
435 nu2 = (Nurb *)MEM_callocN(sizeof(Nurb), "underline_nurb");
436 nu2->resolu = cu->resolu;
437 nu2->bezt = NULL;
438 nu2->knotsu = nu2->knotsv = NULL;
439 nu2->flag = CU_2D;
440 nu2->charidx = charidx + 1000;
441 if (mat_nr > 0) {
442 nu2->mat_nr = mat_nr - 1;
443 }
444 nu2->pntsu = 4;
445 nu2->pntsv = 1;
446 nu2->orderu = 4;
447 nu2->orderv = 1;
448 nu2->flagu = CU_NURB_CYCLIC;
449
450 bp = (BPoint *)MEM_calloc_arrayN(4, sizeof(BPoint), "underline_bp");
451
452 copy_v4_fl4(bp[0].vec, rect->xmin, (rect->ymax + yofs), 0.0f, 1.0f);
453 copy_v4_fl4(bp[1].vec, rect->xmax, (rect->ymax + yofs), 0.0f, 1.0f);
454 copy_v4_fl4(bp[2].vec, rect->xmax, (rect->ymin + yofs), 0.0f, 1.0f);
455 copy_v4_fl4(bp[3].vec, rect->xmin, (rect->ymin + yofs), 0.0f, 1.0f);
456
457 /* Used by curve extrusion. */
458 bp[0].radius = bp[1].radius = bp[2].radius = bp[3].radius = 1.0f;
459
460 nu2->bp = bp;
461 BLI_addtail(nubase, nu2);
462
463 if (rot != 0.0f) {
464 float si = sinf(rot);
465 float co = cosf(rot);
466
467 for (int i = nu2->pntsu; i > 0; i--) {
468 float *fp = bp->vec;
469
470 float x = fp[0] - rect->xmin;
471 float y = fp[1] - rect->ymin;
472
473 fp[0] = (+co * x + si * y) + rect->xmin;
474 fp[1] = (-si * x + co * y) + rect->ymin;
475
476 bp++;
477 }
478
479 bp = nu2->bp;
480 }
481
482 mul_v2_fl(bp[0].vec, font_size);
483 mul_v2_fl(bp[1].vec, font_size);
484 mul_v2_fl(bp[2].vec, font_size);
485 mul_v2_fl(bp[3].vec, font_size);
486 }
487
buildchar(Curve * cu,ListBase * nubase,unsigned int character,CharInfo * info,float ofsx,float ofsy,float rot,int charidx,const float fsize)488 static void buildchar(Curve *cu,
489 ListBase *nubase,
490 unsigned int character,
491 CharInfo *info,
492 float ofsx,
493 float ofsy,
494 float rot,
495 int charidx,
496 const float fsize)
497 {
498 VFontData *vfd = vfont_get_data(which_vfont(cu, info));
499 if (!vfd) {
500 return;
501 }
502
503 /* make a copy at distance ofsx, ofsy with shear */
504 float shear = cu->shear;
505 float si = sinf(rot);
506 float co = cosf(rot);
507
508 VChar *che = find_vfont_char(vfd, character);
509
510 /* Select the glyph data */
511 Nurb *nu1 = NULL;
512 if (che) {
513 nu1 = che->nurbsbase.first;
514 }
515
516 /* Create the character */
517 while (nu1) {
518 BezTriple *bezt1 = nu1->bezt;
519 if (bezt1) {
520 Nurb *nu2 = (Nurb *)MEM_mallocN(sizeof(Nurb), "duplichar_nurb");
521 if (nu2 == NULL) {
522 break;
523 }
524 memcpy(nu2, nu1, sizeof(struct Nurb));
525 nu2->resolu = cu->resolu;
526 nu2->bp = NULL;
527 nu2->knotsu = nu2->knotsv = NULL;
528 nu2->flag = CU_SMOOTH;
529 nu2->charidx = charidx;
530 if (info->mat_nr > 0) {
531 nu2->mat_nr = info->mat_nr - 1;
532 }
533 else {
534 nu2->mat_nr = 0;
535 }
536 /* nu2->trim.first = 0; */
537 /* nu2->trim.last = 0; */
538 int u = nu2->pntsu;
539
540 BezTriple *bezt2 = (BezTriple *)MEM_malloc_arrayN(u, sizeof(BezTriple), "duplichar_bezt2");
541 if (bezt2 == NULL) {
542 MEM_freeN(nu2);
543 break;
544 }
545 memcpy(bezt2, bezt1, u * sizeof(struct BezTriple));
546 nu2->bezt = bezt2;
547
548 if (shear != 0.0f) {
549 bezt2 = nu2->bezt;
550
551 for (int i = nu2->pntsu; i > 0; i--) {
552 bezt2->vec[0][0] += shear * bezt2->vec[0][1];
553 bezt2->vec[1][0] += shear * bezt2->vec[1][1];
554 bezt2->vec[2][0] += shear * bezt2->vec[2][1];
555 bezt2++;
556 }
557 }
558 if (rot != 0.0f) {
559 bezt2 = nu2->bezt;
560 for (int i = nu2->pntsu; i > 0; i--) {
561 float *fp = bezt2->vec[0];
562
563 float x = fp[0];
564 fp[0] = co * x + si * fp[1];
565 fp[1] = -si * x + co * fp[1];
566 x = fp[3];
567 fp[3] = co * x + si * fp[4];
568 fp[4] = -si * x + co * fp[4];
569 x = fp[6];
570 fp[6] = co * x + si * fp[7];
571 fp[7] = -si * x + co * fp[7];
572
573 bezt2++;
574 }
575 }
576 bezt2 = nu2->bezt;
577
578 if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
579 const float sca = cu->smallcaps_scale;
580 for (int i = nu2->pntsu; i > 0; i--) {
581 float *fp = bezt2->vec[0];
582 fp[0] *= sca;
583 fp[1] *= sca;
584 fp[3] *= sca;
585 fp[4] *= sca;
586 fp[6] *= sca;
587 fp[7] *= sca;
588 bezt2++;
589 }
590 }
591 bezt2 = nu2->bezt;
592
593 for (int i = nu2->pntsu; i > 0; i--) {
594 float *fp = bezt2->vec[0];
595 fp[0] = (fp[0] + ofsx) * fsize;
596 fp[1] = (fp[1] + ofsy) * fsize;
597 fp[3] = (fp[3] + ofsx) * fsize;
598 fp[4] = (fp[4] + ofsy) * fsize;
599 fp[6] = (fp[6] + ofsx) * fsize;
600 fp[7] = (fp[7] + ofsy) * fsize;
601 bezt2++;
602 }
603
604 BLI_addtail(nubase, nu2);
605 }
606
607 nu1 = nu1->next;
608 }
609 }
610
BKE_vfont_select_get(Object * ob,int * r_start,int * r_end)611 int BKE_vfont_select_get(Object *ob, int *r_start, int *r_end)
612 {
613 Curve *cu = ob->data;
614 EditFont *ef = cu->editfont;
615 int start, end, direction;
616
617 if ((ob->type != OB_FONT) || (ef == NULL)) {
618 return 0;
619 }
620
621 BLI_assert(ef->len >= 0);
622 BLI_assert(ef->selstart >= 0 && ef->selstart <= ef->len + 1);
623 BLI_assert(ef->selend >= 0 && ef->selend <= ef->len + 1);
624 BLI_assert(ef->pos >= 0 && ef->pos <= ef->len);
625
626 if (ef->selstart == 0) {
627 return 0;
628 }
629
630 if (ef->selstart <= ef->selend) {
631 start = ef->selstart - 1;
632 end = ef->selend - 1;
633 direction = 1;
634 }
635 else {
636 start = ef->selend;
637 end = ef->selstart - 2;
638 direction = -1;
639 }
640
641 if (start == end + 1) {
642 return 0;
643 }
644
645 BLI_assert(start < end + 1);
646 *r_start = start;
647 *r_end = end;
648 return direction;
649 }
650
BKE_vfont_select_clamp(Object * ob)651 void BKE_vfont_select_clamp(Object *ob)
652 {
653 Curve *cu = ob->data;
654 EditFont *ef = cu->editfont;
655
656 BLI_assert((ob->type == OB_FONT) && ef);
657
658 CLAMP_MAX(ef->pos, ef->len);
659 CLAMP_MAX(ef->selstart, ef->len + 1);
660 CLAMP_MAX(ef->selend, ef->len);
661 }
662
char_width(Curve * cu,VChar * che,CharInfo * info)663 static float char_width(Curve *cu, VChar *che, CharInfo *info)
664 {
665 /* The character wasn't found, probably ascii = 0, then the width shall be 0 as well */
666 if (che == NULL) {
667 return 0.0f;
668 }
669 if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
670 return che->width * cu->smallcaps_scale;
671 }
672
673 return che->width;
674 }
675
textbox_scale(TextBox * tb_dst,const TextBox * tb_src,float scale)676 static void textbox_scale(TextBox *tb_dst, const TextBox *tb_src, float scale)
677 {
678 tb_dst->x = tb_src->x * scale;
679 tb_dst->y = tb_src->y * scale;
680 tb_dst->w = tb_src->w * scale;
681 tb_dst->h = tb_src->h * scale;
682 }
683
684 /**
685 * Used for storing per-line data for alignment & wrapping.
686 */
687 struct TempLineInfo {
688 float x_min; /* left margin */
689 float x_max; /* right margin */
690 int char_nr; /* number of characters */
691 int wspace_nr; /* number of whitespaces of line */
692 };
693
694 /* -------------------------------------------------------------------- */
695 /** \name VFont Scale Overflow
696 *
697 * Scale the font to fit inside #TextBox bounds.
698 *
699 * - Scale horizontally when #TextBox.h is zero,
700 * otherwise scale vertically, allowing the text to wrap horizontally.
701 * - Never increase scale to fit, only ever scale on overflow.
702 * \{ */
703
704 typedef struct VFontToCurveIter {
705 int iteraction;
706 float scale_to_fit;
707 struct {
708 float min;
709 float max;
710 } bisect;
711 bool ok;
712 int status;
713 } VFontToCurveIter;
714
715 enum {
716 VFONT_TO_CURVE_INIT = 0,
717 VFONT_TO_CURVE_BISECT,
718 VFONT_TO_CURVE_SCALE_ONCE,
719 VFONT_TO_CURVE_DONE,
720 };
721
722 #define FONT_TO_CURVE_SCALE_ITERATIONS 20
723 #define FONT_TO_CURVE_SCALE_THRESHOLD 0.0001f
724
725 /** \} */
726
727 /**
728 * Font metric values explained:
729 *
730 * Baseline: Line where the text "rests", used as the origin vertical position for the glyphs.
731 * Em height: Space most glyphs should fit within.
732 * Ascent: the recommended distance above the baseline to fit most characters.
733 * Descent: the recommended distance below the baseline to fit most characters.
734 *
735 * We obtain ascent and descent from the font itself (FT_Face->ascender / face->height).
736 * And in some cases it is even the same value as FT_Face->bbox.yMax/yMin
737 * (font top and bottom respectively).
738 *
739 * The em_height here is relative to FT_Face->bbox.
740 */
741 #define ASCENT(vfd) ((vfd)->ascender * (vfd)->em_height)
742 #define DESCENT(vfd) ((vfd)->em_height - ASCENT(vfd))
743
vfont_to_curve(Object * ob,Curve * cu,int mode,VFontToCurveIter * iter_data,ListBase * r_nubase,const char32_t ** r_text,int * r_text_len,bool * r_text_free,struct CharTrans ** r_chartransdata)744 static bool vfont_to_curve(Object *ob,
745 Curve *cu,
746 int mode,
747 VFontToCurveIter *iter_data,
748 ListBase *r_nubase,
749 const char32_t **r_text,
750 int *r_text_len,
751 bool *r_text_free,
752 struct CharTrans **r_chartransdata)
753 {
754 EditFont *ef = cu->editfont;
755 EditFontSelBox *selboxes = NULL;
756 VFont *vfont, *oldvfont;
757 VFontData *vfd = NULL;
758 CharInfo *info = NULL, *custrinfo;
759 TextBox tb_scale;
760 bool use_textbox;
761 VChar *che;
762 struct CharTrans *chartransdata = NULL, *ct;
763 struct TempLineInfo *lineinfo;
764 float *f, xof, yof, xtrax, linedist;
765 float twidth = 0, maxlen = 0;
766 int i, slen, j;
767 int curbox;
768 int selstart = 0, selend = 0;
769 int cnr = 0, lnr = 0, wsnr = 0;
770 const char32_t *mem = NULL;
771 char32_t ascii;
772 bool ok = false;
773 const float font_size = cu->fsize * iter_data->scale_to_fit;
774 const float xof_scale = cu->xof / font_size;
775 const float yof_scale = cu->yof / font_size;
776 int last_line = -1;
777 /* Length of the text disregarding \n breaks. */
778 float current_line_length = 0.0f;
779 float longest_line_length = 0.0f;
780
781 /* Text at the beginning of the last used text-box (use for y-axis alignment).
782 * We overallocate by one to simplify logic of getting last char. */
783 int *i_textbox_array = MEM_callocN(sizeof(*i_textbox_array) * (cu->totbox + 1),
784 "TextBox initial char index");
785
786 #define MARGIN_X_MIN (xof_scale + tb_scale.x)
787 #define MARGIN_Y_MIN (yof_scale + tb_scale.y)
788
789 /* remark: do calculations including the trailing '\0' of a string
790 * because the cursor can be at that location */
791
792 BLI_assert(ob == NULL || ob->type == OB_FONT);
793
794 /* Set font data */
795 vfont = cu->vfont;
796
797 if (cu->str == NULL) {
798 return ok;
799 }
800 if (vfont == NULL) {
801 return ok;
802 }
803
804 vfd = vfont_get_data(vfont);
805
806 /* The VFont Data can not be found */
807 if (!vfd) {
808 return ok;
809 }
810
811 if (ef) {
812 slen = ef->len;
813 mem = ef->textbuf;
814 custrinfo = ef->textbufinfo;
815 }
816 else {
817 char32_t *mem_tmp;
818 slen = cu->len_char32;
819
820 /* Create unicode string */
821 mem_tmp = MEM_malloc_arrayN((slen + 1), sizeof(*mem_tmp), "convertedmem");
822 if (!mem_tmp) {
823 return ok;
824 }
825
826 BLI_str_utf8_as_utf32(mem_tmp, cu->str, slen + 1);
827
828 if (cu->strinfo == NULL) { /* old file */
829 cu->strinfo = MEM_calloc_arrayN((slen + 4), sizeof(CharInfo), "strinfo compat");
830 }
831 custrinfo = cu->strinfo;
832 if (!custrinfo) {
833 return ok;
834 }
835
836 mem = mem_tmp;
837 }
838
839 if (cu->tb == NULL) {
840 cu->tb = MEM_calloc_arrayN(MAXTEXTBOX, sizeof(TextBox), "TextBox compat");
841 }
842
843 if (ef != NULL && ob != NULL) {
844 if (ef->selboxes) {
845 MEM_freeN(ef->selboxes);
846 }
847
848 if (BKE_vfont_select_get(ob, &selstart, &selend)) {
849 ef->selboxes_len = (selend - selstart) + 1;
850 ef->selboxes = MEM_calloc_arrayN(ef->selboxes_len, sizeof(EditFontSelBox), "font selboxes");
851 }
852 else {
853 ef->selboxes_len = 0;
854 ef->selboxes = NULL;
855 }
856
857 selboxes = ef->selboxes;
858 }
859
860 /* calc offset and rotation of each char */
861 ct = chartransdata = MEM_calloc_arrayN((slen + 1), sizeof(struct CharTrans), "buildtext");
862
863 /* We assume the worst case: 1 character per line (is freed at end anyway) */
864 lineinfo = MEM_malloc_arrayN((slen * 2 + 1), sizeof(*lineinfo), "lineinfo");
865
866 linedist = cu->linedist;
867
868 curbox = 0;
869 textbox_scale(&tb_scale, &cu->tb[curbox], 1.0f / font_size);
870 use_textbox = (tb_scale.w != 0.0f);
871
872 xof = MARGIN_X_MIN;
873 yof = MARGIN_Y_MIN;
874
875 xtrax = 0.5f * cu->spacing - 0.5f;
876
877 oldvfont = NULL;
878
879 for (i = 0; i < slen; i++) {
880 custrinfo[i].flag &= ~(CU_CHINFO_WRAP | CU_CHINFO_SMALLCAPS_CHECK | CU_CHINFO_OVERFLOW);
881 }
882
883 for (i = 0; i <= slen; i++) {
884 makebreak:
885 /* Characters in the list */
886 info = &custrinfo[i];
887 ascii = mem[i];
888 if (info->flag & CU_CHINFO_SMALLCAPS) {
889 ascii = towupper(ascii);
890 if (mem[i] != ascii) {
891 info->flag |= CU_CHINFO_SMALLCAPS_CHECK;
892 }
893 }
894
895 vfont = which_vfont(cu, info);
896
897 if (vfont == NULL) {
898 break;
899 }
900
901 if (vfont != oldvfont) {
902 vfd = vfont_get_data(vfont);
903 oldvfont = vfont;
904 }
905
906 /* VFont Data for VFont couldn't be found */
907 if (!vfd) {
908 MEM_freeN(chartransdata);
909 chartransdata = NULL;
910 MEM_freeN(lineinfo);
911 goto finally;
912 }
913
914 if (!ELEM(ascii, '\n', '\0')) {
915 BLI_rw_mutex_lock(&vfont_rwlock, THREAD_LOCK_READ);
916 che = find_vfont_char(vfd, ascii);
917 BLI_rw_mutex_unlock(&vfont_rwlock);
918
919 /*
920 * The character wasn't in the current curve base so load it
921 * But if the font is built-in then do not try loading since
922 * whole font is in the memory already
923 */
924 if (che == NULL && BKE_vfont_is_builtin(vfont) == false) {
925 BLI_rw_mutex_lock(&vfont_rwlock, THREAD_LOCK_WRITE);
926 /* Check it once again, char might have been already load
927 * between previous BLI_rw_mutex_unlock() and this BLI_rw_mutex_lock().
928 *
929 * Such a check should not be a bottleneck since it wouldn't
930 * happen often once all the chars are load.
931 */
932 if ((che = find_vfont_char(vfd, ascii)) == NULL) {
933 che = BLI_vfontchar_from_freetypefont(vfont, ascii);
934 }
935 BLI_rw_mutex_unlock(&vfont_rwlock);
936 }
937 }
938 else {
939 che = NULL;
940 }
941
942 twidth = char_width(cu, che, info);
943
944 /* Calculate positions */
945 if ((tb_scale.w != 0.0f) && (ct->dobreak == 0) &&
946 (((xof - tb_scale.x) + twidth) > xof_scale + tb_scale.w)) {
947 // CLOG_WARN(&LOG, "linewidth exceeded: %c%c%c...", mem[i], mem[i+1], mem[i+2]);
948 for (j = i; j && (mem[j] != '\n') && (chartransdata[j].dobreak == 0); j--) {
949 bool dobreak = false;
950 if (mem[j] == ' ' || mem[j] == '-') {
951 ct -= (i - (j - 1));
952 cnr -= (i - (j - 1));
953 if (mem[j] == ' ') {
954 wsnr--;
955 }
956 if (mem[j] == '-') {
957 wsnr++;
958 }
959 i = j - 1;
960 xof = ct->xof;
961 ct[1].dobreak = 1;
962 custrinfo[i + 1].flag |= CU_CHINFO_WRAP;
963 dobreak = true;
964 }
965 else if (chartransdata[j].dobreak) {
966 // CLOG_WARN(&LOG, "word too long: %c%c%c...", mem[j], mem[j+1], mem[j+2]);
967 ct->dobreak = 1;
968 custrinfo[i + 1].flag |= CU_CHINFO_WRAP;
969 ct -= 1;
970 cnr -= 1;
971 i--;
972 xof = ct->xof;
973 dobreak = true;
974 }
975 if (dobreak) {
976 if (tb_scale.h == 0.0f) {
977 /* Note: If underlined text is truncated away, the extra space is also truncated. */
978 custrinfo[i + 1].flag |= CU_CHINFO_OVERFLOW;
979 }
980 goto makebreak;
981 }
982 }
983 }
984
985 if (ascii == '\n' || ascii == 0 || ct->dobreak) {
986 ct->xof = xof;
987 ct->yof = yof;
988 ct->linenr = lnr;
989 ct->charnr = cnr;
990
991 yof -= linedist;
992
993 lineinfo[lnr].x_min = (xof - xtrax) - tb_scale.x;
994 lineinfo[lnr].x_max = tb_scale.w;
995 lineinfo[lnr].char_nr = cnr;
996 lineinfo[lnr].wspace_nr = wsnr;
997
998 CLAMP_MIN(maxlen, lineinfo[lnr].x_min);
999
1000 if ((tb_scale.h != 0.0f) && ((-(yof - tb_scale.y)) > (tb_scale.h - linedist) - yof_scale)) {
1001 if (cu->totbox > (curbox + 1)) {
1002 maxlen = 0;
1003 curbox++;
1004 i_textbox_array[curbox] = i + 1;
1005
1006 textbox_scale(&tb_scale, &cu->tb[curbox], 1.0f / font_size);
1007
1008 yof = MARGIN_Y_MIN;
1009 }
1010 else if (last_line == -1) {
1011 last_line = lnr + 1;
1012 info->flag |= CU_CHINFO_OVERFLOW;
1013 }
1014 }
1015
1016 current_line_length += xof - MARGIN_X_MIN;
1017 if (ct->dobreak) {
1018 current_line_length += twidth;
1019 }
1020 else {
1021 longest_line_length = MAX2(current_line_length, longest_line_length);
1022 current_line_length = 0.0f;
1023 }
1024
1025 /* XXX, has been unused for years, need to check if this is useful, r4613 r5282 - campbell */
1026 #if 0
1027 if (ascii == '\n') {
1028 xof = xof_scale;
1029 }
1030 else {
1031 xof = MARGIN_X_MIN;
1032 }
1033 #else
1034 xof = MARGIN_X_MIN;
1035 #endif
1036 lnr++;
1037 cnr = 0;
1038 wsnr = 0;
1039 }
1040 else if (ascii == 9) { /* TAB */
1041 float tabfac;
1042
1043 ct->xof = xof;
1044 ct->yof = yof;
1045 ct->linenr = lnr;
1046 ct->charnr = cnr++;
1047
1048 tabfac = (xof - MARGIN_X_MIN + 0.01f);
1049 tabfac = 2.0f * ceilf(tabfac / 2.0f);
1050 xof = MARGIN_X_MIN + tabfac;
1051 }
1052 else {
1053 EditFontSelBox *sb = NULL;
1054 float wsfac;
1055
1056 ct->xof = xof;
1057 ct->yof = yof;
1058 ct->linenr = lnr;
1059 ct->charnr = cnr++;
1060
1061 if (selboxes && (i >= selstart) && (i <= selend)) {
1062 sb = &selboxes[i - selstart];
1063 sb->y = yof * font_size - linedist * font_size * 0.1f;
1064 sb->h = linedist * font_size;
1065 sb->w = xof * font_size;
1066 }
1067
1068 if (ascii == 32) {
1069 wsfac = cu->wordspace;
1070 wsnr++;
1071 }
1072 else {
1073 wsfac = 1.0f;
1074 }
1075
1076 /* Set the width of the character */
1077 twidth = char_width(cu, che, info);
1078
1079 xof += (twidth * wsfac * (1.0f + (info->kern / 40.0f))) + xtrax;
1080
1081 if (sb) {
1082 sb->w = (xof * font_size) - sb->w;
1083 }
1084 }
1085 ct++;
1086 }
1087 current_line_length += xof + twidth - MARGIN_X_MIN;
1088 longest_line_length = MAX2(current_line_length, longest_line_length);
1089
1090 cu->lines = 1;
1091 for (i = 0; i <= slen; i++) {
1092 ascii = mem[i];
1093 ct = &chartransdata[i];
1094 if (ascii == '\n' || ct->dobreak) {
1095 cu->lines++;
1096 }
1097 }
1098
1099 /* linedata is now: width of line */
1100
1101 if (cu->spacemode != CU_ALIGN_X_LEFT) {
1102 ct = chartransdata;
1103
1104 if (cu->spacemode == CU_ALIGN_X_RIGHT) {
1105 struct TempLineInfo *li;
1106
1107 for (i = 0, li = lineinfo; i < lnr; i++, li++) {
1108 li->x_min = (li->x_max - li->x_min) + xof_scale;
1109 }
1110
1111 for (i = 0; i <= slen; i++) {
1112 ct->xof += lineinfo[ct->linenr].x_min;
1113 ct++;
1114 }
1115 }
1116 else if (cu->spacemode == CU_ALIGN_X_MIDDLE) {
1117 struct TempLineInfo *li;
1118
1119 for (i = 0, li = lineinfo; i < lnr; i++, li++) {
1120 li->x_min = ((li->x_max - li->x_min) + xof_scale) / 2.0f;
1121 }
1122
1123 for (i = 0; i <= slen; i++) {
1124 ct->xof += lineinfo[ct->linenr].x_min;
1125 ct++;
1126 }
1127 }
1128 else if ((cu->spacemode == CU_ALIGN_X_FLUSH) && use_textbox) {
1129 struct TempLineInfo *li;
1130
1131 for (i = 0, li = lineinfo; i < lnr; i++, li++) {
1132 li->x_min = ((li->x_max - li->x_min) + xof_scale);
1133
1134 if (li->char_nr > 1) {
1135 li->x_min /= (float)(li->char_nr - 1);
1136 }
1137 }
1138 for (i = 0; i <= slen; i++) {
1139 for (j = i; (!ELEM(mem[j], '\0', '\n')) && (chartransdata[j].dobreak == 0) && (j < slen);
1140 j++) {
1141 /* do nothing */
1142 }
1143
1144 // if ((mem[j] != '\n') && (mem[j])) {
1145 ct->xof += ct->charnr * lineinfo[ct->linenr].x_min;
1146 // }
1147 ct++;
1148 }
1149 }
1150 else if ((cu->spacemode == CU_ALIGN_X_JUSTIFY) && use_textbox) {
1151 float curofs = 0.0f;
1152 for (i = 0; i <= slen; i++) {
1153 for (j = i; (mem[j]) && (mem[j] != '\n') && (chartransdata[j].dobreak == 0) && (j < slen);
1154 j++) {
1155 /* pass */
1156 }
1157
1158 if ((mem[j] != '\n') && ((chartransdata[j].dobreak != 0))) {
1159 if (mem[i] == ' ') {
1160 struct TempLineInfo *li;
1161
1162 li = &lineinfo[ct->linenr];
1163 curofs += ((li->x_max - li->x_min) + xof_scale) / (float)li->wspace_nr;
1164 }
1165 ct->xof += curofs;
1166 }
1167 if (mem[i] == '\n' || chartransdata[i].dobreak) {
1168 curofs = 0;
1169 }
1170 ct++;
1171 }
1172 }
1173 }
1174
1175 /* top-baseline is default, in this case, do nothing */
1176 if (cu->align_y != CU_ALIGN_Y_TOP_BASELINE) {
1177 if (tb_scale.h != 0.0f) {
1178 /* We need to loop all the text-boxes even the "full" ones.
1179 * This way they all get the same vertical padding. */
1180 for (int tb_index = 0; tb_index < cu->totbox; tb_index++) {
1181 struct CharTrans *ct_first, *ct_last;
1182 const int i_textbox = i_textbox_array[tb_index];
1183 const int i_textbox_next = i_textbox_array[tb_index + 1];
1184 const bool is_last_filled_textbox = ELEM(i_textbox_next, 0, slen + 1);
1185 int lines;
1186
1187 ct_first = chartransdata + i_textbox;
1188 ct_last = chartransdata + (is_last_filled_textbox ? slen : i_textbox_next - 1);
1189 lines = ct_last->linenr - ct_first->linenr + 1;
1190
1191 if (cu->overflow == CU_OVERFLOW_TRUNCATE) {
1192 /* Ensure overflow doesn't truncate text, before centering vertically
1193 * giving odd/buggy results, see: T66614. */
1194 if ((tb_index == cu->totbox - 1) && (last_line != -1)) {
1195 lines = last_line - ct_first->linenr;
1196 }
1197 }
1198
1199 textbox_scale(&tb_scale, &cu->tb[tb_index], 1.0f / font_size);
1200 /* The initial Y origin of the textbox is hardcoded to 1.0f * text scale. */
1201 const float textbox_y_origin = 1.0f;
1202 float yoff = 0.0f;
1203
1204 switch (cu->align_y) {
1205 case CU_ALIGN_Y_TOP_BASELINE:
1206 break;
1207 case CU_ALIGN_Y_TOP:
1208 yoff = textbox_y_origin - ASCENT(vfd);
1209 break;
1210 case CU_ALIGN_Y_CENTER:
1211 yoff = ((((vfd->em_height + (lines - 1) * linedist) * 0.5f) - ASCENT(vfd)) -
1212 (tb_scale.h * 0.5f) + textbox_y_origin);
1213 break;
1214 case CU_ALIGN_Y_BOTTOM_BASELINE:
1215 yoff = textbox_y_origin + ((lines - 1) * linedist) - tb_scale.h;
1216 break;
1217 case CU_ALIGN_Y_BOTTOM:
1218 yoff = textbox_y_origin + ((lines - 1) * linedist) - tb_scale.h + DESCENT(vfd);
1219 break;
1220 }
1221
1222 for (ct = ct_first; ct <= ct_last; ct++) {
1223 ct->yof += yoff;
1224 }
1225
1226 if (is_last_filled_textbox) {
1227 break;
1228 }
1229 }
1230 }
1231 else {
1232 /* Non text-box case handled separately. */
1233 float yoff = 0.0f;
1234
1235 switch (cu->align_y) {
1236 case CU_ALIGN_Y_TOP_BASELINE:
1237 break;
1238 case CU_ALIGN_Y_TOP:
1239 yoff = -ASCENT(vfd);
1240 break;
1241 case CU_ALIGN_Y_CENTER:
1242 yoff = ((vfd->em_height + (lnr - 1) * linedist) * 0.5f) - ASCENT(vfd);
1243 break;
1244 case CU_ALIGN_Y_BOTTOM_BASELINE:
1245 yoff = (lnr - 1) * linedist;
1246 break;
1247 case CU_ALIGN_Y_BOTTOM:
1248 yoff = (lnr - 1) * linedist + DESCENT(vfd);
1249 break;
1250 }
1251
1252 ct = chartransdata;
1253 for (i = 0; i <= slen; i++) {
1254 ct->yof += yoff;
1255 ct++;
1256 }
1257 }
1258 }
1259
1260 MEM_freeN(lineinfo);
1261 MEM_freeN(i_textbox_array);
1262
1263 /* TEXT ON CURVE */
1264 /* Note: Only OB_CURVE objects could have a path */
1265 if (cu->textoncurve && cu->textoncurve->type == OB_CURVE) {
1266 BLI_assert(cu->textoncurve->runtime.curve_cache != NULL);
1267 if (cu->textoncurve->runtime.curve_cache != NULL &&
1268 cu->textoncurve->runtime.curve_cache->path != NULL) {
1269 float distfac, imat[4][4], imat3[3][3], cmat[3][3];
1270 float minx, maxx;
1271 float timeofs, sizefac;
1272
1273 if (ob != NULL) {
1274 invert_m4_m4(imat, ob->obmat);
1275 }
1276 else {
1277 unit_m4(imat);
1278 }
1279 copy_m3_m4(imat3, imat);
1280
1281 copy_m3_m4(cmat, cu->textoncurve->obmat);
1282 mul_m3_m3m3(cmat, cmat, imat3);
1283 sizefac = normalize_v3(cmat[0]) / font_size;
1284
1285 ct = chartransdata;
1286 minx = maxx = ct->xof;
1287 ct++;
1288 for (i = 1; i <= slen; i++, ct++) {
1289 if (minx > ct->xof) {
1290 minx = ct->xof;
1291 }
1292 if (maxx < ct->xof) {
1293 maxx = ct->xof;
1294 }
1295 }
1296
1297 /* We put the x-coordinate exact at the curve, the y is rotated. */
1298
1299 /* length correction */
1300 const float chartrans_size_x = maxx - minx;
1301 if (chartrans_size_x != 0.0f) {
1302 const float totdist = cu->textoncurve->runtime.curve_cache->path->totdist;
1303 distfac = (sizefac * totdist) / chartrans_size_x;
1304 distfac = (distfac > 1.0f) ? (1.0f / distfac) : 1.0f;
1305 }
1306 else {
1307 /* Happens when there are no characters, set this value to place the text cursor. */
1308 distfac = 0.0f;
1309 }
1310
1311 timeofs = 0.0f;
1312
1313 if (distfac < 1.0f) {
1314 /* Path longer than text: space-mode is involved. */
1315
1316 if (cu->spacemode == CU_ALIGN_X_RIGHT) {
1317 timeofs = 1.0f - distfac;
1318 }
1319 else if (cu->spacemode == CU_ALIGN_X_MIDDLE) {
1320 timeofs = (1.0f - distfac) / 2.0f;
1321 }
1322 else if (cu->spacemode == CU_ALIGN_X_FLUSH) {
1323 distfac = 1.0f;
1324 }
1325 }
1326
1327 if (chartrans_size_x != 0.0f) {
1328 distfac /= chartrans_size_x;
1329 }
1330
1331 timeofs += distfac * cu->xof; /* not cyclic */
1332
1333 ct = chartransdata;
1334 for (i = 0; i <= slen; i++, ct++) {
1335 float ctime, dtime, vec[4], tvec[4], rotvec[3];
1336 float si, co;
1337
1338 /* Rotate around center character. */
1339 info = &custrinfo[i];
1340 ascii = mem[i];
1341 if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
1342 ascii = towupper(ascii);
1343 }
1344
1345 che = find_vfont_char(vfd, ascii);
1346
1347 twidth = char_width(cu, che, info);
1348
1349 dtime = distfac * 0.5f * twidth;
1350
1351 ctime = timeofs + distfac * (ct->xof - minx);
1352 CLAMP(ctime, 0.0f, 1.0f);
1353
1354 /* calc the right loc AND the right rot separately */
1355 /* vec, tvec need 4 items */
1356 where_on_path(cu->textoncurve, ctime, vec, tvec, NULL, NULL, NULL);
1357 where_on_path(cu->textoncurve, ctime + dtime, tvec, rotvec, NULL, NULL, NULL);
1358
1359 mul_v3_fl(vec, sizefac);
1360
1361 ct->rot = (float)M_PI - atan2f(rotvec[1], rotvec[0]);
1362
1363 si = sinf(ct->rot);
1364 co = cosf(ct->rot);
1365
1366 yof = ct->yof;
1367
1368 ct->xof = vec[0] + si * yof;
1369 ct->yof = vec[1] + co * yof;
1370
1371 if (selboxes && (i >= selstart) && (i <= selend)) {
1372 EditFontSelBox *sb;
1373 sb = &selboxes[i - selstart];
1374 sb->rot = -ct->rot;
1375 }
1376 }
1377 }
1378 }
1379
1380 if (selboxes) {
1381 ct = chartransdata;
1382 for (i = 0; i <= selend; i++, ct++) {
1383 if (i >= selstart) {
1384 selboxes[i - selstart].x = ct->xof * font_size;
1385 selboxes[i - selstart].y = ct->yof * font_size;
1386 }
1387 }
1388 }
1389
1390 if (ELEM(mode, FO_CURSUP, FO_CURSDOWN, FO_PAGEUP, FO_PAGEDOWN) &&
1391 iter_data->status == VFONT_TO_CURVE_INIT) {
1392 ct = &chartransdata[ef->pos];
1393
1394 if (ELEM(mode, FO_CURSUP, FO_PAGEUP) && ct->linenr == 0) {
1395 /* pass */
1396 }
1397 else if (ELEM(mode, FO_CURSDOWN, FO_PAGEDOWN) && ct->linenr == lnr) {
1398 /* pass */
1399 }
1400 else {
1401 switch (mode) {
1402 case FO_CURSUP:
1403 lnr = ct->linenr - 1;
1404 break;
1405 case FO_CURSDOWN:
1406 lnr = ct->linenr + 1;
1407 break;
1408 case FO_PAGEUP:
1409 lnr = ct->linenr - 10;
1410 break;
1411 case FO_PAGEDOWN:
1412 lnr = ct->linenr + 10;
1413 break;
1414 }
1415 cnr = ct->charnr;
1416 /* seek for char with lnr en cnr */
1417 ef->pos = 0;
1418 ct = chartransdata;
1419 for (i = 0; i < slen; i++) {
1420 if (ct->linenr == lnr) {
1421 if ((ct->charnr == cnr) || ((ct + 1)->charnr == 0)) {
1422 break;
1423 }
1424 }
1425 else if (ct->linenr > lnr) {
1426 break;
1427 }
1428 ef->pos++;
1429 ct++;
1430 }
1431 }
1432 }
1433
1434 /* cursor first */
1435 if (ef) {
1436 float si, co;
1437
1438 ct = &chartransdata[ef->pos];
1439 si = sinf(ct->rot);
1440 co = cosf(ct->rot);
1441
1442 f = ef->textcurs[0];
1443
1444 f[0] = font_size * (-0.1f * co + ct->xof);
1445 f[1] = font_size * (0.1f * si + ct->yof);
1446
1447 f[2] = font_size * (0.1f * co + ct->xof);
1448 f[3] = font_size * (-0.1f * si + ct->yof);
1449
1450 f[4] = font_size * (0.1f * co + 0.8f * si + ct->xof);
1451 f[5] = font_size * (-0.1f * si + 0.8f * co + ct->yof);
1452
1453 f[6] = font_size * (-0.1f * co + 0.8f * si + ct->xof);
1454 f[7] = font_size * (0.1f * si + 0.8f * co + ct->yof);
1455 }
1456
1457 if (mode == FO_SELCHANGE) {
1458 MEM_freeN(chartransdata);
1459 chartransdata = NULL;
1460 }
1461 else if (mode == FO_EDIT) {
1462 /* make nurbdata */
1463 BKE_nurbList_free(r_nubase);
1464
1465 ct = chartransdata;
1466 for (i = 0; i < slen; i++) {
1467 unsigned int cha = (unsigned int)mem[i];
1468 info = &(custrinfo[i]);
1469
1470 if ((cu->overflow == CU_OVERFLOW_TRUNCATE) && (ob && ob->mode != OB_MODE_EDIT) &&
1471 (info->flag & CU_CHINFO_OVERFLOW)) {
1472 break;
1473 }
1474
1475 if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
1476 cha = towupper(cha);
1477 }
1478
1479 /* Only do that check in case we do have an object, otherwise all materials get erased every
1480 * time that code is called without an object... */
1481 if (ob != NULL && (info->mat_nr > (ob->totcol))) {
1482 // CLOG_ERROR(
1483 // &LOG, "Illegal material index (%d) in text object, setting to 0", info->mat_nr);
1484 info->mat_nr = 0;
1485 }
1486 /* We do not want to see any character for \n or \r */
1487 if (cha != '\n') {
1488 buildchar(cu, r_nubase, cha, info, ct->xof, ct->yof, ct->rot, i, font_size);
1489 }
1490
1491 if ((info->flag & CU_CHINFO_UNDERLINE) && (cha != '\n')) {
1492 float ulwidth, uloverlap = 0.0f;
1493 rctf rect;
1494
1495 if ((i < (slen - 1)) && (mem[i + 1] != '\n') &&
1496 ((mem[i + 1] != ' ') || (custrinfo[i + 1].flag & CU_CHINFO_UNDERLINE)) &&
1497 ((custrinfo[i + 1].flag & CU_CHINFO_WRAP) == 0)) {
1498 uloverlap = xtrax + 0.1f;
1499 }
1500 /* Find the character, the characters has to be in the memory already
1501 * since character checking has been done earlier already. */
1502 che = find_vfont_char(vfd, cha);
1503
1504 twidth = char_width(cu, che, info);
1505 ulwidth = (twidth * (1.0f + (info->kern / 40.0f))) + uloverlap;
1506
1507 rect.xmin = ct->xof;
1508 rect.xmax = rect.xmin + ulwidth;
1509
1510 rect.ymin = ct->yof;
1511 rect.ymax = rect.ymin - cu->ulheight;
1512
1513 build_underline(
1514 cu, r_nubase, &rect, cu->ulpos - 0.05f, ct->rot, i, info->mat_nr, font_size);
1515 }
1516 ct++;
1517 }
1518 }
1519
1520 if (iter_data->status == VFONT_TO_CURVE_SCALE_ONCE) {
1521 /* That means we were in a final run, just exit. */
1522 BLI_assert(cu->overflow == CU_OVERFLOW_SCALE);
1523 iter_data->status = VFONT_TO_CURVE_DONE;
1524 }
1525 else if (cu->overflow == CU_OVERFLOW_NONE) {
1526 /* Do nothing. */
1527 }
1528 else if ((tb_scale.h == 0.0f) && (tb_scale.w == 0.0f)) {
1529 /* Do nothing. */
1530 }
1531 else if (cu->overflow == CU_OVERFLOW_SCALE) {
1532 if ((cu->totbox == 1) && ((tb_scale.w == 0.0f) || (tb_scale.h == 0.0f))) {
1533 /* These are special cases, simpler to deal with. */
1534 if (tb_scale.w == 0.0f) {
1535 /* This is a potential vertical overflow.
1536 * Since there is no width limit, all the new lines are from line breaks. */
1537 if ((last_line != -1) && (lnr > last_line)) {
1538 const float total_text_height = lnr * linedist;
1539 iter_data->scale_to_fit = tb_scale.h / total_text_height;
1540 iter_data->status = VFONT_TO_CURVE_SCALE_ONCE;
1541 }
1542 }
1543 else if (tb_scale.h == 0.0f) {
1544 /* This is a horizontal overflow. */
1545 if (longest_line_length > tb_scale.w) {
1546 /* We make sure longest line before it broke can fit here. */
1547 float scale_to_fit = tb_scale.w / longest_line_length;
1548 scale_to_fit -= FLT_EPSILON;
1549
1550 iter_data->scale_to_fit = scale_to_fit;
1551 iter_data->status = VFONT_TO_CURVE_SCALE_ONCE;
1552 }
1553 }
1554 }
1555 else {
1556 /* This is the really complicated case, the best we can do is to iterate over
1557 * this function a few times until we get an acceptable result.
1558 *
1559 * Keep in mind that there is no single number that will make all fit to the end.
1560 * In a way, our ultimate goal is to get the highest scale that still leads to the
1561 * number of extra lines to zero.
1562 */
1563 if (iter_data->status == VFONT_TO_CURVE_INIT) {
1564 bool valid = true;
1565
1566 for (int tb_index = 0; tb_index <= curbox; tb_index++) {
1567 TextBox *tb = &cu->tb[tb_index];
1568 if ((tb->w == 0.0f) || (tb->h == 0.0f)) {
1569 valid = false;
1570 break;
1571 }
1572 }
1573
1574 if (valid && (last_line != -1) && (lnr > last_line)) {
1575 const float total_text_height = lnr * linedist;
1576 float scale_to_fit = tb_scale.h / total_text_height;
1577
1578 iter_data->bisect.max = 1.0f;
1579 iter_data->bisect.min = scale_to_fit;
1580
1581 iter_data->status = VFONT_TO_CURVE_BISECT;
1582 }
1583 }
1584 else {
1585 BLI_assert(iter_data->status == VFONT_TO_CURVE_BISECT);
1586 /* Try to get the highest scale that gives us the exactly
1587 * number of lines we need. */
1588 bool valid = false;
1589
1590 if ((last_line != -1) && (lnr > last_line)) {
1591 /* It is overflowing, scale it down. */
1592 iter_data->bisect.max = iter_data->scale_to_fit;
1593 }
1594 else {
1595 /* It fits inside the textbox, scale it up. */
1596 iter_data->bisect.min = iter_data->scale_to_fit;
1597 valid = true;
1598 }
1599
1600 /* Bisecting to try to find the best fit. */
1601 iter_data->scale_to_fit = (iter_data->bisect.max + iter_data->bisect.min) * 0.5f;
1602
1603 /* We iterated enough or got a good enough result. */
1604 if ((!iter_data->iteraction--) || ((iter_data->bisect.max - iter_data->bisect.min) <
1605 (cu->fsize * FONT_TO_CURVE_SCALE_THRESHOLD))) {
1606 if (valid) {
1607 iter_data->status = VFONT_TO_CURVE_DONE;
1608 }
1609 else {
1610 iter_data->scale_to_fit = iter_data->bisect.min;
1611 iter_data->status = VFONT_TO_CURVE_SCALE_ONCE;
1612 }
1613 }
1614 }
1615 }
1616 }
1617
1618 /* Scale to fit only works for single text box layouts. */
1619 if (ELEM(iter_data->status, VFONT_TO_CURVE_SCALE_ONCE, VFONT_TO_CURVE_BISECT)) {
1620 /* Always cleanup before going to the scale-to-fit repetition. */
1621 if (r_nubase != NULL) {
1622 BKE_nurbList_free(r_nubase);
1623 }
1624
1625 if (chartransdata != NULL) {
1626 MEM_freeN(chartransdata);
1627 }
1628
1629 if (ef == NULL) {
1630 MEM_freeN((void *)mem);
1631 }
1632 return true;
1633 }
1634
1635 ok = true;
1636 finally:
1637 if (r_text) {
1638 *r_text = mem;
1639 *r_text_len = slen;
1640 *r_text_free = (ef == NULL);
1641 }
1642 else {
1643 if (ef == NULL) {
1644 MEM_freeN((void *)mem);
1645 }
1646 }
1647
1648 if (chartransdata) {
1649 if (ok && r_chartransdata) {
1650 *r_chartransdata = chartransdata;
1651 }
1652 else {
1653 MEM_freeN(chartransdata);
1654 }
1655 }
1656
1657 /* Store the effective scale, to use for the textbox lines. */
1658 cu->fsize_realtime = font_size;
1659
1660 return ok;
1661
1662 #undef MARGIN_X_MIN
1663 #undef MARGIN_Y_MIN
1664 }
1665
1666 #undef DESCENT
1667 #undef ASCENT
1668
BKE_vfont_to_curve_ex(Object * ob,Curve * cu,int mode,ListBase * r_nubase,const char32_t ** r_text,int * r_text_len,bool * r_text_free,struct CharTrans ** r_chartransdata)1669 bool BKE_vfont_to_curve_ex(Object *ob,
1670 Curve *cu,
1671 int mode,
1672 ListBase *r_nubase,
1673 const char32_t **r_text,
1674 int *r_text_len,
1675 bool *r_text_free,
1676 struct CharTrans **r_chartransdata)
1677 {
1678 VFontToCurveIter data = {
1679 .iteraction = cu->totbox * FONT_TO_CURVE_SCALE_ITERATIONS,
1680 .scale_to_fit = 1.0f,
1681 .ok = true,
1682 .status = VFONT_TO_CURVE_INIT,
1683 };
1684
1685 do {
1686 data.ok &= vfont_to_curve(
1687 ob, cu, mode, &data, r_nubase, r_text, r_text_len, r_text_free, r_chartransdata);
1688 } while (data.ok && ELEM(data.status, VFONT_TO_CURVE_SCALE_ONCE, VFONT_TO_CURVE_BISECT));
1689
1690 return data.ok;
1691 }
1692
1693 #undef FONT_TO_CURVE_SCALE_ITERATIONS
1694 #undef FONT_TO_CURVE_SCALE_THRESHOLD
1695
BKE_vfont_to_curve_nubase(Object * ob,int mode,ListBase * r_nubase)1696 bool BKE_vfont_to_curve_nubase(Object *ob, int mode, ListBase *r_nubase)
1697 {
1698 BLI_assert(ob->type == OB_FONT);
1699
1700 return BKE_vfont_to_curve_ex(ob, ob->data, mode, r_nubase, NULL, NULL, NULL, NULL);
1701 }
1702
1703 /**
1704 * Warning: expects to have access to evaluated data
1705 * (i.e. passed object should be evaluated one...).
1706 */
BKE_vfont_to_curve(Object * ob,int mode)1707 bool BKE_vfont_to_curve(Object *ob, int mode)
1708 {
1709 Curve *cu = ob->data;
1710
1711 return BKE_vfont_to_curve_ex(ob, ob->data, mode, &cu->nurb, NULL, NULL, NULL, NULL);
1712 }
1713
1714 /* -------------------------------------------------------------------- */
1715 /** \name VFont Clipboard
1716 * \{ */
1717
1718 static struct {
1719 char32_t *text_buffer;
1720 CharInfo *info_buffer;
1721 size_t len_utf32;
1722 size_t len_utf8;
1723 } g_vfont_clipboard = {NULL};
1724
BKE_vfont_clipboard_free(void)1725 void BKE_vfont_clipboard_free(void)
1726 {
1727 MEM_SAFE_FREE(g_vfont_clipboard.text_buffer);
1728 MEM_SAFE_FREE(g_vfont_clipboard.info_buffer);
1729 g_vfont_clipboard.len_utf32 = 0;
1730 g_vfont_clipboard.len_utf8 = 0;
1731 }
1732
BKE_vfont_clipboard_set(const char32_t * text_buf,const CharInfo * info_buf,const size_t len)1733 void BKE_vfont_clipboard_set(const char32_t *text_buf, const CharInfo *info_buf, const size_t len)
1734 {
1735 char32_t *text;
1736 CharInfo *info;
1737
1738 /* clean previous buffers*/
1739 BKE_vfont_clipboard_free();
1740
1741 text = MEM_malloc_arrayN((len + 1), sizeof(*text), __func__);
1742 if (text == NULL) {
1743 return;
1744 }
1745
1746 info = MEM_malloc_arrayN(len, sizeof(CharInfo), __func__);
1747 if (info == NULL) {
1748 MEM_freeN(text);
1749 return;
1750 }
1751
1752 memcpy(text, text_buf, len * sizeof(*text));
1753 text[len] = '\0';
1754 memcpy(info, info_buf, len * sizeof(CharInfo));
1755
1756 /* store new buffers */
1757 g_vfont_clipboard.text_buffer = text;
1758 g_vfont_clipboard.info_buffer = info;
1759 g_vfont_clipboard.len_utf8 = BLI_str_utf32_as_utf8_len(text);
1760 g_vfont_clipboard.len_utf32 = len;
1761 }
1762
BKE_vfont_clipboard_get(char32_t ** r_text_buf,CharInfo ** r_info_buf,size_t * r_len_utf8,size_t * r_len_utf32)1763 void BKE_vfont_clipboard_get(char32_t **r_text_buf,
1764 CharInfo **r_info_buf,
1765 size_t *r_len_utf8,
1766 size_t *r_len_utf32)
1767 {
1768 if (r_text_buf) {
1769 *r_text_buf = g_vfont_clipboard.text_buffer;
1770 }
1771
1772 if (r_info_buf) {
1773 *r_info_buf = g_vfont_clipboard.info_buffer;
1774 }
1775
1776 if (r_len_utf32) {
1777 *r_len_utf32 = g_vfont_clipboard.len_utf32;
1778 }
1779
1780 if (r_len_utf8) {
1781 *r_len_utf8 = g_vfont_clipboard.len_utf8;
1782 }
1783 }
1784
1785 /** \} */
1786