Blender V5.0
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_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
40
42{
43 int fontid = BLF_load_mem("FTVFont", static_cast<const uchar *>(pf->data), pf->size);
44 if (fontid == -1) {
45 return nullptr;
46 }
47
48 /* allocate blender font */
49 VFontData *vfd = MEM_callocN<VFontData>("FTVFontData");
50
51 /* Get the font name. */
52 char *name = BLF_display_name_from_id(fontid);
53 STRNCPY(vfd->name, name);
54 /* BLF_display_name result must be freed. */
56
58
60 fontid, &vfd->metrics.ascend_ratio, &vfd->metrics.em_ratio, &vfd->metrics.scale))
61 {
63 }
64
65 vfd->characters = BLI_ghash_int_new_ex(__func__, 255);
66
67 BLF_unload_id(fontid);
68
69 return vfd;
70}
71
72static void *vfontdata_copy_characters_value_cb(const void *src)
73{
74 return src ? BKE_vfontdata_char_copy(static_cast<const VChar *>(src)) : nullptr;
75}
76
77VFontData *BKE_vfontdata_copy(const VFontData *vfont_src, const int /*flag*/)
78{
79 VFontData *vfont_dst = static_cast<VFontData *>(MEM_dupallocN(vfont_src));
80
81 if (vfont_src->characters != nullptr) {
82 vfont_dst->characters = BLI_ghash_copy(
84 }
85
86 return vfont_dst;
87}
88
90{
91 if (!vfont) {
92 return nullptr;
93 }
94
95 int font_id = -1;
96 const bool is_builtin = BKE_vfont_is_builtin(vfont);
97
98 /* If the default font is selected (usually because nothing has been selected)
99 * then allow use of the fallback font stack when characters are not found in it.
100 * This allows a nice first experience, for testing, and allows the translation of the initial
101 * "Text" string. However, when a different specific font then do not use fallback,
102 * only allow characters that are included in that font.
103 * Do this for predictable control of what is shown when selecting specific fonts,
104 * also for consistent output when the UI fonts are changed or updated. */
105 const bool use_fallback = is_builtin;
106
107 if (is_builtin) {
108 font_id = BLF_load_mem(
109 vfont->data->name, static_cast<const uchar *>(builtin_font_data), builtin_font_size);
110 }
111 else if (vfont->temp_pf) {
112 font_id = BLF_load_mem(
113 vfont->data->name, static_cast<const uchar *>(vfont->temp_pf->data), vfont->temp_pf->size);
114 }
115
116 if (font_id == -1) {
117 return nullptr;
118 }
119
120 VChar *che = MEM_callocN<VChar>("objfnt_char");
121
122 /* need to set a size for embolden, etc. */
123 BLF_size(font_id, 16);
124
125 if (!BLF_character_to_curves(font_id,
126 character,
127 &che->nurbsbase,
128 vfont->data->metrics.scale,
129 use_fallback,
130 &che->width))
131 {
132 /* Free but add to the character cache to prevent future lookups
133 * from attempting to load the font again. */
134 MEM_freeN(che);
135 che = nullptr;
136 }
137
138 BLI_ghash_insert(vfont->data->characters, POINTER_FROM_UINT(character), che);
139 BLF_unload_id(font_id);
140 return che;
141}
142
144{
145 VChar *vchar_dst = static_cast<VChar *>(MEM_dupallocN(vchar_src));
146
147 BLI_listbase_clear(&vchar_dst->nurbsbase);
148 BKE_nurbList_duplicate(&vchar_dst->nurbsbase, &vchar_src->nurbsbase);
149
150 return vchar_dst;
151}
void BKE_nurbList_duplicate(ListBase *lb1, const ListBase *lb2)
Definition curve.cc:674
bool BKE_vfont_is_builtin(const VFont *vfont)
Definition vfont.cc:277
A structure to represent vector fonts, and to load them from PostScript fonts.
bool BLF_character_to_curves(int fontid, unsigned int unicode, ListBase *nurbsbase, const float scale, bool use_fallback, float *r_advance)
Definition blf.cc:1146
void BLF_size(int fontid, float size)
Definition blf.cc:443
#define BLF_VFONT_METRICS_EM_RATIO_DEFAULT
Definition BLF_api.hh:113
char * BLF_display_name_from_id(int fontid)
Definition blf.cc:1093
bool BLF_get_vfont_metrics(int fontid, float *ascend_ratio, float *em_ratio, float *scale)
Definition blf.cc:1103
bool BLF_unload_id(int fontid)
Definition blf.cc:280
int BLF_load_mem(const char *name, const unsigned char *mem, int mem_size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
#define BLF_VFONT_METRICS_SCALE_DEFAULT
Definition BLF_api.hh:112
#define BLF_VFONT_METRICS_ASCEND_RATIO_DEFAULT
Definition BLF_api.hh:114
GHash * BLI_ghash_copy(const GHash *gh, GHashKeyCopyFP keycopyfp, GHashValCopyFP valcopyfp) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc: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.cc:707
BLI_INLINE void BLI_listbase_clear(ListBase *lb)
char * STRNCPY(char(&dst)[N], const char *src)
Definition BLI_string.h:693
int BLI_str_utf8_invalid_strip(char *str, size_t str_len) ATTR_NONNULL(1)
unsigned char uchar
unsigned int uint
#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_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void * MEM_dupallocN(const void *vmemh)
Definition mallocn.cc:143
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
const char * name
const void * data
float width
ListBase nurbsbase
GHash * characters
VFontData_Metrics metrics
char name[128]
struct VFontData * data
struct PackedFile * temp_pf
int builtin_font_size
Definition vfont.cc:61
const void * builtin_font_data
Definition vfont.cc:60
VChar * BKE_vfontdata_char_from_freetypefont(VFont *vfont, uint character)
VFontData * BKE_vfontdata_copy(const VFontData *vfont_src, const int)
void BKE_vfontdata_metrics_get_defaults(VFontData_Metrics *metrics)
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)