Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.