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