diff --git a/text2img/parse_font.go b/text2img/parse_font.go index f514252..79bd7a4 100644 --- a/text2img/parse_font.go +++ b/text2img/parse_font.go @@ -667,7 +667,7 @@ func RenderTextToImage(fontContent *truetype.Font, text string, fontSize float64 // 计算总高度(每行高度+行间距) totalHeight := 0 for _, height := range lineHeights { - totalHeight += height + lineSpacing + 1 + totalHeight += height + lineSpacing + height/12 } // 减去最后一行多加的行间距 if len(lineHeights) > 0 { @@ -679,6 +679,13 @@ func RenderTextToImage(fontContent *truetype.Font, text string, fontSize float64 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)) draw.Draw(rgba, rgba.Bounds(), image.Transparent, image.Point{}, draw.Src)