Skip to content

Commit

Permalink
fix: y offset when textBaseline is bottom
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed May 30, 2022
1 parent 51f6493 commit 26fdc3c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
Binary file added __test__/snapshots/text-baseline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions __test__/text.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,13 @@ test(`measureText with suffix spaces`, async (t) => {
t.is(ctx.measureText(' ').width, widthWithSpace - width)
t.is(ctx.measureText(' ').width, widthWithTwoSpace - width)
})

// https://github.com/Brooooooklyn/canvas/issues/386
test('text-baseline', async (t) => {
const { ctx } = t.context
ctx.font = '48px Iosevka Slab'
ctx.textBaseline = 'bottom'
ctx.fillText('abcdef', 50, 50)
ctx.fillText('abcdefg', 50, 50)
await snapshotImage(t)
})
4 changes: 2 additions & 2 deletions skia-c/skia_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ extern "C"
baseline_offset = -alphabetic_baseline - font_metrics.fAscent;
break;
case CssBaseline::Hanging:
// https://github1s.com/chromium/chromium/blob/HEAD/third_party/blink/renderer/core/html/canvas/text_metrics.cc#L21-L24
// https://github.com/chromium/chromium/blob/104.0.5092.1/third_party/blink/renderer/core/html/canvas/text_metrics.cc#L21-L25
// According to
// http://wiki.apache.org/xmlgraphics-fop/LineLayout/AlignmentHandling
// "FOP (Formatting Objects Processor) puts the hanging baseline at 80% of
Expand All @@ -442,7 +442,7 @@ extern "C"
baseline_offset = -paragraph->getIdeographicBaseline();
break;
case CssBaseline::Bottom:
baseline_offset = -alphabetic_baseline - descent;
baseline_offset = font_metrics.fStrikeoutPosition;
break;
};

Expand Down

0 comments on commit 26fdc3c

Please sign in to comment.