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