Blender V4.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
15#include "MEM_guardedalloc.h"
16
17#include "BLF_api.hh"
18
19#include "BLI_ghash.h"
20#include "BLI_listbase.h"
21#include "BLI_string.h"
22#include "BLI_string_utf8.h"
23
24#include "BKE_curve.hh"
25#include "BKE_vfont.hh"
26#include "BKE_vfontdata.hh"
27
29#include "DNA_vfont_types.h"
30
31extern const void *builtin_font_data;
32extern int builtin_font_size;
33
35{
36 int fontid = BLF_load_mem("FTVFont", static_cast<const uchar *>(pf->data), pf->size);
37 if (fontid == -1) {
38 return nullptr;
39 }
40
41 /* allocate blender font */
42 VFontData *vfd = static_cast<VFontData *>(MEM_callocN(sizeof(*vfd), "FTVFontData"));
43
44 /* Get the font name. */
45 char *name = BLF_display_name_from_id(fontid);
46 STRNCPY(vfd->name, name);
47 /* BLF_display_name result must be freed. */
48 MEM_freeN(name);
49
51
52 BLF_get_vfont_metrics(fontid, &vfd->ascender, &vfd->em_height, &vfd->scale);
53
54 vfd->characters = BLI_ghash_int_new_ex(__func__, 255);
55
56 BLF_unload_id(fontid);
57
58 return vfd;
59}
60
61static void *vfontdata_copy_characters_value_cb(const void *src)
62{
63 return BKE_vfontdata_char_copy(static_cast<const VChar *>(src));
64}
65
66VFontData *BKE_vfontdata_copy(const VFontData *vfont_src, const int /*flag*/)
67{
68 VFontData *vfont_dst = static_cast<VFontData *>(MEM_dupallocN(vfont_src));
69
70 if (vfont_src->characters != nullptr) {
71 vfont_dst->characters = BLI_ghash_copy(
73 }
74
75 return vfont_dst;
76}
77
79{
80 if (!vfont) {
81 return nullptr;
82 }
83
84 int font_id = -1;
85
86 if (BKE_vfont_is_builtin(vfont)) {
87 font_id = BLF_load_mem(
88 vfont->data->name, static_cast<const uchar *>(builtin_font_data), builtin_font_size);
89 }
90 else if (vfont->temp_pf) {
91 font_id = BLF_load_mem(
92 vfont->data->name, static_cast<const uchar *>(vfont->temp_pf->data), vfont->temp_pf->size);
93 }
94
95 if (font_id == -1) {
96 /* This could happen for a saved file with an unpacked local font that was
97 * later removed. Load the default UI font so we can still show _something_. */
98 font_id = BLF_load_mem(
99 vfont->data->name, static_cast<const uchar *>(builtin_font_data), builtin_font_size);
100 }
101
102 VChar *che = (VChar *)MEM_callocN(sizeof(VChar), "objfnt_char");
103 che->index = character;
104
105 /* need to set a size for embolden, etc. */
106 BLF_size(font_id, 16);
107
108 che->width = BLF_character_to_curves(font_id, character, &che->nurbsbase, vfont->data->scale);
109
111 BLF_unload_id(font_id);
112 return che;
113}
114
116{
117 VChar *vchar_dst = static_cast<VChar *>(MEM_dupallocN(vchar_src));
118
119 BLI_listbase_clear(&vchar_dst->nurbsbase);
120 BKE_nurbList_duplicate(&vchar_dst->nurbsbase, &vchar_src->nurbsbase);
121
122 return vchar_dst;
123}
void BKE_nurbList_duplicate(ListBase *lb1, const ListBase *lb2)
Definition curve.cc:673
bool BKE_vfont_is_builtin(const VFont *vfont)
Definition vfont.cc:232
A structure to represent vector fonts, and to load them from PostScript fonts.
void BLF_size(int fontid, float size)
Definition blf.cc:426
char * BLF_display_name_from_id(int fontid)
Definition blf.cc:1009
bool BLF_get_vfont_metrics(int fontid, float *ascend_ratio, float *em_ratio, float *scale)
Definition blf.cc:1019
void BLF_unload_id(int fontid)
Definition blf.cc:284
float BLF_character_to_curves(int fontid, unsigned int unicode, ListBase *nurbsbase, const float scale)
Definition blf.cc:1062
int BLF_load_mem(const char *name, const unsigned char *mem, int mem_size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
GHash * BLI_ghash_copy(const GHash *gh, GHashKeyCopyFP keycopyfp, GHashValCopyFP valcopyfp) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.c:691
GHash * BLI_ghash_int_new_ex(const char *info, unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition BLI_ghash.c:707
BLI_INLINE void BLI_listbase_clear(struct ListBase *lb)
#define STRNCPY(dst, src)
Definition BLI_string.h:593
int BLI_str_utf8_invalid_strip(char *str, size_t length) ATTR_NONNULL(1)
unsigned char uchar
unsigned long ulong
#define ARRAY_SIZE(arr)
#define POINTER_FROM_UINT(i)
Read Guarded memory(de)allocation.
#define pf(_x, _i)
Prefetch 64.
Definition gim_memory.h:48
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
void *(* MEM_callocN)(size_t len, const char *str)
Definition mallocn.cc:42
void *(* MEM_dupallocN)(const void *vmemh)
Definition mallocn.cc:39
const void * data
float width
ListBase nurbsbase
unsigned int index
GHash * characters
char name[128]
float em_height
struct VFontData * data
struct PackedFile * temp_pf
int builtin_font_size
Definition vfont.cc:64
VFontData * BKE_vfontdata_copy(const VFontData *vfont_src, const int)
const void * builtin_font_data
Definition vfont.cc:63
VChar * BKE_vfontdata_char_from_freetypefont(VFont *vfont, ulong character)
VChar * BKE_vfontdata_char_copy(const VChar *vchar_src)
VFontData * BKE_vfontdata_from_freetypefont(PackedFile *pf)
static void * vfontdata_copy_characters_value_cb(const void *src)