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