Skip to content

Commit

Permalink
fix pattern position and scaling (yWorks#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Bretzigheimer authored and Rootek committed Jun 12, 2024
1 parent 0b3a452 commit ef851d3
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
34 changes: 21 additions & 13 deletions src/nodes/pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,35 @@ export class Pattern extends NonRenderedNode {
return
}

const scaleFactor = context.pdf.internal.scaleFactor

// the transformations directly at the node are written to the pattern transformation matrix
const bBox = this.getBoundingBox(context)
const x = context.svg2pdfParameters.x ?? 0
const y = context.svg2pdfParameters.y ?? 0
const [patternX, patternY, width, height] = this.getBoundingBox(context)
const startX = (patternX + x) * scaleFactor
const startY = (patternY + y) * scaleFactor
const endX = startX + width * scaleFactor
const endY = startY + height * scaleFactor
const pattern = new TilingPattern(
[bBox[0], bBox[1], bBox[0] + bBox[2], bBox[1] + bBox[3]],
bBox[2],
bBox[3]
[startX, startY, endX, endY],
width * scaleFactor,
height * scaleFactor
)

context.pdf.beginTilingPattern(pattern)
// continue without transformation

for (const child of this.children) {
await child.render(
new Context(context.pdf, {
attributeState: context.attributeState,
refsHandler: context.refsHandler,
styleSheets: context.styleSheets,
viewport: context.viewport,
svg2pdfParameters: context.svg2pdfParameters
})
)
const childContext = new Context(context.pdf, {
attributeState: context.attributeState,
refsHandler: context.refsHandler,
styleSheets: context.styleSheets,
viewport: context.viewport,
svg2pdfParameters: context.svg2pdfParameters,
transform: context.pdf.Matrix(scaleFactor, 0, 0, scaleFactor, startX, startY)
})
await child.render(childContext)
}
context.pdf.endTilingPattern(id, pattern)
}
Expand Down
2 changes: 2 additions & 0 deletions test/common/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ window.tests = [
'nested-tspans',
'opacity-and-rgba',
'path-arc-support',
['pattern-offset', undefined, { x: 10, y: 10 }],
['pattern-scale', [400, 300, 'mm'], {}],
'pattern-units',
'patterns',
'polyline',
Expand Down
Binary file added test/specs/pattern-offset/reference.pdf
Binary file not shown.
8 changes: 8 additions & 0 deletions test/specs/pattern-offset/spec.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions test/specs/pattern-scale/spec.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion test/unit/all.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ for (const test of window.tests) {
it(`testing ${name}`, async function() {
const width = jsPDFOptions ? jsPDFOptions[0] : svgElement.width.baseVal.value
const height = jsPDFOptions ? jsPDFOptions[1] : svgElement.height.baseVal.value
const pdf = new jsPDF(width > height ? 'l' : 'p', 'pt', [width, height])
const unit = jsPDFOptions ? jsPDFOptions[2] || 'pt' : 'pt'
const pdf = new jsPDF(width > height ? 'l' : 'p', unit, [width, height])

if (name === 'custom-fonts') {
const filename = '/base/test/specs/custom-fonts/Batang.ttf'
Expand Down

1 comment on commit ef851d3

@yGuy
Copy link

@yGuy yGuy commented on ef851d3 Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks interesting! Thanks for spending time on this. Would you consider rebasing this and creating a PR?! Thanks.

Please sign in to comment.