[font/draw] Move slanting to font layer

I moved it to the draw layer in 11.0.0; That was a bad design:
font-funcs should NOT be responsible for synthetic bold & slant.

This design reverts that and makes the font layer apply it,
using a recording.

This is a minor API change from 11.0.0 since hb_draw_state_t::slant_xy
which was introduced in 11.0.0, is removed now. I believe no client
had started using it. It was only relevant to other font-funcs draw
implementations.
This commit is contained in:
Behdad Esfahbod committed 2025-04-22 19:52:21 -06:00
1 parent 9ef82f8926
commit 1a9e21758d
10 files changed
+35 -59

No files matched your search

+2 -16
View File
@@ -523,7 +523,7 @@ impl OutlinePen for HbPen {
}
extern "C" fn _hb_fontations_draw_glyph(
font: *mut hb_font_t,
_font: *mut hb_font_t,
font_data: *mut ::std::os::raw::c_void,
glyph: hb_codepoint_t,
draw_funcs: *mut hb_draw_funcs_t,
@@ -543,21 +543,7 @@ extern "C" fn _hb_fontations_draw_glyph(
};
let draw_settings = DrawSettings::unhinted(*size, location);
let slant = unsafe { hb_font_get_synthetic_slant(font) };
let mut x_scale: i32 = 0;
let mut y_scale: i32 = 0;
unsafe {
hb_font_get_scale(font, &mut x_scale, &mut y_scale);
}
let slant = if y_scale != 0 {
slant as f32 * x_scale as f32 / y_scale as f32
} else {
0.
};
let mut draw_state = hb_draw_state_t {
slant_xy: slant,
..unsafe { std::mem::zeroed() }
};
let mut draw_state = unsafe { std::mem::zeroed::<hb_draw_state_t>() };
let mut pen = HbPen {
x_mult: data.x_mult,
+1 -1
View File
@@ -385,7 +385,7 @@ hb_coretext_draw_glyph (hb_font_t *font,
if (!path)
return;
hb_draw_session_t drawing = {draw_funcs, draw_data, font->slant};
hb_draw_session_t drawing {draw_funcs, draw_data};
CGPathApply (path, &drawing, ct_apply_func);
+1 -1
View File
@@ -238,7 +238,7 @@ public:
GeometrySink(hb_font_t *font,
hb_draw_funcs_t *draw_funcs,
void *draw_data)
: font (font), drawing ({draw_funcs, draw_data, font->slant}) {}
: font (font), drawing ({draw_funcs, draw_data}) {}
virtual ~GeometrySink() {}
+2 -10
View File
@@ -41,16 +41,9 @@ HB_BEGIN_DECLS
* @path_start_y: Y component of the start of current path
* @current_x: X component of current point
* @current_y: Y component of current point
* @slant_xy: Slanting factor for synthetic oblique, Since: 11.0.0
*
* Current drawing state.
*
* The @slant_xy is a slanting factor for synthetic oblique. If the font's
* oblique angle is not 0, this factor is used to slant the drawing. For
* fonts with uniform x and y scales, this factor is calculated as
* tan(oblique_angle). For fonts with non-uniform scales, this factor is
* calculated as tan(oblique_angle) * x_scale / y_scale, or 0 if y_scale is 0.
*
* Since: 4.0.0
**/
typedef struct hb_draw_state_t {
@@ -62,8 +55,6 @@ typedef struct hb_draw_state_t {
float current_x;
float current_y;
float slant_xy;
/*< private >*/
hb_var_num_t reserved1;
hb_var_num_t reserved2;
@@ -71,6 +62,7 @@ typedef struct hb_draw_state_t {
hb_var_num_t reserved4;
hb_var_num_t reserved5;
hb_var_num_t reserved6;
hb_var_num_t reserved7;
} hb_draw_state_t;
/**
@@ -78,7 +70,7 @@ typedef struct hb_draw_state_t {
*
* The default #hb_draw_state_t at the start of glyph drawing.
*/
#define HB_DRAW_STATE_DEFAULT {0, 0.f, 0.f, 0.f, 0.f, 0.f, {0.}, {0.}, {0.}, {0.}, {0.}, {0.}}
#define HB_DRAW_STATE_DEFAULT {0, 0.f, 0.f, 0.f, 0.f, {0}, {0}, {0}, {0}, {0}, {0}, {0}}
/**
+2 -21
View File
@@ -100,9 +100,6 @@ struct hb_draw_funcs_t
{
if (unlikely (st.path_open)) close_path (draw_data, st);
if (st.slant_xy)
to_x += to_y * st.slant_xy;
st.current_x = to_x;
st.current_y = to_y;
}
@@ -114,9 +111,6 @@ struct hb_draw_funcs_t
{
if (unlikely (!st.path_open)) start_path (draw_data, st);
if (st.slant_xy)
to_x += to_y * st.slant_xy;
emit_line_to (draw_data, st, to_x, to_y);
st.current_x = to_x;
@@ -131,12 +125,6 @@ struct hb_draw_funcs_t
{
if (unlikely (!st.path_open)) start_path (draw_data, st);
if (st.slant_xy)
{
control_x += control_y * st.slant_xy;
to_x += to_y * st.slant_xy;
}
emit_quadratic_to (draw_data, st, control_x, control_y, to_x, to_y);
st.current_x = to_x;
@@ -152,13 +140,6 @@ struct hb_draw_funcs_t
{
if (unlikely (!st.path_open)) start_path (draw_data, st);
if (st.slant_xy)
{
control1_x += control1_y * st.slant_xy;
control2_x += control2_y * st.slant_xy;
to_x += to_y * st.slant_xy;
}
emit_cubic_to (draw_data, st, control1_x, control1_y, control2_x, control2_y, to_x, to_y);
st.current_x = to_x;
@@ -194,9 +175,9 @@ DECLARE_NULL_INSTANCE (hb_draw_funcs_t);
struct hb_draw_session_t
{
hb_draw_session_t (hb_draw_funcs_t *funcs_, void *draw_data_, float slant_xy = 0.f)
hb_draw_session_t (hb_draw_funcs_t *funcs_, void *draw_data_)
: funcs {funcs_}, draw_data {draw_data_}, st HB_DRAW_STATE_DEFAULT
{ st.slant_xy = slant_xy; }
{}
~hb_draw_session_t () { close_path (); }
+18 -8
View File
@@ -512,11 +512,13 @@ struct hb_font_t
{
#ifndef HB_NO_OUTLINE
bool embolden = x_strength || y_strength;
bool slanted = slant_xy;
bool synthetic = embolden || slanted;
#else
constexpr bool embolden = false;
constexpr bool synthetic = false;
#endif
if (!embolden)
if (!synthetic)
{
klass->get.f.draw_glyph (this, user_data,
glyph,
@@ -526,18 +528,26 @@ struct hb_font_t
}
#ifndef HB_NO_OUTLINE
/* Emboldening. */
hb_outline_t outline;
klass->get.f.draw_glyph (this, user_data,
glyph,
hb_outline_recording_pen_get_funcs (), &outline,
!klass->user_data ? nullptr : klass->user_data->draw_glyph);
float x_shift = embolden_in_place ? 0 : (float) x_strength / 2;
float y_shift = (float) y_strength / 2;
if (x_scale < 0) x_shift = -x_shift;
if (y_scale < 0) y_shift = -y_shift;
outline.embolden (x_strength, y_strength, x_shift, y_shift);
// Slant before embolden; produces nicer results.
if (slanted)
outline.slant (slant_xy);
if (embolden)
{
float x_shift = embolden_in_place ? 0 : (float) x_strength / 2;
float y_shift = (float) y_strength / 2;
if (x_scale < 0) x_shift = -x_shift;
if (y_scale < 0) y_shift = -y_shift;
outline.embolden (x_strength, y_strength, x_shift, y_shift);
}
outline.replay (draw_funcs, draw_data);
#endif
+1 -1
View File
@@ -877,7 +877,7 @@ hb_ft_draw_glyph (hb_font_t *font,
0, /* delta */
};
hb_draw_session_t draw_session (draw_funcs, draw_data, font->slant_xy);
hb_draw_session_t draw_session {draw_funcs, draw_data};
FT_Outline_Decompose (&ft_face->glyph->outline,
&outline_funcs,
+1 -1
View File
@@ -498,7 +498,7 @@ hb_ot_draw_glyph (hb_font_t *font,
hb_draw_funcs_t *draw_funcs, void *draw_data,
void *user_data)
{
hb_draw_session_t draw_session (draw_funcs, draw_data, font->slant_xy);
hb_draw_session_t draw_session {draw_funcs, draw_data};
#ifndef HB_NO_VAR_COMPOSITES
if (!font->face->table.VARC->get_path (font, glyph, draw_session))
#endif
+6
View File
@@ -84,6 +84,12 @@ void hb_outline_t::replay (hb_draw_funcs_t *pen, void *pen_data) const
}
}
void hb_outline_t::slant (float slant_xy)
{
for (auto &p : points)
p.x += slant_xy * p.y;
}
float hb_outline_t::control_area () const
{
float a = 0;
+1
View File
@@ -69,6 +69,7 @@ struct hb_outline_t
HB_INTERNAL void replay (hb_draw_funcs_t *pen, void *pen_data) const;
HB_INTERNAL float control_area () const;
HB_INTERNAL void slant (float slant_xy);
HB_INTERNAL void embolden (float x_strength, float y_strength,
float x_shift, float y_shift);