fix: 修改RenderTextToImage函数以优化行高计算,确保单行文本有足够的垂直空间显示字符底部

This commit is contained in:
lixiangwuxian 2025-05-16 01:38:31 +08:00
parent 82e3548628
commit 6585d9dd66

View File

@ -667,7 +667,7 @@ func RenderTextToImage(fontContent *truetype.Font, text string, fontSize float64
// 计算总高度(每行高度+行间距) // 计算总高度(每行高度+行间距)
totalHeight := 0 totalHeight := 0
for _, height := range lineHeights { for _, height := range lineHeights {
totalHeight += height + lineSpacing + 1 totalHeight += height + lineSpacing + height/12
} }
// 减去最后一行多加的行间距 // 减去最后一行多加的行间距
if len(lineHeights) > 0 { if len(lineHeights) > 0 {
@ -679,6 +679,13 @@ func RenderTextToImage(fontContent *truetype.Font, text string, fontSize float64
totalHeight = int(fontSize) totalHeight = int(fontSize)
} }
// 单行文本情况下,确保有足够的空间容纳文本底部
if len(lines) == 1 && len(lines[0]) > 0 {
// 获取最低的descender值确保有足够的垂直空间显示字符底部
descenderSpace := int(fontSize / 4)
totalHeight += descenderSpace
}
// 创建图像 // 创建图像
rgba := image.NewRGBA(image.Rect(0, 0, maxWidth, totalHeight)) rgba := image.NewRGBA(image.Rect(0, 0, maxWidth, totalHeight))
draw.Draw(rgba, rgba.Bounds(), image.Transparent, image.Point{}, draw.Src) draw.Draw(rgba, rgba.Bounds(), image.Transparent, image.Point{}, draw.Src)