Skip to content

Commit 9ed6e37

Browse files
committed
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
1 parent 649f805 commit 9ed6e37

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/nodes/anchor.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Group } from './group'
2+
import { Context } from '../context/context'
3+
import { getAttribute } from '../utils/node'
4+
5+
export class Anchor extends Group {
6+
protected async renderCore(context: Context): Promise<void> {
7+
await super.renderCore(context)
8+
9+
const href = getAttribute(this.element, context.styleSheets, 'href')
10+
if (href) {
11+
const box = this.getBoundingBox(context)
12+
const scale = context.pdf.internal.scaleFactor
13+
const ph = context.pdf.internal.pageSize.getHeight()
14+
15+
context.pdf.link(scale*(box[0] * context.transform.sx + context.transform.tx),
16+
ph - scale*(box[1] * context.transform.sy + context.transform.ty), scale*box[2], scale*box[3], { url: href })
17+
}
18+
}
19+
}

src/parse.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { RadialGradient } from './nodes/radialgradient'
1717
import { Polyline } from './nodes/polyline'
1818
import { Svg } from './nodes/svg'
1919
import { Group } from './nodes/group'
20+
import { Anchor } from './nodes/anchor'
2021
import cssesc from 'cssesc'
2122
import { ClipPath } from './nodes/clippath'
2223
import { Symbol } from './nodes/symbol'
@@ -29,6 +30,8 @@ export function parse(node: Element, idMap?: { [id: string]: SvgNode }): SvgNode
2930

3031
switch (node.tagName.toLowerCase()) {
3132
case 'a':
33+
svgnode = new Anchor(node, children)
34+
break
3235
case 'g':
3336
svgnode = new Group(node, children)
3437
break

0 commit comments

Comments
 (0)