From cade28480c86952946be94ec7c6a710a0e526c60 Mon Sep 17 00:00:00 2001 From: codechrl Date: Fri, 27 Nov 2026 17:53:25 +0700 Subject: [PATCH] fix(model): use the global textStyle font in getTextRect `getTextRect` reads `fontStyle`, `fontWeight`, `fontSize` and `fontFamily` from the model only. `createTextStyle` falls back to the global `textStyle` for these keys. `getTextRect` measures such a label in the default font. Each font key now falls back to `ecModel.option.textStyle`, as in `createTextStyle`. zrender still builds the font string from the separate keys, so a `fontSize` with a unit such as `'25px'` keeps working. --- src/model/mixin/textStyle.ts | 10 ++- test/ut/spec/model/textStyle.test.ts | 97 ++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 test/ut/spec/model/textStyle.test.ts diff --git a/src/model/mixin/textStyle.ts b/src/model/mixin/textStyle.ts index 6c5cce9a3f..c14ed4a709 100644 --- a/src/model/mixin/textStyle.ts +++ b/src/model/mixin/textStyle.ts @@ -22,6 +22,7 @@ import {getFont} from '../../label/labelStyle'; import Model from '../Model'; import { LabelOption, ColorString } from '../../util/types'; import ZRText, {TextStyleProps} from 'zrender/src/graphic/Text'; +import { retrieve2 } from 'zrender/src/core/util'; const PATH_COLOR = ['textStyle', 'color'] as const; @@ -31,9 +32,9 @@ type LabelRectRelatedOption = Pick & LabelFontOption; +const fontParams = ['fontStyle', 'fontWeight', 'fontSize', 'fontFamily'] as const; const textStyleParams = [ - 'fontStyle', 'fontWeight', 'fontSize', 'fontFamily', 'padding', - 'lineHeight', 'rich', 'width', 'height', 'overflow' + 'padding', 'lineHeight', 'rich', 'width', 'height', 'overflow' ] as const; // TODO Performance improvement? @@ -70,6 +71,11 @@ class TextStyleMixin { verticalAlign: this.getShallow('verticalAlign') || this.getShallow('baseline') }; + const globalTextStyle = this.ecModel && this.ecModel.option.textStyle; + for (let i = 0; i < fontParams.length; i++) { + const key = fontParams[i]; + (style as any)[key] = retrieve2(this.getShallow(key), globalTextStyle && globalTextStyle[key]); + } for (let i = 0; i < textStyleParams.length; i++) { (style as any)[textStyleParams[i]] = this.getShallow(textStyleParams[i]); } diff --git a/test/ut/spec/model/textStyle.test.ts b/test/ut/spec/model/textStyle.test.ts new file mode 100644 index 0000000000..3384b13295 --- /dev/null +++ b/test/ut/spec/model/textStyle.test.ts @@ -0,0 +1,97 @@ +/* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +*/ + +import { createChart, getECModel } from '../../core/utHelper'; +import { EChartsType } from '../../../../src/echarts'; +import { EChartsOption } from '../../../../src/export/option'; +import Model from '../../../../src/model/Model'; +import AxisModel from '../../../../src/coord/cartesian/AxisModel'; +import { setPlatformAPI } from 'zrender/src/core/platform'; + +describe('model/mixin/textStyle', function () { + + let chart: EChartsType; + + beforeEach(function () { + // Width per character: the font size for monospace, half of it otherwise. + // A font the stub cannot parse measures as `10px sans-serif`, the canvas default font. + setPlatformAPI({ + measureText(text: string, font: string) { + const match = /(\d+(?:\.\d+)?)px (.+)$/.exec(font); + const fontSize = match ? +match[1] : 10; + return { width: text.length * fontSize * (match && match[2] === 'monospace' ? 1 : 0.5) }; + } + }); + }); + + afterEach(function () { + chart.dispose(); + }); + + function getAxisLabelModel(option: EChartsOption) { + chart = createChart(); + chart.setOption(option); + const axisModel = getECModel(chart).getComponent('xAxis', 0) as AxisModel; + return axisModel.getModel('axisLabel'); + } + + describe('getTextRect', function () { + + it('should use the global textStyle as the fallback font', function () { + const axisLabelModel = getAxisLabelModel({ + textStyle: { fontFamily: 'monospace', fontSize: 20 }, + xAxis: { type: 'category', data: ['a'] }, + yAxis: {} + }); + const model = new Model({}, null, getECModel(chart)); + + expect(model.getTextRect('abcd').width).toEqual(80); + // `axisLabel.fontSize` defaults to 12, so only the global font family applies. + expect(axisLabelModel.getTextRect('abcd').width).toEqual(48); + }); + + it('should merge the axisLabel font with the global textStyle', function () { + const axisLabelModel = getAxisLabelModel({ + textStyle: { fontFamily: 'monospace', fontSize: 20 }, + xAxis: { type: 'category', data: ['a'], axisLabel: { fontSize: 10 } }, + yAxis: {} + }); + + expect(axisLabelModel.getTextRect('abcd').width).toEqual(40); + }); + + it('should keep a font size given with a unit', function () { + const axisLabelModel = getAxisLabelModel({ + xAxis: { type: 'category', data: ['a'], axisLabel: { fontSize: '25px' } }, + yAxis: {} + }); + + expect(axisLabelModel.getTextRect('abcd').width).toEqual(50); + }); + + it('should use the default font when no font is configured', function () { + const axisLabelModel = getAxisLabelModel({ + xAxis: { type: 'category', data: ['a'] }, + yAxis: {} + }); + + expect(axisLabelModel.getTextRect('abcd').width).toEqual(24); + }); + }); +});