Skip to content

Commit 5221a0b

Browse files
JulianGluecktomloprod
authored andcommitted
Add support for setting the font weight
1 parent 8530e14 commit 5221a0b

5 files changed

Lines changed: 30 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ $svg = radiance()
143143
| `fontFamily(string $font)` | Set the font-family for the text. | `monospace` |
144144
| `fontSizeRatio(float $ratio)` | Set font size as ratio of avatar size (0.01-1.0). Disables auto-sizing. | `0.5` |
145145
| `fontSizeRatioAuto(bool $enabled)` | Enable/disable automatic font sizing based on text length. | `true` |
146+
| `fontWeight(int $fontWeight)` | Set the weight of the used font. | `600` |
146147
| `textShadow(float $val)` | Set text shadow intensity. 0 = no shadow. | `1.0` |
147148

148149
### Pixel Pattern

src/Concerns/ConfiguresText.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ trait ConfiguresText
2323
*/
2424
private float $fontSizeRatio = 0.5;
2525

26+
/**
27+
* The font weight for the text.
28+
*/
29+
private int $fontWeight = 600;
30+
2631
/**
2732
* The intensity of the text shadow.
2833
*
@@ -70,6 +75,16 @@ public function fontSizeRatio(float $ratio): self
7075
return $this;
7176
}
7277

78+
/**
79+
* Set the font weight for the text.
80+
*/
81+
public function fontWeight(int $fontWeight): self
82+
{
83+
$this->fontWeight = $fontWeight;
84+
85+
return $this;
86+
}
87+
7388
/**
7489
* Set the intensity of the text shadow.
7590
*/

src/Services/RadianceManager.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ public function toSvg(): string
121121
$fontSize = (int) ($this->size * $ratio);
122122
$escapedText = htmlspecialchars($this->text, ENT_XML1, 'UTF-8');
123123

124+
$fontWeight = $this->fontWeight;
125+
124126
$textElement = <<<SVG
125127
<text
126128
x="50%"
@@ -131,7 +133,7 @@ public function toSvg(): string
131133
fill-opacity="1"
132134
font-family="{$fontFamilyWithFallbacks}"
133135
font-size="{$fontSize}"
134-
font-weight="600"
136+
font-weight="{$fontWeight}"
135137
filter="url(#{$filterId})"
136138
>{$escapedText}</text>
137139
SVG;

src/Support/Facades/Radiance.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* @method static RadianceManager text(string $text)
2323
* @method static RadianceManager fontFamily(string $fontFamily)
2424
* @method static RadianceManager fontSizeRatio(float $ratio)
25+
* @method static RadianceManager fontWeight(int $fontWeight)
2526
* @method static RadianceManager textShadow(float $intensity)
2627
* @method static RadianceManager fontSizeRatioAuto(bool $enabled = true)
2728
* @method static RadianceManager enablePixelPattern(bool $enable = true)

tests/Services/RadianceManagerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,16 @@
238238
expect($svg)->toContain('font-size="100"');
239239
});
240240

241+
it('allows customizing font weight', function (): void {
242+
$svg = RadianceManager::instance()
243+
->seed('test')
244+
->text('XY')
245+
->fontWeight(400)
246+
->toSvg();
247+
248+
expect($svg)->toContain('font-weight="400"');
249+
});
250+
241251
it('uses auto fontSizeRatio by default', function (): void {
242252
$svg = RadianceManager::instance()
243253
->seed('test')

0 commit comments

Comments
 (0)