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