parent
8eee356787
commit
da2dfd3a16
@ -1,31 +1,72 @@
|
|||||||
import { DeepRequired } from '../../../interface/Common'
|
import { DeepRequired } from '../../../interface/Common'
|
||||||
import { IEditorOption } from '../../../interface/Editor'
|
import { IEditorOption } from '../../../interface/Editor'
|
||||||
|
import { IElement, IElementPosition } from '../../../interface/Element'
|
||||||
|
import { IRow } from '../../../interface/Row'
|
||||||
|
import { Position } from '../../position/Position'
|
||||||
import { Draw } from '../Draw'
|
import { Draw } from '../Draw'
|
||||||
|
|
||||||
export class Header {
|
export class Header {
|
||||||
|
|
||||||
private draw: Draw
|
private draw: Draw
|
||||||
|
private position: Position
|
||||||
private options: DeepRequired<IEditorOption>
|
private options: DeepRequired<IEditorOption>
|
||||||
|
|
||||||
|
private elementList: IElement[]
|
||||||
|
private rowList: IRow[]
|
||||||
|
private positionList: IElementPosition[]
|
||||||
|
|
||||||
constructor(draw: Draw) {
|
constructor(draw: Draw) {
|
||||||
this.draw = draw
|
this.draw = draw
|
||||||
this.options = <DeepRequired<IEditorOption>>draw.getOptions()
|
this.position = draw.getPosition()
|
||||||
|
this.options = draw.getOptions()
|
||||||
|
|
||||||
|
this.elementList = draw.getHeaderElementList()
|
||||||
|
this.rowList = []
|
||||||
|
this.positionList = []
|
||||||
|
}
|
||||||
|
|
||||||
|
public compute() {
|
||||||
|
this._recovery()
|
||||||
|
this._computeRowList()
|
||||||
|
this._computePositionList()
|
||||||
|
}
|
||||||
|
|
||||||
|
private _recovery() {
|
||||||
|
this.rowList = []
|
||||||
|
this.positionList = []
|
||||||
|
}
|
||||||
|
|
||||||
|
private _computeRowList() {
|
||||||
|
const innerWidth = this.draw.getInnerWidth()
|
||||||
|
this.rowList = this.draw.computeRowList(innerWidth, this.elementList)
|
||||||
|
}
|
||||||
|
|
||||||
|
private _computePositionList() {
|
||||||
|
const { header: { top } } = this.options
|
||||||
|
const innerWidth = this.draw.getInnerWidth()
|
||||||
|
const margins = this.draw.getMargins()
|
||||||
|
const startX = margins[3]
|
||||||
|
const startY = margins[0] + top
|
||||||
|
this.position.computePageRowPosition({
|
||||||
|
positionList: this.positionList,
|
||||||
|
rowList: this.rowList,
|
||||||
|
pageNo: 0,
|
||||||
|
startIndex: 0,
|
||||||
|
startX,
|
||||||
|
startY,
|
||||||
|
innerWidth
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
public render(ctx: CanvasRenderingContext2D) {
|
public render(ctx: CanvasRenderingContext2D) {
|
||||||
const { header: { data, size, color, font }, scale } = this.options
|
const innerWidth = this.draw.getInnerWidth()
|
||||||
if (!data) return
|
this.draw.drawRow(ctx, {
|
||||||
const width = this.draw.getWidth()
|
positionList: this.positionList,
|
||||||
const top = this.draw.getHeaderTop()
|
rowList: this.rowList,
|
||||||
ctx.save()
|
pageNo: 0,
|
||||||
ctx.fillStyle = color
|
startIndex: 0,
|
||||||
ctx.font = `${size! * scale}px ${font}`
|
innerWidth
|
||||||
// 文字长度
|
})
|
||||||
const textWidth = ctx.measureText(`${data}`).width
|
|
||||||
// 偏移量
|
|
||||||
const left = (width - textWidth) / 2
|
|
||||||
ctx.fillText(`${data}`, left < 0 ? 0 : left, top)
|
|
||||||
ctx.restore()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1,8 +1,7 @@
|
|||||||
import { IHeader } from '../../interface/Header'
|
import { IHeader } from '../../interface/Header'
|
||||||
|
import { HeaderMaxHeightRatio } from '../enum/Header'
|
||||||
|
|
||||||
export const defaultHeaderOption: Readonly<Required<IHeader>> = {
|
export const defaultHeaderOption: Readonly<Required<IHeader>> = {
|
||||||
data: '',
|
top: -50,
|
||||||
color: '#AAAAAA',
|
maxHeightRadio: HeaderMaxHeightRatio.HALF
|
||||||
size: 14,
|
|
||||||
font: 'Yahei'
|
|
||||||
}
|
}
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
export enum HeaderMaxHeightRatio {
|
||||||
|
HALF = 'half',
|
||||||
|
ONE_THIRD = 'one-third',
|
||||||
|
QUARTER = 'quarter'
|
||||||
|
}
|
||||||
@ -1,6 +1,6 @@
|
|||||||
|
import { HeaderMaxHeightRatio } from '../dataset/enum/Header'
|
||||||
|
|
||||||
export interface IHeader {
|
export interface IHeader {
|
||||||
data: string;
|
top?: number;
|
||||||
color?: string;
|
maxHeightRadio?: HeaderMaxHeightRatio;
|
||||||
size?: number;
|
|
||||||
font?: string;
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in new issue