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