Skip to content

Commit

Permalink
fix: og cjk char width calc
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed May 1, 2024
1 parent 33f0669 commit 3f193a4
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/app/(app)/og/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,20 +199,21 @@ export const GET = async (req: NextRequest) => {
let canShownTitle = ''

let leftContainerWidth = 1200 - 80 * 2
const cjkWidth = 64
for (let i = 0; i < title.length; i++) {
if (leftContainerWidth < 0) break
// cjk 字符算 64 px
const char = title[i]
// char 不能是 emoji
if ((char >= '\u4e00' && char <= '\u9fa5') || char === ' ') {
leftContainerWidth -= 64
leftContainerWidth -= cjkWidth
canShownTitle += char
} else if (char >= '\u0000' && char <= '\u00ff') {
// latin 字符算 40px
leftContainerWidth -= 40
canShownTitle += char
} else {
leftContainerWidth -= 64
leftContainerWidth -= cjkWidth
canShownTitle += char
}
}
Expand Down Expand Up @@ -278,7 +279,7 @@ export const GET = async (req: NextRequest) => {
style={{
color: 'rgba(255, 255, 255, 0.92)',

fontSize: `${(canShownTitle.length / title.length) * 64}px`,
fontSize: `${(canShownTitle.length / title.length) * (cjkWidth - 2)}px`,
whiteSpace: 'nowrap',
textOverflow: 'ellipsis',
WebkitLineClamp: 1,
Expand Down

0 comments on commit 3f193a4

Please sign in to comment.