improve: print quality #185

pr675
Hufe921 3 years ago
parent 1bd2e455a5
commit 842b4fca07

@ -29,7 +29,7 @@ const {
用法: 用法:
```javascript ```javascript
const base64StringList = await instance.command.getImage() const base64StringList = await instance.command.getImage(pixelRatio?: number)
``` ```
## getWordCount ## getWordCount

@ -52,6 +52,7 @@ interface IEditorOption {
paperDirection?: PaperDirection; // 纸张方向:纵向、横向 paperDirection?: PaperDirection; // 纸张方向:纵向、横向
inactiveAlpha?: number; // 正文内容失焦时透明度。默认值0.6 inactiveAlpha?: number; // 正文内容失焦时透明度。默认值0.6
historyMaxRecordCount: number; // 历史撤销重做最大记录次数。默认100次 historyMaxRecordCount: number; // 历史撤销重做最大记录次数。默认100次
printPixelRatio: number; // 打印像素比率值越大越清晰但尺寸越大。默认3
wordBreak: WordBreak; // 单词与标点断行BREAK_WORD首行不出现标点&单词不拆分、BREAK_ALL按字符宽度撑满后折行。默认BREAK_WORD wordBreak: WordBreak; // 单词与标点断行BREAK_WORD首行不出现标点&单词不拆分、BREAK_ALL按字符宽度撑满后折行。默认BREAK_WORD
watermark?: IWatermark; // 水印信息。{data:string; color?:string; opacity?:number; size?:number; font?:string;} watermark?: IWatermark; // 水印信息。{data:string; color?:string; opacity?:number; size?:number; font?:string;}
control?: IControlOption; // 控件信息。 {placeholderColor?:string; bracketColor?:string; prefix?:string; postfix?:string;} control?: IControlOption; // 控件信息。 {placeholderColor?:string; bracketColor?:string; prefix?:string; postfix?:string;}

@ -1014,6 +1014,7 @@ export class CommandAdapt {
let endTd = curTrList[endTrIndex!].tdList[endTdIndex!] let endTd = curTrList[endTrIndex!].tdList[endTdIndex!]
// 交换起始位置 // 交换起始位置
if (startTd.x! > endTd.x! || startTd.y! > endTd.y!) { if (startTd.x! > endTd.x! || startTd.y! > endTd.y!) {
// prettier-ignore
[startTd, endTd] = [endTd, startTd] [startTd, endTd] = [endTd, startTd]
} }
const startColIndex = startTd.colIndex! const startColIndex = startTd.colIndex!
@ -1628,13 +1629,13 @@ export class CommandAdapt {
} }
public async print() { public async print() {
const { scale } = this.options const { scale, printPixelRatio } = this.options
if (scale !== 1) { if (scale !== 1) {
this.draw.setPageScale(1) this.draw.setPageScale(1)
} }
const width = this.draw.getOriginalWidth() const width = this.draw.getOriginalWidth()
const height = this.draw.getOriginalHeight() const height = this.draw.getOriginalHeight()
const base64List = await this.draw.getDataURL() const base64List = await this.draw.getDataURL(printPixelRatio)
printImageBase64(base64List, width, height) printImageBase64(base64List, width, height)
if (scale !== 1) { if (scale !== 1) {
this.draw.setPageScale(scale) this.draw.setPageScale(scale)
@ -1671,8 +1672,8 @@ export class CommandAdapt {
}) })
} }
public getImage(): Promise<string[]> { public getImage(pixelRatio?: number): Promise<string[]> {
return this.draw.getDataURL() return this.draw.getDataURL(pixelRatio)
} }
public getValue(options?: IGetValueOption): IEditorResult { public getValue(options?: IGetValueOption): IEditorResult {

@ -86,6 +86,7 @@ export class Draw {
private pageList: HTMLCanvasElement[] private pageList: HTMLCanvasElement[]
private ctxList: CanvasRenderingContext2D[] private ctxList: CanvasRenderingContext2D[]
private pageNo: number private pageNo: number
private pagePixelRatio: number | null
private mode: EditorMode private mode: EditorMode
private options: DeepRequired<IEditorOption> private options: DeepRequired<IEditorOption>
private position: Position private position: Position
@ -151,6 +152,7 @@ export class Draw {
this.pageList = [] this.pageList = []
this.ctxList = [] this.ctxList = []
this.pageNo = 0 this.pageNo = 0
this.pagePixelRatio = null
this.mode = options.mode this.mode = options.mode
this.options = options this.options = options
this.headerElementList = data.header || [] this.headerElementList = data.header || []
@ -627,7 +629,10 @@ export class Draw {
return this.rowList.length return this.rowList.length
} }
public async getDataURL(): Promise<string[]> { public async getDataURL(pixelRatio?: number): Promise<string[]> {
if (pixelRatio) {
this.setPagePixelRatio(pixelRatio)
}
this.render({ this.render({
isLazy: false, isLazy: false,
isCompute: false, isCompute: false,
@ -635,7 +640,11 @@ export class Draw {
isSubmitHistory: false isSubmitHistory: false
}) })
await this.imageObserver.allSettled() await this.imageObserver.allSettled()
return this.pageList.map(c => c.toDataURL()) const dataUrlList = this.pageList.map(c => c.toDataURL())
if (pixelRatio) {
this.setPagePixelRatio(null)
}
return dataUrlList
} }
public getPainterStyle(): IElementStyle | null { public getPainterStyle(): IElementStyle | null {
@ -678,7 +687,7 @@ export class Draw {
// 纸张大小重置 // 纸张大小重置
if (payload === PageMode.PAGING) { if (payload === PageMode.PAGING) {
const { height } = this.options const { height } = this.options
const dpr = window.devicePixelRatio const dpr = this.getPagePixelRatio()
const canvas = this.pageList[0] const canvas = this.pageList[0]
canvas.style.height = `${height}px` canvas.style.height = `${height}px`
canvas.height = height * dpr canvas.height = height * dpr
@ -704,7 +713,7 @@ export class Draw {
} }
public setPageScale(payload: number) { public setPageScale(payload: number) {
const dpr = window.devicePixelRatio const dpr = this.getPagePixelRatio()
this.options.scale = payload this.options.scale = payload
const width = this.getWidth() const width = this.getWidth()
const height = this.getHeight() const height = this.getHeight()
@ -726,8 +735,23 @@ export class Draw {
} }
} }
public getPagePixelRatio(): number {
return this.pagePixelRatio || window.devicePixelRatio
}
public setPagePixelRatio(payload: number | null) {
if (
(!this.pagePixelRatio && payload === window.devicePixelRatio) ||
payload === this.pagePixelRatio
) {
return
}
this.pagePixelRatio = payload
this.setPageDevicePixel()
}
public setPageDevicePixel() { public setPageDevicePixel() {
const dpr = window.devicePixelRatio const dpr = this.getPagePixelRatio()
const width = this.getWidth() const width = this.getWidth()
const height = this.getHeight() const height = this.getHeight()
this.pageList.forEach((p, i) => { this.pageList.forEach((p, i) => {
@ -744,7 +768,7 @@ export class Draw {
public setPaperSize(width: number, height: number) { public setPaperSize(width: number, height: number) {
this.options.width = width this.options.width = width
this.options.height = height this.options.height = height
const dpr = window.devicePixelRatio const dpr = this.getPagePixelRatio()
const realWidth = this.getWidth() const realWidth = this.getWidth()
const realHeight = this.getHeight() const realHeight = this.getHeight()
this.container.style.width = `${realWidth}px` this.container.style.width = `${realWidth}px`
@ -762,7 +786,7 @@ export class Draw {
} }
public setPaperDirection(payload: PaperDirection) { public setPaperDirection(payload: PaperDirection) {
const dpr = window.devicePixelRatio const dpr = this.getPagePixelRatio()
this.options.paperDirection = payload this.options.paperDirection = payload
const width = this.getWidth() const width = this.getWidth()
const height = this.getHeight() const height = this.getHeight()
@ -878,7 +902,7 @@ export class Draw {
canvas.setAttribute('data-index', String(pageNo)) canvas.setAttribute('data-index', String(pageNo))
this.pageContainer.append(canvas) this.pageContainer.append(canvas)
// 调整分辨率 // 调整分辨率
const dpr = window.devicePixelRatio const dpr = this.getPagePixelRatio()
canvas.width = width * dpr canvas.width = width * dpr
canvas.height = height * dpr canvas.height = height * dpr
canvas.style.cursor = 'text' canvas.style.cursor = 'text'
@ -891,7 +915,7 @@ export class Draw {
} }
private _initPageContext(ctx: CanvasRenderingContext2D) { private _initPageContext(ctx: CanvasRenderingContext2D) {
const dpr = window.devicePixelRatio const dpr = this.getPagePixelRatio()
ctx.scale(dpr, dpr) ctx.scale(dpr, dpr)
// 重置以下属性是因部分浏览器(chrome)会应用css样式 // 重置以下属性是因部分浏览器(chrome)会应用css样式
ctx.letterSpacing = '0px' ctx.letterSpacing = '0px'
@ -1309,7 +1333,7 @@ export class Draw {
pageRowList[0] = this.rowList pageRowList[0] = this.rowList
// 重置高度 // 重置高度
pageHeight += this.rowList.reduce((pre, cur) => pre + cur.height, 0) pageHeight += this.rowList.reduce((pre, cur) => pre + cur.height, 0)
const dpr = window.devicePixelRatio const dpr = this.getPagePixelRatio()
const pageDom = this.pageList[0] const pageDom = this.pageList[0]
const pageDomHeight = Number(pageDom.style.height.replace('px', '')) const pageDomHeight = Number(pageDom.style.height.replace('px', ''))
if (pageHeight > pageDomHeight) { if (pageHeight > pageDomHeight) {

@ -144,6 +144,7 @@ export default class Editor {
inactiveAlpha: 0.6, inactiveAlpha: 0.6,
historyMaxRecordCount: 100, historyMaxRecordCount: 100,
wordBreak: WordBreak.BREAK_WORD, wordBreak: WordBreak.BREAK_WORD,
printPixelRatio: 3,
...options, ...options,
header: headerOptions, header: headerOptions,
footer: footerOptions, footer: footerOptions,

@ -58,6 +58,7 @@ export interface IEditorOption {
paperDirection?: PaperDirection paperDirection?: PaperDirection
inactiveAlpha?: number inactiveAlpha?: number
historyMaxRecordCount?: number historyMaxRecordCount?: number
printPixelRatio?: number
wordBreak?: WordBreak wordBreak?: WordBreak
header?: IHeader header?: IHeader
footer?: IFooter footer?: IFooter

Loading…
Cancel
Save