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