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

fix: textDecoration should compatible with transform #640

Merged
merged 1 commit into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 17 additions & 11 deletions src/builder/text-decoration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ export default function buildDecoration(
top,
ascender,
clipPathId,
matrix,
}: {
width: number
left: number
top: number
ascender: number
clipPathId?: string
matrix?: string
},
style: Record<string, any>
) {
Expand Down Expand Up @@ -43,15 +45,19 @@ export default function buildDecoration(
? `0 ${height * 2}`
: undefined

return buildXMLString('line', {
x1: left,
y1: y,
x2: left + width,
y2: y,
stroke: textDecorationColor || color,
'stroke-width': height,
'stroke-dasharray': dasharray,
'stroke-linecap': textDecorationStyle === 'dotted' ? 'round' : 'square',
'clip-path': clipPathId ? `url(#${clipPathId})` : undefined,
})
return (
(clipPathId ? `<g clip-path="url(#${clipPathId})">` : '') +
buildXMLString('line', {
x1: left,
y1: y,
x2: left + width,
y2: y,
stroke: textDecorationColor || color,
'stroke-width': height,
'stroke-dasharray': dasharray,
'stroke-linecap': textDecorationStyle === 'dotted' ? 'round' : 'square',
transform: matrix,
}) +
(clipPathId ? '</g>' : '')
)
}
1 change: 1 addition & 0 deletions src/text/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ export default async function* buildTextNodes(
width: deco[3],
ascender: deco[2],
clipPathId,
matrix,
},
parentStyle
)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions test/text-decoration.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,39 @@ describe('Text Decoration', () => {
)
expect(toImage(svg, 200)).toMatchImageSnapshot()
})

it('Should work correctly with `text-decoration` and `transform`', async () => {
const svg = await satori(
<div
style={{
height: '100%',
width: '100%',
display: 'flex',
padding: 10,
backgroundColor: '#fff',
fontSize: 32,
}}
>
<div
style={{
display: 'flex',
transform: 'translate(5px, 5px)',
padding: 10,
textDecoration: 'underline',
}}
>
lynn
</div>
</div>,
{
width: 100,
height: 100,
fonts,
loadAdditionalAsset: (languageCode: string, segment: string) => {
return loadDynamicAsset(languageCode, segment) as any
},
}
)
expect(toImage(svg, 100)).toMatchImageSnapshot()
})
})
Loading