fix(font): Anchor scaling at bounding box center

pull/8990/head
Daniel Wennberg 2025-10-01 13:32:46 -07:00 committed by Mitchell Hashimoto
parent b99ca6ad97
commit 7acf617763
1 changed files with 17 additions and 22 deletions

View File

@ -265,15 +265,15 @@ pub const RenderOptions = struct {
}; };
}; };
// The new, constrained glyph size // Apply prescribed scaling, preserving the
var constrained_glyph = glyph; // center bearings of the group bounding box
// Apply prescribed scaling
const width_factor, const height_factor = self.scale_factors(group, metrics, min_constraint_width); const width_factor, const height_factor = self.scale_factors(group, metrics, min_constraint_width);
constrained_glyph.width *= width_factor; const center_x = group.x + (group.width / 2);
constrained_glyph.x *= width_factor; const center_y = group.y + (group.height / 2);
constrained_glyph.height *= height_factor; group.width *= width_factor;
constrained_glyph.y *= height_factor; group.height *= height_factor;
group.x = center_x - (group.width / 2);
group.y = center_y - (group.height / 2);
// NOTE: font_patcher jumps through a lot of hoops at this // NOTE: font_patcher jumps through a lot of hoops at this
// point to ensure that the glyph remains within the target // point to ensure that the glyph remains within the target
@ -283,25 +283,20 @@ pub const RenderOptions = struct {
// Align vertically // Align vertically
if (self.align_vertical != .none) { if (self.align_vertical != .none) {
// Vertically scale group bounding box. group.y += self.offset_vertical(group, metrics);
group.height *= height_factor;
group.y *= height_factor;
// Calculate offset and shift the glyph
constrained_glyph.y += self.offset_vertical(group, metrics);
} }
// Align horizontally // Align horizontally
if (self.align_horizontal != .none) { if (self.align_horizontal != .none) {
// Horizontally scale group bounding box. group.x += self.offset_horizontal(group, metrics, min_constraint_width);
group.width *= width_factor;
group.x *= width_factor;
// Calculate offset and shift the glyph
constrained_glyph.x += self.offset_horizontal(group, metrics, min_constraint_width);
} }
return constrained_glyph; // Transfer the scaling and alignment back to the glyph and return.
return .{
.width = width_factor * glyph.width,
.height = height_factor * glyph.height,
.x = group.x + (group.width * self.relative_x),
.y = group.y + (group.height * self.relative_y),
};
} }
/// Return width and height scaling factors for this scaling group. /// Return width and height scaling factors for this scaling group.