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