fix: underline position of superscript and subscript elements is error #268

pr675
Hufe921 2 years ago
parent b3354ac35f
commit 90efe1020f

@ -1616,12 +1616,29 @@ export class Draw {
} }
// 下划线记录 // 下划线记录
if (element.underline || element.control?.underline) { 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 = const rowMargin =
defaultBasicRowMarginHeight * defaultBasicRowMarginHeight *
(element.rowMargin || defaultRowMargin) * (element.rowMargin || defaultRowMargin) *
scale 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 = const color =
element.controlComponent === ControlComponent.PLACEHOLDER element.controlComponent === ControlComponent.PLACEHOLDER
@ -1629,9 +1646,9 @@ export class Draw {
: element.color : element.color
this.underline.recordFillInfo( this.underline.recordFillInfo(
ctx, ctx,
x - left, x - offsetX,
y + curRow.height - rowMargin, y + curRow.height - rowMargin + offsetY,
metrics.width + left, metrics.width + offsetX,
0, 0,
color color
) )

@ -1,6 +1,11 @@
import { IRowElement } from '../../../interface/Row' import { IRowElement } from '../../../interface/Row'
export class SubscriptParticle { export class SubscriptParticle {
// 向下偏移字高的一半
public getOffsetY(element: IRowElement): number {
return element.metrics.height / 2
}
public render( public render(
ctx: CanvasRenderingContext2D, ctx: CanvasRenderingContext2D,
element: IRowElement, element: IRowElement,
@ -12,7 +17,7 @@ export class SubscriptParticle {
if (element.color) { if (element.color) {
ctx.fillStyle = 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() ctx.restore()
} }
} }

@ -1,6 +1,11 @@
import { IRowElement } from '../../../interface/Row' import { IRowElement } from '../../../interface/Row'
export class SuperscriptParticle { export class SuperscriptParticle {
// 向上偏移字高的一半
public getOffsetY(element: IRowElement): number {
return -element.metrics.height / 2
}
public render( public render(
ctx: CanvasRenderingContext2D, ctx: CanvasRenderingContext2D,
element: IRowElement, element: IRowElement,
@ -12,7 +17,7 @@ export class SuperscriptParticle {
if (element.color) { if (element.color) {
ctx.fillStyle = 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() ctx.restore()
} }
} }

Loading…
Cancel
Save