-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg.php
469 lines (401 loc) · 10.8 KB
/
svg.php
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<?php
namespace HitchinHackspace\SVG;
use ArrayIterator;
/**
* Something that can be embedded in an SVG document (despite the name, not necessarily a node)
* @package HitchinHackspace\SVG
*/
abstract class Node {
/** Write this object to standard out */
abstract function output();
}
/**
* Generate a string encoding the specified parameter dictionary.
*
* @param array<int, mixed> $attrs
* @return string
*/
function formatAttributes($attrs) {
$result = '';
foreach ($attrs as $key => $value) {
$key = htmlspecialchars($key);
$value = htmlspecialchars($value);
$result .= " $key=\"$value\"";
}
return $result;
}
/**
* A trait for Nodes that have descendants.
*/
trait Container {
/** @var Node[] */
protected $children = [];
/**
* @param Node $child
* @return void
*/
function addChild($child) {
$this->children[] = $child;
}
}
/**
* Basic SVG path style properties.
*/
class Style {
/** @var string $stroke Stroke colour */
protected $stroke;
/** @var string $fill Fill colour */
protected $fill;
/** @var float $strokeWidth Stroke width */
protected $strokeWidth;
/** @var float $strokeOpacity Stroke opacity */
protected $strokeOpacity;
/** @var float $fillOpacity Stroke width */
protected $fillOpacity;
/**
* @param string $stroke
* @param string $fill
* @param float $strokeWidth
* @param float $strokeOpacity
* @param float $fillOpacity
*/
function __construct($stroke = 'black', $fill = 'black', $strokeWidth = 1, $strokeOpacity = 1, $fillOpacity = 1) {
$this->stroke = $stroke;
$this->fill = $fill;
$this->strokeWidth = $strokeWidth;
$this->strokeOpacity = $strokeOpacity;
$this->fillOpacity = $fillOpacity;
}
/**
* Convert this style to an attribute dictionary for embedding in a node.
*
* @return array<string, mixed>
*/
function getAttrs() {
$attrs = [
'stroke' => $this->stroke,
'fill' => $this->fill,
'stroke-width' => $this->strokeWidth,
'stroke-opacity' => $this->strokeOpacity,
'fill-opacity' => $this->fillOpacity
];
return $attrs;
}
}
/**
* A basic rectangle.
*/
class Rect extends Node {
/** @var float */
protected $x;
/** @var float */
protected $y;
/** @var float */
protected $width;
/** @var float */
protected $height;
/** @var Style */
protected $style;
/**
* @param float $x
* @param float $y
* @param float $width
* @param float $height
* @param Style $style
*/
public function __construct($x, $y, $width, $height, $style) {
$this->x = $x; $this->y = $y;
$this->width = $width; $this->height = $height;
$this->style = $style;
}
function output() {
$attrs = $this->style->getAttrs();
$attrs += [
'x' => $this->x,
'y' => $this->y,
'width' => $this->width,
'height' => $this->height
];
?>
<rect <?= formatAttributes($attrs) ?> />
<?php
}
}
/**
* A basic (single) line.
*/
class Line extends Node {
/** @var float */
protected $x1;
/** @var float */
protected $y1;
/** @var float */
protected $x2;
/** @var float */
protected $y2;
/** @var Style */
protected $style;
/**
* @param float $x1
* @param float $y1
* @param float $x2
* @param float $y2
* @param float $style
*/
public function __construct($x1, $y1, $x2, $y2, $style) {
$this->x1 = $x1; $this->y1 = $y1;
$this->x2 = $x2; $this->y2 = $y2;
$this->style = $style;
}
function output() {
$attrs = $this->style->getAttrs();
$attrs += [
'x1' => $this->x1,
'y1' => $this->y1,
'x2' => $this->x2,
'y2' => $this->y2
];
?>
<line <?= formatAttributes($attrs) ?> />
<?php
}
}
/**
* A simple group (<g>) element.
*/
class Group extends Node {
use Container;
function output() {
?>
<g>
<?php
foreach ($this->children as $child)
$child->output();
?>
</g>
<?php
}
}
/**
* A text (<text>) element.
*/
class Text extends Node {
protected $attributes = [];
/** @var string */
protected $content;
/**
* @param float $x
* @param float $y
* @param string $text
*/
public function __construct($x, $y, $text) {
$this->attributes = [
'x' => $x,
'y' => $y
];
$this->content = $text;
}
function output() {
?>
<text <?= formatAttributes($this->attributes) ?>><?= $this->content ?></text>
<?php
}
}
/**
* An SVG document.
*/
class Document extends Node {
use Container;
/** @var float */
protected $width;
/** @var float */
protected $height;
/**
* @param float $width
* @param float $height
*/
public function __construct($width, $height) {
$this->width = $width;
$this->height = $height;
}
/** @return float */
public function width() { return $this->width; }
/** @return float */
public function height() { return $this->height; }
function output() {
$width = $this->width; $height = $this->height;
$attrs = ['version' => '1.1', 'width' => $width, 'height' => $height, 'viewBox' => "0 0 $width $height", 'xmlns' =>'http://www.w3.org/2000/svg', 'class' => 'hh-energy-graph'];
?>
<svg <?= formatAttributes($attrs) ?>>
<?php
foreach ($this->children as $child)
$child->output();
?>
</svg>
<?php
}
}
/**
* Something that can form part of an SVG path definition.
*/
abstract class PathSegment {
/** @var float[] Get the [x, y] co-ordinate of the start of this segment. */
abstract function getStart();
/** @var string[] Get the constituent path commands of this segment. */
abstract function getCommands();
/**
* Generate an (absolute) move-to command.
* @param float $x
* @param float $y
* @return string
*/
static function move($x, $y) {
return "M $x $y";
}
/**
* Generate an (absolute) line-to command.
* @param float $x
* @param float $y
* @return string
*/
static function line($x, $y) {
return "L $x $y";
}
/**
* Generate an (absolute) quadratic-bezier-to command, optionally specifying the control point.
* @param float $x
* @param float $y
* @param float $cx
* @param float $cy
* @return string
*/
static function quadratic($x, $y, $cx = null, $cy = null) {
if ($cy)
return "Q $cx $cy, $x $y";
return "T $x $y";
}
/**
* Generate an (absolute) cubic-bezier-to command, optionally specifying the first control point.
* @param float $x
* @param float $y
* @param float $cx2
* @param float $cy2
* @param float $cx1
* @param float $cy1
* @return string
*/
static function cubic($x, $y, $cx2, $cy2, $cx1 = null, $cy1 = null) {
if ($cy1)
return "C $cx1 $cy1, $cx2 $cy2, $x $y";
return "S $cx2 $cy2, $x $y";
}
}
/**
* An SVG <path> element.
* @package HitchinHackspace\SVG
*/
class Path extends Node {
/** @var Style */
protected $style;
/** @var PathSegment[] */
protected $segments;
/**
* @param Style $style
* @param PathSegment[] $segments
*/
public function __construct($style, $segments) {
$this->style = $style;
$this->segments = $segments;
}
public function output() {
$commands = [];
$first = true;
foreach ($this->segments as $segment) {
[$x, $y] = $segment->getStart();
if ($first) {
$commands[] = PathSegment::move($x, $y);
$first = false;
}
else {
$commands[] = PathSegment::line($x, $y);
}
foreach ($segment->getCommands() as $command)
$commands[] = $command;
}
?>
<path <?= formatAttributes($this->style->getAttrs() + ['d' => implode(' ', $commands)]) ?> />
<?php
}
}
/**
* A path running through points.
*/
abstract class JoinPoints extends PathSegment {
/** @var float[][] */
protected $points;
/**
* @param float[][] $points
*/
public function __construct($points) {
$this->points = $points;
}
}
/**
* A path composed of linear segments between points.
*/
class LinearPath extends JoinPoints {
public function getStart() {
return $this->points[0];
}
public function getCommands() {
$it = new ArrayIterator($this->points);
$it->next();
while ($it->valid()) {
[$x, $y] = $it->current(); $it->next();
yield self::line($x, $y);
}
}
}
class QuadraticPath extends JoinPoints {
public function getStart() {
return $this->points[0];
}
public function getCommands() {
[$ox, $oy] = $this->getStart();
$it = new ArrayIterator($this->points);
$it->next();
[$x, $y] = $it->current(); $it->next();
yield self::quadratic($x, $y, ($ox + $x) / 2, ($oy + $y) / 2);
while ($it->valid()) {
[$x, $y] = $it->current(); $it->next();
yield self::quadratic($x, $y);
}
}
}
class CubicPath extends JoinPoints {
public function getStart() {
return $this->points[0];
}
protected function getControlPoint($prev, $current, $next) {
$k = 0.5;
$m = 0.5;
$gradient = $m * ($next[1] - $prev[1]) / ($next[0] - $prev[0]);
$offset = $current[0] - $prev[0];
$cp1 = [$current[0] - $k * $offset, $current[1] - $k * $offset * $gradient];
// $cp2 = [$current[0] + $k * $offset, $current[1] + $k * $offset * $gradient];
return $cp1;
}
public function getCommands() {
$it = new ArrayIterator($this->points);
$it->next();
$current = $prev = $this->getStart();
while ($it->valid()) {
$next = $it->current(); $it->next();
[$cpx, $cpy] = $this->getControlPoint($prev, $current, $next);
yield self::cubic($current[0], $current[1], $cpx, $cpy);
[$prev, $current] = [$current, $next];
}
[$cpx, $cpy] = $this->getControlPoint($prev, $current, $current);
yield self::cubic($current[0], $current[1], $cpx, $cpy);
}
}