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