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