Skip to content

Commit 6f9516c

Browse files
duotone support
1 parent b901030 commit 6f9516c

File tree

2 files changed

+72
-9
lines changed

2 files changed

+72
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fontawesome-svgs/

src/FontAwesomeSVG.php

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ public function get_svg($id, $opts=[]) {
2929
$doc->load($icon['filepath']);
3030

3131
$default_opts = [
32-
'title' => false,
33-
'class' => false,
32+
'title' => false,
33+
'class' => false,
3434
'default_class' => true,
35-
'role' => 'img',
36-
'fill' => 'currentColor',
35+
'inline_style' => true,
36+
'role' => 'img',
37+
'fill' => 'currentColor',
3738
];
3839

3940
$opts = array_merge($default_opts, $opts);
@@ -90,9 +91,69 @@ public function get_svg($id, $opts=[]) {
9091

9192

9293

94+
9395
foreach ($doc->getElementsByTagName('path') as $item) {
94-
$item->setAttribute('fill', $opts['fill']);
96+
$fill = $opts['fill'];
97+
$opacity = false;
98+
99+
// duotone
100+
switch($item->getAttribute('class')) {
101+
case 'fa-primary':
102+
if(isset($opts['primary']['fill'])) $fill = $opts['primary']['fill'];
103+
if(isset($opts['primary']['opacity'])) $opacity = $opts['primary']['opacity'];
104+
break;
105+
106+
107+
case 'fa-secondary':
108+
if(isset($opts['secondary']['fill'])) $fill = $opts['secondary']['fill'];
109+
if(isset($opts['secondary']['opacity'])) $opacity = $opts['secondary']['opacity'];
110+
break;
111+
}
112+
113+
114+
$item->setAttribute('fill', $fill);
115+
if($opacity) $item->setAttribute('opacity', $opacity);
95116
}
117+
118+
119+
120+
121+
122+
// duotone styles
123+
if($opts['inline_style']) {
124+
$styles = [];
125+
126+
if(isset($opts['primary']['fill'])) {
127+
$styles[] = '--fa-primary-color:' . $opts['primary']['fill'];
128+
}
129+
130+
if(isset($opts['primary']['opacity'])) {
131+
$styles[] = '--fa-primary-opacity:' . $opts['primary']['opacity'];
132+
}
133+
134+
135+
if(isset($opts['secondary']['fill'])) {
136+
$styles[] = '--fa-secondary-color:' . $opts['secondary']['fill'];
137+
}
138+
139+
if(isset($opts['secondary']['opacity'])) {
140+
$styles[] = '--fa-secondary-opacity:' . $opts['secondary']['opacity'];
141+
}
142+
143+
144+
if(empty($styles) || !isset($opts['primary']['fill'], $opts['secondary']['fill']) ) {
145+
$styles[] = 'color:' . $opts['fill'];
146+
}
147+
148+
149+
150+
if($styles) {
151+
foreach ($doc->getElementsByTagName('svg') as $svg) {
152+
$svg->setAttribute('style', implode(';', $styles));
153+
}
154+
}
155+
}
156+
96157

97158
return $doc->saveHTML();
98159
}
@@ -135,10 +196,6 @@ public function get_icon_details($id) {
135196
*/
136197
public function get_icon_dir($style) {
137198
switch($style) {
138-
case 'fas':
139-
$dir = 'solid';
140-
break;
141-
142199
case 'far':
143200
$dir = 'regular';
144201
break;
@@ -151,6 +208,11 @@ public function get_icon_dir($style) {
151208
$dir = 'brands';
152209
break;
153210

211+
case 'fad':
212+
$dir = 'duotone';
213+
break;
214+
215+
case 'fas':
154216
default:
155217
$dir = 'solid';
156218
}

0 commit comments

Comments
 (0)