NLA SoC: Merge from 2.5
[blender.git] / source / blender / blenkernel / intern / font.c
1 /*  font.c     
2  *  
3  * 
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <math.h>
35 #include <stdlib.h>
36 #include <wchar.h>
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include "MEM_guardedalloc.h"
43
44 #include "BLI_arithb.h"
45 #include "BLI_blenlib.h"
46 #include "BLI_vfontdata.h"
47
48 #include "DNA_packedFile_types.h"
49 #include "DNA_curve_types.h"
50 #include "DNA_object_types.h"
51 #include "DNA_view3d_types.h"
52 #include "DNA_vfont_types.h"
53 #include "DNA_scene_types.h"
54
55 #include "BKE_utildefines.h"
56
57 #include "BKE_packedFile.h"
58
59 #include "BKE_library.h"
60 #include "BKE_font.h"
61 #include "BKE_global.h"
62 #include "BKE_main.h"
63 #include "BKE_screen.h"
64 #include "BKE_anim.h"
65 #include "BKE_curve.h"
66 #include "BKE_displist.h"
67
68 static ListBase ttfdata= {NULL, NULL};
69
70 /* UTF-8 <-> wchar transformations */
71 void
72 chtoutf8(unsigned long c, char *o)
73 {
74         // Variables and initialization
75 /*      memset(o, 0, 16);       */
76
77         // Create the utf-8 string
78         if (c < 0x80)
79         {
80                 o[0] = (char) c;
81         }
82         else if (c < 0x800)
83         {
84                 o[0] = (0xC0 | (c>>6));
85                 o[1] = (0x80 | (c & 0x3f));
86         }
87         else if (c < 0x10000)
88         {
89                 o[0] = (0xe0 | (c >> 12));
90                 o[1] = (0x80 | (c >>6 & 0x3f));
91                 o[2] = (0x80 | (c & 0x3f));
92         }
93         else if (c < 0x200000)
94         {
95         o[0] = (0xf0 | (c>>18));
96         o[1] = (0x80 | (c >>12 & 0x3f));
97         o[2] = (0x80 | (c >> 6 & 0x3f));
98         o[3] = (0x80 | (c & 0x3f));
99         }
100 }
101
102 void
103 wcs2utf8s(char *dst, wchar_t *src)
104 {
105         char ch[5];
106
107         while(*src)
108         {
109                 memset(ch, 0, 5);
110                 chtoutf8(*src++, ch);
111                 strcat(dst, ch);
112         }
113 }
114
115 int
116 wcsleninu8(wchar_t *src)
117 {
118         char ch[16];
119         int len = 0;
120
121         while(*src)
122         {
123                 memset(ch, 0, 16);
124                 chtoutf8(*src++, ch);
125                 len = len + strlen(ch);
126         }
127
128         return len;
129 }
130
131 int
132 utf8slen(char *src)
133 {
134         int size = 0, index = 0;
135         unsigned char c;
136         
137         c = src[index++];
138         while(c)
139         {    
140                 if((c & 0x80) == 0)
141                 {
142                         index += 0;
143                 }
144                 else if((c & 0xe0) == 0xe0)
145                 {
146                         index += 2;
147                 }
148                 else
149                 {
150                         index += 1;
151                 }
152                 size += 1;
153                 c = src[index++];               
154         }
155         
156         return size;
157 }
158
159
160 /* Converts Unicode to wchar
161
162 According to RFC 3629 "UTF-8, a transformation format of ISO 10646"
163 (http://tools.ietf.org/html/rfc3629), the valid UTF-8 encoding are:
164
165   Char. number range  |        UTF-8 octet sequence
166       (hexadecimal)    |              (binary)
167    --------------------+---------------------------------------------
168    0000 0000-0000 007F | 0xxxxxxx
169    0000 0080-0000 07FF | 110xxxxx 10xxxxxx
170    0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
171    0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
172
173 If the encoding incidated by the first character is incorrect (because the
174 1 to 3 following characters do not match 10xxxxxx), the output is a '?' and
175 only a single input character is consumed.
176
177 */
178
179 int utf8towchar(wchar_t *w, char *c)
180 {
181         int len=0;
182
183         if(w==NULL || c==NULL) return(0);
184
185         while(*c) {
186                 if ((*c & 0xe0) == 0xc0) {
187                         if((c[1] & 0x80) && (c[1] & 0x40) == 0x00) {
188                                 *w=((c[0] &0x1f)<<6) | (c[1]&0x3f);
189                                 c++;
190                         } else {
191                                 *w = '?';
192                         }
193                 } else if ((*c & 0xf0) == 0xe0) {
194                         if((c[1] & c[2] & 0x80) && ((c[1] | c[2]) & 0x40) == 0x00) {
195                                 *w=((c[0] & 0x0f)<<12) | ((c[1]&0x3f)<<6) | (c[2]&0x3f);
196                                 c += 2;
197                         } else {
198                                 *w = '?';
199                         }
200                 } else if ((*c & 0xf8) == 0xf0) {
201                         if((c[1] & c[2] & c[3] & 0x80) && ((c[1] | c[2] | c[3]) & 0x40) == 0x00) {
202                                 *w=((c[0] & 0x07)<<18) | ((c[1]&0x1f)<<12) | ((c[2]&0x3f)<<6) | (c[3]&0x3f);
203                                 c += 3;
204                         } else {
205                                 *w = '?';
206                         }
207                 } else
208                     *w=(c[0] & 0x7f);
209
210                 c++;
211                 w++;
212                 len++;
213         }
214         return len;
215 }
216
217 /* The vfont code */
218 void free_vfont(struct VFont *vf)
219 {
220         if (vf == 0) return;
221
222         if (vf->data) {
223                 while(vf->data->characters.first)
224                 {
225                         VChar *che = vf->data->characters.first;
226                         
227                         while (che->nurbsbase.first) {
228                                 Nurb *nu = che->nurbsbase.first;
229                                 if (nu->bezt) MEM_freeN(nu->bezt);
230                                 BLI_freelinkN(&che->nurbsbase, nu);
231                         }
232         
233                         BLI_freelinkN(&vf->data->characters, che);
234                 }
235
236                 MEM_freeN(vf->data);
237                 vf->data = NULL;
238         }
239         
240         if (vf->packedfile) {
241                 freePackedFile(vf->packedfile);
242                 vf->packedfile = NULL;
243         }
244 }
245
246 static void *builtin_font_data= NULL;
247 static int builtin_font_size= 0;
248
249 void BKE_font_register_builtin(void *mem, int size)
250 {
251         builtin_font_data= mem;
252         builtin_font_size= size;
253 }
254
255 static PackedFile *get_builtin_packedfile(void)
256 {
257         if (!builtin_font_data) {
258                 printf("Internal error, builtin font not loaded\n");
259
260                 return NULL;
261         } else {
262                 void *mem= MEM_mallocN(builtin_font_size, "vfd_builtin");
263
264                 memcpy(mem, builtin_font_data, builtin_font_size);
265         
266                 return newPackedFileMemory(mem, builtin_font_size);
267         }
268 }
269
270 void free_ttfont(void)
271 {
272         struct TmpFont *tf;
273         
274         tf= ttfdata.first;
275         while(tf) {
276                 freePackedFile(tf->pf);
277                 tf->pf= NULL;
278                 tf->vfont= NULL;
279                 tf= tf->next;
280         }
281         BLI_freelistN(&ttfdata);
282 }
283
284 struct TmpFont *vfont_find_tmpfont(VFont *vfont)
285 {
286         struct TmpFont *tmpfnt = NULL;
287         
288         if(vfont==NULL) return NULL;
289         
290         // Try finding the font from font list
291         tmpfnt = ttfdata.first;
292         while(tmpfnt)
293         {
294                 if(tmpfnt->vfont == vfont)
295                         break;
296                 tmpfnt = tmpfnt->next;
297         }
298         return tmpfnt;
299 }
300
301 static VFontData *vfont_get_data(VFont *vfont)
302 {
303         struct TmpFont *tmpfnt = NULL;
304         PackedFile *tpf;
305         
306         if(vfont==NULL) return NULL;
307         
308         // Try finding the font from font list
309         tmpfnt = vfont_find_tmpfont(vfont);
310         
311         // And then set the data        
312         if (!vfont->data) {
313                 PackedFile *pf;
314                 
315                 if (BLI_streq(vfont->name, "<builtin>")) {
316                         pf= get_builtin_packedfile();
317                 } else {
318                         if (vfont->packedfile) {
319                                 pf= vfont->packedfile;
320                                 
321                                 // We need to copy a tmp font to memory unless it is already there
322                                 if(!tmpfnt)
323                                 {
324                                         tpf= MEM_callocN(sizeof(*tpf), "PackedFile");
325                                         tpf->data= MEM_mallocN(pf->size, "packFile");
326                                         tpf->size= pf->size;
327                                         memcpy(tpf->data, pf->data, pf->size);
328                                         
329                                         // Add temporary packed file to globals
330                                         tmpfnt= (struct TmpFont *) MEM_callocN(sizeof(struct TmpFont), "temp_font");
331                                         tmpfnt->pf= tpf;
332                                         tmpfnt->vfont= vfont;
333                                         BLI_addtail(&ttfdata, tmpfnt);
334                                 }
335                         } else {
336                                 pf= newPackedFile(vfont->name);
337                                 
338                                 if(!tmpfnt)
339                                 {
340                                         tpf= newPackedFile(vfont->name);
341                                         
342                                         // Add temporary packed file to globals
343                                         tmpfnt= (struct TmpFont *) MEM_callocN(sizeof(struct TmpFont), "temp_font");
344                                         tmpfnt->pf= tpf;
345                                         tmpfnt->vfont= vfont;
346                                         BLI_addtail(&ttfdata, tmpfnt);
347                                 }
348                         }
349                         if(!pf) {
350                                 printf("Font file doesn't exist: %s\n", vfont->name);
351
352                                 strcpy(vfont->name, "<builtin>");
353                                 pf= get_builtin_packedfile();
354                         }
355                 }
356                 
357                 if (pf) {
358                         vfont->data= BLI_vfontdata_from_freetypefont(pf);
359                         if (pf != vfont->packedfile) {
360                                 freePackedFile(pf);
361                         }
362                 }
363         }
364         
365         return vfont->data;     
366 }
367
368 VFont *load_vfont(char *name)
369 {
370         char filename[FILE_MAXFILE];
371         VFont *vfont= NULL;
372         PackedFile *pf;
373         PackedFile *tpf = NULL; 
374         int is_builtin;
375         struct TmpFont *tmpfnt;
376         
377         if (BLI_streq(name, "<builtin>")) {
378                 strcpy(filename, name);
379                 
380                 pf= get_builtin_packedfile();
381                 is_builtin= 1;
382         } else {
383                 char dir[FILE_MAXDIR];
384                 
385                 strcpy(dir, name);
386                 BLI_splitdirstring(dir, filename);
387
388                 pf= newPackedFile(name);
389                 tpf= newPackedFile(name);               
390                 
391                 is_builtin= 0;
392         }
393
394         if (pf) {
395                 VFontData *vfd;
396
397                 vfd= BLI_vfontdata_from_freetypefont(pf);
398                 if (vfd) {
399                         vfont = alloc_libblock(&G.main->vfont, ID_VF, filename);
400                         vfont->data = vfd;
401                         
402                         BLI_strncpy(vfont->name, name, sizeof(vfont->name));
403
404                         // if autopack is on store the packedfile in de font structure
405                         if (!is_builtin && (G.fileflags & G_AUTOPACK)) {
406                                 vfont->packedfile = pf;
407                         }
408                         
409                         // Do not add <builtin> to temporary listbase
410                         if(strcmp(filename, "<builtin>"))
411                         {
412                                 tmpfnt= (struct TmpFont *) MEM_callocN(sizeof(struct TmpFont), "temp_font");
413                                 tmpfnt->pf= tpf;
414                                 tmpfnt->vfont= vfont;
415                                 BLI_addtail(&ttfdata, tmpfnt);
416                         }                       
417                 }
418                 
419                 // Free the packed file
420                 if (!vfont || vfont->packedfile != pf) {
421                         freePackedFile(pf);
422                 }
423         
424                 //XXX waitcursor(0);
425         }
426         
427         return vfont;
428 }
429
430 static VFont *which_vfont(Curve *cu, CharInfo *info)
431 {
432         switch(info->flag & CU_STYLE) {
433                 case CU_BOLD:
434                         if (cu->vfontb) return(cu->vfontb); else return(cu->vfont);
435                 case CU_ITALIC:
436                         if (cu->vfonti) return(cu->vfonti); else return(cu->vfont);
437                 case (CU_BOLD|CU_ITALIC):
438                         if (cu->vfontbi) return(cu->vfontbi); else return(cu->vfont);
439                 default:
440                         return(cu->vfont);
441         }                       
442 }
443
444 VFont *get_builtin_font(void)
445 {
446         VFont *vf;
447         
448         for (vf= G.main->vfont.first; vf; vf= vf->id.next)
449                 if (BLI_streq(vf->name, "<builtin>"))
450                         return vf;
451         
452         return load_vfont("<builtin>");
453 }
454
455 static void build_underline(Curve *cu, float x1, float y1, float x2, float y2, int charidx, short mat_nr)
456 {
457         Nurb *nu2;
458         BPoint *bp;
459         
460         nu2 =(Nurb*) MEM_callocN(sizeof(Nurb),"underline_nurb");
461         if (nu2 == NULL) return;
462         nu2->resolu= cu->resolu;
463         nu2->bezt = NULL;
464         nu2->knotsu = nu2->knotsv = NULL;
465         nu2->flag= 0;
466         nu2->charidx = charidx+1000;
467         if (mat_nr > 0) nu2->mat_nr= mat_nr-1;
468         nu2->pntsu = 4;
469         nu2->pntsv = 1;
470         nu2->orderu = 4;
471         nu2->orderv = 1;
472         nu2->flagu = CU_CYCLIC;
473
474         bp = (BPoint*)MEM_callocN(4 * sizeof(BPoint),"underline_bp"); 
475         if (bp == 0){
476                 MEM_freeN(nu2);
477                 return;
478         }
479         nu2->bp = bp;
480
481         nu2->bp[0].vec[0] = x1;
482         nu2->bp[0].vec[1] = y1; 
483         nu2->bp[0].vec[2] = 0;
484         nu2->bp[0].vec[3] = 1.0;
485         nu2->bp[1].vec[0] = x2;
486         nu2->bp[1].vec[1] = y1;
487         nu2->bp[1].vec[2] = 0;  
488         nu2->bp[1].vec[3] = 1.0;        
489         nu2->bp[2].vec[0] = x2;
490         nu2->bp[2].vec[1] = y2; 
491         nu2->bp[2].vec[2] = 0;
492         nu2->bp[2].vec[3] = 1.0; 
493         nu2->bp[3].vec[0] = x1;
494         nu2->bp[3].vec[1] = y2;
495         nu2->bp[3].vec[2] = 0;  
496         nu2->bp[3].vec[3] = 1.0;        
497         
498         nu2->type = CU_2D;
499         BLI_addtail(&(cu->nurb), nu2);  
500
501 }
502
503 static void buildchar(Curve *cu, unsigned long character, CharInfo *info, float ofsx, float ofsy, float rot, int charidx)
504 {
505         BezTriple *bezt1, *bezt2;
506         Nurb *nu1 = NULL, *nu2 = NULL;
507         float *fp, fsize, shear, x, si, co;
508         VFontData *vfd = NULL;
509         VChar *che = NULL;
510         int i, sel=0;
511
512         vfd= vfont_get_data(which_vfont(cu, info));     
513         if (!vfd) return;
514
515         if (cu->selend < cu->selstart) {
516                 if ((charidx >= (cu->selend)) && (charidx <= (cu->selstart-2)))
517                         sel= 1;
518         }
519         else {
520                 if ((charidx >= (cu->selstart-1)) && (charidx <= (cu->selend-1)))
521                         sel= 1;
522         }
523
524         /* make a copy at distance ofsx,ofsy with shear*/
525         fsize= cu->fsize;
526         shear= cu->shear;
527         si= (float)sin(rot);
528         co= (float)cos(rot);
529
530         // Find the correct character from the font
531         che = vfd->characters.first;
532         while(che)
533         {
534                 if(che->index == character)
535                         break;
536                 che = che->next;
537         }
538         
539         // Select the glyph data
540         if(che)
541                 nu1 = che->nurbsbase.first;
542
543         // Create the character
544         while(nu1)
545         {
546                 bezt1 = nu1->bezt;
547                 if (bezt1){
548                         nu2 =(Nurb*) MEM_mallocN(sizeof(Nurb),"duplichar_nurb");
549                         if (nu2 == 0) break;
550                         memcpy(nu2, nu1, sizeof(struct Nurb));
551                         nu2->resolu= cu->resolu;
552                         nu2->bp = 0;
553                         nu2->knotsu = nu2->knotsv = NULL;
554                         nu2->flag= CU_SMOOTH;
555                         nu2->charidx = charidx;
556                         if (info->mat_nr) {
557                                 nu2->mat_nr= info->mat_nr-1;
558                         }
559                         else {
560                                 nu2->mat_nr= 0;
561                         }
562                         /* nu2->trim.first = 0; */
563                         /* nu2->trim.last = 0; */
564                         i = nu2->pntsu;
565
566                         bezt2 = (BezTriple*)MEM_mallocN(i * sizeof(BezTriple),"duplichar_bezt2"); 
567                         if (bezt2 == 0){
568                                 MEM_freeN(nu2);
569                                 break;
570                         }
571                         memcpy(bezt2, bezt1, i * sizeof(struct BezTriple));
572                         nu2->bezt = bezt2;
573                         
574                         if (shear != 0.0) {
575                                 bezt2 = nu2->bezt;
576                                 
577                                 for (i= nu2->pntsu; i > 0; i--) {
578                                         bezt2->vec[0][0] += shear * bezt2->vec[0][1];
579                                         bezt2->vec[1][0] += shear * bezt2->vec[1][1];
580                                         bezt2->vec[2][0] += shear * bezt2->vec[2][1];
581                                         bezt2++;
582                                 }
583                         }
584                         if(rot!=0.0) {
585                                 bezt2= nu2->bezt;
586                                 for (i=nu2->pntsu; i > 0; i--) {
587                                         fp= bezt2->vec[0];
588                                         
589                                         x= fp[0];
590                                         fp[0]= co*x + si*fp[1];
591                                         fp[1]= -si*x + co*fp[1];
592                                         x= fp[3];
593                                         fp[3]= co*x + si*fp[4];
594                                         fp[4]= -si*x + co*fp[4];
595                                         x= fp[6];
596                                         fp[6]= co*x + si*fp[7];
597                                         fp[7]= -si*x + co*fp[7];
598
599                                         bezt2++;
600                                 }
601                         }
602                         bezt2 = nu2->bezt;
603
604                         for (i= nu2->pntsu; i > 0; i--) {
605                                 fp= bezt2->vec[0];
606
607                                 fp[0]= (fp[0]+ofsx)*fsize;
608                                 fp[1]= (fp[1]+ofsy)*fsize;
609                                 fp[3]= (fp[3]+ofsx)*fsize;
610                                 fp[4]= (fp[4]+ofsy)*fsize;
611                                 fp[6]= (fp[6]+ofsx)*fsize;
612                                 fp[7]= (fp[7]+ofsy)*fsize;
613                                 bezt2++;
614                         }
615                         
616                         BLI_addtail(&(cu->nurb), nu2);
617                 }
618                 
619                 nu1 = nu1->next;
620         }
621 }
622
623 int BKE_font_getselection(Object *ob, int *start, int *end)
624 {
625         Curve *cu= ob->data;
626         
627         if (cu->editfont==NULL || ob->type != OB_FONT) return 0;
628
629         if (cu->selstart == 0) return 0;
630         if (cu->selstart <= cu->selend) {
631                 *start = cu->selstart-1;
632                 *end = cu->selend-1;
633                 return 1;
634         }
635         else {
636                 *start = cu->selend;
637                 *end = cu->selstart-2;
638                 return -1;
639         }
640 }
641
642 struct chartrans *BKE_text_to_curve(Scene *scene, Object *ob, int mode) 
643 {
644         VFont *vfont, *oldvfont;
645         VFontData *vfd= NULL;
646         Curve *cu;
647         CharInfo *info, *custrinfo;
648         TextBox *tb;
649         VChar *che;
650         struct chartrans *chartransdata=NULL, *ct;
651         float *f, xof, yof, xtrax, linedist, *linedata, *linedata2, *linedata3, *linedata4;
652         float twidth, maxlen= 0;
653         int i, slen, j;
654         int curbox;
655         int selstart, selend;
656         int utf8len;
657         short cnr=0, lnr=0, wsnr= 0;
658         wchar_t *mem, *tmp, ascii;
659
660         /* renark: do calculations including the trailing '\0' of a string
661            because the cursor can be at that location */
662
663         if(ob->type!=OB_FONT) return 0;
664
665         // Set font data
666         cu= (Curve *) ob->data;
667         vfont= cu->vfont;
668         
669         if(cu->str == NULL) return 0;
670         if(vfont == NULL) return 0;
671
672         // Create unicode string
673         utf8len = utf8slen(cu->str);
674         tmp = mem = MEM_callocN(((utf8len + 1) * sizeof(wchar_t)), "convertedmem");
675         
676         utf8towchar(mem, cu->str);
677
678         // Count the wchar_t string length
679         slen = wcslen(mem);
680
681         if (cu->ulheight == 0.0) 
682                 cu->ulheight = 0.05;
683         
684         if (cu->strinfo==NULL)  /* old file */
685                 cu->strinfo = MEM_callocN((slen+1) * sizeof(CharInfo), "strinfo compat");
686         
687         custrinfo= cu->strinfo;
688         if (cu->editfont)
689                 custrinfo= cu->editfont->textbufinfo;
690         
691         if (cu->tb==NULL)
692                 cu->tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBox compat");
693
694         vfd= vfont_get_data(vfont);
695
696         /* The VFont Data can not be found */
697         if(!vfd) {
698                 if(mem)
699                         MEM_freeN(mem); 
700                 return 0;
701         }
702
703         /* calc offset and rotation of each char */
704         ct = chartransdata =
705                 (struct chartrans*)MEM_callocN((slen+1)* sizeof(struct chartrans),"buildtext");
706
707         /* We assume the worst case: 1 character per line (is freed at end anyway) */
708
709         linedata= MEM_mallocN(sizeof(float)*(slen+2),"buildtext2");
710         linedata2= MEM_mallocN(sizeof(float)*(slen+2),"buildtext3");
711         linedata3= MEM_callocN(sizeof(float)*(slen+2),"buildtext4");    
712         linedata4= MEM_callocN(sizeof(float)*(slen+2),"buildtext5");            
713         
714         linedist= cu->linedist;
715         
716         xof= cu->xof + (cu->tb[0].x/cu->fsize);
717         yof= cu->yof + (cu->tb[0].y/cu->fsize);
718
719         xtrax= 0.5f*cu->spacing-0.5f;
720
721         oldvfont = NULL;
722
723         for (i=0; i<slen; i++) custrinfo[i].flag &= ~CU_WRAP;
724
725         if (cu->selboxes) MEM_freeN(cu->selboxes);
726         cu->selboxes = NULL;
727         if (BKE_font_getselection(ob, &selstart, &selend))
728                 cu->selboxes = MEM_callocN((selend-selstart+1)*sizeof(SelBox), "font selboxes");
729
730         tb = &(cu->tb[0]);
731         curbox= 0;
732         for (i = 0 ; i<=slen ; i++) {
733         makebreak:
734                 // Characters in the list
735                 che = vfd->characters.first;
736                 ascii = mem[i];
737                 info = &(custrinfo[i]);
738                 vfont = which_vfont(cu, info);
739                 
740                 if(vfont==NULL) break;
741                 
742                 // Find the character
743                 while(che) {
744                         if(che->index == ascii)
745                                 break;
746                         che = che->next;
747                 }
748
749                 /*
750                  * The character wasn't in the current curve base so load it
751                  * But if the font is <builtin> then do not try loading since
752                  * whole font is in the memory already
753                  */
754                 if(che == NULL && strcmp(vfont->name, "<builtin>"))     {
755                         BLI_vfontchar_from_freetypefont(vfont, ascii);
756                 }
757
758                 /* Try getting the character again from the list */
759                 che = vfd->characters.first;
760                 while(che) {
761                         if(che->index == ascii)
762                                 break;
763                         che = che->next;
764                 }
765
766                 /* No VFont found */
767                 if (vfont==0) {
768                         if(mem)
769                                 MEM_freeN(mem);
770                         MEM_freeN(chartransdata);
771                         return 0;
772                 }
773
774                 if (vfont != oldvfont) {
775                         vfd= vfont_get_data(vfont);
776                         oldvfont = vfont;
777                 }
778
779                 /* VFont Data for VFont couldn't be found */
780                 if (!vfd) {
781                         if(mem)
782                                 MEM_freeN(mem);
783                         MEM_freeN(chartransdata);
784                         return 0;
785                 }
786
787                 // The character wasn't found, propably ascii = 0, then the width shall be 0 as well
788                 if(!che)
789                         twidth = 0;
790                 else
791                         twidth = che->width;
792
793                 // Calculate positions
794                 if((tb->w != 0.0) && (ct->dobreak==0) && ((xof-(tb->x/cu->fsize)+twidth)*cu->fsize) > tb->w) {
795         //              fprintf(stderr, "linewidth exceeded: %c%c%c...\n", mem[i], mem[i+1], mem[i+2]);
796                         for (j=i; j && (mem[j] != '\n') && (mem[j] != '\r') && (chartransdata[j].dobreak==0); j--) {
797                                 if (mem[j]==' ' || mem[j]=='-') {
798                                         ct -= (i-(j-1));
799                                         cnr -= (i-(j-1));
800                                         if (mem[j] == ' ') wsnr--;
801                                         if (mem[j] == '-') wsnr++;
802                                         i = j-1;
803                                         xof = ct->xof;
804                                         ct[1].dobreak = 1;
805                                         custrinfo[i+1].flag |= CU_WRAP;
806                                         goto makebreak;
807                                 }
808                                 if (chartransdata[j].dobreak) {
809         //                              fprintf(stderr, "word too long: %c%c%c...\n", mem[j], mem[j+1], mem[j+2]);
810                                         ct->dobreak= 1;
811                                         custrinfo[i+1].flag |= CU_WRAP;
812                                         ct -= 1;
813                                         cnr -= 1;
814                                         i--;
815                                         xof = ct->xof;
816                                         goto makebreak;
817                                 }
818                         }
819                 }
820                 if(ascii== '\n' || ascii== '\r' || ascii==0 || ct->dobreak) {
821                         ct->xof= xof;
822                         ct->yof= yof;
823                         ct->linenr= lnr;
824                         ct->charnr= cnr;
825                         
826                         yof-= linedist;
827                         
828                         maxlen= MAX2(maxlen, (xof-tb->x/cu->fsize));
829                         linedata[lnr]= xof-tb->x/cu->fsize;
830                         linedata2[lnr]= cnr;
831                         linedata3[lnr]= tb->w/cu->fsize;
832                         linedata4[lnr]= wsnr;
833                         
834                         if ( (tb->h != 0.0) &&
835                              ((-(yof-(tb->y/cu->fsize))) > ((tb->h/cu->fsize)-(linedist*cu->fsize))) &&
836                              (cu->totbox > (curbox+1)) ) {
837                                 maxlen= 0;
838                                 tb++;
839                                 curbox++;
840                                 yof= cu->yof + tb->y/cu->fsize;
841                         }
842
843                         if(ascii == '\n' || ascii == '\r')
844                                 xof = cu->xof;
845                         else
846                                 xof= cu->xof + (tb->x/cu->fsize);
847
848                         xof= cu->xof + (tb->x/cu->fsize);
849                         lnr++;
850                         cnr= 0;
851                         wsnr= 0;
852                 }
853                 else if(ascii==9) {     /* TAB */
854                         float tabfac;
855                         
856                         ct->xof= xof;
857                         ct->yof= yof;
858                         ct->linenr= lnr;
859                         ct->charnr= cnr++;
860
861                         tabfac= (xof-cu->xof+0.01f);
862                         tabfac= (float)(2.0*ceil(tabfac/2.0));
863                         xof= cu->xof+tabfac;
864                 }
865                 else {
866                         SelBox *sb= NULL;
867                         float wsfac;
868
869                         ct->xof= xof;
870                         ct->yof= yof;
871                         ct->linenr= lnr;
872                         ct->charnr= cnr++;
873
874                         if (cu->selboxes && (i>=selstart) && (i<=selend)) {
875                                 sb = &(cu->selboxes[i-selstart]);
876                                 sb->y = yof*cu->fsize-linedist*cu->fsize*0.1;
877                                 sb->h = linedist*cu->fsize;
878                                 sb->w = xof*cu->fsize;
879                         }
880         
881                         if (ascii==32) {
882                                 wsfac = cu->wordspace; 
883                                 wsnr++;
884                         } 
885                         else wsfac = 1.0;
886                         
887                         // Set the width of the character
888                         if(!che)
889                                 twidth = 0;
890                         else 
891                                 twidth = che->width;
892
893                         xof += (twidth*wsfac*(1.0+(info->kern/40.0)) ) + xtrax;
894                         
895                         if (sb) 
896                                 sb->w = (xof*cu->fsize) - sb->w;
897                 }
898                 ct++;
899         }
900         
901         cu->lines= 1;
902         ct= chartransdata;
903         tmp = mem;
904         for (i= 0; i<=slen; i++, tmp++, ct++) {
905                 ascii = *tmp;
906                 if(ascii== '\n' || ascii== '\r' || ct->dobreak) cu->lines++;
907         }       
908
909         // linedata is now: width of line
910         // linedata2 is now: number of characters
911         // linedata3 is now: maxlen of that line
912         // linedata4 is now: number of whitespaces of line
913
914         if(cu->spacemode!=CU_LEFT) {
915                 ct= chartransdata;
916
917                 if(cu->spacemode==CU_RIGHT) {
918                         for(i=0;i<lnr;i++) linedata[i]= linedata3[i]-linedata[i];
919                         for (i=0; i<=slen; i++) {
920                                 ct->xof+= linedata[ct->linenr];
921                                 ct++;
922                         }
923                 } else if(cu->spacemode==CU_MIDDLE) {
924                         for(i=0;i<lnr;i++) linedata[i]= (linedata3[i]-linedata[i])/2;
925                         for (i=0; i<=slen; i++) {
926                                 ct->xof+= linedata[ct->linenr];
927                                 ct++;
928                         }
929                 } else if((cu->spacemode==CU_FLUSH) &&
930                           (cu->tb[0].w != 0.0)) {
931                         for(i=0;i<lnr;i++)
932                                 if(linedata2[i]>1)
933                                         linedata[i]= (linedata3[i]-linedata[i])/(linedata2[i]-1);
934                         for (i=0; i<=slen; i++) {
935                                 for (j=i; (mem[j]) && (mem[j]!='\n') && 
936                                           (mem[j]!='\r') && (chartransdata[j].dobreak==0) && (j<slen); j++);
937 //                              if ((mem[j]!='\r') && (mem[j]!='\n') && (mem[j])) {
938                                         ct->xof+= ct->charnr*linedata[ct->linenr];
939 //                              }
940                                 ct++;
941                         }
942                 } 
943                 else if((cu->spacemode==CU_JUSTIFY) && (cu->tb[0].w != 0.0)) {
944                         float curofs= 0.0f;
945                         for (i=0; i<=slen; i++) {
946                                 for (j=i; (mem[j]) && (mem[j]!='\n') && 
947                                           (mem[j]!='\r') && (chartransdata[j].dobreak==0) && (j<slen); j++);
948                                 if ((mem[j]!='\r') && (mem[j]!='\n') &&
949                                     ((chartransdata[j].dobreak!=0))) {
950                                     if (mem[i]==' ') curofs += (linedata3[ct->linenr]-linedata[ct->linenr])/linedata4[ct->linenr];
951                                         ct->xof+= curofs;
952                                 }
953                                 if (mem[i]=='\n' || mem[i]=='\r' || chartransdata[i].dobreak) curofs= 0;
954                                 ct++;
955                         }                       
956                 }
957         }
958         
959         /* TEXT ON CURVE */
960         if(cu->textoncurve) {
961                 Curve *cucu= cu->textoncurve->data;
962                 int oldflag= cucu->flag;
963                 
964                 cucu->flag |= (CU_PATH+CU_FOLLOW);
965                 
966                 if(cucu->path==NULL) makeDispListCurveTypes(scene, cu->textoncurve, 0);
967                 if(cucu->path) {
968                         float distfac, imat[4][4], imat3[3][3], cmat[3][3];
969                         float minx, maxx, miny, maxy;
970                         float timeofs, sizefac;
971                         
972                         Mat4Invert(imat, ob->obmat);
973                         Mat3CpyMat4(imat3, imat);
974
975                         Mat3CpyMat4(cmat, cu->textoncurve->obmat);
976                         Mat3MulMat3(cmat, cmat, imat3);
977                         sizefac= Normalize(cmat[0])/cu->fsize;
978                         
979                         minx=miny= 1.0e20f;
980                         maxx=maxy= -1.0e20f;
981                         ct= chartransdata;
982                         for (i=0; i<=slen; i++, ct++) {
983                                 if(minx>ct->xof) minx= ct->xof;
984                                 if(maxx<ct->xof) maxx= ct->xof;
985                                 if(miny>ct->yof) miny= ct->yof;
986                                 if(maxy<ct->yof) maxy= ct->yof;
987                         }
988                         
989                         /* we put the x-coordinaat exact at the curve, the y is rotated */
990                         
991                         /* length correction */
992                         distfac= sizefac*cucu->path->totdist/(maxx-minx);
993                         timeofs= 0.0;
994                         
995                         if(distfac > 1.0) {
996                                 /* path longer than text: spacemode involves */
997                                 distfac= 1.0f/distfac;
998                                 
999                                 if(cu->spacemode==CU_RIGHT) {
1000                                         timeofs= 1.0f-distfac;
1001                                 }
1002                                 else if(cu->spacemode==CU_MIDDLE) {
1003                                         timeofs= (1.0f-distfac)/2.0f;
1004                                 }
1005                                 else if(cu->spacemode==CU_FLUSH) distfac= 1.0f;
1006                                 
1007                         }
1008                         else distfac= 1.0;
1009                         
1010                         distfac/= (maxx-minx);
1011                         
1012                         timeofs+= distfac*cu->xof;      /* not cyclic */
1013                         
1014                         ct= chartransdata;
1015                         for (i=0; i<=slen; i++, ct++) {
1016                                 float ctime, dtime, vec[4], tvec[4], rotvec[3];
1017                                 float si, co;
1018                                 
1019                                 /* rotate around center character */
1020                                 ascii = mem[i];
1021                                 
1022                                 // Find the character
1023                                 che = vfd->characters.first;
1024                                 while(che) {
1025                                         if(che->index == ascii)
1026                                                 break;
1027                                         che = che->next;
1028                                 }
1029         
1030                                 if(che)
1031                                         twidth = che->width;
1032                                 else
1033                                         twidth = 0;
1034                                 
1035                                 dtime= distfac*0.35f*twidth;    /* why not 0.5? */
1036                                 dtime= distfac*0.5f*twidth;     /* why not 0.5? */
1037                                 
1038                                 ctime= timeofs + distfac*( ct->xof - minx);
1039                                 CLAMP(ctime, 0.0, 1.0);
1040
1041                                 /* calc the right loc AND the right rot separately */
1042                                 /* vec, tvec need 4 items */
1043                                 where_on_path(cu->textoncurve, ctime, vec, tvec);
1044                                 where_on_path(cu->textoncurve, ctime+dtime, tvec, rotvec);
1045                                 
1046                                 VecMulf(vec, sizefac);
1047                                 
1048                                 ct->rot= (float)(M_PI-atan2(rotvec[1], rotvec[0]));
1049
1050                                 si= (float)sin(ct->rot);
1051                                 co= (float)cos(ct->rot);
1052
1053                                 yof= ct->yof;
1054                                 
1055                                 ct->xof= vec[0] + si*yof;
1056                                 ct->yof= vec[1] + co*yof;
1057                                 
1058                         }
1059                         cucu->flag= oldflag;
1060                 }
1061         }
1062
1063         if (cu->selboxes) {
1064                 ct= chartransdata;
1065                 for (i=0; i<=selend; i++, ct++) {
1066                         if (i>=selstart) {
1067                                 cu->selboxes[i-selstart].x = ct->xof*cu->fsize;
1068                                 cu->selboxes[i-selstart].y = ct->yof*cu->fsize;                         
1069                         }
1070                 }
1071         }
1072
1073         if(mode==FO_CURSUP || mode==FO_CURSDOWN || mode==FO_PAGEUP || mode==FO_PAGEDOWN) {
1074                 /* 2: curs up
1075                    3: curs down */
1076                 ct= chartransdata+cu->pos;
1077                 
1078                 if((mode==FO_CURSUP || mode==FO_PAGEUP) && ct->linenr==0);
1079                 else if((mode==FO_CURSDOWN || mode==FO_PAGEDOWN) && ct->linenr==lnr);
1080                 else {
1081                         switch(mode) {
1082                                 case FO_CURSUP:         lnr= ct->linenr-1; break;
1083                                 case FO_CURSDOWN:       lnr= ct->linenr+1; break;
1084                                 case FO_PAGEUP:         lnr= ct->linenr-10; break;
1085                                 case FO_PAGEDOWN:       lnr= ct->linenr+10; break;
1086                         }
1087                         cnr= ct->charnr;
1088                         /* seek for char with lnr en cnr */
1089                         cu->pos= 0;
1090                         ct= chartransdata;
1091                         for (i= 0; i<slen; i++) {
1092                                 if(ct->linenr==lnr) {
1093                                         if(ct->charnr==cnr) break;
1094                                         if( (ct+1)->charnr==0) break;
1095                                 }
1096                                 else if(ct->linenr>lnr) break;
1097                                 cu->pos++;
1098                                 ct++;
1099                         }
1100                 }
1101         }
1102         
1103         /* cursor first */
1104         if(cu->editfont) {
1105                 float si, co;
1106                 
1107                 ct= chartransdata+cu->pos;
1108                 si= (float)sin(ct->rot);
1109                 co= (float)cos(ct->rot);
1110                                 
1111                 f= cu->editfont->textcurs[0];
1112                 
1113                 f[0]= cu->fsize*(-0.1f*co + ct->xof);
1114                 f[1]= cu->fsize*(0.1f*si + ct->yof);
1115                 
1116                 f[2]= cu->fsize*(0.1f*co + ct->xof);
1117                 f[3]= cu->fsize*(-0.1f*si + ct->yof);
1118                 
1119                 f[4]= cu->fsize*( 0.1f*co + 0.8f*si + ct->xof);
1120                 f[5]= cu->fsize*(-0.1f*si + 0.8f*co + ct->yof);
1121                 
1122                 f[6]= cu->fsize*(-0.1f*co + 0.8f*si + ct->xof);
1123                 f[7]= cu->fsize*( 0.1f*si + 0.8f*co + ct->yof);
1124                 
1125         }
1126
1127         MEM_freeN(linedata);
1128         MEM_freeN(linedata2);           
1129         MEM_freeN(linedata3);
1130         MEM_freeN(linedata4);
1131
1132         if (mode == FO_SELCHANGE) {
1133                 MEM_freeN(chartransdata);
1134                 MEM_freeN(mem);
1135                 return NULL;
1136         }
1137
1138         if(mode==0) {
1139                 /* make nurbdata */
1140                 unsigned long cha;
1141                 
1142                 freeNurblist(&cu->nurb);
1143                 
1144                 ct= chartransdata;
1145                 if (cu->sepchar==0) {
1146                         for (i= 0; i<slen; i++) {
1147                                 cha = (uintptr_t) mem[i];
1148                                 info = &(cu->strinfo[i]);
1149                                 if (info->mat_nr > (ob->totcol)) {
1150                                         /* printf("Error: Illegal material index (%d) in text object, setting to 0\n", info->mat_nr); */
1151                                         info->mat_nr = 0;
1152                                 }
1153                                 // We do not want to see any character for \n or \r
1154                                 if(cha != '\n' && cha != '\r')
1155                                         buildchar(cu, cha, info, ct->xof, ct->yof, ct->rot, i);
1156                                 
1157                                 if ((info->flag & CU_UNDERLINE) && (cu->textoncurve == NULL) && (cha != '\n') && (cha != '\r')) {
1158                                         float ulwidth, uloverlap= 0.0f;
1159                                         
1160                                         if ( (i<(slen-1)) && (mem[i+1] != '\n') && (mem[i+1] != '\r') &&
1161                                                  ((mem[i+1] != ' ') || (custrinfo[i+1].flag & CU_UNDERLINE)) && ((custrinfo[i+1].flag & CU_WRAP)==0)
1162                                                  ) {
1163                                                 uloverlap = xtrax + 0.1;
1164                                         }
1165                                         // Find the character, the characters has to be in the memory already 
1166                                         // since character checking has been done earlier already.
1167                                         che = vfd->characters.first;
1168                                         while(che) {
1169                                                 if(che->index == cha)
1170                                                         break;
1171                                                 che = che->next;
1172                                         }
1173                                         
1174                                         if(!che) twidth =0; else twidth=che->width;
1175                                         ulwidth = cu->fsize * ((twidth* (1.0+(info->kern/40.0)))+uloverlap);
1176                                         build_underline(cu, ct->xof*cu->fsize, ct->yof*cu->fsize + (cu->ulpos-0.05)*cu->fsize, 
1177                                                                         ct->xof*cu->fsize + ulwidth, 
1178                                                                         ct->yof*cu->fsize + (cu->ulpos-0.05)*cu->fsize - cu->ulheight*cu->fsize, 
1179                                                                         i, info->mat_nr);
1180                                 }
1181                                 ct++;
1182                         }
1183                 }
1184                 else {
1185                         int outta = 0;
1186                         for (i= 0; (i<slen) && (outta==0); i++) {
1187                                 ascii = mem[i];
1188                                 info = &(custrinfo[i]);
1189                                 if (cu->sepchar == (i+1)) {
1190                                         float vecyo[3];
1191                                         
1192                                         mem[0] = ascii;
1193                                         mem[1] = 0;
1194                                         custrinfo[0]= *info;
1195                                         cu->pos = 1;
1196                                         cu->len = 1;
1197                                         vecyo[0] = ct->xof;
1198                                         vecyo[1] = ct->yof;
1199                                         vecyo[2] = 0;
1200                                         Mat4MulVecfl(ob->obmat, vecyo);
1201                                         VECCOPY(ob->loc, vecyo);
1202                                         outta = 1;
1203                                         cu->sepchar = 0;
1204                                 }
1205                                 ct++;
1206                         }
1207                 }
1208         }
1209
1210         if(mode==FO_DUPLI) {
1211                 MEM_freeN(mem);
1212                 return chartransdata;
1213         }
1214
1215         if(mem)
1216                 MEM_freeN(mem);
1217
1218         MEM_freeN(chartransdata);
1219         return 0;
1220 }
1221
1222