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