From 67d6334a37e18a88c412dac23175941ede519f50 Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Sat, 16 Sep 2023 08:12:36 -0700 Subject: [PATCH 01/23] WIP: UI: Configurable UI Text Styles Allows text character manipulation in Text Style --- scripts/startup/bl_ui/space_userpref.py | 4 +++ source/blender/blenfont/BLF_api.h | 5 +++ source/blender/blenfont/intern/blf.cc | 32 +++++++++++++++++++ .../editors/interface/interface_style.cc | 4 +++ source/blender/makesdna/DNA_userdef_types.h | 4 +++ source/blender/makesrna/intern/rna_userdef.cc | 28 ++++++++++++++++ 6 files changed, 77 insertions(+) diff --git a/scripts/startup/bl_ui/space_userpref.py b/scripts/startup/bl_ui/space_userpref.py index 3d5f8f74502..e6422da3518 100644 --- a/scripts/startup/bl_ui/space_userpref.py +++ b/scripts/startup/bl_ui/space_userpref.py @@ -1049,6 +1049,10 @@ class USERPREF_PT_theme_text_style(ThemePanel, CenterAlignMixIn, Panel): col = flow.column() col.prop(font_style, "points") + col.prop(font_style, "character_weight", text="Weight") + col.prop(font_style, "character_slant", text="Slant") + col.prop(font_style, "character_width", text="Width") + col.prop(font_style, "character_spacing", text="Spacing") col = flow.column(align=True) col.prop(font_style, "shadow_offset_x", text="Shadow Offset X") diff --git a/source/blender/blenfont/BLF_api.h b/source/blender/blenfont/BLF_api.h index 152323a24f8..9a58a830d8e 100644 --- a/source/blender/blenfont/BLF_api.h +++ b/source/blender/blenfont/BLF_api.h @@ -79,6 +79,11 @@ void BLF_aspect(int fontid, float x, float y, float z); void BLF_position(int fontid, float x, float y, float z); void BLF_size(int fontid, float size); +void BLF_character_weight(int fontid, float factor); +void BLF_character_slant(int fontid, float factor); +void BLF_character_width(int fontid, float factor); +void BLF_character_spacing(int fontid, float factor); + /* Goal: small but useful color API. */ void BLF_color4ubv(int fontid, const unsigned char rgba[4]); diff --git a/source/blender/blenfont/intern/blf.cc b/source/blender/blenfont/intern/blf.cc index 1912d4cc699..776feaec6bf 100644 --- a/source/blender/blenfont/intern/blf.cc +++ b/source/blender/blenfont/intern/blf.cc @@ -309,6 +309,38 @@ void BLF_disable(int fontid, int option) } } +void BLF_character_weight(int fontid, float factor) +{ + FontBLF *font = blf_get(fontid); + if (font) { + font->char_weight = factor; + } +} + +void BLF_character_slant(int fontid, float factor) +{ + FontBLF *font = blf_get(fontid); + if (font) { + font->char_slant = factor; + } +} + +void BLF_character_width(int fontid, float factor) +{ + FontBLF *font = blf_get(fontid); + if (font) { + font->char_width = factor; + } +} + +void BLF_character_spacing(int fontid, float factor) +{ + FontBLF *font = blf_get(fontid); + if (font) { + font->char_spacing = factor; + } +} + void BLF_aspect(int fontid, float x, float y, float z) { FontBLF *font = blf_get(fontid); diff --git a/source/blender/editors/interface/interface_style.cc b/source/blender/editors/interface/interface_style.cc index 13905b15438..74a23f51d21 100644 --- a/source/blender/editors/interface/interface_style.cc +++ b/source/blender/editors/interface/interface_style.cc @@ -489,6 +489,10 @@ static void fontstyle_set_ex(const uiFontStyle *fs, const float dpi_fac) uiFont *font = uifont_to_blfont(fs->uifont_id); BLF_size(font->blf_id, fs->points * dpi_fac); + BLF_character_weight(fs->uifont_id, fs->character_weight); + BLF_character_slant(fs->uifont_id, fs->character_slant); + BLF_character_width(fs->uifont_id, fs->character_width); + BLF_character_spacing(fs->uifont_id, fs->character_spacing); } void UI_fontstyle_set(const uiFontStyle *fs) diff --git a/source/blender/makesdna/DNA_userdef_types.h b/source/blender/makesdna/DNA_userdef_types.h index d3064c1b736..d87ff94bcb3 100644 --- a/source/blender/makesdna/DNA_userdef_types.h +++ b/source/blender/makesdna/DNA_userdef_types.h @@ -82,6 +82,10 @@ typedef struct uiFontStyle { /** 1 value, typically white or black anyway. */ float shadowcolor; char _pad2[4]; + float character_weight; + float character_slant; + float character_width; + float character_spacing; } uiFontStyle; /* this is fed to the layout engine and widget code */ diff --git a/source/blender/makesrna/intern/rna_userdef.cc b/source/blender/makesrna/intern/rna_userdef.cc index 75e6a1e4875..a1a73c34dd6 100644 --- a/source/blender/makesrna/intern/rna_userdef.cc +++ b/source/blender/makesrna/intern/rna_userdef.cc @@ -1280,6 +1280,34 @@ static void rna_def_userdef_theme_ui_font_style(BlenderRNA *brna) RNA_def_property_ui_text(prop, "Points", "Font size in points"); RNA_def_property_update(prop, 0, "rna_userdef_gpu_update"); + prop = RNA_def_property(srna, "character_weight", PROP_FLOAT, PROP_NONE); + RNA_def_property_float_default(prop, 0.0f); + RNA_def_property_range(prop, -1.0f, 1.0f); + RNA_def_property_ui_range(prop, -1.0f, 1.0f, 0.1, 3); + RNA_def_property_ui_text(prop, "Character Weight", "Change to the weight of the characters"); + RNA_def_property_update(prop, 0, "rna_userdef_text_update"); + + prop = RNA_def_property(srna, "character_slant", PROP_FLOAT, PROP_NONE); + RNA_def_property_float_default(prop, 0.0f); + RNA_def_property_range(prop, -1.0f, 1.0f); + RNA_def_property_ui_range(prop, -1.0f, 1.0f, 0.1, 3); + RNA_def_property_ui_text(prop, "Character Slant", "Change to the slant of the characters"); + RNA_def_property_update(prop, 0, "rna_userdef_text_update"); + + prop = RNA_def_property(srna, "character_width", PROP_FLOAT, PROP_NONE); + RNA_def_property_float_default(prop, 0.0f); + RNA_def_property_range(prop, -1.0f, 1.0f); + RNA_def_property_ui_range(prop, -1.0f, 1.0f, 0.1, 3); + RNA_def_property_ui_text(prop, "Character Width", "Change to the width of the characters"); + RNA_def_property_update(prop, 0, "rna_userdef_text_update"); + + prop = RNA_def_property(srna, "character_spacing", PROP_FLOAT, PROP_NONE); + RNA_def_property_float_default(prop, 0.0f); + RNA_def_property_range(prop, -1.0f, 1.0f); + RNA_def_property_ui_range(prop, -1.0f, 1.0f, 0.1, 3); + RNA_def_property_ui_text(prop, "Character Spacing", "Change to the spacing of the characters"); + RNA_def_property_update(prop, 0, "rna_userdef_text_update"); + prop = RNA_def_property(srna, "shadow", PROP_INT, PROP_PIXEL); RNA_def_property_range(prop, 0, 5); RNA_def_property_ui_text(prop, "Shadow Size", "Shadow size (0, 3 and 5 supported)"); -- 2.30.2 From fd368c1c4f43ca30eba790439526806de1b6e036 Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Sun, 17 Sep 2023 10:58:08 -0700 Subject: [PATCH 02/23] Comment change. --- source/blender/makesrna/intern/rna_userdef.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/source/blender/makesrna/intern/rna_userdef.cc b/source/blender/makesrna/intern/rna_userdef.cc index a1a73c34dd6..ec268b64392 100644 --- a/source/blender/makesrna/intern/rna_userdef.cc +++ b/source/blender/makesrna/intern/rna_userdef.cc @@ -1291,7 +1291,9 @@ static void rna_def_userdef_theme_ui_font_style(BlenderRNA *brna) RNA_def_property_float_default(prop, 0.0f); RNA_def_property_range(prop, -1.0f, 1.0f); RNA_def_property_ui_range(prop, -1.0f, 1.0f, 0.1, 3); - RNA_def_property_ui_text(prop, "Character Slant", "Change to the slant of the characters"); + RNA_def_property_ui_text(prop, + "Character Slant", + "Slant of the characters. Use negative values for a rightward slant."); RNA_def_property_update(prop, 0, "rna_userdef_text_update"); prop = RNA_def_property(srna, "character_width", PROP_FLOAT, PROP_NONE); -- 2.30.2 From 1aafb9a26498c84a4013d9565a4ee53b114ce444 Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Thu, 21 Sep 2023 15:05:44 -0700 Subject: [PATCH 03/23] Remove width from user control in Properties --- scripts/startup/bl_ui/space_userpref.py | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/startup/bl_ui/space_userpref.py b/scripts/startup/bl_ui/space_userpref.py index 6678f6c7049..ffe5afd6ef1 100644 --- a/scripts/startup/bl_ui/space_userpref.py +++ b/scripts/startup/bl_ui/space_userpref.py @@ -1051,7 +1051,6 @@ class USERPREF_PT_theme_text_style(ThemePanel, CenterAlignMixIn, Panel): col.prop(font_style, "points") col.prop(font_style, "character_weight", text="Weight") col.prop(font_style, "character_slant", text="Slant") - col.prop(font_style, "character_width", text="Width") col.prop(font_style, "character_spacing", text="Spacing") col = flow.column(align=True) -- 2.30.2 From df0619e5537aeabdbee7cc34e0fcce2a3b322ae0 Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Mon, 2 Oct 2023 15:57:59 -0700 Subject: [PATCH 04/23] All css-like settings. Augment variable axes with transforms --- scripts/startup/bl_ui/space_userpref.py | 1 + source/blender/blenfont/BLF_api.h | 4 +- source/blender/blenfont/intern/blf.cc | 8 +- source/blender/blenfont/intern/blf_font.cc | 6 + source/blender/blenfont/intern/blf_glyph.cc | 148 ++++++++++++------ .../blenfont/intern/blf_internal_types.h | 12 +- .../editors/interface/interface_style.cc | 16 ++ source/blender/makesdna/DNA_userdef_types.h | 2 +- source/blender/makesrna/intern/rna_userdef.cc | 35 ++--- 9 files changed, 154 insertions(+), 78 deletions(-) diff --git a/scripts/startup/bl_ui/space_userpref.py b/scripts/startup/bl_ui/space_userpref.py index dd7a7c9932f..d9a80a4267c 100644 --- a/scripts/startup/bl_ui/space_userpref.py +++ b/scripts/startup/bl_ui/space_userpref.py @@ -1078,6 +1078,7 @@ class USERPREF_PT_theme_text_style(ThemePanel, CenterAlignMixIn, Panel): col.prop(font_style, "points") col.prop(font_style, "character_weight", text="Weight") col.prop(font_style, "character_slant", text="Slant") + col.prop(font_style, "character_width", text="Width") col.prop(font_style, "character_spacing", text="Spacing") col = flow.column(align=True) diff --git a/source/blender/blenfont/BLF_api.h b/source/blender/blenfont/BLF_api.h index d2a6d232ffa..4d54bc51c99 100644 --- a/source/blender/blenfont/BLF_api.h +++ b/source/blender/blenfont/BLF_api.h @@ -79,8 +79,8 @@ void BLF_aspect(int fontid, float x, float y, float z); void BLF_position(int fontid, float x, float y, float z); void BLF_size(int fontid, float size); -void BLF_character_weight(int fontid, float factor); -void BLF_character_slant(int fontid, float factor); +void BLF_character_weight(int fontid, int weight); +void BLF_character_slant(int fontid, float degrees); void BLF_character_width(int fontid, float factor); void BLF_character_spacing(int fontid, float factor); diff --git a/source/blender/blenfont/intern/blf.cc b/source/blender/blenfont/intern/blf.cc index c0eb1d721de..49215219463 100644 --- a/source/blender/blenfont/intern/blf.cc +++ b/source/blender/blenfont/intern/blf.cc @@ -309,19 +309,19 @@ void BLF_disable(int fontid, int option) } } -void BLF_character_weight(int fontid, float factor) +void BLF_character_weight(int fontid, int weight) { FontBLF *font = blf_get(fontid); if (font) { - font->char_weight = factor; + font->char_weight = weight; } } -void BLF_character_slant(int fontid, float factor) +void BLF_character_slant(int fontid, float degrees) { FontBLF *font = blf_get(fontid); if (font) { - font->char_slant = factor; + font->char_slant = degrees; } } diff --git a/source/blender/blenfont/intern/blf_font.cc b/source/blender/blenfont/intern/blf_font.cc index ff06cc3f4c3..b942fff1380 100644 --- a/source/blender/blenfont/intern/blf_font.cc +++ b/source/blender/blenfont/intern/blf_font.cc @@ -1320,6 +1320,12 @@ static void blf_font_fill(FontBLF *font) font->clip_rec.ymax = 0; font->flags = 0; font->size = 0; + + font->char_weight = 400; + font->char_slant = 0.0f; + font->char_width = 1.0f; + font->char_spacing = 1.0f; + BLI_listbase_clear(&font->cache); font->kerning_cache = nullptr; #if BLF_BLUR_ENABLE diff --git a/source/blender/blenfont/intern/blf_glyph.cc b/source/blender/blenfont/intern/blf_glyph.cc index b3741b09356..ef1c1d212ac 100644 --- a/source/blender/blenfont/intern/blf_glyph.cc +++ b/source/blender/blenfont/intern/blf_glyph.cc @@ -58,6 +58,14 @@ static FT_Fixed to_16dot16(double val) return (FT_Fixed)lround(val * 65536.0); } +/** + * Convert a floating point value to a FreeType 16.16 fixed point value. + */ +static float from_16dot16(FT_Fixed val) +{ + return float(val) / 65536.0f; +} + /** \} */ /* -------------------------------------------------------------------- */ @@ -862,19 +870,62 @@ static bool blf_glyph_set_variation_normalized(const FontBLF *font, static bool blf_glyph_set_variation_float(FontBLF *font, FT_Fixed coords[], uint32_t tag, - float value) + float *value) { int axis_index; const FT_Var_Axis *axis = blf_var_axis_by_tag(font->variations, tag, &axis_index); if (axis && (axis_index < BLF_VARIATIONS_MAX)) { - FT_Fixed int_value = to_16dot16(value); + FT_Fixed int_value = to_16dot16(*value); CLAMP(int_value, axis->minimum, axis->maximum); coords[axis_index] = int_value; + *value = from_16dot16(int_value); return true; } return false; } +static float blf_glyph_set_variation_weight(FontBLF *font, FT_Fixed coords[], float weight) +{ + float value = weight; + if (blf_glyph_set_variation_float(font, coords, BLF_VARIATION_AXIS_WEIGHT, &value)) { + return value; + } + return 400.0f; +} + +static float blf_glyph_set_variation_slant(FontBLF *font, FT_Fixed coords[], float degrees) +{ + float value = -degrees; + if (blf_glyph_set_variation_float(font, coords, BLF_VARIATION_AXIS_SLANT, &value)) { + return -value; + } + return 0.0f; +} + +static float blf_glyph_set_variation_width(FontBLF *font, FT_Fixed coords[], float width) +{ + float value = width; + if (blf_glyph_set_variation_float(font, coords, BLF_VARIATION_AXIS_WIDTH, &value)) { + return value; + } + return 1.0f; +} + +static float blf_glyph_set_variation_spacing(FontBLF *font, FT_Fixed coords[], float spacing) +{ + float value = spacing; + if (blf_glyph_set_variation_float(font, coords, BLF_VARIATION_AXIS_SPACING, &value)) { + return value; + } + return 1.0f; +} + +static bool blf_glyph_set_variation_optical_size(FontBLF *font, FT_Fixed coords[], const float points) +{ + float value = points; + return blf_glyph_set_variation_float(font, coords, BLF_VARIATION_AXIS_OPTSIZE, &value); +} + /** \} */ /* -------------------------------------------------------------------- */ @@ -885,14 +936,15 @@ static bool blf_glyph_set_variation_float(FontBLF *font, * Adjust the glyph's weight by a factor. * Used for fonts without #BLF_VARIATION_AXIS_WEIGHT variable axis. * - * \param factor: -1 (min stroke width) <= 0 (normal) => 1 (max boldness). + * \param width: -500 (make thinner) <= 0 (normal) => 500 (add boldness). */ -static bool blf_glyph_transform_weight(FT_GlyphSlot glyph, float factor, bool monospaced) +static bool blf_glyph_transform_weight(FT_GlyphSlot glyph, float width, bool monospaced) { if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) { const FontBLF *font = (FontBLF *)glyph->face->generic.data; const FT_Pos average_width = font->ft_size->metrics.height; - FT_Pos change = (FT_Pos)(float(average_width) * factor * 0.12f); + float factor = width * 0.000225f; + FT_Pos change = (FT_Pos)(float(average_width) * factor); FT_Outline_EmboldenXY(&glyph->outline, change, 0); if (monospaced) { /* Widened fixed-pitch font needs a nudge left. */ @@ -911,16 +963,19 @@ static bool blf_glyph_transform_weight(FT_GlyphSlot glyph, float factor, bool mo * Adjust the glyph's slant by a factor. * Used for fonts without #BLF_VARIATION_AXIS_SLANT variable axis. * - * \param factor: -1 (max right-leaning) <= 0 (no slant) => 1 (max left-leaning). - * - * \note that left-leaning italics are possible in some RTL writing systems. + * \param degrees: amount of tilt in clockwise degrees. */ -static bool blf_glyph_transform_slant(FT_GlyphSlot glyph, float factor) +static bool blf_glyph_transform_slant(FT_GlyphSlot glyph, float degrees) { if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) { - /* Slant angle is counter-clockwise as per OTF specs. */ - FT_Matrix transform = {to_16dot16(1), to_16dot16(-factor / 4.0f), 0, to_16dot16(1)}; + FT_Matrix transform = {to_16dot16(1), to_16dot16(degrees * 0.0225f), 0, to_16dot16(1)}; FT_Outline_Transform(&glyph->outline, &transform); + if (degrees < 0.0f) { + const FontBLF *font = (FontBLF *)glyph->face->generic.data; + const FT_Pos average_width = font->ft_size->metrics.height; + FT_Pos change = (FT_Pos)(float(average_width) * degrees * -0.01f); + FT_Outline_Translate(&glyph->outline, change, 0); + } return true; } return false; @@ -930,12 +985,12 @@ static bool blf_glyph_transform_slant(FT_GlyphSlot glyph, float factor) * Adjust the glyph width by factor. * Used for fonts without #BLF_VARIATION_AXIS_WIDTH variable axis. * - * \param factor: -1 (min width) <= 0 (normal) => 1 (max width). + * \param factor: -1 (subtract width) <= 0 (normal) => 1 (add width). */ static bool blf_glyph_transform_width(FT_GlyphSlot glyph, float factor) { if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) { - float scale = (factor * 0.4f) + 1.0f; /* 0.6f - 1.4f */ + float scale = factor + 1.0f; FT_Matrix matrix = {to_16dot16(scale), 0, 0, to_16dot16(1)}; FT_Outline_Transform(&glyph->outline, &matrix); glyph->advance.x = (FT_Pos)(double(glyph->advance.x) * scale); @@ -948,7 +1003,7 @@ static bool blf_glyph_transform_width(FT_GlyphSlot glyph, float factor) * Adjust the glyph spacing by factor. * Used for fonts without #BLF_VARIATION_AXIS_SPACING variable axis. * - * \param factor: -1 (min tightness) <= 0 (normal) => 1 (max looseness). + * \param factor: -1 (make tighter) <= 0 (normal) => 1 (add space). */ static bool blf_glyph_transform_spacing(FT_GlyphSlot glyph, float factor) { @@ -1011,21 +1066,23 @@ static FT_GlyphSlot blf_glyph_render(FontBLF *settings_font, blf_ensure_size(glyph_font); - /* We need to keep track if changes are still needed. */ - bool weight_done = false; - bool slant_done = false; - bool width_done = false; - bool spacing_done = false; + /* Current variation values. */ + float weight = 400.0f; + float slant = 0.0f; + float width = 1.0f; + float spacing = 1.0f; - /* Treat bold as 75% of maximum weight. */ - float weight = (settings_font->flags & BLF_BOLD) ? 0.75f : settings_font->char_weight; - - /* Treat italics as 75% of maximum rightward slant. Note that slant angle is in - * counter-clockwise degrees per OTF spec, so negative. */ - float slant = (settings_font->flags & BLF_ITALIC) ? -0.75f : settings_font->char_slant; - - float width = settings_font->char_width; - float spacing = settings_font->char_spacing; + /* The variation targets we are hoping to get. */ + float weight_target = float(settings_font->char_weight); + if (settings_font->flags & BLF_BOLD) { + weight_target = MIN2(weight_target + 300.0f, 900.0f); + } + float slant_target = settings_font->char_slant; + if (settings_font->flags & BLF_ITALIC) { + slant_target = MIN2(slant_target + 8.0f, 15.0f); + } + float width_target = settings_font->char_width; + float spacing_target = settings_font->char_spacing; /* Font variations need to be set before glyph loading. Even if new value is zero. */ @@ -1034,17 +1091,14 @@ static FT_GlyphSlot blf_glyph_render(FontBLF *settings_font, /* Load current design coordinates. */ FT_Get_Var_Design_Coordinates(glyph_font->face, BLF_VARIATIONS_MAX, &coords[0]); /* Update design coordinates with new values. */ - weight_done = blf_glyph_set_variation_normalized( - glyph_font, coords, BLF_VARIATION_AXIS_WEIGHT, weight); - slant_done = blf_glyph_set_variation_normalized( - glyph_font, coords, BLF_VARIATION_AXIS_SLANT, slant); - width_done = blf_glyph_set_variation_normalized( - glyph_font, coords, BLF_VARIATION_AXIS_WIDTH, width); - spacing_done = blf_glyph_set_variation_normalized( - glyph_font, coords, BLF_VARIATION_AXIS_SPACING, spacing); - /* Optical size, if available, is set to current font size. */ - blf_glyph_set_variation_float( - glyph_font, coords, BLF_VARIATION_AXIS_OPTSIZE, settings_font->size); + + weight = blf_glyph_set_variation_weight(glyph_font, coords, weight_target); + slant = blf_glyph_set_variation_slant(glyph_font, coords, slant_target); + width = blf_glyph_set_variation_width(glyph_font, coords, width_target); + spacing = blf_glyph_set_variation_spacing(glyph_font, coords, spacing_target); + + blf_glyph_set_variation_optical_size(glyph_font, coords, settings_font->size); + /* Save updated design coordinates. */ FT_Set_Var_Design_Coordinates(glyph_font->face, BLF_VARIATIONS_MAX, &coords[0]); } @@ -1063,17 +1117,17 @@ static FT_GlyphSlot blf_glyph_render(FontBLF *settings_font, /* Fallback glyph transforms, but only if required and not yet done. */ - if (weight != 0.0f && !weight_done) { - blf_glyph_transform_weight(glyph, weight, FT_IS_FIXED_WIDTH(glyph_font)); + if (weight != weight_target) { + blf_glyph_transform_weight(glyph, weight_target - weight, FT_IS_FIXED_WIDTH(glyph_font)); } - if (slant != 0.0f && !slant_done) { - blf_glyph_transform_slant(glyph, slant); + if (slant != slant_target) { + blf_glyph_transform_slant(glyph, slant_target - slant); } - if (width != 0.0f && !width_done) { - blf_glyph_transform_width(glyph, width); + if (width != width_target) { + blf_glyph_transform_width(glyph, width_target - width); } - if (spacing != 0.0f && !spacing_done) { - blf_glyph_transform_spacing(glyph, spacing); + if (spacing != spacing_target) { + blf_glyph_transform_spacing(glyph, spacing_target - spacing); } FT_Outline_Translate(&glyph->outline, (FT_Pos)subpixel, 0); diff --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h index 4785de08463..1ceabb98050 100644 --- a/source/blender/blenfont/intern/blf_internal_types.h +++ b/source/blender/blenfont/intern/blf_internal_types.h @@ -116,7 +116,7 @@ typedef struct GlyphCacheBLF { /** Font size. */ float size; - float char_weight; + int char_weight; float char_slant; float char_width; float char_spacing; @@ -274,11 +274,11 @@ typedef struct FontBLF { /** Axes data for Adobe MM, TrueType GX, or OpenType variation fonts. */ FT_MM_Var *variations; - /** Character variation; 0=default, -1=min, +1=max. */ - float char_weight; - float char_slant; - float char_width; - float char_spacing; + /** Character variation in 16.16 format. */ + int char_weight; /* 100 - 900, 400 = normal. */ + float char_slant; /* Slant in clockwise degrees. 0.0 = upright. */ + float char_width; /* Factor of normal character width. 1.0 = normal. */ + float char_spacing /* Factor of normal normal spacing. 1.0 = normal. */; /** Max texture size. */ int tex_size_max; diff --git a/source/blender/editors/interface/interface_style.cc b/source/blender/editors/interface/interface_style.cc index f9a950c7583..57925db8820 100644 --- a/source/blender/editors/interface/interface_style.cc +++ b/source/blender/editors/interface/interface_style.cc @@ -69,6 +69,10 @@ static uiStyle *ui_style_new(ListBase *styles, const char *name, short uifont_id style->paneltitle.uifont_id = uifont_id; style->paneltitle.points = UI_DEFAULT_TITLE_POINTS; + style->paneltitle.character_weight = 400; + style->paneltitle.character_width = 1.0f; + style->paneltitle.character_slant = 0.0f; + style->paneltitle.character_spacing = 1.0f; style->paneltitle.shadow = 3; style->paneltitle.shadx = 0; style->paneltitle.shady = -1; @@ -77,6 +81,10 @@ static uiStyle *ui_style_new(ListBase *styles, const char *name, short uifont_id style->grouplabel.uifont_id = uifont_id; style->grouplabel.points = UI_DEFAULT_TITLE_POINTS; + style->grouplabel.character_weight = 400; + style->grouplabel.character_width = 1.0f; + style->grouplabel.character_slant = 0.0f; + style->grouplabel.character_spacing = 1.0f; style->grouplabel.shadow = 3; style->grouplabel.shadx = 0; style->grouplabel.shady = -1; @@ -85,6 +93,10 @@ static uiStyle *ui_style_new(ListBase *styles, const char *name, short uifont_id style->widgetlabel.uifont_id = uifont_id; style->widgetlabel.points = UI_DEFAULT_TEXT_POINTS; + style->widgetlabel.character_weight = 400; + style->widgetlabel.character_width = 1.0f; + style->widgetlabel.character_slant = 0.0f; + style->widgetlabel.character_spacing = 1.0f; style->widgetlabel.shadow = 3; style->widgetlabel.shadx = 0; style->widgetlabel.shady = -1; @@ -93,6 +105,10 @@ static uiStyle *ui_style_new(ListBase *styles, const char *name, short uifont_id style->widget.uifont_id = uifont_id; style->widget.points = UI_DEFAULT_TEXT_POINTS; + style->widget.character_weight = 400; + style->widget.character_width = 1.0f; + style->widget.character_slant = 0.0f; + style->widget.character_spacing = 1.0f; style->widget.shadow = 1; style->widget.shady = -1; style->widget.shadowalpha = 0.5f; diff --git a/source/blender/makesdna/DNA_userdef_types.h b/source/blender/makesdna/DNA_userdef_types.h index 980a57f76ae..3de806f8811 100644 --- a/source/blender/makesdna/DNA_userdef_types.h +++ b/source/blender/makesdna/DNA_userdef_types.h @@ -82,7 +82,7 @@ typedef struct uiFontStyle { /** 1 value, typically white or black anyway. */ float shadowcolor; char _pad2[4]; - float character_weight; + int character_weight; float character_slant; float character_width; float character_spacing; diff --git a/source/blender/makesrna/intern/rna_userdef.cc b/source/blender/makesrna/intern/rna_userdef.cc index 7bd3ffffbcc..1f450ecab76 100644 --- a/source/blender/makesrna/intern/rna_userdef.cc +++ b/source/blender/makesrna/intern/rna_userdef.cc @@ -1294,34 +1294,33 @@ static void rna_def_userdef_theme_ui_font_style(BlenderRNA *brna) RNA_def_property_ui_text(prop, "Points", "Font size in points"); RNA_def_property_update(prop, 0, "rna_userdef_gpu_update"); - prop = RNA_def_property(srna, "character_weight", PROP_FLOAT, PROP_NONE); - RNA_def_property_float_default(prop, 0.0f); - RNA_def_property_range(prop, -1.0f, 1.0f); - RNA_def_property_ui_range(prop, -1.0f, 1.0f, 0.1, 3); - RNA_def_property_ui_text(prop, "Character Weight", "Change to the weight of the characters"); + prop = RNA_def_property(srna, "character_weight", PROP_INT, PROP_NONE); + RNA_def_property_int_default(prop, 400); + RNA_def_property_range(prop, 100.0f, 900.0f); + RNA_def_property_ui_range(prop, 100.0f, 900.0f, 1, 0); + RNA_def_property_ui_text( + prop, "Character Weight", "Weight of the characters. 100-900, 400 is normal"); RNA_def_property_update(prop, 0, "rna_userdef_text_update"); prop = RNA_def_property(srna, "character_slant", PROP_FLOAT, PROP_NONE); RNA_def_property_float_default(prop, 0.0f); - RNA_def_property_range(prop, -1.0f, 1.0f); - RNA_def_property_ui_range(prop, -1.0f, 1.0f, 0.1, 3); - RNA_def_property_ui_text(prop, - "Character Slant", - "Slant of the characters. Use negative values for a rightward slant."); + RNA_def_property_range(prop, -45.0f, 45.0f); + RNA_def_property_ui_range(prop, -15.0f, 15.0f, 1.0f, 2); + RNA_def_property_ui_text(prop, "Character Slant", "Slant of the characters in degrees"); RNA_def_property_update(prop, 0, "rna_userdef_text_update"); prop = RNA_def_property(srna, "character_width", PROP_FLOAT, PROP_NONE); - RNA_def_property_float_default(prop, 0.0f); - RNA_def_property_range(prop, -1.0f, 1.0f); - RNA_def_property_ui_range(prop, -1.0f, 1.0f, 0.1, 3); - RNA_def_property_ui_text(prop, "Character Width", "Change to the width of the characters"); + RNA_def_property_float_default(prop, 1.0f); + RNA_def_property_range(prop, 0.01f, 5.0f); + RNA_def_property_ui_range(prop, 0.5f, 2.0f, 1.0, 3); + RNA_def_property_ui_text(prop, "Character Width", "Factor for character width. 1 is normal"); RNA_def_property_update(prop, 0, "rna_userdef_text_update"); prop = RNA_def_property(srna, "character_spacing", PROP_FLOAT, PROP_NONE); - RNA_def_property_float_default(prop, 0.0f); - RNA_def_property_range(prop, -1.0f, 1.0f); - RNA_def_property_ui_range(prop, -1.0f, 1.0f, 0.1, 3); - RNA_def_property_ui_text(prop, "Character Spacing", "Change to the spacing of the characters"); + RNA_def_property_float_default(prop, 1.0f); + RNA_def_property_range(prop, 0.00f, 10.0f); + RNA_def_property_ui_range(prop, 0.5f, 5.0f, 1.0, 3); + RNA_def_property_ui_text(prop, "Character Spacing", "Factor for character spacing. 1 is normal"); RNA_def_property_update(prop, 0, "rna_userdef_text_update"); prop = RNA_def_property(srna, "shadow", PROP_INT, PROP_PIXEL); -- 2.30.2 From 145d0343f19e4a1301eb612ecb6d331dbf63dc54 Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Wed, 4 Oct 2023 19:04:36 -0700 Subject: [PATCH 05/23] Versioning. Start to collecting font info. --- source/blender/blenfont/intern/blf_glyph.cc | 10 ++--- .../blenfont/intern/blf_internal_types.h | 38 +++++++++++++++++++ .../blenloader/intern/versioning_userdef.cc | 15 ++++++++ 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/source/blender/blenfont/intern/blf_glyph.cc b/source/blender/blenfont/intern/blf_glyph.cc index ef1c1d212ac..8046d101626 100644 --- a/source/blender/blenfont/intern/blf_glyph.cc +++ b/source/blender/blenfont/intern/blf_glyph.cc @@ -1066,11 +1066,11 @@ static FT_GlyphSlot blf_glyph_render(FontBLF *settings_font, blf_ensure_size(glyph_font); - /* Current variation values. */ - float weight = 400.0f; - float slant = 0.0f; - float width = 1.0f; - float spacing = 1.0f; + /* Current (base) style values. */ + float weight = settings_font->info.weight; + float slant = settings_font->info.slant; + float width = settings_font->info.width; + float spacing = settings_font->info.spacing; /* The variation targets we are hoping to get. */ float weight_target = float(settings_font->char_weight); diff --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h index 1ceabb98050..26b560cbe09 100644 --- a/source/blender/blenfont/intern/blf_internal_types.h +++ b/source/blender/blenfont/intern/blf_internal_types.h @@ -209,6 +209,42 @@ typedef struct FontBufInfoBLF { } FontBufInfoBLF; +typedef struct FontInfo { + // preview? + short weight = 400; // default weight, 100 - 900. get from OS/2 usWeightClass + float width = 1.0f; // default width 1.0. get from OS/2 usWidthClass + float slant = 0.0f; // default slant 0.0. get from Post italicAngle; + float spacing = 1.0f; // default spacing 1.0 + + short family_class; + char panose_family_type; + char panose_serif_style; + char panose_weight; + char panose_proportion; + char panose_contrast; + char panose_stroke_variation; + char panose_arm_style; + char panose_letter_form; + char panose_midline; + char panose_xheight; + short selection_flags; + short first_charindex; + short last_charindex; + short typo_ascender; + short typo_descender; + short typo_linegap; + + short x_height; + short cap_height; + + short lower_optical_point_size; + short upper_optical_point_size; + + // move unicode range bits here too + // CodePageRangebits + +} FontInfo; + typedef struct FontBLF { /** Full path to font file or NULL if from memory. */ char *filepath; @@ -307,6 +343,8 @@ typedef struct FontBLF { /** Copy of the font->face->face_flags, in case we don't have a face loaded. */ FT_Long face_flags; + FontInfo info; + /** Data for buffer usage (drawing into a texture buffer) */ FontBufInfoBLF buf_info; diff --git a/source/blender/blenloader/intern/versioning_userdef.cc b/source/blender/blenloader/intern/versioning_userdef.cc index 7e785184212..d202be210b9 100644 --- a/source/blender/blenloader/intern/versioning_userdef.cc +++ b/source/blender/blenloader/intern/versioning_userdef.cc @@ -896,6 +896,21 @@ void blo_do_versions_userdef(UserDef *userdef) */ { /* Keep this block, even when empty. */ + + LISTBASE_FOREACH (uiStyle *, style, &userdef->uistyles) { + style->paneltitle.character_weight = 400; + style->paneltitle.character_width = 1.0f; + style->paneltitle.character_spacing = 1.0f; + style->grouplabel.character_weight = 400; + style->grouplabel.character_width = 1.0f; + style->grouplabel.character_spacing = 1.0f; + style->widgetlabel.character_weight = 400; + style->widgetlabel.character_width = 1.0f; + style->widgetlabel.character_spacing = 1.0f; + style->widget.character_weight = 400; + style->widget.character_width = 1.0f; + style->widget.character_spacing = 1.0f; + } } LISTBASE_FOREACH (bTheme *, btheme, &userdef->themes) { -- 2.30.2 From 596c0121b44d96551f7e4390d1eccb1eaddf8b03 Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Fri, 6 Oct 2023 16:51:45 -0700 Subject: [PATCH 06/23] Mostly gathering font metrics needed for this and vfont. --- source/blender/blenfont/intern/blf_font.cc | 93 +++++++++++++++++++ .../blenfont/intern/blf_internal_types.h | 24 ++++- 2 files changed, 112 insertions(+), 5 deletions(-) diff --git a/source/blender/blenfont/intern/blf_font.cc b/source/blender/blenfont/intern/blf_font.cc index b942fff1380..e6825fa584c 100644 --- a/source/blender/blenfont/intern/blf_font.cc +++ b/source/blender/blenfont/intern/blf_font.cc @@ -1342,6 +1342,10 @@ static void blf_font_fill(FontBLF *font) font->buf_info.col_init[1] = 0; font->buf_info.col_init[2] = 0; font->buf_info.col_init[3] = 0; + + font->info.weight = 400; + font->info.width = 1.0f; + font->info.spacing = 1.0f; } bool blf_ensure_face(FontBLF *font) @@ -1438,8 +1442,97 @@ bool blf_ensure_face(FontBLF *font) font->unicode_ranges[1] = uint(os2_table->ulUnicodeRange2); font->unicode_ranges[2] = uint(os2_table->ulUnicodeRange3); font->unicode_ranges[3] = uint(os2_table->ulUnicodeRange4); + + if (os2_table->usWeightClass >= 1 && os2_table->usWeightClass <= 1000) { + font->info.weight = short(os2_table->usWeightClass); + } + + if (os2_table->usWidthClass >= 1 && os2_table->usWidthClass <= 9) { + switch (os2_table->usWidthClass) { + case 1: + font->info.width = 0.5f; + break; + case 2: + font->info.width = 0.625f; + break; + case 3: + font->info.width = 0.75f; + break; + case 4: + font->info.width = 0.875f; + break; + case 5: + font->info.width = 1.0f; + break; + case 6: + font->info.width = 1.125f; + break; + case 7: + font->info.width = 1.25f; + break; + case 8: + font->info.width = 1.5f; + break; + case 9: + font->info.width = 2.0f; + break; + } + } + + font->info.family_class = short(os2_table->sFamilyClass); + + font->info.panose_family_type = char(os2_table->panose[0]); + font->info.panose_serif_style = char(os2_table->panose[1]); + font->info.panose_weight = char(os2_table->panose[2]); + font->info.panose_proportion = char(os2_table->panose[3]); + font->info.panose_contrast = char(os2_table->panose[4]); + font->info.panose_stroke_variation = char(os2_table->panose[5]); + font->info.panose_arm_style = char(os2_table->panose[6]); + font->info.panose_letter_form = char(os2_table->panose[7]); + font->info.panose_midline = char(os2_table->panose[7]); + font->info.panose_xheight = char(os2_table->panose[9]); + + font->info.selection_flags = short(os2_table->fsSelection); + font->info.first_charindex = short(os2_table->usFirstCharIndex); + font->info.last_charindex = short(os2_table->usLastCharIndex); + font->info.typo_ascender = short(os2_table->sTypoAscender); + font->info.typo_descender = short(os2_table->sTypoDescender); + font->info.typo_linegap = short(os2_table->sTypoLineGap); + + if (os2_table->version > 1) { + font->info.cap_height = short(os2_table->sCapHeight); + font->info.x_height = short(os2_table->sxHeight); + } + + if (os2_table->version > 4) { + font->info.lower_optical_point_size = short(os2_table->usLowerOpticalPointSize); + font->info.upper_optical_point_size = short(os2_table->usUpperOpticalPointSize); + } + } + TT_Postscript *post_table = (TT_Postscript *)FT_Get_Sfnt_Table(font->face, FT_SFNT_POST); + if (post_table) { + if (post_table->italicAngle != 0) { + font->info.slant = -float(post_table->italicAngle) / -65536.0f; + } + } + + font->info.units_per_EM = short(font->face->units_per_EM); + font->info.ascender = short(font->face->ascender); + font->info.descender = short(font->face->descender); + font->info.height = short(font->face->height); + font->info.max_advance_width = short(font->face->max_advance_width); + font->info.max_advance_height = short(font->face->max_advance_height); + font->info.underline_position = short(font->face->underline_position); + font->info.underline_height = short(font->face->underline_thickness); + font->info.num_glyphs = int(font->face->num_glyphs); + + font->info.bounding_box.xmin = int(font->face->bbox.xMin); + font->info.bounding_box.xmax = int(font->face->bbox.xMax); + font->info.bounding_box.ymin = int(font->face->bbox.yMin); + font->info.bounding_box.ymax = int(font->face->bbox.yMax); + if (FT_IS_FIXED_WIDTH(font)) { font->flags |= BLF_MONOSPACED; } diff --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h index 26b560cbe09..2a21572fd07 100644 --- a/source/blender/blenfont/intern/blf_internal_types.h +++ b/source/blender/blenfont/intern/blf_internal_types.h @@ -210,13 +210,26 @@ typedef struct FontBufInfoBLF { } FontBufInfoBLF; typedef struct FontInfo { - // preview? - short weight = 400; // default weight, 100 - 900. get from OS/2 usWeightClass - float width = 1.0f; // default width 1.0. get from OS/2 usWidthClass - float slant = 0.0f; // default slant 0.0. get from Post italicAngle; - float spacing = 1.0f; // default spacing 1.0 + short weight; + float width; + float slant; + float spacing; + + short units_per_EM; + short ascender; + short descender; + short height; + short max_advance_width; + short max_advance_height; + short underline_position; + short underline_height; + + int num_glyphs; + + rcti bounding_box; short family_class; + char panose_family_type; char panose_serif_style; char panose_weight; @@ -227,6 +240,7 @@ typedef struct FontInfo { char panose_letter_form; char panose_midline; char panose_xheight; + short selection_flags; short first_charindex; short last_charindex; -- 2.30.2 From d59ff523f3e3b1b2d7493764c64027e759fa7d55 Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Fri, 6 Oct 2023 18:38:44 -0700 Subject: [PATCH 07/23] Mostly x-height and cap height calculations. --- source/blender/blenfont/intern/blf_font.cc | 30 +++++++++++++++++-- .../blenfont/intern/blf_internal_types.h | 2 -- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/source/blender/blenfont/intern/blf_font.cc b/source/blender/blenfont/intern/blf_font.cc index e6825fa584c..733a26ca1b2 100644 --- a/source/blender/blenfont/intern/blf_font.cc +++ b/source/blender/blenfont/intern/blf_font.cc @@ -1495,8 +1495,6 @@ bool blf_ensure_face(FontBLF *font) font->info.selection_flags = short(os2_table->fsSelection); font->info.first_charindex = short(os2_table->usFirstCharIndex); font->info.last_charindex = short(os2_table->usLastCharIndex); - font->info.typo_ascender = short(os2_table->sTypoAscender); - font->info.typo_descender = short(os2_table->sTypoDescender); font->info.typo_linegap = short(os2_table->sTypoLineGap); if (os2_table->version > 1) { @@ -1533,6 +1531,34 @@ bool blf_ensure_face(FontBLF *font) font->info.bounding_box.ymin = int(font->face->bbox.yMin); font->info.bounding_box.ymax = int(font->face->bbox.yMax); + if (font->info.ascender == 0) { + font->info.ascender = short(float(font->info.units_per_EM) * 0.8f); + } + + if (font->info.descender == 0) { + font->info.descender = font->info.ascender - font->info.units_per_EM; + } + + if (font->info.cap_height == 0) { + FT_UInt gi = FT_Get_Char_Index(font->face, (FT_ULong)'H'); + if (gi && FT_Load_Glyph(font->face, gi, FT_LOAD_NO_SCALE | FT_LOAD_NO_BITMAP) == FT_Err_Ok) { + font->info.cap_height = short(font->face->glyph->metrics.height); + } + else { + font->info.cap_height = short(float(font->info.units_per_EM) * 0.7f); + } + } + + if (font->info.x_height == 0) { + FT_UInt gi = FT_Get_Char_Index(font->face, (FT_ULong)'x'); + if (gi && FT_Load_Glyph(font->face, gi, FT_LOAD_NO_SCALE | FT_LOAD_NO_BITMAP) == FT_Err_Ok) { + font->info.x_height = short(font->face->glyph->metrics.height); + } + else { + font->info.x_height = short(float(font->info.units_per_EM) * 0.5f); + } + } + if (FT_IS_FIXED_WIDTH(font)) { font->flags |= BLF_MONOSPACED; } diff --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h index 2a21572fd07..fb81dcc6453 100644 --- a/source/blender/blenfont/intern/blf_internal_types.h +++ b/source/blender/blenfont/intern/blf_internal_types.h @@ -244,8 +244,6 @@ typedef struct FontInfo { short selection_flags; short first_charindex; short last_charindex; - short typo_ascender; - short typo_descender; short typo_linegap; short x_height; -- 2.30.2 From 5dd128d89bdbf68c8308676db464b5a851937816 Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Fri, 6 Oct 2023 19:17:54 -0700 Subject: [PATCH 08/23] Mostly detecting weight and slant for non-variable fonts. --- source/blender/blenfont/intern/blf_font.cc | 29 ++++++++++++++++------ 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/source/blender/blenfont/intern/blf_font.cc b/source/blender/blenfont/intern/blf_font.cc index 733a26ca1b2..ad74f8d8e75 100644 --- a/source/blender/blenfont/intern/blf_font.cc +++ b/source/blender/blenfont/intern/blf_font.cc @@ -1443,10 +1443,12 @@ bool blf_ensure_face(FontBLF *font) font->unicode_ranges[2] = uint(os2_table->ulUnicodeRange3); font->unicode_ranges[3] = uint(os2_table->ulUnicodeRange4); + /* The default weight. */ if (os2_table->usWeightClass >= 1 && os2_table->usWeightClass <= 1000) { font->info.weight = short(os2_table->usWeightClass); } + /* Width value is just integers 1-9 with known values. */ if (os2_table->usWidthClass >= 1 && os2_table->usWidthClass <= 9) { switch (os2_table->usWidthClass) { case 1: @@ -1479,8 +1481,8 @@ bool blf_ensure_face(FontBLF *font) } } + /* This table contains most of the font metrics. */ font->info.family_class = short(os2_table->sFamilyClass); - font->info.panose_family_type = char(os2_table->panose[0]); font->info.panose_serif_style = char(os2_table->panose[1]); font->info.panose_weight = char(os2_table->panose[2]); @@ -1491,24 +1493,21 @@ bool blf_ensure_face(FontBLF *font) font->info.panose_letter_form = char(os2_table->panose[7]); font->info.panose_midline = char(os2_table->panose[7]); font->info.panose_xheight = char(os2_table->panose[9]); - font->info.selection_flags = short(os2_table->fsSelection); font->info.first_charindex = short(os2_table->usFirstCharIndex); font->info.last_charindex = short(os2_table->usLastCharIndex); font->info.typo_linegap = short(os2_table->sTypoLineGap); - if (os2_table->version > 1) { font->info.cap_height = short(os2_table->sCapHeight); font->info.x_height = short(os2_table->sxHeight); } - if (os2_table->version > 4) { font->info.lower_optical_point_size = short(os2_table->usLowerOpticalPointSize); font->info.upper_optical_point_size = short(os2_table->usUpperOpticalPointSize); } - } + /* The Post table usually contains the slant value, though in counter-clockwise degrees. */ TT_Postscript *post_table = (TT_Postscript *)FT_Get_Sfnt_Table(font->face, FT_SFNT_POST); if (post_table) { if (post_table->italicAngle != 0) { @@ -1516,6 +1515,7 @@ bool blf_ensure_face(FontBLF *font) } } + /* Copy the metrics in case we need them when we don't have the face loaded. */ font->info.units_per_EM = short(font->face->units_per_EM); font->info.ascender = short(font->face->ascender); font->info.descender = short(font->face->descender); @@ -1525,22 +1525,24 @@ bool blf_ensure_face(FontBLF *font) font->info.underline_position = short(font->face->underline_position); font->info.underline_height = short(font->face->underline_thickness); font->info.num_glyphs = int(font->face->num_glyphs); - font->info.bounding_box.xmin = int(font->face->bbox.xMin); font->info.bounding_box.xmax = int(font->face->bbox.xMax); font->info.bounding_box.ymin = int(font->face->bbox.yMin); font->info.bounding_box.ymax = int(font->face->bbox.yMax); if (font->info.ascender == 0) { + /* Set a sane value for ascender if not set in the font. */ font->info.ascender = short(float(font->info.units_per_EM) * 0.8f); } if (font->info.descender == 0) { + /* Set a sane value for descender if not set in the font. */ font->info.descender = font->info.ascender - font->info.units_per_EM; } if (font->info.cap_height == 0) { - FT_UInt gi = FT_Get_Char_Index(font->face, (FT_ULong)'H'); + /* Calculate or guess cap height if it is not set in the font. */ + FT_UInt gi = blf_get_char_index(font, uint('H')); if (gi && FT_Load_Glyph(font->face, gi, FT_LOAD_NO_SCALE | FT_LOAD_NO_BITMAP) == FT_Err_Ok) { font->info.cap_height = short(font->face->glyph->metrics.height); } @@ -1550,7 +1552,8 @@ bool blf_ensure_face(FontBLF *font) } if (font->info.x_height == 0) { - FT_UInt gi = FT_Get_Char_Index(font->face, (FT_ULong)'x'); + /* Calculate or guess x-height if it is not set in the font. */ + FT_UInt gi = blf_get_char_index(font, uint('x')); if (gi && FT_Load_Glyph(font->face, gi, FT_LOAD_NO_SCALE | FT_LOAD_NO_BITMAP) == FT_Err_Ok) { font->info.x_height = short(font->face->glyph->metrics.height); } @@ -1559,6 +1562,16 @@ bool blf_ensure_face(FontBLF *font) } } + if (font->info.weight == 400 && font->face->style_flags & FT_STYLE_FLAG_BOLD) { + /* Normal weight yet this is an bold font, so set a sane weight value. */ + font->info.weight = 700; + } + + if (font->info.slant == 0.0f && font->face->style_flags & FT_STYLE_FLAG_ITALIC) { + /* No known slant yet this is an italic font, so set a sane slant value. */ + font->info.slant = 8.0f; + } + if (FT_IS_FIXED_WIDTH(font)) { font->flags |= BLF_MONOSPACED; } -- 2.30.2 From 426f4ffeb51ccfd2682ed94afbcebcca6108f5ec Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Sat, 7 Oct 2023 10:37:01 -0700 Subject: [PATCH 09/23] More metrics. Now in "metrics", not "info" --- source/blender/blenfont/intern/blf_font.cc | 307 ++++++++++-------- source/blender/blenfont/intern/blf_glyph.cc | 8 +- .../blenfont/intern/blf_internal_types.h | 25 +- 3 files changed, 194 insertions(+), 146 deletions(-) diff --git a/source/blender/blenfont/intern/blf_font.cc b/source/blender/blenfont/intern/blf_font.cc index ad74f8d8e75..cfa20cc311d 100644 --- a/source/blender/blenfont/intern/blf_font.cc +++ b/source/blender/blenfont/intern/blf_font.cc @@ -1343,9 +1343,180 @@ static void blf_font_fill(FontBLF *font) font->buf_info.col_init[2] = 0; font->buf_info.col_init[3] = 0; - font->info.weight = 400; - font->info.width = 1.0f; - font->info.spacing = 1.0f; + font->metrics.weight = 400; + font->metrics.width = 1.0f; + font->metrics.spacing = 1.0f; +} + +static void blf_font_metrics(FT_Face face, FontMetrics *metrics) +{ + TT_OS2 *os2_table = (TT_OS2 *)FT_Get_Sfnt_Table(face, FT_SFNT_OS2); + if (os2_table) { + /* The default weight. */ + if (os2_table->usWeightClass >= 1 && os2_table->usWeightClass <= 1000) { + metrics->weight = short(os2_table->usWeightClass); + } + + /* Width value is just integers 1-9 with known values. */ + if (os2_table->usWidthClass >= 1 && os2_table->usWidthClass <= 9) { + switch (os2_table->usWidthClass) { + case 1: + metrics->width = 0.5f; + break; + case 2: + metrics->width = 0.625f; + break; + case 3: + metrics->width = 0.75f; + break; + case 4: + metrics->width = 0.875f; + break; + case 5: + metrics->width = 1.0f; + break; + case 6: + metrics->width = 1.125f; + break; + case 7: + metrics->width = 1.25f; + break; + case 8: + metrics->width = 1.5f; + break; + case 9: + metrics->width = 2.0f; + break; + } + } + + metrics->strikeout_position = short(os2_table->yStrikeoutPosition); + metrics->strikeout_thickness = short(os2_table->yStrikeoutSize); + + metrics->subscript_size = short(os2_table->ySubscriptYSize); + metrics->subscript_xoffset = short(os2_table->ySubscriptXOffset); + metrics->subscript_yoffset = short(os2_table->ySubscriptYOffset); + metrics->superscript_size = short(os2_table->ySuperscriptYSize); + metrics->superscript_xoffset = short(os2_table->ySuperscriptXOffset); + metrics->superscript_yoffset = short(os2_table->ySuperscriptYOffset); + + metrics->family_class = short(os2_table->sFamilyClass); + metrics->panose_family_type = char(os2_table->panose[0]); + metrics->panose_serif_style = char(os2_table->panose[1]); + metrics->panose_weight = char(os2_table->panose[2]); + metrics->panose_proportion = char(os2_table->panose[3]); + metrics->panose_contrast = char(os2_table->panose[4]); + metrics->panose_stroke_variation = char(os2_table->panose[5]); + metrics->panose_arm_style = char(os2_table->panose[6]); + metrics->panose_letter_form = char(os2_table->panose[7]); + metrics->panose_midline = char(os2_table->panose[7]); + metrics->panose_xheight = char(os2_table->panose[9]); + metrics->selection_flags = short(os2_table->fsSelection); + metrics->first_charindex = short(os2_table->usFirstCharIndex); + metrics->last_charindex = short(os2_table->usLastCharIndex); + metrics->typo_linegap = short(os2_table->sTypoLineGap); + if (os2_table->version > 1) { + metrics->cap_height = short(os2_table->sCapHeight); + metrics->x_height = short(os2_table->sxHeight); + } + } + + /* The Post table usually contains the slant value, though in counter-clockwise degrees. */ + TT_Postscript *post_table = (TT_Postscript *)FT_Get_Sfnt_Table(face, FT_SFNT_POST); + if (post_table) { + if (post_table->italicAngle != 0) { + metrics->slant = -float(post_table->italicAngle) / -65536.0f; + } + } + + metrics->units_per_EM = short(face->units_per_EM); + metrics->ascender = short(face->ascender); + metrics->descender = short(face->descender); + metrics->height = short(face->height); + metrics->max_advance_width = short(face->max_advance_width); + metrics->max_advance_height = short(face->max_advance_height); + metrics->underline_position = short(face->underline_position); + metrics->underline_thickness = short(face->underline_thickness); + + metrics->num_glyphs = int(face->num_glyphs); + metrics->bounding_box.xmin = int(face->bbox.xMin); + metrics->bounding_box.xmax = int(face->bbox.xMax); + metrics->bounding_box.ymin = int(face->bbox.yMin); + metrics->bounding_box.ymax = int(face->bbox.yMax); + + if (metrics->cap_height == 0) { + /* Calculate or guess cap height if it is not set in the font. */ + FT_UInt gi = FT_Get_Char_Index(face, uint('H')); + if (gi && FT_Load_Glyph(face, gi, FT_LOAD_NO_SCALE | FT_LOAD_NO_BITMAP) == FT_Err_Ok) { + metrics->cap_height = short(face->glyph->metrics.height); + } + else { + metrics->cap_height = short(float(metrics->units_per_EM) * 0.7f); + } + } + + if (metrics->x_height == 0) { + /* Calculate or guess x-height if it is not set in the font. */ + FT_UInt gi = FT_Get_Char_Index(face, uint('x')); + if (gi && FT_Load_Glyph(face, gi, FT_LOAD_NO_SCALE | FT_LOAD_NO_BITMAP) == FT_Err_Ok) { + metrics->x_height = short(face->glyph->metrics.height); + } + else { + metrics->x_height = short(float(metrics->units_per_EM) * 0.5f); + } + } + + if (metrics->ascender == 0) { + /* Set a sane value for ascender if not set in the font. */ + metrics->ascender = short(float(metrics->units_per_EM) * 0.8f); + } + + if (metrics->descender == 0) { + /* Set a sane value for descender if not set in the font. */ + metrics->descender = metrics->ascender - metrics->units_per_EM; + } + + if (metrics->weight == 400 && face->style_flags & FT_STYLE_FLAG_BOLD) { + /* Normal weight yet this is an bold font, so set a sane weight value. */ + metrics->weight = 700; + } + + if (metrics->slant == 0.0f && face->style_flags & FT_STYLE_FLAG_ITALIC) { + /* No slant yet this is an italic font, so set a sane slant value. */ + metrics->slant = 8.0f; + } + + if (metrics->underline_position == 0) { + metrics->underline_position = short(float(metrics->units_per_EM) * -0.2f); + } + + if (metrics->underline_thickness == 0) { + metrics->underline_thickness = short(float(metrics->units_per_EM) * 0.07f); + } + + if (metrics->strikeout_position == 0) { + metrics->strikeout_position = short(float(metrics->x_height) * 0.6f); + } + + if (metrics->strikeout_thickness == 0) { + metrics->strikeout_thickness = metrics->underline_thickness; + } + + if (metrics->subscript_size == 0) { + metrics->subscript_size = short(float(metrics->units_per_EM) * 0.6f); + } + + if (metrics->subscript_yoffset == 0) { + metrics->subscript_yoffset = short(float(metrics->units_per_EM) * 0.075f); + } + + if (metrics->superscript_size == 0) { + metrics->superscript_size = short(float(metrics->units_per_EM) * 0.6f); + } + + if (metrics->superscript_yoffset == 0) { + metrics->superscript_yoffset = short(float(metrics->units_per_EM) * 0.35f); + } } bool blf_ensure_face(FontBLF *font) @@ -1435,6 +1606,8 @@ bool blf_ensure_face(FontBLF *font) FT_Get_MM_Var(font->face, &(font->variations)); } + blf_font_metrics(font->face, &font->metrics); + /* Save TrueType table with bits to quickly test most unicode block coverage. */ TT_OS2 *os2_table = (TT_OS2 *)FT_Get_Sfnt_Table(font->face, FT_SFNT_OS2); if (os2_table) { @@ -1442,134 +1615,6 @@ bool blf_ensure_face(FontBLF *font) font->unicode_ranges[1] = uint(os2_table->ulUnicodeRange2); font->unicode_ranges[2] = uint(os2_table->ulUnicodeRange3); font->unicode_ranges[3] = uint(os2_table->ulUnicodeRange4); - - /* The default weight. */ - if (os2_table->usWeightClass >= 1 && os2_table->usWeightClass <= 1000) { - font->info.weight = short(os2_table->usWeightClass); - } - - /* Width value is just integers 1-9 with known values. */ - if (os2_table->usWidthClass >= 1 && os2_table->usWidthClass <= 9) { - switch (os2_table->usWidthClass) { - case 1: - font->info.width = 0.5f; - break; - case 2: - font->info.width = 0.625f; - break; - case 3: - font->info.width = 0.75f; - break; - case 4: - font->info.width = 0.875f; - break; - case 5: - font->info.width = 1.0f; - break; - case 6: - font->info.width = 1.125f; - break; - case 7: - font->info.width = 1.25f; - break; - case 8: - font->info.width = 1.5f; - break; - case 9: - font->info.width = 2.0f; - break; - } - } - - /* This table contains most of the font metrics. */ - font->info.family_class = short(os2_table->sFamilyClass); - font->info.panose_family_type = char(os2_table->panose[0]); - font->info.panose_serif_style = char(os2_table->panose[1]); - font->info.panose_weight = char(os2_table->panose[2]); - font->info.panose_proportion = char(os2_table->panose[3]); - font->info.panose_contrast = char(os2_table->panose[4]); - font->info.panose_stroke_variation = char(os2_table->panose[5]); - font->info.panose_arm_style = char(os2_table->panose[6]); - font->info.panose_letter_form = char(os2_table->panose[7]); - font->info.panose_midline = char(os2_table->panose[7]); - font->info.panose_xheight = char(os2_table->panose[9]); - font->info.selection_flags = short(os2_table->fsSelection); - font->info.first_charindex = short(os2_table->usFirstCharIndex); - font->info.last_charindex = short(os2_table->usLastCharIndex); - font->info.typo_linegap = short(os2_table->sTypoLineGap); - if (os2_table->version > 1) { - font->info.cap_height = short(os2_table->sCapHeight); - font->info.x_height = short(os2_table->sxHeight); - } - if (os2_table->version > 4) { - font->info.lower_optical_point_size = short(os2_table->usLowerOpticalPointSize); - font->info.upper_optical_point_size = short(os2_table->usUpperOpticalPointSize); - } - } - - /* The Post table usually contains the slant value, though in counter-clockwise degrees. */ - TT_Postscript *post_table = (TT_Postscript *)FT_Get_Sfnt_Table(font->face, FT_SFNT_POST); - if (post_table) { - if (post_table->italicAngle != 0) { - font->info.slant = -float(post_table->italicAngle) / -65536.0f; - } - } - - /* Copy the metrics in case we need them when we don't have the face loaded. */ - font->info.units_per_EM = short(font->face->units_per_EM); - font->info.ascender = short(font->face->ascender); - font->info.descender = short(font->face->descender); - font->info.height = short(font->face->height); - font->info.max_advance_width = short(font->face->max_advance_width); - font->info.max_advance_height = short(font->face->max_advance_height); - font->info.underline_position = short(font->face->underline_position); - font->info.underline_height = short(font->face->underline_thickness); - font->info.num_glyphs = int(font->face->num_glyphs); - font->info.bounding_box.xmin = int(font->face->bbox.xMin); - font->info.bounding_box.xmax = int(font->face->bbox.xMax); - font->info.bounding_box.ymin = int(font->face->bbox.yMin); - font->info.bounding_box.ymax = int(font->face->bbox.yMax); - - if (font->info.ascender == 0) { - /* Set a sane value for ascender if not set in the font. */ - font->info.ascender = short(float(font->info.units_per_EM) * 0.8f); - } - - if (font->info.descender == 0) { - /* Set a sane value for descender if not set in the font. */ - font->info.descender = font->info.ascender - font->info.units_per_EM; - } - - if (font->info.cap_height == 0) { - /* Calculate or guess cap height if it is not set in the font. */ - FT_UInt gi = blf_get_char_index(font, uint('H')); - if (gi && FT_Load_Glyph(font->face, gi, FT_LOAD_NO_SCALE | FT_LOAD_NO_BITMAP) == FT_Err_Ok) { - font->info.cap_height = short(font->face->glyph->metrics.height); - } - else { - font->info.cap_height = short(float(font->info.units_per_EM) * 0.7f); - } - } - - if (font->info.x_height == 0) { - /* Calculate or guess x-height if it is not set in the font. */ - FT_UInt gi = blf_get_char_index(font, uint('x')); - if (gi && FT_Load_Glyph(font->face, gi, FT_LOAD_NO_SCALE | FT_LOAD_NO_BITMAP) == FT_Err_Ok) { - font->info.x_height = short(font->face->glyph->metrics.height); - } - else { - font->info.x_height = short(float(font->info.units_per_EM) * 0.5f); - } - } - - if (font->info.weight == 400 && font->face->style_flags & FT_STYLE_FLAG_BOLD) { - /* Normal weight yet this is an bold font, so set a sane weight value. */ - font->info.weight = 700; - } - - if (font->info.slant == 0.0f && font->face->style_flags & FT_STYLE_FLAG_ITALIC) { - /* No known slant yet this is an italic font, so set a sane slant value. */ - font->info.slant = 8.0f; } if (FT_IS_FIXED_WIDTH(font)) { diff --git a/source/blender/blenfont/intern/blf_glyph.cc b/source/blender/blenfont/intern/blf_glyph.cc index 8046d101626..74e8baa81fb 100644 --- a/source/blender/blenfont/intern/blf_glyph.cc +++ b/source/blender/blenfont/intern/blf_glyph.cc @@ -1067,10 +1067,10 @@ static FT_GlyphSlot blf_glyph_render(FontBLF *settings_font, blf_ensure_size(glyph_font); /* Current (base) style values. */ - float weight = settings_font->info.weight; - float slant = settings_font->info.slant; - float width = settings_font->info.width; - float spacing = settings_font->info.spacing; + float weight = settings_font->metrics.weight; + float slant = settings_font->metrics.slant; + float width = settings_font->metrics.width; + float spacing = settings_font->metrics.spacing; /* The variation targets we are hoping to get. */ float weight_target = float(settings_font->char_weight); diff --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h index fb81dcc6453..5c0c20890bb 100644 --- a/source/blender/blenfont/intern/blf_internal_types.h +++ b/source/blender/blenfont/intern/blf_internal_types.h @@ -209,7 +209,7 @@ typedef struct FontBufInfoBLF { } FontBufInfoBLF; -typedef struct FontInfo { +typedef struct FontMetrics { short weight; float width; float slant; @@ -221,8 +221,18 @@ typedef struct FontInfo { short height; short max_advance_width; short max_advance_height; + short underline_position; - short underline_height; + short underline_thickness; + short strikeout_position; + short strikeout_thickness; + + short subscript_size; + short subscript_xoffset; + short subscript_yoffset; + short superscript_size; + short superscript_xoffset; + short superscript_yoffset; int num_glyphs; @@ -248,14 +258,7 @@ typedef struct FontInfo { short x_height; short cap_height; - - short lower_optical_point_size; - short upper_optical_point_size; - - // move unicode range bits here too - // CodePageRangebits - -} FontInfo; +} FontMetrics; typedef struct FontBLF { /** Full path to font file or NULL if from memory. */ @@ -355,7 +358,7 @@ typedef struct FontBLF { /** Copy of the font->face->face_flags, in case we don't have a face loaded. */ FT_Long face_flags; - FontInfo info; + FontMetrics metrics; /** Data for buffer usage (drawing into a texture buffer) */ FontBufInfoBLF buf_info; -- 2.30.2 From acca3db398204f7a976fff071cf612a31c215cca Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Sat, 7 Oct 2023 10:40:19 -0700 Subject: [PATCH 10/23] Misplaced semicolon --- source/blender/blenfont/intern/blf_internal_types.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h index 5c0c20890bb..804eaa6917e 100644 --- a/source/blender/blenfont/intern/blf_internal_types.h +++ b/source/blender/blenfont/intern/blf_internal_types.h @@ -329,7 +329,7 @@ typedef struct FontBLF { int char_weight; /* 100 - 900, 400 = normal. */ float char_slant; /* Slant in clockwise degrees. 0.0 = upright. */ float char_width; /* Factor of normal character width. 1.0 = normal. */ - float char_spacing /* Factor of normal normal spacing. 1.0 = normal. */; + float char_spacing; /* Factor of normal normal spacing. 1.0 = normal. */ /** Max texture size. */ int tex_size_max; -- 2.30.2 From f8088027cf2b3141014139875df3a312bd66e241 Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Sat, 7 Oct 2023 17:47:40 -0700 Subject: [PATCH 11/23] Starting point is glyph font, not source font. Cleanup. --- source/blender/blenfont/intern/blf_font.cc | 11 ------ source/blender/blenfont/intern/blf_glyph.cc | 12 +++---- .../blenfont/intern/blf_internal_types.h | 35 +++++-------------- 3 files changed, 15 insertions(+), 43 deletions(-) diff --git a/source/blender/blenfont/intern/blf_font.cc b/source/blender/blenfont/intern/blf_font.cc index cfa20cc311d..f9349f90fcb 100644 --- a/source/blender/blenfont/intern/blf_font.cc +++ b/source/blender/blenfont/intern/blf_font.cc @@ -1401,20 +1401,9 @@ static void blf_font_metrics(FT_Face face, FontMetrics *metrics) metrics->superscript_yoffset = short(os2_table->ySuperscriptYOffset); metrics->family_class = short(os2_table->sFamilyClass); - metrics->panose_family_type = char(os2_table->panose[0]); - metrics->panose_serif_style = char(os2_table->panose[1]); - metrics->panose_weight = char(os2_table->panose[2]); - metrics->panose_proportion = char(os2_table->panose[3]); - metrics->panose_contrast = char(os2_table->panose[4]); - metrics->panose_stroke_variation = char(os2_table->panose[5]); - metrics->panose_arm_style = char(os2_table->panose[6]); - metrics->panose_letter_form = char(os2_table->panose[7]); - metrics->panose_midline = char(os2_table->panose[7]); - metrics->panose_xheight = char(os2_table->panose[9]); metrics->selection_flags = short(os2_table->fsSelection); metrics->first_charindex = short(os2_table->usFirstCharIndex); metrics->last_charindex = short(os2_table->usLastCharIndex); - metrics->typo_linegap = short(os2_table->sTypoLineGap); if (os2_table->version > 1) { metrics->cap_height = short(os2_table->sCapHeight); metrics->x_height = short(os2_table->sxHeight); diff --git a/source/blender/blenfont/intern/blf_glyph.cc b/source/blender/blenfont/intern/blf_glyph.cc index 74e8baa81fb..197df1ae9c9 100644 --- a/source/blender/blenfont/intern/blf_glyph.cc +++ b/source/blender/blenfont/intern/blf_glyph.cc @@ -1066,13 +1066,13 @@ static FT_GlyphSlot blf_glyph_render(FontBLF *settings_font, blf_ensure_size(glyph_font); - /* Current (base) style values. */ - float weight = settings_font->metrics.weight; - float slant = settings_font->metrics.slant; - float width = settings_font->metrics.width; - float spacing = settings_font->metrics.spacing; + /* Default style values of the font containing the glyph. */ + float weight = glyph_font->metrics.weight; + float slant = glyph_font->metrics.slant; + float width = glyph_font->metrics.width; + float spacing = glyph_font->metrics.spacing; - /* The variation targets we are hoping to get. */ + /* Style targets are on the settings_font. */ float weight_target = float(settings_font->char_weight); if (settings_font->flags & BLF_BOLD) { weight_target = MIN2(weight_target + 300.0f, 900.0f); diff --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h index 804eaa6917e..1a775ddbf3b 100644 --- a/source/blender/blenfont/intern/blf_internal_types.h +++ b/source/blender/blenfont/intern/blf_internal_types.h @@ -216,9 +216,18 @@ typedef struct FontMetrics { float spacing; short units_per_EM; + short family_class; + short selection_flags; + int num_glyphs; + short first_charindex; + short last_charindex; + + rcti bounding_box; short ascender; short descender; short height; + short x_height; + short cap_height; short max_advance_width; short max_advance_height; @@ -226,38 +235,12 @@ typedef struct FontMetrics { short underline_thickness; short strikeout_position; short strikeout_thickness; - short subscript_size; short subscript_xoffset; short subscript_yoffset; short superscript_size; short superscript_xoffset; short superscript_yoffset; - - int num_glyphs; - - rcti bounding_box; - - short family_class; - - char panose_family_type; - char panose_serif_style; - char panose_weight; - char panose_proportion; - char panose_contrast; - char panose_stroke_variation; - char panose_arm_style; - char panose_letter_form; - char panose_midline; - char panose_xheight; - - short selection_flags; - short first_charindex; - short last_charindex; - short typo_linegap; - - short x_height; - short cap_height; } FontMetrics; typedef struct FontBLF { -- 2.30.2 From 8c3d7107c7c531dc6f2cb044a1216052db33e7bb Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Sat, 7 Oct 2023 18:29:57 -0700 Subject: [PATCH 12/23] Accidental double negative on the slant calculation. --- source/blender/blenfont/intern/blf_font.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/blender/blenfont/intern/blf_font.cc b/source/blender/blenfont/intern/blf_font.cc index f9349f90fcb..724dbf5a8e8 100644 --- a/source/blender/blenfont/intern/blf_font.cc +++ b/source/blender/blenfont/intern/blf_font.cc @@ -1414,7 +1414,7 @@ static void blf_font_metrics(FT_Face face, FontMetrics *metrics) TT_Postscript *post_table = (TT_Postscript *)FT_Get_Sfnt_Table(face, FT_SFNT_POST); if (post_table) { if (post_table->italicAngle != 0) { - metrics->slant = -float(post_table->italicAngle) / -65536.0f; + metrics->slant = float(post_table->italicAngle) / -65536.0f; } } -- 2.30.2 From 59e90378fafd567e9b295d59370524d031b216b9 Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Sun, 8 Oct 2023 11:28:46 -0700 Subject: [PATCH 13/23] Piles of comments. --- source/blender/blenfont/intern/blf_font.cc | 2 +- .../blenfont/intern/blf_internal_types.h | 37 ++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/source/blender/blenfont/intern/blf_font.cc b/source/blender/blenfont/intern/blf_font.cc index 724dbf5a8e8..bf5f696dce1 100644 --- a/source/blender/blenfont/intern/blf_font.cc +++ b/source/blender/blenfont/intern/blf_font.cc @@ -1421,7 +1421,7 @@ static void blf_font_metrics(FT_Face face, FontMetrics *metrics) metrics->units_per_EM = short(face->units_per_EM); metrics->ascender = short(face->ascender); metrics->descender = short(face->descender); - metrics->height = short(face->height); + metrics->line_height = short(face->height); metrics->max_advance_width = short(face->max_advance_width); metrics->max_advance_height = short(face->max_advance_height); metrics->underline_position = short(face->underline_position); diff --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h index 1a775ddbf3b..26ef8e5dc69 100644 --- a/source/blender/blenfont/intern/blf_internal_types.h +++ b/source/blender/blenfont/intern/blf_internal_types.h @@ -210,36 +210,69 @@ typedef struct FontBufInfoBLF { } FontBufInfoBLF; typedef struct FontMetrics { + /** This font's default weight, 100-900, 400 is normal. */ short weight; + /** This font's default width, 1 is normal, 2 is twice as wide. */ float width; + /** This font's slant in clockwise degrees, 0 being upright. */ float slant; + /** This font's default spacing, 1 is normal. */ float spacing; - short units_per_EM; + /** Number of font units in an EM square. 2048, 1024, 1000 are typical. */ + short units_per_EM; /* */ + /** Design classification from OS/2 sFamilyClass. */ short family_class; + /** Style classification from OS/2 fsSelection. */ short selection_flags; + /** Total number of glyphs in the font. */ int num_glyphs; + /** Minimum Unicode index, typically 0x0020. */ short first_charindex; + /** Maximum Unicode index, or 0xFFFF if greater than. */ short last_charindex; + /** + * Bounds that can contain every glyph in the font when in default positions. Can used for + * maximum ascender, minimum descender. Can be out by a pixel when hinting. Does not change with + * variation axis changes. */ rcti bounding_box; + /** + * Positive number of font units from baseline to top of typical capitals. Can be slightly more + * than cap height when head serifs, terminals, or apexes extend above cap line. */ short ascender; + /** Negative (!) number of font units from baseline to bottom of letters like "gjpqy". */ short descender; - short height; + /** Positive number of font units between consecutive baselines. */ + short line_height; + /** Font units from baseline to lowercase mean line, typically to top of "x". */ short x_height; + /** Font units from baseline to top of capital letters, specifically "H". */ short cap_height; + /** Font unit maximal horizontal advance for all glyphs in font. Can help with wrapping. */ short max_advance_width; + /** As above but only for vertical layout fonts, otherwise is set to line_height value. */ short max_advance_height; + /** Negative (!) number of font units below baseline to center (!) of unlining stem. */ short underline_position; + /** thickness of the underline in font units. */ short underline_thickness; + /** Positive number of font units above baseline to the top (!) of strikeout stroke. */ short strikeout_position; + /** thickness of the strikeout line in font units. */ short strikeout_thickness; + /** EM size font units of recommended subscript letters. */ short subscript_size; + /** Horizontal offset before first subscript character, typically 0. */ short subscript_xoffset; + /** Postive number of font units above baseline for subscript characters. */ short subscript_yoffset; + /** EM size font units of recommended superscript letters. */ short superscript_size; + /** Horizontal offset before first superscript character, typically 0. */ short superscript_xoffset; + /** Postive (!) number of font units below baseline for subscript characters. */ short superscript_yoffset; } FontMetrics; -- 2.30.2 From 724d657b4ffc7f712fcb8ba91d9a2a081e446225 Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Sun, 8 Oct 2023 11:35:19 -0700 Subject: [PATCH 14/23] A few comment typos. --- source/blender/blenfont/intern/blf_internal_types.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h index 26ef8e5dc69..88120bc9181 100644 --- a/source/blender/blenfont/intern/blf_internal_types.h +++ b/source/blender/blenfont/intern/blf_internal_types.h @@ -233,7 +233,7 @@ typedef struct FontMetrics { short last_charindex; /** - * Bounds that can contain every glyph in the font when in default positions. Can used for + * Bounds that can contain every glyph in the font when in default positions. Can be used for * maximum ascender, minimum descender. Can be out by a pixel when hinting. Does not change with * variation axis changes. */ rcti bounding_box; @@ -249,12 +249,12 @@ typedef struct FontMetrics { short x_height; /** Font units from baseline to top of capital letters, specifically "H". */ short cap_height; - /** Font unit maximal horizontal advance for all glyphs in font. Can help with wrapping. */ + /** Font unit maximum horizontal advance for all glyphs in font. Can help with wrapping. */ short max_advance_width; /** As above but only for vertical layout fonts, otherwise is set to line_height value. */ short max_advance_height; - /** Negative (!) number of font units below baseline to center (!) of unlining stem. */ + /** Negative (!) number of font units below baseline to center (!) of underlining stem. */ short underline_position; /** thickness of the underline in font units. */ short underline_thickness; -- 2.30.2 From efcbce7323323f130b42cd71f85a2c6a4fe67f8c Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Sat, 14 Oct 2023 15:30:58 -0700 Subject: [PATCH 15/23] Changing weight UI factor --- source/blender/makesrna/intern/rna_userdef.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/blender/makesrna/intern/rna_userdef.cc b/source/blender/makesrna/intern/rna_userdef.cc index 9eb697b12b9..0233bfdfd8a 100644 --- a/source/blender/makesrna/intern/rna_userdef.cc +++ b/source/blender/makesrna/intern/rna_userdef.cc @@ -1297,7 +1297,7 @@ static void rna_def_userdef_theme_ui_font_style(BlenderRNA *brna) prop = RNA_def_property(srna, "character_weight", PROP_INT, PROP_NONE); RNA_def_property_int_default(prop, 400); RNA_def_property_range(prop, 100.0f, 900.0f); - RNA_def_property_ui_range(prop, 100.0f, 900.0f, 1, 0); + RNA_def_property_ui_range(prop, 100.0f, 900.0f, 50, 0); RNA_def_property_ui_text( prop, "Character Weight", "Weight of the characters. 100-900, 400 is normal"); RNA_def_property_update(prop, 0, "rna_userdef_text_update"); -- 2.30.2 From 3628b6bb43739a6d2ec603ee99be83473e10db63 Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Fri, 20 Oct 2023 12:46:25 -0700 Subject: [PATCH 16/23] Removing BLF setting of non-weight --- source/blender/blenfont/BLF_api.h | 4 +- source/blender/blenfont/intern/blf.cc | 24 -------- source/blender/blenfont/intern/blf_font.cc | 8 +-- source/blender/blenfont/intern/blf_glyph.cc | 55 +++++++++++++++++-- .../blenfont/intern/blf_internal_types.h | 2 +- .../blenloader/intern/versioning_userdef.cc | 8 --- .../editors/interface/interface_style.cc | 15 ----- source/blender/makesdna/DNA_userdef_types.h | 5 +- source/blender/makesrna/intern/rna_userdef.cc | 21 ------- 9 files changed, 55 insertions(+), 87 deletions(-) diff --git a/source/blender/blenfont/BLF_api.h b/source/blender/blenfont/BLF_api.h index c6e99a43836..d6f8e489ab8 100644 --- a/source/blender/blenfont/BLF_api.h +++ b/source/blender/blenfont/BLF_api.h @@ -85,10 +85,8 @@ void BLF_aspect(int fontid, float x, float y, float z); void BLF_position(int fontid, float x, float y, float z); void BLF_size(int fontid, float size); +/* Weight class: 100 (Thin) - 400 (Normal) - 900 (Heavy). */ void BLF_character_weight(int fontid, int weight); -void BLF_character_slant(int fontid, float degrees); -void BLF_character_width(int fontid, float factor); -void BLF_character_spacing(int fontid, float factor); /* Goal: small but useful color API. */ diff --git a/source/blender/blenfont/intern/blf.cc b/source/blender/blenfont/intern/blf.cc index db3af596dee..d37bdb738fd 100644 --- a/source/blender/blenfont/intern/blf.cc +++ b/source/blender/blenfont/intern/blf.cc @@ -332,30 +332,6 @@ void BLF_character_weight(int fontid, int weight) } } -void BLF_character_slant(int fontid, float degrees) -{ - FontBLF *font = blf_get(fontid); - if (font) { - font->char_slant = degrees; - } -} - -void BLF_character_width(int fontid, float factor) -{ - FontBLF *font = blf_get(fontid); - if (font) { - font->char_width = factor; - } -} - -void BLF_character_spacing(int fontid, float factor) -{ - FontBLF *font = blf_get(fontid); - if (font) { - font->char_spacing = factor; - } -} - void BLF_aspect(int fontid, float x, float y, float z) { FontBLF *font = blf_get(fontid); diff --git a/source/blender/blenfont/intern/blf_font.cc b/source/blender/blenfont/intern/blf_font.cc index c95d15d9224..b02fd20a373 100644 --- a/source/blender/blenfont/intern/blf_font.cc +++ b/source/blender/blenfont/intern/blf_font.cc @@ -1351,7 +1351,7 @@ static void blf_font_fill(FontBLF *font) font->char_weight = 400; font->char_slant = 0.0f; font->char_width = 1.0f; - font->char_spacing = 1.0f; + font->char_spacing = 0.0f; BLI_listbase_clear(&font->cache); font->kerning_cache = nullptr; @@ -1369,10 +1369,6 @@ static void blf_font_fill(FontBLF *font) font->buf_info.col_init[1] = 0; font->buf_info.col_init[2] = 0; font->buf_info.col_init[3] = 0; - - font->metrics.weight = 400; - font->metrics.width = 1.0f; - font->metrics.spacing = 1.0f; } /* Note that the data the following function creates is not yet used. @@ -1382,7 +1378,7 @@ static void blf_font_metrics(FT_Face face, FontMetrics *metrics) /* Members with non-zero defaults. */ metrics->weight = 400; metrics->width = 1.0f; - metrics->spacing = 1.0f; + metrics->spacing = 0.0f; TT_OS2 *os2_table = (TT_OS2 *)FT_Get_Sfnt_Table(face, FT_SFNT_OS2); if (os2_table) { diff --git a/source/blender/blenfont/intern/blf_glyph.cc b/source/blender/blenfont/intern/blf_glyph.cc index 197df1ae9c9..b8b637a7af1 100644 --- a/source/blender/blenfont/intern/blf_glyph.cc +++ b/source/blender/blenfont/intern/blf_glyph.cc @@ -821,11 +821,15 @@ static const FT_Var_Axis *blf_var_axis_by_tag(const FT_MM_Var *variations, /** * Convert a float factor to a fixed-point design coordinate. + * Currently unused because we are only dealing with known axes + * with specific functions, but this would be needed for unregistered, + * custom, or private tags. These are all uppercase tags. * * \param axis: Pointer to a design space axis structure. * \param factor: -1 to 1 with 0 meaning "default" */ -static FT_Fixed blf_factor_to_coordinate(const FT_Var_Axis *axis, const float factor) +[[maybe_unused]] static FT_Fixed blf_factor_to_coordinate(const FT_Var_Axis *axis, + const float factor) { FT_Fixed value = axis->def; if (factor > 0) { @@ -840,13 +844,17 @@ static FT_Fixed blf_factor_to_coordinate(const FT_Var_Axis *axis, const float fa } /** - * Alter a face variation axis by a factor + * Alter a face variation axis by a factor. + * Currently unused because we are only dealing with known axes + * with specific functions, but this would be needed for unregistered, + * custom, or private tags. These are all uppercase tags. * * \param coords: array of design coordinates, per axis. * \param tag: Axis tag, e.g. #BLF_VARIATION_AXIS_WEIGHT. - * \param factor: -1 to 1 with 0 meaning "default" + * \param factor: -1 to 1 with 0 meaning "default". + * \return success if able to set this value. */ -static bool blf_glyph_set_variation_normalized(const FontBLF *font, +[[maybe_unused]] static bool blf_glyph_set_variation_normalized(const FontBLF *font, FT_Fixed coords[], const uint32_t tag, const float factor) @@ -866,6 +874,7 @@ static bool blf_glyph_set_variation_normalized(const FontBLF *font, * \param coords: Array of design coordinates, per axis. * \param tag: Axis tag, e.g. #BLF_VARIATION_AXIS_OPTSIZE. * \param value: New float value. Converted to 16.16 and clamped within allowed range. + * \return success if able to set this value. */ static bool blf_glyph_set_variation_float(FontBLF *font, FT_Fixed coords[], @@ -884,6 +893,14 @@ static bool blf_glyph_set_variation_float(FontBLF *font, return false; } + +/** + * Set the "wght" (Weight) axis to a specific weight value. + * + * \param coords: Array of design coordinates, per axis. + * \param weight: Weight class value (1-1000 allowed, 100-900 typical). + * \return value set (could be clamped), or 400 (normal) if the axis does not exist. + */ static float blf_glyph_set_variation_weight(FontBLF *font, FT_Fixed coords[], float weight) { float value = weight; @@ -893,6 +910,13 @@ static float blf_glyph_set_variation_weight(FontBLF *font, FT_Fixed coords[], fl return 400.0f; } +/** + * Set the "slnt" (Slant) axis to a specific slant value. + * + * \param coords: Array of design coordinates, per axis. + * \param degrees: Slant in clockwise (opposite to spec) degrees. + * \return value set (could be clamped), or 0 (upright) if the axis does not exist. + */ static float blf_glyph_set_variation_slant(FontBLF *font, FT_Fixed coords[], float degrees) { float value = -degrees; @@ -902,6 +926,13 @@ static float blf_glyph_set_variation_slant(FontBLF *font, FT_Fixed coords[], flo return 0.0f; } +/** + * Set the "wdth" (Width) axis to a specific width value. + * + * \param coords: Array of design coordinates, per axis. + * \param width: Glyph width value. 1.0 is normal, as per spec (which uses percent). + * \return value set (could be clamped), or 1.0 (normal) if the axis does not exist. + */ static float blf_glyph_set_variation_width(FontBLF *font, FT_Fixed coords[], float width) { float value = width; @@ -911,15 +942,29 @@ static float blf_glyph_set_variation_width(FontBLF *font, FT_Fixed coords[], flo return 1.0f; } +/** + * Set the proposed "spac" (Spacing) axis to a specific value. + * + * \param coords: Array of design coordinates, per axis. + * \param spacing: Glyph spacing value. 0.0 is normal, as per spec. + * \return value set (could be clamped), or 0.0 (normal) if the axis does not exist. + */ static float blf_glyph_set_variation_spacing(FontBLF *font, FT_Fixed coords[], float spacing) { float value = spacing; if (blf_glyph_set_variation_float(font, coords, BLF_VARIATION_AXIS_SPACING, &value)) { return value; } - return 1.0f; + return 0.0f; } +/** + * Set the proposed "opsz" (Optical Size) axis to a specific size value. + * + * \param coords: Array of design coordinates, per axis. + * \param points: Non-zero size in typographic points. + * \return success if able to set this value. + */ static bool blf_glyph_set_variation_optical_size(FontBLF *font, FT_Fixed coords[], const float points) { float value = points; diff --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h index a26ca975048..c4e16d2cfab 100644 --- a/source/blender/blenfont/intern/blf_internal_types.h +++ b/source/blender/blenfont/intern/blf_internal_types.h @@ -347,7 +347,7 @@ typedef struct FontBLF { int char_weight; /* 100 - 900, 400 = normal. */ float char_slant; /* Slant in clockwise degrees. 0.0 = upright. */ float char_width; /* Factor of normal character width. 1.0 = normal. */ - float char_spacing; /* Factor of normal normal spacing. 1.0 = normal. */ + float char_spacing; /* Factor of normal normal spacing. 0.0 = normal. */ /** Max texture size. */ int tex_size_max; diff --git a/source/blender/blenloader/intern/versioning_userdef.cc b/source/blender/blenloader/intern/versioning_userdef.cc index 34598e3b67f..f45424fb054 100644 --- a/source/blender/blenloader/intern/versioning_userdef.cc +++ b/source/blender/blenloader/intern/versioning_userdef.cc @@ -899,17 +899,9 @@ void blo_do_versions_userdef(UserDef *userdef) LISTBASE_FOREACH (uiStyle *, style, &userdef->uistyles) { style->paneltitle.character_weight = 400; - style->paneltitle.character_width = 1.0f; - style->paneltitle.character_spacing = 1.0f; style->grouplabel.character_weight = 400; - style->grouplabel.character_width = 1.0f; - style->grouplabel.character_spacing = 1.0f; style->widgetlabel.character_weight = 400; - style->widgetlabel.character_width = 1.0f; - style->widgetlabel.character_spacing = 1.0f; style->widget.character_weight = 400; - style->widget.character_width = 1.0f; - style->widget.character_spacing = 1.0f; } } diff --git a/source/blender/editors/interface/interface_style.cc b/source/blender/editors/interface/interface_style.cc index 57925db8820..c7a602e7380 100644 --- a/source/blender/editors/interface/interface_style.cc +++ b/source/blender/editors/interface/interface_style.cc @@ -70,9 +70,6 @@ static uiStyle *ui_style_new(ListBase *styles, const char *name, short uifont_id style->paneltitle.uifont_id = uifont_id; style->paneltitle.points = UI_DEFAULT_TITLE_POINTS; style->paneltitle.character_weight = 400; - style->paneltitle.character_width = 1.0f; - style->paneltitle.character_slant = 0.0f; - style->paneltitle.character_spacing = 1.0f; style->paneltitle.shadow = 3; style->paneltitle.shadx = 0; style->paneltitle.shady = -1; @@ -82,9 +79,6 @@ static uiStyle *ui_style_new(ListBase *styles, const char *name, short uifont_id style->grouplabel.uifont_id = uifont_id; style->grouplabel.points = UI_DEFAULT_TITLE_POINTS; style->grouplabel.character_weight = 400; - style->grouplabel.character_width = 1.0f; - style->grouplabel.character_slant = 0.0f; - style->grouplabel.character_spacing = 1.0f; style->grouplabel.shadow = 3; style->grouplabel.shadx = 0; style->grouplabel.shady = -1; @@ -94,9 +88,6 @@ static uiStyle *ui_style_new(ListBase *styles, const char *name, short uifont_id style->widgetlabel.uifont_id = uifont_id; style->widgetlabel.points = UI_DEFAULT_TEXT_POINTS; style->widgetlabel.character_weight = 400; - style->widgetlabel.character_width = 1.0f; - style->widgetlabel.character_slant = 0.0f; - style->widgetlabel.character_spacing = 1.0f; style->widgetlabel.shadow = 3; style->widgetlabel.shadx = 0; style->widgetlabel.shady = -1; @@ -106,9 +97,6 @@ static uiStyle *ui_style_new(ListBase *styles, const char *name, short uifont_id style->widget.uifont_id = uifont_id; style->widget.points = UI_DEFAULT_TEXT_POINTS; style->widget.character_weight = 400; - style->widget.character_width = 1.0f; - style->widget.character_slant = 0.0f; - style->widget.character_spacing = 1.0f; style->widget.shadow = 1; style->widget.shady = -1; style->widget.shadowalpha = 0.5f; @@ -509,9 +497,6 @@ static void fontstyle_set_ex(const uiFontStyle *fs, const float dpi_fac) BLF_size(font->blf_id, fs->points * dpi_fac); BLF_character_weight(fs->uifont_id, fs->character_weight); - BLF_character_slant(fs->uifont_id, fs->character_slant); - BLF_character_width(fs->uifont_id, fs->character_width); - BLF_character_spacing(fs->uifont_id, fs->character_spacing); } void UI_fontstyle_set(const uiFontStyle *fs) diff --git a/source/blender/makesdna/DNA_userdef_types.h b/source/blender/makesdna/DNA_userdef_types.h index d4ba2abbc4c..1479bcaaeae 100644 --- a/source/blender/makesdna/DNA_userdef_types.h +++ b/source/blender/makesdna/DNA_userdef_types.h @@ -81,11 +81,8 @@ typedef struct uiFontStyle { float shadowalpha; /** 1 value, typically white or black anyway. */ float shadowcolor; - char _pad2[4]; + /** Weight class 100-900, 400 is normal. */ int character_weight; - float character_slant; - float character_width; - float character_spacing; } uiFontStyle; /* this is fed to the layout engine and widget code */ diff --git a/source/blender/makesrna/intern/rna_userdef.cc b/source/blender/makesrna/intern/rna_userdef.cc index e13ef18961d..df34e222ceb 100644 --- a/source/blender/makesrna/intern/rna_userdef.cc +++ b/source/blender/makesrna/intern/rna_userdef.cc @@ -1302,27 +1302,6 @@ static void rna_def_userdef_theme_ui_font_style(BlenderRNA *brna) prop, "Character Weight", "Weight of the characters. 100-900, 400 is normal"); RNA_def_property_update(prop, 0, "rna_userdef_text_update"); - prop = RNA_def_property(srna, "character_slant", PROP_FLOAT, PROP_NONE); - RNA_def_property_float_default(prop, 0.0f); - RNA_def_property_range(prop, -45.0f, 45.0f); - RNA_def_property_ui_range(prop, -15.0f, 15.0f, 1.0f, 2); - RNA_def_property_ui_text(prop, "Character Slant", "Slant of the characters in degrees"); - RNA_def_property_update(prop, 0, "rna_userdef_text_update"); - - prop = RNA_def_property(srna, "character_width", PROP_FLOAT, PROP_NONE); - RNA_def_property_float_default(prop, 1.0f); - RNA_def_property_range(prop, 0.01f, 5.0f); - RNA_def_property_ui_range(prop, 0.5f, 2.0f, 1.0, 3); - RNA_def_property_ui_text(prop, "Character Width", "Factor for character width. 1 is normal"); - RNA_def_property_update(prop, 0, "rna_userdef_text_update"); - - prop = RNA_def_property(srna, "character_spacing", PROP_FLOAT, PROP_NONE); - RNA_def_property_float_default(prop, 1.0f); - RNA_def_property_range(prop, 0.00f, 10.0f); - RNA_def_property_ui_range(prop, 0.5f, 5.0f, 1.0, 3); - RNA_def_property_ui_text(prop, "Character Spacing", "Factor for character spacing. 1 is normal"); - RNA_def_property_update(prop, 0, "rna_userdef_text_update"); - prop = RNA_def_property(srna, "shadow", PROP_INT, PROP_PIXEL); RNA_def_property_range(prop, 0, 5); RNA_def_property_ui_text(prop, "Shadow Size", "Shadow size (0, 3 and 5 supported)"); -- 2.30.2 From 0f61667a466be94b77b5c072b7329111dc0b38af Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Fri, 20 Oct 2023 12:50:32 -0700 Subject: [PATCH 17/23] Formatting changes --- source/blender/blenfont/intern/blf.cc | 2 +- source/blender/blenfont/intern/blf_glyph.cc | 11 ++++++----- source/blender/blenfont/intern/blf_internal_types.h | 6 +++--- source/blender/makesrna/intern/rna_userdef.cc | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/source/blender/blenfont/intern/blf.cc b/source/blender/blenfont/intern/blf.cc index d37bdb738fd..1ab4bcb8b19 100644 --- a/source/blender/blenfont/intern/blf.cc +++ b/source/blender/blenfont/intern/blf.cc @@ -324,7 +324,7 @@ void BLF_disable(int fontid, int option) } } -void BLF_character_weight(int fontid, int weight) +void BLF_character_weight(int fontid, int weight) { FontBLF *font = blf_get(fontid); if (font) { diff --git a/source/blender/blenfont/intern/blf_glyph.cc b/source/blender/blenfont/intern/blf_glyph.cc index b8b637a7af1..64e3b07bf3d 100644 --- a/source/blender/blenfont/intern/blf_glyph.cc +++ b/source/blender/blenfont/intern/blf_glyph.cc @@ -855,9 +855,9 @@ static const FT_Var_Axis *blf_var_axis_by_tag(const FT_MM_Var *variations, * \return success if able to set this value. */ [[maybe_unused]] static bool blf_glyph_set_variation_normalized(const FontBLF *font, - FT_Fixed coords[], - const uint32_t tag, - const float factor) + FT_Fixed coords[], + const uint32_t tag, + const float factor) { int axis_index; const FT_Var_Axis *axis = blf_var_axis_by_tag(font->variations, tag, &axis_index); @@ -893,7 +893,6 @@ static bool blf_glyph_set_variation_float(FontBLF *font, return false; } - /** * Set the "wght" (Weight) axis to a specific weight value. * @@ -965,7 +964,9 @@ static float blf_glyph_set_variation_spacing(FontBLF *font, FT_Fixed coords[], f * \param points: Non-zero size in typographic points. * \return success if able to set this value. */ -static bool blf_glyph_set_variation_optical_size(FontBLF *font, FT_Fixed coords[], const float points) +static bool blf_glyph_set_variation_optical_size(FontBLF *font, + FT_Fixed coords[], + const float points) { float value = points; return blf_glyph_set_variation_float(font, coords, BLF_VARIATION_AXIS_OPTSIZE, &value); diff --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h index c4e16d2cfab..5c9b10ed153 100644 --- a/source/blender/blenfont/intern/blf_internal_types.h +++ b/source/blender/blenfont/intern/blf_internal_types.h @@ -344,9 +344,9 @@ typedef struct FontBLF { FT_MM_Var *variations; /** Character variation in 16.16 format. */ - int char_weight; /* 100 - 900, 400 = normal. */ - float char_slant; /* Slant in clockwise degrees. 0.0 = upright. */ - float char_width; /* Factor of normal character width. 1.0 = normal. */ + int char_weight; /* 100 - 900, 400 = normal. */ + float char_slant; /* Slant in clockwise degrees. 0.0 = upright. */ + float char_width; /* Factor of normal character width. 1.0 = normal. */ float char_spacing; /* Factor of normal normal spacing. 0.0 = normal. */ /** Max texture size. */ diff --git a/source/blender/makesrna/intern/rna_userdef.cc b/source/blender/makesrna/intern/rna_userdef.cc index df34e222ceb..7f6e35a4e29 100644 --- a/source/blender/makesrna/intern/rna_userdef.cc +++ b/source/blender/makesrna/intern/rna_userdef.cc @@ -1294,7 +1294,7 @@ static void rna_def_userdef_theme_ui_font_style(BlenderRNA *brna) RNA_def_property_ui_text(prop, "Points", "Font size in points"); RNA_def_property_update(prop, 0, "rna_userdef_gpu_update"); - prop = RNA_def_property(srna, "character_weight", PROP_INT, PROP_NONE); + prop = RNA_def_property(srna, "character_weight", PROP_INT, PROP_NONE); RNA_def_property_int_default(prop, 400); RNA_def_property_range(prop, 100.0f, 900.0f); RNA_def_property_ui_range(prop, 100.0f, 900.0f, 50, 0); -- 2.30.2 From 8d3c0d3452c05e2c0abd83676a940742585e3ad2 Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Fri, 20 Oct 2023 13:04:50 -0700 Subject: [PATCH 18/23] Cleanup, improved comments --- source/blender/blenfont/BLF_api.h | 4 +++- source/blender/blenfont/intern/blf_font.cc | 2 -- source/blender/blenfont/intern/blf_glyph.cc | 11 +++++------ source/blender/blenfont/intern/blf_internal_types.h | 2 +- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/source/blender/blenfont/BLF_api.h b/source/blender/blenfont/BLF_api.h index d6f8e489ab8..9838835349c 100644 --- a/source/blender/blenfont/BLF_api.h +++ b/source/blender/blenfont/BLF_api.h @@ -85,7 +85,9 @@ void BLF_aspect(int fontid, float x, float y, float z); void BLF_position(int fontid, float x, float y, float z); void BLF_size(int fontid, float size); -/* Weight class: 100 (Thin) - 400 (Normal) - 900 (Heavy). */ +/** + * Weight class: 100 (Thin) - 400 (Normal) - 900 (Heavy). + */ void BLF_character_weight(int fontid, int weight); /* Goal: small but useful color API. */ diff --git a/source/blender/blenfont/intern/blf_font.cc b/source/blender/blenfont/intern/blf_font.cc index b02fd20a373..e3731e12685 100644 --- a/source/blender/blenfont/intern/blf_font.cc +++ b/source/blender/blenfont/intern/blf_font.cc @@ -1347,7 +1347,6 @@ static void blf_font_fill(FontBLF *font) font->clip_rec.ymax = 0; font->flags = 0; font->size = 0; - font->char_weight = 400; font->char_slant = 0.0f; font->char_width = 1.0f; @@ -1378,7 +1377,6 @@ static void blf_font_metrics(FT_Face face, FontMetrics *metrics) /* Members with non-zero defaults. */ metrics->weight = 400; metrics->width = 1.0f; - metrics->spacing = 0.0f; TT_OS2 *os2_table = (TT_OS2 *)FT_Get_Sfnt_Table(face, FT_SFNT_OS2); if (os2_table) { diff --git a/source/blender/blenfont/intern/blf_glyph.cc b/source/blender/blenfont/intern/blf_glyph.cc index 64e3b07bf3d..cd8bb488770 100644 --- a/source/blender/blenfont/intern/blf_glyph.cc +++ b/source/blender/blenfont/intern/blf_glyph.cc @@ -61,9 +61,9 @@ static FT_Fixed to_16dot16(double val) /** * Convert a floating point value to a FreeType 16.16 fixed point value. */ -static float from_16dot16(FT_Fixed val) +static float from_16dot16(FT_Fixed value) { - return float(val) / 65536.0f; + return float(value) / 65536.0f; } /** \} */ @@ -823,7 +823,7 @@ static const FT_Var_Axis *blf_var_axis_by_tag(const FT_MM_Var *variations, * Convert a float factor to a fixed-point design coordinate. * Currently unused because we are only dealing with known axes * with specific functions, but this would be needed for unregistered, - * custom, or private tags. These are all uppercase tags. + * custom, or private tags. These are all uppercase axis tags. * * \param axis: Pointer to a design space axis structure. * \param factor: -1 to 1 with 0 meaning "default" @@ -847,11 +847,11 @@ static const FT_Var_Axis *blf_var_axis_by_tag(const FT_MM_Var *variations, * Alter a face variation axis by a factor. * Currently unused because we are only dealing with known axes * with specific functions, but this would be needed for unregistered, - * custom, or private tags. These are all uppercase tags. + * custom, or private tags. These are all uppercase axis tags. * * \param coords: array of design coordinates, per axis. * \param tag: Axis tag, e.g. #BLF_VARIATION_AXIS_WEIGHT. - * \param factor: -1 to 1 with 0 meaning "default". + * \param factor: -1 to 1 with 0 meaning "default" * \return success if able to set this value. */ [[maybe_unused]] static bool blf_glyph_set_variation_normalized(const FontBLF *font, @@ -1142,7 +1142,6 @@ static FT_GlyphSlot blf_glyph_render(FontBLF *settings_font, slant = blf_glyph_set_variation_slant(glyph_font, coords, slant_target); width = blf_glyph_set_variation_width(glyph_font, coords, width_target); spacing = blf_glyph_set_variation_spacing(glyph_font, coords, spacing_target); - blf_glyph_set_variation_optical_size(glyph_font, coords, settings_font->size); /* Save updated design coordinates. */ diff --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h index 5c9b10ed153..5afd7f171a8 100644 --- a/source/blender/blenfont/intern/blf_internal_types.h +++ b/source/blender/blenfont/intern/blf_internal_types.h @@ -343,7 +343,7 @@ typedef struct FontBLF { /** Axes data for Adobe MM, TrueType GX, or OpenType variation fonts. */ FT_MM_Var *variations; - /** Character variation in 16.16 format. */ + /** Character variations. */ int char_weight; /* 100 - 900, 400 = normal. */ float char_slant; /* Slant in clockwise degrees. 0.0 = upright. */ float char_width; /* Factor of normal character width. 1.0 = normal. */ -- 2.30.2 From 04a42fd6fe7efd1618318d975b2569f38996434e Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Fri, 20 Oct 2023 13:14:28 -0700 Subject: [PATCH 19/23] Comment changes --- source/blender/blenfont/intern/blf_glyph.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/source/blender/blenfont/intern/blf_glyph.cc b/source/blender/blenfont/intern/blf_glyph.cc index cd8bb488770..da914689616 100644 --- a/source/blender/blenfont/intern/blf_glyph.cc +++ b/source/blender/blenfont/intern/blf_glyph.cc @@ -1006,10 +1006,10 @@ static bool blf_glyph_transform_weight(FT_GlyphSlot glyph, float width, bool mon } /** - * Adjust the glyph's slant by a factor. + * Adjust the glyph's slant by a number of degrees. * Used for fonts without #BLF_VARIATION_AXIS_SLANT variable axis. * - * \param degrees: amount of tilt in clockwise degrees. + * \param degrees: amount of tilt to add in clockwise degrees. */ static bool blf_glyph_transform_slant(FT_GlyphSlot glyph, float degrees) { @@ -1017,6 +1017,7 @@ static bool blf_glyph_transform_slant(FT_GlyphSlot glyph, float degrees) FT_Matrix transform = {to_16dot16(1), to_16dot16(degrees * 0.0225f), 0, to_16dot16(1)}; FT_Outline_Transform(&glyph->outline, &transform); if (degrees < 0.0f) { + /* Leftward slant could interfere with prior characters to nudge right. */ const FontBLF *font = (FontBLF *)glyph->face->generic.data; const FT_Pos average_width = font->ft_size->metrics.height; FT_Pos change = (FT_Pos)(float(average_width) * degrees * -0.01f); -- 2.30.2 From f313e753f5e439694690884bd9ebccad823722be Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Fri, 20 Oct 2023 13:27:12 -0700 Subject: [PATCH 20/23] With version bump --- source/blender/blenkernel/BKE_blender_version.h | 2 +- .../blenloader/intern/versioning_userdef.cc | 16 +++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/source/blender/blenkernel/BKE_blender_version.h b/source/blender/blenkernel/BKE_blender_version.h index eabb647dfd6..960e1922aad 100644 --- a/source/blender/blenkernel/BKE_blender_version.h +++ b/source/blender/blenkernel/BKE_blender_version.h @@ -29,7 +29,7 @@ extern "C" { /* Blender file format version. */ #define BLENDER_FILE_VERSION BLENDER_VERSION -#define BLENDER_FILE_SUBVERSION 2 +#define BLENDER_FILE_SUBVERSION 3 /* Minimum Blender version that supports reading file written with the current * version. Older Blender versions will test this and cancel loading the file, showing a warning to diff --git a/source/blender/blenloader/intern/versioning_userdef.cc b/source/blender/blenloader/intern/versioning_userdef.cc index f45424fb054..ad3dcfcbf73 100644 --- a/source/blender/blenloader/intern/versioning_userdef.cc +++ b/source/blender/blenloader/intern/versioning_userdef.cc @@ -885,6 +885,15 @@ void blo_do_versions_userdef(UserDef *userdef) userdef->text_render |= USER_TEXT_RENDER_SUBPIXELAA; } + if (!USER_VERSION_ATLEAST(401, 3)) { + LISTBASE_FOREACH (uiStyle *, style, &userdef->uistyles) { + style->paneltitle.character_weight = 400; + style->grouplabel.character_weight = 400; + style->widgetlabel.character_weight = 400; + style->widget.character_weight = 400; + } + } + /** * Versioning code until next subversion bump goes here. * @@ -896,13 +905,6 @@ void blo_do_versions_userdef(UserDef *userdef) */ { /* Keep this block, even when empty. */ - - LISTBASE_FOREACH (uiStyle *, style, &userdef->uistyles) { - style->paneltitle.character_weight = 400; - style->grouplabel.character_weight = 400; - style->widgetlabel.character_weight = 400; - style->widget.character_weight = 400; - } } LISTBASE_FOREACH (bTheme *, btheme, &userdef->themes) { -- 2.30.2 From 6e686042c1b0786ec968aa4caf399387767fab03 Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Fri, 20 Oct 2023 14:19:28 -0700 Subject: [PATCH 21/23] Variation "wdth" axis has 100 as normal. --- source/blender/blenfont/intern/blf_glyph.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/blender/blenfont/intern/blf_glyph.cc b/source/blender/blenfont/intern/blf_glyph.cc index da914689616..964041c1bd7 100644 --- a/source/blender/blenfont/intern/blf_glyph.cc +++ b/source/blender/blenfont/intern/blf_glyph.cc @@ -934,9 +934,9 @@ static float blf_glyph_set_variation_slant(FontBLF *font, FT_Fixed coords[], flo */ static float blf_glyph_set_variation_width(FontBLF *font, FT_Fixed coords[], float width) { - float value = width; + float value = width * 100.0f; if (blf_glyph_set_variation_float(font, coords, BLF_VARIATION_AXIS_WIDTH, &value)) { - return value; + return value / 100.0f; } return 1.0f; } -- 2.30.2 From 02a95bc8f4a37e3945d98745d570cb3f96debfa4 Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Fri, 20 Oct 2023 15:01:31 -0700 Subject: [PATCH 22/23] Ensure we don't un-italicize variable or normal italic fonts. --- source/blender/blenfont/intern/blf_font.cc | 5 ++ source/blender/blenfont/intern/blf_glyph.cc | 52 +++++++++++++-------- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/source/blender/blenfont/intern/blf_font.cc b/source/blender/blenfont/intern/blf_font.cc index e3731e12685..fa2a9b0f581 100644 --- a/source/blender/blenfont/intern/blf_font.cc +++ b/source/blender/blenfont/intern/blf_font.cc @@ -1629,6 +1629,11 @@ bool blf_ensure_face(FontBLF *font) blf_ensure_size(font); blf_font_metrics(font->face, &font->metrics); + font->char_weight = font->metrics.weight; + font->char_slant = font->metrics.slant; + font->char_width = font->metrics.width; + font->char_spacing = font->metrics.spacing; + /* Save TrueType table with bits to quickly test most unicode block coverage. */ TT_OS2 *os2_table = (TT_OS2 *)FT_Get_Sfnt_Table(font->face, FT_SFNT_OS2); if (os2_table) { diff --git a/source/blender/blenfont/intern/blf_glyph.cc b/source/blender/blenfont/intern/blf_glyph.cc index 964041c1bd7..e22ddb3466f 100644 --- a/source/blender/blenfont/intern/blf_glyph.cc +++ b/source/blender/blenfont/intern/blf_glyph.cc @@ -898,15 +898,18 @@ static bool blf_glyph_set_variation_float(FontBLF *font, * * \param coords: Array of design coordinates, per axis. * \param weight: Weight class value (1-1000 allowed, 100-900 typical). - * \return value set (could be clamped), or 400 (normal) if the axis does not exist. + * \return value set (could be clamped), or current weight if the axis does not exist. */ -static float blf_glyph_set_variation_weight(FontBLF *font, FT_Fixed coords[], float weight) +static float blf_glyph_set_variation_weight(FontBLF *font, + FT_Fixed coords[], + float current_weight, + float target_weight) { - float value = weight; + float value = target_weight; if (blf_glyph_set_variation_float(font, coords, BLF_VARIATION_AXIS_WEIGHT, &value)) { return value; } - return 400.0f; + return current_weight; } /** @@ -914,15 +917,18 @@ static float blf_glyph_set_variation_weight(FontBLF *font, FT_Fixed coords[], fl * * \param coords: Array of design coordinates, per axis. * \param degrees: Slant in clockwise (opposite to spec) degrees. - * \return value set (could be clamped), or 0 (upright) if the axis does not exist. + * \return value set (could be clamped), or current slant if the axis does not exist. */ -static float blf_glyph_set_variation_slant(FontBLF *font, FT_Fixed coords[], float degrees) +static float blf_glyph_set_variation_slant(FontBLF *font, + FT_Fixed coords[], + float current_degrees, + float target_degrees) { - float value = -degrees; + float value = -target_degrees; if (blf_glyph_set_variation_float(font, coords, BLF_VARIATION_AXIS_SLANT, &value)) { return -value; } - return 0.0f; + return current_degrees; } /** @@ -930,15 +936,18 @@ static float blf_glyph_set_variation_slant(FontBLF *font, FT_Fixed coords[], flo * * \param coords: Array of design coordinates, per axis. * \param width: Glyph width value. 1.0 is normal, as per spec (which uses percent). - * \return value set (could be clamped), or 1.0 (normal) if the axis does not exist. + * \return value set (could be clamped), or current width if the axis does not exist. */ -static float blf_glyph_set_variation_width(FontBLF *font, FT_Fixed coords[], float width) +static float blf_glyph_set_variation_width(FontBLF *font, + FT_Fixed coords[], + float current_width, + float target_width) { - float value = width * 100.0f; + float value = target_width * 100.0f; if (blf_glyph_set_variation_float(font, coords, BLF_VARIATION_AXIS_WIDTH, &value)) { return value / 100.0f; } - return 1.0f; + return current_width; } /** @@ -946,15 +955,18 @@ static float blf_glyph_set_variation_width(FontBLF *font, FT_Fixed coords[], flo * * \param coords: Array of design coordinates, per axis. * \param spacing: Glyph spacing value. 0.0 is normal, as per spec. - * \return value set (could be clamped), or 0.0 (normal) if the axis does not exist. + * \return value set (could be clamped), or current spacing if the axis does not exist. */ -static float blf_glyph_set_variation_spacing(FontBLF *font, FT_Fixed coords[], float spacing) +static float blf_glyph_set_variation_spacing(FontBLF *font, + FT_Fixed coords[], + float current_spacing, + float target_spacing) { - float value = spacing; + float value = target_spacing; if (blf_glyph_set_variation_float(font, coords, BLF_VARIATION_AXIS_SPACING, &value)) { return value; } - return 0.0f; + return current_spacing; } /** @@ -1139,10 +1151,10 @@ static FT_GlyphSlot blf_glyph_render(FontBLF *settings_font, FT_Get_Var_Design_Coordinates(glyph_font->face, BLF_VARIATIONS_MAX, &coords[0]); /* Update design coordinates with new values. */ - weight = blf_glyph_set_variation_weight(glyph_font, coords, weight_target); - slant = blf_glyph_set_variation_slant(glyph_font, coords, slant_target); - width = blf_glyph_set_variation_width(glyph_font, coords, width_target); - spacing = blf_glyph_set_variation_spacing(glyph_font, coords, spacing_target); + weight = blf_glyph_set_variation_weight(glyph_font, coords, weight, weight_target); + slant = blf_glyph_set_variation_slant(glyph_font, coords, slant, slant_target); + width = blf_glyph_set_variation_width(glyph_font, coords, width, width_target); + spacing = blf_glyph_set_variation_spacing(glyph_font, coords, spacing, spacing_target); blf_glyph_set_variation_optical_size(glyph_font, coords, settings_font->size); /* Save updated design coordinates. */ -- 2.30.2 From 49338e77185d4d83f74d0adf11ba149a8dc792e9 Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Fri, 20 Oct 2023 15:13:38 -0700 Subject: [PATCH 23/23] small comment change. --- source/blender/blenfont/intern/blf_glyph.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/blender/blenfont/intern/blf_glyph.cc b/source/blender/blenfont/intern/blf_glyph.cc index e22ddb3466f..85865466685 100644 --- a/source/blender/blenfont/intern/blf_glyph.cc +++ b/source/blender/blenfont/intern/blf_glyph.cc @@ -970,7 +970,7 @@ static float blf_glyph_set_variation_spacing(FontBLF *font, } /** - * Set the proposed "opsz" (Optical Size) axis to a specific size value. + * Set the "opsz" (Optical Size) axis to a specific size value. * * \param coords: Array of design coordinates, per axis. * \param points: Non-zero size in typographic points. -- 2.30.2