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