Skip to content

Commit 890b87d

Browse files
committed
feat: optimize zero width element ascent calculation #1324
1 parent 543eaec commit 890b87d

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/editor/core/draw/Draw.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,9 +1734,13 @@ export class Draw {
17341734
if (element.letterSpacing) {
17351735
metrics.width += element.letterSpacing * scale
17361736
}
1737+
// 零宽字符ascent默认为:基线元素ascent
17371738
metrics.boundingBoxAscent =
17381739
(element.value === ZERO
1739-
? element.size || defaultSize
1740+
? this.textParticle.getBasisWordBoundingBoxAscent(
1741+
ctx,
1742+
element.font!
1743+
)
17401744
: fontMetrics.actualBoundingBoxAscent) * scale
17411745
metrics.boundingBoxDescent =
17421746
fontMetrics.actualBoundingBoxDescent * scale
@@ -1947,10 +1951,11 @@ export class Draw {
19471951
// 行距离顶部偏移量等于行高时 => 行增加默认标准元素偏移量
19481952
// 如整行都是空格测量偏移量为0,导致行塌陷
19491953
if (curRow.ascent === rowMargin) {
1950-
const boundingBoxDescent = this.textParticle.measureBasisWord(
1951-
ctx,
1952-
element.font!
1953-
).actualBoundingBoxAscent
1954+
const boundingBoxDescent =
1955+
this.textParticle.getBasisWordBoundingBoxAscent(
1956+
ctx,
1957+
element.font!
1958+
) * scale
19541959
curRow.ascent += boundingBoxDescent
19551960
curRow.height += boundingBoxDescent
19561961
}

src/editor/core/draw/particle/TextParticle.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ export class TextParticle {
113113
return textMetrics
114114
}
115115

116+
public getBasisWordBoundingBoxAscent(
117+
ctx: CanvasRenderingContext2D,
118+
font: string
119+
): number {
120+
return this.measureBasisWord(ctx, font).actualBoundingBoxAscent
121+
}
122+
116123
public complete() {
117124
this._render()
118125
this.text = ''

0 commit comments

Comments
 (0)