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