Skip to content

Commit

Permalink
fix: text align and font fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
LongYinan committed Dec 31, 2021
1 parent fb88351 commit d80ac6b
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 103 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ jobs:
target: 'x86_64-unknown-linux-gnu'
downloadTarget: ''
docker: |
docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD $DOCKER_REGISTRY_URL
docker pull ghcr.io/brooooooklyn/canvas/debian-builder:stretch
docker tag ghcr.io/brooooooklyn/canvas/debian-builder:stretch builder
build: |
Expand All @@ -46,7 +45,6 @@ jobs:
downloadTarget: 'x86_64-unknown-linux-musl'
target: 'x86_64-unknown-linux-musl'
docker: |
docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD $DOCKER_REGISTRY_URL
docker pull ghcr.io/brooooooklyn/canvas/musl-builder:lts
docker tag ghcr.io/brooooooklyn/canvas/musl-builder:lts builder
build: docker run --rm -v ~/.cargo/git:/root/.cargo/git -v ~/.cargo/registry:/root/.cargo/registry -v $(pwd):/canvas -w /canvas builder sh -c "yarn build && strip skia.linux-x64-musl.node"
Expand Down Expand Up @@ -74,7 +72,6 @@ jobs:
target: 'aarch64-unknown-linux-musl'
downloadTarget: 'aarch64-unknown-linux-musl'
docker: |
docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD $DOCKER_REGISTRY_URL
docker pull ghcr.io/brooooooklyn/canvas/musl-builder:lts
docker tag ghcr.io/brooooooklyn/canvas/musl-builder:lts builder
build: docker run --rm -v ~/.cargo/git:/root/.cargo/git -v ~/.cargo/registry:/root/.cargo/registry -v $(pwd):/canvas -w /canvas builder sh -c "rustup toolchain install $(cat ./rust-toolchain) && rustup target add aarch64-unknown-linux-musl && pnpm build -- --target=aarch64-unknown-linux-musl && /aarch64-linux-musl-cross/bin/aarch64-linux-musl-strip skia.linux-arm64-musl.node"
Expand Down
39 changes: 39 additions & 0 deletions __test__/echarts.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import test from 'ava'
import { init, setCanvasCreator } from 'echarts'

import { createCanvas } from '../index.js'
import { snapshotImage } from './image-snapshot'

test('echarts-start', async (t) => {
if (process.platform !== 'darwin') {
t.pass()
return
}
const canvas = createCanvas(800, 600)
// @ts-expect-error
setCanvasCreator(() => canvas)
// @ts-expect-error
const chart = init(canvas)
chart.setOption({
title: {
text: 'ECharts 入门示例',
},
tooltip: {},
legend: {
data: ['销量'],
},
xAxis: {
data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子'],
},
yAxis: {},
series: [
{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20],
},
],
})

await snapshotImage(t, { canvas, ctx: canvas.getContext('2d') })
})
Binary file added __test__/snapshots/echarts-start.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 modified __test__/snapshots/text-align-end.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 modified __test__/snapshots/text-align-start.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion __test__/text.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ for (const align of ['center', 'end', 'left', 'right', 'start'] as CanvasTextAli
ctx.stroke()
ctx.textAlign = align
ctx.font = '16px Iosevka Slab'
ctx.fillText('Hello Canvas', 0, 200)
ctx.fillText('Hello Canvas', x, 200)
await snapshotImage(t)
})
}
178 changes: 99 additions & 79 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,89 +1,109 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="/node_modules/canvaskit-wasm/bin/canvaskit.js"></script>
<title>Canvas</title>
</head>

<body>
<style type="text/css">
@font-face {
font-family: 'Iosevka Slab';
src: url('/__test__/fonts/iosevka-slab-regular.ttf');
}
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="/node_modules/canvaskit-wasm/bin/canvaskit.js"></script>
<title>Canvas</title>
</head>

body {
font-family: 'Iosevka Slab';
}
<body>
<style type="text/css">
@font-face {
font-family: 'Iosevka Slab';
src: url('/__test__/fonts/iosevka-slab-regular.ttf');
}

