Text3D: fixes to selection
[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 (strcmp(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->selstart >= 0 && ef->selstart <= ef->len + 1);
492         BLI_assert(ef->selend   >= 0 && ef->selend   <= ef->len);
493         BLI_assert(ef->pos      >= 0 && ef->pos      <= ef->len);
494
495         if (ef->selstart == 0) {
496                 return 0;
497         }
498
499         if (ef->selstart <= ef->selend) {
500                 start = ef->selstart - 1;
501                 end = ef->selend - 1;
502                 direction = 1;
503         }
504         else {
505                 start = ef->selend;
506                 end = ef->selstart - 2;
507                 direction = -1;
508         }
509
510         if (start == end + 1) {
511                 return 0;
512         }
513         else {
514                 BLI_assert(start < end + 1);
515                 *r_start = start;
516                 *r_end = end;
517                 return direction;
518         }
519 }
520
521 void BKE_vfont_select_clamp(Object *ob)
522 {
523         Curve *cu = ob->data;
524         EditFont *ef = cu->editfont;
525
526         BLI_assert((ob->type == OB_FONT) && ef);
527
528         CLAMP_MAX(ef->pos,      ef->len);
529         CLAMP_MAX(ef->selstart, ef->len + 1);
530         CLAMP_MAX(ef->selend,   ef->len);
531 }
532
533 static float char_width(Curve *cu, VChar *che, CharInfo *info)
534 {
535         /* The character wasn't found, propably ascii = 0, then the width shall be 0 as well */
536         if (che == NULL) {
537                 return 0.0f;
538         }
539         else if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
540                 return che->width * cu->smallcaps_scale;
541         }
542         else {
543                 return che->width;
544         }
545 }
546
547 static void textbox_scale(TextBox *tb_dst, const TextBox *tb_src, float scale)
548 {
549         tb_dst->x = tb_src->x * scale;
550         tb_dst->y = tb_src->y * scale;
551         tb_dst->w = tb_src->w * scale;
552         tb_dst->h = tb_src->h * scale;
553 }
554
555 /**
556  * Used for storing per-line data for alignment & wrapping.
557  */
558 struct TempLineInfo {
559         float x_min;      /* left margin */
560         float x_max;      /* right margin */
561         int   char_nr;    /* number of characters */
562         int   wspace_nr;  /* number of whitespaces of line */
563 };
564
565 bool BKE_vfont_to_curve_ex(Main *bmain, Object *ob, int mode, ListBase *r_nubase,
566                            const wchar_t **r_text, int *r_text_len, bool *r_text_free,
567                            struct CharTrans **r_chartransdata)
568 {
569         Curve *cu = ob->data;
570         EditFont *ef = cu->editfont;
571         EditFontSelBox *selboxes = NULL;
572         VFont *vfont, *oldvfont;
573         VFontData *vfd = NULL;
574         CharInfo *info = NULL, *custrinfo;
575         TextBox tb_scale;
576         bool use_textbox;
577         VChar *che;
578         struct CharTrans *chartransdata = NULL, *ct;
579         struct TempLineInfo *lineinfo;
580         float *f, xof, yof, xtrax, linedist;
581         float twidth, maxlen = 0;
582         int i, slen, j;
583         int curbox;
584         int selstart, selend;
585         int cnr = 0, lnr = 0, wsnr = 0;
586         const wchar_t *mem;
587         wchar_t ascii;
588         bool ok = false;
589         const float xof_scale = cu->xof / cu->fsize;
590         const float yof_scale = cu->yof / cu->fsize;
591
592 #define MARGIN_X_MIN (xof_scale + tb_scale.x)
593 #define MARGIN_Y_MIN (yof_scale + tb_scale.y)
594
595         /* remark: do calculations including the trailing '\0' of a string
596          * because the cursor can be at that location */
597
598         BLI_assert(ob->type == OB_FONT);
599
600         /* Set font data */
601         vfont = cu->vfont;
602
603         if (cu->str == NULL) return ok;
604         if (vfont == NULL) return ok;
605
606         vfd = vfont_get_data(bmain, vfont);
607
608         /* The VFont Data can not be found */
609         if (!vfd) return ok;
610         
611         if (ef) {
612                 slen = ef->len;
613                 mem = ef->textbuf;
614                 custrinfo = ef->textbufinfo;
615         }
616         else {
617                 wchar_t *mem_tmp;
618                 slen = cu->len_wchar;
619
620                 /* Create unicode string */
621                 mem_tmp = MEM_mallocN(((slen + 1) * sizeof(wchar_t)), "convertedmem");
622
623                 BLI_strncpy_wchar_from_utf8(mem_tmp, cu->str, slen + 1);
624
625                 if (cu->strinfo == NULL) {  /* old file */
626                         cu->strinfo = MEM_callocN((slen + 4) * sizeof(CharInfo), "strinfo compat");
627                 }
628                 custrinfo = cu->strinfo;
629
630                 mem = mem_tmp;
631         }
632
633         if (cu->tb == NULL)
634                 cu->tb = MEM_callocN(MAXTEXTBOX * sizeof(TextBox), "TextBox compat");
635
636         if (ef) {
637                 if (ef->selboxes)
638                         MEM_freeN(ef->selboxes);
639
640                 if (BKE_vfont_select_get(ob, &selstart, &selend))
641                         ef->selboxes = MEM_callocN((selend - selstart + 1) * sizeof(EditFontSelBox), "font selboxes");
642                 else
643                         ef->selboxes = NULL;
644
645                 selboxes = ef->selboxes;
646         }
647
648         /* calc offset and rotation of each char */
649         ct = chartransdata = MEM_callocN((slen + 1) * sizeof(struct CharTrans), "buildtext");
650
651         /* We assume the worst case: 1 character per line (is freed at end anyway) */
652         lineinfo = MEM_mallocN(sizeof(*lineinfo) * (slen * 2 + 1), "lineinfo");
653         
654         linedist = cu->linedist;
655         
656         curbox = 0;
657         textbox_scale(&tb_scale, &cu->tb[curbox], 1.0f / cu->fsize);
658         use_textbox = (tb_scale.w != 0.0f);
659
660
661         xof = MARGIN_X_MIN;
662         yof = MARGIN_Y_MIN;
663
664         xtrax = 0.5f * cu->spacing - 0.5f;
665
666         oldvfont = NULL;
667
668         for (i = 0; i < slen; i++) {
669                 custrinfo[i].flag &= ~(CU_CHINFO_WRAP | CU_CHINFO_SMALLCAPS_CHECK);
670         }
671
672         for (i = 0; i <= slen; i++) {
673 makebreak:
674                 /* Characters in the list */
675                 info = &custrinfo[i];
676                 ascii = mem[i];
677                 if (info->flag & CU_CHINFO_SMALLCAPS) {
678                         ascii = towupper(ascii);
679                         if (mem[i] != ascii) {
680                                 info->flag |= CU_CHINFO_SMALLCAPS_CHECK;
681                         }
682                 }
683
684                 vfont = which_vfont(cu, info);
685
686                 if (vfont == NULL) break;
687
688                 if (vfont != oldvfont) {
689                         vfd = vfont_get_data(bmain, vfont);
690                         oldvfont = vfont;
691                 }
692
693                 /* VFont Data for VFont couldn't be found */
694                 if (!vfd) {
695                         MEM_freeN(chartransdata);
696                         chartransdata = NULL;
697                         MEM_freeN(lineinfo);
698                         goto finally;
699                 }
700
701                 if (!ELEM(ascii, '\n', '\0')) {
702                         BLI_rw_mutex_lock(&vfont_rwlock, THREAD_LOCK_READ);
703                         che = find_vfont_char(vfd, ascii);
704                         BLI_rw_mutex_unlock(&vfont_rwlock);
705
706                         /*
707                          * The character wasn't in the current curve base so load it
708                          * But if the font is built-in then do not try loading since
709                          * whole font is in the memory already
710                          */
711                         if (che == NULL && BKE_vfont_is_builtin(vfont) == false) {
712                                 BLI_rw_mutex_lock(&vfont_rwlock, THREAD_LOCK_WRITE);
713                                 /* Check it once again, char might have been already load
714                                  * between previous BLI_rw_mutex_unlock() and this BLI_rw_mutex_lock().
715                                  *
716                                  * Such a check should not be a bottleneck since it wouldn't
717                                  * happen often once all the chars are load.
718                                  */
719                                 if ((che = find_vfont_char(vfd, ascii)) == NULL) {
720                                         che = BLI_vfontchar_from_freetypefont(vfont, ascii);
721                                 }
722                                 BLI_rw_mutex_unlock(&vfont_rwlock);
723                         }
724                 }
725                 else {
726                         che = NULL;
727                 }
728
729                 twidth = char_width(cu, che, info);
730
731                 /* Calculate positions */
732                 if ((tb_scale.w != 0.0f) &&
733                     (ct->dobreak == 0) &&
734                     (((xof - tb_scale.x) + twidth) > xof_scale + tb_scale.w))
735                 {
736                         //              fprintf(stderr, "linewidth exceeded: %c%c%c...\n", mem[i], mem[i+1], mem[i+2]);
737                         for (j = i; j && (mem[j] != '\n') && (chartransdata[j].dobreak == 0); j--) {
738                                 if (mem[j] == ' ' || mem[j] == '-') {
739                                         ct -= (i - (j - 1));
740                                         cnr -= (i - (j - 1));
741                                         if (mem[j] == ' ') wsnr--;
742                                         if (mem[j] == '-') wsnr++;
743                                         i = j - 1;
744                                         xof = ct->xof;
745                                         ct[1].dobreak = 1;
746                                         custrinfo[i + 1].flag |= CU_CHINFO_WRAP;
747                                         goto makebreak;
748                                 }
749                                 if (chartransdata[j].dobreak) {
750                                         //                              fprintf(stderr, "word too long: %c%c%c...\n", mem[j], mem[j+1], mem[j+2]);
751                                         ct->dobreak = 1;
752                                         custrinfo[i + 1].flag |= CU_CHINFO_WRAP;
753                                         ct -= 1;
754                                         cnr -= 1;
755                                         i--;
756                                         xof = ct->xof;
757                                         goto makebreak;
758                                 }
759                         }
760                 }
761
762                 if (ascii == '\n' || ascii == 0 || ct->dobreak) {
763                         ct->xof = xof;
764                         ct->yof = yof;
765                         ct->linenr = lnr;
766                         ct->charnr = cnr;
767
768                         yof -= linedist;
769
770                         lineinfo[lnr].x_min     = (xof - xtrax) - tb_scale.x;
771                         lineinfo[lnr].x_max     = tb_scale.w;
772                         lineinfo[lnr].char_nr   = cnr;
773                         lineinfo[lnr].wspace_nr = wsnr;
774
775                         CLAMP_MIN(maxlen, lineinfo[lnr].x_min);
776
777                         if ((tb_scale.h != 0.0f) &&
778                             (cu->totbox > (curbox + 1)) &&
779                             ((-(yof - tb_scale.y)) > (tb_scale.h - (linedist * cu->fsize)) - yof_scale))
780                         {
781                                 maxlen = 0;
782                                 curbox++;
783
784                                 textbox_scale(&tb_scale, &cu->tb[curbox], 1.0f / cu->fsize);
785
786                                 yof = MARGIN_Y_MIN;
787                         }
788
789                         /* XXX, has been unused for years, need to check if this is useful, r4613 r5282 - campbell */
790 #if 0
791                         if (ascii == '\n')
792                                 xof = xof_scale;
793                         else
794                                 xof = MARGIN_X_MIN;
795 #else
796                         xof = MARGIN_X_MIN;
797 #endif
798                         lnr++;
799                         cnr = 0;
800                         wsnr = 0;
801                 }
802                 else if (ascii == 9) {    /* TAB */
803                         float tabfac;
804                         
805                         ct->xof = xof;
806                         ct->yof = yof;
807                         ct->linenr = lnr;
808                         ct->charnr = cnr++;
809
810                         tabfac = (xof - MARGIN_X_MIN + 0.01f);
811                         tabfac = 2.0f * ceilf(tabfac / 2.0f);
812                         xof = MARGIN_X_MIN + tabfac;
813                 }
814                 else {
815                         EditFontSelBox *sb = NULL;
816                         float wsfac;
817
818                         ct->xof = xof;
819                         ct->yof = yof;
820                         ct->linenr = lnr;
821                         ct->charnr = cnr++;
822
823                         if (selboxes && (i >= selstart) && (i <= selend)) {
824                                 sb = &selboxes[i - selstart];
825                                 sb->y = yof * cu->fsize - linedist * cu->fsize * 0.1f;
826                                 sb->h = linedist * cu->fsize;
827                                 sb->w = xof * cu->fsize;
828                         }
829         
830                         if (ascii == 32) {
831                                 wsfac = cu->wordspace; 
832                                 wsnr++;
833                         }
834                         else {
835                                 wsfac = 1.0f;
836                         }
837                         
838                         /* Set the width of the character */
839                         twidth = char_width(cu, che, info);
840
841                         xof += (twidth * wsfac * (1.0f + (info->kern / 40.0f)) ) + xtrax;
842                         
843                         if (sb) {
844                                 sb->w = (xof * cu->fsize) - sb->w;
845                         }
846                 }
847                 ct++;
848         }
849         
850         cu->lines = 1;
851         for (i = 0; i <= slen; i++) {
852                 ascii = mem[i];
853                 ct = &chartransdata[i];
854                 if (ascii == '\n' || ct->dobreak) cu->lines++;
855         }
856
857         /* linedata is now: width of line */
858
859         if (cu->spacemode != CU_LEFT) {
860                 ct = chartransdata;
861
862                 if (cu->spacemode == CU_RIGHT) {
863                         struct TempLineInfo *li;
864
865                         for (i = 0, li = lineinfo; i < lnr; i++, li++) {
866                                 li->x_min = (li->x_max - li->x_min) + xof_scale;
867                         }
868
869                         for (i = 0; i <= slen; i++) {
870                                 ct->xof += lineinfo[ct->linenr].x_min;
871                                 ct++;
872                         }
873                 }
874                 else if (cu->spacemode == CU_MIDDLE) {
875                         struct TempLineInfo *li;
876
877                         for (i = 0, li = lineinfo; i < lnr; i++, li++) {
878                                 li->x_min = ((li->x_max - li->x_min) + xof_scale) / 2.0f;
879                         }
880
881                         for (i = 0; i <= slen; i++) {
882                                 ct->xof += lineinfo[ct->linenr].x_min;
883                                 ct++;
884                         }
885                 }
886                 else if ((cu->spacemode == CU_FLUSH) && use_textbox) {
887                         struct TempLineInfo *li;
888
889                         for (i = 0, li = lineinfo; i < lnr; i++, li++) {
890                                 li->x_min = ((li->x_max - li->x_min) + xof_scale);
891
892                                 if (li->char_nr > 1) {
893                                         li->x_min /= (float)(li->char_nr - 1);
894                                 }
895                         }
896                         for (i = 0; i <= slen; i++) {
897                                 for (j = i; (!ELEM(mem[j], '\0', '\n')) && (chartransdata[j].dobreak == 0) && (j < slen); j++) {
898                                         /* do nothing */
899                                 }
900
901 //                              if ((mem[j] != '\n') && (mem[j])) {
902                                 ct->xof += ct->charnr * lineinfo[ct->linenr].x_min;
903 //                              }
904                                 ct++;
905                         }
906                 }
907                 else if ((cu->spacemode == CU_JUSTIFY) && use_textbox) {
908                         float curofs = 0.0f;
909                         for (i = 0; i <= slen; i++) {
910                                 for (j = i;
911                                      (mem[j]) && (mem[j] != '\n') && (chartransdata[j].dobreak == 0) && (j < slen);
912                                      j++)
913                                 {
914                                         /* pass */
915                                 }
916
917                                 if ((mem[j] != '\n') &&
918                                     ((chartransdata[j].dobreak != 0)))
919                                 {
920                                         if (mem[i] == ' ') {
921                                                 struct TempLineInfo *li;
922
923                                                 li = &lineinfo[ct->linenr];
924                                                 curofs += ((li->x_max - li->x_min) + xof_scale) / (float)li->wspace_nr;
925                                         }
926                                         ct->xof += curofs;
927                                 }
928                                 if (mem[i] == '\n' || chartransdata[i].dobreak) curofs = 0;
929                                 ct++;
930                         }
931                 }
932         }
933
934         MEM_freeN(lineinfo);
935
936         /* TEXT ON CURVE */
937         /* Note: Only OB_CURVE objects could have a path  */
938         if (cu->textoncurve && cu->textoncurve->type == OB_CURVE) {
939                 BLI_assert(cu->textoncurve->curve_cache != NULL);
940                 if (cu->textoncurve->curve_cache->path) {
941                         float distfac, imat[4][4], imat3[3][3], cmat[3][3];
942                         float minx, maxx, miny, maxy;
943                         float timeofs, sizefac;
944                         
945                         invert_m4_m4(imat, ob->obmat);
946                         copy_m3_m4(imat3, imat);
947
948                         copy_m3_m4(cmat, cu->textoncurve->obmat);
949                         mul_m3_m3m3(cmat, cmat, imat3);
950                         sizefac = normalize_v3(cmat[0]) / cu->fsize;
951                         
952                         minx = miny = 1.0e20f;
953                         maxx = maxy = -1.0e20f;
954                         ct = chartransdata;
955                         for (i = 0; i <= slen; i++, ct++) {
956                                 if (minx > ct->xof) minx = ct->xof;
957                                 if (maxx < ct->xof) maxx = ct->xof;
958                                 if (miny > ct->yof) miny = ct->yof;
959                                 if (maxy < ct->yof) maxy = ct->yof;
960                         }
961                         
962                         /* we put the x-coordinaat exact at the curve, the y is rotated */
963                         
964                         /* length correction */
965                         distfac = sizefac * cu->textoncurve->curve_cache->path->totdist / (maxx - minx);
966                         timeofs = 0.0f;
967                         
968                         if (distfac > 1.0f) {
969                                 /* path longer than text: spacemode involves */
970                                 distfac = 1.0f / distfac;
971                                 
972                                 if (cu->spacemode == CU_RIGHT) {
973                                         timeofs = 1.0f - distfac;
974                                 }
975                                 else if (cu->spacemode == CU_MIDDLE) {
976                                         timeofs = (1.0f - distfac) / 2.0f;
977                                 }
978                                 else if (cu->spacemode == CU_FLUSH) {
979                                         distfac = 1.0f;
980                                 }
981                         }
982                         else {
983                                 distfac = 1.0;
984                         }
985
986                         distfac /= (maxx - minx);
987                         
988                         timeofs += distfac * cu->xof;  /* not cyclic */
989                         
990                         ct = chartransdata;
991                         for (i = 0; i <= slen; i++, ct++) {
992                                 float ctime, dtime, vec[4], tvec[4], rotvec[3];
993                                 float si, co;
994                                 
995                                 /* rotate around center character */
996                                 info = &custrinfo[i];
997                                 ascii = mem[i];
998                                 if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
999                                         ascii = towupper(ascii);
1000                                 }
1001
1002                                 che = find_vfont_char(vfd, ascii);
1003         
1004                                 twidth = char_width(cu, che, info);
1005
1006                                 dtime = distfac * 0.5f * twidth;
1007
1008                                 ctime = timeofs + distfac * (ct->xof - minx);
1009                                 CLAMP(ctime, 0.0f, 1.0f);
1010
1011                                 /* calc the right loc AND the right rot separately */
1012                                 /* vec, tvec need 4 items */
1013                                 where_on_path(cu->textoncurve, ctime, vec, tvec, NULL, NULL, NULL);
1014                                 where_on_path(cu->textoncurve, ctime + dtime, tvec, rotvec, NULL, NULL, NULL);
1015                                 
1016                                 mul_v3_fl(vec, sizefac);
1017                                 
1018                                 ct->rot = (float)M_PI - atan2f(rotvec[1], rotvec[0]);
1019
1020                                 si = sinf(ct->rot);
1021                                 co = cosf(ct->rot);
1022
1023                                 yof = ct->yof;
1024                                 
1025                                 ct->xof = vec[0] + si * yof;
1026                                 ct->yof = vec[1] + co * yof;
1027
1028                                 if (selboxes && (i >= selstart) && (i <= selend)) {
1029                                         EditFontSelBox *sb;
1030                                         sb = &selboxes[i - selstart];
1031                                         sb->rot = -ct->rot;
1032                                 }
1033                                 
1034                         }
1035                 }
1036         }
1037
1038         if (selboxes) {
1039                 ct = chartransdata;
1040                 for (i = 0; i <= selend; i++, ct++) {
1041                         if (i >= selstart) {
1042                                 selboxes[i - selstart].x = ct->xof * cu->fsize;
1043                                 selboxes[i - selstart].y = ct->yof * cu->fsize;
1044                         }
1045                 }
1046         }
1047
1048         if (mode == FO_CURSUP || mode == FO_CURSDOWN || mode == FO_PAGEUP || mode == FO_PAGEDOWN) {
1049                 ct = &chartransdata[ef->pos];
1050
1051                 if ((mode == FO_CURSUP || mode == FO_PAGEUP) && ct->linenr == 0) {
1052                         /* pass */
1053                 }
1054                 else if ((mode == FO_CURSDOWN || mode == FO_PAGEDOWN) && ct->linenr == lnr) {
1055                         /* pass */
1056                 }
1057                 else {
1058                         switch (mode) {
1059                                 case FO_CURSUP:     lnr = ct->linenr - 1; break;
1060                                 case FO_CURSDOWN:   lnr = ct->linenr + 1; break;
1061                                 case FO_PAGEUP:     lnr = ct->linenr - 10; break;
1062                                 case FO_PAGEDOWN:   lnr = ct->linenr + 10; break;
1063                         }
1064                         cnr = ct->charnr;
1065                         /* seek for char with lnr en cnr */
1066                         ef->pos = 0;
1067                         ct = chartransdata;
1068                         for (i = 0; i < slen; i++) {
1069                                 if (ct->linenr == lnr) {
1070                                         if ((ct->charnr == cnr) || ((ct + 1)->charnr == 0)) {
1071                                                 break;
1072                                         }
1073                                 }
1074                                 else if (ct->linenr > lnr) {
1075                                         break;
1076                                 }
1077                                 ef->pos++;
1078                                 ct++;
1079                         }
1080                 }
1081         }
1082         
1083         /* cursor first */
1084         if (ef) {
1085                 float si, co;
1086                 
1087                 ct = &chartransdata[ef->pos];
1088                 si = sinf(ct->rot);
1089                 co = cosf(ct->rot);
1090
1091                 f = ef->textcurs[0];
1092                 
1093                 f[0] = cu->fsize * (-0.1f * co + ct->xof);
1094                 f[1] = cu->fsize * ( 0.1f * si + ct->yof);
1095                 
1096                 f[2] = cu->fsize * ( 0.1f * co + ct->xof);
1097                 f[3] = cu->fsize * (-0.1f * si + ct->yof);
1098                 
1099                 f[4] = cu->fsize * ( 0.1f * co + 0.8f * si + ct->xof);
1100                 f[5] = cu->fsize * (-0.1f * si + 0.8f * co + ct->yof);
1101                 
1102                 f[6] = cu->fsize * (-0.1f * co + 0.8f * si + ct->xof);
1103                 f[7] = cu->fsize * ( 0.1f * si + 0.8f * co + ct->yof);
1104                 
1105         }
1106
1107         if (mode == FO_SELCHANGE) {
1108                 MEM_freeN(chartransdata);
1109                 chartransdata = NULL;
1110                 goto finally;
1111         }
1112
1113         if (mode == FO_EDIT) {
1114                 /* make nurbdata */
1115                 BKE_nurbList_free(r_nubase);
1116                 
1117                 ct = chartransdata;
1118                 for (i = 0; i < slen; i++) {
1119                         unsigned int cha = (unsigned int) mem[i];
1120                         info = &(custrinfo[i]);
1121
1122                         if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
1123                                 cha = towupper(cha);
1124                         }
1125
1126                         if (info->mat_nr > (ob->totcol)) {
1127                                 /* printf("Error: Illegal material index (%d) in text object, setting to 0\n", info->mat_nr); */
1128                                 info->mat_nr = 0;
1129                         }
1130                         /* We do not want to see any character for \n or \r */
1131                         if (cha != '\n')
1132                                 buildchar(bmain, cu, r_nubase, cha, info, ct->xof, ct->yof, ct->rot, i);
1133
1134                         if ((info->flag & CU_CHINFO_UNDERLINE) && (cha != '\n')) {
1135                                 float ulwidth, uloverlap = 0.0f;
1136                                 rctf rect;
1137
1138                                 if ((i < (slen - 1)) && (mem[i + 1] != '\n') &&
1139                                     ((mem[i + 1] != ' ') || (custrinfo[i + 1].flag & CU_CHINFO_UNDERLINE)) &&
1140                                     ((custrinfo[i + 1].flag & CU_CHINFO_WRAP) == 0))
1141                                 {
1142                                         uloverlap = xtrax + 0.1f;
1143                                 }
1144                                 /* Find the character, the characters has to be in the memory already
1145                                  * since character checking has been done earlier already. */
1146                                 che = find_vfont_char(vfd, cha);
1147
1148                                 twidth = char_width(cu, che, info);
1149                                 ulwidth = (twidth * (1.0f + (info->kern / 40.0f))) + uloverlap;
1150
1151                                 rect.xmin = ct->xof;
1152                                 rect.xmax = rect.xmin + ulwidth;
1153
1154                                 rect.ymin = ct->yof;
1155                                 rect.ymax = rect.ymin - cu->ulheight;
1156
1157                                 build_underline(cu, r_nubase,
1158                                                 &rect, cu->ulpos - 0.05f,
1159                                                 ct->rot, i, info->mat_nr);
1160                         }
1161                         ct++;
1162                 }
1163         }
1164
1165         ok = true;
1166
1167 finally:
1168
1169         {
1170                 if (r_text) {
1171                         *r_text = mem;
1172                         *r_text_len = slen;
1173                         *r_text_free = (ef == NULL);
1174                 }
1175                 else {
1176                         if (ef == NULL) {
1177                                 MEM_freeN((void *)mem);
1178                         }
1179                 }
1180         }
1181
1182         if (chartransdata) {
1183                 if (ok && r_chartransdata) {
1184                         *r_chartransdata = chartransdata;
1185                 }
1186                 else {
1187                         MEM_freeN(chartransdata);
1188                 }
1189         }
1190
1191         return ok;
1192
1193 #undef MARGIN_X_MIN
1194 #undef MARGIN_Y_MIN
1195 }
1196
1197
1198 bool BKE_vfont_to_curve_nubase(Main *bmain, Object *ob, int mode, ListBase *r_nubase)
1199 {
1200         BLI_assert(ob->type == OB_FONT);
1201
1202         return BKE_vfont_to_curve_ex(bmain, ob, mode, r_nubase,
1203                                      NULL, NULL, NULL, NULL);
1204 }
1205
1206 bool BKE_vfont_to_curve(Main *bmain, Object *ob, int mode)
1207 {
1208         Curve *cu = ob->data;
1209
1210         return BKE_vfont_to_curve_ex(bmain, ob, mode, &cu->nurb, NULL, NULL, NULL, NULL);
1211 }