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