feat: add separator option #530

This commit is contained in:
Hufe921
2024-05-03 22:54:24 +08:00
parent 15f52c5e43
commit 7416a88151
8 changed files with 38 additions and 12 deletions
+5 -2
View File
@@ -1412,11 +1412,14 @@ export class Draw {
}
}
} else if (element.type === ElementType.SEPARATOR) {
const {
separator: { lineWidth }
} = this.options
element.width = availableWidth / scale
metrics.width = availableWidth
metrics.height = defaultSize
metrics.height = lineWidth * scale
metrics.boundingBoxAscent = -rowMargin
metrics.boundingBoxDescent = -rowMargin
metrics.boundingBoxDescent = -rowMargin + metrics.height
} else if (element.type === ElementType.PAGE_BREAK) {
element.width = availableWidth / scale
metrics.width = availableWidth
+11 -9
View File
@@ -16,18 +16,20 @@ export class SeparatorParticle {
y: number
) {
ctx.save()
const { scale } = this.options
ctx.lineWidth = scale
if (element.color) {
ctx.strokeStyle = element.color
}
if (element.dashArray && element.dashArray.length) {
const {
scale,
separator: { lineWidth, strokeStyle }
} = this.options
ctx.lineWidth = lineWidth * scale
ctx.strokeStyle = element.color || strokeStyle
if (element.dashArray?.length) {
ctx.setLineDash(element.dashArray)
}
ctx.translate(0, 0.5) // 从1处渲染,避免线宽度等于3
const offsetY = Math.round(y) // 四舍五入避免绘制模糊
ctx.translate(0, ctx.lineWidth / 2)
ctx.beginPath()
ctx.moveTo(x, y)
ctx.lineTo(x + element.width! * scale, y)
ctx.moveTo(x, offsetY)
ctx.lineTo(x + element.width! * scale, offsetY)
ctx.stroke()
ctx.restore()
}