Merge branch 'master' into blender2.8
[blender.git] / source / blender / blenkernel / intern / font.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/font.c
29  *  \ingroup bke
30  */
31
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <math.h>
36 #include <stdlib.h>
37 #include <wchar.h>
38 #include <wctype.h>
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_utildefines.h"
43 #include "BLI_path_util.h"
44 #include "BLI_listbase.h"
45 #include "BLI_ghash.h"
46 #include "BLI_string.h"
47 #include "BLI_string_utf8.h"
48 #include "BLI_math.h"
49 #include "BLI_threads.h"
50 #include "BLI_vfontdata.h"
51
52 #include "DNA_packedFile_types.h"
53 #include "DNA_curve_types.h"
54 #include "DNA_vfont_types.h"
55 #include "DNA_object_types.h"
56
57 #include "BKE_packedFile.h"
58 #include "BKE_library.h"
59 #include "BKE_font.h"
60 #include "BKE_global.h"
61 #include "BKE_main.h"
62 #include "BKE_anim.h"
63 #include "BKE_curve.h"
64
65 static ThreadRWMutex vfont_rwlock = BLI_RWLOCK_INITIALIZER;
66
67 /* The vfont code */
68 void BKE_vfont_free_data(struct VFont *vfont)
69 {
70         if (vfont->data) {
71                 if (vfont->data->characters) {
72                         GHashIterator gh_iter;
73                         GHASH_ITER (gh_iter, vfont->data->characters) {
74                                 VChar *che = BLI_ghashIterator_getValue(&gh_iter);
75
76                                 while (che->nurbsbase.first) {
77                                         Nurb *nu = che->nurbsbase.first;
78                                         if (nu->bezt) MEM_freeN(nu->bezt);
79                                         BLI_freelinkN(&che->nurbsbase, nu);
80                                 }
81
82                                 MEM_freeN(che);
83                         }
84
85                         BLI_ghash_free(vfont->data->characters, NULL, NULL);
86                 }
87
88                 MEM_freeN(vfont->data);
89                 vfont->data = NULL;
90         }
91
92         if (vfont->temp_pf) {
93                 freePackedFile(vfont->temp_pf);  /* NULL when the font file can't be found on disk */
94                 vfont->temp_pf = NULL;
95         }
96 }
97
98 /** Free (or release) any data used by this font (does not free the font itself). */
99 void BKE_vfont_free(struct VFont *vf)
100 {
101         BKE_vfont_free_data(vf);
102
103         if (vf->packedfile) {
104                 freePackedFile(vf->packedfile);
105                 vf->packedfile = NULL;
106         }
107 }
108
109 void BKE_vfont_copy_data(Main *UNUSED(bmain), VFont *vfont_dst, const VFont *UNUSED(vfont_src), const int flag)
110 {
111         /* We never handle usercount here for own data. */
112         const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT;
113
114         /* Just to be sure, should not have any value actually after reading time. */
115         vfont_dst->temp_pf = NULL;
116
117         if (vfont_dst->packedfile) {
118                 vfont_dst->packedfile = dupPackedFile(vfont_dst->packedfile);
119         }
120
121         if (vfont_dst->data) {
122                 vfont_dst->data = BLI_vfontdata_copy(vfont_dst->data, flag_subdata);
123         }
124 }
125
126 static void *builtin_font_data = NULL;
127 static int builtin_font_size = 0;
128
129 bool BKE_vfont_is_builtin(struct VFont *vfont)
130 {
131         return STREQ(vfont->name, FO_BUILTIN_NAME);
132 }
133
134 void BKE_vfont_builtin_register(void *mem, int size)
135 {
136         builtin_font_data = mem;
137         builtin_font_size = size;
138 }
139
140 static PackedFile *get_builtin_packedfile(void)
141 {
142         if (!builtin_font_data) {
143                 printf("Internal error, builtin font not loaded\n");
144
145                 return NULL;
146         }
147         else {
148                 void *mem = MEM_mallocN(builtin_font_size, "vfd_builtin");
149
150                 memcpy(mem, builtin_font_data, builtin_font_size);
151
152                 return newPackedFileMemory(mem, builtin_font_size);
153         }
154 }
155
156 static VFontData *vfont_get_data(VFont *vfont)
157 {
158         if (vfont == NULL) {
159                 return NULL;
160         }
161
162         /* And then set the data */
163         if (!vfont->data) {
164                 PackedFile *pf;
165
166                 BLI_rw_mutex_lock(&vfont_rwlock, THREAD_LOCK_WRITE);
167
168                 if (vfont->data) {
169                         /* Check data again, since it might have been already
170                          * initialized from other thread (previous check is
171                          * not accurate or threading, just prevents unneeded
172                          * lock if all the data is here for sure).
173                          */
174                         BLI_rw_mutex_unlock(&vfont_rwlock);
175                         return vfont->data;
176                 }
177
178                 if (BKE_vfont_is_builtin(vfont)) {
179                         pf = get_builtin_packedfile();
180                 }
181                 else {
182                         if (vfont->packedfile) {
183                                 pf = vfont->packedfile;
184
185                                 /* We need to copy a tmp font to memory unless it is already there */
186                                 if (vfont->temp_pf == NULL) {
187                                         vfont->temp_pf = dupPackedFile(pf);
188                                 }
189                         }
190                         else {
191                                 pf = newPackedFile(NULL, vfont->name, ID_BLEND_PATH_FROM_GLOBAL(&vfont->id));
192
193                                 if (vfont->temp_pf == NULL) {
194                                         vfont->temp_pf = newPackedFile(NULL, vfont->name, ID_BLEND_PATH_FROM_GLOBAL(&vfont->id));
195                                 }
196                         }
197                         if (!pf) {
198                                 printf("Font file doesn't exist: %s\n", vfont->name);
199
200                                 /* DON'T DO THIS
201                                  * missing file shouldn't modify path! - campbell */
202 #if 0
203                                 strcpy(vfont->name, FO_BUILTIN_NAME);
204 #endif
205                                 pf = get_builtin_packedfile();
206                         }
207                 }
208
209                 if (pf) {
210                         vfont->data = BLI_vfontdata_from_freetypefont(pf);
211                         if (pf != vfont->packedfile) {
212                                 freePackedFile(pf);
213                         }
214                 }
215
216                 BLI_rw_mutex_unlock(&vfont_rwlock);
217         }
218
219         return vfont->data;
220 }
221
222 /* Bad naming actually in this case... */
223 void BKE_vfont_init(VFont *vfont)
224 {
225         PackedFile *pf = get_builtin_packedfile();
226
227         if (pf) {
228                 VFontData *vfd;
229
230                 vfd = BLI_vfontdata_from_freetypefont(pf);
231                 if (vfd) {
232                         vfont->data = vfd;
233
234                         BLI_strncpy(vfont->name, FO_BUILTIN_NAME, sizeof(vfont->name));
235                 }
236
237                 /* Free the packed file */
238                 freePackedFile(pf);
239         }
240 }
241
242 VFont *BKE_vfont_load(Main *bmain, const char *filepath)
243 {
244         char filename[FILE_MAXFILE];
245         VFont *vfont = NULL;
246         PackedFile *pf;
247         PackedFile *temp_pf = NULL;
248         bool is_builtin;
249
250         if (STREQ(filepath, FO_BUILTIN_NAME)) {
251                 BLI_strncpy(filename, filepath, sizeof(filename));
252
253                 pf = get_builtin_packedfile();
254                 is_builtin = true;
255         }
256         else {
257                 BLI_split_file_part(filepath, filename, sizeof(filename));
258                 pf = newPackedFile(NULL, filepath, BKE_main_blendfile_path(bmain));
259                 temp_pf = newPackedFile(NULL, filepath, BKE_main_blendfile_path(bmain));
260
261                 is_builtin = false;
262         }
263
264         if (pf) {
265                 VFontData *vfd;
266
267                 vfd = BLI_vfontdata_from_freetypefont(pf);
268                 if (vfd) {
269                         vfont = BKE_libblock_alloc(bmain, ID_VF, filename, 0);
270                         vfont->data = vfd;
271
272                         /* if there's a font name, use it for the ID name */
273                         if (vfd->name[0] != '\0') {
274                                 BLI_strncpy(vfont->id.name + 2, vfd->name, sizeof(vfont->id.name) - 2);
275                         }
276                         BLI_strncpy(vfont->name, filepath, sizeof(vfont->name));
277
278                         /* if autopack is on store the packedfile in de font structure */
279                         if (!is_builtin && (G.fileflags & G_AUTOPACK)) {
280                                 vfont->packedfile = pf;
281                         }
282
283                         /* Do not add FO_BUILTIN_NAME to temporary listbase */
284                         if (!STREQ(filename, FO_BUILTIN_NAME)) {
285                                 vfont->temp_pf = temp_pf;
286                         }
287                 }
288
289                 /* Free the packed file */
290                 if (!vfont || vfont->packedfile != pf) {
291                         freePackedFile(pf);
292                 }
293         }
294
295         return vfont;
296 }
297
298 VFont *BKE_vfont_load_exists_ex(struct Main *bmain, const char *filepath, bool *r_exists)
299 {
300         VFont *vfont;
301         char str[FILE_MAX], strtest[FILE_MAX];
302
303         BLI_strncpy(str, filepath, sizeof(str));
304         BLI_path_abs(str, BKE_main_blendfile_path(bmain));
305
306         /* first search an identical filepath */
307         for (vfont = bmain->vfont.first; vfont; vfont = vfont->id.next) {
308                 BLI_strncpy(strtest, vfont->name, sizeof(vfont->name));
309                 BLI_path_abs(strtest, ID_BLEND_PATH(bmain, &vfont->id));
310
311                 if (BLI_path_cmp(strtest, str) == 0) {
312                         id_us_plus(&vfont->id);  /* officially should not, it doesn't link here! */
313                         if (r_exists)
314                                 *r_exists = true;
315                         return vfont;
316                 }
317         }
318
319         if (r_exists)
320                 *r_exists = false;
321         return BKE_vfont_load(bmain, filepath);
322 }
323
324 VFont *BKE_vfont_load_exists(struct Main *bmain, const char *filepath)
325 {
326         return BKE_vfont_load_exists_ex(bmain, filepath, NULL);
327 }
328
329 void BKE_vfont_make_local(Main *bmain, VFont *vfont, const bool lib_local)
330 {
331         BKE_id_make_local_generic(bmain, &vfont->id, true, lib_local);
332 }
333
334 static VFont *which_vfont(Curve *cu, CharInfo *info)
335 {
336         switch (info->flag & (CU_CHINFO_BOLD | CU_CHINFO_ITALIC)) {
337                 case CU_CHINFO_BOLD:
338                         return cu->vfontb ? cu->vfontb : cu->vfont;
339                 case CU_CHINFO_ITALIC:
340                         return cu->vfonti ? cu->vfonti : cu->vfont;
341                 case (CU_CHINFO_BOLD | CU_CHINFO_ITALIC):
342                         return cu->vfontbi ? cu->vfontbi : cu->vfont;
343                 default:
344                         return cu->vfont;
345         }
346 }
347
348 VFont *BKE_vfont_builtin_get(void)
349 {
350         VFont *vfont;
351
352         for (vfont = G_MAIN->vfont.first; vfont; vfont = vfont->id.next) {
353                 if (BKE_vfont_is_builtin(vfont)) {
354                         return vfont;
355                 }
356         }
357
358         return BKE_vfont_load(G_MAIN, FO_BUILTIN_NAME);
359 }
360
361 static VChar *find_vfont_char(VFontData *vfd, unsigned int character)
362 {
363         return BLI_ghash_lookup(vfd->characters, POINTER_FROM_UINT(character));
364 }
365
366 static void build_underline(Curve *cu, ListBase *nubase, const rctf *rect,
367                             float yofs, float rot, int charidx, short mat_nr)
368 {
369         Nurb *nu2;
370         BPoint *bp;
371
372         nu2 = (Nurb *) MEM_callocN(sizeof(Nurb), "underline_nurb");
373         nu2->resolu = cu->resolu;
374         nu2->bezt = NULL;
375         nu2->knotsu = nu2->knotsv = NULL;
376         nu2->flag = CU_2D;
377         nu2->charidx = charidx + 1000;
378         if (mat_nr > 0) nu2->mat_nr = mat_nr - 1;
379         nu2->pntsu = 4;
380         nu2->pntsv = 1;
381         nu2->orderu = 4;
382         nu2->orderv = 1;
383         nu2->flagu = CU_NURB_CYCLIC;
384
385         bp = (BPoint *)MEM_calloc_arrayN(4, sizeof(BPoint), "underline_bp");
386
387         copy_v4_fl4(bp[0].vec, rect->xmin, (rect->ymax + yofs), 0.0f, 1.0f);
388         copy_v4_fl4(bp[1].vec, rect->xmax, (rect->ymax + yofs), 0.0f, 1.0f);
389         copy_v4_fl4(bp[2].vec, rect->xmax, (rect->ymin + yofs), 0.0f, 1.0f);
390         copy_v4_fl4(bp[3].vec, rect->xmin, (rect->ymin + yofs), 0.0f, 1.0f);
391
392         nu2->bp = bp;
393         BLI_addtail(nubase, nu2);
394
395         if (rot != 0.0f) {
396                 float si, co;
397                 int i;
398
399                 si = sinf(rot);
400                 co = cosf(rot);
401
402                 for (i = nu2->pntsu; i > 0; i--) {
403                         float *fp;
404                         float x, y;
405
406                         fp = bp->vec;
407
408                         x = fp[0] - rect->xmin;
409                         y = fp[1] - rect->ymin;
410
411                         fp[0] = (+co * x + si * y) + rect->xmin;
412                         fp[1] = (-si * x + co * y) + rect->ymin;
413
414                         bp++;
415                 }
416
417                 bp = nu2->bp;
418         }
419
420         mul_v2_fl(bp[0].vec, cu->fsize);
421         mul_v2_fl(bp[1].vec, cu->fsize);
422         mul_v2_fl(bp[2].vec, cu->fsize);
423         mul_v2_fl(bp[3].vec, cu->fsize);
424 }
425
426 static void buildchar(Curve *cu, ListBase *nubase, unsigned int character, CharInfo *info,
427                       float ofsx, float ofsy, float rot, int charidx)
428 {
429         BezTriple *bezt1, *bezt2;
430         Nurb *nu1 = NULL, *nu2 = NULL;
431         float *fp, fsize, shear, x, si, co;
432         VFontData *vfd = NULL;
433         VChar *che = NULL;
434         int i;
435
436         vfd = vfont_get_data(which_vfont(cu, info));
437         if (!vfd) return;
438
439 #if 0
440         if (cu->selend < cu->selstart) {
441                 if ((charidx >= (cu->selend)) && (charidx <= (cu->selstart - 2)))
442                         sel = 1;
443         }
444         else {
445                 if ((charidx >= (cu->selstart - 1)) && (charidx <= (cu->selend - 1)))
446                         sel = 1;
447         }
448 #endif
449
450         /* make a copy at distance ofsx, ofsy with shear */
451         fsize = cu->fsize;
452         shear = cu->shear;
453         si = sinf(rot);
454         co = cosf(rot);
455
456         che = find_vfont_char(vfd, character);
457
458         /* Select the glyph data */
459         if (che)
460                 nu1 = che->nurbsbase.first;
461
462         /* Create the character */
463         while (nu1) {
464                 bezt1 = nu1->bezt;
465                 if (bezt1) {
466                         nu2 = (Nurb *) MEM_mallocN(sizeof(Nurb), "duplichar_nurb");
467                         if (nu2 == NULL) break;
468                         memcpy(nu2, nu1, sizeof(struct Nurb));
469                         nu2->resolu = cu->resolu;
470                         nu2->bp = NULL;
471                         nu2->knotsu = nu2->knotsv = NULL;
472                         nu2->flag = CU_SMOOTH;
473                         nu2->charidx = charidx;
474                         if (info->mat_nr > 0) {
475                                 nu2->mat_nr = info->mat_nr - 1;
476                         }
477                         else {
478                                 nu2->mat_nr = 0;
479                         }
480                         /* nu2->trim.first = 0; */
481                         /* nu2->trim.last = 0; */
482                         i = nu2->pntsu;
483
484                         bezt2 = (BezTriple *)MEM_malloc_arrayN(i, sizeof(BezTriple), "duplichar_bezt2");
485                         if (bezt2 == NULL) {
486                                 MEM_freeN(nu2);
487                                 break;
488                         }
489                         memcpy(bezt2, bezt1, i * sizeof(struct BezTriple));
490                         nu2->bezt = bezt2;
491
492                         if (shear != 0.0f) {
493                                 bezt2 = nu2->bezt;
494
495                                 for (i = nu2->pntsu; i > 0; i--) {
496                                         bezt2->vec[0][0] += shear * bezt2->vec[0][1];
497                                         bezt2->vec[1][0] += shear * bezt2->vec[1][1];
498                                         bezt2->vec[2][0] += shear * bezt2->vec[2][1];
499                                         bezt2++;
500                                 }
501                         }
502                         if (rot != 0.0f) {
503                                 bezt2 = nu2->bezt;
504                                 for (i = nu2->pntsu; i > 0; i--) {
505                                         fp = bezt2->vec[0];
506
507                                         x = fp[0];
508                                         fp[0] = co * x + si * fp[1];
509                                         fp[1] = -si * x + co * fp[1];
510                                         x = fp[3];
511                                         fp[3] = co * x + si * fp[4];
512                                         fp[4] = -si * x + co * fp[4];
513                                         x = fp[6];
514                                         fp[6] = co * x + si * fp[7];
515                                         fp[7] = -si * x + co * fp[7];
516
517                                         bezt2++;
518                                 }
519                         }
520                         bezt2 = nu2->bezt;
521
522                         if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
523                                 const float sca = cu->smallcaps_scale;
524                                 for (i = nu2->pntsu; i > 0; i--) {
525                                         fp = bezt2->vec[0];
526                                         fp[0] *= sca;
527                                         fp[1] *= sca;
528                                         fp[3] *= sca;
529                                         fp[4] *= sca;
530                                         fp[6] *= sca;
531                                         fp[7] *= sca;
532                                         bezt2++;
533                                 }
534                         }
535                         bezt2 = nu2->bezt;
536
537                         for (i = nu2->pntsu; i > 0; i--) {
538                                 fp = bezt2->vec[0];
539                                 fp[0] = (fp[0] + ofsx) * fsize;
540                                 fp[1] = (fp[1] + ofsy) * fsize;
541                                 fp[3] = (fp[3] + ofsx) * fsize;
542                                 fp[4] = (fp[4] + ofsy) * fsize;
543                                 fp[6] = (fp[6] + ofsx) * fsize;
544                                 fp[7] = (fp[7] + ofsy) * fsize;
545                                 bezt2++;
546                         }
547
548                         BLI_addtail(nubase, nu2);
549                 }
550
551                 nu1 = nu1->next;
552         }
553 }
554
555 int BKE_vfont_select_get(Object *ob, int *r_start, int *r_end)
556 {
557         Curve *cu = ob->data;
558         EditFont *ef = cu->editfont;
559         int start, end, direction;
560
561         if ((ob->type != OB_FONT) || (ef == NULL)) return 0;
562
563         BLI_assert(ef->len >= 0);
564         BLI_assert(ef->selstart >= 0 && ef->selstart <= ef->len + 1);
565         BLI_assert(ef->selend   >= 0 && ef->selend   <= ef->len + 1);
566         BLI_assert(ef->pos      >= 0 && ef->pos      <= ef->len);
567
568         if (ef->selstart == 0) {
569                 return 0;
570         }
571
572         if (ef->selstart <= ef->selend) {
573                 start = ef->selstart - 1;
574                 end = ef->selend - 1;
575                 direction = 1;
576         }
577         else {
578                 start = ef->selend;
579                 end = ef->selstart - 2;
580                 direction = -1;
581         }
582
583         if (start == end + 1) {
584                 return 0;
585         }
586         else {
587                 BLI_assert(start < end + 1);
588                 *r_start = start;
589                 *r_end = end;
590                 return direction;
591         }
592 }
593
594 void BKE_vfont_select_clamp(Object *ob)
595 {
596         Curve *cu = ob->data;
597         EditFont *ef = cu->editfont;
598
599         BLI_assert((ob->type == OB_FONT) && ef);
600
601         CLAMP_MAX(ef->pos,      ef->len);
602         CLAMP_MAX(ef->selstart, ef->len + 1);
603         CLAMP_MAX(ef->selend,   ef->len);
604 }
605
606 static float char_width(Curve *cu, VChar *che, CharInfo *info)
607 {
608         /* The character wasn't found, probably ascii = 0, then the width shall be 0 as well */
609         if (che == NULL) {
610                 return 0.0f;
611         }
612         else if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
613                 return che->width * cu->smallcaps_scale;
614         }
615         else {
616                 return che->width;
617         }
618 }
619
620 static void textbox_scale(TextBox *tb_dst, const TextBox *tb_src, float scale)
621 {
622         tb_dst->x = tb_src->x * scale;
623         tb_dst->y = tb_src->y * scale;
624         tb_dst->w = tb_src->w * scale;
625         tb_dst->h = tb_src->h * scale;
626 }
627
628 /**
629  * Used for storing per-line data for alignment & wrapping.
630  */
631 struct TempLineInfo {
632         float x_min;      /* left margin */
633         float x_max;      /* right margin */
634         int   char_nr;    /* number of characters */
635         int   wspace_nr;  /* number of whitespaces of line */
636 };
637
638 /**
639  * Font metric values explained:
640  *
641  * Baseline: Line where the text "rests", used as the origin vertical position for the glyphs.
642  * Em height: Space most glyphs should fit within.
643  * Ascent: the recommended distance above the baseline to fit most characters.
644  * Descent: the recommended distance below the baseline to fit most characters.
645  *
646  * We obtain ascent and descent from the font itself (FT_Face->ascender / face->height).
647  * And in some cases it is even the same value as FT_Face->bbox.yMax/yMin (font top and bottom respectively).
648  *
649  * The em_height here is relative to FT_Face->bbox.
650 */
651 #define ASCENT(vfd) ((vfd)->ascender * (vfd)->em_height)
652 #define DESCENT(vfd) ((vfd)->em_height - ASCENT(vfd))
653
654 bool BKE_vfont_to_curve_ex(Object *ob, Curve *cu, int mode, ListBase *r_nubase,
655                            const wchar_t **r_text, int *r_text_len, bool *r_text_free,
656                            struct CharTrans **r_chartransdata)
657 {
658         EditFont *ef = cu->editfont;
659         EditFontSelBox *selboxes = NULL;
660         VFont *vfont, *oldvfont;
661         VFontData *vfd = NULL;
662         CharInfo *info = NULL, *custrinfo;
663         TextBox tb_scale;
664         bool use_textbox;
665         VChar *che;
666         struct CharTrans *chartransdata = NULL, *ct;
667         struct TempLineInfo *lineinfo;
668         float *f, xof, yof, xtrax, linedist;
669         float twidth, maxlen = 0;
670         int i, slen, j;
671         int curbox;
672         int selstart, selend;
673         int cnr = 0, lnr = 0, wsnr = 0;
674         const wchar_t *mem;
675         wchar_t ascii;
676         bool ok = false;
677         const float xof_scale = cu->xof / cu->fsize;
678         const float yof_scale = cu->yof / cu->fsize;
679
680         /* Text at the beginning of the last used text-box (use for y-axis alignment).
681          * We overallocate by one to simplify logic of getting last char. */
682         int *i_textbox_array = MEM_callocN(sizeof(*i_textbox_array) * (cu->totbox + 1), "TextBox initial char index");
683
684 #define MARGIN_X_MIN (xof_scale + tb_scale.x)
685 #define MARGIN_Y_MIN (yof_scale + tb_scale.y)
686
687         /* remark: do calculations including the trailing '\0' of a string
688          * because the cursor can be at that location */
689
690         BLI_assert(ob == NULL || ob->type == OB_FONT);
691
692         /* Set font data */
693         vfont = cu->vfont;
694
695         if (cu->str == NULL) return ok;
696         if (vfont == NULL) return ok;
697
698         vfd = vfont_get_data(vfont);
699
700         /* The VFont Data can not be found */
701         if (!vfd) return ok;
702
703         if (ef) {
704                 slen = ef->len;
705                 mem = ef->textbuf;
706                 custrinfo = ef->textbufinfo;
707         }
708         else {
709                 wchar_t *mem_tmp;
710                 slen = cu->len_wchar;
711
712                 /* Create unicode string */
713                 mem_tmp = MEM_malloc_arrayN((slen + 1), sizeof(wchar_t), "convertedmem");
714                 if (!mem_tmp) {
715                         return ok;
716                 }
717
718                 BLI_strncpy_wchar_from_utf8(mem_tmp, cu->str, slen + 1);
719
720                 if (cu->strinfo == NULL) {  /* old file */
721                         cu->strinfo = MEM_calloc_arrayN((slen + 4), sizeof(CharInfo), "strinfo compat");
722                 }
723                 custrinfo = cu->strinfo;
724                 if (!custrinfo) {
725                         return ok;
726                 }
727
728                 mem = mem_tmp;
729         }
730
731         if (cu->tb == NULL)
732                 cu->tb = MEM_calloc_arrayN(MAXTEXTBOX, sizeof(TextBox), "TextBox compat");
733
734         if (ef != NULL && ob != NULL) {
735                 if (ef->selboxes)
736                         MEM_freeN(ef->selboxes);
737
738                 if (BKE_vfont_select_get(ob, &selstart, &selend)) {
739                         ef->selboxes_len = (selend - selstart) + 1;
740                         ef->selboxes = MEM_calloc_arrayN(ef->selboxes_len, sizeof(EditFontSelBox), "font selboxes");
741                 }
742                 else {
743                         ef->selboxes_len = 0;
744                         ef->selboxes = NULL;
745                 }
746
747                 selboxes = ef->selboxes;
748         }
749
750         /* calc offset and rotation of each char */
751         ct = chartransdata = MEM_calloc_arrayN((slen + 1), sizeof(struct CharTrans), "buildtext");
752
753         /* We assume the worst case: 1 character per line (is freed at end anyway) */
754         lineinfo = MEM_malloc_arrayN((slen * 2 + 1), sizeof(*lineinfo), "lineinfo");
755
756         linedist = cu->linedist;
757
758         curbox = 0;
759         textbox_scale(&tb_scale, &cu->tb[curbox], 1.0f / cu->fsize);
760         use_textbox = (tb_scale.w != 0.0f);
761
762
763         xof = MARGIN_X_MIN;
764         yof = MARGIN_Y_MIN;
765
766         xtrax = 0.5f * cu->spacing - 0.5f;
767
768         oldvfont = NULL;
769
770         for (i = 0; i < slen; i++) {
771                 custrinfo[i].flag &= ~(CU_CHINFO_WRAP | CU_CHINFO_SMALLCAPS_CHECK);
772         }
773
774         for (i = 0; i <= slen; i++) {
775 makebreak:
776                 /* Characters in the list */
777                 info = &custrinfo[i];
778                 ascii = mem[i];
779                 if (info->flag & CU_CHINFO_SMALLCAPS) {
780                         ascii = towupper(ascii);
781                         if (mem[i] != ascii) {
782                                 info->flag |= CU_CHINFO_SMALLCAPS_CHECK;
783                         }
784                 }
785
786                 vfont = which_vfont(cu, info);
787
788                 if (vfont == NULL) break;
789
790                 if (vfont != oldvfont) {
791                         vfd = vfont_get_data(vfont);
792                         oldvfont = vfont;
793                 }
794
795                 /* VFont Data for VFont couldn't be found */
796                 if (!vfd) {
797                         MEM_freeN(chartransdata);
798                         chartransdata = NULL;
799                         MEM_freeN(lineinfo);
800                         goto finally;
801                 }
802
803                 if (!ELEM(ascii, '\n', '\0')) {
804                         BLI_rw_mutex_lock(&vfont_rwlock, THREAD_LOCK_READ);
805                         che = find_vfont_char(vfd, ascii);
806                         BLI_rw_mutex_unlock(&vfont_rwlock);
807
808                         /*
809                          * The character wasn't in the current curve base so load it
810                          * But if the font is built-in then do not try loading since
811                          * whole font is in the memory already
812                          */
813                         if (che == NULL && BKE_vfont_is_builtin(vfont) == false) {
814                                 BLI_rw_mutex_lock(&vfont_rwlock, THREAD_LOCK_WRITE);
815                                 /* Check it once again, char might have been already load
816                                  * between previous BLI_rw_mutex_unlock() and this BLI_rw_mutex_lock().
817                                  *
818                                  * Such a check should not be a bottleneck since it wouldn't
819                                  * happen often once all the chars are load.
820                                  */
821                                 if ((che = find_vfont_char(vfd, ascii)) == NULL) {
822                                         che = BLI_vfontchar_from_freetypefont(vfont, ascii);
823                                 }
824                                 BLI_rw_mutex_unlock(&vfont_rwlock);
825                         }
826                 }
827                 else {
828                         che = NULL;
829                 }
830
831                 twidth = char_width(cu, che, info);
832
833                 /* Calculate positions */
834                 if ((tb_scale.w != 0.0f) &&
835                     (ct->dobreak == 0) &&
836                     (((xof - tb_scale.x) + twidth) > xof_scale + tb_scale.w))
837                 {
838                         //              fprintf(stderr, "linewidth exceeded: %c%c%c...\n", mem[i], mem[i+1], mem[i+2]);
839                         for (j = i; j && (mem[j] != '\n') && (chartransdata[j].dobreak == 0); j--) {
840                                 if (mem[j] == ' ' || mem[j] == '-') {
841                                         ct -= (i - (j - 1));
842                                         cnr -= (i - (j - 1));
843                                         if (mem[j] == ' ') wsnr--;
844                                         if (mem[j] == '-') wsnr++;
845                                         i = j - 1;
846                                         xof = ct->xof;
847                                         ct[1].dobreak = 1;
848                                         custrinfo[i + 1].flag |= CU_CHINFO_WRAP;
849                                         goto makebreak;
850                                 }
851                                 if (chartransdata[j].dobreak) {
852                                         //                              fprintf(stderr, "word too long: %c%c%c...\n", mem[j], mem[j+1], mem[j+2]);
853                                         ct->dobreak = 1;
854                                         custrinfo[i + 1].flag |= CU_CHINFO_WRAP;
855                                         ct -= 1;
856                                         cnr -= 1;
857                                         i--;
858                                         xof = ct->xof;
859                                         goto makebreak;
860                                 }
861                         }
862                 }
863
864                 if (ascii == '\n' || ascii == 0 || ct->dobreak) {
865                         ct->xof = xof;
866                         ct->yof = yof;
867                         ct->linenr = lnr;
868                         ct->charnr = cnr;
869
870                         yof -= linedist;
871
872                         lineinfo[lnr].x_min     = (xof - xtrax) - tb_scale.x;
873                         lineinfo[lnr].x_max     = tb_scale.w;
874                         lineinfo[lnr].char_nr   = cnr;
875                         lineinfo[lnr].wspace_nr = wsnr;
876
877                         CLAMP_MIN(maxlen, lineinfo[lnr].x_min);
878
879                         if ((tb_scale.h != 0.0f) &&
880                             (cu->totbox > (curbox + 1)) &&
881                             ((-(yof - tb_scale.y)) > (tb_scale.h - linedist) - yof_scale))
882                         {
883                                 maxlen = 0;
884                                 curbox++;
885                                 i_textbox_array[curbox] = i + 1;
886
887                                 textbox_scale(&tb_scale, &cu->tb[curbox], 1.0f / cu->fsize);
888
889                                 yof = MARGIN_Y_MIN;
890                         }
891
892                         /* XXX, has been unused for years, need to check if this is useful, r4613 r5282 - campbell */
893 #if 0
894                         if (ascii == '\n')
895                                 xof = xof_scale;
896                         else
897                                 xof = MARGIN_X_MIN;
898 #else
899                         xof = MARGIN_X_MIN;
900 #endif
901                         lnr++;
902                         cnr = 0;
903                         wsnr = 0;
904                 }
905                 else if (ascii == 9) {    /* TAB */
906                         float tabfac;
907
908                         ct->xof = xof;
909                         ct->yof = yof;
910                         ct->linenr = lnr;
911                         ct->charnr = cnr++;
912
913                         tabfac = (xof - MARGIN_X_MIN + 0.01f);
914                         tabfac = 2.0f * ceilf(tabfac / 2.0f);
915                         xof = MARGIN_X_MIN + tabfac;
916                 }
917                 else {
918                         EditFontSelBox *sb = NULL;
919                         float wsfac;
920
921                         ct->xof = xof;
922                         ct->yof = yof;
923                         ct->linenr = lnr;
924                         ct->charnr = cnr++;
925
926                         if (selboxes && (i >= selstart) && (i <= selend)) {
927                                 sb = &selboxes[i - selstart];
928                                 sb->y = yof * cu->fsize - linedist * cu->fsize * 0.1f;
929                                 sb->h = linedist * cu->fsize;
930                                 sb->w = xof * cu->fsize;
931                         }
932
933                         if (ascii == 32) {
934                                 wsfac = cu->wordspace;
935                                 wsnr++;
936                         }
937                         else {
938                                 wsfac = 1.0f;
939                         }
940
941                         /* Set the width of the character */
942                         twidth = char_width(cu, che, info);
943
944                         xof += (twidth * wsfac * (1.0f + (info->kern / 40.0f)) ) + xtrax;
945
946                         if (sb) {
947                                 sb->w = (xof * cu->fsize) - sb->w;
948                         }
949                 }
950                 ct++;
951         }
952
953         cu->lines = 1;
954         for (i = 0; i <= slen; i++) {
955                 ascii = mem[i];
956                 ct = &chartransdata[i];
957                 if (ascii == '\n' || ct->dobreak) cu->lines++;
958         }
959
960         /* linedata is now: width of line */
961
962         if (cu->spacemode != CU_ALIGN_X_LEFT) {
963                 ct = chartransdata;
964
965                 if (cu->spacemode == CU_ALIGN_X_RIGHT) {
966                         struct TempLineInfo *li;
967
968                         for (i = 0, li = lineinfo; i < lnr; i++, li++) {
969                                 li->x_min = (li->x_max - li->x_min) + xof_scale;
970                         }
971
972                         for (i = 0; i <= slen; i++) {
973                                 ct->xof += lineinfo[ct->linenr].x_min;
974                                 ct++;
975                         }
976                 }
977                 else if (cu->spacemode == CU_ALIGN_X_MIDDLE) {
978                         struct TempLineInfo *li;
979
980                         for (i = 0, li = lineinfo; i < lnr; i++, li++) {
981                                 li->x_min = ((li->x_max - li->x_min) + xof_scale) / 2.0f;
982                         }
983
984                         for (i = 0; i <= slen; i++) {
985                                 ct->xof += lineinfo[ct->linenr].x_min;
986                                 ct++;
987                         }
988                 }
989                 else if ((cu->spacemode == CU_ALIGN_X_FLUSH) && use_textbox) {
990                         struct TempLineInfo *li;
991
992                         for (i = 0, li = lineinfo; i < lnr; i++, li++) {
993                                 li->x_min = ((li->x_max - li->x_min) + xof_scale);
994
995                                 if (li->char_nr > 1) {
996                                         li->x_min /= (float)(li->char_nr - 1);
997                                 }
998                         }
999                         for (i = 0; i <= slen; i++) {
1000                                 for (j = i; (!ELEM(mem[j], '\0', '\n')) && (chartransdata[j].dobreak == 0) && (j < slen); j++) {
1001                                         /* do nothing */
1002                                 }
1003
1004 //                              if ((mem[j] != '\n') && (mem[j])) {
1005                                 ct->xof += ct->charnr * lineinfo[ct->linenr].x_min;
1006 //                              }
1007                                 ct++;
1008                         }
1009                 }
1010                 else if ((cu->spacemode == CU_ALIGN_X_JUSTIFY) && use_textbox) {
1011                         float curofs = 0.0f;
1012                         for (i = 0; i <= slen; i++) {
1013                                 for (j = i;
1014                                      (mem[j]) && (mem[j] != '\n') && (chartransdata[j].dobreak == 0) && (j < slen);
1015                                      j++)
1016                                 {
1017                                         /* pass */
1018                                 }
1019
1020                                 if ((mem[j] != '\n') &&
1021                                     ((chartransdata[j].dobreak != 0)))
1022                                 {
1023                                         if (mem[i] == ' ') {
1024                                                 struct TempLineInfo *li;
1025
1026                                                 li = &lineinfo[ct->linenr];
1027                                                 curofs += ((li->x_max - li->x_min) + xof_scale) / (float)li->wspace_nr;
1028                                         }
1029                                         ct->xof += curofs;
1030                                 }
1031                                 if (mem[i] == '\n' || chartransdata[i].dobreak) curofs = 0;
1032                                 ct++;
1033                         }
1034                 }
1035         }
1036
1037         /* top-baseline is default, in this case, do nothing */
1038         if (cu->align_y != CU_ALIGN_Y_TOP_BASELINE) {
1039                 if (tb_scale.h != 0.0f) {
1040                         /* We need to loop all the text-boxes even the "full" ones.
1041                          * This way they all get the same vertical padding. */
1042                         for (int tb_index = 0; tb_index < cu->totbox; tb_index++) {
1043                                 struct CharTrans *ct_first, *ct_last;
1044                                 const int i_textbox = i_textbox_array[tb_index];
1045                                 const int i_textbox_next = i_textbox_array[tb_index + 1];
1046                                 const bool is_last_filled_textbox = ELEM(i_textbox_next, 0, slen + 1);
1047                                 int lines;
1048
1049                                 ct_first = chartransdata + i_textbox;
1050                                 ct_last = chartransdata + (is_last_filled_textbox ? slen: i_textbox_next - 1);
1051                                 lines = ct_last->linenr - ct_first->linenr + 1;
1052
1053                                 textbox_scale(&tb_scale, &cu->tb[tb_index], 1.0f / cu->fsize);
1054                                 /* The initial Y origin of the textbox is hardcoded to 1.0f * text scale. */
1055                                 const float textbox_y_origin = 1.0f;
1056                                 float yoff = 0.0f;
1057
1058                                 switch (cu->align_y) {
1059                                         case CU_ALIGN_Y_TOP_BASELINE:
1060                                                 break;
1061                                         case CU_ALIGN_Y_TOP:
1062                                                 yoff = textbox_y_origin - ASCENT(vfd);
1063                                                 break;
1064                                         case CU_ALIGN_Y_CENTER:
1065                                                 yoff = ((((vfd->em_height + (lines - 1) * linedist) * 0.5f) - ASCENT(vfd)) -
1066                                                         (tb_scale.h  * 0.5f) + textbox_y_origin);
1067                                                 break;
1068                                         case CU_ALIGN_Y_BOTTOM_BASELINE:
1069                                                 yoff = textbox_y_origin + ((lines - 1) * linedist) - tb_scale.h;
1070                                                 break;
1071                                         case CU_ALIGN_Y_BOTTOM:
1072                                                 yoff = textbox_y_origin + ((lines - 1) * linedist) - tb_scale.h + DESCENT(vfd);
1073                                                 break;
1074                                 }
1075
1076                                 for (ct = ct_first; ct <= ct_last; ct++) {
1077                                         ct->yof += yoff;
1078                                 }
1079
1080                                 if (is_last_filled_textbox) {
1081                                         break;
1082                                 }
1083                         }
1084                 }
1085                 else {
1086                         /* Non text-box case handled separately. */
1087                         float yoff = 0.0f;
1088
1089                         switch (cu->align_y) {
1090                                 case CU_ALIGN_Y_TOP_BASELINE:
1091                                         break;
1092                                 case CU_ALIGN_Y_TOP:
1093                                         yoff = -ASCENT(vfd);
1094                                         break;
1095                                 case CU_ALIGN_Y_CENTER:
1096                                         yoff = ((vfd->em_height + (lnr - 1) * linedist) * 0.5f) - ASCENT(vfd);
1097                                         break;
1098                                 case CU_ALIGN_Y_BOTTOM_BASELINE:
1099                                         yoff = (lnr - 1) * linedist;
1100                                         break;
1101                                 case CU_ALIGN_Y_BOTTOM:
1102                                         yoff = (lnr - 1) * linedist + DESCENT(vfd);
1103                                         break;
1104                         }
1105
1106                         ct = chartransdata;
1107                         for (i = 0; i <= slen; i++) {
1108                                 ct->yof += yoff;
1109                                 ct++;
1110                         }
1111                 }
1112         }
1113
1114         MEM_freeN(lineinfo);
1115         MEM_freeN(i_textbox_array);
1116
1117         /* TEXT ON CURVE */
1118         /* Note: Only OB_CURVE objects could have a path  */
1119         if (cu->textoncurve && cu->textoncurve->type == OB_CURVE) {
1120                 BLI_assert(cu->textoncurve->runtime.curve_cache != NULL);
1121                 if (cu->textoncurve->runtime.curve_cache != NULL && cu->textoncurve->runtime.curve_cache->path != NULL) {
1122                         float distfac, imat[4][4], imat3[3][3], cmat[3][3];
1123                         float minx, maxx, miny, maxy;
1124                         float timeofs, sizefac;
1125
1126                         if (ob != NULL) {
1127                                 invert_m4_m4(imat, ob->obmat);
1128                         }
1129                         else {
1130                                 unit_m4(imat);
1131                         }
1132                         copy_m3_m4(imat3, imat);
1133
1134                         copy_m3_m4(cmat, cu->textoncurve->obmat);
1135                         mul_m3_m3m3(cmat, cmat, imat3);
1136                         sizefac = normalize_v3(cmat[0]) / cu->fsize;
1137
1138                         minx = miny = 1.0e20f;
1139                         maxx = maxy = -1.0e20f;
1140                         ct = chartransdata;
1141                         for (i = 0; i <= slen; i++, ct++) {
1142                                 if (minx > ct->xof) minx = ct->xof;
1143                                 if (maxx < ct->xof) maxx = ct->xof;
1144                                 if (miny > ct->yof) miny = ct->yof;
1145                                 if (maxy < ct->yof) maxy = ct->yof;
1146                         }
1147
1148                         /* we put the x-coordinaat exact at the curve, the y is rotated */
1149
1150                         /* length correction */
1151                         distfac = sizefac * cu->textoncurve->runtime.curve_cache->path->totdist / (maxx - minx);
1152                         timeofs = 0.0f;
1153
1154                         if (distfac > 1.0f) {
1155                                 /* path longer than text: spacemode involves */
1156                                 distfac = 1.0f / distfac;
1157
1158                                 if (cu->spacemode == CU_ALIGN_X_RIGHT) {
1159                                         timeofs = 1.0f - distfac;
1160                                 }
1161                                 else if (cu->spacemode == CU_ALIGN_X_MIDDLE) {
1162                                         timeofs = (1.0f - distfac) / 2.0f;
1163                                 }
1164                                 else if (cu->spacemode == CU_ALIGN_X_FLUSH) {
1165                                         distfac = 1.0f;
1166                                 }
1167                         }
1168                         else {
1169                                 distfac = 1.0;
1170                         }
1171
1172                         distfac /= (maxx - minx);
1173
1174                         timeofs += distfac * cu->xof;  /* not cyclic */
1175
1176                         ct = chartransdata;
1177                         for (i = 0; i < slen; i++, ct++) {
1178                                 float ctime, dtime, vec[4], tvec[4], rotvec[3];
1179                                 float si, co;
1180
1181                                 /* rotate around center character */
1182                                 info = &custrinfo[i];
1183                                 ascii = mem[i];
1184                                 if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
1185                                         ascii = towupper(ascii);
1186                                 }
1187
1188                                 che = find_vfont_char(vfd, ascii);
1189
1190                                 twidth = char_width(cu, che, info);
1191
1192                                 dtime = distfac * 0.5f * twidth;
1193
1194                                 ctime = timeofs + distfac * (ct->xof - minx);
1195                                 CLAMP(ctime, 0.0f, 1.0f);
1196
1197                                 /* calc the right loc AND the right rot separately */
1198                                 /* vec, tvec need 4 items */
1199                                 where_on_path(cu->textoncurve, ctime, vec, tvec, NULL, NULL, NULL);
1200                                 where_on_path(cu->textoncurve, ctime + dtime, tvec, rotvec, NULL, NULL, NULL);
1201
1202                                 mul_v3_fl(vec, sizefac);
1203
1204                                 ct->rot = (float)M_PI - atan2f(rotvec[1], rotvec[0]);
1205
1206                                 si = sinf(ct->rot);
1207                                 co = cosf(ct->rot);
1208
1209                                 yof = ct->yof;
1210
1211                                 ct->xof = vec[0] + si * yof;
1212                                 ct->yof = vec[1] + co * yof;
1213
1214                                 if (selboxes && (i >= selstart) && (i <= selend)) {
1215                                         EditFontSelBox *sb;
1216                                         sb = &selboxes[i - selstart];
1217                                         sb->rot = -ct->rot;
1218                                 }
1219                         }
1220                         /* null character is always zero width, no need to iterate over it */
1221                         chartransdata[slen] = chartransdata[slen - 1];
1222                 }
1223         }
1224
1225         if (selboxes) {
1226                 ct = chartransdata;
1227                 for (i = 0; i <= selend; i++, ct++) {
1228                         if (i >= selstart) {
1229                                 selboxes[i - selstart].x = ct->xof * cu->fsize;
1230                                 selboxes[i - selstart].y = ct->yof * cu->fsize;
1231                         }
1232                 }
1233         }
1234
1235         if (ELEM(mode, FO_CURSUP, FO_CURSDOWN, FO_PAGEUP, FO_PAGEDOWN)) {
1236                 ct = &chartransdata[ef->pos];
1237
1238                 if (ELEM(mode, FO_CURSUP, FO_PAGEUP) && ct->linenr == 0) {
1239                         /* pass */
1240                 }
1241                 else if (ELEM(mode, FO_CURSDOWN, FO_PAGEDOWN) && ct->linenr == lnr) {
1242                         /* pass */
1243                 }
1244                 else {
1245                         switch (mode) {
1246                                 case FO_CURSUP:     lnr = ct->linenr - 1; break;
1247                                 case FO_CURSDOWN:   lnr = ct->linenr + 1; break;
1248                                 case FO_PAGEUP:     lnr = ct->linenr - 10; break;
1249                                 case FO_PAGEDOWN:   lnr = ct->linenr + 10; break;
1250                         }
1251                         cnr = ct->charnr;
1252                         /* seek for char with lnr en cnr */
1253                         ef->pos = 0;
1254                         ct = chartransdata;
1255                         for (i = 0; i < slen; i++) {
1256                                 if (ct->linenr == lnr) {
1257                                         if ((ct->charnr == cnr) || ((ct + 1)->charnr == 0)) {
1258                                                 break;
1259                                         }
1260                                 }
1261                                 else if (ct->linenr > lnr) {
1262                                         break;
1263                                 }
1264                                 ef->pos++;
1265                                 ct++;
1266                         }
1267                 }
1268         }
1269
1270         /* cursor first */
1271         if (ef) {
1272                 float si, co;
1273
1274                 ct = &chartransdata[ef->pos];
1275                 si = sinf(ct->rot);
1276                 co = cosf(ct->rot);
1277
1278                 f = ef->textcurs[0];
1279
1280                 f[0] = cu->fsize * (-0.1f * co + ct->xof);
1281                 f[1] = cu->fsize * ( 0.1f * si + ct->yof);
1282
1283                 f[2] = cu->fsize * ( 0.1f * co + ct->xof);
1284                 f[3] = cu->fsize * (-0.1f * si + ct->yof);
1285
1286                 f[4] = cu->fsize * ( 0.1f * co + 0.8f * si + ct->xof);
1287                 f[5] = cu->fsize * (-0.1f * si + 0.8f * co + ct->yof);
1288
1289                 f[6] = cu->fsize * (-0.1f * co + 0.8f * si + ct->xof);
1290                 f[7] = cu->fsize * ( 0.1f * si + 0.8f * co + ct->yof);
1291
1292         }
1293
1294         if (mode == FO_SELCHANGE) {
1295                 MEM_freeN(chartransdata);
1296                 chartransdata = NULL;
1297                 goto finally;
1298         }
1299
1300         if (mode == FO_EDIT) {
1301                 /* make nurbdata */
1302                 BKE_nurbList_free(r_nubase);
1303
1304                 ct = chartransdata;
1305                 for (i = 0; i < slen; i++) {
1306                         unsigned int cha = (unsigned int) mem[i];
1307                         info = &(custrinfo[i]);
1308
1309                         if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
1310                                 cha = towupper(cha);
1311                         }
1312
1313                         if (ob == NULL || info->mat_nr > (ob->totcol)) {
1314                                 /* printf("Error: Illegal material index (%d) in text object, setting to 0\n", info->mat_nr); */
1315                                 info->mat_nr = 0;
1316                         }
1317                         /* We do not want to see any character for \n or \r */
1318                         if (cha != '\n')
1319                                 buildchar(cu, r_nubase, cha, info, ct->xof, ct->yof, ct->rot, i);
1320
1321                         if ((info->flag & CU_CHINFO_UNDERLINE) && (cha != '\n')) {
1322                                 float ulwidth, uloverlap = 0.0f;
1323                                 rctf rect;
1324
1325                                 if ((i < (slen - 1)) && (mem[i + 1] != '\n') &&
1326                                     ((mem[i + 1] != ' ') || (custrinfo[i + 1].flag & CU_CHINFO_UNDERLINE)) &&
1327                                     ((custrinfo[i + 1].flag & CU_CHINFO_WRAP) == 0))
1328                                 {
1329                                         uloverlap = xtrax + 0.1f;
1330                                 }
1331                                 /* Find the character, the characters has to be in the memory already
1332                                  * since character checking has been done earlier already. */
1333                                 che = find_vfont_char(vfd, cha);
1334
1335                                 twidth = char_width(cu, che, info);
1336                                 ulwidth = (twidth * (1.0f + (info->kern / 40.0f))) + uloverlap;
1337
1338                                 rect.xmin = ct->xof;
1339                                 rect.xmax = rect.xmin + ulwidth;
1340
1341                                 rect.ymin = ct->yof;
1342                                 rect.ymax = rect.ymin - cu->ulheight;
1343
1344                                 build_underline(cu, r_nubase,
1345                                                 &rect, cu->ulpos - 0.05f,
1346                                                 ct->rot, i, info->mat_nr);
1347                         }
1348                         ct++;
1349                 }
1350         }
1351
1352         ok = true;
1353
1354 finally:
1355
1356         {
1357                 if (r_text) {
1358                         *r_text = mem;
1359                         *r_text_len = slen;
1360                         *r_text_free = (ef == NULL);
1361                 }
1362                 else {
1363                         if (ef == NULL) {
1364                                 MEM_freeN((void *)mem);
1365                         }
1366                 }
1367         }
1368
1369         if (chartransdata) {
1370                 if (ok && r_chartransdata) {
1371                         *r_chartransdata = chartransdata;
1372                 }
1373                 else {
1374                         MEM_freeN(chartransdata);
1375                 }
1376         }
1377
1378         return ok;
1379
1380 #undef MARGIN_X_MIN
1381 #undef MARGIN_Y_MIN
1382 }
1383
1384 #undef DESCENT
1385 #undef ASCENT
1386
1387 bool BKE_vfont_to_curve_nubase(Object *ob, int mode, ListBase *r_nubase)
1388 {
1389         BLI_assert(ob->type == OB_FONT);
1390
1391         return BKE_vfont_to_curve_ex(ob, ob->data, mode, r_nubase,
1392                                      NULL, NULL, NULL, NULL);
1393 }
1394
1395 /** Warning: expects to have access to evaluated data (i.e. passed object should be evaluated one...). */
1396 bool BKE_vfont_to_curve(Object *ob, int mode)
1397 {
1398         Curve *cu = ob->data;
1399
1400         return BKE_vfont_to_curve_ex(ob, ob->data, mode, &cu->nurb, NULL, NULL, NULL, NULL);
1401 }
1402
1403
1404 /* -------------------------------------------------------------------- */
1405
1406 /** \name VFont Clipboard
1407  * \{ */
1408
1409 static struct {
1410         wchar_t *text_buffer;
1411         CharInfo *info_buffer;
1412         size_t len_wchar;
1413         size_t len_utf8;
1414 } g_vfont_clipboard = {NULL};
1415
1416 void BKE_vfont_clipboard_free(void)
1417 {
1418         MEM_SAFE_FREE(g_vfont_clipboard.text_buffer);
1419         MEM_SAFE_FREE(g_vfont_clipboard.info_buffer);
1420         g_vfont_clipboard.len_wchar = 0;
1421         g_vfont_clipboard.len_utf8 = 0;
1422 }
1423
1424 void BKE_vfont_clipboard_set(const wchar_t *text_buf, const CharInfo *info_buf, const size_t len)
1425 {
1426         wchar_t *text;
1427         CharInfo *info;
1428
1429         /* clean previous buffers*/
1430         BKE_vfont_clipboard_free();
1431
1432         text = MEM_malloc_arrayN((len + 1), sizeof(wchar_t), __func__);
1433         if (text == NULL) {
1434                 return;
1435         }
1436
1437         info = MEM_malloc_arrayN(len, sizeof(CharInfo), __func__);
1438         if (info == NULL) {
1439                 MEM_freeN(text);
1440                 return;
1441         }
1442
1443         memcpy(text, text_buf, len * sizeof(wchar_t));
1444         text[len] = '\0';
1445         memcpy(info, info_buf, len * sizeof(CharInfo));
1446
1447         /* store new buffers */
1448         g_vfont_clipboard.text_buffer = text;
1449         g_vfont_clipboard.info_buffer = info;
1450         g_vfont_clipboard.len_utf8 = BLI_wstrlen_utf8(text);
1451         g_vfont_clipboard.len_wchar = len;
1452 }
1453
1454 void BKE_vfont_clipboard_get(
1455         wchar_t **r_text_buf, CharInfo **r_info_buf,
1456         size_t *r_len_utf8, size_t *r_len_wchar)
1457 {
1458         if (r_text_buf) {
1459                 *r_text_buf = g_vfont_clipboard.text_buffer;
1460         }
1461
1462         if (r_info_buf) {
1463                 *r_info_buf = g_vfont_clipboard.info_buffer;
1464         }
1465
1466         if (r_len_wchar) {
1467                 *r_len_wchar = g_vfont_clipboard.len_wchar;
1468         }
1469
1470         if (r_len_utf8) {
1471                 *r_len_utf8 = g_vfont_clipboard.len_utf8;
1472         }
1473 }
1474
1475 /** \} */