Blender V5.3
vfont.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdio>
10#include <cstdlib>
11#include <cstring>
12#include <cwctype>
13#include <optional>
14
15#include "CLG_log.h"
16
17#include "MEM_guardedalloc.h"
18
19#include "BLI_ghash.hh"
20#include "BLI_listbase.hh"
21#include "BLI_path_utils.hh"
22#include "BLI_string.hh"
23#include "BLI_string_utf8.hh"
24#include "BLI_utildefines.hh"
25
26#include "BLT_translation.hh"
27
28#include "DNA_curve_types.h"
29#include "DNA_object_types.h"
31#include "DNA_vfont_types.h"
32
33#include "BKE_bpath.hh"
34#include "BKE_curve.hh"
35#include "BKE_global.hh"
36#include "BKE_idtype.hh"
37#include "BKE_lib_id.hh"
38#include "BKE_library.hh"
39#include "BKE_main.hh"
40#include "BKE_packedFile.hh"
41#include "BKE_vfont.hh"
42#include "BKE_vfontdata.hh"
43
44#include "BLO_read_write.hh"
45
46namespace blender {
47
48static CLG_LogRef LOG = {"geom.vfont"};
49
50/* -------------------------------------------------------------------- */
53
54static PackedFile *packedfile_new_from_builtin();
55
57
58/* -------------------------------------------------------------------- */
61
62const void *builtin_font_data = nullptr;
64
65static void vfont_init_data(ID *id)
66{
67 VFont *vfont = id_cast<VFont *>(id);
69
70 if (pf) {
71 VFontData *vfd;
72
74 if (vfd) {
75 vfont->data = vfd;
76
78 }
79
80 /* Free the packed file */
82 }
83}
84
85static void vfont_copy_data(Main * /*bmain*/,
86 std::optional<Library *> /*owner_library*/,
87 ID *id_dst,
88 const ID * /*id_src*/,
89 const int flag)
90{
91 VFont *vfont_dst = id_cast<VFont *>(id_dst);
92
93 /* We never handle user-count here for own data. */
94 const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT;
95
96 /* Just to be sure, should not have any value actually after reading time. */
97 vfont_dst->temp_pf = nullptr;
98
99 if (vfont_dst->packedfile) {
100 vfont_dst->packedfile = BKE_packedfile_duplicate(vfont_dst->packedfile);
101 }
102
103 if (vfont_dst->data) {
104 vfont_dst->data = BKE_vfontdata_copy(vfont_dst->data, flag_subdata);
105 }
106}
107
109static void vfont_free_data(ID *id)
110{
111 VFont *vfont = id_cast<VFont *>(id);
112 BKE_vfont_data_free(vfont);
113
114 if (vfont->packedfile) {
116 vfont->packedfile = nullptr;
117 }
118}
119
120static void vfont_foreach_path(ID *id, BPathForeachPathData *bpath_data)
121{
122 VFont *vfont = id_cast<VFont *>(id);
123
124 if ((vfont->packedfile != nullptr) &&
125 (bpath_data->flag & BKE_BPATH_FOREACH_PATH_SKIP_PACKED) != 0)
126 {
127 return;
128 }
129
130 if (BKE_vfont_is_builtin(vfont)) {
131 return;
132 }
133
134 BKE_bpath_foreach_path_fixed_process(bpath_data, vfont->filepath, sizeof(vfont->filepath));
135}
136
137static void vfont_blend_write(BlendWriter *writer, ID *id, const void *id_address)
138{
139 VFont *vf = id_cast<VFont *>(id);
140 const bool is_undo = writer->is_undo();
141
142 /* Clean up, important in undo case to reduce false detection of changed datablocks. */
143 vf->data = nullptr;
144 vf->temp_pf = nullptr;
145
146 /* Do not store packed files in case this is a library override ID. */
147 if (ID_IS_OVERRIDE_LIBRARY(vf) && !is_undo) {
148 vf->packedfile = nullptr;
149 }
150
151 /* write LibData */
152 writer->write_id_struct(id_address, vf);
153 BKE_id_blend_write(writer, &vf->id);
154
155 /* direct data */
157}
158
159static void vfont_blend_read_data(BlendDataReader *reader, ID *id)
160{
161 VFont *vf = id_cast<VFont *>(id);
162 vf->data = nullptr;
163 vf->temp_pf = nullptr;
165}
166
168 .id_code = VFont::id_type,
169 .id_filter = FILTER_ID_VF,
170 .dependencies_id_types = 0,
171 .main_listbase_index = INDEX_ID_VF,
172 .struct_size = sizeof(VFont),
173 .name = "Font",
174 .name_plural = N_("fonts"),
175 .translation_context = BLT_I18NCONTEXT_ID_VFONT,
177 .asset_type_info = nullptr,
178
179 .init_data = vfont_init_data,
180 .copy_data = vfont_copy_data,
181 .free_data = vfont_free_data,
182 .make_local = nullptr,
183 .foreach_id = nullptr,
184 .foreach_cache = nullptr,
185 .foreach_path = vfont_foreach_path,
186 .foreach_working_space_color = nullptr,
187 .foreach_asset_weak_reference = nullptr,
188 .owner_pointer_get = nullptr,
189
190 .blend_write = vfont_blend_write,
191 .blend_read_data = vfont_blend_read_data,
192 .blend_read_after_liblink = nullptr,
193
194 .blend_read_undo_preserve = nullptr,
195
196 .lib_override_apply_post = nullptr,
197};
198
200
201/* -------------------------------------------------------------------- */
204
206{
207 PackedFile *pf;
208 if (BKE_vfont_is_builtin(vfont)) {
210 }
211 else {
212 if (vfont->packedfile) {
213 pf = vfont->packedfile;
214
215 /* We need to copy a temporary font to memory unless it is already there. */
216 if (vfont->temp_pf == nullptr) {
218 }
219 }
220 else {
221 pf = BKE_packedfile_new(nullptr, vfont->filepath, ID_BLEND_PATH_FROM_GLOBAL(&vfont->id));
222
223 if (vfont->temp_pf == nullptr) {
225 nullptr, vfont->filepath, ID_BLEND_PATH_FROM_GLOBAL(&vfont->id));
226 }
227 }
228 if (!pf) {
229 CLOG_WARN(&LOG, "Font file doesn't exist: %s", vfont->filepath);
230
231 /* NOTE(@ideasman42): Don't attempt to find a fallback.
232 * If the font requested by the user doesn't load, font rendering will display
233 * placeholder characters instead. */
234 }
235 }
236
237 if (pf) {
239 if (pf != vfont->packedfile) {
241 }
242 }
243}
244
246{
247 if (vfont->data) {
248 if (vfont->data->characters) {
249 for (VChar *che : vfont->data->characters->values()) {
250 if (che == nullptr) {
251 continue;
252 }
253
254 while (che->nurbsbase.first()) {
255 Nurb *nu = che->nurbsbase.first();
256 if (nu->bezt) {
257 MEM_delete(nu->bezt);
258 }
259 BLI_freelinkN(&che->nurbsbase, nu);
260 }
261
262 MEM_delete(che);
263 }
264
265 MEM_delete(vfont->data->characters);
266 }
267
268 MEM_delete(vfont->data);
269 vfont->data = nullptr;
270 }
271
272 if (vfont->temp_pf) {
273 BKE_packedfile_free(vfont->temp_pf); /* Null when the font file can't be found on disk. */
274 vfont->temp_pf = nullptr;
275 }
276}
277
278bool BKE_vfont_is_builtin(const VFont *vfont)
279{
280 return STREQ(vfont->filepath, FO_BUILTIN_NAME);
281}
282
283void BKE_vfont_builtin_register(const void *mem, int size)
284{
285 builtin_font_data = mem;
286 builtin_font_size = size;
287}
288
290{
291 if (!builtin_font_data) {
292 CLOG_ERROR(&LOG, "Internal error, builtin font not loaded");
293
294 return nullptr;
295 }
296
297 void *mem = MEM_new_uninitialized(builtin_font_size, "vfd_builtin");
298
300
302}
303
304VFont *BKE_vfont_load(Main *bmain, const char *filepath)
305{
307 VFont *vfont = nullptr;
308 PackedFile *pf;
309 bool is_builtin;
310
311 if (STREQ(filepath, FO_BUILTIN_NAME)) {
312 STRNCPY(filename, filepath);
313
315 is_builtin = true;
316 }
317 else {
318 BLI_path_split_file_part(filepath, filename, sizeof(filename));
319 pf = BKE_packedfile_new(nullptr, filepath, BKE_main_blendfile_path(bmain));
320
321 is_builtin = false;
322 }
323
324 if (pf) {
325 VFontData *vfd;
326
328 if (vfd) {
329 /* If there's a font name, use it for the ID name. */
330 vfont = static_cast<VFont *>(
331 BKE_libblock_alloc(bmain, ID_VF, vfd->name[0] ? vfd->name : filename, 0));
332 vfont->data = vfd;
333 STRNCPY(vfont->filepath, filepath);
334
335 /* if auto-pack is on store the packed-file in de font structure */
336 if (!is_builtin && (G.fileflags & G_FILE_AUTOPACK)) {
337 vfont->packedfile = pf;
338 }
339
340 /* Do not add #FO_BUILTIN_NAME to temporary list-base. */
342 vfont->temp_pf = BKE_packedfile_new(nullptr, filepath, BKE_main_blendfile_path(bmain));
343 }
344 }
345
346 /* Free the packed file */
347 if (!vfont || vfont->packedfile != pf) {
349 }
350 }
351
352 return vfont;
353}
354
355VFont *BKE_vfont_load_exists_ex(Main *bmain, const char *filepath, bool *r_exists)
356{
357 char filepath_abs[FILE_MAX], filepath_test[FILE_MAX];
358
359 STRNCPY(filepath_abs, filepath);
360 BLI_path_abs(filepath_abs, BKE_main_blendfile_path(bmain));
361
362 /* first search an identical filepath */
363 for (VFont &vfont : bmain->fonts) {
364 STRNCPY(filepath_test, vfont.filepath);
365 BLI_path_abs(filepath_test, ID_BLEND_PATH(bmain, &vfont.id));
366
367 if (BLI_path_cmp(filepath_test, filepath_abs) == 0) {
368 id_us_plus(&vfont.id); /* officially should not, it doesn't link here! */
369 if (r_exists) {
370 *r_exists = true;
371 }
372 return &vfont;
373 }
374 }
375
376 if (r_exists) {
377 *r_exists = false;
378 }
379 return BKE_vfont_load(bmain, filepath);
380}
381
382VFont *BKE_vfont_load_exists(Main *bmain, const char *filepath)
383{
384 return BKE_vfont_load_exists_ex(bmain, filepath, nullptr);
385}
386
388{
389 for (VFont &vfont : G_MAIN->fonts) {
390 if (BKE_vfont_is_builtin(&vfont)) {
391 return &vfont;
392 }
393 }
394
395 /* Newly loaded ID's have a user by default, in this case the caller is responsible
396 * for assigning a user, otherwise an additional user would be added, see: #100819. */
398 id_us_min(&vfont->id);
399 BLI_assert(vfont->id.us == 0);
400 return vfont;
401}
402
403void BKE_vfont_packfile_ensure(Main *bmain, VFont *vfont, ReportList *reports)
404{
405 if (vfont->packedfile != nullptr) {
406 /* Font is already packed and considered unmodified, do not attempt to repack it, since its
407 * original file may not be available anymore on the current FS.
408 *
409 * See #152638.
410 */
411 return;
412 }
413
415 reports, vfont->filepath, ID_BLEND_PATH(bmain, &vfont->id));
416}
417
419
420/* -------------------------------------------------------------------- */
423
424int BKE_vfont_select_get(const Curve *cu, int *r_start, int *r_end)
425{
426 EditFont *ef = cu->editfont;
427 int start, end, direction;
428
429 if (ef == nullptr || (cu->ob_type != OB_FONT)) {
430 return 0;
431 }
432
433 BLI_assert(ef->len >= 0);
434 BLI_assert(ef->selstart >= 0 && ef->selstart <= ef->len + 1);
435 BLI_assert(ef->selend >= 0 && ef->selend <= ef->len + 1);
436 BLI_assert(ef->pos >= 0 && ef->pos <= ef->len);
437
438 if (ef->selstart == 0) {
439 return 0;
440 }
441
442 if (ef->selstart <= ef->selend) {
443 start = ef->selstart - 1;
444 end = ef->selend - 1;
445 direction = 1;
446 }
447 else {
448 start = ef->selend;
449 end = ef->selstart - 2;
450 direction = -1;
451 }
452
453 if (start == end + 1) {
454 return 0;
455 }
456
457 BLI_assert(start < end + 1);
458 *r_start = start;
459 *r_end = end;
460 return direction;
461}
462
464{
465 EditFont *ef = cu->editfont;
466
467 BLI_assert((cu->ob_type == OB_FONT) && ef);
468
469 CLAMP_MAX(ef->pos, ef->len);
470 CLAMP_MAX(ef->selstart, ef->len + 1);
471 CLAMP_MAX(ef->selend, ef->len);
472}
473
475
476/* -------------------------------------------------------------------- */
479
480static struct {
481 char32_t *text_buffer;
483 size_t len_utf32;
484 size_t len_utf8;
485} g_vfont_clipboard = {nullptr};
486
488{
489 MEM_SAFE_DELETE(g_vfont_clipboard.text_buffer);
490 MEM_SAFE_DELETE(g_vfont_clipboard.info_buffer);
491 g_vfont_clipboard.len_utf32 = 0;
492 g_vfont_clipboard.len_utf8 = 0;
493}
494
495void BKE_vfont_clipboard_set(const char32_t *text_buf, const CharInfo *info_buf, const size_t len)
496{
497 char32_t *text;
498 CharInfo *info;
499
500 /* Clean previous buffers. */
502
503 text = MEM_new_array_uninitialized<char32_t>((len + 1), __func__);
504 if (text == nullptr) {
505 return;
506 }
507
508 info = MEM_new_array<CharInfo>(len, __func__);
509 if (info == nullptr) {
510 MEM_delete(text);
511 return;
512 }
513
514 memcpy(text, text_buf, len * sizeof(*text));
515 text[len] = '\0';
516 memcpy(info, info_buf, len * sizeof(CharInfo));
517
518 /* store new buffers */
519 g_vfont_clipboard.text_buffer = text;
520 g_vfont_clipboard.info_buffer = info;
522 g_vfont_clipboard.len_utf32 = len;
523}
524
525void BKE_vfont_clipboard_get(char32_t **r_text_buf,
526 CharInfo **r_info_buf,
527 size_t *r_len_utf8,
528 size_t *r_len_utf32)
529{
530 if (r_text_buf) {
531 *r_text_buf = g_vfont_clipboard.text_buffer;
532 }
533
534 if (r_info_buf) {
535 *r_info_buf = g_vfont_clipboard.info_buffer;
536 }
537
538 if (r_len_utf32) {
539 *r_len_utf32 = g_vfont_clipboard.len_utf32;
540 }
541
542 if (r_len_utf8) {
543 *r_len_utf8 = g_vfont_clipboard.len_utf8;
544 }
545}
546
548
549} // namespace blender
#define G_MAIN
A structure to represent vector fonts, and to load them from PostScript fonts.
#define FILE_MAXFILE
#define FILE_MAX
#define BLI_path_cmp
#define BLT_I18NCONTEXT_ID_VFONT
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define FILTER_ID_VF
Definition DNA_ID.h:1224
#define ID_BLEND_PATH_FROM_GLOBAL(_id)
Definition DNA_ID.h:691
#define ID_BLEND_PATH(_bmain, _id)
Definition DNA_ID.h:688
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
Object is a sort of wrapper for general info.
#define FO_BUILTIN_NAME
Read Blender memory allocation and freeing.
#define CLAMP_MAX(a, c)
#define STREQ(a, b)
#define pf(_x, _i)
Prefetch 64.
Definition gim_memory.h:48
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
std::string filename(const std::string_view &str, size_t pos)
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_packedfile_blend_write(BlendWriter *writer, const PackedFile *pf)
static void vfont_free_data(ID *id)
Definition vfont.cc:109
static void vfont_blend_read_data(BlendDataReader *reader, ID *id)
Definition vfont.cc:159
char * STRNCPY(char(&dst)[N], const char *src)
VFont * BKE_vfont_load_exists_ex(Main *bmain, const char *filepath, bool *r_exists)
Definition vfont.cc:355
PackedFile * BKE_packedfile_duplicate(const PackedFile *pf_src)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
bool BKE_vfont_is_builtin(const VFont *vfont)
Definition vfont.cc:278
void id_us_plus(ID *id)
Definition lib_id.cc:433
void BKE_vfont_packfile_ensure(Main *bmain, VFont *vfont, ReportList *reports)
Definition vfont.cc:403
void BKE_vfont_clipboard_set(const char32_t *text_buf, const CharInfo *info_buf, size_t len)
Definition vfont.cc:495
void BKE_packedfile_free(PackedFile *pf)
PackedFile * BKE_packedfile_new(ReportList *reports, const char *filepath_rel, const char *basepath)
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2890
static struct blender::@161346021153211343265151217245342146117057105015 g_vfont_clipboard
@ LIB_ID_CREATE_NO_USER_REFCOUNT
void BKE_vfont_select_clamp(Curve *cu)
Definition vfont.cc:463
Global G
Definition blender.cc:57
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:1058
static void vfont_blend_write(BlendWriter *writer, ID *id, const void *id_address)
Definition vfont.cc:137
@ G_FILE_AUTOPACK
static PackedFile * packedfile_new_from_builtin()
Definition vfont.cc:289
VFontData * BKE_vfontdata_from_freetypefont(PackedFile *pf)
const void * builtin_font_data
Definition vfont.cc:62
static void vfont_init_data(ID *id)
Definition vfont.cc:65
void BKE_vfont_clipboard_get(char32_t **r_text_buf, CharInfo **r_info_buf, size_t *r_len_utf8, size_t *r_len_utf32)
Definition vfont.cc:525
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
void BKE_vfont_data_ensure(VFont *vfont)
Definition vfont.cc:205
int BKE_vfont_select_get(const Curve *cu, int *r_start, int *r_end)
Definition vfont.cc:424
VFont * BKE_vfont_load_exists(Main *bmain, const char *filepath)
Definition vfont.cc:382
static void vfont_copy_data(Main *, std::optional< Library * >, ID *id_dst, const ID *, const int flag)
Definition vfont.cc:85
@ BKE_BPATH_FOREACH_PATH_SKIP_PACKED
Definition BKE_bpath.hh:49
void void void BLI_path_split_file_part(const char *filepath, char *file, size_t file_maxncpy) ATTR_NONNULL(1
bool BKE_bpath_foreach_path_fixed_process(BPathForeachPathData *bpath_data, char *path, size_t path_maxncpy)
Definition bpath.cc:130
void BKE_vfont_builtin_register(const void *mem, int size)
Definition vfont.cc:283
@ INDEX_ID_VF
Definition DNA_ID.h:1302
IDTypeInfo IDType_ID_VF
Definition vfont.cc:167
void BKE_vfont_clipboard_free()
Definition vfont.cc:487
VFontData * BKE_vfontdata_copy(const VFontData *vfont_src, int flag)
VFont * BKE_vfont_builtin_ensure()
Definition vfont.cc:387
VFont * BKE_vfont_load(Main *bmain, const char *filepath)
Definition vfont.cc:304
static CLG_LogRef LOG
Definition logging.cpp:12
void BKE_vfont_data_free(VFont *vfont)
Definition vfont.cc:245
void BKE_packedfile_blend_read(BlendDataReader *reader, PackedFile **pf_p, StringRefNull filepath)
static void vfont_foreach_path(ID *id, BPathForeachPathData *bpath_data)
Definition vfont.cc:120
size_t BLI_str_utf32_as_utf8_len(const char32_t *src) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
PackedFile * BKE_packedfile_new_from_memory(const void *mem, int memlen, const ImplicitSharingInfo *sharing_info=nullptr)
void id_us_min(ID *id)
Definition lib_id.cc:441
int builtin_font_size
Definition vfont.cc:63
void * BKE_libblock_alloc(Main *bmain, short type, const char *name, int flag) ATTR_WARN_UNUSED_RESULT
Definition lib_id.cc:1545
@ IDTYPE_FLAGS_NO_ANIMDATA
Definition BKE_idtype.hh:52
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:50
void write_id_struct(const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
bool is_undo() const
struct EditFont * editfont
ListBaseT< VFont > fonts
Definition BKE_main.hh:401
BezTriple * bezt
Map< uint, struct VChar * > * characters
struct PackedFile * packedfile
char filepath[1024]
struct PackedFile * temp_pf
struct VFontData * data
#define N_(msgid)