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