2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/blenkernel/intern/font.c
40 #include "MEM_guardedalloc.h"
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"
49 #include "BLI_threads.h"
50 #include "BLI_vfontdata.h"
52 #include "DNA_packedFile_types.h"
53 #include "DNA_curve_types.h"
54 #include "DNA_vfont_types.h"
55 #include "DNA_object_types.h"
57 #include "BKE_packedFile.h"
58 #include "BKE_library.h"
60 #include "BKE_global.h"
63 #include "BKE_curve.h"
65 static ThreadRWMutex vfont_rwlock = BLI_RWLOCK_INITIALIZER;
68 void BKE_vfont_free_data(struct VFont *vfont)
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);
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);
85 BLI_ghash_free(vfont->data->characters, NULL, NULL);
88 MEM_freeN(vfont->data);
93 freePackedFile(vfont->temp_pf); /* NULL when the font file can't be found on disk */
94 vfont->temp_pf = NULL;
98 void BKE_vfont_free(struct VFont *vf)
100 if (vf == NULL) return;
102 BKE_vfont_free_data(vf);
104 if (vf->packedfile) {
105 freePackedFile(vf->packedfile);
106 vf->packedfile = NULL;
110 static void *builtin_font_data = NULL;
111 static int builtin_font_size = 0;
113 bool BKE_vfont_is_builtin(struct VFont *vfont)
115 return STREQ(vfont->name, FO_BUILTIN_NAME);
118 void BKE_vfont_builtin_register(void *mem, int size)
120 builtin_font_data = mem;
121 builtin_font_size = size;
124 static PackedFile *get_builtin_packedfile(void)
126 if (!builtin_font_data) {
127 printf("Internal error, builtin font not loaded\n");
132 void *mem = MEM_mallocN(builtin_font_size, "vfd_builtin");
134 memcpy(mem, builtin_font_data, builtin_font_size);
136 return newPackedFileMemory(mem, builtin_font_size);
140 static VFontData *vfont_get_data(Main *bmain, VFont *vfont)
146 /* And then set the data */
150 BLI_rw_mutex_lock(&vfont_rwlock, THREAD_LOCK_WRITE);
153 /* Check data again, since it might have been already
154 * initialized from other thread (previous check is
155 * not accurate or threading, just prevents unneeded
156 * lock if all the data is here for sure).
158 BLI_rw_mutex_unlock(&vfont_rwlock);
162 if (BKE_vfont_is_builtin(vfont)) {
163 pf = get_builtin_packedfile();
166 if (vfont->packedfile) {
167 pf = vfont->packedfile;
169 /* We need to copy a tmp font to memory unless it is already there */
170 if (vfont->temp_pf == NULL) {
171 vfont->temp_pf = dupPackedFile(pf);
175 pf = newPackedFile(NULL, vfont->name, ID_BLEND_PATH(bmain, &vfont->id));
177 if (vfont->temp_pf == NULL) {
178 vfont->temp_pf = newPackedFile(NULL, vfont->name, ID_BLEND_PATH(bmain, &vfont->id));
182 printf("Font file doesn't exist: %s\n", vfont->name);
185 * missing file shouldn't modify path! - campbell */
187 strcpy(vfont->name, FO_BUILTIN_NAME);
189 pf = get_builtin_packedfile();
194 vfont->data = BLI_vfontdata_from_freetypefont(pf);
195 if (pf != vfont->packedfile) {
200 BLI_rw_mutex_unlock(&vfont_rwlock);
206 VFont *BKE_vfont_load(Main *bmain, const char *filepath)
208 char filename[FILE_MAXFILE];
211 PackedFile *temp_pf = NULL;
214 if (STREQ(filepath, FO_BUILTIN_NAME)) {
215 BLI_strncpy(filename, filepath, sizeof(filename));
217 pf = get_builtin_packedfile();
221 BLI_split_file_part(filepath, filename, sizeof(filename));
222 pf = newPackedFile(NULL, filepath, bmain->name);
223 temp_pf = newPackedFile(NULL, filepath, bmain->name);
231 vfd = BLI_vfontdata_from_freetypefont(pf);
233 vfont = BKE_libblock_alloc(bmain, ID_VF, filename);
236 /* if there's a font name, use it for the ID name */
237 if (vfd->name[0] != '\0') {
238 BLI_strncpy(vfont->id.name + 2, vfd->name, sizeof(vfont->id.name) - 2);
240 BLI_strncpy(vfont->name, filepath, sizeof(vfont->name));
242 /* if autopack is on store the packedfile in de font structure */
243 if (!is_builtin && (G.fileflags & G_AUTOPACK)) {
244 vfont->packedfile = pf;
247 /* Do not add FO_BUILTIN_NAME to temporary listbase */
248 if (!STREQ(filename, FO_BUILTIN_NAME)) {
249 vfont->temp_pf = temp_pf;
253 /* Free the packed file */
254 if (!vfont || vfont->packedfile != pf) {
262 VFont *BKE_vfont_load_exists_ex(struct Main *bmain, const char *filepath, bool *r_exists)
265 char str[FILE_MAX], strtest[FILE_MAX];
267 BLI_strncpy(str, filepath, sizeof(str));
268 BLI_path_abs(str, bmain->name);
270 /* first search an identical filepath */
271 for (vfont = bmain->vfont.first; vfont; vfont = vfont->id.next) {
272 BLI_strncpy(strtest, vfont->name, sizeof(vfont->name));
273 BLI_path_abs(strtest, ID_BLEND_PATH(bmain, &vfont->id));
275 if (BLI_path_cmp(strtest, str) == 0) {
276 vfont->id.us++; /* officially should not, it doesn't link here! */
285 return BKE_vfont_load(bmain, filepath);
288 VFont *BKE_vfont_load_exists(struct Main *bmain, const char *filepath)
290 return BKE_vfont_load_exists_ex(bmain, filepath, NULL);
293 static VFont *which_vfont(Curve *cu, CharInfo *info)
295 switch (info->flag & (CU_CHINFO_BOLD | CU_CHINFO_ITALIC)) {
297 return cu->vfontb ? cu->vfontb : cu->vfont;
298 case CU_CHINFO_ITALIC:
299 return cu->vfonti ? cu->vfonti : cu->vfont;
300 case (CU_CHINFO_BOLD | CU_CHINFO_ITALIC):
301 return cu->vfontbi ? cu->vfontbi : cu->vfont;
307 VFont *BKE_vfont_builtin_get(void)
311 for (vfont = G.main->vfont.first; vfont; vfont = vfont->id.next) {
312 if (BKE_vfont_is_builtin(vfont)) {
317 return BKE_vfont_load(G.main, FO_BUILTIN_NAME);
320 static VChar *find_vfont_char(VFontData *vfd, unsigned int character)
322 return BLI_ghash_lookup(vfd->characters, SET_UINT_IN_POINTER(character));
325 static void build_underline(Curve *cu, ListBase *nubase, const rctf *rect,
326 float yofs, float rot, int charidx, short mat_nr)
331 nu2 = (Nurb *) MEM_callocN(sizeof(Nurb), "underline_nurb");
332 nu2->resolu = cu->resolu;
334 nu2->knotsu = nu2->knotsv = NULL;
336 nu2->charidx = charidx + 1000;
337 if (mat_nr > 0) nu2->mat_nr = mat_nr - 1;
342 nu2->flagu = CU_NURB_CYCLIC;
344 bp = (BPoint *)MEM_callocN(4 * sizeof(BPoint), "underline_bp");
346 copy_v4_fl4(bp[0].vec, rect->xmin, (rect->ymax + yofs), 0.0f, 1.0f);
347 copy_v4_fl4(bp[1].vec, rect->xmax, (rect->ymax + yofs), 0.0f, 1.0f);
348 copy_v4_fl4(bp[2].vec, rect->xmax, (rect->ymin + yofs), 0.0f, 1.0f);
349 copy_v4_fl4(bp[3].vec, rect->xmin, (rect->ymin + yofs), 0.0f, 1.0f);
352 BLI_addtail(nubase, nu2);
361 for (i = nu2->pntsu; i > 0; i--) {
367 x = fp[0] - rect->xmin;
368 y = fp[1] - rect->ymin;
370 fp[0] = (+co * x + si * y) + rect->xmin;
371 fp[1] = (-si * x + co * y) + rect->ymin;
379 mul_v2_fl(bp[0].vec, cu->fsize);
380 mul_v2_fl(bp[1].vec, cu->fsize);
381 mul_v2_fl(bp[2].vec, cu->fsize);
382 mul_v2_fl(bp[3].vec, cu->fsize);
385 static void buildchar(Main *bmain, Curve *cu, ListBase *nubase, unsigned int character, CharInfo *info,
386 float ofsx, float ofsy, float rot, int charidx)
388 BezTriple *bezt1, *bezt2;
389 Nurb *nu1 = NULL, *nu2 = NULL;
390 float *fp, fsize, shear, x, si, co;
391 VFontData *vfd = NULL;
395 vfd = vfont_get_data(bmain, which_vfont(cu, info));
399 if (cu->selend < cu->selstart) {
400 if ((charidx >= (cu->selend)) && (charidx <= (cu->selstart - 2)))
404 if ((charidx >= (cu->selstart - 1)) && (charidx <= (cu->selend - 1)))
409 /* make a copy at distance ofsx, ofsy with shear */
415 che = find_vfont_char(vfd, character);
417 /* Select the glyph data */
419 nu1 = che->nurbsbase.first;
421 /* Create the character */
425 nu2 = (Nurb *) MEM_mallocN(sizeof(Nurb), "duplichar_nurb");
426 if (nu2 == NULL) break;
427 memcpy(nu2, nu1, sizeof(struct Nurb));
428 nu2->resolu = cu->resolu;
430 nu2->knotsu = nu2->knotsv = NULL;
431 nu2->flag = CU_SMOOTH;
432 nu2->charidx = charidx;
433 if (info->mat_nr > 0) {
434 nu2->mat_nr = info->mat_nr - 1;
439 /* nu2->trim.first = 0; */
440 /* nu2->trim.last = 0; */
443 bezt2 = (BezTriple *)MEM_mallocN(i * sizeof(BezTriple), "duplichar_bezt2");
448 memcpy(bezt2, bezt1, i * sizeof(struct BezTriple));
454 for (i = nu2->pntsu; i > 0; i--) {
455 bezt2->vec[0][0] += shear * bezt2->vec[0][1];
456 bezt2->vec[1][0] += shear * bezt2->vec[1][1];
457 bezt2->vec[2][0] += shear * bezt2->vec[2][1];
463 for (i = nu2->pntsu; i > 0; i--) {
467 fp[0] = co * x + si * fp[1];
468 fp[1] = -si * x + co * fp[1];
470 fp[3] = co * x + si * fp[4];
471 fp[4] = -si * x + co * fp[4];
473 fp[6] = co * x + si * fp[7];
474 fp[7] = -si * x + co * fp[7];
481 if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
482 const float sca = cu->smallcaps_scale;
483 for (i = nu2->pntsu; i > 0; i--) {
496 for (i = nu2->pntsu; i > 0; i--) {
498 fp[0] = (fp[0] + ofsx) * fsize;
499 fp[1] = (fp[1] + ofsy) * fsize;
500 fp[3] = (fp[3] + ofsx) * fsize;
501 fp[4] = (fp[4] + ofsy) * fsize;
502 fp[6] = (fp[6] + ofsx) * fsize;
503 fp[7] = (fp[7] + ofsy) * fsize;
507 BLI_addtail(nubase, nu2);
514 int BKE_vfont_select_get(Object *ob, int *r_start, int *r_end)
516 Curve *cu = ob->data;
517 EditFont *ef = cu->editfont;
518 int start, end, direction;
520 if ((ob->type != OB_FONT) || (ef == NULL)) return 0;
522 BLI_assert(ef->len >= 0);
523 BLI_assert(ef->selstart >= 0 && ef->selstart <= ef->len + 1);
524 BLI_assert(ef->selend >= 0 && ef->selend <= ef->len);
525 BLI_assert(ef->pos >= 0 && ef->pos <= ef->len);
527 if (ef->selstart == 0) {
531 if (ef->selstart <= ef->selend) {
532 start = ef->selstart - 1;
533 end = ef->selend - 1;
538 end = ef->selstart - 2;
542 if (start == end + 1) {
546 BLI_assert(start < end + 1);
553 void BKE_vfont_select_clamp(Object *ob)
555 Curve *cu = ob->data;
556 EditFont *ef = cu->editfont;
558 BLI_assert((ob->type == OB_FONT) && ef);
560 CLAMP_MAX(ef->pos, ef->len);
561 CLAMP_MAX(ef->selstart, ef->len + 1);
562 CLAMP_MAX(ef->selend, ef->len);
565 static float char_width(Curve *cu, VChar *che, CharInfo *info)
567 /* The character wasn't found, propably ascii = 0, then the width shall be 0 as well */
571 else if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
572 return che->width * cu->smallcaps_scale;
579 static void textbox_scale(TextBox *tb_dst, const TextBox *tb_src, float scale)
581 tb_dst->x = tb_src->x * scale;
582 tb_dst->y = tb_src->y * scale;
583 tb_dst->w = tb_src->w * scale;
584 tb_dst->h = tb_src->h * scale;
588 * Used for storing per-line data for alignment & wrapping.
590 struct TempLineInfo {
591 float x_min; /* left margin */
592 float x_max; /* right margin */
593 int char_nr; /* number of characters */
594 int wspace_nr; /* number of whitespaces of line */
597 bool BKE_vfont_to_curve_ex(Main *bmain, Object *ob, int mode, ListBase *r_nubase,
598 const wchar_t **r_text, int *r_text_len, bool *r_text_free,
599 struct CharTrans **r_chartransdata)
601 Curve *cu = ob->data;
602 EditFont *ef = cu->editfont;
603 EditFontSelBox *selboxes = NULL;
604 VFont *vfont, *oldvfont;
605 VFontData *vfd = NULL;
606 CharInfo *info = NULL, *custrinfo;
610 struct CharTrans *chartransdata = NULL, *ct;
611 struct TempLineInfo *lineinfo;
612 float *f, xof, yof, xtrax, linedist;
613 float twidth, maxlen = 0;
616 int selstart, selend;
617 int cnr = 0, lnr = 0, wsnr = 0;
621 const float xof_scale = cu->xof / cu->fsize;
622 const float yof_scale = cu->yof / cu->fsize;
624 #define MARGIN_X_MIN (xof_scale + tb_scale.x)
625 #define MARGIN_Y_MIN (yof_scale + tb_scale.y)
627 /* remark: do calculations including the trailing '\0' of a string
628 * because the cursor can be at that location */
630 BLI_assert(ob->type == OB_FONT);
635 if (cu->str == NULL) return ok;
636 if (vfont == NULL) return ok;
638 vfd = vfont_get_data(bmain, vfont);
640 /* The VFont Data can not be found */
646 custrinfo = ef->textbufinfo;
650 slen = cu->len_wchar;
652 /* Create unicode string */
653 mem_tmp = MEM_mallocN(((slen + 1) * sizeof(wchar_t)), "convertedmem");
655 BLI_strncpy_wchar_from_utf8(mem_tmp, cu->str, slen + 1);
657 if (cu->strinfo == NULL) { /* old file */
658 cu->strinfo = MEM_callocN((slen + 4) * sizeof(CharInfo), "strinfo compat");
660 custrinfo = cu->strinfo;
666 cu->tb = MEM_callocN(MAXTEXTBOX * sizeof(TextBox), "TextBox compat");
670 MEM_freeN(ef->selboxes);
672 if (BKE_vfont_select_get(ob, &selstart, &selend))
673 ef->selboxes = MEM_callocN((selend - selstart + 1) * sizeof(EditFontSelBox), "font selboxes");
677 selboxes = ef->selboxes;
680 /* calc offset and rotation of each char */
681 ct = chartransdata = MEM_callocN((slen + 1) * sizeof(struct CharTrans), "buildtext");
683 /* We assume the worst case: 1 character per line (is freed at end anyway) */
684 lineinfo = MEM_mallocN(sizeof(*lineinfo) * (slen * 2 + 1), "lineinfo");
686 linedist = cu->linedist;
689 textbox_scale(&tb_scale, &cu->tb[curbox], 1.0f / cu->fsize);
690 use_textbox = (tb_scale.w != 0.0f);
696 xtrax = 0.5f * cu->spacing - 0.5f;
700 for (i = 0; i < slen; i++) {
701 custrinfo[i].flag &= ~(CU_CHINFO_WRAP | CU_CHINFO_SMALLCAPS_CHECK);
704 for (i = 0; i <= slen; i++) {
706 /* Characters in the list */
707 info = &custrinfo[i];
709 if (info->flag & CU_CHINFO_SMALLCAPS) {
710 ascii = towupper(ascii);
711 if (mem[i] != ascii) {
712 info->flag |= CU_CHINFO_SMALLCAPS_CHECK;
716 vfont = which_vfont(cu, info);
718 if (vfont == NULL) break;
720 if (vfont != oldvfont) {
721 vfd = vfont_get_data(bmain, vfont);
725 /* VFont Data for VFont couldn't be found */
727 MEM_freeN(chartransdata);
728 chartransdata = NULL;
733 if (!ELEM(ascii, '\n', '\0')) {
734 BLI_rw_mutex_lock(&vfont_rwlock, THREAD_LOCK_READ);
735 che = find_vfont_char(vfd, ascii);
736 BLI_rw_mutex_unlock(&vfont_rwlock);
739 * The character wasn't in the current curve base so load it
740 * But if the font is built-in then do not try loading since
741 * whole font is in the memory already
743 if (che == NULL && BKE_vfont_is_builtin(vfont) == false) {
744 BLI_rw_mutex_lock(&vfont_rwlock, THREAD_LOCK_WRITE);
745 /* Check it once again, char might have been already load
746 * between previous BLI_rw_mutex_unlock() and this BLI_rw_mutex_lock().
748 * Such a check should not be a bottleneck since it wouldn't
749 * happen often once all the chars are load.
751 if ((che = find_vfont_char(vfd, ascii)) == NULL) {
752 che = BLI_vfontchar_from_freetypefont(vfont, ascii);
754 BLI_rw_mutex_unlock(&vfont_rwlock);
761 twidth = char_width(cu, che, info);
763 /* Calculate positions */
764 if ((tb_scale.w != 0.0f) &&
765 (ct->dobreak == 0) &&
766 (((xof - tb_scale.x) + twidth) > xof_scale + tb_scale.w))
768 // fprintf(stderr, "linewidth exceeded: %c%c%c...\n", mem[i], mem[i+1], mem[i+2]);
769 for (j = i; j && (mem[j] != '\n') && (chartransdata[j].dobreak == 0); j--) {
770 if (mem[j] == ' ' || mem[j] == '-') {
772 cnr -= (i - (j - 1));
773 if (mem[j] == ' ') wsnr--;
774 if (mem[j] == '-') wsnr++;
778 custrinfo[i + 1].flag |= CU_CHINFO_WRAP;
781 if (chartransdata[j].dobreak) {
782 // fprintf(stderr, "word too long: %c%c%c...\n", mem[j], mem[j+1], mem[j+2]);
784 custrinfo[i + 1].flag |= CU_CHINFO_WRAP;
794 if (ascii == '\n' || ascii == 0 || ct->dobreak) {
802 lineinfo[lnr].x_min = (xof - xtrax) - tb_scale.x;
803 lineinfo[lnr].x_max = tb_scale.w;
804 lineinfo[lnr].char_nr = cnr;
805 lineinfo[lnr].wspace_nr = wsnr;
807 CLAMP_MIN(maxlen, lineinfo[lnr].x_min);
809 if ((tb_scale.h != 0.0f) &&
810 (cu->totbox > (curbox + 1)) &&
811 ((-(yof - tb_scale.y)) > (tb_scale.h - linedist) - yof_scale))
816 textbox_scale(&tb_scale, &cu->tb[curbox], 1.0f / cu->fsize);
821 /* XXX, has been unused for years, need to check if this is useful, r4613 r5282 - campbell */
834 else if (ascii == 9) { /* TAB */
842 tabfac = (xof - MARGIN_X_MIN + 0.01f);
843 tabfac = 2.0f * ceilf(tabfac / 2.0f);
844 xof = MARGIN_X_MIN + tabfac;
847 EditFontSelBox *sb = NULL;
855 if (selboxes && (i >= selstart) && (i <= selend)) {
856 sb = &selboxes[i - selstart];
857 sb->y = yof * cu->fsize - linedist * cu->fsize * 0.1f;
858 sb->h = linedist * cu->fsize;
859 sb->w = xof * cu->fsize;
863 wsfac = cu->wordspace;
870 /* Set the width of the character */
871 twidth = char_width(cu, che, info);
873 xof += (twidth * wsfac * (1.0f + (info->kern / 40.0f)) ) + xtrax;
876 sb->w = (xof * cu->fsize) - sb->w;
883 for (i = 0; i <= slen; i++) {
885 ct = &chartransdata[i];
886 if (ascii == '\n' || ct->dobreak) cu->lines++;
889 /* linedata is now: width of line */
891 if (cu->spacemode != CU_LEFT) {
894 if (cu->spacemode == CU_RIGHT) {
895 struct TempLineInfo *li;
897 for (i = 0, li = lineinfo; i < lnr; i++, li++) {
898 li->x_min = (li->x_max - li->x_min) + xof_scale;
901 for (i = 0; i <= slen; i++) {
902 ct->xof += lineinfo[ct->linenr].x_min;
906 else if (cu->spacemode == CU_MIDDLE) {
907 struct TempLineInfo *li;
909 for (i = 0, li = lineinfo; i < lnr; i++, li++) {
910 li->x_min = ((li->x_max - li->x_min) + xof_scale) / 2.0f;
913 for (i = 0; i <= slen; i++) {
914 ct->xof += lineinfo[ct->linenr].x_min;
918 else if ((cu->spacemode == CU_FLUSH) && use_textbox) {
919 struct TempLineInfo *li;
921 for (i = 0, li = lineinfo; i < lnr; i++, li++) {
922 li->x_min = ((li->x_max - li->x_min) + xof_scale);
924 if (li->char_nr > 1) {
925 li->x_min /= (float)(li->char_nr - 1);
928 for (i = 0; i <= slen; i++) {
929 for (j = i; (!ELEM(mem[j], '\0', '\n')) && (chartransdata[j].dobreak == 0) && (j < slen); j++) {
933 // if ((mem[j] != '\n') && (mem[j])) {
934 ct->xof += ct->charnr * lineinfo[ct->linenr].x_min;
939 else if ((cu->spacemode == CU_JUSTIFY) && use_textbox) {
941 for (i = 0; i <= slen; i++) {
943 (mem[j]) && (mem[j] != '\n') && (chartransdata[j].dobreak == 0) && (j < slen);
949 if ((mem[j] != '\n') &&
950 ((chartransdata[j].dobreak != 0)))
953 struct TempLineInfo *li;
955 li = &lineinfo[ct->linenr];
956 curofs += ((li->x_max - li->x_min) + xof_scale) / (float)li->wspace_nr;
960 if (mem[i] == '\n' || chartransdata[i].dobreak) curofs = 0;
969 /* Note: Only OB_CURVE objects could have a path */
970 if (cu->textoncurve && cu->textoncurve->type == OB_CURVE) {
971 BLI_assert(cu->textoncurve->curve_cache != NULL);
972 if (cu->textoncurve->curve_cache->path) {
973 float distfac, imat[4][4], imat3[3][3], cmat[3][3];
974 float minx, maxx, miny, maxy;
975 float timeofs, sizefac;
977 invert_m4_m4(imat, ob->obmat);
978 copy_m3_m4(imat3, imat);
980 copy_m3_m4(cmat, cu->textoncurve->obmat);
981 mul_m3_m3m3(cmat, cmat, imat3);
982 sizefac = normalize_v3(cmat[0]) / cu->fsize;
984 minx = miny = 1.0e20f;
985 maxx = maxy = -1.0e20f;
987 for (i = 0; i <= slen; i++, ct++) {
988 if (minx > ct->xof) minx = ct->xof;
989 if (maxx < ct->xof) maxx = ct->xof;
990 if (miny > ct->yof) miny = ct->yof;
991 if (maxy < ct->yof) maxy = ct->yof;
994 /* we put the x-coordinaat exact at the curve, the y is rotated */
996 /* length correction */
997 distfac = sizefac * cu->textoncurve->curve_cache->path->totdist / (maxx - minx);
1000 if (distfac > 1.0f) {
1001 /* path longer than text: spacemode involves */
1002 distfac = 1.0f / distfac;
1004 if (cu->spacemode == CU_RIGHT) {
1005 timeofs = 1.0f - distfac;
1007 else if (cu->spacemode == CU_MIDDLE) {
1008 timeofs = (1.0f - distfac) / 2.0f;
1010 else if (cu->spacemode == CU_FLUSH) {
1018 distfac /= (maxx - minx);
1020 timeofs += distfac * cu->xof; /* not cyclic */
1023 for (i = 0; i <= slen; i++, ct++) {
1024 float ctime, dtime, vec[4], tvec[4], rotvec[3];
1027 /* rotate around center character */
1028 info = &custrinfo[i];
1030 if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
1031 ascii = towupper(ascii);
1034 che = find_vfont_char(vfd, ascii);
1036 twidth = char_width(cu, che, info);
1038 dtime = distfac * 0.5f * twidth;
1040 ctime = timeofs + distfac * (ct->xof - minx);
1041 CLAMP(ctime, 0.0f, 1.0f);
1043 /* calc the right loc AND the right rot separately */
1044 /* vec, tvec need 4 items */
1045 where_on_path(cu->textoncurve, ctime, vec, tvec, NULL, NULL, NULL);
1046 where_on_path(cu->textoncurve, ctime + dtime, tvec, rotvec, NULL, NULL, NULL);
1048 mul_v3_fl(vec, sizefac);
1050 ct->rot = (float)M_PI - atan2f(rotvec[1], rotvec[0]);
1057 ct->xof = vec[0] + si * yof;
1058 ct->yof = vec[1] + co * yof;
1060 if (selboxes && (i >= selstart) && (i <= selend)) {
1062 sb = &selboxes[i - selstart];
1072 for (i = 0; i <= selend; i++, ct++) {
1073 if (i >= selstart) {
1074 selboxes[i - selstart].x = ct->xof * cu->fsize;
1075 selboxes[i - selstart].y = ct->yof * cu->fsize;
1080 if (mode == FO_CURSUP || mode == FO_CURSDOWN || mode == FO_PAGEUP || mode == FO_PAGEDOWN) {
1081 ct = &chartransdata[ef->pos];
1083 if ((mode == FO_CURSUP || mode == FO_PAGEUP) && ct->linenr == 0) {
1086 else if ((mode == FO_CURSDOWN || mode == FO_PAGEDOWN) && ct->linenr == lnr) {
1091 case FO_CURSUP: lnr = ct->linenr - 1; break;
1092 case FO_CURSDOWN: lnr = ct->linenr + 1; break;
1093 case FO_PAGEUP: lnr = ct->linenr - 10; break;
1094 case FO_PAGEDOWN: lnr = ct->linenr + 10; break;
1097 /* seek for char with lnr en cnr */
1100 for (i = 0; i < slen; i++) {
1101 if (ct->linenr == lnr) {
1102 if ((ct->charnr == cnr) || ((ct + 1)->charnr == 0)) {
1106 else if (ct->linenr > lnr) {
1119 ct = &chartransdata[ef->pos];
1123 f = ef->textcurs[0];
1125 f[0] = cu->fsize * (-0.1f * co + ct->xof);
1126 f[1] = cu->fsize * ( 0.1f * si + ct->yof);
1128 f[2] = cu->fsize * ( 0.1f * co + ct->xof);
1129 f[3] = cu->fsize * (-0.1f * si + ct->yof);
1131 f[4] = cu->fsize * ( 0.1f * co + 0.8f * si + ct->xof);
1132 f[5] = cu->fsize * (-0.1f * si + 0.8f * co + ct->yof);
1134 f[6] = cu->fsize * (-0.1f * co + 0.8f * si + ct->xof);
1135 f[7] = cu->fsize * ( 0.1f * si + 0.8f * co + ct->yof);
1139 if (mode == FO_SELCHANGE) {
1140 MEM_freeN(chartransdata);
1141 chartransdata = NULL;
1145 if (mode == FO_EDIT) {
1147 BKE_nurbList_free(r_nubase);
1150 for (i = 0; i < slen; i++) {
1151 unsigned int cha = (unsigned int) mem[i];
1152 info = &(custrinfo[i]);
1154 if (info->flag & CU_CHINFO_SMALLCAPS_CHECK) {
1155 cha = towupper(cha);
1158 if (info->mat_nr > (ob->totcol)) {
1159 /* printf("Error: Illegal material index (%d) in text object, setting to 0\n", info->mat_nr); */
1162 /* We do not want to see any character for \n or \r */
1164 buildchar(bmain, cu, r_nubase, cha, info, ct->xof, ct->yof, ct->rot, i);
1166 if ((info->flag & CU_CHINFO_UNDERLINE) && (cha != '\n')) {
1167 float ulwidth, uloverlap = 0.0f;
1170 if ((i < (slen - 1)) && (mem[i + 1] != '\n') &&
1171 ((mem[i + 1] != ' ') || (custrinfo[i + 1].flag & CU_CHINFO_UNDERLINE)) &&
1172 ((custrinfo[i + 1].flag & CU_CHINFO_WRAP) == 0))
1174 uloverlap = xtrax + 0.1f;
1176 /* Find the character, the characters has to be in the memory already
1177 * since character checking has been done earlier already. */
1178 che = find_vfont_char(vfd, cha);
1180 twidth = char_width(cu, che, info);
1181 ulwidth = (twidth * (1.0f + (info->kern / 40.0f))) + uloverlap;
1183 rect.xmin = ct->xof;
1184 rect.xmax = rect.xmin + ulwidth;
1186 rect.ymin = ct->yof;
1187 rect.ymax = rect.ymin - cu->ulheight;
1189 build_underline(cu, r_nubase,
1190 &rect, cu->ulpos - 0.05f,
1191 ct->rot, i, info->mat_nr);
1205 *r_text_free = (ef == NULL);
1209 MEM_freeN((void *)mem);
1214 if (chartransdata) {
1215 if (ok && r_chartransdata) {
1216 *r_chartransdata = chartransdata;
1219 MEM_freeN(chartransdata);
1230 bool BKE_vfont_to_curve_nubase(Main *bmain, Object *ob, int mode, ListBase *r_nubase)
1232 BLI_assert(ob->type == OB_FONT);
1234 return BKE_vfont_to_curve_ex(bmain, ob, mode, r_nubase,
1235 NULL, NULL, NULL, NULL);
1238 bool BKE_vfont_to_curve(Main *bmain, Object *ob, int mode)
1240 Curve *cu = ob->data;
1242 return BKE_vfont_to_curve_ex(bmain, ob, mode, &cu->nurb, NULL, NULL, NULL, NULL);