From 1461f38cccba7a0918c84c79c5ad2befb7dd54fd Mon Sep 17 00:00:00 2001 From: Jiang-Red <1343396474@qq.com> Date: Wed, 11 Jan 2023 23:06:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- title.go | 12 +++++++++--- utils.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/title.go b/title.go index a2c7cc7..c35c145 100644 --- a/title.go +++ b/title.go @@ -92,7 +92,7 @@ func (t *Title) DrawTitleWithText(info []string) (imgs image.Image, err error) { // 加载size为108的字体 fontsize1, fontsize2 := 108.0, 54.0 - err = canvas.LoadFontFace(t.TextFont, fontsize1) + err = canvas.LoadFontFace(t.TitleFont, fontsize1) if err != nil { return } @@ -104,7 +104,7 @@ func (t *Title) DrawTitleWithText(info []string) (imgs image.Image, err error) { canvas.DrawStringAnchored(t.LeftTitle, 25+stringwight/2+t.OffsetX, 25+fontsize1*72/96*0.5+t.OffsetY, 0.5, 0.5) // 加载size为54的字体 - err = canvas.LoadFontFace(t.TitleFont, fontsize2) + err = canvas.LoadFontFace(t.TextFont, fontsize2) if err != nil { return } @@ -123,13 +123,19 @@ func (t *Title) DrawTitleWithText(info []string) (imgs image.Image, err error) { canvas.Fill() canvas.SetRGBA255(15, 15, 15, 255) + // 加载size为54的字体 + err = canvas.LoadFontFace(t.TitleFont, fontsize2) + if err != nil { + return + } + stringwight, _ = canvas.MeasureString(t.RightTitle) canvas.DrawStringAnchored(t.RightTitle, DefaultWidth-40-stringwight/2+t.OffsetX, 40+fontsize2*72/96*0.5+t.OffsetY, 0.5, 0.5) stringwight, _ = canvas.MeasureString(t.RightSubtitle) canvas.DrawStringAnchored(t.RightSubtitle, DefaultWidth-40-stringwight/2+t.OffsetX, 40+25+fontsize2*72/96*1.5+t.OffsetY, 0.5, 0.5) // 加载size为38的字体 - err = canvas.LoadFontFace(t.TitleFont, 38) + err = canvas.LoadFontFace(t.TextFont, 38) if err != nil { return } diff --git a/utils.go b/utils.go index 29db211..4efb0ff 100644 --- a/utils.go +++ b/utils.go @@ -3,6 +3,7 @@ package rendercard import ( "image" "image/color" + "strings" "github.com/Coloured-glaze/gg" ) @@ -40,3 +41,41 @@ func Transparency(dst image.Image, magnification float64) image.Image { } return dstr } + +// Truncate 截断文字 +func Truncate(fontfile string, texts []string, maxW, fontsize float64) ([]string, error) { + one := gg.NewContext(1, 1) + err := one.LoadFontFace(fontfile, fontsize) + if err != nil { + return nil, err + } + newtexts := make([]string, 0, len(texts)*2) + for i := 0; i < len(texts); i++ { + newlinetext, textw, tmpw := "", 0.0, 0.0 + text := texts[i] + for len(texts[i]) > 0 { + var tmp strings.Builder + tmp.Grow(len(text)) + res := make([]rune, 0, len(text)) + for _, r := range text { + tmp.WriteRune(r) + width, _ := one.MeasureString(tmp.String()) // 获取文字宽度 + if width > maxW { // 如果宽度大于文字边距 + break // 跳出 + } else { + res = append(res, r) // 写入 + } + newlinetext = string(res) + } + newtexts = append(newtexts, newlinetext) + if tmpw > textw { + textw = tmpw + } + if len(newlinetext) >= len(texts[i]) { + break + } + texts[i] = texts[i][len(newlinetext):] + } + } + return newtexts, nil +}