@@ -19,7 +19,7 @@ class Exporter extends Pretty
19
19
protected $ size = 16 ;
20
20
21
21
/** @var string Font path */
22
- protected $ font = __DIR__ . '/../font/dejavu .ttf ' ;
22
+ protected $ font = __DIR__ . '/../font/ubuntu .ttf ' ;
23
23
24
24
/** @var resource The image */
25
25
protected $ image ;
@@ -28,10 +28,10 @@ class Exporter extends Pretty
28
28
protected $ imgSize = [];
29
29
30
30
/** @var array Colors cached for each types. */
31
- protected static $ colors = [];
31
+ protected $ colors = [];
32
32
33
33
/** @var array Lengths of each line */
34
- protected static $ lengths = [];
34
+ protected $ lengths = [];
35
35
36
36
public function __destruct ()
37
37
{
@@ -40,10 +40,12 @@ public function __destruct()
40
40
}
41
41
}
42
42
43
- public function export (string $ output )
43
+ public function export (string $ output, array $ options = [] )
44
44
{
45
- $ this ->imgSize = $ this ->estimateSize ($ this ->code , 25 );
46
- $ this ->image = \imagecreate ($ this ->imgSize ['x ' ], $ this ->imgSize ['y ' ]);
45
+ $ this ->setOptions ($ options );
46
+
47
+ $ this ->imgSize = $ this ->estimateSize ($ this ->code );
48
+ $ this ->image = \imagecreate ($ this ->imgSize ['x ' ] + 50 , $ this ->imgSize ['y ' ] + 25 );
47
49
48
50
\imagecolorallocate ($ this ->image , 0 , 0 , 0 );
49
51
@@ -52,47 +54,76 @@ public function export(string $output)
52
54
\imagepng ($ this ->image , $ output );
53
55
}
54
56
55
- protected function estimateSize (string $ for , int $ pad = 0 ): array
57
+ protected function setOptions (array $ options )
58
+ {
59
+ if (isset ($ options ['size ' ])) {
60
+ $ this ->size = $ options ['size ' ];
61
+ }
62
+
63
+ if (!isset ($ options ['font ' ])) {
64
+ return ;
65
+ }
66
+
67
+ $ font = $ options ['font ' ];
68
+ if (!\is_file ($ font )) {
69
+ $ basename = \basename ($ font , '.ttf ' );
70
+ $ font = __DIR__ . "/../font/ $ basename.ttf " ;
71
+ }
72
+
73
+ if (!\is_file ($ font )) {
74
+ throw new \InvalidArgumentException ('The given font doesnot exist. ' );
75
+ }
76
+
77
+ $ this ->font = $ font ;
78
+ }
79
+
80
+ protected function estimateSize (string $ for ): array
56
81
{
57
82
$ eol = \substr_count ($ for , "\n" ) ?: 1 ;
58
83
$ box = \imagettfbbox ($ this ->size , 0 , $ this ->font , $ for );
59
84
60
- return ['x ' => $ box [2 ] + $ pad , 'y ' => $ box [1 ] + $ pad , 'y1 ' => \intval ($ box [1 ] / $ eol )];
85
+ return ['x ' => $ box [2 ], 'y ' => $ box [1 ], 'y1 ' => \intval ($ box [1 ] / $ eol )];
86
+ }
87
+
88
+ protected function reset ()
89
+ {
90
+ $ this ->colors = $ this ->lengths = [];
61
91
}
62
92
63
- protected function visit (\DOMElement $ el )
93
+ protected function visit (\DOMNode $ el )
64
94
{
65
- $ lineNo = $ el ->getLineNo () - 1 ;
66
- $ type = $ el ->getAttribute ('data-type ' );
95
+ $ lineNo = $ el ->getLineNo () - 2 ;
96
+ $ type = $ el instanceof \DOMElement ? $ el ->getAttribute ('data-type ' ) : ' raw ' ;
67
97
$ color = $ this ->colorCode ($ type );
68
98
$ text = \str_replace ([' ' , '< ' , '> ' ], [' ' , '< ' , '> ' ], $ el ->textContent );
69
99
70
- foreach (\explode ("\n" , \rtrim ( $ text, "\r\n" ) ) as $ line ) {
100
+ foreach (\explode ("\n" , $ text ) as $ line ) {
71
101
$ lineNo ++;
72
102
73
- $ xlen = static :: $ lengths [$ lineNo ] ?? 0 ;
103
+ $ xlen = $ this -> lengths [$ lineNo ] ?? 0 ;
74
104
$ xpos = 12 + $ xlen ;
75
- $ ypos = $ this ->imgSize ['y1 ' ] * $ lineNo ;
105
+ $ ypos = 12 + $ this ->imgSize ['y1 ' ] * $ lineNo ;
76
106
77
107
\imagefttext ($ this ->image , $ this ->size , 0 , $ xpos , $ ypos , $ color , $ this ->font , $ line );
78
108
79
- static :: $ lengths [$ lineNo ] = $ xlen + $ this ->estimateSize ($ line )['x ' ];
109
+ $ this -> lengths [$ lineNo ] = $ xlen + $ this ->estimateSize ($ line )['x ' ];
80
110
}
81
111
}
82
112
83
113
protected function colorCode (string $ type ): int
84
114
{
85
- if (isset (static :: $ colors [$ type ])) {
86
- return static :: $ colors [$ type ];
115
+ if (isset ($ this -> colors [$ type ])) {
116
+ return $ this -> colors [$ type ];
87
117
}
88
118
89
119
$ palette = [
90
120
'comment ' => [0 , 96 , 192 ],
91
121
'default ' => [0 , 192 , 0 ],
92
122
'keyword ' => [192 , 0 , 0 ],
93
123
'string ' => [192 , 192 , 0 ],
124
+ 'raw ' => [128 , 128 , 128 ],
94
125
];
95
126
96
- return static :: $ colors [$ type ] = \imagecolorallocate ($ this ->image , ...$ palette [$ type ]);
127
+ return $ this -> colors [$ type ] = \imagecolorallocate ($ this ->image , ...$ palette [$ type ]);
97
128
}
98
129
}
0 commit comments