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