Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiang-Red committed Jan 11, 2023
1 parent 7f38e55 commit 1461f38
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
12 changes: 9 additions & 3 deletions title.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
39 changes: 39 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rendercard
import (
"image"
"image/color"
"strings"

"github.com/Coloured-glaze/gg"
)
Expand Down Expand Up @@ -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
}

0 comments on commit 1461f38

Please sign in to comment.