Code cleanup: remove '\r' check for 3d text, its removed on load even on windows
[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 bool BKE_vfont_to_curve_ex(Main *bmain, Object *ob, int mode, ListBase *r_nubase,
522                            const wchar_t **r_text, int *r_text_len, bool *r_text_free,
523                            struct CharTrans **r_chartransdata)
524 {
525         Curve *cu = ob->data;
526         EditFont *ef = cu->editfont;
527         EditFontSelBox *selboxes = NULL;
528         VFont *vfont, *oldvfont;
529         VFontData *vfd = NULL;
530         CharInfo *info = NULL, *custrinfo;
531         TextBox *tb;
532         VChar *che;
533         struct CharTrans *chartransdata = NULL, *ct;
534         float *f, xof, yof, xtrax, linedist, *linedata, *linedata2, *linedata3, *linedata4;
535         float twidth, maxlen = 0;
536         int i, slen, j;
537         int curbox;
538         int selstart, selend;
539         short cnr = 0, lnr = 0, wsnr = 0;
540         const wchar_t *mem;
541         wchar_t ascii;
542         bool ok = false;
543
544         /* remark: do calculations including the trailing '\0' of a string
545          * because the cursor can be at that location */
546
547         BLI_assert(ob->type == OB_FONT);
548
549         /* Set font data */
550         vfont = cu->vfont;
551
552         if (cu->str == NULL) return ok;
553         if (vfont == NULL) return ok;
554
555         vfd = vfont_get_data(bmain, vfont);
556
557         /* The VFont Data can not be found */
558         if (!vfd) return ok;
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') && (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 == 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                         linedata[lnr] = (xof - xtrax) - (tb->x / cu->fsize);
718                         linedata2[lnr] = cnr;
719                         linedata3[lnr] = tb->w / cu->fsize;
720                         linedata4[lnr] = wsnr;
721
722                         CLAMP_MIN(maxlen, linedata[lnr]);
723
724                         if ((tb->h != 0.0f) &&
725                             ((-(yof - (tb->y / cu->fsize))) > ((tb->h / cu->fsize) - (linedist * cu->fsize)) - cu->yof) &&
726                             (cu->totbox > (curbox + 1)) )
727                         {
728                                 maxlen = 0;
729                                 tb++;
730                                 curbox++;
731                                 yof = cu->yof + tb->y / cu->fsize;
732                         }
733
734                         /* XXX, has been unused for years, need to check if this is useful, r4613 r5282 - campbell */
735 #if 0
736                         if (ascii == '\n')
737                                 xof = cu->xof;
738                         else
739                                 xof = cu->xof + (tb->x / cu->fsize);
740 #else
741                         xof = cu->xof + (tb->x / cu->fsize);
742 #endif
743                         lnr++;
744                         cnr = 0;
745                         wsnr = 0;
746                 }
747                 else if (ascii == 9) {    /* TAB */
748                         float tabfac;
749                         
750                         ct->xof = xof;
751                         ct->yof = yof;
752                         ct->linenr = lnr;
753                         ct->charnr = cnr++;
754
755                         tabfac = (xof - cu->xof + 0.01f);
756                         tabfac = 2.0f * ceilf(tabfac / 2.0f);
757                         xof = cu->xof + tabfac;
758                 }
759                 else {
760                         EditFontSelBox *sb = NULL;
761                         float wsfac;
762
763                         ct->xof = xof;
764                         ct->yof = yof;
765                         ct->linenr = lnr;
766                         ct->charnr = cnr++;
767
768                         if (selboxes && (i >= selstart) && (i <= selend)) {
769                                 sb = &selboxes[i - selstart];
770                                 sb->y = yof * cu->fsize - linedist * cu->fsize * 0.1f;
771                                 sb->h = linedist * cu->fsize;
772                                 sb->w = xof * cu->fsize;
773                         }
774         
775                         if (ascii == 32) {
776                                 wsfac = cu->wordspace; 
777                                 wsnr++;
778                         }
779                         else {
780                                 wsfac = 1.0f;
781                         }
782                         
783                         /* Set the width of the character */
784                         twidth = char_width(cu, che, info);
785
786                         xof += (twidth * wsfac * (1.0f + (info->kern / 40.0f)) ) + xtrax;
787                         
788                         if (sb) {
789                                 sb->w = (xof * cu->fsize) - sb->w;
790                         }
791                 }
792                 ct++;
793         }
794         
795         cu->lines = 1;
796         for (i = 0; i <= slen; i++) {
797                 ascii = mem[i];
798                 ct = &chartransdata[i];
799                 if (ascii == '\n' || ct->dobreak) cu->lines++;
800         }
801
802         /* linedata is now: width of line
803          * linedata2 is now: number of characters
804          * linedata3 is now: maxlen of that line
805          * linedata4 is now: number of whitespaces of line */
806
807         if (cu->spacemode != CU_LEFT) {
808                 ct = chartransdata;
809
810                 if (cu->spacemode == CU_RIGHT) {
811                         for (i = 0; i < lnr; i++) linedata[i] = (linedata3[i] - linedata[i]) + cu->xof;
812                         for (i = 0; i <= slen; i++) {
813                                 ct->xof += linedata[ct->linenr];
814                                 ct++;
815                         }
816                 }
817                 else if (cu->spacemode == CU_MIDDLE) {
818                         for (i = 0; i < lnr; i++) linedata[i] = ((linedata3[i] - linedata[i]) + cu->xof) / 2;
819                         for (i = 0; i <= slen; i++) {
820                                 ct->xof += linedata[ct->linenr];
821                                 ct++;
822                         }
823                 }
824                 else if ((cu->spacemode == CU_FLUSH) && (cu->tb[0].w != 0.0f)) {
825                         for (i = 0; i < lnr; i++)
826                                 if (linedata2[i] > 1)
827                                         linedata[i] = ((linedata3[i] - linedata[i]) + cu->xof) / (linedata2[i] - 1);
828                         for (i = 0; i <= slen; i++) {
829                                 for (j = i; (!ELEM(mem[j], '\0', '\n')) && (chartransdata[j].dobreak == 0) && (j < slen); j++) {
830                                         /* do nothing */
831                                 }
832
833 //                              if ((mem[j] != '\n') && (mem[j])) {
834                                 ct->xof += ct->charnr * linedata[ct->linenr];
835 //                              }
836                                 ct++;
837                         }
838                 }
839                 else if ((cu->spacemode == CU_JUSTIFY) && (cu->tb[0].w != 0.0f)) {
840                         float curofs = 0.0f;
841                         for (i = 0; i <= slen; i++) {
842                                 for (j = i;
843                                      (mem[j]) && (mem[j] != '\n') && (chartransdata[j].dobreak == 0) && (j < slen);
844                                      j++)
845                                 {
846                                         /* pass */
847                                 }
848
849                                 if ((mem[j] != '\n') &&
850                                     ((chartransdata[j].dobreak != 0)))
851                                 {
852                                         if (mem[i] == ' ')
853                                                 curofs += ((linedata3[ct->linenr] - linedata[ct->linenr]) + cu->xof) / linedata4[ct->linenr];
854                                         ct->xof += curofs;
855                                 }
856                                 if (mem[i] == '\n' || chartransdata[i].dobreak) curofs = 0;
857                                 ct++;
858                         }
859                 }
860         }
861         
862         /* TEXT ON CURVE */
863         /* Note: Only OB_CURVE objects could have a path  */
864         if (cu->textoncurve && cu->textoncurve->type == OB_CURVE) {
865                 BLI_assert(cu->textoncurve->curve_cache != NULL);
866                 if (cu->textoncurve->curve_cache->path) {
867                         float distfac, imat[4][4], imat3[3][3], cmat[3][3];
868                         float minx, maxx, miny, maxy;
869                         float timeofs, sizefac;
870                         
871                         invert_m4_m4(imat, ob->obmat);
872                         copy_m3_m4(imat3, imat);
873
874                         copy_m3_m4(cmat, cu->textoncurve->obmat);
875                         mul_m3_m3m3(cmat, cmat, imat3);
876                         sizefac = normalize_v3(cmat[0]) / cu->fsize;
877                         
878                         minx = miny = 1.0e20f;
879                         maxx = maxy = -1.0e20f;
880                         ct = chartransdata;
881                         for (i = 0; i <= slen; i++, ct++) {
882                                 if (minx > ct->xof) minx = ct->xof;
883                                 if (maxx < ct->xof) maxx = ct->xof;
884                                 if (miny > ct->yof) miny = ct->yof;
885                                 if (maxy < ct->yof) maxy = ct->yof;
886                         }
887                         
888                         /* we put the x-coordinaat exact at the curve, the y is rotated */
889                         
890                         /* length correction */
891                         distfac = sizefac * cu->textoncurve->curve_cache->path->totdist / (maxx - minx);
892                         timeofs = 0.0f;
893                         
894                         if (distfac > 1.0f) {
895                                 /* path longer than text: spacemode involves */
896                                 distfac = 1.0f / distfac;
897                                 
898                                 if (cu->spacemode == CU_RIGHT) {
899                                         timeofs = 1.0f - distfac;
900                                 }
901                                 else if (cu->spacemode == CU_MIDDLE) {
902                                         timeofs = (1.0f - distfac) / 2.0f;
903                                 }
904                                 else if (cu->spacemode == CU_FLUSH) {
905                                         distfac = 1.0f;
906                                 }
907                         }
908                         else {
909                                 distfac = 1.0;
910                         }
911
912                         distfac /= (maxx - minx);
913                         
914                         timeofs += distfac * cu->xof;  /* not cyclic */
915                         
916                         ct = chartransdata;
917                         for (i = 0; i <= slen; i++, ct++) {
918                                 float ctime, dtime, vec[4], tvec[4], rotvec[3];
919                                 float si, co;
920                                 
921                                 /* rotate around center character */
922                                 info = &custrinfo[i];
923                                 ascii = mem[i];
924                                 if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
925                                         ascii = towupper(ascii);
926                                 }
927
928                                 che = find_vfont_char(vfd, ascii);
929         
930                                 twidth = char_width(cu, che, info);
931
932                                 dtime = distfac * 0.5f * twidth;
933
934                                 ctime = timeofs + distfac * (ct->xof - minx);
935                                 CLAMP(ctime, 0.0f, 1.0f);
936
937                                 /* calc the right loc AND the right rot separately */
938                                 /* vec, tvec need 4 items */
939                                 where_on_path(cu->textoncurve, ctime, vec, tvec, NULL, NULL, NULL);
940                                 where_on_path(cu->textoncurve, ctime + dtime, tvec, rotvec, NULL, NULL, NULL);
941                                 
942                                 mul_v3_fl(vec, sizefac);
943                                 
944                                 ct->rot = (float)M_PI - atan2f(rotvec[1], rotvec[0]);
945
946                                 si = sinf(ct->rot);
947                                 co = cosf(ct->rot);
948
949                                 yof = ct->yof;
950                                 
951                                 ct->xof = vec[0] + si * yof;
952                                 ct->yof = vec[1] + co * yof;
953
954                                 if (selboxes && (i >= selstart) && (i <= selend)) {
955                                         EditFontSelBox *sb;
956                                         sb = &selboxes[i - selstart];
957                                         sb->rot = -ct->rot;
958                                 }
959                                 
960                         }
961                 }
962         }
963
964         if (selboxes) {
965                 ct = chartransdata;
966                 for (i = 0; i <= selend; i++, ct++) {
967                         if (i >= selstart) {
968                                 selboxes[i - selstart].x = ct->xof * cu->fsize;
969                                 selboxes[i - selstart].y = ct->yof * cu->fsize;
970                         }
971                 }
972         }
973
974         if (mode == FO_CURSUP || mode == FO_CURSDOWN || mode == FO_PAGEUP || mode == FO_PAGEDOWN) {
975                 ct = &chartransdata[ef->pos];
976
977                 if ((mode == FO_CURSUP || mode == FO_PAGEUP) && ct->linenr == 0) {
978                         /* pass */
979                 }
980                 else if ((mode == FO_CURSDOWN || mode == FO_PAGEDOWN) && ct->linenr == lnr) {
981                         /* pass */
982                 }
983                 else {
984                         switch (mode) {
985                                 case FO_CURSUP:     lnr = ct->linenr - 1; break;
986                                 case FO_CURSDOWN:   lnr = ct->linenr + 1; break;
987                                 case FO_PAGEUP:     lnr = ct->linenr - 10; break;
988                                 case FO_PAGEDOWN:   lnr = ct->linenr + 10; break;
989                         }
990                         cnr = ct->charnr;
991                         /* seek for char with lnr en cnr */
992                         ef->pos = 0;
993                         ct = chartransdata;
994                         for (i = 0; i < slen; i++) {
995                                 if (ct->linenr == lnr) {
996                                         if ((ct->charnr == cnr) || ((ct + 1)->charnr == 0)) {
997                                                 break;
998                                         }
999                                 }
1000                                 else if (ct->linenr > lnr) {
1001                                         break;
1002                                 }
1003                                 ef->pos++;
1004                                 ct++;
1005                         }
1006                 }
1007         }
1008         
1009         /* cursor first */
1010         if (ef) {
1011                 float si, co;
1012                 
1013                 ct = &chartransdata[ef->pos];
1014                 si = sinf(ct->rot);
1015                 co = cosf(ct->rot);
1016
1017                 f = ef->textcurs[0];
1018                 
1019                 f[0] = cu->fsize * (-0.1f * co + ct->xof);
1020                 f[1] = cu->fsize * ( 0.1f * si + ct->yof);
1021                 
1022                 f[2] = cu->fsize * ( 0.1f * co + ct->xof);
1023                 f[3] = cu->fsize * (-0.1f * si + ct->yof);
1024                 
1025                 f[4] = cu->fsize * ( 0.1f * co + 0.8f * si + ct->xof);
1026                 f[5] = cu->fsize * (-0.1f * si + 0.8f * co + ct->yof);
1027                 
1028                 f[6] = cu->fsize * (-0.1f * co + 0.8f * si + ct->xof);
1029                 f[7] = cu->fsize * ( 0.1f * si + 0.8f * co + ct->yof);
1030                 
1031         }
1032
1033         MEM_freeN(linedata);
1034         MEM_freeN(linedata2);
1035         MEM_freeN(linedata3);
1036         MEM_freeN(linedata4);
1037
1038         if (mode == FO_SELCHANGE) {
1039                 MEM_freeN(chartransdata);
1040                 chartransdata = NULL;
1041                 goto finally;
1042         }
1043
1044         if (mode == FO_EDIT) {
1045                 /* make nurbdata */
1046                 BKE_nurbList_free(r_nubase);
1047                 
1048                 ct = chartransdata;
1049                 for (i = 0; i < slen; i++) {
1050                         unsigned int cha = (unsigned int) mem[i];
1051                         info = &(custrinfo[i]);
1052
1053                         if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
1054                                 cha = towupper(cha);
1055                         }
1056
1057                         if (info->mat_nr > (ob->totcol)) {
1058                                 /* printf("Error: Illegal material index (%d) in text object, setting to 0\n", info->mat_nr); */
1059                                 info->mat_nr = 0;
1060                         }
1061                         /* We do not want to see any character for \n or \r */
1062                         if (cha != '\n')
1063                                 buildchar(bmain, cu, r_nubase, cha, info, ct->xof, ct->yof, ct->rot, i);
1064
1065                         if ((info->flag & CU_CHINFO_UNDERLINE) && (cha != '\n')) {
1066                                 float ulwidth, uloverlap = 0.0f;
1067                                 rctf rect;
1068
1069                                 if ((i < (slen - 1)) && (mem[i + 1] != '\n') &&
1070                                     ((mem[i + 1] != ' ') || (custrinfo[i + 1].flag & CU_CHINFO_UNDERLINE)) &&
1071                                     ((custrinfo[i + 1].flag & CU_CHINFO_WRAP) == 0))
1072                                 {
1073                                         uloverlap = xtrax + 0.1f;
1074                                 }
1075                                 /* Find the character, the characters has to be in the memory already
1076                                  * since character checking has been done earlier already. */
1077                                 che = find_vfont_char(vfd, cha);
1078
1079                                 twidth = char_width(cu, che, info);
1080                                 ulwidth = (twidth * (1.0f + (info->kern / 40.0f))) + uloverlap;
1081
1082                                 rect.xmin = ct->xof;
1083                                 rect.xmax = rect.xmin + ulwidth;
1084
1085                                 rect.ymin = ct->yof;
1086                                 rect.ymax = rect.ymin - cu->ulheight;
1087
1088                                 build_underline(cu, r_nubase,
1089                                                 &rect, cu->ulpos - 0.05f,
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, Object *ob, int mode, ListBase *r_nubase)
1127 {
1128         BLI_assert(ob->type == OB_FONT);
1129
1130         return BKE_vfont_to_curve_ex(bmain, ob, mode, r_nubase,
1131                                      NULL, NULL, NULL, NULL);
1132 }
1133
1134 bool BKE_vfont_to_curve(Main *bmain, Object *ob, int mode)
1135 {
1136         Curve *cu = ob->data;
1137
1138         return BKE_vfont_to_curve_ex(bmain, ob, mode, &cu->nurb, NULL, NULL, NULL, NULL);
1139 }