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: text-align #527

Merged
merged 1 commit into from
Aug 11, 2023
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
11 changes: 7 additions & 4 deletions src/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,12 @@ export default async function* buildTextNodes(
// @TODO: Support different writing modes.
// @TODO: Support RTL languages.
let i = 0
let prevLineEndingSpacesWidth = 0
while (i < words.length && lines < lineLimit) {
let word = words[i]
const forceBreak = requiredBreaks[i]

let w = 0
let lineEndingSpacesWidth = 0

const {
originWidth,
Expand All @@ -243,7 +243,7 @@ export default async function* buildTextNodes(
word = _word

w = originWidth
lineEndingSpacesWidth = endingSpacesWidth
const lineEndingSpacesWidth = endingSpacesWidth

// When starting a new line from an empty line, we should push one extra
// line height.
Expand Down Expand Up @@ -281,7 +281,7 @@ export default async function* buildTextNodes(
words.splice(i, 1, ...chars)
if (currentWidth > 0) {
// Start a new line, spaces can be ignored.
lineWidths.push(currentWidth)
lineWidths.push(currentWidth - prevLineEndingSpacesWidth)
baselines.push(currentBaselineOffset)
lines++
height += currentLineHeight
Expand All @@ -291,6 +291,7 @@ export default async function* buildTextNodes(
lineSegmentNumber.push(1)
lineIndex = -1
}
prevLineEndingSpacesWidth = lineEndingSpacesWidth
continue
}
if (forceBreak || willWrap) {
Expand All @@ -299,7 +300,8 @@ export default async function* buildTextNodes(
if (shouldCollapseTabsAndSpaces && word === ' ') {
w = 0
}
lineWidths.push(currentWidth)

lineWidths.push(currentWidth - prevLineEndingSpacesWidth)
baselines.push(currentBaselineOffset)
lines++
height += currentLineHeight
Expand Down Expand Up @@ -376,6 +378,7 @@ export default async function* buildTextNodes(
}

i++
prevLineEndingSpacesWidth = lineEndingSpacesWidth
}

if (currentWidth) {
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.
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.
2 changes: 2 additions & 0 deletions test/line-clamp.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ describe('Line Clamp', () => {
fontSize: 32,
textAlign: 'center',
lineClamp: 2,
backgroundColor: '#ff6c2f',
color: 'white',
}}
>
Making the Web. Superfast
Expand Down
149 changes: 149 additions & 0 deletions test/text-align.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { it, describe, expect } from 'vitest'

import { initFonts, toImage } from './utils.js'
import satori from '../src/index.js'

describe('Text Align', () => {
let fonts
initFonts((f) => (fonts = f))

it('Should work correctly when `text-align: left`', async () => {
const svg = await satori(
<div
style={{
height: '100%',
width: '100%',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#fff',
fontSize: 20,
fontWeight: 600,
}}
>
<div
style={{
display: 'flex',
maxWidth: '190px',
textAlign: 'left',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#ff6c2f',
color: 'white',
letterSpacing: '1px',
}}
>
God kisses the finite in his love and man the infinite
</div>
</div>,
{ width: 200, height: 200, fonts, embedFont: true }
)
expect(toImage(svg, 200)).toMatchImageSnapshot()
})

it('Should work correctly when `text-align: center`', async () => {
const svg = await satori(
<div
style={{
height: '100%',
width: '100%',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#fff',
fontSize: 20,
fontWeight: 600,
}}
>
<div
style={{
display: 'flex',
maxWidth: '190px',
textAlign: 'center',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#ff6c2f',
color: 'white',
letterSpacing: '1px',
}}
>
God kisses the finite in his love and man the infinite
</div>
</div>,
{ width: 200, height: 200, fonts, embedFont: true }
)
expect(toImage(svg, 200)).toMatchImageSnapshot()
})

it('Should work correctly when `text-align: right`', async () => {
const svg = await satori(
<div
style={{
height: '100%',
width: '100%',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#fff',
fontSize: 20,
fontWeight: 600,
}}
>
<div
style={{
display: 'flex',
maxWidth: '190px',
textAlign: 'right',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#ff6c2f',
color: 'white',
letterSpacing: '1px',
}}
>
God kisses the finite in his love and man the infinite
</div>
</div>,
{ width: 200, height: 200, fonts, embedFont: true }
)
expect(toImage(svg, 200)).toMatchImageSnapshot()
})

it('Should work correctly when `text-align: end`', async () => {
const svg = await satori(
<div
style={{
height: '100%',
width: '100%',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#fff',
fontSize: 20,
fontWeight: 600,
}}
>
<div
style={{
display: 'flex',
maxWidth: '190px',
textAlign: 'end',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#ff6c2f',
color: 'white',
letterSpacing: '1px',
}}
>
God kisses the finite in his love and man the infinite
</div>
</div>,
{ width: 200, height: 200, fonts, embedFont: true }
)
expect(toImage(svg, 200)).toMatchImageSnapshot()
})
})