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