tedious string copying changes
[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 (strcmp(vfont->name, FO_BUILTIN_NAME)==0) {
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, FO_BUILTIN_NAME);
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 (strcmp(name, FO_BUILTIN_NAME)==0) {
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, sizeof(vfont->id.name)-2);
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 FO_BUILTIN_NAME to temporary listbase
407                         if(strcmp(filename, FO_BUILTIN_NAME))
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 (strcmp(vf->name, FO_BUILTIN_NAME)==0)
447                         return vf;
448         
449         return load_vfont(FO_BUILTIN_NAME);
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;
518
519         vfd= vfont_get_data(which_vfont(cu, info));     
520         if (!vfd) return;
521
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
533         /* make a copy at distance ofsx,ofsy with shear*/
534         fsize= cu->fsize;
535         shear= cu->shear;
536         si= (float)sin(rot);
537         co= (float)cos(rot);
538
539         che= find_vfont_char(vfd, character);
540         
541         // Select the glyph data
542         if(che)
543                 nu1 = che->nurbsbase.first;
544
545         // Create the character
546         while(nu1)
547         {
548                 bezt1 = nu1->bezt;
549                 if (bezt1){
550                         nu2 =(Nurb*) MEM_mallocN(sizeof(Nurb),"duplichar_nurb");
551                         if (nu2 == 0) break;
552                         memcpy(nu2, nu1, sizeof(struct Nurb));
553                         nu2->resolu= cu->resolu;
554                         nu2->bp = 0;
555                         nu2->knotsu = nu2->knotsv = NULL;
556                         nu2->flag= CU_SMOOTH;
557                         nu2->charidx = charidx;
558                         if (info->mat_nr) {
559                                 nu2->mat_nr= info->mat_nr-1;
560                         }
561                         else {
562                                 nu2->mat_nr= 0;
563                         }
564                         /* nu2->trim.first = 0; */
565                         /* nu2->trim.last = 0; */
566                         i = nu2->pntsu;
567
568                         bezt2 = (BezTriple*)MEM_mallocN(i * sizeof(BezTriple),"duplichar_bezt2"); 
569                         if (bezt2 == 0){
570                                 MEM_freeN(nu2);
571                                 break;
572                         }
573                         memcpy(bezt2, bezt1, i * sizeof(struct BezTriple));
574                         nu2->bezt = bezt2;
575                         
576                         if (shear != 0.0) {
577                                 bezt2 = nu2->bezt;
578                                 
579                                 for (i= nu2->pntsu; i > 0; i--) {
580                                         bezt2->vec[0][0] += shear * bezt2->vec[0][1];
581                                         bezt2->vec[1][0] += shear * bezt2->vec[1][1];
582                                         bezt2->vec[2][0] += shear * bezt2->vec[2][1];
583                                         bezt2++;
584                                 }
585                         }
586                         if(rot!=0.0) {
587                                 bezt2= nu2->bezt;
588                                 for (i=nu2->pntsu; i > 0; i--) {
589                                         fp= bezt2->vec[0];
590                                         
591                                         x= fp[0];
592                                         fp[0]= co*x + si*fp[1];
593                                         fp[1]= -si*x + co*fp[1];
594                                         x= fp[3];
595                                         fp[3]= co*x + si*fp[4];
596                                         fp[4]= -si*x + co*fp[4];
597                                         x= fp[6];
598                                         fp[6]= co*x + si*fp[7];
599                                         fp[7]= -si*x + co*fp[7];
600
601                                         bezt2++;
602                                 }
603                         }
604                         bezt2 = nu2->bezt;
605
606                         if(info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
607                                 const float sca= cu->smallcaps_scale;
608                                 for (i= nu2->pntsu; i > 0; i--) {
609                                         fp= bezt2->vec[0];
610                                         fp[0] *= sca;
611                                         fp[1] *= sca;
612                                         fp[3] *= sca;
613                                         fp[4] *= sca;
614                                         fp[6] *= sca;
615                                         fp[7] *= sca;
616                                         bezt2++;
617                                 }
618                         }
619                         bezt2 = nu2->bezt;
620
621                         for (i= nu2->pntsu; i > 0; i--) {
622                                 fp= bezt2->vec[0];
623                                 fp[0]= (fp[0]+ofsx)*fsize;
624                                 fp[1]= (fp[1]+ofsy)*fsize;
625                                 fp[3]= (fp[3]+ofsx)*fsize;
626                                 fp[4]= (fp[4]+ofsy)*fsize;
627                                 fp[6]= (fp[6]+ofsx)*fsize;
628                                 fp[7]= (fp[7]+ofsy)*fsize;
629                                 bezt2++;
630                         }
631                         
632                         BLI_addtail(&(cu->nurb), nu2);
633                 }
634                 
635                 nu1 = nu1->next;
636         }
637 }
638
639 int BKE_font_getselection(Object *ob, int *start, int *end)
640 {
641         Curve *cu= ob->data;
642         
643         if (cu->editfont==NULL || ob->type != OB_FONT) return 0;
644
645         if (cu->selstart == 0) return 0;
646         if (cu->selstart <= cu->selend) {
647                 *start = cu->selstart-1;
648                 *end = cu->selend-1;
649                 return 1;
650         }
651         else {
652                 *start = cu->selend;
653                 *end = cu->selstart-2;
654                 return -1;
655         }
656 }
657
658 static float char_width(Curve *cu, VChar *che, CharInfo *info)
659 {
660         // The character wasn't found, propably ascii = 0, then the width shall be 0 as well
661         if(che == NULL) {
662                 return 0.0f;
663         }
664         else if(info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
665                 return che->width * cu->smallcaps_scale;
666         }
667         else {
668                 return che->width;
669         }
670 }
671
672 struct chartrans *BKE_text_to_curve(Scene *scene, Object *ob, int mode) 
673 {
674         VFont *vfont, *oldvfont;
675         VFontData *vfd= NULL;
676         Curve *cu;
677         CharInfo *info, *custrinfo;
678         TextBox *tb;
679         VChar *che;
680         struct chartrans *chartransdata=NULL, *ct;
681         float *f, xof, yof, xtrax, linedist, *linedata, *linedata2, *linedata3, *linedata4;
682         float twidth, maxlen= 0;
683         int i, slen, j;
684         int curbox;
685         int selstart, selend;
686         int utf8len;
687         short cnr=0, lnr=0, wsnr= 0;
688         wchar_t *mem, *tmp, ascii;
689
690         /* renark: do calculations including the trailing '\0' of a string
691            because the cursor can be at that location */
692
693         if(ob->type!=OB_FONT) return 0;
694
695         // Set font data
696         cu= (Curve *) ob->data;
697         vfont= cu->vfont;
698         
699         if(cu->str == NULL) return 0;
700         if(vfont == NULL) return 0;
701
702         // Create unicode string
703         utf8len = utf8slen(cu->str);
704         tmp = mem = MEM_callocN(((utf8len + 1) * sizeof(wchar_t)), "convertedmem");
705         
706         utf8towchar(mem, cu->str);
707
708         // Count the wchar_t string length
709         slen = wcslen(mem);
710
711         if (cu->ulheight == 0.0) 
712                 cu->ulheight = 0.05;
713         
714         if (cu->strinfo==NULL)  /* old file */
715                 cu->strinfo = MEM_callocN((slen+4) * sizeof(CharInfo), "strinfo compat");
716         
717         custrinfo= cu->strinfo;
718         if (cu->editfont)
719                 custrinfo= cu->editfont->textbufinfo;
720         
721         if (cu->tb==NULL)
722                 cu->tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "TextBox compat");
723
724         vfd= vfont_get_data(vfont);
725
726         /* The VFont Data can not be found */
727         if(!vfd) {
728                 if(mem)
729                         MEM_freeN(mem); 
730                 return 0;
731         }
732
733         /* calc offset and rotation of each char */
734         ct = chartransdata =
735                 (struct chartrans*)MEM_callocN((slen+1)* sizeof(struct chartrans),"buildtext");
736
737         /* We assume the worst case: 1 character per line (is freed at end anyway) */
738
739         linedata= MEM_mallocN(sizeof(float)*(slen*2 + 1),"buildtext2");
740         linedata2= MEM_mallocN(sizeof(float)*(slen*2 + 1),"buildtext3");
741         linedata3= MEM_callocN(sizeof(float)*(slen*2 + 1),"buildtext4");        
742         linedata4= MEM_callocN(sizeof(float)*(slen*2 + 1),"buildtext5");                
743         
744         linedist= cu->linedist;
745         
746         xof= cu->xof + (cu->tb[0].x/cu->fsize);
747         yof= cu->yof + (cu->tb[0].y/cu->fsize);
748
749         xtrax= 0.5f*cu->spacing-0.5f;
750
751         oldvfont = NULL;
752
753         for (i=0; i<slen; i++) custrinfo[i].flag &= ~(CU_CHINFO_WRAP|CU_CHINFO_SMALLCAPS_CHECK);
754
755         if (cu->selboxes) MEM_freeN(cu->selboxes);
756         cu->selboxes = NULL;
757         if (BKE_font_getselection(ob, &selstart, &selend))
758                 cu->selboxes = MEM_callocN((selend-selstart+1)*sizeof(SelBox), "font selboxes");
759
760         tb = &(cu->tb[0]);
761         curbox= 0;
762         for (i = 0 ; i<=slen ; i++) {
763         makebreak:
764                 // Characters in the list
765                 che = vfd->characters.first;
766                 info = &(custrinfo[i]);
767                 ascii = mem[i];
768                 if(info->flag & CU_CHINFO_SMALLCAPS) {
769                         ascii = towupper(ascii);
770                         if(mem[i] != ascii) {
771                                 mem[i]= ascii;
772                                 info->flag |= CU_CHINFO_SMALLCAPS_CHECK;
773                         }
774                 }
775
776                 vfont = which_vfont(cu, info);
777                 
778                 if(vfont==NULL) break;
779
780                 che= find_vfont_char(vfd, ascii);
781
782                 /*
783                  * The character wasn't in the current curve base so load it
784                  * But if the font is FO_BUILTIN_NAME then do not try loading since
785                  * whole font is in the memory already
786                  */
787                 if(che == NULL && strcmp(vfont->name, FO_BUILTIN_NAME)) {
788                         BLI_vfontchar_from_freetypefont(vfont, ascii);
789                 }
790
791                 /* Try getting the character again from the list */
792                 che= find_vfont_char(vfd, ascii);
793
794                 /* No VFont found */
795                 if (vfont==0) {
796                         if(mem)
797                                 MEM_freeN(mem);
798                         MEM_freeN(chartransdata);
799                         return 0;
800                 }
801
802                 if (vfont != oldvfont) {
803                         vfd= vfont_get_data(vfont);
804                         oldvfont = vfont;
805                 }
806
807                 /* VFont Data for VFont couldn't be found */
808                 if (!vfd) {
809                         if(mem)
810                                 MEM_freeN(mem);
811                         MEM_freeN(chartransdata);
812                         return 0;
813                 }
814
815                 twidth = char_width(cu, che, info);
816
817                 // Calculate positions
818                 if((tb->w != 0.0) && (ct->dobreak==0) && ((xof-(tb->x/cu->fsize)+twidth)*cu->fsize) > tb->w + cu->xof*cu->fsize) {
819         //              fprintf(stderr, "linewidth exceeded: %c%c%c...\n", mem[i], mem[i+1], mem[i+2]);
820                         for (j=i; j && (mem[j] != '\n') && (mem[j] != '\r') && (chartransdata[j].dobreak==0); j--) {
821                                 if (mem[j]==' ' || mem[j]=='-') {
822                                         ct -= (i-(j-1));
823                                         cnr -= (i-(j-1));
824                                         if (mem[j] == ' ') wsnr--;
825                                         if (mem[j] == '-') wsnr++;
826                                         i = j-1;
827                                         xof = ct->xof;
828                                         ct[1].dobreak = 1;
829                                         custrinfo[i+1].flag |= CU_CHINFO_WRAP;
830                                         goto makebreak;
831                                 }
832                                 if (chartransdata[j].dobreak) {
833         //                              fprintf(stderr, "word too long: %c%c%c...\n", mem[j], mem[j+1], mem[j+2]);
834                                         ct->dobreak= 1;
835                                         custrinfo[i+1].flag |= CU_CHINFO_WRAP;
836                                         ct -= 1;
837                                         cnr -= 1;
838                                         i--;
839                                         xof = ct->xof;
840                                         goto makebreak;
841                                 }
842                         }
843                 }
844                 if(ascii== '\n' || ascii== '\r' || ascii==0 || ct->dobreak) {
845                         ct->xof= xof;
846                         ct->yof= yof;
847                         ct->linenr= lnr;
848                         ct->charnr= cnr;
849                         
850                         yof-= linedist;
851                         
852                         maxlen= MAX2(maxlen, (xof-tb->x/cu->fsize));
853                         linedata[lnr]= xof-tb->x/cu->fsize;
854                         linedata2[lnr]= cnr;
855                         linedata3[lnr]= tb->w/cu->fsize;
856                         linedata4[lnr]= wsnr;
857                         
858                         if ( (tb->h != 0.0) &&
859                                  ((-(yof-(tb->y/cu->fsize))) > ((tb->h/cu->fsize)-(linedist*cu->fsize)) - cu->yof) &&
860                                  (cu->totbox > (curbox+1)) ) {
861                                 maxlen= 0;
862                                 tb++;
863                                 curbox++;
864                                 yof= cu->yof + tb->y/cu->fsize;
865                         }
866
867                         if(ascii == '\n' || ascii == '\r')
868                                 xof = cu->xof;
869                         else
870                                 xof= cu->xof + (tb->x/cu->fsize);
871
872                         xof= cu->xof + (tb->x/cu->fsize);
873                         lnr++;
874                         cnr= 0;
875                         wsnr= 0;
876                 }
877                 else if(ascii==9) {     /* TAB */
878                         float tabfac;
879                         
880                         ct->xof= xof;
881                         ct->yof= yof;
882                         ct->linenr= lnr;
883                         ct->charnr= cnr++;
884
885                         tabfac= (xof-cu->xof+0.01f);
886                         tabfac= (float)(2.0*ceil(tabfac/2.0));
887                         xof= cu->xof+tabfac;
888                 }
889                 else {
890                         SelBox *sb= NULL;
891                         float wsfac;
892
893                         ct->xof= xof;
894                         ct->yof= yof;
895                         ct->linenr= lnr;
896                         ct->charnr= cnr++;
897
898                         if (cu->selboxes && (i>=selstart) && (i<=selend)) {
899                                 sb = &(cu->selboxes[i-selstart]);
900                                 sb->y = yof*cu->fsize-linedist*cu->fsize*0.1;
901                                 sb->h = linedist*cu->fsize;
902                                 sb->w = xof*cu->fsize;
903                         }
904         
905                         if (ascii==32) {
906                                 wsfac = cu->wordspace; 
907                                 wsnr++;
908                         } 
909                         else wsfac = 1.0;
910                         
911                         // Set the width of the character
912                         twidth = char_width(cu, che, info);
913
914                         xof += (twidth*wsfac*(1.0+(info->kern/40.0)) ) + xtrax;
915                         
916                         if (sb) 
917                                 sb->w = (xof*cu->fsize) - sb->w;
918                 }
919                 ct++;
920         }
921         
922         cu->lines= 1;
923         ct= chartransdata;
924         tmp = mem;
925         for (i= 0; i<=slen; i++, tmp++, ct++) {
926                 ascii = *tmp;
927                 if(ascii== '\n' || ascii== '\r' || ct->dobreak) cu->lines++;
928         }       
929
930         // linedata is now: width of line
931         // linedata2 is now: number of characters
932         // linedata3 is now: maxlen of that line
933         // linedata4 is now: number of whitespaces of line
934
935         if(cu->spacemode!=CU_LEFT) {
936                 ct= chartransdata;
937
938                 if(cu->spacemode==CU_RIGHT) {
939                         for(i=0;i<lnr;i++) linedata[i]= linedata3[i]-linedata[i];
940                         for (i=0; i<=slen; i++) {
941                                 ct->xof+= linedata[ct->linenr];
942                                 ct++;
943                         }
944                 } else if(cu->spacemode==CU_MIDDLE) {
945                         for(i=0;i<lnr;i++) linedata[i]= (linedata3[i]-linedata[i])/2;
946                         for (i=0; i<=slen; i++) {
947                                 ct->xof+= linedata[ct->linenr];
948                                 ct++;
949                         }
950                 } else if((cu->spacemode==CU_FLUSH) &&
951                                   (cu->tb[0].w != 0.0)) {
952                         for(i=0;i<lnr;i++)
953                                 if(linedata2[i]>1)
954                                         linedata[i]= (linedata3[i]-linedata[i])/(linedata2[i]-1);
955                         for (i=0; i<=slen; i++) {
956                                 for (j=i; (mem[j]) && (mem[j]!='\n') && 
957                                                   (mem[j]!='\r') && (chartransdata[j].dobreak==0) && (j<slen); j++);
958 //                              if ((mem[j]!='\r') && (mem[j]!='\n') && (mem[j])) {
959                                         ct->xof+= ct->charnr*linedata[ct->linenr];
960 //                              }
961                                 ct++;
962                         }
963                 } 
964                 else if((cu->spacemode==CU_JUSTIFY) && (cu->tb[0].w != 0.0)) {
965                         float curofs= 0.0f;
966                         for (i=0; i<=slen; i++) {
967                                 for (j=i; (mem[j]) && (mem[j]!='\n') && 
968                                                   (mem[j]!='\r') && (chartransdata[j].dobreak==0) && (j<slen); j++);
969                                 if ((mem[j]!='\r') && (mem[j]!='\n') &&
970                                         ((chartransdata[j].dobreak!=0))) {
971                                         if (mem[i]==' ') curofs += (linedata3[ct->linenr]-linedata[ct->linenr])/linedata4[ct->linenr];
972                                         ct->xof+= curofs;
973                                 }
974                                 if (mem[i]=='\n' || mem[i]=='\r' || chartransdata[i].dobreak) curofs= 0;
975                                 ct++;
976                         }                       
977                 }
978         }
979         
980         /* TEXT ON CURVE */
981         /* Note: Only OB_CURVE objects could have a path  */
982         if(cu->textoncurve && cu->textoncurve->type==OB_CURVE) {
983                 Curve *cucu= cu->textoncurve->data;
984                 int oldflag= cucu->flag;
985                 
986                 cucu->flag |= (CU_PATH+CU_FOLLOW);
987                 
988                 if(cucu->path==NULL) makeDispListCurveTypes(scene, cu->textoncurve, 0);
989                 if(cucu->path) {
990                         float distfac, imat[4][4], imat3[3][3], cmat[3][3];
991                         float minx, maxx, miny, maxy;
992                         float timeofs, sizefac;
993                         
994                         invert_m4_m4(imat, ob->obmat);
995                         copy_m3_m4(imat3, imat);
996
997                         copy_m3_m4(cmat, cu->textoncurve->obmat);
998                         mul_m3_m3m3(cmat, cmat, imat3);
999                         sizefac= normalize_v3(cmat[0])/cu->fsize;
1000                         
1001                         minx=miny= 1.0e20f;
1002                         maxx=maxy= -1.0e20f;
1003                         ct= chartransdata;
1004                         for (i=0; i<=slen; i++, ct++) {
1005                                 if(minx>ct->xof) minx= ct->xof;
1006                                 if(maxx<ct->xof) maxx= ct->xof;
1007                                 if(miny>ct->yof) miny= ct->yof;
1008                                 if(maxy<ct->yof) maxy= ct->yof;
1009                         }
1010                         
1011                         /* we put the x-coordinaat exact at the curve, the y is rotated */
1012                         
1013                         /* length correction */
1014                         distfac= sizefac*cucu->path->totdist/(maxx-minx);
1015                         timeofs= 0.0;
1016                         
1017                         if(distfac > 1.0) {
1018                                 /* path longer than text: spacemode involves */
1019                                 distfac= 1.0f/distfac;
1020                                 
1021                                 if(cu->spacemode==CU_RIGHT) {
1022                                         timeofs= 1.0f-distfac;
1023                                 }
1024                                 else if(cu->spacemode==CU_MIDDLE) {
1025                                         timeofs= (1.0f-distfac)/2.0f;
1026                                 }
1027                                 else if(cu->spacemode==CU_FLUSH) distfac= 1.0f;
1028                                 
1029                         }
1030                         else distfac= 1.0;
1031                         
1032                         distfac/= (maxx-minx);
1033                         
1034                         timeofs+= distfac*cu->xof;      /* not cyclic */
1035                         
1036                         ct= chartransdata;
1037                         for (i=0; i<=slen; i++, ct++) {
1038                                 float ctime, dtime, vec[4], tvec[4], rotvec[3];
1039                                 float si, co;
1040                                 
1041                                 /* rotate around center character */
1042                                 ascii = mem[i];
1043
1044                                 che= find_vfont_char(vfd, ascii);
1045         
1046                                 twidth = char_width(cu, che, info);
1047                                 
1048                                 dtime= distfac*0.35f*twidth;    /* why not 0.5? */
1049                                 dtime= distfac*0.5f*twidth;     /* why not 0.5? */
1050                                 
1051                                 ctime= timeofs + distfac*( ct->xof - minx);
1052                                 CLAMP(ctime, 0.0, 1.0);
1053
1054                                 /* calc the right loc AND the right rot separately */
1055                                 /* vec, tvec need 4 items */
1056                                 where_on_path(cu->textoncurve, ctime, vec, tvec, NULL, NULL, NULL);
1057                                 where_on_path(cu->textoncurve, ctime+dtime, tvec, rotvec, NULL, NULL, NULL);
1058                                 
1059                                 mul_v3_fl(vec, sizefac);
1060                                 
1061                                 ct->rot= (float)(M_PI-atan2(rotvec[1], rotvec[0]));
1062
1063                                 si= (float)sin(ct->rot);
1064                                 co= (float)cos(ct->rot);
1065
1066                                 yof= ct->yof;
1067                                 
1068                                 ct->xof= vec[0] + si*yof;
1069                                 ct->yof= vec[1] + co*yof;
1070                                 
1071                         }
1072                         cucu->flag= oldflag;
1073                 }
1074         }
1075
1076         if (cu->selboxes) {
1077                 ct= chartransdata;
1078                 for (i=0; i<=selend; i++, ct++) {
1079                         if (i>=selstart) {
1080                                 cu->selboxes[i-selstart].x = ct->xof*cu->fsize;
1081                                 cu->selboxes[i-selstart].y = ct->yof*cu->fsize;                         
1082                         }
1083                 }
1084         }
1085
1086         if(mode==FO_CURSUP || mode==FO_CURSDOWN || mode==FO_PAGEUP || mode==FO_PAGEDOWN) {
1087                 /* 2: curs up
1088                    3: curs down */
1089                 ct= chartransdata+cu->pos;
1090                 
1091                 if((mode==FO_CURSUP || mode==FO_PAGEUP) && ct->linenr==0);
1092                 else if((mode==FO_CURSDOWN || mode==FO_PAGEDOWN) && ct->linenr==lnr);
1093                 else {
1094                         switch(mode) {
1095                                 case FO_CURSUP:         lnr= ct->linenr-1; break;
1096                                 case FO_CURSDOWN:       lnr= ct->linenr+1; break;
1097                                 case FO_PAGEUP:         lnr= ct->linenr-10; break;
1098                                 case FO_PAGEDOWN:       lnr= ct->linenr+10; break;
1099                         }
1100                         cnr= ct->charnr;
1101                         /* seek for char with lnr en cnr */
1102                         cu->pos= 0;
1103                         ct= chartransdata;
1104                         for (i= 0; i<slen; i++) {
1105                                 if(ct->linenr==lnr) {
1106                                         if(ct->charnr==cnr) break;
1107                                         if( (ct+1)->charnr==0) break;
1108                                 }
1109                                 else if(ct->linenr>lnr) break;
1110                                 cu->pos++;
1111                                 ct++;
1112                         }
1113                 }
1114         }
1115         
1116         /* cursor first */
1117         if(cu->editfont) {
1118                 float si, co;
1119                 
1120                 ct= chartransdata+cu->pos;
1121                 si= (float)sin(ct->rot);
1122                 co= (float)cos(ct->rot);
1123                                 
1124                 f= cu->editfont->textcurs[0];
1125                 
1126                 f[0]= cu->fsize*(-0.1f*co + ct->xof);
1127                 f[1]= cu->fsize*(0.1f*si + ct->yof);
1128                 
1129                 f[2]= cu->fsize*(0.1f*co + ct->xof);
1130                 f[3]= cu->fsize*(-0.1f*si + ct->yof);
1131                 
1132                 f[4]= cu->fsize*( 0.1f*co + 0.8f*si + ct->xof);
1133                 f[5]= cu->fsize*(-0.1f*si + 0.8f*co + ct->yof);
1134                 
1135                 f[6]= cu->fsize*(-0.1f*co + 0.8f*si + ct->xof);
1136                 f[7]= cu->fsize*( 0.1f*si + 0.8f*co + ct->yof);
1137                 
1138         }
1139
1140         MEM_freeN(linedata);
1141         MEM_freeN(linedata2);           
1142         MEM_freeN(linedata3);
1143         MEM_freeN(linedata4);
1144
1145         if (mode == FO_SELCHANGE) {
1146                 MEM_freeN(chartransdata);
1147                 MEM_freeN(mem);
1148                 return NULL;
1149         }
1150
1151         if(mode == FO_EDIT) {
1152                 /* make nurbdata */
1153                 freeNurblist(&cu->nurb);
1154                 
1155                 ct= chartransdata;
1156                 if (cu->sepchar==0) {
1157                         for (i= 0; i<slen; i++) {
1158                                 unsigned long 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];
1197
1198                                         vecyo[0]= ct->xof;
1199                                         vecyo[1]= ct->yof;
1200                                         vecyo[2]= 0.0f;
1201
1202                                         mem[0] = ascii;
1203                                         mem[1] = 0;
1204                                         custrinfo[0]= *info;
1205                                         cu->pos = 1;
1206                                         cu->len = 1;
1207                                         mul_v3_m4v3(ob->loc, ob->obmat, vecyo);
1208                                         outta = 1;
1209                                         cu->sepchar = 0;
1210                                 }
1211                                 ct++;
1212                         }
1213                 }
1214         }
1215
1216         if(mode==FO_DUPLI) {
1217                 MEM_freeN(mem);
1218                 return chartransdata;
1219         }
1220
1221         if(mem)
1222                 MEM_freeN(mem);
1223
1224         MEM_freeN(chartransdata);
1225         return 0;
1226 }
1227
1228