-
Notifications
You must be signed in to change notification settings - Fork 17
/
a2s52.php
2449 lines (2117 loc) · 75.8 KB
/
a2s52.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* A2S_ASCIIToSVG.php: ASCII diagram -> SVG art generator.
* Copyright © 2012 Devon H. O'Dell <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#namespace org\dh0\a2s;
include 'svg-path.lex.php';
include 'colors.php';
/*
* A2S_Scale is a singleton class that is instantiated to apply scale
* transformations on the text -> canvas grid geometry. We could probably use
* SVG's native scaling for this, but I'm not sure how yet.
*/
class A2S_Scale {
private static $instance = null;
public $xScale;
public $yScale;
private function __construct() {}
private function __clone() {}
public static function getInstance() {
if (self::$instance == null) {
self::$instance = new A2S_Scale();
}
return self::$instance;
}
public function setScale($x, $y) {
$o = self::getInstance();
$o->xScale = $x;
$o->yScale = $y;
}
}
/*
* A2S_CustomObjects allows users to create their own custom SVG paths and use
* them as box types with a2s:type references.
*
* Paths must have width and height set, and must not span multiple lines.
* Multiple paths can be specified, one path per line. All objects must
* reside in the same directory.
*
* File operations are horribly slow, so we make a best effort to avoid
* as many as possible:
*
* * If the directory mtime hasn't changed, we attempt to load our
* objects from a cache file.
*
* * If this file doesn't exist, can't be read, or the mtime has
* changed, we scan the directory and update files that have changed
* based on their mtime.
*
* * We attempt to save our cache in a temporary directory. It's volatile
* but also requires no configuration.
*
* We could do a bit better by utilizing APC's shared memory storage, which
* would help greatly when running on a server.
*
* Note that the path parser isn't foolproof, mostly because PHP isn't the
* greatest language ever for implementing a parser.
*/
class A2S_CustomObjects {
public static $objects = array();
/*
* Closures / callable function names / whatever for integrating non-default
* loading and storage functionality.
*/
public static $loadCacheFn = null;
public static $storCacheFn = null;
public static $loadObjsFn = null;
public static function loadObjects() {
$cacheFile = sys_get_temp_dir() . "/.a2s.objcache";
$dir = './objects';
if (is_callable(self::$loadCacheFn)) {
/*
* Should return exactly what was given to the $storCacheFn when it was
* last called, or null if nothing can be loaded.
*/
$fn = self::$loadCacheFn;
self::$objects = $fn();
return;
} else {
if (is_readable($cacheFile) && is_readable($dir)) {
$cacheTime = filemtime($cacheFile);
if (filemtime($dir) <= filemtime($cacheFile)) {
self::$objects = unserialize(file_get_contents($cacheFile));
return;
}
} else {
return;
}
}
if (is_callable(self::$loadObjsFn)) {
/*
* Returns an array of arrays of path information. The innermost arrays
* (containing the path information) contain the path name, the width of
* the bounding box, the height of the bounding box, and the path
* command. This interface does *not* want the path's XML tag. An array
* returned from here containing two objects that each have 1 line would
* look like:
*
* array (
* array (
* name => 'pathA',
* paths => array (
* array ('width' => 10, 'height' => 10, 'path' => 'M 0 0 L 10 10'),
* array ('width' => 10, 'height' => 10, 'path' => 'M 0 10 L 10 0'),
* ),
* ),
* array (
* name => 'pathB',
* paths => array (
* array ('width' => 10, 'height' => 10, 'path' => 'M 0 5 L 5 10'),
* array ('width' => 10, 'height' => 10, 'path' => 'M 5 10 L 10 5'),
* ),
* ),
* );
*/
$fn = self::$loadObjsFn;
$objs = $fn();
$i = 0;
foreach ($objs as $obj) {
foreach ($obj['paths'] as $path) {
self::$objects[$obj['name']][$i]['width'] = $path['width'];
self::$objects[$obj['name']][$i]['height'] = $path['height'];
self::$objects[$obj['name']][$i++]['path'] =
self::parsePath($path['path']);
}
}
} else {
$ents = scandir($dir);
foreach ($ents as $ent) {
$file = "{$dir}/{$ent}";
$base = substr($ent, 0, -5);
if (substr($ent, -5) == '.path' && is_readable($file)) {
if (isset(self::$objects[$base]) &&
filemtime($file) <= self::$cacheTime) {
continue;
}
$lines = file($file);
$i = 0;
foreach ($lines as $line) {
preg_match('/width="(\d+)/', $line, $m);
$width = $m[1];
preg_match('/height="(\d+)/', $line, $m);
$height = $m[1];
preg_match('/d="([^"]+)"/', $line, $m);
$path = $m[1];
self::$objects[$base][$i]['width'] = $width;
self::$objects[$base][$i]['height'] = $height;
self::$objects[$base][$i++]['path'] = self::parsePath($path);
}
}
}
}
if (is_callable(self::$storCacheFn)) {
$fn = self::$storCacheFn;
$fn(self::$objects);
} else {
file_put_contents($cacheFile, serialize(self::$objects));
}
}
private static function parsePath($path) {
$stream = fopen("data://text/plain,{$path}", 'r');
$P = new A2S_SVGPathParser();
$S = new A2S_Yylex($stream);
while ($t = $S->nextToken()) {
$P->A2S_SVGPath($t->type, $t);
}
/* Force shift/reduce of last token. */
$P->A2S_SVGPath(0);
fclose($stream);
$cmdArr = array();
$i = 0;
foreach ($P->commands as $cmd) {
foreach ($cmd as $arg) {
$arg = (array)$arg;
$cmdArr[$i][] = $arg['value'];
}
$i++;
}
return $cmdArr;
}
}
/*
* All lines and polygons are represented as a series of point coordinates
* along a path. Points can have different properties; markers appear on
* edges of lines and control points denote that a bezier curve should be
* calculated for the corner represented by this point.
*/
class A2S_Point {
public $gridX;
public $gridY;
public $x;
public $y;
public $flags;
const POINT = 0x1;
const CONTROL = 0x2;
const SMARKER = 0x4;
const IMARKER = 0x8;
const TICK = 0x10;
const DOT = 0x20;
public function __construct($x, $y) {
$this->flags = 0;
$s = A2S_Scale::getInstance();
$this->x = ($x * $s->xScale) + ($s->xScale / 2);
$this->y = ($y * $s->yScale) + ($s->yScale / 2);
$this->gridX = $x;
$this->gridY = $y;
}
}
/*
* Groups objects together and sets common properties for the objects in the
* group.
*/
class A2S_SVGGroup {
private $groups;
private $curGroup;
private $groupStack;
private $options;
public function __construct() {
$this->groups = array();
$this->groupStack = array();
$this->options = array();
}
public function getGroup($groupName) {
return $this->groups[$groupName];
}
public function pushGroup($groupName) {
if (!isset($this->groups[$groupName])) {
$this->groups[$groupName] = array();
$this->options[$groupName] = array();
}
$this->groupStack[] = $groupName;
$this->curGroup = $groupName;
}
public function popGroup() {
/*
* Remove the last group and fetch the current one. array_pop will return
* NULL for an empty array, so this is safe to do when only one element
* is left.
*/
array_pop($this->groupStack);
$this->curGroup = array_pop($this->groupStack);
}
public function addObject($o) {
$this->groups[$this->curGroup][] = $o;
}
public function setOption($opt, $val) {
$this->options[$this->curGroup][$opt] = $val;
}
public function render() {
$out = '';
foreach($this->groups as $groupName => $objects) {
$out .= "<g id=\"{$groupName}\" ";
foreach ($this->options[$groupName] as $opt => $val) {
if (strpos($opt, 'a2s:', 0) === 0) {
continue;
}
$out .= "$opt=\"$val\" ";
}
$out .= ">\n";
foreach($objects as $obj) {
$out .= $obj->render();
}
$out .= "</g>\n";
}
return $out;
}
}
/*
* The Path class represents lines and polygons.
*/
class A2S_SVGPath {
private $options;
private $points;
private $ticks;
private $flags;
private $text;
private $name;
private static $id = 0;
const CLOSED = 0x1;
public function __construct() {
$this->options = array();
$this->points = array();
$this->text = array();
$this->ticks = array();
$this->flags = 0;
$this->name = self::$id++;
}
/*
* Making sure that we always started at the top left coordinate
* makes so many things so much easier. First, find the lowest Y
* position. Then, of all matching Y positions, find the lowest X
* position. This is the top left.
*
* As far as the points are considered, they're definitely on the
* top somewhere, but not necessarily the most left. This could
* happen if there was a corner connector in the top edge (perhaps
* for a line to connect to). Since we couldn't turn right there,
* we have to try now.
*
* This should only be called when we close a polygon.
*/
public function orderPoints() {
$pPoints = count($this->points);
$minY = $this->points[0]->y;
$minX = $this->points[0]->x;
$minIdx = 0;
for ($i = 1; $i < $pPoints; $i++) {
if ($this->points[$i]->y <= $minY) {
$minY = $this->points[$i]->y;
if ($this->points[$i]->x < $minX) {
$minX = $this->points[$i]->x;
$minIdx = $i;
}
}
}
/*
* If our top left isn't at the 0th index, it is at the end. If
* there are bits after it, we need to cut those and put them at
* the front.
*/
if ($minIdx != 0) {
$startPoints = array_splice($this->points, $minIdx);
$this->points = array_merge($startPoints, $this->points);
}
}
/*
* Useful for recursive walkers when speculatively trying a direction.
*/
public function popPoint() {
array_pop($this->points);
}
public function addPoint($x, $y, $flags = A2S_Point::POINT) {
$p = new A2S_Point($x, $y);
/*
* If we attempt to add our original point back to the path, the polygon
* must be closed.
*/
if (count($this->points) > 0) {
if ($this->points[0]->x == $p->x && $this->points[0]->y == $p->y) {
$this->flags |= self::CLOSED;
return true;
}
/*
* For the purposes of this library, paths should never intersect each
* other. Even in the case of closing the polygon, we do not store the
* final coordinate twice.
*/
foreach ($this->points as $point) {
if ($point->x == $p->x && $point->y == $p->y) {
return true;
}
}
}
$p->flags |= $flags;
$this->points[] = $p;
return false;
}
/*
* It's useful to be able to know the points in a shape.
*/
public function getPoints() {
return $this->points;
}
/*
* Add a marker to a line. The third argument specifies which marker to use,
* and this depends on the orientation of the line. Due to the way the line
* parser works, we may have to use an inverted representation.
*/
public function addMarker($x, $y, $t) {
$p = new A2S_Point($x, $y);
$p->flags |= $t;
$this->points[] = $p;
}
public function addTick($x, $y, $t) {
$p = new A2S_Point($x, $y);
$p->flags |= $t;
$this->ticks[] = $p;
}
/*
* Is this path closed?
*/
public function isClosed() {
return ($this->flags & self::CLOSED);
}
public function addText($t) {
$this->text[] = $t;
}
public function getText() {
return $this->text;
}
public function setID($id) {
$this->name = str_replace(' ', '_', str_replace('"', '_', $id));
}
public function getID() {
return $this->name;
}
/*
* Set options as a JSON string. Specified as a merge operation so that it
* can be called after an individual setOption call.
*/
public function setOptions($opt) {
$this->options = array_merge($this->options, $opt);
}
public function setOption($opt, $val) {
$this->options[$opt] = $val;
}
public function getOption($opt) {
if (isset($this->options[$opt])) {
return $this->options[$opt];
}
return null;
}
/*
* Does the given point exist within this polygon? Since we can
* theoretically have some complex concave and convex polygon edges in the
* same shape, we need to do a full point-in-polygon test. This algorithm
* seems like the standard one. See: http://alienryderflex.com/polygon/
*/
public function hasPoint($x, $y) {
if ($this->isClosed() == false) {
return false;
}
$oddNodes = false;
$bound = count($this->points);
for ($i = 0, $j = count($this->points) - 1; $i < $bound; $i++) {
if (($this->points[$i]->gridY < $y && $this->points[$j]->gridY >= $y ||
$this->points[$j]->gridY < $y && $this->points[$i]->gridY >= $y) &&
($this->points[$i]->gridX <= $x || $this->points[$j]->gridX <= $x)) {
if ($this->points[$i]->gridX + ($y - $this->points[$i]->gridY) /
($this->points[$j]->gridY - $this->points[$i]->gridY) *
($this->points[$j]->gridX - $this->points[$i]->gridX) < $x) {
$oddNodes = !$oddNodes;
}
}
$j = $i;
}
return $oddNodes;
}
/*
* Apply a matrix transformation to the coordinates ($x, $y). The
* multiplication is implemented on the matrices:
*
* | a b c | | x |
* | d e f | * | y |
* | 0 0 1 | | 1 |
*
* Additional information on the transformations and what each R,C in the
* transformation matrix represents, see:
*
* http://www.w3.org/TR/SVG/coords.html#TransformMatrixDefined
*/
private function matrixTransform($matrix, $x, $y) {
$xyMat = array(array($x), array($y), array(1));
$newXY = array(array());
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 1; $j++) {
$sum = 0;
for ($k = 0; $k < 3; $k++) {
$sum += $matrix[$i][$k] * $xyMat[$k][$j];
}
$newXY[$i][$j] = $sum;
}
}
/* Return the coordinates as a vector */
return array($newXY[0][0], $newXY[1][0], $newXY[2][0]);
}
/*
* Translate the X and Y coordinates. tX and tY specify the distance to
* transform.
*/
private function translateTransform($tX, $tY, $x, $y) {
$matrix = array(array(1, 0, $tX), array(0, 1, $tY), array(0, 0, 1));
return $this->matrixTransform($matrix, $x, $y);
}
/*
* A2S_Scale transformations are implemented by applying the scale to the X and
* Y coordinates. One unit in the new coordinate system equals $s[XY] units
* in the old system. Thus, if you want to double the size of an object on
* both axes, you sould call scaleTransform(0.5, 0.5, $x, $y)
*/
private function scaleTransform($sX, $sY, $x, $y) {
$matrix = array(array($sX, 0, 0), array(0, $sY, 0), array(0, 0, 1));
return $this->matrixTransform($matrix, $x, $y);
}
/*
* Rotate the coordinates around the center point cX and cY. If these
* are not specified, the coordinate is rotated around 0,0. The angle
* is specified in degrees.
*/
private function rotateTransform($angle, $x, $y, $cX = 0, $cY = 0) {
$angle = $angle * (pi() / 180);
if ($cX != 0 || $cY != 0) {
list ($x, $y) = $this->translateTransform(-$cX, -$cY, $x, $y);
}
$matrix = array(array(cos($angle), -sin($angle), 0),
array(sin($angle), cos($angle), 0),
array(0, 0, 1));
$ret = $this->matrixTransform($matrix, $x, $y);
if ($cX != 0 || $cY != 0) {
list ($x, $y) = $this->translateTransform($cX, $cY, $ret[0], $ret[1]);
$ret[0] = $x;
$ret[1] = $y;
}
return $ret;
}
/*
* Skews along the X axis at specified angle. The angle is specified in
* degrees.
*/
private function skewXTransform($angle, $x, $y) {
$angle = $angle * (pi() / 180);
$matrix = array(array(1, tan($angle), 0), array(0, 1, 0), array(0, 0, 1));
return $this->matrixTransform($matrix, $x, $y);
}
/*
* Skews along the Y axis at specified angle. The angle is specified in
* degrees.
*/
private function skewYTransform($angle, $x, $y) {
$angle = $angle * (pi() / 180);
$matrix = array(array(1, 0, 0), array(tan($angle), 1, 0), array(0, 0, 1));
return $this->matrixTransform($matrix, $x, $y);
}
/*
* Apply a transformation to a point $p.
*/
private function applyTransformToPoint($txf, $p, $args) {
switch ($txf) {
case 'translate':
return $this->translateTransform($args[0], $args[1], $p->x, $p->y);
case 'scale':
return $this->scaleTransform($args[0], $args[1], $p->x, $p->y);
case 'rotate':
if (count($args) > 1) {
return $this->rotateTransform($args[0], $p->x, $p->y, $args[1], $args[2]);
} else {
return $this->rotateTransform($args[0], $p->x, $p->y);
}
case 'skewX':
return $this->skewXTransform($args[0], $p->x, $p->y);
case 'skewY':
return $this->skewYTransform($args[0], $p->x, $p->y);
}
}
/*
* Apply the transformation function $txf to all coordinates on path $p
* providing $args as arguments to the transformation function.
*/
private function applyTransformToPath($txf, &$p, $args) {
$pathCmds = count($p['path']);
$curPoint = new A2S_Point(0, 0);
$prevType = null;
$curType = null;
for ($i = 0; $i < $pathCmds; $i++) {
$cmd = &$p['path'][$i];
$prevType = $curType;
$curType = $cmd[0];
switch ($curType) {
case 'z':
case 'Z':
/* Can't transform this */
break;
case 'm':
if ($prevType != null) {
$curPoint->x += $cmd[1];
$curPoint->y += $cmd[2];
list ($x, $y) = $this->applyTransformToPoint($txf, $curPoint, $args);
$curPoint->x = $x;
$curPoint->y = $y;
$cmd[1] = $x;
$cmd[2] = $y;
} else {
$curPoint->x = $cmd[1];
$curPoint->y = $cmd[2];
list ($x, $y) = $this->applyTransformToPoint($txf, $curPoint, $args);
$curPoint->x = $x;
$curPoint->y = $y;
$cmd[1] = $x;
$cmd[2] = $y;
$curType = 'l';
}
break;
case 'M':
$curPoint->x = $cmd[1];
$curPoint->y = $cmd[2];
list ($x, $y) = $this->applyTransformToPoint($txf, $curPoint, $args);
$curPoint->x = $x;
$curPoint->y = $y;
$cmd[1] = $x;
$cmd[2] = $y;
if ($prevType == null) {
$curType = 'L';
}
break;
case 'l':
$curPoint->x += $cmd[1];
$curPoint->y += $cmd[2];
list ($x, $y) = $this->applyTransformToPoint($txf, $curPoint, $args);
$curPoint->x = $x;
$curPoint->y = $y;
$cmd[1] = $x;
$cmd[2] = $y;
break;
case 'L':
$curPoint->x = $cmd[1];
$curPoint->y = $cmd[2];
list ($x, $y) = $this->applyTransformToPoint($txf, $curPoint, $args);
$curPoint->x = $x;
$curPoint->y = $y;
$cmd[1] = $x;
$cmd[2] = $y;
break;
case 'v':
$curPoint->y += $cmd[1];
$curPoint->x += 0;
list ($x, $y) = $this->applyTransformToPoint($txf, $curPoint, $args);
$curPoint->x = $x;
$curPoint->y = $y;
$cmd[1] = $y;
break;
case 'V':
$curPoint->y = $cmd[1];
list ($x, $y) = $this->applyTransformToPoint($txf, $curPoint, $args);
$curPoint->x = $x;
$curPoint->y = $y;
$cmd[1] = $y;
break;
case 'h':
$curPoint->x += $cmd[1];
list ($x, $y) = $this->applyTransformToPoint($txf, $curPoint, $args);
$curPoint->x = $x;
$curPoint->y = $y;
$cmd[1] = $x;
break;
case 'H':
$curPoint->x = $cmd[1];
list ($x, $y) = $this->applyTransformToPoint($txf, $curPoint, $args);
$curPoint->x = $x;
$curPoint->y = $y;
$cmd[1] = $x;
break;
case 'c':
$tP = new A2S_Point(0, 0);
$tP->x = $curPoint->x + $cmd[1]; $tP->y = $curPoint->y + $cmd[2];
list ($x, $y) = $this->applyTransformToPoint($txf, $tP, $args);
$cmd[1] = $x;
$cmd[2] = $y;
$tP->x = $curPoint->x + $cmd[3]; $tP->y = $curPoint->y + $cmd[4];
list ($x, $y) = $this->applyTransformToPoint($txf, $tP, $args);
$cmd[3] = $x;
$cmd[4] = $y;
$curPoint->x += $cmd[5];
$curPoint->y += $cmd[6];
list ($x, $y) = $this->applyTransformToPoint($txf, $curPoint, $args);
$curPoint->x = $x;
$curPoint->y = $y;
$cmd[5] = $x;
$cmd[6] = $y;
break;
case 'C':
$curPoint->x = $cmd[1];
$curPoint->y = $cmd[2];
list ($x, $y) = $this->applyTransformToPoint($txf, $curPoint, $args);
$cmd[1] = $x;
$cmd[2] = $y;
$curPoint->x = $cmd[3];
$curPoint->y = $cmd[4];
list ($x, $y) = $this->applyTransformToPoint($txf, $curPoint, $args);
$cmd[3] = $x;
$cmd[4] = $y;
$curPoint->x = $cmd[5];
$curPoint->y = $cmd[6];
list ($x, $y) = $this->applyTransformToPoint($txf, $curPoint, $args);
$curPoint->x = $x;
$curPoint->y = $y;
$cmd[5] = $x;
$cmd[6] = $y;
break;
case 's':
case 'S':
case 'q':
case 'Q':
case 't':
case 'T':
case 'a':
break;
case 'A':
/*
* This radius is relative to the start and end points, so it makes
* sense to scale, rotate, or skew it, but not translate it.
*/
if ($txf != 'translate') {
$curPoint->x = $cmd[1];
$curPoint->y = $cmd[2];
list ($x, $y) = $this->applyTransformToPoint($txf, $curPoint, $args);
$cmd[1] = $x;
$cmd[2] = $y;
}
$curPoint->x = $cmd[6];
$curPoint->y = $cmd[7];
list ($x, $y) = $this->applyTransformToPoint($txf, $curPoint, $args);
$curPoint->x = $x;
$curPoint->y = $y;
$cmd[6] = $x;
$cmd[7] = $y;
break;
}
}
}
public function render() {
$startPoint = array_shift($this->points);
$endPoint = $this->points[count($this->points) - 1];
$out = "<g id=\"group{$this->name}\">\n";
/*
* If someone has specified one of our special object types, we are going
* to want to completely override any of the pathing that we would have
* done otherwise, but we defer until here to do anything about it because
* we need information about the object we're replacing.
*/
if (isset($this->options['a2s:type']) &&
isset(A2S_CustomObjects::$objects[$this->options['a2s:type']])) {
$object = A2S_CustomObjects::$objects[$this->options['a2s:type']];
/* Again, if no fill was specified, specify one. */
if (!isset($this->options['fill'])) {
$this->options['fill'] = '#fff';
}
/*
* We don't care so much about the area, but we do care about the width
* and height of the object. All of our "custom" objects are implemented
* in 100x100 space, which makes the transformation marginally easier.
*/
$minX = $startPoint->x; $maxX = $minX;
$minY = $startPoint->y; $maxY = $minY;
foreach ($this->points as $p) {
if ($p->x < $minX) {
$minX = $p->x;
} elseif ($p->x > $maxX) {
$maxX = $p->x;
}
if ($p->y < $minY) {
$minY = $p->y;
} elseif ($p->y > $maxY) {
$maxY = $p->y;
}
}
$objW = $maxX - $minX;
$objH = $maxY - $minY;
$i = 0;
foreach ($object as $o) {
$id = self::$id++;
$out .= "\t<path id=\"path{$this->name}\" d=\"";
$oW = $o['width'];
$oH = $o['height'];
$this->applyTransformToPath('scale', $o, array($objW/$oW, $objH/$oH));
$this->applyTransformToPath('translate', $o, array($minX, $minY));
foreach ($o['path'] as $cmd) {
$out .= join(' ', $cmd) . ' ';
}
$out .= '" ';
/* Don't add options to sub-paths */
if ($i++ < 1) {
foreach ($this->options as $opt => $val) {
if (strpos($opt, 'a2s:', 0) === 0) {
continue;
}
$out .= "$opt=\"$val\" ";
}
}
$out .= " />\n";
}
if (count($this->text) > 0) {
foreach ($this->text as $text) {
$out .= "\t" . $text->render() . "\n";
}
}
$out .= "</g>\n";
/* Bazinga. */
return $out;
}
/*
* Nothing fancy here -- this is just rendering for our standard
* polygons.
*
* Our start point is represented by a single moveto command (unless the
* start point is curved) as the shape will be closed with the Z command
* automatically if it is a closed shape. If we have a control point, we
* have to go ahead and draw the curve.
*/
if (($startPoint->flags & A2S_Point::CONTROL)) {
$cX = $startPoint->x;
$cY = $startPoint->y;
$sX = $startPoint->x;
$sY = $startPoint->y + 10;
$eX = $startPoint->x + 10;
$eY = $startPoint->y;
$path = "M {$sX} {$sY} Q {$cX} {$cY} {$eX} {$eY} ";
} else {
$path = "M {$startPoint->x} {$startPoint->y} ";
}
$prevP = $startPoint;
$bound = count($this->points);
for ($i = 0; $i < $bound; $i++) {
$p = $this->points[$i];
/*
* Handle quadratic Bezier curves. NOTE: This algorithm for drawing
* the curves only works if the shapes are drawn in a clockwise
* manner.
*/
if (($p->flags & A2S_Point::CONTROL)) {