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