feat: add line number option #734
parent
8878fd7734
commit
d89218a916
@ -0,0 +1,43 @@
|
||||
import { LineNumberType } from '../../../dataset/enum/LineNumber'
|
||||
import { DeepRequired } from '../../../interface/Common'
|
||||
import { IEditorOption } from '../../../interface/Editor'
|
||||
import { Draw } from '../Draw'
|
||||
|
||||
export class LineNumber {
|
||||
private draw: Draw
|
||||
private options: DeepRequired<IEditorOption>
|
||||
|
||||
constructor(draw: Draw) {
|
||||
this.draw = draw
|
||||
this.options = draw.getOptions()
|
||||
}
|
||||
|
||||
public render(ctx: CanvasRenderingContext2D, pageNo: number) {
|
||||
const {
|
||||
scale,
|
||||
lineNumber: { color, size, font, right, type }
|
||||
} = this.options
|
||||
const textParticle = this.draw.getTextParticle()
|
||||
const margins = this.draw.getMargins()
|
||||
const positionList = this.draw.getPosition().getOriginalMainPositionList()
|
||||
const pageRowList = this.draw.getPageRowList()
|
||||
const rowList = pageRowList[pageNo]
|
||||
ctx.save()
|
||||
ctx.fillStyle = color
|
||||
ctx.font = `${size * scale}px ${font}`
|
||||
for (let i = 0; i < rowList.length; i++) {
|
||||
const row = rowList[i]
|
||||
const {
|
||||
coordinate: { leftBottom }
|
||||
} = positionList[row.startIndex]
|
||||
const seq = type === LineNumberType.PAGE ? i + 1 : row.rowIndex + 1
|
||||
const textMetrics = textParticle.measureText(ctx, {
|
||||
value: `${seq}`
|
||||
})
|
||||
const x = margins[3] - (textMetrics.width + right) * scale
|
||||
const y = leftBottom[1] - textMetrics.actualBoundingBoxAscent * scale
|
||||
ctx.fillText(`${seq}`, x, y)
|
||||
}
|
||||
ctx.restore()
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
import { ILineNumberOption } from '../../interface/LineNumber'
|
||||
import { LineNumberType } from '../enum/LineNumber'
|
||||
|
||||
export const defaultLineNumberOption: Readonly<Required<ILineNumberOption>> = {
|
||||
size: 12,
|
||||
font: 'Microsoft YaHei',
|
||||
color: '#000000',
|
||||
disabled: true,
|
||||
right: 20,
|
||||
type: LineNumberType.CONTINUITY
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
export enum LineNumberType {
|
||||
PAGE = 'page',
|
||||
CONTINUITY = 'continuity'
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
import { LineNumberType } from '../dataset/enum/LineNumber'
|
||||
|
||||
export interface ILineNumberOption {
|
||||
size?: number
|
||||
font?: string
|
||||
color?: string
|
||||
disabled?: boolean
|
||||
right?: number
|
||||
type?: LineNumberType
|
||||
}
|
||||
Loading…
Reference in new issue