Merge branch 'master' into free-refcount-ids
[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 /**
99  * Free (or release) any data used by this font (does not free the font itself).
100  *
101  * \param vf The font to free.
102  * \param do_id_user When \a true, ID datablocks used (referenced) by this font are 'released'
103  *                   (their user count is decreased).
104  */
105 void BKE_vfont_free(struct VFont *vf, const bool UNUSED(do_id_user))
106 {
107         BKE_vfont_free_data(vf);
108
109         if (vf->packedfile) {
110                 freePackedFile(vf->packedfile);
111                 vf->packedfile = NULL;
112         }
113 }
114
115 static void *builtin_font_data = NULL;
116 static int builtin_font_size = 0;
117
118 bool BKE_vfont_is_builtin(struct VFont *vfont)
119 {
120         return STREQ(vfont->name, FO_BUILTIN_NAME);
121 }
122
123 void BKE_vfont_builtin_register(void *mem, int size)
124 {
125         builtin_font_data = mem;
126         builtin_font_size = size;
127 }
128
129 static PackedFile *get_builtin_packedfile(void)
130 {
131         if (!builtin_font_data) {
132                 printf("Internal error, builtin font not loaded\n");
133
134                 return NULL;
135         }
136         else {
137                 void *mem = MEM_mallocN(builtin_font_size, "vfd_builtin");
138
139                 memcpy(mem, builtin_font_data, builtin_font_size);
140         
141                 return newPackedFileMemory(mem, builtin_font_size);
142         }
143 }
144
145 static VFontData *vfont_get_data(Main *bmain, VFont *vfont)
146 {
147         if (vfont == NULL) {
148                 return NULL;
149         }
150
151         /* And then set the data */
152         if (!vfont->data) {
153                 PackedFile *pf;
154
155                 BLI_rw_mutex_lock(&vfont_rwlock, THREAD_LOCK_WRITE);
156
157                 if (vfont->data) {
158                         /* Check data again, since it might have been already
159                          * initialized from other thread (previous check is
160                          * not accurate or threading, just prevents unneeded
161                          * lock if all the data is here for sure).
162                          */
163                         BLI_rw_mutex_unlock(&vfont_rwlock);
164                         return vfont->data;
165                 }
166
167                 if (BKE_vfont_is_builtin(vfont)) {
168                         pf = get_builtin_packedfile();
169                 }
170                 else {
171                         if (vfont->packedfile) {
172                                 pf = vfont->packedfile;
173
174                                 /* We need to copy a tmp font to memory unless it is already there */
175                                 if (vfont->temp_pf == NULL) {
176                                         vfont->temp_pf = dupPackedFile(pf);
177                                 }
178                         }
179                         else {
180                                 pf = newPackedFile(NULL, vfont->name, ID_BLEND_PATH(bmain, &vfont->id));
181
182                                 if (vfont->temp_pf == NULL) {
183                                         vfont->temp_pf = newPackedFile(NULL, vfont->name, ID_BLEND_PATH(bmain, &vfont->id));
184                                 }
185                         }
186                         if (!pf) {
187                                 printf("Font file doesn't exist: %s\n", vfont->name);
188
189                                 /* DON'T DO THIS
190                                  * missing file shouldn't modify path! - campbell */
191 #if 0
192                                 strcpy(vfont->name, FO_BUILTIN_NAME);
193 #endif
194                                 pf = get_builtin_packedfile();
195                         }
196                 }
197                 
198                 if (pf) {
199                         vfont->data = BLI_vfontdata_from_freetypefont(pf);
200                         if (pf != vfont->packedfile) {
201                                 freePackedFile(pf);
202                         }
203                 }
204
205                 BLI_rw_mutex_unlock(&vfont_rwlock);
206         }
207
208         return vfont->data;
209 }
210
211 VFont *BKE_vfont_load(Main *bmain, const char *name)
212 {
213         char filename[FILE_MAXFILE];
214         VFont *vfont = NULL;
215         PackedFile *pf;
216         PackedFile *temp_pf = NULL;
217         bool is_builtin;
218         
219         if (STREQ(name, FO_BUILTIN_NAME)) {
220                 BLI_strncpy(filename, name, sizeof(filename));
221                 
222                 pf = get_builtin_packedfile();
223                 is_builtin = true;
224         }
225         else {
226                 BLI_split_file_part(name, filename, sizeof(filename));
227                 pf = newPackedFile(NULL, name, bmain->name);
228                 temp_pf = newPackedFile(NULL, name, bmain->name);
229                 
230                 is_builtin = false;
231         }
232
233         if (pf) {
234                 VFontData *vfd;
235
236                 vfd = BLI_vfontdata_from_freetypefont(pf);
237                 if (vfd) {
238                         vfont = BKE_libblock_alloc(bmain, ID_VF, filename);
239                         vfont->data = vfd;
240
241                         /* if there's a font name, use it for the ID name */
242                         if (vfd->name[0] != '\0') {
243                                 BLI_strncpy(vfont->id.name + 2, vfd->name, sizeof(vfont->id.name) - 2);
244                         }
245                         BLI_strncpy(vfont->name, name, sizeof(vfont->name));
246
247                         /* if autopack is on store the packedfile in de font structure */
248                         if (!is_builtin && (G.fileflags & G_AUTOPACK)) {
249                                 vfont->packedfile = pf;
250                         }
251
252                         /* Do not add FO_BUILTIN_NAME to temporary listbase */
253                         if (!STREQ(filename, FO_BUILTIN_NAME)) {
254                                 vfont->temp_pf = temp_pf;
255                         }
256                 }
257
258                 /* Free the packed file */
259                 if (!vfont || vfont->packedfile != pf) {
260                         freePackedFile(pf);
261                 }
262         }
263         
264         return vfont;
265 }
266
267 static VFont *which_vfont(Curve *cu, CharInfo *info)
268 {
269         switch (info->flag & (CU_CHINFO_BOLD | CU_CHINFO_ITALIC)) {
270                 case CU_CHINFO_BOLD:
271                         return cu->vfontb ? cu->vfontb : cu->vfont;
272                 case CU_CHINFO_ITALIC:
273                         return cu->vfonti ? cu->vfonti : cu->vfont;
274                 case (CU_CHINFO_BOLD | CU_CHINFO_ITALIC):
275                         return cu->vfontbi ? cu->vfontbi : cu->vfont;
276                 default:
277                         return cu->vfont;
278         }
279 }
280
281 VFont *BKE_vfont_builtin_get(void)
282 {
283         VFont *vfont;
284         
285         for (vfont = G.main->vfont.first; vfont; vfont = vfont->id.next) {
286                 if (BKE_vfont_is_builtin(vfont)) {
287                         return vfont;
288                 }
289         }
290         
291         return BKE_vfont_load(G.main, FO_BUILTIN_NAME);
292 }
293
294 static VChar *find_vfont_char(VFontData *vfd, unsigned int character)
295 {
296         return BLI_ghash_lookup(vfd->characters, SET_UINT_IN_POINTER(character));
297 }
298
299 static void build_underline(Curve *cu, ListBase *nubase, const rctf *rect,
300                             float yofs, float rot, int charidx, short mat_nr)
301 {
302         Nurb *nu2;
303         BPoint *bp;
304         
305         nu2 = (Nurb *) MEM_callocN(sizeof(Nurb), "underline_nurb");
306         nu2->resolu = cu->resolu;
307         nu2->bezt = NULL;
308         nu2->knotsu = nu2->knotsv = NULL;
309         nu2->flag = CU_2D;
310         nu2->charidx = charidx + 1000;
311         if (mat_nr > 0) nu2->mat_nr = mat_nr - 1;
312         nu2->pntsu = 4;
313         nu2->pntsv = 1;
314         nu2->orderu = 4;
315         nu2->orderv = 1;
316         nu2->flagu = CU_NURB_CYCLIC;
317
318         bp = (BPoint *)MEM_callocN(4 * sizeof(BPoint), "underline_bp");
319
320         copy_v4_fl4(bp[0].vec, rect->xmin, (rect->ymax + yofs), 0.0f, 1.0f);
321         copy_v4_fl4(bp[1].vec, rect->xmax, (rect->ymax + yofs), 0.0f, 1.0f);
322         copy_v4_fl4(bp[2].vec, rect->xmax, (rect->ymin + yofs), 0.0f, 1.0f);
323         copy_v4_fl4(bp[3].vec, rect->xmin, (rect->ymin + yofs), 0.0f, 1.0f);
324
325         nu2->bp = bp;
326         BLI_addtail(nubase, nu2);
327
328         if (rot != 0.0f) {
329                 float si, co;
330                 int i;
331
332                 si = sinf(rot);
333                 co = cosf(rot);
334
335                 for (i = nu2->pntsu; i > 0; i--) {
336                         float *fp;
337                         float x, y;
338
339                         fp = bp->vec;
340
341                         x = fp[0] - rect->xmin;
342                         y = fp[1] - rect->ymin;
343
344                         fp[0] = (+co * x + si * y) + rect->xmin;
345                         fp[1] = (-si * x + co * y) + rect->ymin;
346
347                         bp++;
348                 }
349
350                 bp = nu2->bp;
351         }
352
353         mul_v2_fl(bp[0].vec, cu->fsize);
354         mul_v2_fl(bp[1].vec, cu->fsize);
355         mul_v2_fl(bp[2].vec, cu->fsize);
356         mul_v2_fl(bp[3].vec, cu->fsize);
357 }
358
359 static void buildchar(Main *bmain, Curve *cu, ListBase *nubase, unsigned int character, CharInfo *info,
360                       float ofsx, float ofsy, float rot, int charidx)
361 {
362         BezTriple *bezt1, *bezt2;
363         Nurb *nu1 = NULL, *nu2 = NULL;
364         float *fp, fsize, shear, x, si, co;
365         VFontData *vfd = NULL;
366         VChar *che = NULL;
367         int i;
368
369         vfd = vfont_get_data(bmain, which_vfont(cu, info));
370         if (!vfd) return;
371
372 #if 0
373         if (cu->selend < cu->selstart) {
374                 if ((charidx >= (cu->selend)) && (charidx <= (cu->selstart - 2)))
375                         sel = 1;
376         }
377         else {
378                 if ((charidx >= (cu->selstart - 1)) && (charidx <= (cu->selend - 1)))
379                         sel = 1;
380         }
381 #endif
382
383         /* make a copy at distance ofsx, ofsy with shear */
384         fsize = cu->fsize;
385         shear = cu->shear;
386         si = sinf(rot);
387         co = cosf(rot);
388
389         che = find_vfont_char(vfd, character);
390         
391         /* Select the glyph data */
392         if (che)
393                 nu1 = che->nurbsbase.first;
394
395         /* Create the character */
396         while (nu1) {
397                 bezt1 = nu1->bezt;
398                 if (bezt1) {
399                         nu2 = (Nurb *) MEM_mallocN(sizeof(Nurb), "duplichar_nurb");
400                         if (nu2 == NULL) break;
401                         memcpy(nu2, nu1, sizeof(struct Nurb));
402                         nu2->resolu = cu->resolu;
403                         nu2->bp = NULL;
404                         nu2->knotsu = nu2->knotsv = NULL;
405                         nu2->flag = CU_SMOOTH;
406                         nu2->charidx = charidx;
407                         if (info->mat_nr > 0) {
408                                 nu2->mat_nr = info->mat_nr - 1;
409                         }
410                         else {
411                                 nu2->mat_nr = 0;
412                         }
413                         /* nu2->trim.first = 0; */
414                         /* nu2->trim.last = 0; */
415                         i = nu2->pntsu;
416
417                         bezt2 = (BezTriple *)MEM_mallocN(i * sizeof(BezTriple), "duplichar_bezt2");
418                         if (bezt2 == NULL) {
419                                 MEM_freeN(nu2);
420                                 break;
421                         }
422                         memcpy(bezt2, bezt1, i * sizeof(struct BezTriple));
423                         nu2->bezt = bezt2;
424                         
425                         if (shear != 0.0f) {
426                                 bezt2 = nu2->bezt;
427                                 
428                                 for (i = nu2->pntsu; i > 0; i--) {
429                                         bezt2->vec[0][0] += shear * bezt2->vec[0][1];
430                                         bezt2->vec[1][0] += shear * bezt2->vec[1][1];
431                                         bezt2->vec[2][0] += shear * bezt2->vec[2][1];
432                                         bezt2++;
433                                 }
434                         }
435                         if (rot != 0.0f) {
436                                 bezt2 = nu2->bezt;
437                                 for (i = nu2->pntsu; i > 0; i--) {
438                                         fp = bezt2->vec[0];
439
440                                         x = fp[0];
441                                         fp[0] = co * x + si * fp[1];
442                                         fp[1] = -si * x + co * fp[1];
443                                         x = fp[3];
444                                         fp[3] = co * x + si * fp[4];
445                                         fp[4] = -si * x + co * fp[4];
446                                         x = fp[6];
447                                         fp[6] = co * x + si * fp[7];
448                                         fp[7] = -si * x + co * fp[7];
449
450                                         bezt2++;
451                                 }
452                         }
453                         bezt2 = nu2->bezt;
454
455                         if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
456                                 const float sca = cu->smallcaps_scale;
457                                 for (i = nu2->pntsu; i > 0; i--) {
458                                         fp = bezt2->vec[0];
459                                         fp[0] *= sca;
460                                         fp[1] *= sca;
461                                         fp[3] *= sca;
462                                         fp[4] *= sca;
463                                         fp[6] *= sca;
464                                         fp[7] *= sca;
465                                         bezt2++;
466                                 }
467                         }
468                         bezt2 = nu2->bezt;
469
470                         for (i = nu2->pntsu; i > 0; i--) {
471                                 fp = bezt2->vec[0];
472                                 fp[0] = (fp[0] + ofsx) * fsize;
473                                 fp[1] = (fp[1] + ofsy) * fsize;
474                                 fp[3] = (fp[3] + ofsx) * fsize;
475                                 fp[4] = (fp[4] + ofsy) * fsize;
476                                 fp[6] = (fp[6] + ofsx) * fsize;
477                                 fp[7] = (fp[7] + ofsy) * fsize;
478                                 bezt2++;
479                         }
480                         
481                         BLI_addtail(nubase, nu2);
482                 }
483                 
484                 nu1 = nu1->next;
485         }
486 }
487
488 int BKE_vfont_select_get(Object *ob, int *r_start, int *r_end)
489 {
490         Curve *cu = ob->data;
491         EditFont *ef = cu->editfont;
492         int start, end, direction;
493         
494         if ((ob->type != OB_FONT) || (ef == NULL)) return 0;
495
496         BLI_assert(ef->len >= 0);
497         BLI_assert(ef->selstart >= 0 && ef->selstart <= ef->len + 1);
498         BLI_assert(ef->selend   >= 0 && ef->selend   <= ef->len);
499         BLI_assert(ef->pos      >= 0 && ef->pos      <= ef->len);
500
501         if (ef->selstart == 0) {
502                 return 0;
503         }
504
505         if (ef->selstart <= ef->selend) {
506                 start = ef->selstart - 1;
507                 end = ef->selend - 1;
508                 direction = 1;
509         }
510         else {
511                 start = ef->selend;
512                 end = ef->selstart - 2;
513                 direction = -1;
514         }
515
516         if (start == end + 1) {
517                 return 0;
518         }
519         else {
520                 BLI_assert(start < end + 1);
521                 *r_start = start;
522                 *r_end = end;
523                 return direction;
524         }
525 }
526
527 void BKE_vfont_select_clamp(Object *ob)
528 {
529         Curve *cu = ob->data;
530         EditFont *ef = cu->editfont;
531
532         BLI_assert((ob->type == OB_FONT) && ef);
533
534         CLAMP_MAX(ef->pos,      ef->len);
535         CLAMP_MAX(ef->selstart, ef->len + 1);
536         CLAMP_MAX(ef->selend,   ef->len);
537 }
538
539 static float char_width(Curve *cu, VChar *che, CharInfo *info)
540 {
541         /* The character wasn't found, propably ascii = 0, then the width shall be 0 as well */
542         if (che == NULL) {
543                 return 0.0f;
544         }
545         else if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
546                 return che->width * cu->smallcaps_scale;
547         }
548         else {
549                 return che->width;
550         }
551 }
552
553 static void textbox_scale(TextBox *tb_dst, const TextBox *tb_src, float scale)
554 {
555         tb_dst->x = tb_src->x * scale;
556         tb_dst->y = tb_src->y * scale;
557         tb_dst->w = tb_src->w * scale;
558         tb_dst->h = tb_src->h * scale;
559 }
560
561 /**
562  * Used for storing per-line data for alignment & wrapping.
563  */
564 struct TempLineInfo {
565         float x_min;      /* left margin */
566         float x_max;      /* right margin */
567         int   char_nr;    /* number of characters */
568         int   wspace_nr;  /* number of whitespaces of line */
569 };
570
571 bool BKE_vfont_to_curve_ex(Main *bmain, Object *ob, int mode, ListBase *r_nubase,
572                            const wchar_t **r_text, int *r_text_len, bool *r_text_free,
573                            struct CharTrans **r_chartransdata)
574 {
575         Curve *cu = ob->data;
576         EditFont *ef = cu->editfont;
577         EditFontSelBox *selboxes = NULL;
578         VFont *vfont, *oldvfont;
579         VFontData *vfd = NULL;
580         CharInfo *info = NULL, *custrinfo;
581         TextBox tb_scale;
582         bool use_textbox;
583         VChar *che;
584         struct CharTrans *chartransdata = NULL, *ct;
585         struct TempLineInfo *lineinfo;
586         float *f, xof, yof, xtrax, linedist;
587         float twidth, maxlen = 0;
588         int i, slen, j;
589         int curbox;
590         int selstart, selend;
591         int cnr = 0, lnr = 0, wsnr = 0;
592         const wchar_t *mem;
593         wchar_t ascii;
594         bool ok = false;
595         const float xof_scale = cu->xof / cu->fsize;
596         const float yof_scale = cu->yof / cu->fsize;
597
598 #define MARGIN_X_MIN (xof_scale + tb_scale.x)
599 #define MARGIN_Y_MIN (yof_scale + tb_scale.y)
600
601         /* remark: do calculations including the trailing '\0' of a string
602          * because the cursor can be at that location */
603
604         BLI_assert(ob->type == OB_FONT);
605
606         /* Set font data */
607         vfont = cu->vfont;
608
609         if (cu->str == NULL) return ok;
610         if (vfont == NULL) return ok;
611
612         vfd = vfont_get_data(bmain, vfont);
613
614         /* The VFont Data can not be found */
615         if (!vfd) return ok;
616         
617         if (ef) {
618                 slen = ef->len;
619                 mem = ef->textbuf;
620                 custrinfo = ef->textbufinfo;
621         }
622         else {
623                 wchar_t *mem_tmp;
624                 slen = cu->len_wchar;
625
626                 /* Create unicode string */
627                 mem_tmp = MEM_mallocN(((slen + 1) * sizeof(wchar_t)), "convertedmem");
628
629                 BLI_strncpy_wchar_from_utf8(mem_tmp, cu->str, slen + 1);
630
631                 if (cu->strinfo == NULL) {  /* old file */
632                         cu->strinfo = MEM_callocN((slen + 4) * sizeof(CharInfo), "strinfo compat");
633                 }
634                 custrinfo = cu->strinfo;
635
636                 mem = mem_tmp;
637         }
638
639         if (cu->tb == NULL)
640                 cu->tb = MEM_callocN(MAXTEXTBOX * sizeof(TextBox), "TextBox compat");
641
642         if (ef) {
643                 if (ef->selboxes)
644                         MEM_freeN(ef->selboxes);
645
646                 if (BKE_vfont_select_get(ob, &selstart, &selend))
647                         ef->selboxes = MEM_callocN((selend - selstart + 1) * sizeof(EditFontSelBox), "font selboxes");
648                 else
649                         ef->selboxes = NULL;
650
651                 selboxes = ef->selboxes;
652         }
653
654         /* calc offset and rotation of each char */
655         ct = chartransdata = MEM_callocN((slen + 1) * sizeof(struct CharTrans), "buildtext");
656
657         /* We assume the worst case: 1 character per line (is freed at end anyway) */
658         lineinfo = MEM_mallocN(sizeof(*lineinfo) * (slen * 2 + 1), "lineinfo");
659         
660         linedist = cu->linedist;
661         
662         curbox = 0;
663         textbox_scale(&tb_scale, &cu->tb[curbox], 1.0f / cu->fsize);
664         use_textbox = (tb_scale.w != 0.0f);
665
666
667         xof = MARGIN_X_MIN;
668         yof = MARGIN_Y_MIN;
669
670         xtrax = 0.5f * cu->spacing - 0.5f;
671
672         oldvfont = NULL;
673
674         for (i = 0; i < slen; i++) {
675                 custrinfo[i].flag &= ~(CU_CHINFO_WRAP | CU_CHINFO_SMALLCAPS_CHECK);
676         }
677
678         for (i = 0; i <= slen; i++) {
679 makebreak:
680                 /* Characters in the list */
681                 info = &custrinfo[i];
682                 ascii = mem[i];
683                 if (info->flag & CU_CHINFO_SMALLCAPS) {
684                         ascii = towupper(ascii);
685                         if (mem[i] != ascii) {
686                                 info->flag |= CU_CHINFO_SMALLCAPS_CHECK;
687                         }
688                 }
689
690                 vfont = which_vfont(cu, info);
691
692                 if (vfont == NULL) break;
693
694                 if (vfont != oldvfont) {
695                         vfd = vfont_get_data(bmain, vfont);
696                         oldvfont = vfont;
697                 }
698
699                 /* VFont Data for VFont couldn't be found */
700                 if (!vfd) {
701                         MEM_freeN(chartransdata);
702                         chartransdata = NULL;
703                         MEM_freeN(lineinfo);
704                         goto finally;
705                 }
706
707                 if (!ELEM(ascii, '\n', '\0')) {
708                         BLI_rw_mutex_lock(&vfont_rwlock, THREAD_LOCK_READ);
709                         che = find_vfont_char(vfd, ascii);
710                         BLI_rw_mutex_unlock(&vfont_rwlock);
711
712                         /*
713                          * The character wasn't in the current curve base so load it
714                          * But if the font is built-in then do not try loading since
715                          * whole font is in the memory already
716                          */
717                         if (che == NULL && BKE_vfont_is_builtin(vfont) == false) {
718                                 BLI_rw_mutex_lock(&vfont_rwlock, THREAD_LOCK_WRITE);
719                                 /* Check it once again, char might have been already load
720                                  * between previous BLI_rw_mutex_unlock() and this BLI_rw_mutex_lock().
721                                  *
722                                  * Such a check should not be a bottleneck since it wouldn't
723                                  * happen often once all the chars are load.
724                                  */
725                                 if ((che = find_vfont_char(vfd, ascii)) == NULL) {
726                                         che = BLI_vfontchar_from_freetypefont(vfont, ascii);
727                                 }
728                                 BLI_rw_mutex_unlock(&vfont_rwlock);
729                         }
730                 }
731                 else {
732                         che = NULL;
733                 }
734
735                 twidth = char_width(cu, che, info);
736
737                 /* Calculate positions */
738                 if ((tb_scale.w != 0.0f) &&
739                     (ct->dobreak == 0) &&
740                     (((xof - tb_scale.x) + twidth) > xof_scale + tb_scale.w))
741                 {
742                         //              fprintf(stderr, "linewidth exceeded: %c%c%c...\n", mem[i], mem[i+1], mem[i+2]);
743                         for (j = i; j && (mem[j] != '\n') && (chartransdata[j].dobreak == 0); j--) {
744                                 if (mem[j] == ' ' || mem[j] == '-') {
745                                         ct -= (i - (j - 1));
746                                         cnr -= (i - (j - 1));
747                                         if (mem[j] == ' ') wsnr--;
748                                         if (mem[j] == '-') wsnr++;
749                                         i = j - 1;
750                                         xof = ct->xof;
751                                         ct[1].dobreak = 1;
752                                         custrinfo[i + 1].flag |= CU_CHINFO_WRAP;
753                                         goto makebreak;
754                                 }
755                                 if (chartransdata[j].dobreak) {
756                                         //                              fprintf(stderr, "word too long: %c%c%c...\n", mem[j], mem[j+1], mem[j+2]);
757                                         ct->dobreak = 1;
758                                         custrinfo[i + 1].flag |= CU_CHINFO_WRAP;
759                                         ct -= 1;
760                                         cnr -= 1;
761                                         i--;
762                                         xof = ct->xof;
763                                         goto makebreak;
764                                 }
765                         }
766                 }
767
768                 if (ascii == '\n' || ascii == 0 || ct->dobreak) {
769                         ct->xof = xof;
770                         ct->yof = yof;
771                         ct->linenr = lnr;
772                         ct->charnr = cnr;
773
774                         yof -= linedist;
775
776                         lineinfo[lnr].x_min     = (xof - xtrax) - tb_scale.x;
777                         lineinfo[lnr].x_max     = tb_scale.w;
778                         lineinfo[lnr].char_nr   = cnr;
779                         lineinfo[lnr].wspace_nr = wsnr;
780
781                         CLAMP_MIN(maxlen, lineinfo[lnr].x_min);
782
783                         if ((tb_scale.h != 0.0f) &&
784                             (cu->totbox > (curbox + 1)) &&
785                             ((-(yof - tb_scale.y)) > (tb_scale.h - linedist) - yof_scale))
786                         {
787                                 maxlen = 0;
788                                 curbox++;
789
790                                 textbox_scale(&tb_scale, &cu->tb[curbox], 1.0f / cu->fsize);
791
792                                 yof = MARGIN_Y_MIN;
793                         }
794
795                         /* XXX, has been unused for years, need to check if this is useful, r4613 r5282 - campbell */
796 #if 0
797                         if (ascii == '\n')
798                                 xof = xof_scale;
799                         else
800                                 xof = MARGIN_X_MIN;
801 #else
802                         xof = MARGIN_X_MIN;
803 #endif
804                         lnr++;
805                         cnr = 0;
806                         wsnr = 0;
807                 }
808                 else if (ascii == 9) {    /* TAB */
809                         float tabfac;
810                         
811                         ct->xof = xof;
812                         ct->yof = yof;
813                         ct->linenr = lnr;
814                         ct->charnr = cnr++;
815
816                         tabfac = (xof - MARGIN_X_MIN + 0.01f);
817                         tabfac = 2.0f * ceilf(tabfac / 2.0f);
818                         xof = MARGIN_X_MIN + tabfac;
819                 }
820                 else {
821                         EditFontSelBox *sb = NULL;
822                         float wsfac;
823
824                         ct->xof = xof;
825                         ct->yof = yof;
826                         ct->linenr = lnr;
827                         ct->charnr = cnr++;
828
829                         if (selboxes && (i >= selstart) && (i <= selend)) {
830                                 sb = &selboxes[i - selstart];
831                                 sb->y = yof * cu->fsize - linedist * cu->fsize * 0.1f;
832                                 sb->h = linedist * cu->fsize;
833                                 sb->w = xof * cu->fsize;
834                         }
835         
836                         if (ascii == 32) {
837                                 wsfac = cu->wordspace; 
838                                 wsnr++;
839                         }
840                         else {
841                                 wsfac = 1.0f;
842                         }
843                         
844                         /* Set the width of the character */
845                         twidth = char_width(cu, che, info);
846
847                         xof += (twidth * wsfac * (1.0f + (info->kern / 40.0f)) ) + xtrax;
848                         
849                         if (sb) {
850                                 sb->w = (xof * cu->fsize) - sb->w;
851                         }
852                 }
853                 ct++;
854         }
855         
856         cu->lines = 1;
857         for (i = 0; i <= slen; i++) {
858                 ascii = mem[i];
859                 ct = &chartransdata[i];
860                 if (ascii == '\n' || ct->dobreak) cu->lines++;
861         }
862
863         /* linedata is now: width of line */
864
865         if (cu->spacemode != CU_LEFT) {
866                 ct = chartransdata;
867
868                 if (cu->spacemode == CU_RIGHT) {
869                         struct TempLineInfo *li;
870
871                         for (i = 0, li = lineinfo; i < lnr; i++, li++) {
872                                 li->x_min = (li->x_max - li->x_min) + xof_scale;
873                         }
874
875                         for (i = 0; i <= slen; i++) {
876                                 ct->xof += lineinfo[ct->linenr].x_min;
877                                 ct++;
878                         }
879                 }
880                 else if (cu->spacemode == CU_MIDDLE) {
881                         struct TempLineInfo *li;
882
883                         for (i = 0, li = lineinfo; i < lnr; i++, li++) {
884                                 li->x_min = ((li->x_max - li->x_min) + xof_scale) / 2.0f;
885                         }
886
887                         for (i = 0; i <= slen; i++) {
888                                 ct->xof += lineinfo[ct->linenr].x_min;
889                                 ct++;
890                         }
891                 }
892                 else if ((cu->spacemode == CU_FLUSH) && use_textbox) {
893                         struct TempLineInfo *li;
894
895                         for (i = 0, li = lineinfo; i < lnr; i++, li++) {
896                                 li->x_min = ((li->x_max - li->x_min) + xof_scale);
897
898                                 if (li->char_nr > 1) {
899                                         li->x_min /= (float)(li->char_nr - 1);
900                                 }
901                         }
902                         for (i = 0; i <= slen; i++) {
903                                 for (j = i; (!ELEM(mem[j], '\0', '\n')) && (chartransdata[j].dobreak == 0) && (j < slen); j++) {
904                                         /* do nothing */
905                                 }
906
907 //                              if ((mem[j] != '\n') && (mem[j])) {
908                                 ct->xof += ct->charnr * lineinfo[ct->linenr].x_min;
909 //                              }
910                                 ct++;
911                         }
912                 }
913                 else if ((cu->spacemode == CU_JUSTIFY) && use_textbox) {
914                         float curofs = 0.0f;
915                         for (i = 0; i <= slen; i++) {
916                                 for (j = i;
917                                      (mem[j]) && (mem[j] != '\n') && (chartransdata[j].dobreak == 0) && (j < slen);
918                                      j++)
919                                 {
920                                         /* pass */
921                                 }
922
923                                 if ((mem[j] != '\n') &&
924                                     ((chartransdata[j].dobreak != 0)))
925                                 {
926                                         if (mem[i] == ' ') {
927                                                 struct TempLineInfo *li;
928
929                                                 li = &lineinfo[ct->linenr];
930                                                 curofs += ((li->x_max - li->x_min) + xof_scale) / (float)li->wspace_nr;
931                                         }
932                                         ct->xof += curofs;
933                                 }
934                                 if (mem[i] == '\n' || chartransdata[i].dobreak) curofs = 0;
935                                 ct++;
936                         }
937                 }
938         }
939
940         MEM_freeN(lineinfo);
941
942         /* TEXT ON CURVE */
943         /* Note: Only OB_CURVE objects could have a path  */
944         if (cu->textoncurve && cu->textoncurve->type == OB_CURVE) {
945                 BLI_assert(cu->textoncurve->curve_cache != NULL);
946                 if (cu->textoncurve->curve_cache->path) {
947                         float distfac, imat[4][4], imat3[3][3], cmat[3][3];
948                         float minx, maxx, miny, maxy;
949                         float timeofs, sizefac;
950                         
951                         invert_m4_m4(imat, ob->obmat);
952                         copy_m3_m4(imat3, imat);
953
954                         copy_m3_m4(cmat, cu->textoncurve->obmat);
955                         mul_m3_m3m3(cmat, cmat, imat3);
956                         sizefac = normalize_v3(cmat[0]) / cu->fsize;
957                         
958                         minx = miny = 1.0e20f;
959                         maxx = maxy = -1.0e20f;
960                         ct = chartransdata;
961                         for (i = 0; i <= slen; i++, ct++) {
962                                 if (minx > ct->xof) minx = ct->xof;
963                                 if (maxx < ct->xof) maxx = ct->xof;
964                                 if (miny > ct->yof) miny = ct->yof;
965                                 if (maxy < ct->yof) maxy = ct->yof;
966                         }
967                         
968                         /* we put the x-coordinaat exact at the curve, the y is rotated */
969                         
970                         /* length correction */
971                         distfac = sizefac * cu->textoncurve->curve_cache->path->totdist / (maxx - minx);
972                         timeofs = 0.0f;
973                         
974                         if (distfac > 1.0f) {
975                                 /* path longer than text: spacemode involves */
976                                 distfac = 1.0f / distfac;
977                                 
978                                 if (cu->spacemode == CU_RIGHT) {
979                                         timeofs = 1.0f - distfac;
980                                 }
981                                 else if (cu->spacemode == CU_MIDDLE) {
982                                         timeofs = (1.0f - distfac) / 2.0f;
983                                 }
984                                 else if (cu->spacemode == CU_FLUSH) {
985                                         distfac = 1.0f;
986                                 }
987                         }
988                         else {
989                                 distfac = 1.0;
990                         }
991
992                         distfac /= (maxx - minx);
993                         
994                         timeofs += distfac * cu->xof;  /* not cyclic */
995                         
996                         ct = chartransdata;
997                         for (i = 0; i <= slen; i++, ct++) {
998                                 float ctime, dtime, vec[4], tvec[4], rotvec[3];
999                                 float si, co;
1000                                 
1001                                 /* rotate around center character */
1002                                 info = &custrinfo[i];
1003                                 ascii = mem[i];
1004                                 if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
1005                                         ascii = towupper(ascii);
1006                                 }
1007
1008                                 che = find_vfont_char(vfd, ascii);
1009         
1010                                 twidth = char_width(cu, che, info);
1011
1012                                 dtime = distfac * 0.5f * twidth;
1013
1014                                 ctime = timeofs + distfac * (ct->xof - minx);
1015                                 CLAMP(ctime, 0.0f, 1.0f);
1016
1017                                 /* calc the right loc AND the right rot separately */
1018                                 /* vec, tvec need 4 items */
1019                                 where_on_path(cu->textoncurve, ctime, vec, tvec, NULL, NULL, NULL);
1020                                 where_on_path(cu->textoncurve, ctime + dtime, tvec, rotvec, NULL, NULL, NULL);
1021                                 
1022                                 mul_v3_fl(vec, sizefac);
1023                                 
1024                                 ct->rot = (float)M_PI - atan2f(rotvec[1], rotvec[0]);
1025
1026                                 si = sinf(ct->rot);
1027                                 co = cosf(ct->rot);
1028
1029                                 yof = ct->yof;
1030                                 
1031                                 ct->xof = vec[0] + si * yof;
1032                                 ct->yof = vec[1] + co * yof;
1033
1034                                 if (selboxes && (i >= selstart) && (i <= selend)) {
1035                                         EditFontSelBox *sb;
1036                                         sb = &selboxes[i - selstart];
1037                                         sb->rot = -ct->rot;
1038                                 }
1039                                 
1040                         }
1041                 }
1042         }
1043
1044         if (selboxes) {
1045                 ct = chartransdata;
1046                 for (i = 0; i <= selend; i++, ct++) {
1047                         if (i >= selstart) {
1048                                 selboxes[i - selstart].x = ct->xof * cu->fsize;
1049                                 selboxes[i - selstart].y = ct->yof * cu->fsize;
1050                         }
1051                 }
1052         }
1053
1054         if (mode == FO_CURSUP || mode == FO_CURSDOWN || mode == FO_PAGEUP || mode == FO_PAGEDOWN) {
1055                 ct = &chartransdata[ef->pos];
1056
1057                 if ((mode == FO_CURSUP || mode == FO_PAGEUP) && ct->linenr == 0) {
1058                         /* pass */
1059                 }
1060                 else if ((mode == FO_CURSDOWN || mode == FO_PAGEDOWN) && ct->linenr == lnr) {
1061                         /* pass */
1062                 }
1063                 else {
1064                         switch (mode) {
1065                                 case FO_CURSUP:     lnr = ct->linenr - 1; break;
1066                                 case FO_CURSDOWN:   lnr = ct->linenr + 1; break;
1067                                 case FO_PAGEUP:     lnr = ct->linenr - 10; break;
1068                                 case FO_PAGEDOWN:   lnr = ct->linenr + 10; break;
1069                         }
1070                         cnr = ct->charnr;
1071                         /* seek for char with lnr en cnr */
1072                         ef->pos = 0;
1073                         ct = chartransdata;
1074                         for (i = 0; i < slen; i++) {
1075                                 if (ct->linenr == lnr) {
1076                                         if ((ct->charnr == cnr) || ((ct + 1)->charnr == 0)) {
1077                                                 break;
1078                                         }
1079                                 }
1080                                 else if (ct->linenr > lnr) {
1081                                         break;
1082                                 }
1083                                 ef->pos++;
1084                                 ct++;
1085                         }
1086                 }
1087         }
1088         
1089         /* cursor first */
1090         if (ef) {
1091                 float si, co;
1092                 
1093                 ct = &chartransdata[ef->pos];
1094                 si = sinf(ct->rot);
1095                 co = cosf(ct->rot);
1096
1097                 f = ef->textcurs[0];
1098                 
1099                 f[0] = cu->fsize * (-0.1f * co + ct->xof);
1100                 f[1] = cu->fsize * ( 0.1f * si + ct->yof);
1101                 
1102                 f[2] = cu->fsize * ( 0.1f * co + ct->xof);
1103                 f[3] = cu->fsize * (-0.1f * si + ct->yof);
1104                 
1105                 f[4] = cu->fsize * ( 0.1f * co + 0.8f * si + ct->xof);
1106                 f[5] = cu->fsize * (-0.1f * si + 0.8f * co + ct->yof);
1107                 
1108                 f[6] = cu->fsize * (-0.1f * co + 0.8f * si + ct->xof);
1109                 f[7] = cu->fsize * ( 0.1f * si + 0.8f * co + ct->yof);
1110                 
1111         }
1112
1113         if (mode == FO_SELCHANGE) {
1114                 MEM_freeN(chartransdata);
1115                 chartransdata = NULL;
1116                 goto finally;
1117         }
1118
1119         if (mode == FO_EDIT) {
1120                 /* make nurbdata */
1121                 BKE_nurbList_free(r_nubase);
1122                 
1123                 ct = chartransdata;
1124                 for (i = 0; i < slen; i++) {
1125                         unsigned int cha = (unsigned int) mem[i];
1126                         info = &(custrinfo[i]);
1127
1128                         if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
1129                                 cha = towupper(cha);
1130                         }
1131
1132                         if (info->mat_nr > (ob->totcol)) {
1133                                 /* printf("Error: Illegal material index (%d) in text object, setting to 0\n", info->mat_nr); */
1134                                 info->mat_nr = 0;
1135                         }
1136                         /* We do not want to see any character for \n or \r */
1137                         if (cha != '\n')
1138                                 buildchar(bmain, cu, r_nubase, cha, info, ct->xof, ct->yof, ct->rot, i);
1139
1140                         if ((info->flag & CU_CHINFO_UNDERLINE) && (cha != '\n')) {
1141                                 float ulwidth, uloverlap = 0.0f;
1142                                 rctf rect;
1143
1144                                 if ((i < (slen - 1)) && (mem[i + 1] != '\n') &&
1145                                     ((mem[i + 1] != ' ') || (custrinfo[i + 1].flag & CU_CHINFO_UNDERLINE)) &&
1146                                     ((custrinfo[i + 1].flag & CU_CHINFO_WRAP) == 0))
1147                                 {
1148                                         uloverlap = xtrax + 0.1f;
1149                                 }
1150                                 /* Find the character, the characters has to be in the memory already
1151                                  * since character checking has been done earlier already. */
1152                                 che = find_vfont_char(vfd, cha);
1153
1154                                 twidth = char_width(cu, che, info);
1155                                 ulwidth = (twidth * (1.0f + (info->kern / 40.0f))) + uloverlap;
1156
1157                                 rect.xmin = ct->xof;
1158                                 rect.xmax = rect.xmin + ulwidth;
1159
1160                                 rect.ymin = ct->yof;
1161                                 rect.ymax = rect.ymin - cu->ulheight;
1162
1163                                 build_underline(cu, r_nubase,
1164                                                 &rect, cu->ulpos - 0.05f,
1165                                                 ct->rot, i, info->mat_nr);
1166                         }
1167                         ct++;
1168                 }
1169         }
1170
1171         ok = true;
1172
1173 finally:
1174
1175         {
1176                 if (r_text) {
1177                         *r_text = mem;
1178                         *r_text_len = slen;
1179                         *r_text_free = (ef == NULL);
1180                 }
1181                 else {
1182                         if (ef == NULL) {
1183                                 MEM_freeN((void *)mem);
1184                         }
1185                 }
1186         }
1187
1188         if (chartransdata) {
1189                 if (ok && r_chartransdata) {
1190                         *r_chartransdata = chartransdata;
1191                 }
1192                 else {
1193                         MEM_freeN(chartransdata);
1194                 }
1195         }
1196
1197         return ok;
1198
1199 #undef MARGIN_X_MIN
1200 #undef MARGIN_Y_MIN
1201 }
1202
1203
1204 bool BKE_vfont_to_curve_nubase(Main *bmain, Object *ob, int mode, ListBase *r_nubase)
1205 {
1206         BLI_assert(ob->type == OB_FONT);
1207
1208         return BKE_vfont_to_curve_ex(bmain, ob, mode, r_nubase,
1209                                      NULL, NULL, NULL, NULL);
1210 }
1211
1212 bool BKE_vfont_to_curve(Main *bmain, Object *ob, int mode)
1213 {
1214         Curve *cu = ob->data;
1215
1216         return BKE_vfont_to_curve_ex(bmain, ob, mode, &cu->nurb, NULL, NULL, NULL, NULL);
1217 }