Merge branch 'master' into blender2.8
[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_object_types.h"
56
57 #include "BKE_packedFile.h"
58 #include "BKE_library.h"
59 #include "BKE_font.h"
60 #include "BKE_global.h"
61 #include "BKE_main.h"
62 #include "BKE_anim.h"
63 #include "BKE_curve.h"
64
65 static ThreadRWMutex vfont_rwlock = BLI_RWLOCK_INITIALIZER;
66
67 /* The vfont code */
68 void BKE_vfont_free_data(struct VFont *vfont)
69 {
70         if (vfont->data) {
71                 if (vfont->data->characters) {
72                         GHashIterator gh_iter;
73                         GHASH_ITER (gh_iter, vfont->data->characters) {
74                                 VChar *che = BLI_ghashIterator_getValue(&gh_iter);
75
76                                 while (che->nurbsbase.first) {
77                                         Nurb *nu = che->nurbsbase.first;
78                                         if (nu->bezt) MEM_freeN(nu->bezt);
79                                         BLI_freelinkN(&che->nurbsbase, nu);
80                                 }
81
82                                 MEM_freeN(che);
83                         }
84
85                         BLI_ghash_free(vfont->data->characters, NULL, NULL);
86                 }
87
88                 MEM_freeN(vfont->data);
89                 vfont->data = NULL;
90         }
91
92         if (vfont->temp_pf) {
93                 freePackedFile(vfont->temp_pf);  /* NULL when the font file can't be found on disk */
94                 vfont->temp_pf = NULL;
95         }
96 }
97
98 /** Free (or release) any data used by this font (does not free the font itself). */
99 void BKE_vfont_free(struct VFont *vf)
100 {
101         BKE_vfont_free_data(vf);
102
103         if (vf->packedfile) {
104                 freePackedFile(vf->packedfile);
105                 vf->packedfile = NULL;
106         }
107 }
108
109 void BKE_vfont_copy_data(Main *UNUSED(bmain), VFont *vfont_dst, const VFont *UNUSED(vfont_src), const int flag)
110 {
111         /* We never handle usercount here for own data. */
112         const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT;
113
114         /* Just to be sure, should not have any value actually after reading time. */
115         vfont_dst->temp_pf = NULL;
116
117         if (vfont_dst->packedfile) {
118                 vfont_dst->packedfile = dupPackedFile(vfont_dst->packedfile);
119         }
120
121         if (vfont_dst->data) {
122                 vfont_dst->data = BLI_vfontdata_copy(vfont_dst->data, flag_subdata);
123         }
124 }
125
126 static void *builtin_font_data = NULL;
127 static int builtin_font_size = 0;
128
129 bool BKE_vfont_is_builtin(struct VFont *vfont)
130 {
131         return STREQ(vfont->name, FO_BUILTIN_NAME);
132 }
133
134 void BKE_vfont_builtin_register(void *mem, int size)
135 {
136         builtin_font_data = mem;
137         builtin_font_size = size;
138 }
139
140 static PackedFile *get_builtin_packedfile(void)
141 {
142         if (!builtin_font_data) {
143                 printf("Internal error, builtin font not loaded\n");
144
145                 return NULL;
146         }
147         else {
148                 void *mem = MEM_mallocN(builtin_font_size, "vfd_builtin");
149
150                 memcpy(mem, builtin_font_data, builtin_font_size);
151         
152                 return newPackedFileMemory(mem, builtin_font_size);
153         }
154 }
155
156 static VFontData *vfont_get_data(VFont *vfont)
157 {
158         if (vfont == NULL) {
159                 return NULL;
160         }
161
162         /* And then set the data */
163         if (!vfont->data) {
164                 PackedFile *pf;
165
166                 BLI_rw_mutex_lock(&vfont_rwlock, THREAD_LOCK_WRITE);
167
168                 if (vfont->data) {
169                         /* Check data again, since it might have been already
170                          * initialized from other thread (previous check is
171                          * not accurate or threading, just prevents unneeded
172                          * lock if all the data is here for sure).
173                          */
174                         BLI_rw_mutex_unlock(&vfont_rwlock);
175                         return vfont->data;
176                 }
177
178                 if (BKE_vfont_is_builtin(vfont)) {
179                         pf = get_builtin_packedfile();
180                 }
181                 else {
182                         if (vfont->packedfile) {
183                                 pf = vfont->packedfile;
184
185                                 /* We need to copy a tmp font to memory unless it is already there */
186                                 if (vfont->temp_pf == NULL) {
187                                         vfont->temp_pf = dupPackedFile(pf);
188                                 }
189                         }
190                         else {
191                                 pf = newPackedFile(NULL, vfont->name, ID_BLEND_PATH_FROM_GLOBAL(&vfont->id));
192
193                                 if (vfont->temp_pf == NULL) {
194                                         vfont->temp_pf = newPackedFile(NULL, vfont->name, ID_BLEND_PATH_FROM_GLOBAL(&vfont->id));
195                                 }
196                         }
197                         if (!pf) {
198                                 printf("Font file doesn't exist: %s\n", vfont->name);
199
200                                 /* DON'T DO THIS
201                                  * missing file shouldn't modify path! - campbell */
202 #if 0
203                                 strcpy(vfont->name, FO_BUILTIN_NAME);
204 #endif
205                                 pf = get_builtin_packedfile();
206                         }
207                 }
208                 
209                 if (pf) {
210                         vfont->data = BLI_vfontdata_from_freetypefont(pf);
211                         if (pf != vfont->packedfile) {
212                                 freePackedFile(pf);
213                         }
214                 }
215
216                 BLI_rw_mutex_unlock(&vfont_rwlock);
217         }
218
219         return vfont->data;
220 }
221
222 /* Bad naming actually in this case... */
223 void BKE_vfont_init(VFont *vfont)
224 {
225         PackedFile *pf = get_builtin_packedfile();
226
227         if (pf) {
228                 VFontData *vfd;
229
230                 vfd = BLI_vfontdata_from_freetypefont(pf);
231                 if (vfd) {
232                         vfont->data = vfd;
233
234                         BLI_strncpy(vfont->name, FO_BUILTIN_NAME, sizeof(vfont->name));
235                 }
236
237                 /* Free the packed file */
238                 freePackedFile(pf);
239         }
240 }
241
242 VFont *BKE_vfont_load(Main *bmain, const char *filepath)
243 {
244         char filename[FILE_MAXFILE];
245         VFont *vfont = NULL;
246         PackedFile *pf;
247         PackedFile *temp_pf = NULL;
248         bool is_builtin;
249         
250         if (STREQ(filepath, FO_BUILTIN_NAME)) {
251                 BLI_strncpy(filename, filepath, sizeof(filename));
252                 
253                 pf = get_builtin_packedfile();
254                 is_builtin = true;
255         }
256         else {
257                 BLI_split_file_part(filepath, filename, sizeof(filename));
258                 pf = newPackedFile(NULL, filepath, BKE_main_blendfile_path(bmain));
259                 temp_pf = newPackedFile(NULL, filepath, BKE_main_blendfile_path(bmain));
260                 
261                 is_builtin = false;
262         }
263
264         if (pf) {
265                 VFontData *vfd;
266
267                 vfd = BLI_vfontdata_from_freetypefont(pf);
268                 if (vfd) {
269                         vfont = BKE_libblock_alloc(bmain, ID_VF, filename, 0);
270                         vfont->data = vfd;
271
272                         /* if there's a font name, use it for the ID name */
273                         if (vfd->name[0] != '\0') {
274                                 BLI_strncpy(vfont->id.name + 2, vfd->name, sizeof(vfont->id.name) - 2);
275                         }
276                         BLI_strncpy(vfont->name, filepath, sizeof(vfont->name));
277
278                         /* if autopack is on store the packedfile in de font structure */
279                         if (!is_builtin && (G.fileflags & G_AUTOPACK)) {
280                                 vfont->packedfile = pf;
281                         }
282
283                         /* Do not add FO_BUILTIN_NAME to temporary listbase */
284                         if (!STREQ(filename, FO_BUILTIN_NAME)) {
285                                 vfont->temp_pf = temp_pf;
286                         }
287                 }
288
289                 /* Free the packed file */
290                 if (!vfont || vfont->packedfile != pf) {
291                         freePackedFile(pf);
292                 }
293         }
294         
295         return vfont;
296 }
297
298 VFont *BKE_vfont_load_exists_ex(struct Main *bmain, const char *filepath, bool *r_exists)
299 {
300         VFont *vfont;
301         char str[FILE_MAX], strtest[FILE_MAX];
302
303         BLI_strncpy(str, filepath, sizeof(str));
304         BLI_path_abs(str, BKE_main_blendfile_path(bmain));
305
306         /* first search an identical filepath */
307         for (vfont = bmain->vfont.first; vfont; vfont = vfont->id.next) {
308                 BLI_strncpy(strtest, vfont->name, sizeof(vfont->name));
309                 BLI_path_abs(strtest, ID_BLEND_PATH(bmain, &vfont->id));
310
311                 if (BLI_path_cmp(strtest, str) == 0) {
312                         id_us_plus(&vfont->id);  /* officially should not, it doesn't link here! */
313                         if (r_exists)
314                                 *r_exists = true;
315                         return vfont;
316                 }
317         }
318
319         if (r_exists)
320                 *r_exists = false;
321         return BKE_vfont_load(bmain, filepath);
322 }
323
324 VFont *BKE_vfont_load_exists(struct Main *bmain, const char *filepath)
325 {
326         return BKE_vfont_load_exists_ex(bmain, filepath, NULL);
327 }
328
329 void BKE_vfont_make_local(Main *bmain, VFont *vfont, const bool lib_local)
330 {
331         BKE_id_make_local_generic(bmain, &vfont->id, true, lib_local);
332 }
333
334 static VFont *which_vfont(Curve *cu, CharInfo *info)
335 {
336         switch (info->flag & (CU_CHINFO_BOLD | CU_CHINFO_ITALIC)) {
337                 case CU_CHINFO_BOLD:
338                         return cu->vfontb ? cu->vfontb : cu->vfont;
339                 case CU_CHINFO_ITALIC:
340                         return cu->vfonti ? cu->vfonti : cu->vfont;
341                 case (CU_CHINFO_BOLD | CU_CHINFO_ITALIC):
342                         return cu->vfontbi ? cu->vfontbi : cu->vfont;
343                 default:
344                         return cu->vfont;
345         }
346 }
347
348 VFont *BKE_vfont_builtin_get(void)
349 {
350         VFont *vfont;
351         
352         for (vfont = G_MAIN->vfont.first; vfont; vfont = vfont->id.next) {
353                 if (BKE_vfont_is_builtin(vfont)) {
354                         return vfont;
355                 }
356         }
357         
358         return BKE_vfont_load(G_MAIN, FO_BUILTIN_NAME);
359 }
360
361 static VChar *find_vfont_char(VFontData *vfd, unsigned int character)
362 {
363         return BLI_ghash_lookup(vfd->characters, SET_UINT_IN_POINTER(character));
364 }
365
366 static void build_underline(Curve *cu, ListBase *nubase, const rctf *rect,
367                             float yofs, float rot, int charidx, short mat_nr)
368 {
369         Nurb *nu2;
370         BPoint *bp;
371         
372         nu2 = (Nurb *) MEM_callocN(sizeof(Nurb), "underline_nurb");
373         nu2->resolu = cu->resolu;
374         nu2->bezt = NULL;
375         nu2->knotsu = nu2->knotsv = NULL;
376         nu2->flag = CU_2D;
377         nu2->charidx = charidx + 1000;
378         if (mat_nr > 0) nu2->mat_nr = mat_nr - 1;
379         nu2->pntsu = 4;
380         nu2->pntsv = 1;
381         nu2->orderu = 4;
382         nu2->orderv = 1;
383         nu2->flagu = CU_NURB_CYCLIC;
384
385         bp = (BPoint *)MEM_calloc_arrayN(4, sizeof(BPoint), "underline_bp");
386
387         copy_v4_fl4(bp[0].vec, rect->xmin, (rect->ymax + yofs), 0.0f, 1.0f);
388         copy_v4_fl4(bp[1].vec, rect->xmax, (rect->ymax + yofs), 0.0f, 1.0f);
389         copy_v4_fl4(bp[2].vec, rect->xmax, (rect->ymin + yofs), 0.0f, 1.0f);
390         copy_v4_fl4(bp[3].vec, rect->xmin, (rect->ymin + yofs), 0.0f, 1.0f);
391
392         nu2->bp = bp;
393         BLI_addtail(nubase, nu2);
394
395         if (rot != 0.0f) {
396                 float si, co;
397                 int i;
398
399                 si = sinf(rot);
400                 co = cosf(rot);
401
402                 for (i = nu2->pntsu; i > 0; i--) {
403                         float *fp;
404                         float x, y;
405
406                         fp = bp->vec;
407
408                         x = fp[0] - rect->xmin;
409                         y = fp[1] - rect->ymin;
410
411                         fp[0] = (+co * x + si * y) + rect->xmin;
412                         fp[1] = (-si * x + co * y) + rect->ymin;
413
414                         bp++;
415                 }
416
417                 bp = nu2->bp;
418         }
419
420         mul_v2_fl(bp[0].vec, cu->fsize);
421         mul_v2_fl(bp[1].vec, cu->fsize);
422         mul_v2_fl(bp[2].vec, cu->fsize);
423         mul_v2_fl(bp[3].vec, cu->fsize);
424 }
425
426 static void buildchar(Curve *cu, ListBase *nubase, unsigned int character, CharInfo *info,
427                       float ofsx, float ofsy, float rot, int charidx)
428 {
429         BezTriple *bezt1, *bezt2;
430         Nurb *nu1 = NULL, *nu2 = NULL;
431         float *fp, fsize, shear, x, si, co;
432         VFontData *vfd = NULL;
433         VChar *che = NULL;
434         int i;
435
436         vfd = vfont_get_data(which_vfont(cu, info));
437         if (!vfd) return;
438
439 #if 0
440         if (cu->selend < cu->selstart) {
441                 if ((charidx >= (cu->selend)) && (charidx <= (cu->selstart - 2)))
442                         sel = 1;
443         }
444         else {
445                 if ((charidx >= (cu->selstart - 1)) && (charidx <= (cu->selend - 1)))
446                         sel = 1;
447         }
448 #endif
449
450         /* make a copy at distance ofsx, ofsy with shear */
451         fsize = cu->fsize;
452         shear = cu->shear;
453         si = sinf(rot);
454         co = cosf(rot);
455
456         che = find_vfont_char(vfd, character);
457         
458         /* Select the glyph data */
459         if (che)
460                 nu1 = che->nurbsbase.first;
461
462         /* Create the character */
463         while (nu1) {
464                 bezt1 = nu1->bezt;
465                 if (bezt1) {
466                         nu2 = (Nurb *) MEM_mallocN(sizeof(Nurb), "duplichar_nurb");
467                         if (nu2 == NULL) break;
468                         memcpy(nu2, nu1, sizeof(struct Nurb));
469                         nu2->resolu = cu->resolu;
470                         nu2->bp = NULL;
471                         nu2->knotsu = nu2->knotsv = NULL;
472                         nu2->flag = CU_SMOOTH;
473                         nu2->charidx = charidx;
474                         if (info->mat_nr > 0) {
475                                 nu2->mat_nr = info->mat_nr - 1;
476                         }
477                         else {
478                                 nu2->mat_nr = 0;
479                         }
480                         /* nu2->trim.first = 0; */
481                         /* nu2->trim.last = 0; */
482                         i = nu2->pntsu;
483
484                         bezt2 = (BezTriple *)MEM_malloc_arrayN(i, sizeof(BezTriple), "duplichar_bezt2");
485                         if (bezt2 == NULL) {
486                                 MEM_freeN(nu2);
487                                 break;
488                         }
489                         memcpy(bezt2, bezt1, i * sizeof(struct BezTriple));
490                         nu2->bezt = bezt2;
491                         
492                         if (shear != 0.0f) {
493                                 bezt2 = nu2->bezt;
494                                 
495                                 for (i = nu2->pntsu; i > 0; i--) {
496                                         bezt2->vec[0][0] += shear * bezt2->vec[0][1];
497                                         bezt2->vec[1][0] += shear * bezt2->vec[1][1];
498                                         bezt2->vec[2][0] += shear * bezt2->vec[2][1];
499                                         bezt2++;
500                                 }
501                         }
502                         if (rot != 0.0f) {
503                                 bezt2 = nu2->bezt;
504                                 for (i = nu2->pntsu; i > 0; i--) {
505                                         fp = bezt2->vec[0];
506
507                                         x = fp[0];
508                                         fp[0] = co * x + si * fp[1];
509                                         fp[1] = -si * x + co * fp[1];
510                                         x = fp[3];
511                                         fp[3] = co * x + si * fp[4];
512                                         fp[4] = -si * x + co * fp[4];
513                                         x = fp[6];
514                                         fp[6] = co * x + si * fp[7];
515                                         fp[7] = -si * x + co * fp[7];
516
517                                         bezt2++;
518                                 }
519                         }
520                         bezt2 = nu2->bezt;
521
522                         if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
523                                 const float sca = cu->smallcaps_scale;
524                                 for (i = nu2->pntsu; i > 0; i--) {
525                                         fp = bezt2->vec[0];
526                                         fp[0] *= sca;
527                                         fp[1] *= sca;
528                                         fp[3] *= sca;
529                                         fp[4] *= sca;
530                                         fp[6] *= sca;
531                                         fp[7] *= sca;
532                                         bezt2++;
533                                 }
534                         }
535                         bezt2 = nu2->bezt;
536
537                         for (i = nu2->pntsu; i > 0; i--) {
538                                 fp = bezt2->vec[0];
539                                 fp[0] = (fp[0] + ofsx) * fsize;
540                                 fp[1] = (fp[1] + ofsy) * fsize;
541                                 fp[3] = (fp[3] + ofsx) * fsize;
542                                 fp[4] = (fp[4] + ofsy) * fsize;
543                                 fp[6] = (fp[6] + ofsx) * fsize;
544                                 fp[7] = (fp[7] + ofsy) * fsize;
545                                 bezt2++;
546                         }
547                         
548                         BLI_addtail(nubase, nu2);
549                 }
550                 
551                 nu1 = nu1->next;
552         }
553 }
554
555 int BKE_vfont_select_get(Object *ob, int *r_start, int *r_end)
556 {
557         Curve *cu = ob->data;
558         EditFont *ef = cu->editfont;
559         int start, end, direction;
560         
561         if ((ob->type != OB_FONT) || (ef == NULL)) return 0;
562
563         BLI_assert(ef->len >= 0);
564         BLI_assert(ef->selstart >= 0 && ef->selstart <= ef->len + 1);
565         BLI_assert(ef->selend   >= 0 && ef->selend   <= ef->len + 1);
566         BLI_assert(ef->pos      >= 0 && ef->pos      <= ef->len);
567
568         if (ef->selstart == 0) {
569                 return 0;
570         }
571
572         if (ef->selstart <= ef->selend) {
573                 start = ef->selstart - 1;
574                 end = ef->selend - 1;
575                 direction = 1;
576         }
577         else {
578                 start = ef->selend;
579                 end = ef->selstart - 2;
580                 direction = -1;
581         }
582
583         if (start == end + 1) {
584                 return 0;
585         }
586         else {
587                 BLI_assert(start < end + 1);
588                 *r_start = start;
589                 *r_end = end;
590                 return direction;
591         }
592 }
593
594 void BKE_vfont_select_clamp(Object *ob)
595 {
596         Curve *cu = ob->data;
597         EditFont *ef = cu->editfont;
598
599         BLI_assert((ob->type == OB_FONT) && ef);
600
601         CLAMP_MAX(ef->pos,      ef->len);
602         CLAMP_MAX(ef->selstart, ef->len + 1);
603         CLAMP_MAX(ef->selend,   ef->len);
604 }
605
606 static float char_width(Curve *cu, VChar *che, CharInfo *info)
607 {
608         /* The character wasn't found, propably ascii = 0, then the width shall be 0 as well */
609         if (che == NULL) {
610                 return 0.0f;
611         }
612         else if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
613                 return che->width * cu->smallcaps_scale;
614         }
615         else {
616                 return che->width;
617         }
618 }
619
620 static void textbox_scale(TextBox *tb_dst, const TextBox *tb_src, float scale)
621 {
622         tb_dst->x = tb_src->x * scale;
623         tb_dst->y = tb_src->y * scale;
624         tb_dst->w = tb_src->w * scale;
625         tb_dst->h = tb_src->h * scale;
626 }
627
628 /**
629  * Used for storing per-line data for alignment & wrapping.
630  */
631 struct TempLineInfo {
632         float x_min;      /* left margin */
633         float x_max;      /* right margin */
634         int   char_nr;    /* number of characters */
635         int   wspace_nr;  /* number of whitespaces of line */
636 };
637
638 bool BKE_vfont_to_curve_ex(Object *ob, Curve *cu, int mode, ListBase *r_nubase,
639                            const wchar_t **r_text, int *r_text_len, bool *r_text_free,
640                            struct CharTrans **r_chartransdata)
641 {
642         EditFont *ef = cu->editfont;
643         EditFontSelBox *selboxes = NULL;
644         VFont *vfont, *oldvfont;
645         VFontData *vfd = NULL;
646         CharInfo *info = NULL, *custrinfo;
647         TextBox tb_scale;
648         bool use_textbox;
649         VChar *che;
650         struct CharTrans *chartransdata = NULL, *ct;
651         /* Text at the beginning of the last used text-box (use for y-axis alignment). */
652         int i_textbox = 0;
653         struct TempLineInfo *lineinfo;
654         float *f, xof, yof, xtrax, linedist;
655         float twidth, maxlen = 0;
656         int i, slen, j;
657         int curbox;
658         int selstart, selend;
659         int cnr = 0, lnr = 0, wsnr = 0;
660         const wchar_t *mem;
661         wchar_t ascii;
662         bool ok = false;
663         const float xof_scale = cu->xof / cu->fsize;
664         const float yof_scale = cu->yof / cu->fsize;
665
666 #define MARGIN_X_MIN (xof_scale + tb_scale.x)
667 #define MARGIN_Y_MIN (yof_scale + tb_scale.y)
668
669         /* remark: do calculations including the trailing '\0' of a string
670          * because the cursor can be at that location */
671
672         BLI_assert(ob == NULL || ob->type == OB_FONT);
673
674         /* Set font data */
675         vfont = cu->vfont;
676
677         if (cu->str == NULL) return ok;
678         if (vfont == NULL) return ok;
679
680         vfd = vfont_get_data(vfont);
681
682         /* The VFont Data can not be found */
683         if (!vfd) return ok;
684         
685         if (ef) {
686                 slen = ef->len;
687                 mem = ef->textbuf;
688                 custrinfo = ef->textbufinfo;
689         }
690         else {
691                 wchar_t *mem_tmp;
692                 slen = cu->len_wchar;
693
694                 /* Create unicode string */
695                 mem_tmp = MEM_malloc_arrayN((slen + 1), sizeof(wchar_t), "convertedmem");
696                 if (!mem_tmp) {
697                         return ok;
698                 }
699
700                 BLI_strncpy_wchar_from_utf8(mem_tmp, cu->str, slen + 1);
701
702                 if (cu->strinfo == NULL) {  /* old file */
703                         cu->strinfo = MEM_calloc_arrayN((slen + 4), sizeof(CharInfo), "strinfo compat");
704                 }
705                 custrinfo = cu->strinfo;
706                 if (!custrinfo) {
707                         return ok;
708                 }
709
710                 mem = mem_tmp;
711         }
712
713         if (cu->tb == NULL)
714                 cu->tb = MEM_calloc_arrayN(MAXTEXTBOX, sizeof(TextBox), "TextBox compat");
715
716         if (ef != NULL && ob != NULL) {
717                 if (ef->selboxes)
718                         MEM_freeN(ef->selboxes);
719
720                 if (BKE_vfont_select_get(ob, &selstart, &selend)) {
721                         ef->selboxes_len = (selend - selstart) + 1;
722                         ef->selboxes = MEM_calloc_arrayN(ef->selboxes_len, sizeof(EditFontSelBox), "font selboxes");
723                 }
724                 else {
725                         ef->selboxes_len = 0;
726                         ef->selboxes = NULL;
727                 }
728
729                 selboxes = ef->selboxes;
730         }
731
732         /* calc offset and rotation of each char */
733         ct = chartransdata = MEM_calloc_arrayN((slen + 1), sizeof(struct CharTrans), "buildtext");
734
735         /* We assume the worst case: 1 character per line (is freed at end anyway) */
736         lineinfo = MEM_malloc_arrayN((slen * 2 + 1), sizeof(*lineinfo), "lineinfo");
737         
738         linedist = cu->linedist;
739         
740         curbox = 0;
741         textbox_scale(&tb_scale, &cu->tb[curbox], 1.0f / cu->fsize);
742         use_textbox = (tb_scale.w != 0.0f);
743
744
745         xof = MARGIN_X_MIN;
746         yof = MARGIN_Y_MIN;
747
748         xtrax = 0.5f * cu->spacing - 0.5f;
749
750         oldvfont = NULL;
751
752         for (i = 0; i < slen; i++) {
753                 custrinfo[i].flag &= ~(CU_CHINFO_WRAP | CU_CHINFO_SMALLCAPS_CHECK);
754         }
755
756         for (i = 0; i <= slen; i++) {
757 makebreak:
758                 /* Characters in the list */
759                 info = &custrinfo[i];
760                 ascii = mem[i];
761                 if (info->flag & CU_CHINFO_SMALLCAPS) {
762                         ascii = towupper(ascii);
763                         if (mem[i] != ascii) {
764                                 info->flag |= CU_CHINFO_SMALLCAPS_CHECK;
765                         }
766                 }
767
768                 vfont = which_vfont(cu, info);
769
770                 if (vfont == NULL) break;
771
772                 if (vfont != oldvfont) {
773                         vfd = vfont_get_data(vfont);
774                         oldvfont = vfont;
775                 }
776
777                 /* VFont Data for VFont couldn't be found */
778                 if (!vfd) {
779                         MEM_freeN(chartransdata);
780                         chartransdata = NULL;
781                         MEM_freeN(lineinfo);
782                         goto finally;
783                 }
784
785                 if (!ELEM(ascii, '\n', '\0')) {
786                         BLI_rw_mutex_lock(&vfont_rwlock, THREAD_LOCK_READ);
787                         che = find_vfont_char(vfd, ascii);
788                         BLI_rw_mutex_unlock(&vfont_rwlock);
789
790                         /*
791                          * The character wasn't in the current curve base so load it
792                          * But if the font is built-in then do not try loading since
793                          * whole font is in the memory already
794                          */
795                         if (che == NULL && BKE_vfont_is_builtin(vfont) == false) {
796                                 BLI_rw_mutex_lock(&vfont_rwlock, THREAD_LOCK_WRITE);
797                                 /* Check it once again, char might have been already load
798                                  * between previous BLI_rw_mutex_unlock() and this BLI_rw_mutex_lock().
799                                  *
800                                  * Such a check should not be a bottleneck since it wouldn't
801                                  * happen often once all the chars are load.
802                                  */
803                                 if ((che = find_vfont_char(vfd, ascii)) == NULL) {
804                                         che = BLI_vfontchar_from_freetypefont(vfont, ascii);
805                                 }
806                                 BLI_rw_mutex_unlock(&vfont_rwlock);
807                         }
808                 }
809                 else {
810                         che = NULL;
811                 }
812
813                 twidth = char_width(cu, che, info);
814
815                 /* Calculate positions */
816                 if ((tb_scale.w != 0.0f) &&
817                     (ct->dobreak == 0) &&
818                     (((xof - tb_scale.x) + twidth) > xof_scale + tb_scale.w))
819                 {
820                         //              fprintf(stderr, "linewidth exceeded: %c%c%c...\n", mem[i], mem[i+1], mem[i+2]);
821                         for (j = i; j && (mem[j] != '\n') && (chartransdata[j].dobreak == 0); j--) {
822                                 if (mem[j] == ' ' || mem[j] == '-') {
823                                         ct -= (i - (j - 1));
824                                         cnr -= (i - (j - 1));
825                                         if (mem[j] == ' ') wsnr--;
826                                         if (mem[j] == '-') wsnr++;
827                                         i = j - 1;
828                                         xof = ct->xof;
829                                         ct[1].dobreak = 1;
830                                         custrinfo[i + 1].flag |= CU_CHINFO_WRAP;
831                                         goto makebreak;
832                                 }
833                                 if (chartransdata[j].dobreak) {
834                                         //                              fprintf(stderr, "word too long: %c%c%c...\n", mem[j], mem[j+1], mem[j+2]);
835                                         ct->dobreak = 1;
836                                         custrinfo[i + 1].flag |= CU_CHINFO_WRAP;
837                                         ct -= 1;
838                                         cnr -= 1;
839                                         i--;
840                                         xof = ct->xof;
841                                         goto makebreak;
842                                 }
843                         }
844                 }
845
846                 if (ascii == '\n' || ascii == 0 || ct->dobreak) {
847                         ct->xof = xof;
848                         ct->yof = yof;
849                         ct->linenr = lnr;
850                         ct->charnr = cnr;
851
852                         yof -= linedist;
853
854                         lineinfo[lnr].x_min     = (xof - xtrax) - tb_scale.x;
855                         lineinfo[lnr].x_max     = tb_scale.w;
856                         lineinfo[lnr].char_nr   = cnr;
857                         lineinfo[lnr].wspace_nr = wsnr;
858
859                         CLAMP_MIN(maxlen, lineinfo[lnr].x_min);
860
861                         if ((tb_scale.h != 0.0f) &&
862                             (cu->totbox > (curbox + 1)) &&
863                             ((-(yof - tb_scale.y)) > (tb_scale.h - linedist) - yof_scale))
864                         {
865                                 i_textbox = i + 1;
866                                 maxlen = 0;
867                                 curbox++;
868
869                                 textbox_scale(&tb_scale, &cu->tb[curbox], 1.0f / cu->fsize);
870
871                                 yof = MARGIN_Y_MIN;
872                         }
873
874                         /* XXX, has been unused for years, need to check if this is useful, r4613 r5282 - campbell */
875 #if 0
876                         if (ascii == '\n')
877                                 xof = xof_scale;
878                         else
879                                 xof = MARGIN_X_MIN;
880 #else
881                         xof = MARGIN_X_MIN;
882 #endif
883                         lnr++;
884                         cnr = 0;
885                         wsnr = 0;
886                 }
887                 else if (ascii == 9) {    /* TAB */
888                         float tabfac;
889                         
890                         ct->xof = xof;
891                         ct->yof = yof;
892                         ct->linenr = lnr;
893                         ct->charnr = cnr++;
894
895                         tabfac = (xof - MARGIN_X_MIN + 0.01f);
896                         tabfac = 2.0f * ceilf(tabfac / 2.0f);
897                         xof = MARGIN_X_MIN + tabfac;
898                 }
899                 else {
900                         EditFontSelBox *sb = NULL;
901                         float wsfac;
902
903                         ct->xof = xof;
904                         ct->yof = yof;
905                         ct->linenr = lnr;
906                         ct->charnr = cnr++;
907
908                         if (selboxes && (i >= selstart) && (i <= selend)) {
909                                 sb = &selboxes[i - selstart];
910                                 sb->y = yof * cu->fsize - linedist * cu->fsize * 0.1f;
911                                 sb->h = linedist * cu->fsize;
912                                 sb->w = xof * cu->fsize;
913                         }
914         
915                         if (ascii == 32) {
916                                 wsfac = cu->wordspace; 
917                                 wsnr++;
918                         }
919                         else {
920                                 wsfac = 1.0f;
921                         }
922                         
923                         /* Set the width of the character */
924                         twidth = char_width(cu, che, info);
925
926                         xof += (twidth * wsfac * (1.0f + (info->kern / 40.0f)) ) + xtrax;
927                         
928                         if (sb) {
929                                 sb->w = (xof * cu->fsize) - sb->w;
930                         }
931                 }
932                 ct++;
933         }
934         
935         cu->lines = 1;
936         for (i = 0; i <= slen; i++) {
937                 ascii = mem[i];
938                 ct = &chartransdata[i];
939                 if (ascii == '\n' || ct->dobreak) cu->lines++;
940         }
941
942         /* linedata is now: width of line */
943
944         if (cu->spacemode != CU_ALIGN_X_LEFT) {
945                 ct = chartransdata;
946
947                 if (cu->spacemode == CU_ALIGN_X_RIGHT) {
948                         struct TempLineInfo *li;
949
950                         for (i = 0, li = lineinfo; i < lnr; i++, li++) {
951                                 li->x_min = (li->x_max - li->x_min) + xof_scale;
952                         }
953
954                         for (i = 0; i <= slen; i++) {
955                                 ct->xof += lineinfo[ct->linenr].x_min;
956                                 ct++;
957                         }
958                 }
959                 else if (cu->spacemode == CU_ALIGN_X_MIDDLE) {
960                         struct TempLineInfo *li;
961
962                         for (i = 0, li = lineinfo; i < lnr; i++, li++) {
963                                 li->x_min = ((li->x_max - li->x_min) + xof_scale) / 2.0f;
964                         }
965
966                         for (i = 0; i <= slen; i++) {
967                                 ct->xof += lineinfo[ct->linenr].x_min;
968                                 ct++;
969                         }
970                 }
971                 else if ((cu->spacemode == CU_ALIGN_X_FLUSH) && use_textbox) {
972                         struct TempLineInfo *li;
973
974                         for (i = 0, li = lineinfo; i < lnr; i++, li++) {
975                                 li->x_min = ((li->x_max - li->x_min) + xof_scale);
976
977                                 if (li->char_nr > 1) {
978                                         li->x_min /= (float)(li->char_nr - 1);
979                                 }
980                         }
981                         for (i = 0; i <= slen; i++) {
982                                 for (j = i; (!ELEM(mem[j], '\0', '\n')) && (chartransdata[j].dobreak == 0) && (j < slen); j++) {
983                                         /* do nothing */
984                                 }
985
986 //                              if ((mem[j] != '\n') && (mem[j])) {
987                                 ct->xof += ct->charnr * lineinfo[ct->linenr].x_min;
988 //                              }
989                                 ct++;
990                         }
991                 }
992                 else if ((cu->spacemode == CU_ALIGN_X_JUSTIFY) && use_textbox) {
993                         float curofs = 0.0f;
994                         for (i = 0; i <= slen; i++) {
995                                 for (j = i;
996                                      (mem[j]) && (mem[j] != '\n') && (chartransdata[j].dobreak == 0) && (j < slen);
997                                      j++)
998                                 {
999                                         /* pass */
1000                                 }
1001
1002                                 if ((mem[j] != '\n') &&
1003                                     ((chartransdata[j].dobreak != 0)))
1004                                 {
1005                                         if (mem[i] == ' ') {
1006                                                 struct TempLineInfo *li;
1007
1008                                                 li = &lineinfo[ct->linenr];
1009                                                 curofs += ((li->x_max - li->x_min) + xof_scale) / (float)li->wspace_nr;
1010                                         }
1011                                         ct->xof += curofs;
1012                                 }
1013                                 if (mem[i] == '\n' || chartransdata[i].dobreak) curofs = 0;
1014                                 ct++;
1015                         }
1016                 }
1017         }
1018
1019         /* top-baseline is default, in this case, do nothing */
1020         if (cu->align_y != CU_ALIGN_Y_TOP_BASELINE) {
1021                 if (tb_scale.h != 0.0f) {
1022                         /* top and top-baseline are the same when text-boxes are used */
1023                         if (cu->align_y != CU_ALIGN_Y_TOP && i_textbox < slen) {
1024                                 /* all previous textboxes are 'full', only align the last used text-box */
1025                                 float yoff = 0.0f;
1026                                 int lines;
1027                                 struct CharTrans *ct_last, *ct_textbox;
1028
1029                                 ct_last = chartransdata + slen - 1;
1030                                 ct_textbox = chartransdata + i_textbox;
1031
1032                                 lines = ct_last->linenr - ct_textbox->linenr + 1;
1033                                 if (mem[slen - 1] == '\n') {
1034                                         lines++;
1035                                 }
1036
1037                                 if (cu->align_y == CU_ALIGN_Y_BOTTOM) {
1038                                         yoff = (lines * linedist) - tb_scale.h;
1039                                 }
1040                                 else if (cu->align_y == CU_ALIGN_Y_CENTER) {
1041                                         yoff = 0.5f * ((lines * linedist) - tb_scale.h);
1042                                 }
1043
1044                                 ct = ct_textbox;
1045                                 for (i = i_textbox - 1; i < slen; i++) {
1046                                         ct->yof += yoff;
1047                                         ct++;
1048                                 }
1049                         }
1050                 }
1051                 else {
1052                         /* non text-box case handled separately */
1053                         ct = chartransdata;
1054                         float yoff = 0.0f;
1055
1056                         if (cu->align_y == CU_ALIGN_Y_TOP) {
1057                                 yoff = -linedist;
1058                         }
1059                         else if (cu->align_y == CU_ALIGN_Y_BOTTOM) {
1060                                 yoff = (lnr - 1.0f) * linedist;
1061                         }
1062                         else if (cu->align_y == CU_ALIGN_Y_CENTER) {
1063                                 yoff = (lnr - 2.0f) * linedist * 0.5f;
1064                         }
1065
1066                         for (i = 0; i <= slen; i++) {
1067                                 ct->yof += yoff;
1068                                 ct++;
1069                         }
1070                 }
1071         }
1072
1073         MEM_freeN(lineinfo);
1074
1075         /* TEXT ON CURVE */
1076         /* Note: Only OB_CURVE objects could have a path  */
1077         if (cu->textoncurve && cu->textoncurve->type == OB_CURVE) {
1078                 BLI_assert(cu->textoncurve->curve_cache != NULL);
1079                 if (cu->textoncurve->curve_cache->path) {
1080                         float distfac, imat[4][4], imat3[3][3], cmat[3][3];
1081                         float minx, maxx, miny, maxy;
1082                         float timeofs, sizefac;
1083
1084                         if (ob != NULL) {
1085                                 invert_m4_m4(imat, ob->obmat);
1086                         }
1087                         else {
1088                                 unit_m4(imat);
1089                         }
1090                         copy_m3_m4(imat3, imat);
1091
1092                         copy_m3_m4(cmat, cu->textoncurve->obmat);
1093                         mul_m3_m3m3(cmat, cmat, imat3);
1094                         sizefac = normalize_v3(cmat[0]) / cu->fsize;
1095                         
1096                         minx = miny = 1.0e20f;
1097                         maxx = maxy = -1.0e20f;
1098                         ct = chartransdata;
1099                         for (i = 0; i <= slen; i++, ct++) {
1100                                 if (minx > ct->xof) minx = ct->xof;
1101                                 if (maxx < ct->xof) maxx = ct->xof;
1102                                 if (miny > ct->yof) miny = ct->yof;
1103                                 if (maxy < ct->yof) maxy = ct->yof;
1104                         }
1105                         
1106                         /* we put the x-coordinaat exact at the curve, the y is rotated */
1107                         
1108                         /* length correction */
1109                         distfac = sizefac * cu->textoncurve->curve_cache->path->totdist / (maxx - minx);
1110                         timeofs = 0.0f;
1111                         
1112                         if (distfac > 1.0f) {
1113                                 /* path longer than text: spacemode involves */
1114                                 distfac = 1.0f / distfac;
1115                                 
1116                                 if (cu->spacemode == CU_ALIGN_X_RIGHT) {
1117                                         timeofs = 1.0f - distfac;
1118                                 }
1119                                 else if (cu->spacemode == CU_ALIGN_X_MIDDLE) {
1120                                         timeofs = (1.0f - distfac) / 2.0f;
1121                                 }
1122                                 else if (cu->spacemode == CU_ALIGN_X_FLUSH) {
1123                                         distfac = 1.0f;
1124                                 }
1125                         }
1126                         else {
1127                                 distfac = 1.0;
1128                         }
1129
1130                         distfac /= (maxx - minx);
1131                         
1132                         timeofs += distfac * cu->xof;  /* not cyclic */
1133                         
1134                         ct = chartransdata;
1135                         for (i = 0; i < slen; i++, ct++) {
1136                                 float ctime, dtime, vec[4], tvec[4], rotvec[3];
1137                                 float si, co;
1138                                 
1139                                 /* rotate around center character */
1140                                 info = &custrinfo[i];
1141                                 ascii = mem[i];
1142                                 if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
1143                                         ascii = towupper(ascii);
1144                                 }
1145
1146                                 che = find_vfont_char(vfd, ascii);
1147         
1148                                 twidth = char_width(cu, che, info);
1149
1150                                 dtime = distfac * 0.5f * twidth;
1151
1152                                 ctime = timeofs + distfac * (ct->xof - minx);
1153                                 CLAMP(ctime, 0.0f, 1.0f);
1154
1155                                 /* calc the right loc AND the right rot separately */
1156                                 /* vec, tvec need 4 items */
1157                                 where_on_path(cu->textoncurve, ctime, vec, tvec, NULL, NULL, NULL);
1158                                 where_on_path(cu->textoncurve, ctime + dtime, tvec, rotvec, NULL, NULL, NULL);
1159                                 
1160                                 mul_v3_fl(vec, sizefac);
1161                                 
1162                                 ct->rot = (float)M_PI - atan2f(rotvec[1], rotvec[0]);
1163
1164                                 si = sinf(ct->rot);
1165                                 co = cosf(ct->rot);
1166
1167                                 yof = ct->yof;
1168                                 
1169                                 ct->xof = vec[0] + si * yof;
1170                                 ct->yof = vec[1] + co * yof;
1171
1172                                 if (selboxes && (i >= selstart) && (i <= selend)) {
1173                                         EditFontSelBox *sb;
1174                                         sb = &selboxes[i - selstart];
1175                                         sb->rot = -ct->rot;
1176                                 }
1177                         }
1178                         /* null character is always zero width, no need to iterate over it */
1179                         chartransdata[slen] = chartransdata[slen - 1];
1180                 }
1181         }
1182
1183         if (selboxes) {
1184                 ct = chartransdata;
1185                 for (i = 0; i <= selend; i++, ct++) {
1186                         if (i >= selstart) {
1187                                 selboxes[i - selstart].x = ct->xof * cu->fsize;
1188                                 selboxes[i - selstart].y = ct->yof * cu->fsize;
1189                         }
1190                 }
1191         }
1192
1193         if (mode == FO_CURSUP || mode == FO_CURSDOWN || mode == FO_PAGEUP || mode == FO_PAGEDOWN) {
1194                 ct = &chartransdata[ef->pos];
1195
1196                 if ((mode == FO_CURSUP || mode == FO_PAGEUP) && ct->linenr == 0) {
1197                         /* pass */
1198                 }
1199                 else if ((mode == FO_CURSDOWN || mode == FO_PAGEDOWN) && ct->linenr == lnr) {
1200                         /* pass */
1201                 }
1202                 else {
1203                         switch (mode) {
1204                                 case FO_CURSUP:     lnr = ct->linenr - 1; break;
1205                                 case FO_CURSDOWN:   lnr = ct->linenr + 1; break;
1206                                 case FO_PAGEUP:     lnr = ct->linenr - 10; break;
1207                                 case FO_PAGEDOWN:   lnr = ct->linenr + 10; break;
1208                         }
1209                         cnr = ct->charnr;
1210                         /* seek for char with lnr en cnr */
1211                         ef->pos = 0;
1212                         ct = chartransdata;
1213                         for (i = 0; i < slen; i++) {
1214                                 if (ct->linenr == lnr) {
1215                                         if ((ct->charnr == cnr) || ((ct + 1)->charnr == 0)) {
1216                                                 break;
1217                                         }
1218                                 }
1219                                 else if (ct->linenr > lnr) {
1220                                         break;
1221                                 }
1222                                 ef->pos++;
1223                                 ct++;
1224                         }
1225                 }
1226         }
1227         
1228         /* cursor first */
1229         if (ef) {
1230                 float si, co;
1231                 
1232                 ct = &chartransdata[ef->pos];
1233                 si = sinf(ct->rot);
1234                 co = cosf(ct->rot);
1235
1236                 f = ef->textcurs[0];
1237                 
1238                 f[0] = cu->fsize * (-0.1f * co + ct->xof);
1239                 f[1] = cu->fsize * ( 0.1f * si + ct->yof);
1240                 
1241                 f[2] = cu->fsize * ( 0.1f * co + ct->xof);
1242                 f[3] = cu->fsize * (-0.1f * si + ct->yof);
1243                 
1244                 f[4] = cu->fsize * ( 0.1f * co + 0.8f * si + ct->xof);
1245                 f[5] = cu->fsize * (-0.1f * si + 0.8f * co + ct->yof);
1246                 
1247                 f[6] = cu->fsize * (-0.1f * co + 0.8f * si + ct->xof);
1248                 f[7] = cu->fsize * ( 0.1f * si + 0.8f * co + ct->yof);
1249                 
1250         }
1251
1252         if (mode == FO_SELCHANGE) {
1253                 MEM_freeN(chartransdata);
1254                 chartransdata = NULL;
1255                 goto finally;
1256         }
1257
1258         if (mode == FO_EDIT) {
1259                 /* make nurbdata */
1260                 BKE_nurbList_free(r_nubase);
1261                 
1262                 ct = chartransdata;
1263                 for (i = 0; i < slen; i++) {
1264                         unsigned int cha = (unsigned int) mem[i];
1265                         info = &(custrinfo[i]);
1266
1267                         if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
1268                                 cha = towupper(cha);
1269                         }
1270
1271                         if (ob == NULL || info->mat_nr > (ob->totcol)) {
1272                                 /* printf("Error: Illegal material index (%d) in text object, setting to 0\n", info->mat_nr); */
1273                                 info->mat_nr = 0;
1274                         }
1275                         /* We do not want to see any character for \n or \r */
1276                         if (cha != '\n')
1277                                 buildchar(cu, r_nubase, cha, info, ct->xof, ct->yof, ct->rot, i);
1278
1279                         if ((info->flag & CU_CHINFO_UNDERLINE) && (cha != '\n')) {
1280                                 float ulwidth, uloverlap = 0.0f;
1281                                 rctf rect;
1282
1283                                 if ((i < (slen - 1)) && (mem[i + 1] != '\n') &&
1284                                     ((mem[i + 1] != ' ') || (custrinfo[i + 1].flag & CU_CHINFO_UNDERLINE)) &&
1285                                     ((custrinfo[i + 1].flag & CU_CHINFO_WRAP) == 0))
1286                                 {
1287                                         uloverlap = xtrax + 0.1f;
1288                                 }
1289                                 /* Find the character, the characters has to be in the memory already
1290                                  * since character checking has been done earlier already. */
1291                                 che = find_vfont_char(vfd, cha);
1292
1293                                 twidth = char_width(cu, che, info);
1294                                 ulwidth = (twidth * (1.0f + (info->kern / 40.0f))) + uloverlap;
1295
1296                                 rect.xmin = ct->xof;
1297                                 rect.xmax = rect.xmin + ulwidth;
1298
1299                                 rect.ymin = ct->yof;
1300                                 rect.ymax = rect.ymin - cu->ulheight;
1301
1302                                 build_underline(cu, r_nubase,
1303                                                 &rect, cu->ulpos - 0.05f,
1304                                                 ct->rot, i, info->mat_nr);
1305                         }
1306                         ct++;
1307                 }
1308         }
1309
1310         ok = true;
1311
1312 finally:
1313
1314         {
1315                 if (r_text) {
1316                         *r_text = mem;
1317                         *r_text_len = slen;
1318                         *r_text_free = (ef == NULL);
1319                 }
1320                 else {
1321                         if (ef == NULL) {
1322                                 MEM_freeN((void *)mem);
1323                         }
1324                 }
1325         }
1326
1327         if (chartransdata) {
1328                 if (ok && r_chartransdata) {
1329                         *r_chartransdata = chartransdata;
1330                 }
1331                 else {
1332                         MEM_freeN(chartransdata);
1333                 }
1334         }
1335
1336         return ok;
1337
1338 #undef MARGIN_X_MIN
1339 #undef MARGIN_Y_MIN
1340 }
1341
1342
1343 bool BKE_vfont_to_curve_nubase(Object *ob, int mode, ListBase *r_nubase)
1344 {
1345         BLI_assert(ob->type == OB_FONT);
1346
1347         return BKE_vfont_to_curve_ex(ob, ob->data, mode, r_nubase,
1348                                      NULL, NULL, NULL, NULL);
1349 }
1350
1351 bool BKE_vfont_to_curve(Object *ob, int mode)
1352 {
1353         Curve *cu = ob->data;
1354
1355         return BKE_vfont_to_curve_ex(ob, ob->data, mode, &cu->nurb, NULL, NULL, NULL, NULL);
1356 }
1357
1358
1359 /* -------------------------------------------------------------------- */
1360
1361 /** \name VFont Clipboard
1362  * \{ */
1363
1364 static struct {
1365         wchar_t *text_buffer;
1366         CharInfo *info_buffer;
1367         size_t len_wchar;
1368         size_t len_utf8;
1369 } g_vfont_clipboard = {NULL};
1370
1371 void BKE_vfont_clipboard_free(void)
1372 {
1373         MEM_SAFE_FREE(g_vfont_clipboard.text_buffer);
1374         MEM_SAFE_FREE(g_vfont_clipboard.info_buffer);
1375         g_vfont_clipboard.len_wchar = 0;
1376         g_vfont_clipboard.len_utf8 = 0;
1377 }
1378
1379 void BKE_vfont_clipboard_set(const wchar_t *text_buf, const CharInfo *info_buf, const size_t len)
1380 {
1381         wchar_t *text;
1382         CharInfo *info;
1383
1384         /* clean previous buffers*/
1385         BKE_vfont_clipboard_free();
1386
1387         text = MEM_malloc_arrayN((len + 1), sizeof(wchar_t), __func__);
1388         if (text == NULL) {
1389                 return;
1390         }
1391
1392         info = MEM_malloc_arrayN(len, sizeof(CharInfo), __func__);
1393         if (info == NULL) {
1394                 MEM_freeN(text);
1395                 return;
1396         }
1397
1398         memcpy(text, text_buf, len * sizeof(wchar_t));
1399         text[len] = '\0';
1400         memcpy(info, info_buf, len * sizeof(CharInfo));
1401
1402         /* store new buffers */
1403         g_vfont_clipboard.text_buffer = text;
1404         g_vfont_clipboard.info_buffer = info;
1405         g_vfont_clipboard.len_utf8 = BLI_wstrlen_utf8(text);
1406         g_vfont_clipboard.len_wchar = len;
1407 }
1408
1409 void BKE_vfont_clipboard_get(
1410         wchar_t **r_text_buf, CharInfo **r_info_buf,
1411         size_t *r_len_utf8, size_t *r_len_wchar)
1412 {
1413         if (r_text_buf) {
1414                 *r_text_buf = g_vfont_clipboard.text_buffer;
1415         }
1416
1417         if (r_info_buf) {
1418                 *r_info_buf = g_vfont_clipboard.info_buffer;
1419         }
1420
1421         if (r_len_wchar) {
1422                 *r_len_wchar = g_vfont_clipboard.len_wchar;
1423         }
1424
1425         if (r_len_utf8) {
1426                 *r_len_utf8 = g_vfont_clipboard.len_utf8;
1427         }
1428 }
1429
1430 /** \} */