forked from yWorks/svg2pdf.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkerlist.ts
71 lines (62 loc) · 1.67 KB
/
markerlist.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { Context } from './context/context'
import { MarkerNode } from './nodes/marker'
/**
* @constructor
* @property {Marker[]} markers
*/
export class MarkerList {
public markers: Marker[]
constructor() {
this.markers = []
}
addMarker(markers: Marker): void {
this.markers.push(markers)
}
async draw(context: Context): Promise<void> {
for (let i = 0; i < this.markers.length; i++) {
const marker = this.markers[i]
let tf
const angle = marker.angle,
anchor = marker.anchor
const cos = Math.cos(angle)
const sin = Math.sin(angle)
// position at and rotate around anchor
tf = context.pdf.Matrix(cos, sin, -sin, cos, anchor[0], anchor[1])
// scale with stroke-width
tf = context.pdf.matrixMult(
context.pdf.Matrix(
context.attributeState.strokeWidth,
0,
0,
context.attributeState.strokeWidth,
0,
0
),
tf
)
tf = context.pdf.matrixMult(tf, context.transform)
// as the marker is already scaled by the current line width we must not apply the line width twice!
context.pdf.saveGraphicsState()
await context.refsHandler.getRendered(marker.id, null, node =>
(node as MarkerNode).apply(context)
)
context.pdf.doFormObject(marker.id, tf)
context.pdf.restoreGraphicsState()
}
}
}
/**
* @param {string} id
* @param {[number,number]} anchor
* @param {number} angle
*/
export class Marker {
id: string
anchor: number[]
angle: number
constructor(id: string, anchor: number[], angle: number) {
this.id = id
this.anchor = anchor
this.angle = angle
}
}