1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace SixtyNine\Cloud\Factory; |
5
|
|
|
|
6
|
|
|
use Imagine\Image\Color; |
7
|
|
|
use Imagine\Gd\Font as ImagineFont; |
8
|
|
|
use SixtyNine\Cloud\Model\Font; |
9
|
|
|
use Webmozart\Assert\Assert; |
10
|
|
|
|
11
|
|
|
class FontsFactory |
12
|
|
|
{ |
13
|
|
|
/** @var string|array */ |
14
|
|
|
private $fontsPath; |
15
|
|
|
|
16
|
|
|
/** @var array */ |
17
|
|
|
protected $fonts = array(); |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string|array $fontsPath string to scan directory with .ttf file extension or array with full path to each font |
21
|
|
|
*/ |
22
|
12 |
|
protected function __construct($fontsPath) |
23
|
|
|
{ |
24
|
12 |
|
$this->fontsPath = $fontsPath; |
25
|
12 |
|
$this->loadFonts(); |
26
|
12 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string|array $fontsPath string to scan directory with .ttf file extension or array with full path to each font |
30
|
|
|
* @return FontsFactory |
31
|
|
|
*/ |
32
|
12 |
|
public static function create($fontsPath) |
33
|
|
|
{ |
34
|
12 |
|
return new self($fontsPath); |
35
|
|
|
} |
36
|
|
|
|
37
|
12 |
|
public function loadFonts() |
38
|
|
|
{ |
39
|
12 |
|
$fontsPath = $this->fontsPath; |
40
|
12 |
|
if (is_string($fontsPath)) { |
41
|
12 |
|
Assert::fileExists($fontsPath, 'The fonts path must exist'); |
42
|
12 |
|
$fontsPath = glob($fontsPath . "/*.ttf"); |
43
|
|
|
} |
44
|
12 |
|
foreach ($fontsPath as $filename) { |
45
|
12 |
|
$name = basename($filename); |
46
|
12 |
|
$this->fonts[$name] = new Font($name, realpath($filename)); |
47
|
|
|
} |
48
|
12 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $name |
52
|
|
|
* @return Font |
53
|
|
|
* @throws \InvalidArgumentException |
54
|
|
|
*/ |
55
|
11 |
|
public function getFont($name) |
56
|
|
|
{ |
57
|
11 |
|
Assert::keyExists($this->fonts, $name, 'Font not found: ' . $name); |
58
|
11 |
|
return $this->fonts[$name]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $name |
63
|
|
|
* @param int $size |
64
|
|
|
* @param string $color |
65
|
|
|
* @return ImagineFont |
66
|
|
|
*/ |
67
|
11 |
|
public function getImagineFont($name, $size, $color = '#000000') |
68
|
|
|
{ |
69
|
11 |
|
$font = $this->getFont($name); |
70
|
11 |
|
return new ImagineFont($font->getFile(), $size, new Color($color)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function getFonts() |
77
|
|
|
{ |
78
|
|
|
return array_keys($this->fonts); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|