Skip to content

Commit b952ca1

Browse files
authored
Merge pull request #968 from ecomfe/fix-measure-text
fix(platformApi): fix wrong expression for extracting the font size
2 parents 8914365 + b968b36 commit b952ca1

File tree

3 files changed

+106
-4
lines changed

3 files changed

+106
-4
lines changed

src/core/platform.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ export const platformApi: Platform = {
7575
text = text || '';
7676
font = font || DEFAULT_FONT;
7777
// Use font size if there is no other method can be used.
78-
const res = /^([0-9]*?)px$/.exec(font);
79-
const fontSize = +(res && res[1]) || DEFAULT_FONT_SIZE;
78+
const res = /(\d+)px/.exec(font);
79+
const fontSize = res && +res[1] || DEFAULT_FONT_SIZE;
8080
let width = 0;
8181
if (font.indexOf('mono') >= 0) { // is monospace
8282
width = fontSize * text.length;
@@ -103,7 +103,7 @@ export const platformApi: Platform = {
103103

104104
export function setPlatformAPI(newPlatformApis: Partial<Platform>) {
105105
for (let key in platformApi) {
106-
// Don't assign unkown methods.
106+
// Don't assign unknown methods.
107107
if ((newPlatformApis as any)[key]) {
108108
(platformApi as any)[key] = (newPlatformApis as any)[key];
109109
}

test/ssr-measureText.html

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>Image</title>
6+
<script src="./lib/config.js"></script>
7+
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
8+
<script>window.zrender540 = zrender;</script>
9+
<script src="../dist/zrender.js"></script>
10+
<meta name="viewport" content="width=device-width, initial-scale=1" />
11+
<style>
12+
body {
13+
overflow: hidden;
14+
}
15+
.painter {
16+
height: 50vh;
17+
}
18+
</style>
19+
</head>
20+
<body>
21+
<script type="text/javascript">
22+
// See also https://github.com/ecomfe/zrender/issues/947
23+
// See also https://github.com/apache/echarts/issues/17326
24+
25+
// mock non-canvas environment
26+
zrender.setPlatformAPI({
27+
createCanvas: zrender.util.noop
28+
});
29+
zrender540.setPlatformAPI({
30+
createCanvas: zrender540.util.noop
31+
});
32+
33+
function createZr(zrender, painterIdx, indication) {
34+
var zr = zrender.init(null, {
35+
renderer: 'svg',
36+
ssr: true,
37+
width: document.documentElement.clientWidth,
38+
height: document.documentElement.clientHeight / 2
39+
});
40+
41+
zr.add(new zrender.Text({
42+
style: {
43+
x: 100,
44+
y: 200,
45+
text: (indication || '') + 'ABCDEFG1234567',
46+
fontSize: 18
47+
}
48+
}));
49+
50+
function showBoundingRect() {
51+
zr.storage.traverse(function (el) {
52+
if (el.type === 'text') {
53+
var rect = el.getBoundingRect();
54+
55+
zr.add(new zrender.Rect({
56+
shape: rect,
57+
x: el.x,
58+
y: el.y,
59+
rotation: el.rotation,
60+
scaleX: el.scaleX,
61+
scaleY: el.scaleY,
62+
originX: el.originX,
63+
originY: el.originY,
64+
style: {
65+
fill: null,
66+
stroke: zrender.color.random(),
67+
lineWidth: 1
68+
}
69+
}));
70+
}
71+
});
72+
}
73+
74+
var painter = document.createElement('div');
75+
painter.id = 'painter' + painterIdx;
76+
painter.className = 'painter';
77+
document.body.appendChild(painter);
78+
function paint() {
79+
painter.innerHTML = zr.painter.renderToString();
80+
}
81+
82+
showBoundingRect();
83+
paint();
84+
85+
window.addEventListener('resize', function () {
86+
zr.resize({
87+
width: document.documentElement.clientWidth,
88+
height: document.documentElement.clientHeight / 2
89+
});
90+
paint();
91+
});
92+
}
93+
94+
createZr(zrender540, 0, 'BEFORE: ');
95+
createZr(zrender, 1, 'AFTER: ');
96+
</script>
97+
</body>
98+
</html>

test/ut/spec/core/platform.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,9 @@ describe('platform', function() {
3232
createCanvas: oldCreateCanvas,
3333
measureText: oldMeasureText
3434
});
35-
})
35+
});
36+
37+
it('measureText should return correct width', function () {
38+
expect(platform.platformApi.measureText('A', 'normal normal 18px sans-serif').width).toBe(12.06);
39+
});
3640
});

0 commit comments

Comments
 (0)