Skip to content

Commit

Permalink
Provide href attribute support in a elements (#313)
Browse files Browse the repository at this point in the history
* Fix getBoundingBoxByChildren function

Do not use dummy (all 0) bounding box when search
bonding box for all children.
Otherwise minimal values always will be 0.
Also take into account transformation attributes of child node -
it may differ from child to child.
Apply scale and translation, but not rotation yet

* 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

* Introduce Anchor class

It derives from Group class and uses same rendering.
Plus if `href` attribute specified - adds link with bounding box.
Requires usage of scale and page height values -
otherwise created link area does not match drawn text

* Add test for the anchor element

This test also can be used to demonstrate missing
support of dominant-baseline attribute of the text

* Update some other refs

Changes because of new implementation of bounding box
  • Loading branch information
linev authored Feb 12, 2025
1 parent 36bb1b5 commit 5f6be78
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 3 deletions.
19 changes: 19 additions & 0 deletions src/nodes/anchor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Group } from './group'
import { Context } from '../context/context'
import { getAttribute } from '../utils/node'

export class Anchor extends Group {
protected async renderCore(context: Context): Promise<void> {
await super.renderCore(context)

const href = getAttribute(this.element, context.styleSheets, 'href')
if (href) {
const box = this.getBoundingBox(context)
const scale = context.pdf.internal.scaleFactor
const ph = context.pdf.internal.pageSize.getHeight()

context.pdf.link(scale*(box[0] * context.transform.sx + context.transform.tx),
ph - scale*(box[1] * context.transform.sy + context.transform.ty), scale*box[2], scale*box[3], { url: href })
}
}
}
4 changes: 3 additions & 1 deletion src/nodes/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface TrimInfo {
}

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

protected getBoundingBoxCore(context: Context): Rect {
return defaultBoundingBox(this.element, context)
return this.boundingBox.length > 0 ? this.boundingBox : defaultBoundingBox(this.element, context)
}

protected computeNodeTransformCore(context: Context): Matrix {
Expand Down
3 changes: 3 additions & 0 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { RadialGradient } from './nodes/radialgradient'
import { Polyline } from './nodes/polyline'
import { Svg } from './nodes/svg'
import { Group } from './nodes/group'
import { Anchor } from './nodes/anchor'
import cssesc from 'cssesc'
import { ClipPath } from './nodes/clippath'
import { Symbol } from './nodes/symbol'
Expand All @@ -29,6 +30,8 @@ export function parse(node: Element, idMap?: { [id: string]: SvgNode }): SvgNode

switch (node.tagName.toLowerCase()) {
case 'a':
svgnode = new Anchor(node, children)
break
case 'g':
svgnode = new Group(node, children)
break
Expand Down
15 changes: 13 additions & 2 deletions src/utils/bbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,20 @@ export function getBoundingBoxByChildren(context: Context, svgnode: SvgNode): nu
if (getAttribute(svgnode.element, context.styleSheets, 'display') === 'none') {
return [0, 0, 0, 0]
}
let boundingBox = [0, 0, 0, 0]
let boundingBox : number[] = []
svgnode.children.forEach(child => {
const nodeBox = child.getBoundingBox(context)
if ((nodeBox[0] === 0) && (nodeBox[1] === 0) && (nodeBox[2] === 0) && (nodeBox[3] === 0))
return
const transform = child.computeNodeTransform(context)
// TODO: take into account rotation matrix
nodeBox[0] = nodeBox[0] * transform.sx + transform.tx
nodeBox[1] = nodeBox[1] * transform.sy + transform.ty
nodeBox[2] = nodeBox[2] * transform.sx
nodeBox[3] = nodeBox[3] * transform.sy
if (boundingBox.length === 0)
boundingBox = nodeBox
else
boundingBox = [
Math.min(boundingBox[0], nodeBox[0]),
Math.min(boundingBox[1], nodeBox[1]),
Expand All @@ -19,7 +30,7 @@ export function getBoundingBoxByChildren(context: Context, svgnode: SvgNode): nu
Math.min(boundingBox[1], nodeBox[1])
]
})
return boundingBox
return boundingBox.length === 0 ? [0, 0, 0, 0] : boundingBox
}

export function defaultBoundingBox(element: Element, context: Context): Rect {
Expand Down
1 change: 1 addition & 0 deletions test/common/tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
window.tests = [
'anchor',
'attribute-style-precedence',
'clippath',
'clippath-empty',
Expand Down
Binary file added test/specs/anchor/reference.pdf
Binary file not shown.
34 changes: 34 additions & 0 deletions test/specs/anchor/spec.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/specs/complete-computer-network/reference.pdf
Binary file not shown.
Binary file modified test/specs/complete-movies/reference.pdf
Binary file not shown.
Binary file modified test/specs/complete-organization-chart-new/reference.pdf
Binary file not shown.
Binary file modified test/specs/complete-organization-chart/reference.pdf
Binary file not shown.
Binary file modified test/specs/complete-social-network/reference.pdf
Binary file not shown.

0 comments on commit 5f6be78

Please sign in to comment.