canvas {
margin-top: 100px;
}
</style>
<canvas width="1024" height="768" id="canvas"></canvas>
<canvas width="1024" height="768" id="canvas-text"></canvas>
<canvas width="512" height="512" id="regression"></canvas>
<img width="200" height="200" id="firefox" src="/__test__/fixtures/filter-combine-contrast-brightness.jpeg" />
<script>
setTimeout(() => {
const canvas = document.getElementById('canvas')
/**
* @type {CanvasRenderingContext2D}
*/
const ctx = canvas.getContext('2d')
ctx.fillStyle = 'yellow'
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.strokeStyle = 'black'
ctx.lineWidth = 3
ctx.font = '50px Iosevka Slab'
body {
font-family: 'Iosevka Slab';
}

canvas {
margin-top: 100px;
}
</style>
<canvas width="1024" height="768" id="canvas"></canvas>
<canvas width="1024" height="768" id="canvas-text"></canvas>
<canvas width="512" height="512" id="regression"></canvas>
<canvas width="512" height="512" id="text"></canvas>
<img width="200" height="200" id="firefox" src="/__test__/fixtures/filter-combine-contrast-brightness.jpeg" />
<script>
setTimeout(() => {
const canvas = document.getElementById('canvas')
/**
* @type {CanvasRenderingContext2D}
*/
const ctx = canvas.getContext('2d')
ctx.fillStyle = 'yellow'
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.strokeStyle = 'black'
ctx.lineWidth = 3
ctx.font = '50px Iosevka Slab'

const baselines = ['top', 'hanging', 'middle', 'alphabetic', 'ideographic', 'bottom']
const baselines = ['top', 'hanging', 'middle', 'alphabetic', 'ideographic', 'bottom']

baselines.forEach(function (baseline) {
ctx.textBaseline = baseline
const metrics = ctx.measureText('@napi-rs/canvas')
console.info(baseline, metrics)
})
baselines.forEach(function (baseline) {
ctx.textBaseline = baseline
const metrics = ctx.measureText('@napi-rs/canvas')
console.info(baseline, metrics)
})

ctx.strokeText('skr canvas', 50, 150)
ctx.strokeText('@napi-rs/canvas', 50, 300)
}, 300)
ctx.strokeText('skr canvas', 50, 150)
ctx.strokeText('@napi-rs/canvas', 50, 300)
}, 300)

