@@ -30,9 +30,6 @@ class Exporter extends Pretty
3030 /** @var array Colors cached for each types. */
3131 protected $ colors = [];
3232
33- /** @var array Lengths of each line */
34- protected $ lengths = [];
35-
3633 public function __destruct ()
3734 {
3835 if (\is_resource ($ this ->image )) {
@@ -48,8 +45,11 @@ public function export(string $output, array $options = [])
4845
4946 $ this ->setOptions ($ options );
5047
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 );
5353
5454 \imagecolorallocate ($ this ->image , 0 , 0 , 0 );
5555
@@ -60,6 +60,8 @@ public function export(string $output, array $options = [])
6060
6161 protected function setOptions (array $ options )
6262 {
63+ parent ::setOptions ($ options );
64+
6365 if (isset ($ options ['size ' ])) {
6466 $ this ->size = $ options ['size ' ];
6567 }
@@ -90,28 +92,32 @@ protected function estimateSize(string $for): array
9092 return ['x ' => $ box [2 ], 'y ' => $ box [1 ], 'y1 ' => \intval ($ box [1 ] / $ eol )];
9193 }
9294
93- protected function reset ()
95+ protected function doReset ()
9496 {
95- $ this ->colors = $ this -> lengths = [];
97+ $ this ->colors = [];
9698 }
9799
98100 protected function visit (\DOMNode $ el )
99101 {
100- $ lineNo = $ el ->getLineNo () - 2 ;
101102 $ type = $ el instanceof \DOMElement ? $ el ->getAttribute ('data-type ' ) : 'raw ' ;
102103 $ color = $ this ->colorCode ($ type );
104+ $ ncolor = $ this ->colorCode ('lineno ' );
103105 $ text = \str_replace ([' ' , '< ' , '> ' ], [' ' , '< ' , '> ' ], $ el ->textContent );
104106
105107 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+ }
107115
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 );
111117
112- \imagefttext ( $ this ->image , $ this ->size , 0 , $ xpos , $ ypos , $ color , $ this ->font , $ line );
118+ $ this ->lengths [ $ this ->lineNo ] = $ xlen + $ this ->estimateSize ( $ line )[ ' x ' ] ;
113119
114- $ this ->lengths [ $ lineNo] = $ xlen + $ this -> estimateSize ( $ line )[ ' x ' ] ;
120+ $ this ->lineNo ++ ;
115121 }
116122 }
117123
@@ -127,6 +133,7 @@ protected function colorCode(string $type): int
127133 'keyword ' => [192 , 0 , 0 ],
128134 'string ' => [192 , 192 , 0 ],
129135 'raw ' => [128 , 128 , 128 ],
136+ 'lineno ' => [128 , 224 , 224 ],
130137 ];
131138
132139 return $ this ->colors [$ type ] = \imagecolorallocate ($ this ->image , ...$ palette [$ type ]);
0 commit comments