Skip to content

Commit 6662cc0

Browse files
feature: Added font-weight support (#165)
Co-authored-by: Jonah Lawrence <[email protected]>
1 parent d1dbb26 commit 6662cc0

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

src/demo/index.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ function gtag() {
6464
</div>
6565
<input class="param" type="text" id="font" name="font" alt="Font name" placeholder="Fira Code" value="Fira Code" pattern="^[A-Za-z0-9\- ]*$" title="Font from Google Fonts. Only letters, numbers, and spaces.">
6666

67+
<label for="weight">Font weight</label>
68+
<input class="param" type="number" id="weight" name="weight" alt="Font weight" placeholder="400" value="400" min="100" max="900" step="100">
69+
6770
<label for="size">Font size</label>
6871
<input class="param" type="number" id="size" name="size" alt="Font size" placeholder="20" value="20">
6972

src/demo/js/script.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ let preview = {
22
// default values
33
defaults: {
44
font: "monospace",
5+
weight: "400",
56
color: "36BCF7",
67
background: "00000000",
78
size: "20",

src/models/GoogleFontConverter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ class GoogleFontConverter
1212
* @param string $text Text to display in font
1313
* @return string|false The CSS for displaying the font
1414
*/
15-
public static function fetchFontCSS($font, $text)
15+
public static function fetchFontCSS($font, $weight, $text)
1616
{
1717
$url =
1818
"https://fonts.googleapis.com/css2?" .
1919
http_build_query([
20-
"family" => $font,
20+
"family" => $font . ":wght@" . $weight,
2121
"text" => $text,
2222
"display" => "fallback",
2323
]);

src/models/RendererModel.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class RendererModel
1313
/** @var string $font Font family */
1414
public $font;
1515

16+
/** @var string $font Font weight */
17+
public $weight;
18+
1619
/** @var string $color Font color */
1720
public $color;
1821

@@ -52,6 +55,7 @@ class RendererModel
5255
/** @var array<string, string> $DEFAULTS */
5356
private $DEFAULTS = [
5457
"font" => "monospace",
58+
"weight" => "400",
5559
"color" => "#36BCF7",
5660
"background" => "#00000000",
5761
"size" => "20",
@@ -75,6 +79,7 @@ public function __construct($template, $params)
7579
$this->template = $template;
7680
$this->lines = $this->checkLines($params["lines"] ?? "");
7781
$this->font = $this->checkFont($params["font"] ?? $this->DEFAULTS["font"]);
82+
$this->weight = $this->checkNumberPositive($params["weight"] ?? $this->DEFAULTS["weight"], "Font weight");
7883
$this->color = $this->checkColor($params["color"] ?? $this->DEFAULTS["color"], "color");
7984
$this->background = $this->checkColor($params["background"] ?? $this->DEFAULTS["background"], "background");
8085
$this->size = $this->checkNumberPositive($params["size"] ?? $this->DEFAULTS["size"], "Font size");
@@ -85,7 +90,7 @@ public function __construct($template, $params)
8590
$this->multiline = $this->checkBoolean($params["multiline"] ?? $this->DEFAULTS["multiline"]);
8691
$this->duration = $this->checkNumberPositive($params["duration"] ?? $this->DEFAULTS["duration"], "duration");
8792
$this->pause = $this->checkNumberNonNegative($params["pause"] ?? $this->DEFAULTS["pause"], "pause");
88-
$this->fontCSS = $this->fetchFontCSS($this->font, $params["lines"]);
93+
$this->fontCSS = $this->fetchFontCSS($this->font, $this->weight, $params["lines"]);
8994
}
9095

9196
/**
@@ -185,12 +190,12 @@ private function checkBoolean($bool)
185190
* @param string $text Text to display in font
186191
* @return string The CSS for displaying the font
187192
*/
188-
private function fetchFontCSS($font, $text)
193+
private function fetchFontCSS($font, $weight, $text)
189194
{
190195
// skip checking if left as default
191196
if ($font != $this->DEFAULTS["font"]) {
192197
// fetch and convert from Google Fonts
193-
$from_google_fonts = GoogleFontConverter::fetchFontCSS($font, $text);
198+
$from_google_fonts = GoogleFontConverter::fetchFontCSS($font, $weight, $text);
194199
if ($from_google_fonts) {
195200
// return the CSS for displaying the font
196201
return "<style>\n{$from_google_fonts}</style>\n";

0 commit comments

Comments
 (0)