Skip to content

Commit

Permalink
fix: recalc paint_x according to align if maxWidth is set
Browse files Browse the repository at this point in the history
  • Loading branch information
nieyuyao committed Feb 20, 2023
1 parent b072978 commit 1b3ca96
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
46 changes: 46 additions & 0 deletions __test__/regression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,49 @@ test('draw-text-maxWidth', async (t) => {
ctx.fillText(`Very ${'long '.repeat(2)} text`, pad, 80, maxWidth)
await snapshotImage(t, { ctx, canvas })
})

test('draw-text-right-maxWidth', async (t) => {
GlobalFonts.registerFromPath(join(__dirname, 'fonts', 'iosevka-slab-regular.ttf'))
const canvas = createCanvas(500, 100)
const ctx = canvas.getContext('2d')
const padding = 50
const maxWidth = canvas.width - padding * 2
// The background
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.fillStyle = 'blue'
ctx.fillRect(padding, 0, maxWidth, canvas.height)
ctx.font = '16px Arial'
ctx.textAlign = 'right'
ctx.fillStyle = 'white'
ctx.textBaseline = 'top'
/** Short text */
ctx.fillText('Short text', canvas.width - padding, 10, maxWidth)
/** Very long text (10 repetitions) */
ctx.fillText(`Very ${'long '.repeat(10)} text`, canvas.width - padding, 30, maxWidth)
/** Very long text (20 repetitions) */
ctx.fillText(`Very ${'long '.repeat(20)} text`, canvas.width - padding, 50, maxWidth)
await snapshotImage(t, { ctx, canvas })
})

test('draw-text-center-maxWidth', async (t) => {
GlobalFonts.registerFromPath(join(__dirname, 'fonts', 'iosevka-slab-regular.ttf'))
const canvas = createCanvas(500, 100)
const ctx = canvas.getContext('2d')
const padding = 50
const maxWidth = canvas.width - padding * 2
// The background
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.fillStyle = 'blue'
ctx.fillRect(padding, 0, maxWidth, canvas.height)
ctx.font = '16px Arial'
ctx.textAlign = 'center'
ctx.fillStyle = 'white'
ctx.textBaseline = 'top'
/** Short text */
ctx.fillText('Short text', canvas.width / 2, 10, maxWidth)
/** Very long text (10 repetitions) */
ctx.fillText(`Very ${'long '.repeat(10)} text`, canvas.width / 2, 30, maxWidth)
/** Very long text (20 repetitions) */
ctx.fillText(`Very ${'long '.repeat(20)} text`, canvas.width / 2, 50, maxWidth)
await snapshotImage(t, { ctx, canvas })
})
Binary file added __test__/snapshots/draw-text-center-maxWidth.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added __test__/snapshots/draw-text-right-maxWidth.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion skia-c/skia_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,16 +472,19 @@ extern "C"

auto line_center = line_width / 2.0f;
float paint_x;
float offset_x = 0.0;
switch ((TextAlign)align)
{
case TextAlign::kLeft:
paint_x = x;
break;
case TextAlign::kCenter:
paint_x = x - line_center;
offset_x = line_center;
break;
case TextAlign::kRight:
paint_x = x - line_width;
offset_x = line_width;
break;
// Unreachable
case TextAlign::kJustify:
Expand All @@ -495,6 +498,7 @@ extern "C"
else
{
paint_x = x - line_width;
offset_x = line_width;
}
break;
case TextAlign::kEnd:
Expand All @@ -505,6 +509,7 @@ extern "C"
else
{
paint_x = x - line_width;
offset_x = line_width;
}
break;
};
Expand All @@ -519,7 +524,7 @@ extern "C"
CANVAS_CAST->scale(ratio, 1.0);
}
auto paint_y = y + baseline_offset;
paragraph->paint(CANVAS_CAST, need_scale ? paint_x / ratio : paint_x, paint_y);
paragraph->paint(CANVAS_CAST, need_scale ? (paint_x + (1 - ratio) * offset_x) / ratio : paint_x, paint_y);
if (need_scale)
{
CANVAS_CAST->restore();
Expand Down

0 comments on commit 1b3ca96

Please sign in to comment.