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