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