@ -1,9 +1,19 @@
import { NBSP , ZERO } from '../../../dataset/constant/Common'
import { VerticalAlign } from '../../../dataset/enum/VerticalAlign'
import { DeepRequired } from '../../../interface/Common'
import { IEditorOption } from '../../../interface/Editor'
import { IElement } from '../../../interface/Element'
import { IRow Element } from '../../../interface/Row'
import { IRow , IRow Element } from '../../../interface/Row'
import { Draw } from '../Draw'
interface ICheckboxRenderOption {
ctx : CanvasRenderingContext2D
x : number
y : number
row : IRow
index : number
}
export class CheckboxParticle {
private draw : Draw
private options : DeepRequired < IEditorOption >
@ -28,17 +38,41 @@ export class CheckboxParticle {
} )
}
public render (
ctx : CanvasRenderingContext2D ,
element : IRowElement ,
x : number ,
y : number
) {
public render ( payload : ICheckboxRenderOption ) {
const { ctx , x , index , row } = payload
let { y } = payload
const {
checkbox : { gap , lineWidth , fillStyle , strokeStyle } ,
checkbox : { gap , lineWidth , fillStyle , strokeStyle , verticalAlign } ,
scale
} = this . options
const { metrics , checkbox } = element
const { metrics , checkbox } = row . elementList [ index ]
// 垂直布局设置
if (
verticalAlign === VerticalAlign . TOP ||
verticalAlign === VerticalAlign . MIDDLE
) {
let nextIndex = index + 1
let nextElement : IRowElement | null = null
while ( nextIndex < row . elementList . length ) {
nextElement = row . elementList [ nextIndex ]
if ( nextElement . value !== ZERO && nextElement . value !== NBSP ) break
nextIndex ++
}
// 以后一个非空格元素为基准
if ( nextElement ) {
const {
metrics : { boundingBoxAscent , boundingBoxDescent }
} = nextElement
const textHeight = boundingBoxAscent + boundingBoxDescent
if ( textHeight > metrics . height ) {
if ( verticalAlign === VerticalAlign . TOP ) {
y -= boundingBoxAscent - metrics . height
} else if ( verticalAlign === VerticalAlign . MIDDLE ) {
y -= ( textHeight - metrics . height ) / 2
}
}
}
}
// left top 四舍五入避免1像素问题
const left = Math . round ( x + gap * scale )
const top = Math . round ( y - metrics . height + lineWidth )