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