feat:page number set row flex
This commit is contained in:
@@ -260,7 +260,8 @@ export class Draw {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public getPageNumberBottom(): number {
|
public getPageNumberBottom(): number {
|
||||||
return this.options.pageNumberBottom * this.options.scale
|
const { pageNumber: { bottom }, scale } = this.options
|
||||||
|
return bottom * scale
|
||||||
}
|
}
|
||||||
|
|
||||||
public getHeaderTop(): number {
|
public getHeaderTop(): number {
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
import { PageMode } from '../../../dataset/enum/Editor'
|
import { PageMode } from '../../../dataset/enum/Editor'
|
||||||
|
import { RowFlex } from '../../../dataset/enum/Row'
|
||||||
|
import { DeepRequired } from '../../../interface/Common'
|
||||||
import { IEditorOption } from '../../../interface/Editor'
|
import { IEditorOption } from '../../../interface/Editor'
|
||||||
import { Draw } from '../Draw'
|
import { Draw } from '../Draw'
|
||||||
|
|
||||||
export class PageNumber {
|
export class PageNumber {
|
||||||
|
|
||||||
private draw: Draw
|
private draw: Draw
|
||||||
private options: Required<IEditorOption>
|
private options: DeepRequired<IEditorOption>
|
||||||
|
|
||||||
constructor(draw: Draw) {
|
constructor(draw: Draw) {
|
||||||
this.draw = draw
|
this.draw = draw
|
||||||
@@ -13,16 +15,30 @@ export class PageNumber {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public render(ctx: CanvasRenderingContext2D, pageNo: number) {
|
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()
|
const width = this.draw.getWidth()
|
||||||
|
// 计算y位置
|
||||||
const height = pageMode === PageMode.CONTINUITY
|
const height = pageMode === PageMode.CONTINUITY
|
||||||
? this.draw.getCanvasHeight(pageNo)
|
? this.draw.getCanvasHeight(pageNo)
|
||||||
: this.draw.getHeight()
|
: this.draw.getHeight()
|
||||||
const pageNumberBottom = this.draw.getPageNumberBottom()
|
const pageNumberBottom = this.draw.getPageNumberBottom()
|
||||||
|
const y = height - pageNumberBottom
|
||||||
ctx.save()
|
ctx.save()
|
||||||
ctx.fillStyle = '#00000'
|
ctx.fillStyle = color
|
||||||
ctx.font = `${pageNumberSize * scale}px ${pageNumberFont}`
|
ctx.font = `${size * scale}px ${font}`
|
||||||
ctx.fillText(`${pageNo + 1}`, width / 2, height - pageNumberBottom)
|
// 计算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()
|
ctx.restore()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import { IPageNumber } from '../../interface/PageNumber'
|
||||||
|
import { RowFlex } from '../enum/Row'
|
||||||
|
|
||||||
|
export const defaultPageNumberOption: Readonly<Required<IPageNumber>> = {
|
||||||
|
bottom: 60,
|
||||||
|
size: 12,
|
||||||
|
font: 'Yahei',
|
||||||
|
color: '#000000',
|
||||||
|
rowFlex: RowFlex.CENTER
|
||||||
|
}
|
||||||
+7
-3
@@ -31,6 +31,8 @@ import { IBlock } from './interface/Block'
|
|||||||
import { ILang } from './interface/i18n/I18n'
|
import { ILang } from './interface/i18n/I18n'
|
||||||
import { ICursorOption } from './interface/Cursor'
|
import { ICursorOption } from './interface/Cursor'
|
||||||
import { defaultCursorOption } from './dataset/constant/Cursor'
|
import { defaultCursorOption } from './dataset/constant/Cursor'
|
||||||
|
import { IPageNumber } from './interface/PageNumber'
|
||||||
|
import { defaultPageNumberOption } from './dataset/constant/PageNumber'
|
||||||
|
|
||||||
export default class Editor {
|
export default class Editor {
|
||||||
|
|
||||||
@@ -44,6 +46,10 @@ export default class Editor {
|
|||||||
...defaultHeaderOption,
|
...defaultHeaderOption,
|
||||||
...options.header
|
...options.header
|
||||||
}
|
}
|
||||||
|
const pageNumberOptions: Required<IPageNumber> = {
|
||||||
|
...defaultPageNumberOption,
|
||||||
|
...options.pageNumber
|
||||||
|
}
|
||||||
const waterMarkOptions: Required<IWatermark> = {
|
const waterMarkOptions: Required<IWatermark> = {
|
||||||
...defaultWatermarkOption,
|
...defaultWatermarkOption,
|
||||||
...options.watermark
|
...options.watermark
|
||||||
@@ -73,9 +79,6 @@ export default class Editor {
|
|||||||
height: 1123,
|
height: 1123,
|
||||||
scale: 1,
|
scale: 1,
|
||||||
pageGap: 20,
|
pageGap: 20,
|
||||||
pageNumberBottom: 60,
|
|
||||||
pageNumberSize: 12,
|
|
||||||
pageNumberFont: 'Yahei',
|
|
||||||
underlineColor: '#000000',
|
underlineColor: '#000000',
|
||||||
strikeoutColor: '#FF0000',
|
strikeoutColor: '#FF0000',
|
||||||
rangeAlpha: 0.6,
|
rangeAlpha: 0.6,
|
||||||
@@ -99,6 +102,7 @@ export default class Editor {
|
|||||||
inactiveAlpha: 0.6,
|
inactiveAlpha: 0.6,
|
||||||
...options,
|
...options,
|
||||||
header: headerOptions,
|
header: headerOptions,
|
||||||
|
pageNumber: pageNumberOptions,
|
||||||
watermark: waterMarkOptions,
|
watermark: waterMarkOptions,
|
||||||
control: controlOptions,
|
control: controlOptions,
|
||||||
checkbox: checkboxOptions,
|
checkbox: checkboxOptions,
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { IControlOption } from './Control'
|
|||||||
import { ICursorOption } from './Cursor'
|
import { ICursorOption } from './Cursor'
|
||||||
import { IHeader } from './Header'
|
import { IHeader } from './Header'
|
||||||
import { IMargin } from './Margin'
|
import { IMargin } from './Margin'
|
||||||
|
import { IPageNumber } from './PageNumber'
|
||||||
import { IWatermark } from './Watermark'
|
import { IWatermark } from './Watermark'
|
||||||
|
|
||||||
export interface IEditorData {
|
export interface IEditorData {
|
||||||
@@ -25,9 +26,6 @@ export interface IEditorOption {
|
|||||||
height?: number;
|
height?: number;
|
||||||
scale?: number;
|
scale?: number;
|
||||||
pageGap?: number;
|
pageGap?: number;
|
||||||
pageNumberBottom?: number;
|
|
||||||
pageNumberSize?: number;
|
|
||||||
pageNumberFont?: string;
|
|
||||||
underlineColor?: string;
|
underlineColor?: string;
|
||||||
strikeoutColor?: string;
|
strikeoutColor?: string;
|
||||||
rangeColor?: string;
|
rangeColor?: string;
|
||||||
@@ -50,6 +48,7 @@ export interface IEditorOption {
|
|||||||
paperDirection?: PaperDirection;
|
paperDirection?: PaperDirection;
|
||||||
inactiveAlpha?: number;
|
inactiveAlpha?: number;
|
||||||
header?: IHeader;
|
header?: IHeader;
|
||||||
|
pageNumber?: IPageNumber;
|
||||||
watermark?: IWatermark;
|
watermark?: IWatermark;
|
||||||
control?: IControlOption;
|
control?: IControlOption;
|
||||||
checkbox?: ICheckboxOption;
|
checkbox?: ICheckboxOption;
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import { RowFlex } from '../dataset/enum/Row'
|
||||||
|
|
||||||
|
export interface IPageNumber {
|
||||||
|
bottom?: number;
|
||||||
|
size?: number;
|
||||||
|
font?: string;
|
||||||
|
color?: string;
|
||||||
|
rowFlex?: RowFlex;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user