Skip to content

Commit

Permalink
Add more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV committed Apr 14, 2024
1 parent 5841cb8 commit d5a82ae
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions crates/usvg/src/text/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,29 @@ impl PositionedGlyph {
pixels_per_em: f32,
height: f32,
) -> Transform {
// In contrast to CBDT, we also need to look at the outline bbox of the glyph and add a shift if necessary.
let bbox_x_shift = self.font_size * (-x_min / self.units_per_em as f32);

let bbox_y_shift = if y_min.approx_zero_ulps(4) {
// For unknown reasons, using Apple Color Emoji will lead to a vertical shift on MacOS, but this shift
// doesn't seem to be coming from the font and most likely is somehow hardcoded. On Windows,
// this shift will not be applied. However, if this shift is not applied the emojis are a bit
// too high up when being together with other text, so we try to imitate this.
// See also https://github.com/harfbuzz/harfbuzz/issues/2679#issuecomment-1345595425
// So whenever the y-shift is 0, we approximate this vertical shift that seems to be produced by it.
// This value seems to be pretty close to what is happening on MacOS.
// We can still remove this if it turns out to be a problem, but Apple Color Emoji is pretty
// much the only `sbix` font out there and they all seem to have a y-shift of 0, so it
// makes sense to keep it.
0.128 * self.font_size
} else {
self.font_size * (-y_min / self.units_per_em as f32)
};

self.span_ts
.pre_concat(self.cluster_ts)
.pre_concat(self.glyph_ts)
// In contrast to CBDT, we also need to look at the outline bbox of the glyph and add a shift if necessary.
// For unknown reasons, using Apple Color Emoji will lead to a vertical shift on MacOS, but this shift
// doesn't seem to be coming from the font and most likely is somehow hardcoded into CoreText. But we
// ignore this. See also https://github.com/harfbuzz/harfbuzz/issues/2679#issuecomment-1345595425
.pre_concat(Transform::from_translate(
self.font_size * (-x_min / self.units_per_em as f32),
self.font_size * (-y_min / self.units_per_em as f32),
))
.pre_concat(Transform::from_translate(bbox_x_shift, bbox_y_shift))
.pre_concat(Transform::from_scale(
self.font_size / pixels_per_em,
self.font_size / pixels_per_em,
Expand Down

0 comments on commit d5a82ae

Please sign in to comment.