-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ability to add text stroke (#645)
![af8e1b7b-0c8a-4722-833c-df1b38b0df26](https://github.com/user-attachments/assets/af7d0862-a31e-43f9-9415-acab1ea98dd5) This PR is adding ability to stroke to texts. fixes: #578 Added 2 properties (`WebkitTextStrokeWidth`, `WebkitTextStrokeColor`) and 1 shorthands (`WebkitTextStroke`). When stroke is enabled, `paint-order: stroke;` and `stroke-linejoin: round;` are automatically set to prevent the stroke from obscuring the text. I don't have a deep understanding of all the code so further improvements may be needed.
- Loading branch information
Showing
9 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+2.28 KB
...ext-stroke-test-tsx-webkit-text-stroke-should-work-basic-text-stroke-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+3.04 KB
...st-tsx-webkit-text-stroke-should-work-nested-and-complex-text-stroke-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.37 KB
...xt-stroke-test-tsx-webkit-text-stroke-should-work-nested-text-stroke-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { it, describe, expect } from 'vitest' | ||
|
||
import { initFonts, toImage } from './utils.js' | ||
import satori from '../src/index.js' | ||
|
||
describe('webkit-text-stroke', () => { | ||
let fonts | ||
initFonts((f) => (fonts = f)) | ||
|
||
it('should work basic text stroke', async () => { | ||
const svg = await satori( | ||
<div | ||
style={{ | ||
width: 100, | ||
height: 100, | ||
fontSize: 30, | ||
background: '#ebebeb', | ||
color: '#ffffff', | ||
WebkitTextStroke: '4px #000000', | ||
}} | ||
> | ||
Hello, world | ||
</div>, | ||
{ width: 100, height: 100, fonts } | ||
) | ||
expect(toImage(svg, 100)).toMatchImageSnapshot() | ||
}) | ||
|
||
it('should work nested text stroke', async () => { | ||
const svg = await satori( | ||
<div | ||
style={{ | ||
display: 'flex', | ||
flexWrap: 'wrap', | ||
width: 100, | ||
height: 100, | ||
fontSize: 30, | ||
background: '#ebebeb', | ||
color: '#ffffff', | ||
WebkitTextStroke: '4px #000000', | ||
}} | ||
> | ||
Hello, <span style={{ WebkitTextStrokeColor: '#ff0000' }}>world</span> | ||
</div>, | ||
{ width: 100, height: 100, fonts } | ||
) | ||
expect(toImage(svg, 100)).toMatchImageSnapshot() | ||
}) | ||
|
||
it('should work nested and complex text stroke', async () => { | ||
const svg = await satori( | ||
<div | ||
style={{ | ||
display: 'flex', | ||
flexWrap: 'wrap', | ||
width: 100, | ||
height: 100, | ||
fontSize: 30, | ||
background: '#ebebeb', | ||
color: '#ffffff', | ||
WebkitTextStroke: '4px #000000', | ||
}} | ||
> | ||
Hello, | ||
<span style={{ WebkitTextStrokeColor: '#f00' }}>w</span> | ||
<span style={{ WebkitTextStrokeColor: '#ff0' }}>o</span> | ||
<span style={{ WebkitTextStrokeColor: '#0f0' }}>r</span> | ||
<span style={{ WebkitTextStrokeColor: '#0ff' }}>l</span> | ||
<span style={{ WebkitTextStrokeColor: '#00f' }}>d</span> | ||
<span | ||
style={{ | ||
WebkitTextStrokeColor: '#f0f', | ||
WebkitTextStrokeWidth: '6px', | ||
}} | ||
> | ||
! | ||
</span> | ||
</div>, | ||
{ width: 100, height: 100, fonts } | ||
) | ||
expect(toImage(svg, 100)).toMatchImageSnapshot() | ||
}) | ||
}) |