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