Skip to content

Commit 6a38fd4

Browse files
committed
refactor(exporter): no static, add setOptions, reset, refactor visit
1 parent e6d6b4e commit 6a38fd4

File tree

1 file changed

+49
-18
lines changed

1 file changed

+49
-18
lines changed

src/Exporter.php

+49-18
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Exporter extends Pretty
1919
protected $size = 16;
2020

2121
/** @var string Font path */
22-
protected $font = __DIR__ . '/../font/dejavu.ttf';
22+
protected $font = __DIR__ . '/../font/ubuntu.ttf';
2323

2424
/** @var resource The image */
2525
protected $image;
@@ -28,10 +28,10 @@ class Exporter extends Pretty
2828
protected $imgSize = [];
2929

3030
/** @var array Colors cached for each types. */
31-
protected static $colors = [];
31+
protected $colors = [];
3232

3333
/** @var array Lengths of each line */
34-
protected static $lengths = [];
34+
protected $lengths = [];
3535

3636
public function __destruct()
3737
{
@@ -40,10 +40,12 @@ public function __destruct()
4040
}
4141
}
4242

43-
public function export(string $output)
43+
public function export(string $output, array $options = [])
4444
{
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);
4749

4850
\imagecolorallocate($this->image, 0, 0, 0);
4951

@@ -52,47 +54,76 @@ public function export(string $output)
5254
\imagepng($this->image, $output);
5355
}
5456

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
5681
{
5782
$eol = \substr_count($for, "\n") ?: 1;
5883
$box = \imagettfbbox($this->size, 0, $this->font, $for);
5984

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 = [];
6191
}
6292

63-
protected function visit(\DOMElement $el)
93+
protected function visit(\DOMNode $el)
6494
{
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';
6797
$color = $this->colorCode($type);
6898
$text = \str_replace(['&nbsp;', '&lt;', '&gt;'], [' ', '<', '>'], $el->textContent);
6999

70-
foreach (\explode("\n", \rtrim($text, "\r\n")) as $line) {
100+
foreach (\explode("\n", $text) as $line) {
71101
$lineNo++;
72102

73-
$xlen = static::$lengths[$lineNo] ?? 0;
103+
$xlen = $this->lengths[$lineNo] ?? 0;
74104
$xpos = 12 + $xlen;
75-
$ypos = $this->imgSize['y1'] * $lineNo;
105+
$ypos = 12 + $this->imgSize['y1'] * $lineNo;
76106

77107
\imagefttext($this->image, $this->size, 0, $xpos, $ypos, $color, $this->font, $line);
78108

79-
static::$lengths[$lineNo] = $xlen + $this->estimateSize($line)['x'];
109+
$this->lengths[$lineNo] = $xlen + $this->estimateSize($line)['x'];
80110
}
81111
}
82112

83113
protected function colorCode(string $type): int
84114
{
85-
if (isset(static::$colors[$type])) {
86-
return static::$colors[$type];
115+
if (isset($this->colors[$type])) {
116+
return $this->colors[$type];
87117
}
88118

89119
$palette = [
90120
'comment' => [0, 96, 192],
91121
'default' => [0, 192, 0],
92122
'keyword' => [192, 0, 0],
93123
'string' => [192, 192, 0],
124+
'raw' => [128, 128, 128],
94125
];
95126

96-
return static::$colors[$type] = \imagecolorallocate($this->image, ...$palette[$type]);
127+
return $this->colors[$type] = \imagecolorallocate($this->image, ...$palette[$type]);
97128
}
98129
}

0 commit comments

Comments
 (0)