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