Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(platformApi): fix wrong expression for extracting the font size #968

Merged
merged 2 commits into from
Nov 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/core/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ export const platformApi: Platform = {
text = text || '';
font = font || DEFAULT_FONT;
// Use font size if there is no other method can be used.
const res = /^([0-9]*?)px$/.exec(font);
const fontSize = +(res && res[1]) || DEFAULT_FONT_SIZE;
const res = /(\d+)px/.exec(font);
const fontSize = res && +res[1] || DEFAULT_FONT_SIZE;
let width = 0;
if (font.indexOf('mono') >= 0) { // is monospace
width = fontSize * text.length;
Expand All @@ -103,7 +103,7 @@ export const platformApi: Platform = {

export function setPlatformAPI(newPlatformApis: Partial<Platform>) {
for (let key in platformApi) {
// Don't assign unkown methods.
// Don't assign unknown methods.
if ((newPlatformApis as any)[key]) {
(platformApi as any)[key] = (newPlatformApis as any)[key];
}
Expand Down
98 changes: 98 additions & 0 deletions test/ssr-measureText.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Image</title>
<script src="./lib/config.js"></script>
<script src="https://cdn.jsdelivr.net/npm/zrender@5.4.0"></script>
<script>window.zrender540 = zrender;</script>
<script src="../dist/zrender.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body {
overflow: hidden;
}
.painter {
height: 50vh;
}
</style>
</head>
<body>
<script type="text/javascript">
// See also https://github.com/ecomfe/zrender/issues/947
// See also https://github.com/apache/echarts/issues/17326

// mock non-canvas environment
zrender.setPlatformAPI({
createCanvas: zrender.util.noop
});
zrender540.setPlatformAPI({
createCanvas: zrender540.util.noop
});

function createZr(zrender, painterIdx, indication) {
var zr = zrender.init(null, {
renderer: 'svg',
ssr: true,
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight / 2
});

zr.add(new zrender.Text({
style: {
x: 100,
y: 200,
text: (indication || '') + 'ABCDEFG1234567',
fontSize: 18
}
}));

function showBoundingRect() {
zr.storage.traverse(function (el) {
if (el.type === 'text') {
var rect = el.getBoundingRect();

zr.add(new zrender.Rect({
shape: rect,
x: el.x,
y: el.y,
rotation: el.rotation,
scaleX: el.scaleX,
scaleY: el.scaleY,
originX: el.originX,
originY: el.originY,
style: {
fill: null,
stroke: zrender.color.random(),
lineWidth: 1
}
}));
}
});
}

var painter = document.createElement('div');
painter.id = 'painter' + painterIdx;
painter.className = 'painter';
document.body.appendChild(painter);
function paint() {
painter.innerHTML = zr.painter.renderToString();
}

showBoundingRect();
paint();

window.addEventListener('resize', function () {
zr.resize({
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight / 2
});
paint();
});
}

createZr(zrender540, 0, 'BEFORE: ');
createZr(zrender, 1, 'AFTER: ');
</script>
</body>
</html>
6 changes: 5 additions & 1 deletion test/ut/spec/core/platform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,9 @@ describe('platform', function() {
createCanvas: oldCreateCanvas,
measureText: oldMeasureText
});
})
});

it('measureText should return correct width', function () {
expect(platform.platformApi.measureText('A', 'normal normal 18px sans-serif').width).toBe(12.06);
});
});