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