Skip to content

Commit

Permalink
fix: underline position of superscript and subscript elements is error
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Nov 24, 2023
1 parent b3354ac commit 90efe10
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
27 changes: 22 additions & 5 deletions src/editor/core/draw/Draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1616,22 +1616,39 @@ export class Draw {
}
// 下划线记录
if (element.underline || element.control?.underline) {
// 上下标元素下划线单独绘制
if (
(preElement?.type === ElementType.SUPERSCRIPT &&
element.type !== ElementType.SUPERSCRIPT) ||
(preElement?.type === ElementType.SUBSCRIPT &&
element.type !== ElementType.SUBSCRIPT)
) {
this.underline.render(ctx)
}
// 行间距
const rowMargin =
defaultBasicRowMarginHeight *
(element.rowMargin || defaultRowMargin) *
scale
// 元素偏移量
const left = element.left || 0
// 元素向左偏移量
const offsetX = element.left || 0
// 上下标元素y轴偏移值
let offsetY = 0
if (element.type === ElementType.SUBSCRIPT) {
offsetY = this.subscriptParticle.getOffsetY(element)
} else if (element.type === ElementType.SUPERSCRIPT) {
offsetY = this.superscriptParticle.getOffsetY(element)
}
// 占位符不参与颜色计算
const color =
element.controlComponent === ControlComponent.PLACEHOLDER
? undefined
: element.color
this.underline.recordFillInfo(
ctx,
x - left,
y + curRow.height - rowMargin,
metrics.width + left,
x - offsetX,
y + curRow.height - rowMargin + offsetY,
metrics.width + offsetX,
0,
color
)
Expand Down
7 changes: 6 additions & 1 deletion src/editor/core/draw/particle/Subscript.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { IRowElement } from '../../../interface/Row'

export class SubscriptParticle {
// 向下偏移字高的一半
public getOffsetY(element: IRowElement): number {
return element.metrics.height / 2
}

public render(
ctx: CanvasRenderingContext2D,
element: IRowElement,
Expand All @@ -12,7 +17,7 @@ export class SubscriptParticle {
if (element.color) {
ctx.fillStyle = element.color
}
ctx.fillText(element.value, x, y + element.metrics.height / 2)
ctx.fillText(element.value, x, y + this.getOffsetY(element))
ctx.restore()
}
}
7 changes: 6 additions & 1 deletion src/editor/core/draw/particle/Superscript.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { IRowElement } from '../../../interface/Row'

export class SuperscriptParticle {
// 向上偏移字高的一半
public getOffsetY(element: IRowElement): number {
return -element.metrics.height / 2
}

public render(
ctx: CanvasRenderingContext2D,
element: IRowElement,
Expand All @@ -12,7 +17,7 @@ export class SuperscriptParticle {
if (element.color) {
ctx.fillStyle = element.color
}
ctx.fillText(element.value, x, y - element.metrics.height / 2)
ctx.fillText(element.value, x, y + this.getOffsetY(element))
ctx.restore()
}
}

0 comments on commit 90efe10

Please sign in to comment.