@@ -30,9 +30,6 @@ class Exporter extends Pretty
30
30
/** @var array Colors cached for each types. */
31
31
protected $ colors = [];
32
32
33
- /** @var array Lengths of each line */
34
- protected $ lengths = [];
35
-
36
33
public function __destruct ()
37
34
{
38
35
if (\is_resource ($ this ->image )) {
@@ -48,8 +45,11 @@ public function export(string $output, array $options = [])
48
45
49
46
$ this ->setOptions ($ options );
50
47
51
- $ this ->imgSize = $ this ->estimateSize ($ this ->code );
52
- $ this ->image = \imagecreate ($ this ->imgSize ['x ' ] + 50 , $ this ->imgSize ['y ' ] + 25 );
48
+ $ this ->imgSize = $ this ->estimateSize ($ this ->code );
49
+ $ this ->lineCount = \substr_count ($ this ->code , "\n" ) ?: 1 ;
50
+
51
+ $ padLineNo = $ this ->withLineNo ? $ this ->estimateSize ($ this ->formatLineNo ())['x ' ] : 0 ;
52
+ $ this ->image = \imagecreate ($ this ->imgSize ['x ' ] + $ padLineNo + 50 , $ this ->imgSize ['y ' ] + 25 );
53
53
54
54
\imagecolorallocate ($ this ->image , 0 , 0 , 0 );
55
55
@@ -60,6 +60,8 @@ public function export(string $output, array $options = [])
60
60
61
61
protected function setOptions (array $ options )
62
62
{
63
+ parent ::setOptions ($ options );
64
+
63
65
if (isset ($ options ['size ' ])) {
64
66
$ this ->size = $ options ['size ' ];
65
67
}
@@ -90,28 +92,32 @@ protected function estimateSize(string $for): array
90
92
return ['x ' => $ box [2 ], 'y ' => $ box [1 ], 'y1 ' => \intval ($ box [1 ] / $ eol )];
91
93
}
92
94
93
- protected function reset ()
95
+ protected function doReset ()
94
96
{
95
- $ this ->colors = $ this -> lengths = [];
97
+ $ this ->colors = [];
96
98
}
97
99
98
100
protected function visit (\DOMNode $ el )
99
101
{
100
- $ lineNo = $ el ->getLineNo () - 2 ;
101
102
$ type = $ el instanceof \DOMElement ? $ el ->getAttribute ('data-type ' ) : 'raw ' ;
102
103
$ color = $ this ->colorCode ($ type );
104
+ $ ncolor = $ this ->colorCode ('lineno ' );
103
105
$ text = \str_replace ([' ' , '< ' , '> ' ], [' ' , '< ' , '> ' ], $ el ->textContent );
104
106
105
107
foreach (\explode ("\n" , $ text ) as $ line ) {
106
- $ lineNo ++;
108
+ $ xlen = $ this ->lengths [$ this ->lineNo ] ?? 0 ;
109
+ $ ypos = 12 + $ this ->imgSize ['y1 ' ] * $ this ->lineNo ;
110
+
111
+ if ('' !== $ lineNo = $ this ->formatLineNo ()) {
112
+ $ xlen += $ this ->estimateSize ($ lineNo )['x ' ];
113
+ \imagefttext ($ this ->image , $ this ->size , 0 , 12 , $ ypos , $ ncolor , $ this ->font , $ lineNo );
114
+ }
107
115
108
- $ xlen = $ this ->lengths [$ lineNo ] ?? 0 ;
109
- $ xpos = 12 + $ xlen ;
110
- $ ypos = 12 + $ this ->imgSize ['y1 ' ] * $ lineNo ;
116
+ \imagefttext ($ this ->image , $ this ->size , 0 , 12 + $ xlen , $ ypos , $ color , $ this ->font , $ line );
111
117
112
- \imagefttext ( $ this ->image , $ this ->size , 0 , $ xpos , $ ypos , $ color , $ this ->font , $ line );
118
+ $ this ->lengths [ $ this ->lineNo ] = $ xlen + $ this ->estimateSize ( $ line )[ ' x ' ] ;
113
119
114
- $ this ->lengths [ $ lineNo] = $ xlen + $ this -> estimateSize ( $ line )[ ' x ' ] ;
120
+ $ this ->lineNo ++ ;
115
121
}
116
122
}
117
123
@@ -127,6 +133,7 @@ protected function colorCode(string $type): int
127
133
'keyword ' => [192 , 0 , 0 ],
128
134
'string ' => [192 , 192 , 0 ],
129
135
'raw ' => [128 , 128 , 128 ],
136
+ 'lineno ' => [128 , 224 , 224 ],
130
137
];
131
138
132
139
return $ this ->colors [$ type ] = \imagecolorallocate ($ this ->image , ...$ palette [$ type ]);
0 commit comments