Skip to content

Commit

Permalink
Merge pull request #810 from VisActor/fix/text-wrap
Browse files Browse the repository at this point in the history
fix: fix issue with text while whitespace is normal
  • Loading branch information
neuqzxy authored Dec 19, 2023
2 parents 4965812 + d5237dd commit dd83584
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@visactor/vrender-core",
"comment": "fix: fix issue with text while whitespace is normal",
"type": "none"
}
],
"packageName": "@visactor/vrender-core"
}
12 changes: 8 additions & 4 deletions packages/vrender-core/src/graphic/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class Text extends Graphic<ITextGraphicAttribute> implements IText {
get clipedText(): string | undefined {
const attribute = this.attribute;
const textTheme = getTheme(this).text;
if (Array.isArray(attribute.text)) {
if (this.isMultiLine) {
return undefined;
}
const { maxLineWidth = textTheme.maxLineWidth } = attribute;
Expand All @@ -67,7 +67,7 @@ export class Text extends Graphic<ITextGraphicAttribute> implements IText {
return this.cache.clipedText;
}
get clipedWidth(): number | undefined {
if (Array.isArray(this.attribute.text)) {
if (this.isMultiLine) {
return undefined;
}
this.tryUpdateAABBBounds();
Expand All @@ -76,7 +76,7 @@ export class Text extends Graphic<ITextGraphicAttribute> implements IText {
get cliped(): boolean | undefined {
const textTheme = getTheme(this).text;
const attribute = this.attribute;
if (Array.isArray(attribute.text)) {
if (this.isMultiLine) {
return undefined;
}
const { maxLineWidth = textTheme.maxLineWidth } = attribute;
Expand All @@ -87,13 +87,17 @@ export class Text extends Graphic<ITextGraphicAttribute> implements IText {
return this.clipedText !== attribute.text.toString();
}
get multilineLayout(): LayoutType | undefined {
if (!Array.isArray(this.attribute.text)) {
if (!this.isMultiLine) {
return undefined;
}
this.tryUpdateAABBBounds();
return this.cache.layoutData;
}

get isMultiLine(): boolean {
return Array.isArray(this.attribute.text) || this.attribute.whiteSpace === 'normal';
}

constructor(params: ITextGraphicAttribute = { text: '', fontSize: 16 }) {
super(params);
this.numberType = TEXT_NUMBER_TYPE;
Expand Down
1 change: 1 addition & 0 deletions packages/vrender-core/src/interface/graphic/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export interface IText extends IGraphic<ITextGraphicAttribute> {
cliped?: boolean;
multilineLayout?: LayoutType;
font?: string;
isMultiLine: boolean;
cache?: ITextCache;

getBaselineMapAlign: () => Record<string, string>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class DefaultCanvasTextRender extends BaseRender<IText> implements IGraph
keepDirIn3d = textAttribute.keepDirIn3d,
direction = textAttribute.direction,
// lineHeight = textAttribute.lineHeight,
whiteSpace = textAttribute.whiteSpace,
fontSize = textAttribute.fontSize,
verticalMode = textAttribute.verticalMode,
x: originX = textAttribute.x,
Expand Down Expand Up @@ -147,7 +148,7 @@ export class DefaultCanvasTextRender extends BaseRender<IText> implements IGraph
context.setTransformForCurrent();
}
};
if (Array.isArray(str)) {
if (text.isMultiLine) {
context.setTextStyleWithoutAlignBaseline(text.attribute, textAttribute, z);
if (direction === 'horizontal') {
const { multilineLayout } = text;
Expand Down
26 changes: 20 additions & 6 deletions packages/vrender/__tests__/browser/src/pages/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,26 @@ function performance(stage: any) {
export const page = () => {
const graphics: IGraphic[] = [];
const t = createText({
x: 100,
y: 100,
fontFamily: 'Arial',
text: 'aaa这是aaa',
fill: 'red',
background: 'green'
autoWrapText: false,
dx: 0,
ellipsis: '...',
fill: '#000',
fontFamily: 'Arial,sans-serif',
fontSize: 16,
fontWeight: null,
heightLimit: 52,
lineClamp: undefined,
lineHeight: 16,
lineThrough: undefined,
maxLineWidth: 40,
text: ['129.696', '这是第二行'],
textAlign: 'left',
textBaseline: 'top',
underline: undefined,
whiteSpace: 'normal',
wordBreak: 'break-word',
x: 16,
y: 28
});
console.log(t.AABBBounds);
graphics.push(t);
Expand Down

0 comments on commit dd83584

Please sign in to comment.