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