feat:page header

This commit is contained in:
Hufe921
2022-01-06 21:25:12 +08:00
parent fab200531f
commit 5043dfbd6b
7 changed files with 89 additions and 2 deletions
+9
View File
@@ -27,6 +27,7 @@ import { TableParticle } from "./particle/table/TableParticle"
import { ISearchResult } from "../../interface/Search"
import { TableTool } from "./particle/table/TableTool"
import { HyperlinkParticle } from "./particle/HyperlinkParticle"
import { Header } from "./frame/Header"
export class Draw {
@@ -55,6 +56,7 @@ export class Draw {
private tableParticle: TableParticle
private tableTool: TableTool
private pageNumber: PageNumber
private header: Header
private hyperlinkParticle: HyperlinkParticle
private rowList: IRow[]
@@ -94,6 +96,7 @@ export class Draw {
this.tableParticle = new TableParticle(this)
this.tableTool = new TableTool(this)
this.pageNumber = new PageNumber(this)
this.header = new Header(this)
this.hyperlinkParticle = new HyperlinkParticle(this)
new GlobalObserver(this)
@@ -143,6 +146,10 @@ export class Draw {
return this.options.pageNumberBottom * this.options.scale
}
public getHeaderTop(): number {
return this.options.headerTop * this.options.scale
}
public getMarginIndicatorSize(): number {
return this.options.marginIndicatorSize * this.options.scale
}
@@ -646,6 +653,8 @@ export class Draw {
x = drawRowResult.x
y = drawRowResult.y
index = drawRowResult.index
// 绘制页眉
this.header.render(ctx)
// 绘制页码
this.pageNumber.render(ctx, pageNo)
// 搜索匹配绘制
+31
View File
@@ -0,0 +1,31 @@
import { DeepRequired } from "../../../interface/Common"
import { IEditorOption } from "../../../interface/Editor"
import { Draw } from "../Draw"
export class Header {
private draw: Draw
private options: DeepRequired<IEditorOption>
constructor(draw: Draw) {
this.draw = draw
this.options = <DeepRequired<IEditorOption>>draw.getOptions()
}
public render(ctx: CanvasRenderingContext2D) {
const { header: { data, size, color, font }, scale } = this.options
if (!data) return
const width = this.draw.getWidth()
const top = this.draw.getHeaderTop()
ctx.save()
ctx.fillStyle = color
ctx.font = `${size! * scale}px ${font}`
// 文字长度
const textWidth = ctx.measureText(`${data}`).width
// 偏移量
const left = (width - textWidth) / 2
ctx.fillText(`${data}`, left < 0 ? 0 : left, top)
ctx.restore()
}
}