Style cleanup: displist module
[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         VFont *vfont, *oldvfont;
525         VFontData *vfd = NULL;
526         Curve *cu;
527         CharInfo *info = NULL, *custrinfo;
528         TextBox *tb;
529         VChar *che;
530         struct chartrans *chartransdata = NULL, *ct;
531         float *f, xof, yof, xtrax, linedist, *linedata, *linedata2, *linedata3, *linedata4;
532         float twidth, maxlen = 0;
533         int i, slen, j;
534         int curbox;
535         int selstart, selend;
536         int utf8len;
537         short cnr = 0, lnr = 0, wsnr = 0;
538         wchar_t *mem, *tmp, ascii;
539
540         /* renark: do calculations including the trailing '\0' of a string
541          * because the cursor can be at that location */
542
543         if (ob->type != OB_FONT) return NULL;
544
545         // Set font data
546         cu = (Curve *) ob->data;
547         vfont = cu->vfont;
548         
549         if (cu->str == NULL) return NULL;
550         if (vfont == NULL) return NULL;
551
552         // Create unicode string
553         utf8len = BLI_strlen_utf8(cu->str);
554         mem = MEM_callocN(((utf8len + 1) * sizeof(wchar_t)), "convertedmem");
555         
556         BLI_strncpy_wchar_from_utf8(mem, cu->str, utf8len + 1);
557
558         // Count the wchar_t string length
559         slen = wcslen(mem);
560
561         if (cu->ulheight == 0.0f)
562                 cu->ulheight = 0.05f;
563         
564         if (cu->strinfo == NULL) /* old file */
565                 cu->strinfo = MEM_callocN((slen + 4) * sizeof(CharInfo), "strinfo compat");
566         
567         custrinfo = cu->strinfo;
568         if (cu->editfont)
569                 custrinfo = cu->editfont->textbufinfo;
570         
571         if (cu->tb == NULL)
572                 cu->tb = MEM_callocN(MAXTEXTBOX * sizeof(TextBox), "TextBox compat");
573
574         vfd = vfont_get_data(bmain, vfont);
575
576         /* The VFont Data can not be found */
577         if (!vfd) {
578                 if (mem)
579                         MEM_freeN(mem); 
580                 return NULL;
581         }
582
583         /* calc offset and rotation of each char */
584         ct = chartransdata =
585                  (struct chartrans *)MEM_callocN((slen + 1) * sizeof(struct chartrans), "buildtext");
586
587         /* We assume the worst case: 1 character per line (is freed at end anyway) */
588
589         linedata  = MEM_mallocN(sizeof(float) * (slen * 2 + 1), "buildtext2");
590         linedata2 = MEM_mallocN(sizeof(float) * (slen * 2 + 1), "buildtext3");
591         linedata3 = MEM_callocN(sizeof(float) * (slen * 2 + 1), "buildtext4");
592         linedata4 = MEM_callocN(sizeof(float) * (slen * 2 + 1), "buildtext5");
593         
594         linedist = cu->linedist;
595         
596         xof = cu->xof + (cu->tb[0].x / cu->fsize);
597         yof = cu->yof + (cu->tb[0].y / cu->fsize);
598
599         xtrax = 0.5f * cu->spacing - 0.5f;
600
601         oldvfont = NULL;
602
603         for (i = 0; i < slen; i++) custrinfo[i].flag &= ~(CU_CHINFO_WRAP | CU_CHINFO_SMALLCAPS_CHECK);
604
605         if (cu->selboxes) MEM_freeN(cu->selboxes);
606         cu->selboxes = NULL;
607         if (BKE_vfont_select_get(ob, &selstart, &selend))
608                 cu->selboxes = MEM_callocN((selend - selstart + 1) * sizeof(SelBox), "font selboxes");
609
610         tb = &(cu->tb[0]);
611         curbox = 0;
612         for (i = 0; i <= slen; i++) {
613 makebreak:
614                 // Characters in the list
615                 info = &(custrinfo[i]);
616                 ascii = mem[i];
617                 if (info->flag & CU_CHINFO_SMALLCAPS) {
618                         ascii = towupper(ascii);
619                         if (mem[i] != ascii) {
620                                 mem[i] = ascii;
621                                 info->flag |= CU_CHINFO_SMALLCAPS_CHECK;
622                         }
623                 }
624
625                 vfont = which_vfont(cu, info);
626                 
627                 if (vfont == NULL) break;
628
629                 che = find_vfont_char(vfd, ascii);
630
631                 /*
632                  * The character wasn't in the current curve base so load it
633                  * But if the font is FO_BUILTIN_NAME then do not try loading since
634                  * whole font is in the memory already
635                  */
636                 if (che == NULL && strcmp(vfont->name, FO_BUILTIN_NAME))    {
637                         BLI_vfontchar_from_freetypefont(vfont, ascii);
638                 }
639
640                 /* Try getting the character again from the list */
641                 che = find_vfont_char(vfd, ascii);
642
643                 /* No VFont found */
644                 if (vfont == NULL) {
645                         if (mem)
646                                 MEM_freeN(mem);
647                         MEM_freeN(chartransdata);
648                         return NULL;
649                 }
650
651                 if (vfont != oldvfont) {
652                         vfd = vfont_get_data(bmain, vfont);
653                         oldvfont = vfont;
654                 }
655
656                 /* VFont Data for VFont couldn't be found */
657                 if (!vfd) {
658                         if (mem)
659                                 MEM_freeN(mem);
660                         MEM_freeN(chartransdata);
661                         return NULL;
662                 }
663
664                 twidth = char_width(cu, che, info);
665
666                 // Calculate positions
667                 if ((tb->w != 0.0f) && (ct->dobreak == 0) && ((xof - (tb->x / cu->fsize) + twidth) * cu->fsize) > tb->w + cu->xof * cu->fsize) {
668                         //              fprintf(stderr, "linewidth exceeded: %c%c%c...\n", mem[i], mem[i+1], mem[i+2]);
669                         for (j = i; j && (mem[j] != '\n') && (mem[j] != '\r') && (chartransdata[j].dobreak == 0); j--) {
670                                 if (mem[j] == ' ' || mem[j] == '-') {
671                                         ct -= (i - (j - 1));
672                                         cnr -= (i - (j - 1));
673                                         if (mem[j] == ' ') wsnr--;
674                                         if (mem[j] == '-') wsnr++;
675                                         i = j - 1;
676                                         xof = ct->xof;
677                                         ct[1].dobreak = 1;
678                                         custrinfo[i + 1].flag |= CU_CHINFO_WRAP;
679                                         goto makebreak;
680                                 }
681                                 if (chartransdata[j].dobreak) {
682                                         //                              fprintf(stderr, "word too long: %c%c%c...\n", mem[j], mem[j+1], mem[j+2]);
683                                         ct->dobreak = 1;
684                                         custrinfo[i + 1].flag |= CU_CHINFO_WRAP;
685                                         ct -= 1;
686                                         cnr -= 1;
687                                         i--;
688                                         xof = ct->xof;
689                                         goto makebreak;
690                                 }
691                         }
692                 }
693                 if (ascii == '\n' || ascii == '\r' || ascii == 0 || ct->dobreak) {
694                         ct->xof = xof;
695                         ct->yof = yof;
696                         ct->linenr = lnr;
697                         ct->charnr = cnr;
698
699                         yof -= linedist;
700
701                         maxlen = MAX2(maxlen, (xof - tb->x / cu->fsize));
702                         linedata[lnr] = xof - tb->x / cu->fsize;
703                         linedata2[lnr] = cnr;
704                         linedata3[lnr] = tb->w / cu->fsize;
705                         linedata4[lnr] = wsnr;
706                         
707                         if ( (tb->h != 0.0f) &&
708                              ((-(yof - (tb->y / cu->fsize))) > ((tb->h / cu->fsize) - (linedist * cu->fsize)) - cu->yof) &&
709                              (cu->totbox > (curbox + 1)) ) {
710                                 maxlen = 0;
711                                 tb++;
712                                 curbox++;
713                                 yof = cu->yof + tb->y / cu->fsize;
714                         }
715
716                         /* XXX, has been unused for years, need to check if this is useful, r4613 r5282 - campbell */
717 #if 0
718                         if (ascii == '\n' || ascii == '\r')
719                                 xof = cu->xof;
720                         else
721                                 xof = cu->xof + (tb->x / cu->fsize);
722 #else
723                         xof = cu->xof + (tb->x / cu->fsize);
724 #endif
725                         lnr++;
726                         cnr = 0;
727                         wsnr = 0;
728                 }
729                 else if (ascii == 9) {    /* TAB */
730                         float tabfac;
731                         
732                         ct->xof = xof;
733                         ct->yof = yof;
734                         ct->linenr = lnr;
735                         ct->charnr = cnr++;
736
737                         tabfac = (xof - cu->xof + 0.01f);
738                         tabfac = 2.0f * ceilf(tabfac / 2.0f);
739                         xof = cu->xof + tabfac;
740                 }
741                 else {
742                         SelBox *sb = NULL;
743                         float wsfac;
744
745                         ct->xof = xof;
746                         ct->yof = yof;
747                         ct->linenr = lnr;
748                         ct->charnr = cnr++;
749
750                         if (cu->selboxes && (i >= selstart) && (i <= selend)) {
751                                 sb = &(cu->selboxes[i - selstart]);
752                                 sb->y = yof * cu->fsize - linedist * cu->fsize * 0.1f;
753                                 sb->h = linedist * cu->fsize;
754                                 sb->w = xof * cu->fsize;
755                         }
756         
757                         if (ascii == 32) {
758                                 wsfac = cu->wordspace; 
759                                 wsnr++;
760                         } 
761                         else wsfac = 1.0f;
762                         
763                         // Set the width of the character
764                         twidth = char_width(cu, che, info);
765
766                         xof += (twidth * wsfac * (1.0f + (info->kern / 40.0f)) ) + xtrax;
767                         
768                         if (sb) 
769                                 sb->w = (xof * cu->fsize) - sb->w;
770                 }
771                 ct++;
772         }
773         
774         cu->lines = 1;
775         ct = chartransdata;
776         tmp = mem;
777         for (i = 0; i <= slen; i++, tmp++, ct++) {
778                 ascii = *tmp;
779                 if (ascii == '\n' || ascii == '\r' || ct->dobreak) cu->lines++;
780         }       
781
782         // linedata is now: width of line
783         // linedata2 is now: number of characters
784         // linedata3 is now: maxlen of that line
785         // linedata4 is now: number of whitespaces of line
786
787         if (cu->spacemode != CU_LEFT) {
788                 ct = chartransdata;
789
790                 if (cu->spacemode == CU_RIGHT) {
791                         for (i = 0; i < lnr; i++) linedata[i] = linedata3[i] - linedata[i];
792                         for (i = 0; i <= slen; i++) {
793                                 ct->xof += linedata[ct->linenr];
794                                 ct++;
795                         }
796                 }
797                 else if (cu->spacemode == CU_MIDDLE) {
798                         for (i = 0; i < lnr; i++) linedata[i] = (linedata3[i] - linedata[i]) / 2;
799                         for (i = 0; i <= slen; i++) {
800                                 ct->xof += linedata[ct->linenr];
801                                 ct++;
802                         }
803                 }
804                 else if ((cu->spacemode == CU_FLUSH) &&
805                          (cu->tb[0].w != 0.0f)) {
806                         for (i = 0; i < lnr; i++)
807                                 if (linedata2[i] > 1)
808                                         linedata[i] = (linedata3[i] - linedata[i]) / (linedata2[i] - 1);
809                         for (i = 0; i <= slen; i++) {
810                                 for (j = i; (!ELEM3(mem[j], '\0', '\n', '\r')) && (chartransdata[j].dobreak == 0) && (j < slen); j++) {
811                                         /* do nothing */
812                                 }
813
814 //                              if ((mem[j]!='\r') && (mem[j]!='\n') && (mem[j])) {
815                                 ct->xof += ct->charnr * linedata[ct->linenr];
816 //                              }
817                                 ct++;
818                         }
819                 } 
820                 else if ((cu->spacemode == CU_JUSTIFY) && (cu->tb[0].w != 0.0f)) {
821                         float curofs = 0.0f;
822                         for (i = 0; i <= slen; i++) {
823                                 for (j = i; (mem[j]) && (mem[j] != '\n') &&
824                                      (mem[j] != '\r') && (chartransdata[j].dobreak == 0) && (j < slen);
825                                      j++)
826                                 {
827                                         /* pass */
828                                 }
829
830                                 if ((mem[j] != '\r') && (mem[j] != '\n') &&
831                                     ((chartransdata[j].dobreak != 0))) {
832                                         if (mem[i] == ' ') curofs += (linedata3[ct->linenr] - linedata[ct->linenr]) / linedata4[ct->linenr];
833                                         ct->xof += curofs;
834                                 }
835                                 if (mem[i] == '\n' || mem[i] == '\r' || chartransdata[i].dobreak) curofs = 0;
836                                 ct++;
837                         }
838                 }
839         }
840         
841         /* TEXT ON CURVE */
842         /* Note: Only OB_CURVE objects could have a path  */
843         if (cu->textoncurve && cu->textoncurve->type == OB_CURVE) {
844                 Curve *cucu = cu->textoncurve->data;
845                 int oldflag = cucu->flag;
846                 
847                 cucu->flag |= (CU_PATH + CU_FOLLOW);
848                 
849                 if (cucu->path == NULL) BKE_displist_make_curveTypes(scene, cu->textoncurve, 0);
850                 if (cucu->path) {
851                         float distfac, imat[4][4], imat3[3][3], cmat[3][3];
852                         float minx, maxx, miny, maxy;
853                         float timeofs, sizefac;
854                         
855                         invert_m4_m4(imat, ob->obmat);
856                         copy_m3_m4(imat3, imat);
857
858                         copy_m3_m4(cmat, cu->textoncurve->obmat);
859                         mul_m3_m3m3(cmat, cmat, imat3);
860                         sizefac = normalize_v3(cmat[0]) / cu->fsize;
861                         
862                         minx = miny = 1.0e20f;
863                         maxx = maxy = -1.0e20f;
864                         ct = chartransdata;
865                         for (i = 0; i <= slen; i++, ct++) {
866                                 if (minx > ct->xof) minx = ct->xof;
867                                 if (maxx < ct->xof) maxx = ct->xof;
868                                 if (miny > ct->yof) miny = ct->yof;
869                                 if (maxy < ct->yof) maxy = ct->yof;
870                         }
871                         
872                         /* we put the x-coordinaat exact at the curve, the y is rotated */
873                         
874                         /* length correction */
875                         distfac = sizefac * cucu->path->totdist / (maxx - minx);
876                         timeofs = 0.0f;
877                         
878                         if (distfac > 1.0f) {
879                                 /* path longer than text: spacemode involves */
880                                 distfac = 1.0f / distfac;
881                                 
882                                 if (cu->spacemode == CU_RIGHT) {
883                                         timeofs = 1.0f - distfac;
884                                 }
885                                 else if (cu->spacemode == CU_MIDDLE) {
886                                         timeofs = (1.0f - distfac) / 2.0f;
887                                 }
888                                 else if (cu->spacemode == CU_FLUSH) distfac = 1.0f;
889                                 
890                         }
891                         else distfac = 1.0;
892                         
893                         distfac /= (maxx - minx);
894                         
895                         timeofs += distfac * cu->xof;  /* not cyclic */
896                         
897                         ct = chartransdata;
898                         for (i = 0; i <= slen; i++, ct++) {
899                                 float ctime, dtime, vec[4], tvec[4], rotvec[3];
900                                 float si, co;
901                                 
902                                 /* rotate around center character */
903                                 ascii = mem[i];
904
905                                 che = find_vfont_char(vfd, ascii);
906         
907                                 twidth = char_width(cu, che, info);
908
909                                 dtime = distfac * 0.5f * twidth;
910
911                                 ctime = timeofs + distfac * (ct->xof - minx);
912                                 CLAMP(ctime, 0.0f, 1.0f);
913
914                                 /* calc the right loc AND the right rot separately */
915                                 /* vec, tvec need 4 items */
916                                 where_on_path(cu->textoncurve, ctime, vec, tvec, NULL, NULL, NULL);
917                                 where_on_path(cu->textoncurve, ctime + dtime, tvec, rotvec, NULL, NULL, NULL);
918                                 
919                                 mul_v3_fl(vec, sizefac);
920                                 
921                                 ct->rot = (float)(M_PI - atan2(rotvec[1], rotvec[0]));
922
923                                 si = (float)sin(ct->rot);
924                                 co = (float)cos(ct->rot);
925
926                                 yof = ct->yof;
927                                 
928                                 ct->xof = vec[0] + si * yof;
929                                 ct->yof = vec[1] + co * yof;
930                                 
931                         }
932                         cucu->flag = oldflag;
933                 }
934         }
935
936         if (cu->selboxes) {
937                 ct = chartransdata;
938                 for (i = 0; i <= selend; i++, ct++) {
939                         if (i >= selstart) {
940                                 cu->selboxes[i - selstart].x = ct->xof * cu->fsize;
941                                 cu->selboxes[i - selstart].y = ct->yof * cu->fsize;
942                         }
943                 }
944         }
945
946         if (mode == FO_CURSUP || mode == FO_CURSDOWN || mode == FO_PAGEUP || mode == FO_PAGEDOWN) {
947                 /* 2: curs up
948                  * 3: curs down */
949                 ct = chartransdata + cu->pos;
950                 
951                 if ((mode == FO_CURSUP || mode == FO_PAGEUP) && ct->linenr == 0) ;
952                 else if ((mode == FO_CURSDOWN || mode == FO_PAGEDOWN) && ct->linenr == lnr) ;
953                 else {
954                         switch (mode) {
955                                 case FO_CURSUP:     lnr = ct->linenr - 1; break;
956                                 case FO_CURSDOWN:   lnr = ct->linenr + 1; break;
957                                 case FO_PAGEUP:     lnr = ct->linenr - 10; break;
958                                 case FO_PAGEDOWN:   lnr = ct->linenr + 10; break;
959                         }
960                         cnr = ct->charnr;
961                         /* seek for char with lnr en cnr */
962                         cu->pos = 0;
963                         ct = chartransdata;
964                         for (i = 0; i < slen; i++) {
965                                 if (ct->linenr == lnr) {
966                                         if (ct->charnr == cnr) break;
967                                         if ( (ct + 1)->charnr == 0) break;
968                                 }
969                                 else if (ct->linenr > lnr) break;
970                                 cu->pos++;
971                                 ct++;
972                         }
973                 }
974         }
975         
976         /* cursor first */
977         if (cu->editfont) {
978                 float si, co;
979                 
980                 ct = chartransdata + cu->pos;
981                 si = (float)sin(ct->rot);
982                 co = (float)cos(ct->rot);
983                                 
984                 f = cu->editfont->textcurs[0];
985                 
986                 f[0] = cu->fsize * (-0.1f * co + ct->xof);
987                 f[1] = cu->fsize * (0.1f * si + ct->yof);
988                 
989                 f[2] = cu->fsize * (0.1f * co + ct->xof);
990                 f[3] = cu->fsize * (-0.1f * si + ct->yof);
991                 
992                 f[4] = cu->fsize * (0.1f * co + 0.8f * si + ct->xof);
993                 f[5] = cu->fsize * (-0.1f * si + 0.8f * co + ct->yof);
994                 
995                 f[6] = cu->fsize * (-0.1f * co + 0.8f * si + ct->xof);
996                 f[7] = cu->fsize * (0.1f * si + 0.8f * co + ct->yof);
997                 
998         }
999
1000         MEM_freeN(linedata);
1001         MEM_freeN(linedata2);           
1002         MEM_freeN(linedata3);
1003         MEM_freeN(linedata4);
1004
1005         if (mode == FO_SELCHANGE) {
1006                 MEM_freeN(chartransdata);
1007                 MEM_freeN(mem);
1008                 return NULL;
1009         }
1010
1011         if (mode == FO_EDIT) {
1012                 /* make nurbdata */
1013                 BKE_nurbList_free(&cu->nurb);
1014                 
1015                 ct = chartransdata;
1016                 if (cu->sepchar == 0) {
1017                         for (i = 0; i < slen; i++) {
1018                                 unsigned long cha = (uintptr_t) mem[i];
1019                                 info = &(custrinfo[i]);
1020                                 if (info->mat_nr > (ob->totcol)) {
1021                                         /* printf("Error: Illegal material index (%d) in text object, setting to 0\n", info->mat_nr); */
1022                                         info->mat_nr = 0;
1023                                 }
1024                                 // We do not want to see any character for \n or \r
1025                                 if (cha != '\n' && cha != '\r')
1026                                         buildchar(bmain, cu, cha, info, ct->xof, ct->yof, ct->rot, i);
1027                                 
1028                                 if ((info->flag & CU_CHINFO_UNDERLINE) && (cu->textoncurve == NULL) && (cha != '\n') && (cha != '\r')) {
1029                                         float ulwidth, uloverlap = 0.0f;
1030                                         
1031                                         if ( (i < (slen - 1)) && (mem[i + 1] != '\n') && (mem[i + 1] != '\r') &&
1032                                              ((mem[i + 1] != ' ') || (custrinfo[i + 1].flag & CU_CHINFO_UNDERLINE)) && ((custrinfo[i + 1].flag & CU_CHINFO_WRAP) == 0)
1033                                              ) {
1034                                                 uloverlap = xtrax + 0.1f;
1035                                         }
1036                                         // Find the character, the characters has to be in the memory already 
1037                                         // since character checking has been done earlier already.
1038                                         che = find_vfont_char(vfd, cha);
1039
1040                                         twidth = char_width(cu, che, info);
1041                                         ulwidth = cu->fsize * ((twidth * (1.0f + (info->kern / 40.0f))) + uloverlap);
1042                                         build_underline(cu, ct->xof * cu->fsize, ct->yof * cu->fsize + (cu->ulpos - 0.05f) * cu->fsize,
1043                                                         ct->xof * cu->fsize + ulwidth,
1044                                                         ct->yof * cu->fsize + (cu->ulpos - 0.05f) * cu->fsize - cu->ulheight * cu->fsize,
1045                                                         i, info->mat_nr);
1046                                 }
1047                                 ct++;
1048                         }
1049                 }
1050                 else {
1051                         int outta = 0;
1052                         for (i = 0; (i < slen) && (outta == 0); i++) {
1053                                 ascii = mem[i];
1054                                 info = &(custrinfo[i]);
1055                                 if (cu->sepchar == (i + 1)) {
1056                                         float vecyo[3];
1057
1058                                         vecyo[0] = ct->xof;
1059                                         vecyo[1] = ct->yof;
1060                                         vecyo[2] = 0.0f;
1061
1062                                         mem[0] = ascii;
1063                                         mem[1] = 0;
1064                                         custrinfo[0] = *info;
1065                                         cu->pos = 1;
1066                                         cu->len = 1;
1067                                         mul_v3_m4v3(ob->loc, ob->obmat, vecyo);
1068                                         outta = 1;
1069                                         cu->sepchar = 0;
1070                                 }
1071                                 ct++;
1072                         }
1073                 }
1074         }
1075
1076         if (mode == FO_DUPLI) {
1077                 MEM_freeN(mem);
1078                 return chartransdata;
1079         }
1080
1081         if (mem)
1082                 MEM_freeN(mem);
1083
1084         MEM_freeN(chartransdata);
1085         return NULL;
1086 }