div.js
4.95 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
* 2D Vector.
* Container for x and y coordinates.
*/
class Vector {
constructor(x,y) {
this.x = x;
this.y = y;
}
/**
* @param {Vector} vector
*/
addv(vector) {
return new Vector(
this.x + vector.x,
this.y + vector.y
);
}
/**
* @param {number} x
* @param {number} y
*/
addxy(x = 0,y = 0) {
return new Vector(
this.x + x,
this.y + y
);
}
/**
* @param {number} n
*/
mult(n) {
return new Vector(
this.x * n,
this.y * n
);
}
}
class Line {
/**
* @param {Vector} beggining
* @param {Vector} end
*/
constructor(beggining, end) {
this.beggining = beggining;
this.end = end;
}
/**
* @param {jsPDF} pdf
*/
draw(pdf) {
pdf.line(
this.beggining.x,
this.beggining.y,
this.end.x,
this.end.y
);
}
}
class Text {
/**
* Both arguments should be real coordinates and not a percentage based on parent element
* @param {string} text
* @param {Vector} position
*/
constructor(text, position) {
this.text = text;
this.position = position;
}
/**
* @param {jsPDF} pdf
*/
draw(pdf) {
pdf.text(
this.text,
this.position.x,
this.position.y
);
}
}
/**
* This class does NOT regulate if its children go out of its cofinements.
* It does NOT resize or realocate its children to fit, in any way.
* Its only purpose is to provide percentage based relative coordinates.
*/
class Div {
/**
* @param {Vector} position
* @param {Vector} size
* @param {bool} draw
*/
constructor(position, size, draw = false) {
this.position = position;
this.size = size;
this.drawOutline = draw;
this.children = [];
}
/** Allows any position you want **/
addAbsoluteChild(child) {
if ("draw" in child === false)
console.error("the object passed needs to have a 'draw' method that takes a jsPDF object");
this.children.push(child);
return this.children[this.children.length - 1];
}
/** Allows only positions inside the Div **/
addRelativeChild(child) {
if ("draw" in child === false)
console.error("the object passed needs to have a 'draw' method that takes a jsPDF object");
if ("position" in child)
child.position = this._getDrawCoordinates(child.position);
if ("size" in child)
child.size = this._getSizeCoordinates(child.size);
this.children.push(child);
return this.children[this.children.length - 1];
}
addAbsoluteChildren(children) {
const pointers = [];
children.forEach(c => pointers.push(this.addAbsoluteChild(c)));
return pointers;
}
addRelativeChildren(children) {
const pointers = [];
children.forEach(c => pointers.push(this.addRelativeChild(c)));
return pointers;
}
/** Allows any position you want relative to the position of this Div. Percentage based **/
getAbsoluteVector(vector) {
return this.position.addxy(
(this.size.x * vector.x / 100),
(this.size.y * vector.y / 100)
);
}
/**
* Compiles the true position coordinates based on percentage relative to parent element
* Assumes the elements grow to the right and down
* @param {Vector} position
*/
_getDrawCoordinates(position) {
if (position.x < 0 || position.x > 100 || position.y < 0 || position.y > 100)
console.error("position components need to be real numbers in the range [0;100]");
return this.position.addxy(
(this.size.x * position.x / 100),
(this.size.y * position.y / 100)
);
}
/**
* Compiles the true size based on percentage relative to parent element
* Assumes the elements grow to the right and down
* @param {Vector?} size
*/
_getSizeCoordinates(size) {
if (size.x < 0 || size.x > 100 || size.y < 0 || size.y > 100)
console.error("size components need to be real numbers in the range [0;100]");
return new Vector (
this.size.x * size.x / 100,
this.size.y * size.y / 100,
);
}
draw(pdf) {
/** Draw the confines of the div **/
if (this.drawOutline) {
/** Upper **/
this.addAbsoluteChild(new Line(
this.position,
this.position.addxy(this.size.x, 0)
));
/** Lower **/
this.addAbsoluteChild(new Line(
this.position.addxy(0, this.size.y),
this.position.addv(this.size)
));
/** Left **/
this.addAbsoluteChild(new Line(
this.position,
this.position.addxy(0, this.size.y)
));
/** Right **/
this.addAbsoluteChild(new Line(
this.position.addxy(this.size.x, 0),
this.position.addv(this.size)
));
}
/** Recursion **/
this.children.forEach( child => child.draw(pdf) );
}
}
const Pos = {
"beg": 0,
"beg_margin": 5,
"quarter": 25,
"third": 33.33,
"middle": 50,
"two_thirds": 66.67,
"three_quarters": 75,
"end_margin": 95,
"end": 100
};
function compile(tree, pdf) {
pdf.setFont("Courier");
pdf.setFontType("normal");
pdf.setFontSize(10);
tree.children.forEach(child => {
switch (child.constructor.name) {
case "Text":
pdf.text(child.text, child.position.x, child.position.y);
break;
case "Div":
if (child.drawOutline) {
child.drawDivBox(pdf);
}
compile(child, pdf);
break;
}
});
return pdf;
}