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