Skip to content

Commit 3f193a4

Browse files
committed
fix: og cjk char width calc
Signed-off-by: Innei <[email protected]>
1 parent 33f0669 commit 3f193a4

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/app/(app)/og/route.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -199,20 +199,21 @@ export const GET = async (req: NextRequest) => {
199199
let canShownTitle = ''
200200

201201
let leftContainerWidth = 1200 - 80 * 2
202+
const cjkWidth = 64
202203
for (let i = 0; i < title.length; i++) {
203204
if (leftContainerWidth < 0) break
204205
// cjk 字符算 64 px
205206
const char = title[i]
206207
// char 不能是 emoji
207208
if ((char >= '\u4e00' && char <= '\u9fa5') || char === ' ') {
208-
leftContainerWidth -= 64
209+
leftContainerWidth -= cjkWidth
209210
canShownTitle += char
210211
} else if (char >= '\u0000' && char <= '\u00ff') {
211212
// latin 字符算 40px
212213
leftContainerWidth -= 40
213214
canShownTitle += char
214215
} else {
215-
leftContainerWidth -= 64
216+
leftContainerWidth -= cjkWidth
216217
canShownTitle += char
217218
}
218219
}
@@ -278,7 +279,7 @@ export const GET = async (req: NextRequest) => {
278279
style={{
279280
color: 'rgba(255, 255, 255, 0.92)',
280281

281-
fontSize: `${(canShownTitle.length / title.length) * 64}px`,
282+
fontSize: `${(canShownTitle.length / title.length) * (cjkWidth - 2)}px`,
282283
whiteSpace: 'nowrap',
283284
textOverflow: 'ellipsis',
284285
WebkitLineClamp: 1,

0 commit comments

Comments
 (0)