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