feat:page number set row flex
This commit is contained in:
@@ -260,7 +260,8 @@ export class Draw {
|
||||
}
|
||||
|
||||
public getPageNumberBottom(): number {
|
||||
return this.options.pageNumberBottom * this.options.scale
|
||||
const { pageNumber: { bottom }, scale } = this.options
|
||||
return bottom * scale
|
||||
}
|
||||
|
||||
public getHeaderTop(): number {
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import { PageMode } from '../../../dataset/enum/Editor'
|
||||
import { RowFlex } from '../../../dataset/enum/Row'
|
||||
import { DeepRequired } from '../../../interface/Common'
|
||||
import { IEditorOption } from '../../../interface/Editor'
|
||||
import { Draw } from '../Draw'
|
||||
|
||||
export class PageNumber {
|
||||
|
||||
private draw: Draw
|
||||
private options: Required<IEditorOption>
|
||||
private options: DeepRequired<IEditorOption>
|
||||
|
||||
constructor(draw: Draw) {
|
||||
this.draw = draw
|
||||
@@ -13,16 +15,30 @@ export class PageNumber {
|
||||
}
|
||||
|
||||
public render(ctx: CanvasRenderingContext2D, pageNo: number) {
|
||||
const { pageNumberSize, pageNumberFont, scale, pageMode } = this.options
|
||||
const { pageNumber: { size, font, color, rowFlex }, scale, pageMode } = this.options
|
||||
const text = `${pageNo + 1}`
|
||||
const width = this.draw.getWidth()
|
||||
// 计算y位置
|
||||
const height = pageMode === PageMode.CONTINUITY
|
||||
? this.draw.getCanvasHeight(pageNo)
|
||||
: this.draw.getHeight()
|
||||
const pageNumberBottom = this.draw.getPageNumberBottom()
|
||||
const y = height - pageNumberBottom
|
||||
ctx.save()
|
||||
ctx.fillStyle = '#00000'
|
||||
ctx.font = `${pageNumberSize * scale}px ${pageNumberFont}`
|
||||
ctx.fillText(`${pageNo + 1}`, width / 2, height - pageNumberBottom)
|
||||
ctx.fillStyle = color
|
||||
ctx.font = `${size * scale}px ${font}`
|
||||
// 计算x位置-居左、居中、居右
|
||||
let x = 0
|
||||
const margins = this.draw.getMargins()
|
||||
const { width: textWidth } = ctx.measureText(text)
|
||||
if (rowFlex === RowFlex.CENTER) {
|
||||
x = (width + textWidth) / 2
|
||||
} else if (rowFlex === RowFlex.RIGHT) {
|
||||
x = width - textWidth - margins[1]
|
||||
} else {
|
||||
x = margins[3]
|
||||
}
|
||||
ctx.fillText(text, x, y)
|
||||
ctx.restore()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user