From 6585d9dd6665631af9ed2d8858419917f9e64eaf Mon Sep 17 00:00:00 2001 From: lixiangwuxian Date: Fri, 16 May 2025 01:38:31 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9RenderTextToImage?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E4=BB=A5=E4=BC=98=E5=8C=96=E8=A1=8C=E9=AB=98?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=EF=BC=8C=E7=A1=AE=E4=BF=9D=E5=8D=95=E8=A1=8C?= =?UTF-8?q?=E6=96=87=E6=9C=AC=E6=9C=89=E8=B6=B3=E5=A4=9F=E7=9A=84=E5=9E=82?= =?UTF-8?q?=E7=9B=B4=E7=A9=BA=E9=97=B4=E6=98=BE=E7=A4=BA=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E5=BA=95=E9=83=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- text2img/parse_font.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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)