-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPDFTransformTrait.php
More file actions
276 lines (246 loc) · 8.33 KB
/
PDFTransformTrait.php
File metadata and controls
276 lines (246 loc) · 8.33 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
declare(strict_types=1);
namespace FPDF\Scripts\PDFTransform;
//https://www.fpdf.org/en/script/script79.php
trait PDFTransformTrait {
/**
* Save the current graphic state
*
* Use this before calling any tranformation.
*
* @return void
*/
public function StartTransform () : void {
$this->_out('q');
}
/**
* Scaling is obtained by [ sx 0 0 1 tx ty ].
*
* Scaling factor for height is unchanged
*
* @param float $s_x Scaling factor for width as percent. 0 is not allowed.
* @param null|float $x Abscissa of the scaling center. Default is current x position
* @param null|float $y Ordinate of the scaling center. Default is current y position
* @return void
*/
public function ScaleX (float $s_x, ?float $x = null, ?float $y = null) : void {
$this->Scale($s_x, 100, $x, $y);
}
/**
* Scaling is obtained by [ sy 0 0 1 tx ty ].
*
* Scaling factor for width is unchanged
*
* @param float $s_y Scaling factor for height as percent. 0 is not allowed.
* @param null|float $x Abscissa of the scaling center. Default is current x position
* @param null|float $y Ordinate of the scaling center. Default is current y position
* @return void
*/
public function ScaleY (float $s_y, ?float $x = null, ?float $y = null) : void {
$this->Scale(100, $s_y, $x, $y);
}
/**
* Scaling is obtained by [ s 0 0 s tx ty ].
*
* @param float $s Scaling factor for width and height as percent. 0 is not allowed.
* @param null|float $x Abscissa of the scaling center. Default is current x position
* @param null|float $y Ordinate of the scaling center. Default is current y position
* @return void
*/
public function ScaleXY(float $s, ?float $x = null, ?float $y = null) : void {
$this->Scale($s, $s, $x, $y);
}
/**
* Scaling is obtained by [ sx 0 0 sy tx ty ].
*
* This scales the coordinates so that 1 unit in the horizontal and vertical dimensions of the new coordinate system is the same size as sx and sy units, respectively, in the previous coordinate system.
*
* @param float $s_x Scaling factor for width as percent. 0 is not allowed.
* @param float $s_y Scaling factor for height as percent. 0 is not allowed.
* @param null|float $x Abscissa of the scaling center. Default is current x position
* @param null|float $y Ordinate of the scaling center. Default is current y position
* @return void
*/
public function Scale (float $s_x, float $s_y, ?float $x = null, ?float $y = null) : void {
$x = $x ?? $this->x;
$y = $y ?? $this->y;
if ($s_x == 0 || $s_y == 0) {
$this->Error('Please use values unequal to zero for Scaling');
}
$y = ($this->h - $y) * $this->k;
$x *= $this->k;
$s_x /= 100;
$s_y /= 100;
//calculate elements of transformation matrix
//[ sx 0 0 sy tx ty ]
$a = $s_x;
$b = 0;
$c = 0;
$d = $s_y;
$e = $x * (1 - $s_x);
$f = $y * (1 - $s_y);
//scale the coordinate system
$this->Transform([$a, $b, $c, $d, $e, $f]);
}
/**
* Alias for scaling -100% in x-direction
*
* @param null|float $x Abscissa of the axis of reflection
* @return void
*/
public function MirrorH (?float $x = null) : void {
$this->Scale(-100, 100, $x);
}
/**
* Alias for scaling -100% in y-direction
*
* @param null|float $y Ordinate of the axis of reflection
* @return void
*/
public function MirrorV (?float $y = null) : void {
$this->Scale(100, -100, null, $y);
}
/**
* Point reflection on point (x, y). (alias for scaling -100 in x- and y-direction)
*
* @param null|float $x Abscissa of the point. Default is current x position
* @param null|float $y Ordinate of the point. Default is current y position
* @return void
*/
public function MirrorP (?float $x = null, ?float $y = null) : void {
$this->Scale(-100, -100, $x, $y);
}
/**
* Reflection against a straight line through point (x, y) with the gradient angle (angle).
*
* @param float $angle Gradient angle of the straight line. Default is 0 (horizontal line).
* @param null|float $x Abscissa of the point. Default is current x position
* @param null|float $y Ordinate of the point. Default is current y position
* @return void
*/
public function MirrorL (float $angle = 0, ?float $x = null, ?float $y = null) : void {
$this->Scale(-100, 100, $x, $y);
$this->Rotate(-2 * ($angle - 90), $x, $y);
}
/**
* Translate to the right
*
* @param float $t_x Movement to the right
* @return void
*/
public function TranslateX (float $t_x) : void {
$this->Translate($t_x, 0);
}
/**
* Translate to the bottom
*
* @param float $t_y Movement to the bottom
* @return void
*/
public function TranslateY (float $t_y) : void {
$this->Translate(0, $t_y);
}
/**
* Translate to the right and to the bottom
*
* @param float $t_x Movement to the right
* @param float $t_y Movement to the bottom
* @return void
*/
public function Translate (float $t_x, float $t_y) : void {
//calculate elements of transformation matrix
$a = 1;
$b = 0;
$c = 0;
$d = 1;
$e = $t_x * $this->k;
$f = -$t_y * $this->k;
//translate the coordinate system
$this->Transform([$a, $b, $c, $d, $e, $f]);
}
/**
* Rotate counter-clockwise
*
* @param float $angle Angle in degrees for counter-clockwise rotation
* @param null|float $x Abscissa of the rotation center. Default is current x position
* @param null|float $y Ordinate of the rotation center. Default is current y position
* @return void
*/
public function Rotate (float $angle, ?float $x = null, ?float $y = null) : void {
$x = $x ?? $this->x;
$y = $y ?? $this->y;
$y = ($this->h - $y) * $this->k;
$x *= $this->k;
//calculate elements of transformation matrix
$a = cos(deg2rad($angle));
$b = sin(deg2rad($angle));
$c = -$b;
$d = $a;
$e = $x + $b * $y - $a * $x;
$f = $y - $a * $y - $b * $x;
//rotate the coordinate system around ($x,$y)
$this->Transform([$a, $b, $c, $d, $e, $f]);
}
/**
*
*
* @param float $angle_x Angle in degrees between -90 (skew to the left) and 90 (skew to the right)
* @param null|float $x Abscissa of the skewing center. default is current x position
* @param null|float $y Ordinate of the skewing center. default is current y position
* @return void
*/
public function SkewX (float $angle_x, ?float $x = null, ?float $y = null) : void {
$this->Skew($angle_x, 0, $x, $y);
}
/**
* @param float $angle_y Angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
* @param null|float $x Abscissa of the skewing center. default is current x position
* @param null|float $y Ordinate of the skewing center. default is current y position
* @return void
*/
public function SkewY (float $angle_y, ?float $x = null, ?float $y = null) : void {
$this->Skew(0, $angle_y, $x, $y);
}
/**
* @param float $angle_x Angle in degrees between -90 (skew to the left) and 90 (skew to the right)
* @param float $angle_y Angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
* @param null|float $x Abscissa of the skewing center. default is current x position
* @param null|float $y Ordinate of the skewing center. default is current y position
* @return void
*/
public function Skew (float $angle_x, float $angle_y, ?float $x = null, ?float $y = null) : void {
$x = $x ?? $this->x;
$y = $y ?? $this->y;
if ($angle_x <= -90 || $angle_x >= 90 || $angle_y <= -90 || $angle_y >= 90) {
$this->Error('Please use values between -90° and 90° for skewing');
}
$x *= $this->k;
$y = ($this->h - $y) * $this->k;
//calculate elements of transformation matrix
$a = 1;
$b = tan(deg2rad($angle_y));
$c = tan(deg2rad($angle_x));
$d = 1;
$e = -$c * $y;
$f = -$b * $x;
//skew the coordinate system
$this->Transform([$a, $b, $c, $d, $e, $f]);
}
/**
* Concatenate matrix to current transformation matrix
*
* @param float[] $tm Transformation matrix [ a, b, c, d, e, f ]
* @return void
*/
public function Transform (array $tm) : void {
$this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', ...$tm));
}
/**
* Restores the normal painting and placing behavior as it was before calling StartTransform().
*
* @return void
*/
public function StopTransform () : void {
$this->_out('Q');
}
}