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