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