Merging r46725 through r46963 from trunk into soc-2011-tomato
[blender.git] / source / blender / blenkernel / intern / font.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/font.c
29  *  \ingroup bke
30  */
31
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <math.h>
36 #include <stdlib.h>
37 #include <wchar.h>
38 #include <wctype.h>
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_math.h"
43 #include "BLI_blenlib.h"
44 #include "BLI_vfontdata.h"
45 #include "BLI_utildefines.h"
46
47 #include "DNA_packedFile_types.h"
48 #include "DNA_curve_types.h"
49 #include "DNA_vfont_types.h"
50 #include "DNA_scene_types.h"
51 #include "DNA_object_types.h"
52
53 #include "BKE_utildefines.h"
54 #include "BKE_packedFile.h"
55 #include "BKE_library.h"
56 #include "BKE_font.h"
57 #include "BKE_global.h"
58 #include "BKE_main.h"
59 #include "BKE_anim.h"
60 #include "BKE_curve.h"
61 #include "BKE_displist.h"
62
63 static ListBase ttfdata = {NULL, NULL};
64
65 /* The vfont code */
66 void BKE_vfont_free(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_vfont_builtin_register(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 BKE_vfont_free_global_ttf(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 *BKE_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 = BKE_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 *BKE_vfont_load(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 = BKE_libblock_alloc(&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 *BKE_vfont_builtin_get(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 BKE_vfont_load(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_vfont_select_get(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_vfont_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_vfont_select_get(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                         {
712                                 maxlen = 0;
713                                 tb++;
714                                 curbox++;
715                                 yof = cu->yof + tb->y / cu->fsize;
716                         }
717
718                         /* XXX, has been unused for years, need to check if this is useful, r4613 r5282 - campbell */
719 #if 0
720                         if (ascii == '\n' || ascii == '\r')
721                                 xof = cu->xof;
722                         else
723                                 xof = cu->xof + (tb->x / cu->fsize);
724 #else
725                         xof = cu->xof + (tb->x / cu->fsize);
726 #endif
727                         lnr++;
728                         cnr = 0;
729                         wsnr = 0;
730                 }
731                 else if (ascii == 9) {    /* TAB */
732                         float tabfac;
733                         
734                         ct->xof = xof;
735                         ct->yof = yof;
736                         ct->linenr = lnr;
737                         ct->charnr = cnr++;
738
739                         tabfac = (xof - cu->xof + 0.01f);
740                         tabfac = 2.0f * ceilf(tabfac / 2.0f);
741                         xof = cu->xof + tabfac;
742                 }
743                 else {
744                         SelBox *sb = NULL;
745                         float wsfac;
746
747                         ct->xof = xof;
748                         ct->yof = yof;
749                         ct->linenr = lnr;
750                         ct->charnr = cnr++;
751
752                         if (cu->selboxes && (i >= selstart) && (i <= selend)) {
753                                 sb = &(cu->selboxes[i - selstart]);
754                                 sb->y = yof * cu->fsize - linedist * cu->fsize * 0.1f;
755                                 sb->h = linedist * cu->fsize;
756                                 sb->w = xof * cu->fsize;
757                         }
758         
759                         if (ascii == 32) {
760                                 wsfac = cu->wordspace; 
761                                 wsnr++;
762                         } 
763                         else wsfac = 1.0f;
764                         
765                         // Set the width of the character
766                         twidth = char_width(cu, che, info);
767
768                         xof += (twidth * wsfac * (1.0f + (info->kern / 40.0f)) ) + xtrax;
769                         
770                         if (sb) 
771                                 sb->w = (xof * cu->fsize) - sb->w;
772                 }
773                 ct++;
774         }
775         
776         cu->lines = 1;
777         ct = chartransdata;
778         tmp = mem;
779         for (i = 0; i <= slen; i++, tmp++, ct++) {
780                 ascii = *tmp;
781                 if (ascii == '\n' || ascii == '\r' || ct->dobreak) cu->lines++;
782         }       
783
784         // linedata is now: width of line
785         // linedata2 is now: number of characters
786         // linedata3 is now: maxlen of that line
787         // linedata4 is now: number of whitespaces of line
788
789         if (cu->spacemode != CU_LEFT) {
790                 ct = chartransdata;
791
792                 if (cu->spacemode == CU_RIGHT) {
793                         for (i = 0; i < lnr; i++) linedata[i] = linedata3[i] - linedata[i];
794                         for (i = 0; i <= slen; i++) {
795                                 ct->xof += linedata[ct->linenr];
796                                 ct++;
797                         }
798                 }
799                 else if (cu->spacemode == CU_MIDDLE) {
800                         for (i = 0; i < lnr; i++) linedata[i] = (linedata3[i] - linedata[i]) / 2;
801                         for (i = 0; i <= slen; i++) {
802                                 ct->xof += linedata[ct->linenr];
803                                 ct++;
804                         }
805                 }
806                 else if ((cu->spacemode == CU_FLUSH) && (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                                 {
834                                         if (mem[i] == ' ') curofs += (linedata3[ct->linenr] - linedata[ct->linenr]) / linedata4[ct->linenr];
835                                         ct->xof += curofs;
836                                 }
837                                 if (mem[i] == '\n' || mem[i] == '\r' || chartransdata[i].dobreak) curofs = 0;
838                                 ct++;
839                         }
840                 }
841         }
842         
843         /* TEXT ON CURVE */
844         /* Note: Only OB_CURVE objects could have a path  */
845         if (cu->textoncurve && cu->textoncurve->type == OB_CURVE) {
846                 Curve *cucu = cu->textoncurve->data;
847                 int oldflag = cucu->flag;
848                 
849                 cucu->flag |= (CU_PATH + CU_FOLLOW);
850                 
851                 if (cucu->path == NULL) BKE_displist_make_curveTypes(scene, cu->textoncurve, 0);
852                 if (cucu->path) {
853                         float distfac, imat[4][4], imat3[3][3], cmat[3][3];
854                         float minx, maxx, miny, maxy;
855                         float timeofs, sizefac;
856                         
857                         invert_m4_m4(imat, ob->obmat);
858                         copy_m3_m4(imat3, imat);
859
860                         copy_m3_m4(cmat, cu->textoncurve->obmat);
861                         mul_m3_m3m3(cmat, cmat, imat3);
862                         sizefac = normalize_v3(cmat[0]) / cu->fsize;
863                         
864                         minx = miny = 1.0e20f;
865                         maxx = maxy = -1.0e20f;
866                         ct = chartransdata;
867                         for (i = 0; i <= slen; i++, ct++) {
868                                 if (minx > ct->xof) minx = ct->xof;
869                                 if (maxx < ct->xof) maxx = ct->xof;
870                                 if (miny > ct->yof) miny = ct->yof;
871                                 if (maxy < ct->yof) maxy = ct->yof;
872                         }
873                         
874                         /* we put the x-coordinaat exact at the curve, the y is rotated */
875                         
876                         /* length correction */
877                         distfac = sizefac * cucu->path->totdist / (maxx - minx);
878                         timeofs = 0.0f;
879                         
880                         if (distfac > 1.0f) {
881                                 /* path longer than text: spacemode involves */
882                                 distfac = 1.0f / distfac;
883                                 
884                                 if (cu->spacemode == CU_RIGHT) {
885                                         timeofs = 1.0f - distfac;
886                                 }
887                                 else if (cu->spacemode == CU_MIDDLE) {
888                                         timeofs = (1.0f - distfac) / 2.0f;
889                                 }
890                                 else if (cu->spacemode == CU_FLUSH) distfac = 1.0f;
891                                 
892                         }
893                         else distfac = 1.0;
894                         
895                         distfac /= (maxx - minx);
896                         
897                         timeofs += distfac * cu->xof;  /* not cyclic */
898                         
899                         ct = chartransdata;
900                         for (i = 0; i <= slen; i++, ct++) {
901                                 float ctime, dtime, vec[4], tvec[4], rotvec[3];
902                                 float si, co;
903                                 
904                                 /* rotate around center character */
905                                 ascii = mem[i];
906
907                                 che = find_vfont_char(vfd, ascii);
908         
909                                 twidth = char_width(cu, che, info);
910
911                                 dtime = distfac * 0.5f * twidth;
912
913                                 ctime = timeofs + distfac * (ct->xof - minx);
914                                 CLAMP(ctime, 0.0f, 1.0f);
915
916                                 /* calc the right loc AND the right rot separately */
917                                 /* vec, tvec need 4 items */
918                                 where_on_path(cu->textoncurve, ctime, vec, tvec, NULL, NULL, NULL);
919                                 where_on_path(cu->textoncurve, ctime + dtime, tvec, rotvec, NULL, NULL, NULL);
920                                 
921                                 mul_v3_fl(vec, sizefac);
922                                 
923                                 ct->rot = (float)(M_PI - atan2(rotvec[1], rotvec[0]));
924
925                                 si = (float)sin(ct->rot);
926                                 co = (float)cos(ct->rot);
927
928                                 yof = ct->yof;
929                                 
930                                 ct->xof = vec[0] + si * yof;
931                                 ct->yof = vec[1] + co * yof;
932                                 
933                         }
934                         cucu->flag = oldflag;
935                 }
936         }
937
938         if (cu->selboxes) {
939                 ct = chartransdata;
940                 for (i = 0; i <= selend; i++, ct++) {
941                         if (i >= selstart) {
942                                 cu->selboxes[i - selstart].x = ct->xof * cu->fsize;
943                                 cu->selboxes[i - selstart].y = ct->yof * cu->fsize;
944                         }
945                 }
946         }
947
948         if (mode == FO_CURSUP || mode == FO_CURSDOWN || mode == FO_PAGEUP || mode == FO_PAGEDOWN) {
949                 /* 2: curs up
950                  * 3: curs down */
951                 ct = chartransdata + cu->pos;
952                 
953                 if ((mode == FO_CURSUP || mode == FO_PAGEUP) && ct->linenr == 0) ;
954                 else if ((mode == FO_CURSDOWN || mode == FO_PAGEDOWN) && ct->linenr == lnr) ;
955                 else {
956                         switch (mode) {
957                                 case FO_CURSUP:     lnr = ct->linenr - 1; break;
958                                 case FO_CURSDOWN:   lnr = ct->linenr + 1; break;
959                                 case FO_PAGEUP:     lnr = ct->linenr - 10; break;
960                                 case FO_PAGEDOWN:   lnr = ct->linenr + 10; break;
961                         }
962                         cnr = ct->charnr;
963                         /* seek for char with lnr en cnr */
964                         cu->pos = 0;
965                         ct = chartransdata;
966                         for (i = 0; i < slen; i++) {
967                                 if (ct->linenr == lnr) {
968                                         if (ct->charnr == cnr) break;
969                                         if ( (ct + 1)->charnr == 0) break;
970                                 }
971                                 else if (ct->linenr > lnr) break;
972                                 cu->pos++;
973                                 ct++;
974                         }
975                 }
976         }
977         
978         /* cursor first */
979         if (cu->editfont) {
980                 float si, co;
981                 
982                 ct = chartransdata + cu->pos;
983                 si = (float)sin(ct->rot);
984                 co = (float)cos(ct->rot);
985                                 
986                 f = cu->editfont->textcurs[0];
987                 
988                 f[0] = cu->fsize * (-0.1f * co + ct->xof);
989                 f[1] = cu->fsize * (0.1f * si + ct->yof);
990                 
991                 f[2] = cu->fsize * (0.1f * co + ct->xof);
992                 f[3] = cu->fsize * (-0.1f * si + ct->yof);
993                 
994                 f[4] = cu->fsize * (0.1f * co + 0.8f * si + ct->xof);
995                 f[5] = cu->fsize * (-0.1f * si + 0.8f * co + ct->yof);
996                 
997                 f[6] = cu->fsize * (-0.1f * co + 0.8f * si + ct->xof);
998                 f[7] = cu->fsize * (0.1f * si + 0.8f * co + ct->yof);
999                 
1000         }
1001
1002         MEM_freeN(linedata);
1003         MEM_freeN(linedata2);           
1004         MEM_freeN(linedata3);
1005         MEM_freeN(linedata4);
1006
1007         if (mode == FO_SELCHANGE) {
1008                 MEM_freeN(chartransdata);
1009                 MEM_freeN(mem);
1010                 return NULL;
1011         }
1012
1013         if (mode == FO_EDIT) {
1014                 /* make nurbdata */
1015                 BKE_nurbList_free(&cu->nurb);
1016                 
1017                 ct = chartransdata;
1018                 if (cu->sepchar == 0) {
1019                         for (i = 0; i < slen; i++) {
1020                                 unsigned long cha = (uintptr_t) mem[i];
1021                                 info = &(custrinfo[i]);
1022                                 if (info->mat_nr > (ob->totcol)) {
1023                                         /* printf("Error: Illegal material index (%d) in text object, setting to 0\n", info->mat_nr); */
1024                                         info->mat_nr = 0;
1025                                 }
1026                                 // We do not want to see any character for \n or \r
1027                                 if (cha != '\n' && cha != '\r')
1028                                         buildchar(bmain, cu, cha, info, ct->xof, ct->yof, ct->rot, i);
1029                                 
1030                                 if ((info->flag & CU_CHINFO_UNDERLINE) && (cu->textoncurve == NULL) && (cha != '\n') && (cha != '\r')) {
1031                                         float ulwidth, uloverlap = 0.0f;
1032                                         
1033                                         if ((i < (slen - 1)) && (mem[i + 1] != '\n') && (mem[i + 1] != '\r') &&
1034                                             ((mem[i + 1] != ' ') || (custrinfo[i + 1].flag & CU_CHINFO_UNDERLINE)) && ((custrinfo[i + 1].flag & CU_CHINFO_WRAP) == 0))
1035                                         {
1036                                                 uloverlap = xtrax + 0.1f;
1037                                         }
1038                                         // Find the character, the characters has to be in the memory already 
1039                                         // since character checking has been done earlier already.
1040                                         che = find_vfont_char(vfd, cha);
1041
1042                                         twidth = char_width(cu, che, info);
1043                                         ulwidth = cu->fsize * ((twidth * (1.0f + (info->kern / 40.0f))) + uloverlap);
1044                                         build_underline(cu, ct->xof * cu->fsize, ct->yof * cu->fsize + (cu->ulpos - 0.05f) * cu->fsize,
1045                                                         ct->xof * cu->fsize + ulwidth,
1046                                                         ct->yof * cu->fsize + (cu->ulpos - 0.05f) * cu->fsize - cu->ulheight * cu->fsize,
1047                                                         i, info->mat_nr);
1048                                 }
1049                                 ct++;
1050                         }
1051                 }
1052                 else {
1053                         int outta = 0;
1054                         for (i = 0; (i < slen) && (outta == 0); i++) {
1055                                 ascii = mem[i];
1056                                 info = &(custrinfo[i]);
1057                                 if (cu->sepchar == (i + 1)) {
1058                                         float vecyo[3];
1059
1060                                         vecyo[0] = ct->xof;
1061                                         vecyo[1] = ct->yof;
1062                                         vecyo[2] = 0.0f;
1063
1064                                         mem[0] = ascii;
1065                                         mem[1] = 0;
1066                                         custrinfo[0] = *info;
1067                                         cu->pos = 1;
1068                                         cu->len = 1;
1069                                         mul_v3_m4v3(ob->loc, ob->obmat, vecyo);
1070                                         outta = 1;
1071                                         cu->sepchar = 0;
1072                                 }
1073                                 ct++;
1074                         }
1075                 }
1076         }
1077
1078         if (mode == FO_DUPLI) {
1079                 MEM_freeN(mem);
1080                 return chartransdata;
1081         }
1082
1083         if (mem)
1084                 MEM_freeN(mem);
1085
1086         MEM_freeN(chartransdata);
1087         return NULL;
1088 }