Blender V5.3
vfontdata_freetype.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 * The Original Code is written by Rob Haarsma (phase). All rights reserved. */
5
14
15#include "MEM_guardedalloc.h"
16
17#include "BLF_api.hh"
18
19#include "BLI_listbase.hh"
20#include "BLI_string.hh"
21#include "BLI_string_utf8.hh"
22
23#include "BKE_curve.hh"
24#include "BKE_vfont.hh"
25#include "BKE_vfontdata.hh"
26
28#include "DNA_vfont_types.h"
29
30namespace blender {
31
32extern const void *builtin_font_data;
33extern int builtin_font_size;
34
41
43{
44 int fontid = BLF_load_mem("FTVFont", static_cast<const uchar *>(pf->data), pf->size);
45 if (fontid == -1) {
46 return nullptr;
47 }
48
49 /* allocate blender font */
50 VFontData *vfd = MEM_new_zeroed<VFontData>("FTVFontData");
51
52 /* Get the font name. */
53 char *name = BLF_display_name_from_id(fontid);
54 const size_t name_len = STRNCPY_RLEN(vfd->name, name);
55 /* BLF_display_name result must be freed. */
56 MEM_delete(name);
57
58 BLI_str_utf8_invalid_strip(vfd->name, name_len);
59
61 fontid, &vfd->metrics.ascend_ratio, &vfd->metrics.em_ratio, &vfd->metrics.scale))
62 {
64 }
65
66 vfd->characters = MEM_new<Map<uint, VChar *>>(__func__);
67 vfd->characters->reserve(255);
68
69 BLF_unload_id(fontid);
70
71 return vfd;
72}
73
74VFontData *BKE_vfontdata_copy(const VFontData *vfont_src, const int /*flag*/)
75{
76 VFontData *vfont_dst = MEM_dupalloc(vfont_src);
77
78 if (vfont_src->characters != nullptr) {
79 vfont_dst->characters = MEM_new<Map<uint, VChar *>>(__func__);
80 vfont_dst->characters->reserve(vfont_src->characters->size());
81 for (const auto &item : vfont_src->characters->items()) {
82 VChar *vchar_src = item.value;
83 VChar *vchar_dst = vchar_src ? BKE_vfontdata_char_copy(vchar_src) : nullptr;
84 vfont_dst->characters->add(item.key, vchar_dst);
85 }
86 }
87
88 return vfont_dst;
89}
90
92{
93 if (!vfont) {
94 return nullptr;
95 }
96
97 int font_id = -1;
98 const bool is_builtin = BKE_vfont_is_builtin(vfont);
99
100 /* If the default font is selected (usually because nothing has been selected)
101 * then allow use of the fallback font stack when characters are not found in it.
102 * This allows a nice first experience, for testing, and allows the translation of the initial
103 * "Text" string. However, when a different specific font then do not use fallback,
104 * only allow characters that are included in that font.
105 * Do this for predictable control of what is shown when selecting specific fonts,
106 * also for consistent output when the UI fonts are changed or updated. */
107 const bool use_fallback = is_builtin;
108
109 if (is_builtin) {
110 font_id = BLF_load_mem(
111 vfont->data->name, static_cast<const uchar *>(builtin_font_data), builtin_font_size);
112 }
113 else if (vfont->temp_pf) {
114 font_id = BLF_load_mem(
115 vfont->data->name, static_cast<const uchar *>(vfont->temp_pf->data), vfont->temp_pf->size);
116 }
117
118 if (font_id == -1) {
119 return nullptr;
120 }
121
122 VChar *che = MEM_new_zeroed<VChar>("objfnt_char");
123
124 /* need to set a size for embolden, etc. */
125 BLF_size(font_id, 16);
126
127 if (!BLF_character_to_curves(font_id,
128 character,
129 &che->nurbsbase,
130 vfont->data->metrics.scale,
131 use_fallback,
132 &che->width,
133 &che->bounds))
134 {
135 /* Free but add to the character cache to prevent future lookups
136 * from attempting to load the font again. */
137 MEM_delete(che);
138 che = nullptr;
139 }
140
141 vfont->data->characters->add(character, che);
142 BLF_unload_id(font_id);
143 return che;
144}
145
147{
148 VChar *vchar_dst = MEM_dupalloc(vchar_src);
149
150 vchar_dst->nurbsbase.clear_no_delete();
151 BKE_nurbList_duplicate(&vchar_dst->nurbsbase, &vchar_src->nurbsbase);
152
153 return vchar_dst;
154}
155
156} // namespace blender
A structure to represent vector fonts, and to load them from PostScript fonts.
#define BLF_VFONT_METRICS_EM_RATIO_DEFAULT
Definition BLF_api.hh:117
#define BLF_VFONT_METRICS_SCALE_DEFAULT
Definition BLF_api.hh:116
#define BLF_VFONT_METRICS_ASCEND_RATIO_DEFAULT
Definition BLF_api.hh:118
#define STRNCPY_RLEN(dst, src)
Read Blender memory allocation and freeing.
#define pf(_x, _i)
Prefetch 64.
Definition gim_memory.h:48
BMesh Iterators.
VChar * BKE_vfontdata_char_copy(const VChar *vchar_src)
unsigned int uint
Definition BKE_text.h:55
bool BKE_vfont_is_builtin(const VFont *vfont)
Definition vfont.cc:278
int BLF_load_mem(const char *name, const unsigned char *mem, int mem_size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
Definition blf.cc:227
bool BLF_get_vfont_metrics(int fontid, float *ascend_ratio, float *em_ratio, float *scale)
Definition blf.cc:1145
void BLF_size(int fontid, float size)
Definition blf.cc:445
VChar * BKE_vfontdata_char_from_freetypefont(VFont *vfont, unsigned int character)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
unsigned char uchar
Definition BKE_text.h:58
VFontData * BKE_vfontdata_from_freetypefont(PackedFile *pf)
const void * builtin_font_data
Definition vfont.cc:62
bool BLF_character_to_curves(int fontid, unsigned int unicode, ListBaseT< Nurb > *nurbsbase, const float scale, bool use_fallback, float *r_advance, rctf *r_bounds)
Definition blf.cc:1188
void BKE_nurbList_duplicate(ListBaseT< Nurb > *lb1, const ListBaseT< Nurb > *lb2)
Definition curve.cc:697
VFontData * BKE_vfontdata_copy(const VFontData *vfont_src, int flag)
void BKE_vfontdata_metrics_get_defaults(VFontData_Metrics *metrics)
char * BLF_display_name_from_id(int fontid)
Definition blf.cc:1135
int builtin_font_size
Definition vfont.cc:63
int BLI_str_utf8_invalid_strip(char *str, size_t str_len) ATTR_NONNULL(1)
bool BLF_unload_id(int fontid)
Definition blf.cc:282
ListBaseT< Nurb > nurbsbase
VFontData_Metrics metrics
Map< uint, struct VChar * > * characters
struct PackedFile * temp_pf
struct VFontData * data