Skip to content

Commit

Permalink
Merge pull request #419 from antvis/text_error
Browse files Browse the repository at this point in the history
fix(bbox): string functions error
  • Loading branch information
simaQ authored Feb 24, 2020
2 parents 36938d5 + 9b85720 commit 59b3b4f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/g-base/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/g-base",
"version": "0.3.20",
"version": "0.3.21",
"description": "A common util collection for antv projects",
"main": "lib/index.js",
"module": "esm/index.js",
Expand Down
10 changes: 6 additions & 4 deletions packages/g-base/src/util/text.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNil, each } from './util';
import { isNil, each, isString } from './util';
import { getOffScreenContext } from './offscreen';
import { ShapeAttrs } from '../types';

Expand All @@ -9,8 +9,10 @@ import { ShapeAttrs } from '../types';
* @param lineHeight 行高,可以为空
*/
export function getTextHeight(text: string, fontSize: number, lineHeight?: number): number {
const textArr = text.split('\n');
const lineCount = textArr ? textArr.length : 1;
let lineCount = 1;
if (isString(text)) {
lineCount = text.split('\n').length;
}
if (lineCount > 1) {
const spaceingY = getLineSpaceing(fontSize, lineHeight);
return fontSize * lineCount + spaceingY * (lineCount - 1);
Expand Down Expand Up @@ -41,7 +43,7 @@ export function getTextWidth(text: string, font: string) {
}
context.save();
context.font = font;
if (text.includes('\n')) {
if (isString(text) && text.includes('\n')) {
const textArr = text.split('\n');
each(textArr, (subText) => {
const measureWidth = context.measureText(subText).width;
Expand Down
14 changes: 14 additions & 0 deletions packages/g-base/tests/unit/bbox-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,20 @@ describe('test bbox', () => {
height: 12,
});
});
it('text is number', () => {
const shape = new MyShape({
type: 'text',
attrs: { x: 10, y: 10, text: 123, fontSize: 12, fontFamily: 'sans-serif' },
});
const bbox = shape.getBBox();
const width = getTextWidth(shape.attr('text'), '12px sans-serif');
expect(bbox).eqls({
x: 10,
y: -2,
width,
height: 12,
});
});

it('path', () => {
const shape = new MyShape({
Expand Down

0 comments on commit 59b3b4f

Please sign in to comment.