From 77eec349737318b0537beccda5ca6a7153eca2ef Mon Sep 17 00:00:00 2001 From: Aras Pranckevicius Date: Thu, 31 Jul 2025 18:47:19 +0200 Subject: [PATCH] Cleanup: BLF FontFlags enum type safety, move enums to separate header - BLF font flags were an untyped enum; change to actual enum and update usages from int to that. - Move all BLF API enums to their own header, so that if something needs just the enums they don't have to include whole BLF_api.hh Pull Request: https://projects.blender.org/blender/blender/pulls/143692 --- source/blender/blenfont/BLF_api.hh | 57 ++--------------- source/blender/blenfont/BLF_enums.hh | 63 +++++++++++++++++++ source/blender/blenfont/CMakeLists.txt | 1 + source/blender/blenfont/intern/blf.cc | 8 +-- source/blender/blenfont/intern/blf_font.cc | 2 +- .../blenfont/intern/blf_internal_types.hh | 2 +- .../editors/interface/interface_style.cc | 10 +-- .../regions/interface_region_tooltip.cc | 2 +- source/blender/python/generic/blf_py_api.cc | 4 +- .../sequencer/intern/effects/effects.hh | 4 +- .../intern/effects/vse_effect_text.cc | 6 +- .../sequencer/intern/strip_transform.cc | 4 +- 12 files changed, 90 insertions(+), 73 deletions(-) create mode 100644 source/blender/blenfont/BLF_enums.hh diff --git a/source/blender/blenfont/BLF_api.hh b/source/blender/blenfont/BLF_api.hh index e606d76ad67..180bbe600ce 100644 --- a/source/blender/blenfont/BLF_api.hh +++ b/source/blender/blenfont/BLF_api.hh @@ -8,6 +8,8 @@ #pragma once +#include "BLF_enums.hh" + #include "BLI_array.hh" #include "BLI_bounds_types.hh" #include "BLI_compiler_attrs.h" @@ -34,20 +36,6 @@ class Display; } // namespace blender::ocio using ColorManagedDisplay = blender::ocio::Display; -enum class FontShadowType { - None = 0, - Blur3x3 = 3, - Blur5x5 = 5, - Outline = 6, -}; - -enum class BLFWrapMode : int { - Minimal = 0, /* Only on ASCII space and line feed. Legacy and invariant. */ - Typographical = 1 << 0, /* Multilingual, informed by Unicode Standard Annex #14. */ - Path = 1 << 1, /* Wrap on file path separators, space, underscores. */ - HardLimit = 1 << 2, /* Line break at limit. */ -}; - int BLF_init(); void BLF_exit(); @@ -332,8 +320,8 @@ blender::Vector BLF_string_wrap(int fontid, const int max_pixel_width, BLFWrapMode mode = BLFWrapMode::Minimal); -void BLF_enable(int fontid, int option); -void BLF_disable(int fontid, int option); +void BLF_enable(int fontid, FontFlags flag); +void BLF_disable(int fontid, FontFlags flag); /** * Is this font part of the default fonts in the fallback stack? @@ -433,43 +421,6 @@ void BLF_load_font_stack(); void BLF_state_print(int fontid); #endif -/** #FontBLF.flags. */ -enum { - BLF_ROTATION = 1 << 0, - BLF_CLIPPING = 1 << 1, - BLF_SHADOW = 1 << 2, - // BLF_FLAG_UNUSED_3 = 1 << 3, /* dirty */ - // BLF_MATRIX = 1 << 4, - BLF_ASPECT = 1 << 5, - BLF_WORD_WRAP = 1 << 6, - /** No anti-aliasing. */ - BLF_MONOCHROME = 1 << 7, - BLF_HINTING_NONE = 1 << 8, - BLF_HINTING_SLIGHT = 1 << 9, - BLF_HINTING_FULL = 1 << 10, - BLF_BOLD = 1 << 11, - BLF_ITALIC = 1 << 12, - /** Intended USE is monospaced, regardless of font type. */ - BLF_MONOSPACED = 1 << 13, - /** A font within the default stack of fonts. */ - BLF_DEFAULT = 1 << 14, - /** Must only be used as last font in the stack. */ - BLF_LAST_RESORT = 1 << 15, - /** Failure to load this font. Don't try again. */ - BLF_BAD_FONT = 1 << 16, - /** This font is managed by the FreeType cache subsystem. */ - BLF_CACHED = 1 << 17, - /** - * At small sizes glyphs are rendered at multiple sub-pixel positions. - * - * \note Can be checked without checking #BLF_MONOSPACED which can be assumed to be disabled. - */ - BLF_RENDER_SUBPIXELAA = 1 << 18, - - /* Do not look in other fonts when a glyph is not found in this font. */ - BLF_NO_FALLBACK = 1 << 19, -}; - #define BLF_DRAW_STR_DUMMY_MAX 1024 /* XXX, bad design */ diff --git a/source/blender/blenfont/BLF_enums.hh b/source/blender/blenfont/BLF_enums.hh new file mode 100644 index 00000000000..80b0bc499f1 --- /dev/null +++ b/source/blender/blenfont/BLF_enums.hh @@ -0,0 +1,63 @@ +/* SPDX-FileCopyrightText: 2025 Blender Authors + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +/** \file + * \ingroup blf + */ + +#pragma once + +#include "BLI_utildefines.h" + +enum class FontShadowType { + None = 0, + Blur3x3 = 3, + Blur5x5 = 5, + Outline = 6, +}; + +enum class BLFWrapMode : int { + Minimal = 0, /* Only on ASCII space and line feed. Legacy and invariant. */ + Typographical = 1 << 0, /* Multilingual, informed by Unicode Standard Annex #14. */ + Path = 1 << 1, /* Wrap on file path separators, space, underscores. */ + HardLimit = 1 << 2, /* Line break at limit. */ +}; + +enum FontFlags { + BLF_NONE = 0, + BLF_ROTATION = 1 << 0, + BLF_CLIPPING = 1 << 1, + BLF_SHADOW = 1 << 2, + // BLF_FLAG_UNUSED_3 = 1 << 3, /* dirty */ + // BLF_MATRIX = 1 << 4, + BLF_ASPECT = 1 << 5, + BLF_WORD_WRAP = 1 << 6, + /** No anti-aliasing. */ + BLF_MONOCHROME = 1 << 7, + BLF_HINTING_NONE = 1 << 8, + BLF_HINTING_SLIGHT = 1 << 9, + BLF_HINTING_FULL = 1 << 10, + BLF_BOLD = 1 << 11, + BLF_ITALIC = 1 << 12, + /** Intended USE is monospaced, regardless of font type. */ + BLF_MONOSPACED = 1 << 13, + /** A font within the default stack of fonts. */ + BLF_DEFAULT = 1 << 14, + /** Must only be used as last font in the stack. */ + BLF_LAST_RESORT = 1 << 15, + /** Failure to load this font. Don't try again. */ + BLF_BAD_FONT = 1 << 16, + /** This font is managed by the FreeType cache subsystem. */ + BLF_CACHED = 1 << 17, + /** + * At small sizes glyphs are rendered at multiple sub-pixel positions. + * + * \note Can be checked without checking #BLF_MONOSPACED which can be assumed to be disabled. + */ + BLF_RENDER_SUBPIXELAA = 1 << 18, + + /* Do not look in other fonts when a glyph is not found in this font. */ + BLF_NO_FALLBACK = 1 << 19, +}; +ENUM_OPERATORS(FontFlags, BLF_NO_FALLBACK); diff --git a/source/blender/blenfont/CMakeLists.txt b/source/blender/blenfont/CMakeLists.txt index 03056d30354..def0ff82302 100644 --- a/source/blender/blenfont/CMakeLists.txt +++ b/source/blender/blenfont/CMakeLists.txt @@ -20,6 +20,7 @@ set(SRC intern/blf_glyph.cc intern/blf_thumbs.cc BLF_api.hh + BLF_enums.hh intern/blf_internal.hh intern/blf_internal_types.hh ) diff --git a/source/blender/blenfont/intern/blf.cc b/source/blender/blenfont/intern/blf.cc index 2c92283bd27..00f6d3d6fad 100644 --- a/source/blender/blenfont/intern/blf.cc +++ b/source/blender/blenfont/intern/blf.cc @@ -317,21 +317,21 @@ void BLF_addref_id(int fontid) } } -void BLF_enable(int fontid, int option) +void BLF_enable(int fontid, FontFlags flag) { FontBLF *font = blf_get(fontid); if (font) { - font->flags |= option; + font->flags |= flag; } } -void BLF_disable(int fontid, int option) +void BLF_disable(int fontid, FontFlags flag) { FontBLF *font = blf_get(fontid); if (font) { - font->flags &= ~option; + font->flags &= ~flag; } } diff --git a/source/blender/blenfont/intern/blf_font.cc b/source/blender/blenfont/intern/blf_font.cc index 9543ad358f5..1b924bcec3a 100644 --- a/source/blender/blenfont/intern/blf_font.cc +++ b/source/blender/blenfont/intern/blf_font.cc @@ -1665,7 +1665,7 @@ static void blf_font_fill(FontBLF *font) font->clip_rec.xmax = 0; font->clip_rec.ymin = 0; font->clip_rec.ymax = 0; - font->flags = 0; + font->flags = BLF_NONE; font->size = 0; font->char_weight = 400; font->char_slant = 0.0f; diff --git a/source/blender/blenfont/intern/blf_internal_types.hh b/source/blender/blenfont/intern/blf_internal_types.hh index cef1ef67571..105b4b6dee2 100644 --- a/source/blender/blenfont/intern/blf_internal_types.hh +++ b/source/blender/blenfont/intern/blf_internal_types.hh @@ -371,7 +371,7 @@ struct FontBLF { int tex_size_max; /** Font options. */ - int flags; + FontFlags flags; /** * List of glyph caches (#GlyphCacheBLF) for this font for size, DPI, bold, italic. diff --git a/source/blender/editors/interface/interface_style.cc b/source/blender/editors/interface/interface_style.cc index 8d18656e5e5..456f1ffaa9e 100644 --- a/source/blender/editors/interface/interface_style.cc +++ b/source/blender/editors/interface/interface_style.cc @@ -133,7 +133,7 @@ void UI_fontstyle_draw_ex(const uiFontStyle *fs, ResultBLF *r_info) { int xofs = 0, yofs; - int font_flag = BLF_CLIPPING; + FontFlags font_flag = BLF_CLIPPING; UI_fontstyle_set(fs); @@ -213,7 +213,7 @@ void UI_fontstyle_draw_multiline_clipped_ex(const uiFontStyle *fs, ResultBLF *r_info) { int xofs = 0, yofs; - int font_flag = BLF_CLIPPING; + FontFlags font_flag = BLF_CLIPPING; /* Recommended for testing: Results should be the same with or without BLF clipping since the * string is wrapped and shortened to fit. Disabling it can help spot issues. */ @@ -540,9 +540,9 @@ void uiStyleInit() /* Set default flags based on UI preferences (not render fonts) */ { - const int flag_disable = (BLF_MONOCHROME | BLF_HINTING_NONE | BLF_HINTING_SLIGHT | - BLF_HINTING_FULL | BLF_RENDER_SUBPIXELAA); - int flag_enable = 0; + const FontFlags flag_disable = (BLF_MONOCHROME | BLF_HINTING_NONE | BLF_HINTING_SLIGHT | + BLF_HINTING_FULL | BLF_RENDER_SUBPIXELAA); + FontFlags flag_enable = BLF_NONE; if (U.text_render & USER_TEXT_HINTING_NONE) { flag_enable |= BLF_HINTING_NONE; diff --git a/source/blender/editors/interface/regions/interface_region_tooltip.cc b/source/blender/editors/interface/regions/interface_region_tooltip.cc index 76d4528f663..d753ae26a4e 100644 --- a/source/blender/editors/interface/regions/interface_region_tooltip.cc +++ b/source/blender/editors/interface/regions/interface_region_tooltip.cc @@ -1373,7 +1373,7 @@ static ARegion *ui_tooltip_create_with_data(bContext *C, wmWindow *win = CTX_wm_window(C); const blender::int2 win_size = WM_window_native_pixel_size(win); rcti rect_i; - int font_flag = 0; + FontFlags font_flag = BLF_NONE; /* Create area region. */ ARegion *region = ui_region_temp_add(CTX_wm_screen(C)); diff --git a/source/blender/python/generic/blf_py_api.cc b/source/blender/python/generic/blf_py_api.cc index c08379ac6c7..69c9bd51044 100644 --- a/source/blender/python/generic/blf_py_api.cc +++ b/source/blender/python/generic/blf_py_api.cc @@ -324,7 +324,7 @@ static PyObject *py_blf_disable(PyObject * /*self*/, PyObject *args) return nullptr; } - BLF_disable(fontid, option); + BLF_disable(fontid, FontFlags(option)); Py_RETURN_NONE; } @@ -349,7 +349,7 @@ static PyObject *py_blf_enable(PyObject * /*self*/, PyObject *args) return nullptr; } - BLF_enable(fontid, option); + BLF_enable(fontid, FontFlags(option)); Py_RETURN_NONE; } diff --git a/source/blender/sequencer/intern/effects/effects.hh b/source/blender/sequencer/intern/effects/effects.hh index 7d93dfa9088..c3ae0aa17cd 100644 --- a/source/blender/sequencer/intern/effects/effects.hh +++ b/source/blender/sequencer/intern/effects/effects.hh @@ -8,6 +8,8 @@ * \ingroup sequencer */ +#include "BLF_enums.hh" + #include "BLI_array.hh" #include "BLI_math_color.h" #include "BLI_math_vector_types.hh" @@ -146,6 +148,6 @@ static void apply_effect_op(const OpT &op, const ImBuf *src1, const ImBuf *src2, } TextVarsRuntime *text_effect_calc_runtime(const Strip *strip, int font, const int2 image_size); -int text_effect_font_init(const RenderData *context, const Strip *strip, int font_flags); +int text_effect_font_init(const RenderData *context, const Strip *strip, FontFlags font_flags); } // namespace blender::seq diff --git a/source/blender/sequencer/intern/effects/vse_effect_text.cc b/source/blender/sequencer/intern/effects/vse_effect_text.cc index d323cab7f2a..b4934c1d0b7 100644 --- a/source/blender/sequencer/intern/effects/vse_effect_text.cc +++ b/source/blender/sequencer/intern/effects/vse_effect_text.cc @@ -786,7 +786,7 @@ static int text_effect_line_size_get(const RenderData *context, const Strip *str return size_scale * data->text_size; } -int text_effect_font_init(const RenderData *context, const Strip *strip, int font_flags) +int text_effect_font_init(const RenderData *context, const Strip *strip, FontFlags font_flags) { TextVars *data = static_cast(strip->effectdata); int font = blf_mono_font_render; @@ -1029,8 +1029,8 @@ static ImBuf *do_text_effect(const RenderData *context, const char *display_device = context->scene->display_settings.display_device; const ColorManagedDisplay *display = IMB_colormanagement_display_get_named(display_device); - const int font_flags = ((data->flag & SEQ_TEXT_BOLD) ? BLF_BOLD : 0) | - ((data->flag & SEQ_TEXT_ITALIC) ? BLF_ITALIC : 0); + const FontFlags font_flags = ((data->flag & SEQ_TEXT_BOLD) ? BLF_BOLD : BLF_NONE) | + ((data->flag & SEQ_TEXT_ITALIC) ? BLF_ITALIC : BLF_NONE); /* Guard against parallel accesses to the fonts map. */ std::lock_guard lock(g_font_map.mutex); diff --git a/source/blender/sequencer/intern/strip_transform.cc b/source/blender/sequencer/intern/strip_transform.cc index 74662890d91..a07bd6f550a 100644 --- a/source/blender/sequencer/intern/strip_transform.cc +++ b/source/blender/sequencer/intern/strip_transform.cc @@ -609,8 +609,8 @@ float2 transform_image_raw_size_get(const Scene *scene, const Strip *strip) if (strip->type == STRIP_TYPE_TEXT) { const TextVars *data = static_cast(strip->effectdata); - const int font_flags = ((data->flag & SEQ_TEXT_BOLD) ? BLF_BOLD : 0) | - ((data->flag & SEQ_TEXT_ITALIC) ? BLF_ITALIC : 0); + const FontFlags font_flags = ((data->flag & SEQ_TEXT_BOLD) ? BLF_BOLD : BLF_NONE) | + ((data->flag & SEQ_TEXT_ITALIC) ? BLF_ITALIC : BLF_NONE); const int font = text_effect_font_init(nullptr, strip, font_flags); const TextVarsRuntime *runtime = text_effect_calc_runtime(