Skip to content

Commit

Permalink
feat: text element width #277
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Sep 9, 2023
1 parent 05ddc2d commit bb64626
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/editor/core/draw/Draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1593,6 +1593,10 @@ export class Draw {
this.blockParticle.render(pageNo, element, x, y)
} else {
this.textParticle.record(ctx, element, x, y + offsetY)
// 如果设置字宽需单独绘制
if (element.width) {
this._drawRichText(ctx)
}
}
// 下划线记录
if (element.underline || element.control?.underline) {
Expand Down
14 changes: 14 additions & 0 deletions src/editor/core/draw/particle/TextParticle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ export class TextParticle {
ctx: CanvasRenderingContext2D,
element: IElement
): TextMetrics {
// 优先使用自定义字宽设置
if (element.width) {
const textMetrics = ctx.measureText(element.value)
// TextMetrics是类无法解构
return {
width: element.width,
actualBoundingBoxAscent: textMetrics.actualBoundingBoxAscent,
actualBoundingBoxDescent: textMetrics.actualBoundingBoxDescent,
actualBoundingBoxLeft: textMetrics.actualBoundingBoxLeft,
actualBoundingBoxRight: textMetrics.actualBoundingBoxRight,
fontBoundingBoxAscent: textMetrics.fontBoundingBoxAscent,
fontBoundingBoxDescent: textMetrics.fontBoundingBoxDescent
}
}
const id = `${element.value}${ctx.font}`
const cacheTextMetrics = this.cacheMeasureText.get(id)
if (cacheTextMetrics) {
Expand Down

0 comments on commit bb64626

Please sign in to comment.