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 56b6cb7 commit 7f38e55
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 50 deletions.
9 changes: 2 additions & 7 deletions title.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,6 @@ func (t *Title) DrawTitleWithText(info []string) (imgs image.Image, err error) {
canvas.Fill()
canvas.SetRGBA255(15, 15, 15, 255)

err = canvas.LoadFontFace(t.TextFont, 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)
Expand Down Expand Up @@ -173,14 +168,14 @@ func (t *Title) DrawCard() (imgs image.Image, err error) {
// 绘制插件信息
canvas.SetRGBA255(240, 240, 240, 255)
fontsize1, fontsize2 := 64.0, 32.0
err = canvas.LoadFontFace(t.TextFont, fontsize1)
err = canvas.LoadFontFace(t.TitleFont, fontsize1)
if err != nil {
return
}
stringwight, _ := canvas.MeasureString(t.LeftTitle)
canvas.DrawStringAnchored(t.LeftTitle, stringwight/2+(rech-rech*0.54-(fontsize1+fontsize2)*72/96)*0.33+t.OffsetX, rech*0.54+(rech-rech*0.54-(fontsize1+fontsize2)*72/96)*0.33+fontsize1*72/96*0.5+t.OffsetY, 0.5, 0.5)

err = canvas.LoadFontFace(t.TitleFont, fontsize2)
err = canvas.LoadFontFace(t.TextFont, fontsize2)
if err != nil {
return
}
Expand Down
8 changes: 4 additions & 4 deletions title_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func TestDrawTitleWithText(t *testing.T) {
LeftSubtitle: "简介",
RightTitle: "FloatTech",
RightSubtitle: "ZeroBot-Plugin",
TitleFont: "GlowSansSC-Normal-ExtraBold.ttf",
TextFont: "Impact.ttf",
TitleFont: "Impact.ttf",
TextFont: "GlowSansSC-Normal-ExtraBold.ttf",
ImagePath: ".github/warma.png",
}).DrawTitleWithText([]string{"one", "two", "san", "si"})
if err != nil {
Expand All @@ -69,8 +69,8 @@ func TestDrawCard(t *testing.T) {
IsEnabled: true,
LeftTitle: "ServiceName",
LeftSubtitle: "简介",
TitleFont: "GlowSansSC-Normal-ExtraBold.ttf",
TextFont: "Impact.ttf",
TitleFont: "Impact.ttf",
TextFont: "GlowSansSC-Normal-ExtraBold.ttf",
ImagePath: ".github/warma.png",
}).DrawCard()
if err != nil {
Expand Down
39 changes: 0 additions & 39 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package rendercard
import (
"image"
"image/color"
"strings"

"github.com/Coloured-glaze/gg"
)
Expand Down Expand Up @@ -41,41 +40,3 @@ 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 7f38e55

Please sign in to comment.