Cleanup: improve naming consistency & use doxy groups for vfont* sources

This commit is contained in:
Campbell Barton
2024-12-14 15:35:29 +11:00
parent c1e275b334
commit ff2805fd79
7 changed files with 122 additions and 108 deletions

View File

@@ -77,24 +77,64 @@ enum eEditFontMode {
FO_SELCHANGE = 10,
};
/* BKE_vfont_to_curve will move the cursor in these cases */
/** #BKE_vfont_to_curve will move the cursor in these cases. */
#define FO_CURS_IS_MOTION(mode) (ELEM(mode, FO_CURSUP, FO_CURSDOWN, FO_PAGEUP, FO_PAGEDOWN))
/* -------------------------------------------------------------------- */
/** \name VFont API
*
* See `vfont.c`.
* \{ */
bool BKE_vfont_is_builtin(const VFont *vfont);
void BKE_vfont_builtin_register(const void *mem, int size);
void BKE_vfont_data_ensure(VFont *vfont);
void BKE_vfont_free_data(VFont *vfont);
/**
* Return the built-in #VFont, without adding a user (the user-count may be zero).
* The caller is responsible for adding a user.
*/
VFont *BKE_vfont_builtin_get();
VFont *BKE_vfont_builtin_ensure();
void BKE_vfont_data_ensure(VFont *vfont);
void BKE_vfont_data_free(VFont *vfont);
VFont *BKE_vfont_load(Main *bmain, const char *filepath);
VFont *BKE_vfont_load_exists_ex(Main *bmain, const char *filepath, bool *r_exists);
VFont *BKE_vfont_load_exists(Main *bmain, const char *filepath);
int BKE_vfont_select_get(Object *ob, int *r_start, int *r_end);
void BKE_vfont_select_clamp(Object *ob);
void BKE_vfont_clipboard_free();
void BKE_vfont_clipboard_set(const char32_t *text_buf, const CharInfo *info_buf, size_t len);
void BKE_vfont_clipboard_get(char32_t **r_text_buf,
CharInfo **r_info_buf,
size_t *r_len_utf8,
size_t *r_len_utf32);
/** \} */
/* -------------------------------------------------------------------- */
/** \name VFont Curve & Text Layout API
*
* See `vfont_curve.c`.
* \{ */
int BKE_vfont_cursor_to_text_index(Object *ob, const float cursor_location[2]);
/**
* \warning Expects to have access to evaluated data (i.e. passed object should be evaluated one).
*/
bool BKE_vfont_to_curve(Object *ob, eEditFontMode mode);
void BKE_vfont_char_build(Curve *cu,
ListBase *nubase,
unsigned int character,
const CharInfo *info,
float ofsx,
float ofsy,
float rot,
int charidx,
float fsize);
bool BKE_vfont_to_curve_ex(Object *ob,
Curve *cu,
eEditFontMode mode,
@@ -105,28 +145,4 @@ bool BKE_vfont_to_curve_ex(Object *ob,
CharTrans **r_chartransdata);
bool BKE_vfont_to_curve_nubase(Object *ob, eEditFontMode mode, ListBase *r_nubase);
int BKE_vfont_cursor_to_text_index(Object *ob, const float cursor_location[2]);
/**
* \warning Expects to have access to evaluated data (i.e. passed object should be evaluated one).
*/
bool BKE_vfont_to_curve(Object *ob, eEditFontMode mode);
void BKE_vfont_build_char(Curve *cu,
ListBase *nubase,
unsigned int character,
const CharInfo *info,
float ofsx,
float ofsy,
float rot,
int charidx,
float fsize);
int BKE_vfont_select_get(Object *ob, int *r_start, int *r_end);
void BKE_vfont_select_clamp(Object *ob);
void BKE_vfont_clipboard_free();
void BKE_vfont_clipboard_set(const char32_t *text_buf, const CharInfo *info_buf, size_t len);
void BKE_vfont_clipboard_get(char32_t **r_text_buf,
CharInfo **r_info_buf,
size_t *r_len_utf8,
size_t *r_len_utf32);
/** \} */

View File

@@ -352,7 +352,7 @@ void BKE_curve_init(Curve *cu, const short curve_type)
if (cu->type == OB_FONT) {
cu->flag |= CU_FRONT | CU_BACK;
cu->vfont = cu->vfontb = cu->vfonti = cu->vfontbi = BKE_vfont_builtin_get();
cu->vfont = cu->vfontb = cu->vfonti = cu->vfontbi = BKE_vfont_builtin_ensure();
cu->vfont->id.us += 4;
const char *str = DATA_("Text");

View File

@@ -49,7 +49,7 @@ static CLG_LogRef LOG = {"bke.vfont"};
/** \name Prototypes
* \{ */
static PackedFile *get_builtin_packedfile(void);
static PackedFile *packedfile_new_from_builtin(void);
/** \} */
@@ -63,7 +63,7 @@ int builtin_font_size = 0;
static void vfont_init_data(ID *id)
{
VFont *vfont = (VFont *)id;
PackedFile *pf = get_builtin_packedfile();
PackedFile *pf = packedfile_new_from_builtin();
if (pf) {
VFontData *vfd;
@@ -107,7 +107,7 @@ static void vfont_copy_data(Main * /*bmain*/,
static void vfont_free_data(ID *id)
{
VFont *vfont = (VFont *)id;
BKE_vfont_free_data(vfont);
BKE_vfont_data_free(vfont);
if (vfont->packedfile) {
BKE_packedfile_free(vfont->packedfile);
@@ -198,7 +198,47 @@ IDTypeInfo IDType_ID_VF = {
/** \name VFont
* \{ */
void BKE_vfont_free_data(VFont *vfont)
void BKE_vfont_data_ensure(VFont *vfont)
{
PackedFile *pf;
if (BKE_vfont_is_builtin(vfont)) {
pf = packedfile_new_from_builtin();
}
else {
if (vfont->packedfile) {
pf = vfont->packedfile;
/* We need to copy a tmp font to memory unless it is already there */
if (vfont->temp_pf == nullptr) {
vfont->temp_pf = BKE_packedfile_duplicate(pf);
}
}
else {
pf = BKE_packedfile_new(nullptr, vfont->filepath, ID_BLEND_PATH_FROM_GLOBAL(&vfont->id));
if (vfont->temp_pf == nullptr) {
vfont->temp_pf = BKE_packedfile_new(
nullptr, vfont->filepath, ID_BLEND_PATH_FROM_GLOBAL(&vfont->id));
}
}
if (!pf) {
CLOG_WARN(&LOG, "Font file doesn't exist: %s", vfont->filepath);
/* NOTE(@ideasman42): Don't attempt to find a fallback.
* If the font requested by the user doesn't load, font rendering will display
* placeholder characters instead. */
}
}
if (pf) {
vfont->data = BKE_vfontdata_from_freetypefont(pf);
if (pf != vfont->packedfile) {
BKE_packedfile_free(pf);
}
}
}
void BKE_vfont_data_free(VFont *vfont)
{
if (vfont->data) {
if (vfont->data->characters) {
@@ -241,7 +281,7 @@ void BKE_vfont_builtin_register(const void *mem, int size)
builtin_font_size = size;
}
static PackedFile *get_builtin_packedfile()
static PackedFile *packedfile_new_from_builtin()
{
if (!builtin_font_data) {
CLOG_ERROR(&LOG, "Internal error, builtin font not loaded");
@@ -256,46 +296,6 @@ static PackedFile *get_builtin_packedfile()
return BKE_packedfile_new_from_memory(mem, builtin_font_size);
}
void BKE_vfont_data_ensure(VFont *vfont)
{
PackedFile *pf;
if (BKE_vfont_is_builtin(vfont)) {
pf = get_builtin_packedfile();
}
else {
if (vfont->packedfile) {
pf = vfont->packedfile;
/* We need to copy a tmp font to memory unless it is already there */
if (vfont->temp_pf == nullptr) {
vfont->temp_pf = BKE_packedfile_duplicate(pf);
}
}
else {
pf = BKE_packedfile_new(nullptr, vfont->filepath, ID_BLEND_PATH_FROM_GLOBAL(&vfont->id));
if (vfont->temp_pf == nullptr) {
vfont->temp_pf = BKE_packedfile_new(
nullptr, vfont->filepath, ID_BLEND_PATH_FROM_GLOBAL(&vfont->id));
}
}
if (!pf) {
CLOG_WARN(&LOG, "Font file doesn't exist: %s", vfont->filepath);
/* NOTE(@ideasman42): Don't attempt to find a fallback.
* If the font requested by the user doesn't load, font rendering will display
* placeholder characters instead. */
}
}
if (pf) {
vfont->data = BKE_vfontdata_from_freetypefont(pf);
if (pf != vfont->packedfile) {
BKE_packedfile_free(pf);
}
}
}
VFont *BKE_vfont_load(Main *bmain, const char *filepath)
{
char filename[FILE_MAXFILE];
@@ -306,7 +306,7 @@ VFont *BKE_vfont_load(Main *bmain, const char *filepath)
if (STREQ(filepath, FO_BUILTIN_NAME)) {
STRNCPY(filename, filepath);
pf = get_builtin_packedfile();
pf = packedfile_new_from_builtin();
is_builtin = true;
}
else {
@@ -379,7 +379,7 @@ VFont *BKE_vfont_load_exists(Main *bmain, const char *filepath)
return BKE_vfont_load_exists_ex(bmain, filepath, nullptr);
}
VFont *BKE_vfont_builtin_get()
VFont *BKE_vfont_builtin_ensure()
{
LISTBASE_FOREACH (VFont *, vfont, &G_MAIN->fonts) {
if (BKE_vfont_is_builtin(vfont)) {

View File

@@ -53,7 +53,7 @@ static float vfont_metrics_descent(const VFontData_Metrics *metrics)
return metrics->em_ratio - vfont_metrics_ascent(metrics);
}
static VFont *which_vfont(const Curve *cu, const CharInfo *info)
static VFont *vfont_from_charinfo(const Curve *cu, const CharInfo *info)
{
switch (info->flag & (CU_CHINFO_BOLD | CU_CHINFO_ITALIC)) {
case CU_CHINFO_BOLD:
@@ -96,7 +96,7 @@ static VFontData *vfont_data_ensure_with_lock(VFont *vfont)
return vfont->data;
}
static VChar *find_vfont_char(const VFontData *vfd, uint character)
static VChar *vfont_char_find(const VFontData *vfd, uint character)
{
return static_cast<VChar *>(BLI_ghash_lookup(vfd->characters, POINTER_FROM_UINT(character)));
}
@@ -210,13 +210,11 @@ static VChar *vfont_placeholder_ensure(VCharPlaceHolder &che_placeholder, uint c
return &che_placeholder.data.che[che_index];
}
static VChar *find_vfont_char_or_placeholder(const VFontData *vfd,
static VChar *vfont_char_find_or_placeholder(const VFontData *vfd,
uint character,
VCharPlaceHolder &che_placeholder)
{
VChar *che = vfd ? static_cast<VChar *>(
BLI_ghash_lookup(vfd->characters, POINTER_FROM_UINT(character))) :
nullptr;
VChar *che = vfd ? vfont_char_find(vfd, character) : nullptr;
if (UNLIKELY(che == nullptr)) {
che = vfont_placeholder_ensure(che_placeholder, character);
}
@@ -293,7 +291,7 @@ static void build_underline(Curve *cu,
mul_v2_fl(bp[3].vec, font_size);
}
static void vfont_build_char_impl(Curve *cu,
static void vfont_char_build_impl(Curve *cu,
ListBase *nubase,
const VChar *che,
const CharInfo *info,
@@ -409,7 +407,7 @@ static void vfont_build_char_impl(Curve *cu,
}
}
void BKE_vfont_build_char(Curve *cu,
void BKE_vfont_char_build(Curve *cu,
ListBase *nubase,
uint character,
const CharInfo *info,
@@ -419,15 +417,15 @@ void BKE_vfont_build_char(Curve *cu,
int charidx,
const float fsize)
{
VFontData *vfd = vfont_data_ensure_with_lock(which_vfont(cu, info));
VFontData *vfd = vfont_data_ensure_with_lock(vfont_from_charinfo(cu, info));
if (!vfd) {
return;
}
VChar *che = find_vfont_char(vfd, character);
vfont_build_char_impl(cu, nubase, che, info, ofsx, ofsy, rot, charidx, fsize);
VChar *che = vfont_char_find(vfd, character);
vfont_char_build_impl(cu, nubase, che, info, ofsx, ofsy, rot, charidx, fsize);
}
static float char_width(Curve *cu, VChar *che, const CharInfo *info)
static float vfont_char_width(Curve *cu, VChar *che, const CharInfo *info)
{
/* The character wasn't found, probably ascii = 0, then the width shall be 0 as well */
if (che == nullptr) {
@@ -534,7 +532,7 @@ static void vfont_info_context_update(VFontInfoContext *vfinfo_ctx,
const Curve *cu,
const CharInfo *info)
{
VFont *vfont = which_vfont(cu, info);
VFont *vfont = vfont_from_charinfo(cu, info);
if (vfinfo_ctx->vfont != vfont) {
vfinfo_ctx->vfont = vfont;
vfinfo_ctx->vfd = vfont_data_ensure_with_lock(vfont);
@@ -760,7 +758,7 @@ static bool vfont_to_curve(Object *ob,
if (!ELEM(ascii, '\n', '\0')) {
if (vfinfo_ctx.vfd) {
BLI_rw_mutex_lock(&vfont_rwlock, THREAD_LOCK_READ);
che = find_vfont_char(vfinfo_ctx.vfd, ascii);
che = vfont_char_find(vfinfo_ctx.vfd, ascii);
BLI_rw_mutex_unlock(&vfont_rwlock);
/* The character wasn't in the current curve base so load it. */
@@ -771,7 +769,7 @@ static bool vfont_to_curve(Object *ob,
*
* Such a check should not be a bottleneck since it wouldn't
* happen often once all the chars are load. */
if ((che = find_vfont_char(vfinfo_ctx.vfd, ascii)) == nullptr) {
if ((che = vfont_char_find(vfinfo_ctx.vfd, ascii)) == nullptr) {
che = BKE_vfontdata_char_from_freetypefont(vfinfo_ctx.vfont, ascii);
}
BLI_rw_mutex_unlock(&vfont_rwlock);
@@ -788,7 +786,7 @@ static bool vfont_to_curve(Object *ob,
che = nullptr;
}
twidth = char_width(cu, che, info);
twidth = vfont_char_width(cu, che, info);
/* Calculate positions. */
@@ -942,7 +940,7 @@ static bool vfont_to_curve(Object *ob,
}
/* Set the width of the character. */
twidth = char_width(cu, che, info);
twidth = vfont_char_width(cu, che, info);
xof += (twidth * wsfac * (1.0f + (info->kern / 40.0f))) + xtrax;
@@ -1262,9 +1260,9 @@ static bool vfont_to_curve(Object *ob,
}
vfont_info_context_update(&vfinfo_ctx, cu, info);
che = find_vfont_char_or_placeholder(vfinfo_ctx.vfd, ascii, che_placeholder);
che = vfont_char_find_or_placeholder(vfinfo_ctx.vfd, ascii, che_placeholder);
twidth = char_width(cu, che, info);
twidth = vfont_char_width(cu, che, info);
dtime = distfac * 0.5f * twidth;
@@ -1463,8 +1461,8 @@ static bool vfont_to_curve(Object *ob,
vfont_info_context_update(&vfinfo_ctx, cu, info);
/* Find the character, the characters has to be in the memory already
* since character checking has been done earlier already. */
che = find_vfont_char_or_placeholder(vfinfo_ctx.vfd, cha, che_placeholder);
vfont_build_char_impl(cu, r_nubase, che, info, ct->xof, ct->yof, ct->rot, i, font_size);
che = vfont_char_find_or_placeholder(vfinfo_ctx.vfd, cha, che_placeholder);
vfont_char_build_impl(cu, r_nubase, che, info, ct->xof, ct->yof, ct->rot, i, font_size);
if (info->flag & CU_CHINFO_UNDERLINE) {
float ulwidth, uloverlap = 0.0f;
@@ -1477,7 +1475,7 @@ static bool vfont_to_curve(Object *ob,
uloverlap = xtrax + 0.1f;
}
twidth = char_width(cu, che, info);
twidth = vfont_char_width(cu, che, info);
ulwidth = (twidth * (1.0f + (info->kern / 40.0f))) + uloverlap;
rect.xmin = ct->xof;
@@ -1676,9 +1674,9 @@ static bool vfont_to_curve(Object *ob,
ascii = info->flag & CU_CHINFO_SMALLCAPS_CHECK ? towupper(mem[i]) : mem[i];
vfont_info_context_update(&vfinfo_ctx, cu, info);
che = find_vfont_char_or_placeholder(vfinfo_ctx.vfd, ascii, che_placeholder);
che = vfont_char_find_or_placeholder(vfinfo_ctx.vfd, ascii, che_placeholder);
const float charwidth = char_width(cu, che, info);
const float charwidth = vfont_char_width(cu, che, info);
const float charhalf = (charwidth / 2.0f);
if (cursor_location[0] <= ((chartransdata[i].xof + charhalf) * font_size)) {
break;

View File

@@ -821,7 +821,7 @@ static void txt_add_object(bContext *C,
add_v3_v3(obedit->loc, offset);
cu = static_cast<Curve *>(obedit->data);
cu->vfont = BKE_vfont_builtin_get();
cu->vfont = BKE_vfont_builtin_ensure();
id_us_plus(&cu->vfont->id);
for (tmp = firstline, a = 0; nbytes < MAXTEXT && a < totline; tmp = tmp->next, a++) {
@@ -2503,7 +2503,7 @@ static int font_unlink_exec(bContext *C, wmOperator *op)
return OPERATOR_CANCELLED;
}
builtin_font = BKE_vfont_builtin_get();
builtin_font = BKE_vfont_builtin_ensure();
PointerRNA idptr = RNA_id_pointer_create(&builtin_font->id);
RNA_property_pointer_set(&pprop.ptr, pprop.prop, idptr, nullptr);

View File

@@ -38,7 +38,7 @@ static int rna_VectorFont_filepath_editable(const PointerRNA *ptr, const char **
static void rna_VectorFont_reload_update(Main * /*bmain*/, Scene * /*scene*/, PointerRNA *ptr)
{
VFont *vf = (VFont *)ptr->owner_id;
BKE_vfont_free_data(vf);
BKE_vfont_data_free(vf);
/* update */
WM_main_add_notifier(NC_GEOM | ND_DATA, nullptr);

View File

@@ -79,7 +79,7 @@ static void node_init(bNodeTree * /*tree*/, bNode *node)
data->align_y = GEO_NODE_STRING_TO_CURVES_ALIGN_Y_TOP_BASELINE;
data->pivot_mode = GEO_NODE_STRING_TO_CURVES_PIVOT_MODE_BOTTOM_LEFT;
node->storage = data;
node->id = reinterpret_cast<ID *>(BKE_vfont_builtin_get());
node->id = reinterpret_cast<ID *>(BKE_vfont_builtin_ensure());
}
static float3 get_pivot_point(GeoNodeExecParams &params, bke::CurvesGeometry &curves)
@@ -270,7 +270,7 @@ static Map<int, int> create_curve_instances(GeoNodeExecParams &params,
CharInfo charinfo = {0};
charinfo.mat_nr = 1;
BKE_vfont_build_char(&cu, &cu.nurb, layout.char_codes[i], &charinfo, 0, 0, 0, i, 1);
BKE_vfont_char_build(&cu, &cu.nurb, layout.char_codes[i], &charinfo, 0, 0, 0, i, 1);
Curves *curves_id = bke::curve_legacy_to_curves(cu);
if (curves_id == nullptr) {
if (pivot_required) {