Skip to content

Commit

Permalink
feat: added some tests of WebkitTextStroke
Browse files Browse the repository at this point in the history
  • Loading branch information
AioiLight committed Nov 5, 2024
1 parent 87820b1 commit 37e40ee
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 22 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,13 @@ Satori uses the same Flexbox [layout engine](https://yogalayout.com) as React Na
<tr><td><code>maskRepeat</code></td><td><code>repeat</code>, <code>repeat-x</code>, <code>repeat-y</code>, <code>no-repeat</code>, defaults to <code>repeat</code></td><td><a href="https://og-playground.vercel.app/?share=nVbpjqNIEn6VkqXVzMg1AhtjQ-3MStwGA-Ywl9U_hssJ5jSHAbf63TdxdfXUzh4_FhllHF8cGZkm4usirKJ48bb4LUrvX8qXl7ab8vj3r19n-uUliVOQdG8vP61Q9G8_vb4LhzTqkr_IorStc3-C0ksejx_SmWbTJg67tCqhLqzyvig_tH6eglLs4qKdVXHZxc2H6tq3XXqZmAoKyzn-v6ovUG6mj_jtBVt_Ejnfs92i6Hdp4IcZaKq-jKCPvsl_jvzOf0sLH8RIXYK_B34bbzevqU0fjQE9CKCi4KOaVsJZAFK0PvMaQ3lwYbPiSNqzgHJV00BFqmk34XaGiEbucHlxslDqMNtRAJpyJkUpM0NTFAcXzqco6ztPUSbggs-8BTgY_AMvwl9UU9Qz_lPvPP0-WaiEz6zinkKoPwLIs9_lkGcATGEO-o6jTUANn3iKeJJ2F-6CJ58PJp8_ICFzA7QeFZqSbqHwBOW1zSeow62UY6HeAxNPzgKZnk18E7jfU2LHzbFMulBY5ZHAgVhYtUGpbGMWTT3HuHuFtZ35wLFRzyRScQ-2EDNEQkuKeaJaDM0GmJSLrNcrzGYQr5uDyFBA20vZ-VqbBuf98BkWRqGZUhXtjeGYEvcIizC5DB9yQU7niRiPpwyXH9QkP8RJdqF9unrEDo56Luig_fXD9yf_3NlVr2GRw3zye5DS01nwtp4j3SNXJ8VU_IH_eD9ygfj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Gc1fEaQtxrW6psdO4C5IhRxC1916J3mrxeDerWKcRC_0etLjCiR7IexI2eG0w57VB4S0zd53hq0xZI7sNuqZ2ka4KhmHvBQke2sbO_gn013TtlADlAmPyZEcSqsdVrqFl8IbmAgqnY9pFukjifCFN1mcGPOEFPsIAi-V0rgqfdMZSlIkpEGa4ua3J44SQ4thMqQRr8ujjuxUKgCIq3Iaheg6fbOvF2qN895725IMC-eaTAIA_P77Lx_NvfDbTJw79NvLH3laxn7zK2j8KIUTwM9d9dLMPf71Jcj7-PWla_yyrf0G6n7545P9-3AwTyj1-LJaz8tn90Zcx_48VjRP4tcfSicOsrRTPmXwl6GhvYPlWOQfg4O2V9fnicZ8x0B92OyOLDWIKV2dnbz097B5XMGgMCKICnsK1_MHGk0VczNCzBp-2DG9IDeaKQ4iSwHlJEIsNSrXp49N4Ix9-PjUXGCjiyYcUyb8HhbhfcYpDPmIijDVruPguUYlCjBmho4KMzxUk6Yhpn2-zDDILNeqJ0g_LKDDWDI7v0_dKLMZpJWVyHM45NeqOazgikMfhgJt1KvVzuts66QiOBd5G8D9RuukjgQrFRliCZvOnMvyx0H8ezH_n-P808v_ONQ_Qf_laL99-1L-40v5GwLHXLguXhdVPQ-l7eLt6-I50C7eZpevi_eRd_E2D5GLKA56sHi7-Hkbvy7iorqmp6me5-VueHLQ0Tx5ckUQR4u3runjb6-Lzg8gIonzvBqqJo8W3_4J">Example</a></td></tr>

<tr>
<td rowspan="2"><code>textStroke</code>
<td><code>textStrokeWidth</code></td>
<td rowspan="2"><code>WebkitTextStroke</code>
<td><code>WebkitTextStrokeWidth</code></td>
<td>Supported</td>
<td></td>
</tr>
<tr>
<td><code>textStrokeColor</code></td>
<td><code>WebkitTextStrokeColor</code></td>
<td>Supported</td>
<td></td>
</tr>
Expand Down
12 changes: 7 additions & 5 deletions src/builder/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,14 @@ export default function buildText(
transform: matrix || undefined,
'clip-path': clipPathId ? `url(#${clipPathId})` : undefined,
style: style.filter ? `filter:${style.filter}` : undefined,
'stroke-width': style.textStrokeWidth
? `${style.textStrokeWidth}px`
'stroke-width': style.WebkitTextStrokeWidth
? `${style.WebkitTextStrokeWidth}px`
: undefined,
stroke: style.textStrokeWidth ? style.textStrokeColor : undefined,
'stroke-linejoin': style.textStrokeWidth ? 'round' : undefined,
'paint-order': style.textStrokeWidth ? 'stroke' : undefined,
stroke: style.WebkitTextStrokeWidth
? style.WebkitTextStrokeColor
: undefined,
'stroke-linejoin': style.WebkitTextStrokeWidth ? 'round' : undefined,
'paint-order': style.WebkitTextStrokeWidth ? 'stroke' : undefined,
}
return [
(filter ? `${filter}<g filter="url(#satori_s-${id})">` : '') +
Expand Down
12 changes: 6 additions & 6 deletions src/handler/expand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,16 @@ function handleSpecialCase(
return result
}

if (name === 'textStroke') {
if (name === 'WebkitTextStroke') {
value = value.toString().trim()
const values = value.split(' ')
if (values.length !== 2) {
throw new Error('Invalid `textStroke` value.')
throw new Error('Invalid `WebkitTextStroke` value.')
}

return {
textStrokeWidth: purify(name, values[0]),
textStrokeColor: purify(name, values[1]),
WebkitTextStrokeWidth: purify(name, values[0]),
WebkitTextStrokeColor: purify(name, values[1]),
}
}

Expand Down Expand Up @@ -280,8 +280,8 @@ type MainStyle = {
}[]
textShadowColor: string[]
textShadowRadius: number[]
textStrokeWidth: number
textStrokeColor: string
WebkitTextStrokeWidth: number
WebkitTextStrokeColor: string
}

type OtherStyle = Exclude<Record<PropertyKey, string | number>, keyof MainStyle>
Expand Down
4 changes: 2 additions & 2 deletions src/handler/inheritable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const list = new Set([
'textShadowOffset',
'textShadowColor',
'textShadowRadius',
'textStrokeWidth',
'textStrokeColor',
'WebkitTextStrokeWidth',
'WebkitTextStrokeColor',
'textDecorationLine',
'textDecorationStyle',
'textDecorationColor',
Expand Down
12 changes: 6 additions & 6 deletions src/text/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -734,16 +734,16 @@ export default async function* buildTextNodes(
mask: overflowMaskId ? `url(#${overflowMaskId})` : undefined,

style: cssFilter ? `filter:${cssFilter}` : undefined,
'stroke-width': inheritedStyle.textStrokeWidth
? `${inheritedStyle.textStrokeWidth}px`
'stroke-width': inheritedStyle.WebkitTextStrokeWidth
? `${inheritedStyle.WebkitTextStrokeWidth}px`
: undefined,
stroke: inheritedStyle.textStrokeWidth
? inheritedStyle.textStrokeColor
stroke: inheritedStyle.WebkitTextStrokeWidth
? inheritedStyle.WebkitTextStrokeColor
: undefined,
'stroke-linejoin': inheritedStyle.textStrokeWidth
'stroke-linejoin': inheritedStyle.WebkitTextStrokeWidth
? 'round'
: undefined,
'paint-order': inheritedStyle.textStrokeWidth
'paint-order': inheritedStyle.WebkitTextStrokeWidth
? 'stroke'
: undefined,
})
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 83 additions & 0 deletions test/webkit-text-stroke.test.tsx
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()
})
})

0 comments on commit 37e40ee

Please sign in to comment.