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