Skip to content

Commit 3070ae4

Browse files
linevHackbrettXXX
authored andcommitted
Provide bounding box for plain text - without tspan
While `a` element typically used together with text, one need to have estimation of bounding box for text. To avoid reimplementation of complete text rendering logic just remember text position and sizes as bounding box. Implemented only for plain text, any usage of tspan will not work properly
1 parent c9fb455 commit 3070ae4

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/nodes/text.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ interface TrimInfo {
2424
}
2525

2626
export class TextNode extends GraphicsNode {
27+
private boundingBox: number[] = []
2728
private processTSpans(
2829
textNode: SvgNode,
2930
node: Element,
@@ -159,6 +160,7 @@ export class TextNode extends GraphicsNode {
159160
renderingMode: textRenderingMode === 'fill' ? void 0 : textRenderingMode,
160161
charSpace: charSpace === 0 ? void 0 : charSpace
161162
})
163+
this.boundingBox = [textX + dx - xOffset, textY + dy + 0.1 * pdfFontSize, context.textMeasure.measureTextWidth(transformedText, context.attributeState), pdfFontSize]
162164
}
163165
} else {
164166
// otherwise loop over tspans and position each relative to the previous one
@@ -228,7 +230,7 @@ export class TextNode extends GraphicsNode {
228230
}
229231

230232
protected getBoundingBoxCore(context: Context): Rect {
231-
return defaultBoundingBox(this.element, context)
233+
return this.boundingBox.length > 0 ? this.boundingBox : defaultBoundingBox(this.element, context)
232234
}
233235

234236
protected computeNodeTransformCore(context: Context): Matrix {

0 commit comments

Comments
 (0)