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