Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to add text stroke #645

Merged
merged 6 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,18 @@ Satori uses the same Flexbox [layout engine](https://yogalayout.com) as React Na
<tr><td><code>maskSize</code></td><td>Support two-value size i.e. `10px 20%`</td><td><a href="https://og-playground.vercel.app/?share=pVLfb9MwEP5XLEvLhpQ2P0a3LlpAAiYxJEATk_rSF8e-JNc6drAd2lD1f8duV8H6yoN19ved7j7ffTvKtQBa0HuBv5aKEOtGCeVuF-6EtIBN6wpymaXpxWV8BDcoXHuGCbS9ZKNHawnbExrun9AAd6iV57iWQ6dOLJPYqEcHnQ0UKAfmRK0G67AeP2oPqtD_NV17_Af-hoJc5_9Aixe1N2n6glaMrxujByV8jcHIq9a53hZJgh1rwE4HFWTbdsp1l_StdnqSzfJ5Pr-9e5tnt9mkruD6ZiYyccf4e9xKrEpTTbJpPs2in-V8FtVdueqbiBvdl16jD2O0KbM8TSNuS2uaKsItihLGLy3__KFmiyf8vnpIvz03s_rpzelHHbPrx6DJ6zRMIJOTJkRf8oqj4RIIc2SWXoQTk0oOEBNnmNfPjE96Veg4Gr-ffkvyvztaQLVG9_X_O5EkOWzID90QAzV4nANBRVrXyfNm52oCv98v1buluk-863ykMdV98IilxY4e_EWLMMOYHh1Ii7BTKqAaGlrUTFqIKXR6hc9jH-zrNoeXLxSM8NBVIGjhzAD7mDpW-YwWpNQbbaSg-z8">Example</a></td></tr>
<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_eD9ygfjifEVTf2-gIVvd5TUO8-CzYC3l8rNWJOo750J-cHBfRKqB6rMf4t2-1mDsPCiN5Bn_uhk15t3umJGT79h9JPCQJ_tP9oSM_YfcP-rGwFrAPViZIUAbiH2v97P-p81BsHcJDc-ZWvGSwfHWkRZUm-8UDuWsMsJM7ITXvl8YYxpVaWIcDFuQMp-lDYZXUyUzNVLX4KYTMBgGNxwFaEUoF84QWe6mXMYz13GVeU91ISF0sPF8oDNpbYtxHVWOmSxRzOXLyHCzEenHcHk5as7liJUdRl2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jcj7-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>Supported</td>
<td></td>
</tr>
<tr>
<td><code>textStrokeColor</code></td>
<td>Supported</td>
<td></td>
</tr>

</tbody>
</table>

Expand Down
6 changes: 6 additions & 0 deletions src/builder/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ 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`
: undefined,
stroke: style.textStrokeWidth ? style.textStrokeColor : undefined,
'stroke-linejoin': style.textStrokeWidth ? 'round' : undefined,
'paint-order': style.textStrokeWidth ? 'stroke' : undefined,
}
return [
(filter ? `${filter}<g filter="url(#satori_s-${id})">` : '') +
Expand Down
15 changes: 15 additions & 0 deletions src/handler/expand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,19 @@ function handleSpecialCase(
return result
}

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

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

return
}

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

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

style: cssFilter ? `filter:${cssFilter}` : undefined,
'stroke-width': inheritedStyle.textStrokeWidth
? `${inheritedStyle.textStrokeWidth}px`
: undefined,
stroke: inheritedStyle.textStrokeWidth
? inheritedStyle.textStrokeColor
: undefined,
'stroke-linejoin': inheritedStyle.textStrokeWidth
? 'round'
: undefined,
'paint-order': inheritedStyle.textStrokeWidth
? 'stroke'
: undefined,
})
: ''

Expand Down
Loading