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 dc8dbf9 commit 5f8d9a2
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 71 deletions.
Binary file modified .github/DrawCard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .github/DrawTitle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .github/DrawTitleWithText.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<img src=".github/warma.png" alt="沃玛" width = "300">
</a><br>
<h1>RenderCard</h1>
图标是微博<a href = "https://weibo.com/u/1782488734">_warma_</a>评论区一位微博用户下所画的

~~时间过于久远已经找不到原作者了~~
</div>

> 示例代码详见各`test`
Expand Down
4 changes: 4 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ type Title struct {
TitleFont string
// TextFont 正文字体路径
TextFont string
// OffsetX 文字X坐标偏移 向右为正方向
OffsetX float64
// OffsetX 文字Y坐标偏移 向下为正方向
OffsetY float64
}

// Alignment 对齐规则
Expand Down
98 changes: 37 additions & 61 deletions title.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,28 @@ func (t *Title) DrawTitle() (imgs image.Image, err error) {
canvas.SetRGBA255(0, 0, 0, 153)
canvas.Fill()

fontsize1, fontsize2 := 108.0, 54.0
// 加载size为108的字体
err = canvas.LoadFontFace(t.TitleFont, 108)
err = canvas.LoadFontFace(t.TitleFont, fontsize1)
if err != nil {
return
}

// 绘制标题
canvas.SetRGBA255(250, 250, 250, 255)
canvas.DrawString(t.LeftTitle, 25, 30+40+55+canvas.FontHeight()-canvas.FontHeight()/3)
stringwight, _ := canvas.MeasureString(t.LeftTitle)
canvas.DrawStringAnchored(t.LeftTitle, (220-(fontsize1+fontsize2)*72/96)*0.33+stringwight/2+t.OffsetX, 30+40+(220-(fontsize1+fontsize2)*72/96)*0.33+fontsize1*72/96*0.5+t.OffsetY, 0.5, 0.5)

// 加载size为54的字体
err = canvas.LoadFontFace(t.TitleFont, 54)
err = canvas.LoadFontFace(t.TextFont, fontsize2)
if err != nil {
return
}

canvas.SetRGBA255(250, 250, 250, 255)
// 绘制副标题
canvas.DrawString(t.LeftSubtitle, 25+3, 30+40+165+canvas.FontHeight()/3)
stringwight, _ = canvas.MeasureString(t.LeftSubtitle)
canvas.DrawStringAnchored(t.LeftSubtitle, 3+(220-(fontsize1+fontsize2)*72/96)*0.33+stringwight/2+t.OffsetX, 30+40+(220-(fontsize1+fontsize2)*72/96)*0.66+fontsize1*72/96+fontsize2*72/96*0.5+t.OffsetY, 0.5, 0.5)

// 加载icon并绘制
var icon *img.Factory
Expand All @@ -51,15 +54,16 @@ func (t *Title) DrawTitle() (imgs image.Image, err error) {
return
}
canvas.DrawImage(icon.Im, int(DefaultWidth)-icon.W, 40+30)
// 加载size为54的字体
err = canvas.LoadFontFace(t.TitleFont, 72)
// 加载size为72的字体
fontsize1 = 72
err = canvas.LoadFontFace(t.TextFont, fontsize1)
if err != nil {
return
}
fw, _ := canvas.MeasureString(t.RightTitle)
canvas.DrawString(t.RightTitle, DefaultWidth-25-fw-float64(icon.W), 30+40+15+canvas.FontHeight()*1.25)
fw1, _ := canvas.MeasureString(t.RightSubtitle)
canvas.DrawString(t.RightSubtitle, DefaultWidth-25-fw1-float64(icon.W), 30+40+15+canvas.FontHeight()*2.75)
stringwight, _ = canvas.MeasureString(t.RightTitle)
canvas.DrawStringAnchored(t.RightTitle, DefaultWidth-25-float64(icon.W)-stringwight/2+t.OffsetX, 30+40+(220-fontsize1*72/96*2)*0.33+fontsize1*72/96*0.5+t.OffsetY, 0.5, 0.5)
stringwight, _ = canvas.MeasureString(t.RightSubtitle)
canvas.DrawStringAnchored(t.RightSubtitle, DefaultWidth-25-float64(icon.W)-stringwight/2+t.OffsetX, 30+40+(220-fontsize1*72/96*2)*0.66+fontsize1*72/96*1.5+t.OffsetY, 0.5, 0.5)

imgs = canvas.Image()
return
Expand All @@ -80,39 +84,35 @@ func (t *Title) DrawTitleWithText(info []string) (imgs image.Image, err error) {

// 加载icon
var icon *img.Factory
icon, err = img.LoadFirstFrame(t.ImagePath, 768, 768)
icon, err = img.LoadFirstFrame(t.ImagePath, 512, 512)
if err != nil {
return
}
canvas.DrawImage(icon.Im, DefaultWidth-icon.W, imgh-icon.H)

// 绘制标题与内容的分割线
/*canvas.DrawRectangle(0, 220, Imgwight, 10)
canvas.SetRGBA255(240, 240, 240, 255)
canvas.Fill()*/

// 加载size为108的字体
err = canvas.LoadFontFace(t.TitleFont, 108)
fontsize1, fontsize2 := 108.0, 54.0
err = canvas.LoadFontFace(t.TitleFont, fontsize1)
if err != nil {
return
}

canvas.SetRGBA255(15, 15, 15, 255)

// 绘制标题
titley := 35 + canvas.FontHeight()*0.66
canvas.DrawString(t.LeftTitle, 25, titley)
stringwight, _ := canvas.MeasureString(t.LeftTitle)
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, 54)
err = canvas.LoadFontFace(t.TextFont, fontsize2)
if err != nil {
return
}

// 绘制一系列标题
canvas.DrawString(t.LeftSubtitle, 25+3, titley+canvas.FontHeight()*1.6)
stringwight, _ = canvas.MeasureString(t.LeftSubtitle)
canvas.DrawStringAnchored(t.LeftSubtitle, 25+3+stringwight/2+t.OffsetX, 25+fontsize1*72/96+25+fontsize2*72/96*0.5+t.OffsetY, 0.5, 0.5)

lefttitlewight, _ := canvas.MeasureString(t.LeftSubtitle)
canvas.DrawRectangle(25, titley+canvas.FontHeight()*1.85, lefttitlewight, 6)
canvas.DrawRectangle(25+3+t.OffsetX, 25+fontsize1*72/96+25+fontsize2*72/96+5+t.OffsetY, stringwight, 6)
// 绘制插件开启状态
if t.IsEnabled {
canvas.SetRGBA255(35, 235, 35, 255)
Expand All @@ -122,20 +122,20 @@ func (t *Title) DrawTitleWithText(info []string) (imgs image.Image, err error) {
canvas.Fill()
canvas.SetRGBA255(15, 15, 15, 255)

fw, _ := canvas.MeasureString(t.RightTitle)
canvas.DrawString(t.RightTitle, DefaultWidth-40-fw, 30+canvas.FontHeight()*1.25)
fw1, _ := canvas.MeasureString(t.RightSubtitle)
canvas.DrawString(t.RightSubtitle, DefaultWidth-40-fw1, 30+canvas.FontHeight()*2.5)
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.TextFont, 38)
err = canvas.LoadFontFace(t.TitleFont, 38)
if err != nil {
return
}

y := titley
y := 25 + fontsize1*72/96 + 25 + fontsize2*72/96
for _, text := range info {
canvas.DrawString(text, 25.0, 1.5*titley+y+canvas.FontHeight())
canvas.DrawString(text, 25.0, y+canvas.FontHeight()*2)
y += 20 + canvas.FontHeight()
}
imgs = canvas.Image()
Expand All @@ -156,54 +156,30 @@ func (t *Title) DrawCard() (imgs image.Image, err error) {
canvas.SetRGB255(RandJPColor())
canvas.Fill()
}

// 绘制遮罩
/*canvas.DrawRectangle(0, rech/3*2, recw, rech/3)
canvas.SetRGBA255(0, 0, 0, 153)
canvas.Fill()*/

// 绘制排名
/*canvas.DrawRectangle(recw/10, 0, recw/10, (rech/4)-10)
canvas.DrawRoundedRectangle(recw/10, 0, recw/10, (rech / 4), 8)*/
if t.IsEnabled {
canvas.DrawRectangle(0, rech*0.54, recw, rech-rech*0.54)
// canvas.SetRGBA255(15, 175, 15, 255)
} else {
canvas.DrawRectangle(0, 0, recw, rech)
// canvas.SetRGBA255(200, 15, 15, 255)
}
canvas.SetRGBA255(0, 0, 0, 183)
canvas.Fill()

// 绘制插件排名
/*canvas.SetRGBA255(240, 240, 240, 255)
var fw2 float64
i, _ := strconv.Atoi(t.Rightsubtitle)
if i > 99 {
err = canvas.LoadFontFace(t.Fontpath, 24)
} else {
err = canvas.LoadFontFace(t.Fontpath, 28)
}
if err != nil {
return
}
fw2, _ = canvas.MeasureString(t.Rightsubtitle)
canvas.DrawString(t.Rightsubtitle, recw/10+((recw/10-fw2)/2), canvas.FontHeight()*3/8+(rech/8))*/

// 绘制插件信息
canvas.SetRGBA255(240, 240, 240, 255)
err = canvas.LoadFontFace(t.TitleFont, 64)
fontsize1, fontsize2 := 64.0, 32.0
err = canvas.LoadFontFace(t.TitleFont, fontsize1)
if err != nil {
return
}
y := (rech * 0.56) + canvas.FontHeight()*0.95
canvas.DrawString(t.LeftTitle, recw*0.04, y)
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, 32)
err = canvas.LoadFontFace(t.TextFont, fontsize2)
if err != nil {
return
}
canvas.DrawString(t.LeftSubtitle, recw*0.04, y+canvas.FontHeight()*1.85)
stringwight, _ = canvas.MeasureString(t.LeftSubtitle)
canvas.DrawStringAnchored(t.LeftSubtitle, 3+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.66+fontsize1*72/96+fontsize2*72/96*0.5+t.OffsetY, 0.5, 0.5)

imgs = Fillet(canvas.Image(), 16)
return
Expand Down
19 changes: 10 additions & 9 deletions title_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (
func TestDrawTitle(t *testing.T) {
img, err := (&Title{
Line: 0,
LeftTitle: "FUWULIEBIAO",
LeftTitle: "服务列表",
LeftSubtitle: "service_list",
RightTitle: "FloatTech",
RightSubtitle: "ZeroBot-Plugin",
TitleFont: "Impact.ttf",
TitleFont: "GlowSansSC-Normal-ExtraBold.ttf",
TextFont: "Impact.ttf",
ImagePath: ".github/warma.png",
}).DrawTitle()
if err != nil {
Expand All @@ -32,18 +33,18 @@ func TestDrawTitle(t *testing.T) {
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "7a7ccec658b1acaf1a018d47357b1f62", hex.EncodeToString(h.Sum(nil)))
assert.Equal(t, "28f9f68f3ef7987a9d189b52b9a8b399", hex.EncodeToString(h.Sum(nil)))
}

func TestDrawTitleWithText(t *testing.T) {
img, err := (&Title{
Line: 0,
IsEnabled: true,
LeftTitle: "NAME",
LeftTitle: "服务名",
LeftSubtitle: "instruction",
RightTitle: "FloatTech",
RightSubtitle: "ZeroBot-Plugin",
TitleFont: "Impact.ttf",
TitleFont: "GlowSansSC-Normal-ExtraBold.ttf",
TextFont: "Impact.ttf",
ImagePath: ".github/warma.png",
}).DrawTitleWithText([]string{"one", "two", "san", "si"})
Expand All @@ -59,16 +60,16 @@ func TestDrawTitleWithText(t *testing.T) {
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "fdff61acd070ab6806cb587f5fad527c", hex.EncodeToString(h.Sum(nil)))
assert.Equal(t, "3f18e919e69e38b2302d8b2fc31c4cb2", hex.EncodeToString(h.Sum(nil)))
}

func TestDrawCard(t *testing.T) {
img, err := (&Title{
Line: 0,
IsEnabled: true,
LeftTitle: "NAME",
LeftTitle: "服务名",
LeftSubtitle: "instruction",
TitleFont: "Impact.ttf",
TitleFont: "GlowSansSC-Normal-ExtraBold.ttf",
TextFont: "Impact.ttf",
ImagePath: ".github/warma.png",
}).DrawCard()
Expand All @@ -84,5 +85,5 @@ func TestDrawCard(t *testing.T) {
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "84d9e5f39b921c3866785c9ae39f74d7", hex.EncodeToString(h.Sum(nil)))
assert.Equal(t, "596186f97c3a4126a9d3a7b30b959810", hex.EncodeToString(h.Sum(nil)))
}
1 change: 0 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func Fillet(dst image.Image, r int) image.Image {
dstr.Set(mx-1-x, my-1-y, color.NRGBA{})
}
}

}
}
return dstr
Expand Down

0 comments on commit 5f8d9a2

Please sign in to comment.