setTimeout(() => {
const canvas = document.getElementById('canvas-text')
/**
* @type {CanvasRenderingContext2D}
*/
const ctx = canvas.getContext('2d')
ctx.fillStyle = 'yellow'
ctx.fillRect(0, 0, canvas.width, canvas.height)
const baselines = ['top', 'hanging', 'middle', 'alphabetic', 'ideographic', 'bottom']
ctx.font = '36px Iosevka Slab'
ctx.strokeStyle = 'red'
ctx.fillStyle = 'black'
setTimeout(() => {
const canvas = document.getElementById('canvas-text')
/**
* @type {CanvasRenderingContext2D}
*/
const ctx = canvas.getContext('2d')
ctx.fillStyle = 'yellow'
ctx.fillRect(0, 0, canvas.width, canvas.height)
const baselines = ['top', 'hanging', 'middle', 'alphabetic', 'ideographic', 'bottom']
ctx.font = '36px Iosevka Slab'
ctx.strokeStyle = 'red'
ctx.fillStyle = 'black'

baselines.forEach(function (baseline, index) {
ctx.textBaseline = baseline
const y = 75 + index * 75
ctx.beginPath()
ctx.moveTo(0, y + 0.5)
ctx.lineTo(550, y + 0.5)
ctx.stroke()
ctx.fillText('Abcdefghijklmnop (' + baseline + ')', 0, y)
})
}, 300)
</script>
<script>
setTimeout(() => {
const canvas = document.getElementById('regression')
const ctx = canvas.getContext('2d')
ctx.filter = 'contrast(175%) brightness(103%)'
const image = document.querySelector('#firefox')
ctx.drawImage(image, 0, 0)
}, 1000)
</script>

<script>
setTimeout(() => {
const canvas = document.getElementById('text')
const ctx = canvas.getContext('2d')
for (const align of ['center', 'end', 'left', 'right', 'start']) {
const x = canvas.width / 2
ctx.strokeStyle = 'black'
ctx.moveTo(x, 0)
ctx.lineTo(x, canvas.height)
ctx.stroke()
ctx.textAlign = align
ctx.font = '16px Iosevka Slab'
ctx.fillText('Hello Canvas', x, 200)
}
}, 1000)
</script>
</body>

baselines.forEach(function (baseline, index) {
ctx.textBaseline = baseline
const y = 75 + index * 75
ctx.beginPath()
ctx.moveTo(0, y + 0.5)
ctx.lineTo(550, y + 0.5)
ctx.stroke()
ctx.fillText('Abcdefghijklmnop (' + baseline + ')', 0, y)
})
}, 300)
</script>
<script>
setTimeout(() => {
const canvas = document.getElementById('regression')
const ctx = canvas.getContext('2d')
ctx.filter = 'contrast(175%) brightness(103%)'
const image = document.querySelector('#firefox')
ctx.drawImage(image, 0, 0)
}, 1000)
</script>
</body>
</html>
</html>
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"canvaskit-wasm": "^0.32.0",
"chalk": "4",
"conventional-changelog-cli": "^2.1.1",
"echarts": "^5.2.2",
"eslint": "^8.4.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.25.3",
Expand Down
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nightly-2021-11-30
nightly-2021-12-05
19 changes: 13 additions & 6 deletions skia-c/skia_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,18 +445,18 @@ extern "C"

if (c_canvas)
{
auto canvas_center = canvas_width / 2.0f;
auto line_center = line_width / 2.0f;
float paint_x;
switch ((TextAlign)align)
{
case TextAlign::kLeft:
paint_x = x + canvas_center;
paint_x = x;
break;
case TextAlign::kCenter:
paint_x = x + canvas_center - (line_width / 2);
paint_x = x - line_center;
break;
case TextAlign::kRight:
paint_x = x + canvas_center - line_width;
paint_x = x - line_width;
break;
// Unreachable
case TextAlign::kJustify:
Expand All @@ -469,7 +469,7 @@ extern "C"
}
else
{
paint_x = x + canvas_width - line_width;
paint_x = x - line_width;
}
break;
case TextAlign::kEnd:
Expand All @@ -479,7 +479,7 @@ extern "C"
}
else
{
paint_x = x + canvas_width - line_width;
paint_x = x - line_width;
}
break;
};
Expand Down Expand Up @@ -1501,6 +1501,13 @@ extern "C"
return result;
}

void skiac_font_collection_set_alias(skiac_font_collection *c_font_collection, const char *family, const char *alias)
{
auto style = SkFontStyle();
auto typeface = c_font_collection->assets->matchFamilyStyle(family, style);
c_font_collection->assets->registerTypeface(sk_sp(typeface), SkString(alias));
}

void skiac_font_collection_destroy(skiac_font_collection *c_font_collection)
{
delete c_font_collection;
Expand Down
Loading

1 comment on commit d80ac6b

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: d80ac6b Previous: fb88351 Ratio
Draw house#skia-canvas 18.1 ops/sec (±0.38%) 18 ops/sec (±1.7%) 0.99
Draw house#node-canvas 17.9 ops/sec (±0.17%) 21 ops/sec (±1.18%) 1.17
Draw house#@napi-rs/skia 21.4 ops/sec (±0.1%) 20 ops/sec (±0.55%) 0.93
Draw gradient#skia-canvas 17.2 ops/sec (±0.21%) 17 ops/sec (±0.43%) 0.99
Draw gradient#node-canvas 17 ops/sec (±1.56%) 21 ops/sec (±1.05%) 1.24
Draw gradient#@napi-rs/skia 20.5 ops/sec (±0.3%) 19 ops/sec (±1.24%) 0.93

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.