-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathiconizr.phps
1896 lines (1709 loc) · 71.2 KB
/
iconizr.phps
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
#!/usr/bin/env php
<?php
namespace Jkphl;
// Require Zend classes for argument validation
require_once __DIR__.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'Zend'.DIRECTORY_SEPARATOR.'Console'.DIRECTORY_SEPARATOR.'Exception'.DIRECTORY_SEPARATOR.'ExceptionInterface.php';
require_once __DIR__.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'Zend'.DIRECTORY_SEPARATOR.'Console'.DIRECTORY_SEPARATOR.'Exception'.DIRECTORY_SEPARATOR.'BadMethodCallException.php';
require_once __DIR__.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'Zend'.DIRECTORY_SEPARATOR.'Console'.DIRECTORY_SEPARATOR.'Exception'.DIRECTORY_SEPARATOR.'InvalidArgumentException.php';
require_once __DIR__.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'Zend'.DIRECTORY_SEPARATOR.'Console'.DIRECTORY_SEPARATOR.'Exception'.DIRECTORY_SEPARATOR.'RuntimeException.php';
require_once __DIR__.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'Zend'.DIRECTORY_SEPARATOR.'Console'.DIRECTORY_SEPARATOR.'Getopt.php';
/**
* Iconizr class
*
* @author joschi
*
*/
class Iconizr {
/**
* Command line options
*
* @var \Zend\Console\Getopt
*/
protected $_options = null;
/**
* Default options
*
* @var array
*/
protected $_defaultOptions = array(
'prefix' => self::DEFAULT_PREFIX,
'level' => self::DEFAULT_LEVEL,
'speed' => 2,
'css' => 0,
'sass' => 0,
'quantize' => 0,
'dims' => 0,
'verbose' => 0,
'keep' => 0,
'width' => self::DEFAULT_LENGTH,
'height' => self::DEFAULT_LENGTH,
'maxwidth' => self::DEFAULT_MAXLENGTH,
'maxheight' => self::DEFAULT_MAXLENGTH,
'svg' => self::DEFAULT_THRESHOLD_SVG,
'png' => self::DEFAULT_THRESHOLD_PNG,
'scour' => null,
'sassout' => null,
'python' => null,
'pseudo' => self::PSEUDO_SPLIT,
'padding' => 0,
);
/**
* Supporting binaries
*
* @var array
*/
protected $_binaries = array(
'svgo' => false,
'phantomjs' => false,
'pngcrush' => false,
'pngquant' => false,
'optipng' => false,
'python' => false,
);
/**
* Current working directory
*
* @var string
*/
protected $_cwd = null;
/**
* SVG directories
*
* @var array
*/
protected $_dirs = array();
/**
* List of unique output directory names
*
* @var array
*/
protected $_uniqueDirs = array();
/**
* Target directory for CSS files
*
* @var string
*/
protected $_target = null;
/**
* Target directory for Sass files
*
* @var string
*/
protected $_sassTarget = null;
/**
* Prefix to the CSS directory path for embedding in HTML documents
*
* @var string
*/
protected $_embed = null;
/**
* Temporary directory
*
* @var string
*/
protected $_tmpDir = null;
/**
* Temporary directory name
*
* @var string
*/
protected $_tmpName = null;
/**
* Temporary resources
*
* @var array
*/
protected $_tmpResources = array();
/**
* CSS
*
* @var array
*/
protected $_css = array(
self::SVG => array(
self::SINGLE => array(),
self::DATA => array(),
self::SPRITE => array(),
),
self::PNG => array(
self::SINGLE => array(),
self::DATA => array(),
self::SPRITE => array(),
),
);
/**
* Sass
*
* @var array
*/
protected $_sass = array(
self::SVG => array(
self::SINGLE => array(),
self::DATA => array(),
self::SPRITE => array(),
),
self::PNG => array(
self::SINGLE => array(),
self::DATA => array(),
self::SPRITE => array(),
),
);
/**
* Data URIs
*
* @var string
*/
protected $_dataUris = array();
/**
* Use sprites
*
* @var array
*/
protected $_useSprite = array();
/**
* Icon dimensions
*
* @var array
*/
protected $_dimensions = array();
/**
* Icon names
*
* @var array
*/
protected $_iconNames = array();
/**
* CSS class prefix
*
* @var string
*/
protected $_prefix = self::DEFAULT_PREFIX;
/**
* Whether to optimize PNG images
*
* @var boolean
*/
protected $_optimize = true;
/**
* Quantize speed
*
* @var string
*/
protected $_speed = 3;
/**
* Optimization level
*
* @var string
*/
protected $_optimization = 2;
/**
* Effective data URI byte thresholds
*
* @var int
*/
protected $_thresholds = array(
self::SVG => self::DEFAULT_THRESHOLD_SVG,
self::PNG => self::DEFAULT_THRESHOLD_PNG,
);
/**
* Flags
*
* @var array
*/
protected $_flags = array();
/**
* Absolute path to scour script for cleaning the SVG files
*
* @var string
*/
protected $_scour = null;
/**
* Default icon width
*
* @var int
*/
protected $_width = self::DEFAULT_LENGTH;
/**
* Default icon height
*
* @var int
*/
protected $_height = self::DEFAULT_LENGTH;
/**
* Maximum icon width
*
* @var int
*/
protected $_maxwidth = self::DEFAULT_MAXLENGTH;
/**
* Maximum icon height
*
* @var int
*/
protected $_maxheight = self::DEFAULT_MAXLENGTH;
/**
* Padding around the icons (pixel)
*
* @var int
*/
protected $_padding = 0;
/**
* Logging group (indentation level)
*
* @var int
*/
protected $_logGroup = 0;
/**
* PhantomJS script template
*
* @var string
*/
protected static $_phantomJSSCript = 'var icons=%s,svg=function(){if(icons.length){var a=icons.pop();page.viewportSize={width:a[2],height:a[3]};page.open(a[0],function(){page.render(a[1]),svg()})}else phantom.exit()},page=require("webpage").create();svg();';
/**
* SVG
*
* @var string
*/
const SVG = 'svg';
/**
* PNG
*
* @var string
*/
const PNG = 'png';
/**
* Single image mode
*
* @var string
*/
const SINGLE = 'single';
/**
* Data URI mode
*
* @var string
*/
const DATA = 'data';
/**
* Sprite mode
*
* @var string
*/
const SPRITE = 'sprite';
/**
* Default CSS class prefix
*
* @var string
*/
const DEFAULT_PREFIX = 'icon';
/**
* Default optimization level
*
* @var ing
*/
const DEFAULT_LEVEL = 3;
/**
* Default CSS and Sass file name prefix
*
* @var string
*/
const DEFAULT_FILE = 'iconizr';
/**
* Default byte threshold for SVG data URIs
*
* @var int
*/
const DEFAULT_THRESHOLD_SVG = 1048576;
/**
* Default byte threshold for PNG data URIs
*
* @var int
*/
const DEFAULT_THRESHOLD_PNG = 32768;
/**
* Default icon width / height
*
* @var int
*/
const DEFAULT_LENGTH = 32;
/**
* Default maximum icon width / height
*
* @var int
*/
const DEFAULT_MAXLENGTH = 1000;
/**
* Default padding around the icons (pixel)
*
* @var int
*/
const DEFAULT_PADDING = 0;
/**
* Character sequence for denoting pseudo-selectors
*
* @var string
*/
const PSEUDO_SPLIT = '~';
/**
* Info message
*
* @var int
*/
const LOG_INFO = 0;
/**
* Creation message
*
* @var int
*/
const LOG_CREATE = 1;
/**
* Alert message
*
* @var int
*/
const LOG_ALERT = 2;
/**
* Group message
*
* @var int
*/
const LOG_GROUP = 3;
/**
* Error message
*
* @var int
*/
const LOG_ERROR = 4;
/**
* PCRE character class for the first char of XML IDs
*
* @var string
*/
const PCRE_ID_START_CHARS = 'A-Za-z\:\_\xC0-\xD6\xD8-\xF6\xF8-\x{2FF}\x{370}-\x{37D}\x{37F}-\x{1FFF}\x{200C}-\x{200D}\x{2070}-\x{218F}\x{2C00}-\x{2FEF}\x{3001}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFFD}\x{10000}-\x{EFFFF}';
/**
* PCRE character class for the first char of XML IDs
*
* @var string
*/
const PCRE_ID_FOLLOWER_CHARS = '\-\.\d\xB7\x{0300}-\x{036F}\x{203F}-\x{2040}';
/************************************************************************************************
* PUBLIC METHODS
***********************************************************************************************/
/**
* Constructor
*
* @return void
*/
public function __construct() {
$this->_cwd = getcwd();
// Find the binary helpers supported by the system
foreach ($this->_binaries as $name => $available) {
$return = 0;
$output = array();
$binary = @exec('which '.escapeshellarg($name), $output, $return);
$this->_binaries[$name] = (!intval($return) && strlen($binary)) ? $binary : false;
}
// Parse & validate the command line options
try {
$this->_options = new \Zend\Console\Getopt(array(
'out|o=s' => 'Output directory for the CSS files and the icons subdirectory',
'sassout=s' => 'Optional separate output directory for Sass files',
'css|c-s' => 'Render CSS files (optionally provide a CSS file prefix, default: iconizr)',
'sass|s-s' => 'Render Sass files (optionally provide a Sass file prefix, default: iconizr)',
'prefix|p=s' => 'CSS selector prefix (default: '.self::DEFAULT_PREFIX.')',
'level|l=i' => 'PNG image optimization level: 0 (no optimization), 1 (fast & rough) - 11 (slow & high quality), default: '.self::DEFAULT_LEVEL,
'quantize|q' => 'Quantize PNG images (reduce to 8-bit color depth)',
'embed|e=s' => 'Prefix to the CSS directory path for embedding the stylesheets into your HTML documents (default: no prefix, use output directory as root-relative path)',
'width=i' => 'Default icon width (if SVG is missing a width value; defaults to '.self::DEFAULT_LENGTH.' pixels)',
'height=i' => 'Default icon height (if SVG is missing a height value; defaults to '.self::DEFAULT_LENGTH.' pixels)',
'maxwidth=i' => 'Maximum icon width, default: '.self::DEFAULT_MAXLENGTH.' pixels',
'maxheight=i' => 'Maximum icon height, default: '.self::DEFAULT_MAXLENGTH.' pixels',
'padding=i' => 'Transparent padding around the icons (in pixel), default: '.self::DEFAULT_PADDING.' pixels',
'svg=i' => 'Data URI byte threshold for SVG files, default: '.self::DEFAULT_THRESHOLD_SVG,
'png=i' => 'Data URI byte threshold for PNG files, default: '.self::DEFAULT_THRESHOLD_PNG,
'pseudo=s' => 'Character sequence for denoting CSS pseudo classes, default: '.self::PSEUDO_SPLIT,
'dims|d' => 'Render icon dimensions as separate CSS and / or Sass rules',
'keep|k' => 'Keep intermediate SVG and PNG files (inside the sprite subdirectory)',
'verbose|v-i' => 'Output verbose progress information',
'scour=s' => 'Absolute path to scour script for cleaning SVG files (see http://www.codedread.com/scour)',
'python=s' => 'Absolute path to Python 2 binary (only necessary if another Python version is the machine default)',
));
$options = $this->_defaultOptions;
foreach ($this->_options->getOptions() as $option) {
$options[$option] = $this->_options->getOption($option);
}
// In case of errors: Die with a usage message
} catch(\Zend\Console\Exception\ExceptionInterface $e) {
$this->_usage($e->getMessage());
}
// Verify the output directory
if (array_key_exists('out', $options) && strlen($target = trim($options['out']))) {
$workingDirectory = rtrim($this->_cwd, DIRECTORY_SEPARATOR);
$target = rtrim(strncmp($target, DIRECTORY_SEPARATOR, 1) ? $workingDirectory.DIRECTORY_SEPARATOR.$target : $target, DIRECTORY_SEPARATOR);
if (strncmp($workingDirectory, $target, strlen($workingDirectory))) {
$this->_usage('The output directory must be a subdirectory of the current working directory');
}
if (@is_dir($target) || @mkdir($target, 0777, true)) {
$this->_target = $target.DIRECTORY_SEPARATOR;
}
}
if ($this->_target === null) {
$this->_usage('Please provide a valid output directory');
}
// Determine the HTML embed CSS directory path
$this->_embed = rtrim((array_key_exists('embed', $options) && strlen($options['embed'])) ? $options['embed'] : '', DIRECTORY_SEPARATOR).'/';
// Verify the Python binary
if (strlen($options['python']) && @is_file($options['python']) && ($this->_pythonMajorVersion($options['python']) == 2)) {
$this->_binaries['python'] = $options['python'];
} elseif ($this->_binaries['python'] && ($this->_pythonMajorVersion($this->_binaries['python']) != 2)) {
$this->_binaries['python'] = null;
}
// Select Scour als SVG cleaner if possible
$this->_scour = (strlen(trim($options['scour'])) && @is_readable($options['scour'])) ? trim($options['scour']) : null;
// Set the CSS class prefix
$this->_prefix = strlen(trim($options['prefix'])) ? trim($options['prefix']) : self::DEFAULT_PREFIX;
// Determine default and maximum icon width & height
$this->_width = max(1, min(1000, intval($options['width'])));
$this->_height = max(1, min(1000, intval($options['height'])));
$this->_maxwidth = max($this->_width, intval($options['maxwidth']));
$this->_maxheight = max($this->_width, intval($options['maxheight']));
$this->_padding = max($this->_padding, intval($options['padding']));
// Determine quantize speed, optimization level and other flags
$level = max(0, min(11, intval($options['level']))) - 1;
$this->_optimize = $level >= 0;
$this->_speed = $this->_optimize ? round(10 - (9 * $level / 10)) : 0;
$this->_optimization = $this->_optimize ? round($level * 7 / 10) : 0;
$this->_thresholds[self::SVG] = max(1024, intval($options['svg']));
$this->_thresholds[self::PNG] = max(1024, intval($options['png']));
$this->_flags['css'] = (is_string($options['css']) && strlen(trim($options['css']))) ? trim($options['css']) : (intval($options['css']) ? self::DEFAULT_FILE : false);
$this->_flags['sass'] = (is_string($options['sass']) && strlen(trim($options['sass']))) ? trim($options['sass']) : (intval($options['sass']) ? self::DEFAULT_FILE : false);
$this->_flags['verbose'] = intval($options['verbose']);
$this->_flags['quantize'] = !!$options['quantize'];
$this->_flags['dims'] = !!$options['dims'];
$this->_flags['keep'] = !!$options['keep'];
$this->_flags['pseudo'] = strlen(trim($options['pseudo'])) ? trim($options['pseudo']) : self::PSEUDO_SPLIT;
// Determine Sass output directory (if any)
if ($this->_flags['sass']) {
$this->_sassTarget = $this->_target;
if (array_key_exists('sassout', $options) && strlen($sassTarget = trim($options['sassout']))) {
$sassTarget = rtrim(strncmp($sassTarget, DIRECTORY_SEPARATOR, 1) ? $workingDirectory.DIRECTORY_SEPARATOR.$sassTarget : $sassTarget, DIRECTORY_SEPARATOR);
if (strncmp($workingDirectory, $sassTarget, strlen($workingDirectory))) {
$this->_usage('The sass output directory must be a subdirectory of the current working directory');
}
if (@is_dir($sassTarget) || @mkdir($sassTarget, 0777, true)) {
$this->_sassTarget = $sassTarget.DIRECTORY_SEPARATOR;
}
}
}
// Prepare projected output elements
$outputElements = array();
if ($this->_flags['css'] !== false) {
$outputElements[] = $this->_target.$this->_flags['css'].'-svg-data.css';
$outputElements[] = $this->_target.$this->_flags['css'].'-svg-sprite.css';
$outputElements[] = $this->_target.$this->_flags['css'].'-png-data.css';
$outputElements[] = $this->_target.$this->_flags['css'].'-png-sprite.css';
$outputElements[] = $this->_target.$this->_flags['css'].'-loader-fragment.html';
$outputElements[] = $this->_target.$this->_flags['css'].'-preview.php';
}
if ($this->_flags['sass'] !== false) {
$outputElements[] = $this->_target.$this->_flags['sass'].'-svg-data.scss';
$outputElements[] = $this->_target.$this->_flags['sass'].'-svg-sprite.scss';
$outputElements[] = $this->_target.$this->_flags['sass'].'-png-data.scss';
$outputElements[] = $this->_target.$this->_flags['sass'].'-png-sprite.scss';
}
// Extract and run through all directories
foreach ($this->_options->getRemainingArgs() as $dir) {
$dir = @realpath($dir);
if (strlen($dir) && @is_dir($dir)) {
$this->_dirs[$dir] = array();
// Gather all SVG files in the directory
foreach (scandir($dir) as $file) {
$absFile = $dir.DIRECTORY_SEPARATOR.$file;
if (@is_file($absFile) && (($extension = strtolower(pathinfo($absFile, PATHINFO_EXTENSION))) == 'svg')) {
$this->_dirs[$dir][] = $file;
}
}
// If there are SVG files to be converted
if (count($this->_dirs[$dir])) {
$this->_target.$this->_uniqueName($dir);
// Else: Drop the input directory again
} else {
unset($this->_dirs[$dir]);
}
}
}
// Register the unique output directory names
$outputElements = array_merge($outputElements, array_values($this->_uniqueDirs));
// If at least one input directory is given
if (count($this->_dirs)) {
$cssFiles = array();
// Remove output elements if already present
$this->_delete($outputElements);
// Run through all directories and create icon packs
foreach ($this->_dirs as $directory => $icons) {
$this->_createIconStack($directory, $icons);
}
// Write out all CSS code
if ($this->_flags['css'] !== false) {
foreach ($this->_css as $type => $modeContent) {
foreach ($modeContent as $mode => $content) {
if (is_array($content) && count($content)) {
$css = $this->_target.$this->_flags['css'].'-'.$type.'-'.$mode.'.css';
if (file_put_contents($css, implode("\n", $content))) {
if (!array_key_exists($type, $cssFiles)) {
$cssFiles[$type] = array();
}
$cssFiles[$type][$mode] = strtr(ltrim(substr($css, strlen($workingDirectory)), DIRECTORY_SEPARATOR), DIRECTORY_SEPARATOR, '/');
}
}
}
}
}
// Write out all Sass code
if ($this->_flags['sass'] !== false) {
foreach ($this->_sass as $type => $modeContent) {
foreach ($modeContent as $mode => $content) {
if (is_array($content) && count($content)) {
file_put_contents($this->_sassTarget.$this->_flags['sass'].'-'.$type.'-'.$mode.'.scss', implode("\n", $content));
}
}
}
}
// Write the loader fragment
if (count($cssFiles)) {
$this->_createPreviewAndLoaderFragment($cssFiles);
}
// Else
} else {
$this->_usage('Please provide at least one input directory containing SVG files');
}
}
/************************************************************************************************
* PRIVATE METHODS
***********************************************************************************************/
/**
* Create the loader HTML fragment for the generated CSS files
*
* @param array $css Generated CSS files
* @return void
*/
protected function _createPreviewAndLoaderFragment(array $css) {
// Prepare the loader script fragment
$this->_log('Creating the stylesheet loader fragment', self::LOG_CREATE);
$loader = '<script type="text/javascript" title="https://iconizr.com | © '.date('Y').' Joschi Kuphal | MIT">';
$loader .= file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'src'.DIRECTORY_SEPARATOR.'iconizr.min.js');
$loader .= '</script>';
$loader .= '<noscript><link href="'.$this->_embed.htmlspecialchars($css[self::PNG][self::SPRITE]).'" rel="stylesheet" type="text/css" media="all"></noscript>';
file_put_contents($this->_target.$this->_flags['css'].'-loader-fragment.html', sprintf($loader, $this->_embed.htmlspecialchars($css[self::PNG][self::SPRITE]), $this->_embed.htmlspecialchars($css[self::PNG][self::DATA]), $this->_embed.htmlspecialchars($css[self::SVG][self::SPRITE]), $this->_embed.htmlspecialchars($css[self::SVG][self::DATA])));
// Create the preview documents
$this->_log('Creating the icon kit preview documents', self::LOG_CREATE);
$stylesheets = array('' => 'Automatic detection');
if ($this->_flags['keep']) {
$stylesheets[basename($css[self::PNG][self::SINGLE])] = 'PNG single images';
}
$stylesheets[basename($css[self::PNG][self::SPRITE])] = 'PNG sprite';
$stylesheets[basename($css[self::PNG][self::DATA])] = 'PNG data URIs';
if ($this->_flags['keep']) {
$stylesheets[basename($css[self::SVG][self::SINGLE])] = 'SVG single images';
}
$stylesheets[basename($css[self::SVG][self::SPRITE])] = 'SVG sprite';
$stylesheets[basename($css[self::SVG][self::DATA])] = 'SVG data URIs';
// Prepare the preview document
$preview = array(
'primer' => '<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/><meta http-equiv="cache-control" content="max-age=0"/><meta http-equiv="cache-control" content="no-cache"/><meta http-equiv="expires" content="0"/><meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT"/><meta http-equiv="pragma" content="no-cache"/><title>Icon kit preview | iconizr.com</title>',
'css' => '',
'styles' => '<style type="text/css">@CHARSET "UTF-8";body{padding:0;margin:0;color:#666;background:#fafafa;font-family:Arial, Helvetica, sans-serif;font-size:1em;line-height:1.4}header{display:block;padding:3em 3em 2em 3em;background-color:#fff}header p{margin:2em 0 0 0}#logo{font-size:xx-large;font-size:3rem;padding:0;margin:0;height:53px;background:url(data:image/png;base64,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) center left no-repeat;white-space:nowrap;overflow:hidden;text-indent:100%}nav{font-size:.7em;display:block;width:100%;margin:0 0 2em 0}nav a{display:inline-block;text-decoration:none;margin-left:2em;color:#0f7595;white-space:nowrap}nav a:hover{text-decoration:underline}nav a.current{font-weight:bold;text-decoration:underline;color:#666}section{border-top:1px solid #eee;padding:2em 3em 0 3em}ul{margin:0;padding:0}li{display:inline;display:inline-block;background-color:#fff;position:relative;margin:0 2em 2em 0;vertical-align:top;border:1px solid #ccc;padding:1em 1em 3em 1em;cursor:default}.icon-box{margin:0;width:144px;height:144px;position:relative;background:#ccc url(data:image/gif;base64,R0lGODlhDAAMAIAAAMzMzP///yH/C1hNUCBEYXRhWE1QPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS4wLWMwNjEgNjQuMTQwOTQ5LCAyMDEwLzEyLzA3LTEwOjU3OjAxICAgICAgICAiPiA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPiA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtbG5zOnhtcE1NPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvbW0vIiB4bWxuczpzdFJlZj0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL3NUeXBlL1Jlc291cmNlUmVmIyIgeG1wOkNyZWF0b3JUb29sPSJBZG9iZSBQaG90b3Nob3AgQ1M1LjEgV2luZG93cyIgeG1wTU06SW5zdGFuY2VJRD0ieG1wLmlpZDozQjk4OTI0MUY5NTIxMUUyQkJDMEI5NEFEM0Y1QTYwQyIgeG1wTU06RG9jdW1lbnRJRD0ieG1wLmRpZDozQjk4OTI0MkY5NTIxMUUyQkJDMEI5NEFEM0Y1QTYwQyI+IDx4bXBNTTpEZXJpdmVkRnJvbSBzdFJlZjppbnN0YW5jZUlEPSJ4bXAuaWlkOjNCOTg5MjNGRjk1MjExRTJCQkMwQjk0QUQzRjVBNjBDIiBzdFJlZjpkb2N1bWVudElEPSJ4bXAuZGlkOjNCOTg5MjQwRjk1MjExRTJCQkMwQjk0QUQzRjVBNjBDIi8+IDwvcmRmOkRlc2NyaXB0aW9uPiA8L3JkZjpSREY+IDwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8+Af/+/fz7+vn49/b19PPy8fDv7u3s6+rp6Ofm5eTj4uHg397d3Nva2djX1tXU09LR0M/OzczLysnIx8bFxMPCwcC/vr28u7q5uLe2tbSzsrGwr66trKuqqainpqWko6KhoJ+enZybmpmYl5aVlJOSkZCPjo2Mi4qJiIeGhYSDgoGAf359fHt6eXh3dnV0c3JxcG9ubWxramloZ2ZlZGNiYWBfXl1cW1pZWFdWVVRTUlFQT05NTEtKSUhHRkVEQ0JBQD8+PTw7Ojk4NzY1NDMyMTAvLi0sKyopKCcmJSQjIiEgHx4dHBsaGRgXFhUUExIREA8ODQwLCgkIBwYFBAMCAQAAIfkEAAAAAAAsAAAAAAwADAAAAhaEH6mHmmzcgzJAUG/NVGrfOZ8YLlABADs=) top left repeat;border:1px solid #ccc;display:table-cell;vertical-align:middle;text-align:center}.icon{display:inline;display:inline-block}h2{margin:0;padding:0;font-size:1em;font-weight:normal;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;position:absolute;left:1em;right:1em;bottom:1em}footer{display:block;margin:0;padding:0 3em 3em 3em}footer p{margin:0;font-size:.7em}footer a{color:#0f7595;margin-left:0}</style>',
'prenav' => '</head><body><header><h1 id="logo">iconizr</h1><p>These are the <strong>'.count($this->_iconNames).' icons</strong> contained in your icon kit, along with their CSS class names.</p></header><section><nav>Icon type:',
'typelinks' => '',
'postnav' => '</nav><ul>',
'icons' => '',
'end' => '</ul></section><footer><p>Generated at '.gmdate('D, d M Y H:i:s', time()).' GMT by <a href="http://iconizr.com" target="_blank">iconizr</a>.</p></footer></body></html>',
);
// Render the icon previews
foreach ($this->_iconNames as $icon) {
$pseudoClassPos = strrpos($icon, $this->_flags['pseudo']);
$icon = ($pseudoClassPos === false) ? $icon : substr($icon, 0, $pseudoClassPos).':'.substr($icon, $pseudoClassPos + 1);
$preview['icons'] .= '<li title="'.$icon.'"><div class="icon-box"><div class="icon '.$icon.' '.$icon.'-dims"><!-- '.$icon.' --></div></div><h2>'.$icon.'</h2></li>';
}
// Run through all available icon types / style sheets
foreach ($stylesheets as $stylesheet => $label) {
$iconTypePreview = $preview;
$iconTypePreview['css'] = strlen($stylesheet) ? '<link href="'.htmlspecialchars($stylesheet).'" rel="stylesheet" type="text/css" media="all"/>' : sprintf($loader, htmlspecialchars(basename($css[self::PNG][self::SPRITE])), htmlspecialchars(basename($css[self::PNG][self::DATA])), htmlspecialchars(basename($css[self::SVG][self::SPRITE])), htmlspecialchars(basename($css[self::SVG][self::DATA])));
// Run through all available icon types and create the navigation elements
foreach ($stylesheets as $linkStylesheet => $linkLabel) {
$linkIconPreviewFilename = strlen($linkStylesheet) ? pathinfo($linkStylesheet, PATHINFO_FILENAME).'-preview.html' : $this->_flags['css'].'-preview.html';
$currentIconType = $stylesheet == $linkStylesheet;
$iconTypePreview['typelinks'] .= '<a href="'.$linkIconPreviewFilename.'"'.($currentIconType ? ' class="current"' : '').'>'.htmlspecialchars($linkLabel).'</a>';
}
$previewFilename = strlen($stylesheet) ? pathinfo($stylesheet, PATHINFO_FILENAME).'-preview.html' : $this->_flags['css'].'-preview.html';
// Write the preview file to disk
file_put_contents($this->_target.$previewFilename, implode('', $iconTypePreview));
}
}
/**
* Create a single icon stack
*
* @param string $directory Directory
* @param array $icons SVG icons
* @return void
*/
protected function _createIconStack($directory, array $icons) {
$this->_log(sprintf('Processing icon directory "%s"', substr($directory, strlen($this->_cwd) + 1)), self::LOG_INFO);
// Create a temporary directory
$this->_tmpResources = array();
$this->_tmpName = $this->_uniqueName($directory);
$this->_tmpDir = $this->_target.$this->_tmpName;
if (!@is_dir($this->_tmpDir) && !@mkdir($this->_tmpDir, 0777, true)) {
$this->_error(sprintf('Could not create temporary directory "%s", exiting', $this->_tmpDir));
}
// Prepare variables
$this->_dimensions[$directory] = array();
$this->_dataUris[$directory] =
$this->_useSprite[$directory] = array(
self::SVG => array(),
self::PNG => array(),
);
// Process the SVG icons
$this->_processSVGIcons($directory, $icons);
// Process the PNG icons
$this->_processPNGIcons($directory, $icons);
// Remove the intermediate files if they shouldn't be kept
if (!$this->_flags['keep']) {
$this->_delete($this->_tmpResources);
}
}
/**
* Process the SVG icons
*
* @param string $directory Directory
* @param array $icons SVG icons
* @return void
*/
protected function _processSVGIcons($directory, array &$icons) {
$this->_logGroupStart('Processing SVG icons ...');
$optimizeSVG = false;
// If the SVG files should be optimized using Scour
if ($this->_scour && $this->_binaries['python']) {
$this->_logGroupStart('Optimizing SVG icons using Scour ...');
$optimizeSVG = true;
// If the SVG files should be optimized using SVGO
} elseif ($this->_binaries['svgo']) {
$this->_logGroupStart('Optimizing SVG icons using SVGO '.($this->_scour ? '(No Scour due to missing Python 2 support) ' : '').'...');
$optimizeSVG = true;
}
// Run through all icons
foreach ($icons as $iconIndex => $icon) {
/**
* Resolve all named entities inside the SVG document
*
* @see https://github.com/jkphl/iconizr/issues/5#issuecomment-23448050
*/
try {
$iconSVG = new \DOMDocument();
$iconSVG->load($directory.DIRECTORY_SEPARATOR.$icon, LIBXML_NOENT);
$iconSVG->save($directory.DIRECTORY_SEPARATOR.$icon);
unset($iconSVG);
// If an error had occured
} catch (\Exception $e) {
$this->_error(sprintf('The icon "%s" seems to be no valid XML document, skipping ...: %s', basename($targetIcon), $e->getMessage()), false, 0);
continue;
}
$iconName = pathinfo($icon, PATHINFO_FILENAME);
$this->_iconNames[] = (strlen($this->_prefix) ? $this->_prefix : $this->_tmpName).'-'.$iconName;
$this->_tmpResources[] =
$targetIcon = $this->_tmpDir.DIRECTORY_SEPARATOR.$icon;
$iconOptimized = false;
// If the Scour script is available
if ($optimizeSVG && $this->_scour && $this->_binaries['python']) {
$this->_log(sprintf('[%s/%s] Optimizing SVG icon "%s" using Scour', $iconIndex + 1, count($icons), basename($targetIcon)));
// If an optimized copy of the icon can be created ...
if ($this->_do($this->_binaries['python'], array(
array($this->_scour),
'--create-groups',
'--enable-comment-stripping',
'--remove-metadata',
'--indent=none',
'--renderer-workaround',
'--strip-xml-prolog',
'-q',
'-i' => $directory.DIRECTORY_SEPARATOR.$icon,
'-o' => $targetIcon,
))) {
$iconOptimized = true;
// Else: Error and fallback to SVGO (if available)
} else {
$this->_error(sprintf('Optimization of icon "%s" with Scour failed, skipping ...', basename($targetIcon)), false, 0);
}
}
// If the SVGO binary is available
if ($optimizeSVG && !$iconOptimized && $this->_binaries['svgo']) {
$this->_log(sprintf('[%s/%s] Optimizing SVG icon "%s" using SVGO', $iconIndex + 1, count($icons), basename($targetIcon)));
// If an optimized copy of the icon can be created ...
if ($this->_do($this->_binaries['svgo'], array(
'-i' => $directory.DIRECTORY_SEPARATOR.$icon,
'-o' => $targetIcon,
))) {
$iconOptimized = true;
// Else: Error and fallback to the unoptimized SVG file
} else {
$this->_error(sprintf('Optimization of icon "%s" with SVGO failed, skipping ...', basename($targetIcon)), false, 0);
}
}
// If the unoptimized SVG file is to be used
if (!$iconOptimized && !@copy($directory.DIRECTORY_SEPARATOR.$icon, $targetIcon)) {
$this->_error(sprintf('Could not copy icon "%s", exiting', basename($targetIcon)));
}
try {
// Sanitize and prepare optimized SVG file
$this->_sanitizeSVGIcon($directory, $iconName, $targetIcon);
// Create and register a data URI for this PNG icon
$this->_registerDataURI($directory, self::SVG, $iconName, $targetIcon);
$this->_useSprite[$directory][self::SVG][$iconName] = $targetIcon;
// If an error had occured
} catch (\Exception $e) {
$this->_error(sprintf('The icon "%s" could not be processed due to the following error: %s', basename($targetIcon), $e->getMessage()));
}
}
// If optimization has taken place
if ($optimizeSVG) {
$this->_logGroupEnd();
}
// If single image icons should be created / kept
if ($this->_flags['keep']) {
// If CSS rules shall be generated
if ($this->_flags['css'] !== false) {
$this->_css[self::SVG][self::SINGLE] = array_merge($this->_css[self::SVG][self::SINGLE], $this->_createSingleImageCssRules($directory, $this->_useSprite[$directory][self::SVG], self::SVG));
}
// If Sass rules shall be generated
if ($this->_flags['sass'] !== false) {
$this->_sass[self::SVG][self::SINGLE] = array_merge($this->_sass[self::SVG][self::SINGLE], $this->_createSingleImageSassRules($directory, $this->_useSprite[$directory][self::SVG], self::SVG));
}
}
// If data URIs can be used
if ($dataUris = is_array($this->_dataUris[$directory][self::SVG])) {
// If CSS rules shall be generated
if ($this->_flags['css'] !== false) {
$this->_css[self::SVG][self::DATA] = array_merge($this->_css[self::SVG][self::DATA], $this->_createDataURICssRules($directory, $this->_dataUris[$directory][self::SVG], self::SVG));
}
// If Sass rules shall be generated
if ($this->_flags['sass'] !== false) {
$this->_sass[self::SVG][self::DATA] = array_merge($this->_sass[self::SVG][self::DATA], $this->_createDataURISassRules($directory, $this->_dataUris[$directory][self::SVG], self::SVG));
}
}
// Create SVG sprite and appropriate rules
$this->_createSVGIconSpriteAndRules($directory, $this->_useSprite[$directory][self::SVG], !$dataUris);
$this->_logGroupEnd();
}
/**
* Process the PNG icons
*
* @param string $directory Directory
* @param array $icons SVG icons
* @return void
*/
protected function _processPNGIcons($directory, array $icons) {
$this->_logGroupStart('Processing PNG icons ...');
// If the phantomJS binary is available
if ($this->_binaries['phantomjs']) {
chdir($this->_tmpDir);
$icons = array();
// Run through all icons, create phantomJS scripts an run phantomJS
foreach ($this->_useSprite[$directory][self::SVG] as $name => $icon) {
try {
$icons[] = array_merge(array(basename($icon), $name.'.png'), $this->_getIconDimensions($directory, $icon, $name));
$this->_tmpResources[] =
$this->_useSprite[$directory][self::PNG][$name] = $this->_tmpDir.DIRECTORY_SEPARATOR.$name.'.png';
} catch (\Exception $e) {
$this->_error('Problem processing icon "%s": '.$e->getMessage(), $name);
}
}
$phantomJSScript = $this->_tmpDir.DIRECTORY_SEPARATOR.'iconizr.js';
if (!@file_put_contents($phantomJSScript, sprintf(self::$_phantomJSSCript, json_encode($icons)))) {
$this->_error('Could not create PhantomJS script file, exiting');
}
$this->_log('Rendering SVG icons to PNG images ...');
$rendered = $this->_do($this->_binaries['phantomjs'], array('iconizr.js'));
@unlink($phantomJSScript);
if (!$rendered) {
$this->_error('Could not render SVG images, exiting');
}
// If PNG optimization should happen: Optimize and run through als PNG images
if ($this->_optimize) {
$this->_logGroupStart('Optimizing PNG images ...');
$optimizePNGImages = $this->_optimizePNGImages($this->_useSprite[$directory][self::PNG]);
foreach ($optimizePNGImages as $png => $optimized) {
$targetIcon = $this->_tmpDir.DIRECTORY_SEPARATOR.$optimized;
if (($targetIcon != $png) && (filesize($targetIcon) < filesize($png))) {
unlink($png);
rename($targetIcon, $png);
}
// Create and register a data URI for this PNG icon
$this->_registerDataURI($directory, self::PNG, array_search($png, $this->_useSprite[$directory][self::PNG]), $png);
}
$this->_logGroupEnd();
// Else: Create data URIs for the unoptimized PNG images
} else {
foreach ($this->_useSprite[$directory][self::PNG] as $icon => $png) {
$this->_registerDataURI($directory, self::PNG, $icon, $png);
}
}
// If single image icons should be created / kept
if ($this->_flags['keep']) {
// If CSS rules shall be generated
if ($this->_flags['css'] !== false) {
$this->_css[self::PNG][self::SINGLE] = array_merge($this->_css[self::PNG][self::SINGLE], $this->_createSingleImageCssRules($directory, $this->_useSprite[$directory][self::PNG], self::PNG));
}
// If Sass rules shall be generated
if ($this->_flags['sass'] !== false) {
$this->_sass[self::PNG][self::SINGLE] = array_merge($this->_sass[self::PNG][self::SINGLE], $this->_createSingleImageSassRules($directory, $this->_useSprite[$directory][self::PNG], self::PNG));
}
}
// If data URIs can be used
if ($dataUris = is_array($this->_dataUris[$directory][self::PNG])) {
// If CSS rules shall be generated
if ($this->_flags['css'] !== false) {
$this->_css[self::PNG][self::DATA] = array_merge($this->_css[self::PNG][self::DATA], $this->_createDataURICssRules($directory, $this->_dataUris[$directory][self::PNG], self::PNG));
}
// If Sass rules shall be generated
if ($this->_flags['sass'] !== false) {
$this->_sass[self::PNG][self::DATA] = array_merge($this->_sass[self::PNG][self::DATA], $this->_createDataURISassRules($directory, $this->_dataUris[$directory][self::PNG], self::PNG));
}
}
// Create PNG sprite and appropriate rules
$this->_createPNGIconSpriteAndRules($directory, $this->_useSprite[$directory][self::PNG], !$dataUris);
}
$this->_logGroupEnd();
}
/**
* Create and register a data URI for a file
*
* @param string $directory Directory
* @param string $type Icon type
* @param string $name Icon name
* @param string $file Icon
* @return void
*/
protected function _registerDataURI($directory, $type, $name, $file) {
if (is_array($this->_dataUris[$directory][$type])) {
$this->_dataUris[$directory][$type][$name] = ($type == self::PNG) ? 'data:image/png;base64,'.base64_encode(file_get_contents($file)) : 'data:image/svg+xml,'.rawurlencode(@file_get_contents($file));
if (($dataUriLength = strlen($this->_dataUris[$directory][$type][$name])) > $this->_thresholds[$type]) {
$this->_dataUris[$directory][$type] = false;
$this->_log(sprintf('Data URI for icon "%s" exceeds %s byte limit (%s), switching to sprite only mode', basename($file), $this->_thresholds[$type], $dataUriLength), self::LOG_ALERT);
}
}
}
/**
* Determine the dimensions of an icon
*
* @param string $directory Directory
* @param string\DOMDocument $icon Icon
* @param string $name Icon name
* @return array Dimensions (width & height)
*/
protected function _getIconDimensions($directory, $icon, $name) {
if (!array_key_exists($directory, $this->_dimensions)) {
$this->_dimensions[$directory] = array();
}
if (!array_key_exists($name, $this->_dimensions[$directory])) {
// Determine the real icon dimensions (with fallback to the default dimensions)