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