Skip to content

Commit 9f0f349

Browse files
committed
自适应比例
1 parent ff07458 commit 9f0f349

File tree

6 files changed

+57
-59
lines changed

6 files changed

+57
-59
lines changed

.github/DrawCard.png

-3.26 KB
Loading

.github/DrawTitle.png

-4.07 KB
Loading

.github/DrawTitleWithText.png

-8.83 KB
Loading

title.go

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"image"
55

66
"github.com/FloatTech/gg"
7-
"github.com/FloatTech/imgfactory"
87
)
98

109
// DrawTitle 绘制标题
@@ -24,7 +23,7 @@ func (t *Title) DrawTitle() (imgs image.Image, err error) {
2423
canvas.SetRGBA255(0, 0, 0, 153)
2524
canvas.Fill()
2625

27-
fontsize1, fontsize2 := 108.0+t.TitleFontOffsetPoint, 54.0+t.TextFontOffsetPoint
26+
fontsize1, fontsize2, fontsize3 := 108.0+t.TitleFontOffsetPoint, 72+t.TextFontOffsetPoint, 54.0+t.TextFontOffsetPoint
2827
// 加载size为108的字体
2928
err = canvas.ParseFontFace(t.TitleFontData, fontsize1)
3029
if err != nil {
@@ -33,37 +32,35 @@ func (t *Title) DrawTitle() (imgs image.Image, err error) {
3332

3433
// 绘制标题
3534
canvas.SetRGBA255(250, 250, 250, 255)
36-
stringwight, _ := canvas.MeasureString(t.LeftTitle)
37-
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)
35+
canvas.DrawStringAnchored(t.LeftTitle, (220-fontsize1-fontsize3)/3+t.OffsetX, 30+40+(220-fontsize1-fontsize3)/3+fontsize1/2+t.OffsetY, 0, 0.5)
3836

3937
// 加载size为54的字体
40-
err = canvas.ParseFontFace(t.TextFontData, fontsize2)
38+
err = canvas.ParseFontFace(t.TextFontData, fontsize3)
4139
if err != nil {
4240
return
4341
}
4442

4543
canvas.SetRGBA255(250, 250, 250, 255)
4644
// 绘制副标题
47-
stringwight, _ = canvas.MeasureString(t.LeftSubtitle)
48-
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)
45+
canvas.DrawStringAnchored(t.LeftSubtitle, 3+(220-fontsize1-fontsize3)/3+t.OffsetX, 30+40+(220-fontsize1-fontsize3)/3*2+fontsize1+fontsize3/2+t.OffsetY, 0, 0.5)
4946

50-
// 加载icon并绘制
51-
var icon *imgfactory.Factory
52-
icon, err = imgfactory.LoadFirstFrame(t.ImagePath, 220, 220)
47+
// 加载icon并绘制 高限制220
48+
var icon image.Image
49+
icon, err = gg.LoadImage(t.ImagePath)
5350
if err != nil {
5451
return
5552
}
56-
canvas.DrawImage(icon.Image(), int(DefaultWidth)-icon.W(), 40+30)
53+
sc := 220 / float64(icon.Bounds().Dy())
54+
canvas.ScaleAbout(sc, sc, DefaultWidth-float64(icon.Bounds().Dx())*sc/2, 40+30+220/2)
55+
canvas.DrawImageAnchored(icon, int(DefaultWidth)-int(float64(icon.Bounds().Dx())*sc/2), 40+30+220/2, 0.5, 0.5)
56+
canvas.Identity()
5757
// 加载size为72的字体
58-
fontsize1 = 72 + t.TextFontOffsetPoint
59-
err = canvas.ParseFontFace(t.TextFontData, fontsize1)
58+
err = canvas.ParseFontFace(t.TextFontData, fontsize2)
6059
if err != nil {
6160
return
6261
}
63-
stringwight, _ = canvas.MeasureString(t.RightTitle)
64-
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)
65-
stringwight, _ = canvas.MeasureString(t.RightSubtitle)
66-
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)
62+
canvas.DrawStringAnchored(t.RightTitle, DefaultWidth-float64(icon.Bounds().Dx())*sc-25+t.OffsetX, 30+40+(220-fontsize2*2)/3+fontsize2/2+t.OffsetY, 1, 0.5)
63+
canvas.DrawStringAnchored(t.RightSubtitle, DefaultWidth-float64(icon.Bounds().Dx())*sc-25+t.OffsetX, 30+40+(220-fontsize2*2)/3*2+fontsize2+fontsize2/2+t.OffsetY, 1, 0.5)
6764

6865
imgs = canvas.Image()
6966
return
@@ -83,15 +80,24 @@ func (t *Title) DrawTitleWithText(info []string) (imgs image.Image, err error) {
8380
canvas.Clear()
8481

8582
// 加载icon
86-
var icon *imgfactory.Factory
87-
icon, err = imgfactory.LoadFirstFrame(t.ImagePath, 512, 512)
83+
var icon image.Image
84+
icon, err = gg.LoadImage(t.ImagePath)
8885
if err != nil {
8986
return
9087
}
91-
canvas.DrawImage(icon.Image(), DefaultWidth-icon.W(), imgh-icon.H())
88+
sc := 0.0
89+
if float64(icon.Bounds().Dy())/float64(icon.Bounds().Dx()) > 1 {
90+
sc = 512 / float64(icon.Bounds().Dy())
91+
} else {
92+
sc = DefaultWidth / 2 / float64(icon.Bounds().Dx())
93+
}
94+
canvas.ScaleAbout(sc, sc, float64(canvas.W()), float64(canvas.H()))
95+
canvas.DrawImageAnchored(icon, canvas.W(), canvas.H(), 1, 1)
96+
canvas.Identity()
9297

9398
// 加载size为108的字体
9499
fontsize1, fontsize2 := 108.0+t.TitleFontOffsetPoint, 54.0+t.TextFontOffsetPoint
100+
fsp1, fsp2 := fontsize1*72/96, fontsize2*72/96
95101
err = canvas.ParseFontFace(t.TitleFontData, fontsize1)
96102
if err != nil {
97103
return
@@ -100,8 +106,7 @@ func (t *Title) DrawTitleWithText(info []string) (imgs image.Image, err error) {
100106
canvas.SetRGBA255(15, 15, 15, 255)
101107

102108
// 绘制标题
103-
stringwight, _ := canvas.MeasureString(t.LeftTitle)
104-
canvas.DrawStringAnchored(t.LeftTitle, 25+stringwight/2+t.OffsetX, 25+fontsize1*72/96*0.5+t.OffsetY, 0.5, 0.5)
109+
canvas.DrawStringAnchored(t.LeftTitle, 25+t.OffsetX, 25+fsp1/2+t.OffsetY, 0, 0.5)
105110

106111
// 加载size为54的字体
107112
err = canvas.ParseFontFace(t.TextFontData, fontsize2)
@@ -110,10 +115,9 @@ func (t *Title) DrawTitleWithText(info []string) (imgs image.Image, err error) {
110115
}
111116

112117
// 绘制一系列标题
113-
stringwight, _ = canvas.MeasureString(t.LeftSubtitle)
114-
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)
115-
116-
canvas.DrawRectangle(25+3+t.OffsetX, 25+fontsize1*72/96+25+fontsize2*72/96+7+t.OffsetY, stringwight, 6)
118+
canvas.DrawStringAnchored(t.LeftSubtitle, 25+3+t.OffsetX, 25+fsp1+25+fsp2/2+t.OffsetY, 0, 0.5)
119+
stringwight, _ := canvas.MeasureString(t.LeftSubtitle)
120+
canvas.DrawRectangle(25+3+t.OffsetX, 25+fsp1+25+fsp2+7+t.OffsetY, stringwight, 6)
117121
// 绘制插件开启状态
118122
if t.IsEnabled {
119123
canvas.SetRGBA255(35, 235, 35, 255)
@@ -129,18 +133,16 @@ func (t *Title) DrawTitleWithText(info []string) (imgs image.Image, err error) {
129133
return
130134
}
131135

132-
stringwight, _ = canvas.MeasureString(t.RightTitle)
133-
canvas.DrawStringAnchored(t.RightTitle, DefaultWidth-40-stringwight/2+t.OffsetX, 40+fontsize2*72/96*0.5+t.OffsetY, 0.5, 0.5)
134-
stringwight, _ = canvas.MeasureString(t.RightSubtitle)
135-
canvas.DrawStringAnchored(t.RightSubtitle, DefaultWidth-40-stringwight/2+t.OffsetX, 40+25+fontsize2*72/96*1.5+t.OffsetY, 0.5, 0.5)
136+
canvas.DrawStringAnchored(t.RightTitle, DefaultWidth-40+t.OffsetX, 40+fsp2/2+t.OffsetY, 1, 0.5)
137+
canvas.DrawStringAnchored(t.RightSubtitle, DefaultWidth-40+t.OffsetX, 40+fsp2+25+fsp2/2+t.OffsetY, 1, 0.5)
136138

137139
// 加载size为38的字体
138140
err = canvas.ParseFontFace(t.TextFontData, 38+t.TextFontOffsetPoint)
139141
if err != nil {
140142
return
141143
}
142144

143-
y := 25 + fontsize1*72/96 + 25 + fontsize2*72/96
145+
y := 25 + fsp1 + 25 + fsp2
144146
for _, text := range info {
145147
canvas.DrawString(text, 25.0, y+canvas.FontHeight()*2)
146148
y += 20 + canvas.FontHeight()
@@ -154,14 +156,21 @@ func (t *Title) DrawCard() (imgs image.Image, err error) {
154156
recw, rech := 384.0, 256.0
155157
canvas := gg.NewContext(int(recw), int(rech))
156158
// 绘制图片
157-
var banner *imgfactory.Factory
158-
banner, err = imgfactory.LoadFirstFrame(t.ImagePath, int(recw)*2, int(rech)*2)
159+
var banner image.Image
160+
banner, err = gg.LoadImage(t.ImagePath)
159161
if err == nil {
160-
canvas.DrawImage(imgfactory.Size(banner.Image(), int(recw), int(rech)).Image(), 0, 0)
162+
sc := 0.0
163+
if float64(banner.Bounds().Dy())/float64(banner.Bounds().Dx()) < rech/recw {
164+
sc = rech / float64(banner.Bounds().Dy())
165+
} else {
166+
sc = recw / float64(banner.Bounds().Dx())
167+
}
168+
canvas.ScaleAbout(sc, sc, recw/2, rech/2)
169+
canvas.DrawImageAnchored(banner, int(recw)/2, int(rech)/2, 0.5, 0.5)
170+
canvas.Identity()
161171
} else {
162-
canvas.DrawRectangle(0, 0, recw, rech)
163172
canvas.SetRGB255(RandJPColor())
164-
canvas.Fill()
173+
canvas.Clear()
165174
}
166175
if t.IsEnabled {
167176
canvas.DrawRectangle(0, rech*0.54, recw, rech-rech*0.54)
@@ -174,17 +183,18 @@ func (t *Title) DrawCard() (imgs image.Image, err error) {
174183
// 绘制插件信息
175184
canvas.SetRGBA255(240, 240, 240, 255)
176185
fontsize1, fontsize2 := 64.0+t.TitleFontOffsetPoint, 32.0+t.TextFontOffsetPoint
186+
fsp1, fsp2 := fontsize1*72/96, fontsize2*72/96
177187
err = canvas.ParseFontFace(t.TitleFontData, fontsize1)
178188
if err != nil {
179189
return
180190
}
181-
canvas.DrawStringAnchored(t.LeftTitle, recw/2+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)
191+
canvas.DrawStringAnchored(t.LeftTitle, recw/2+t.OffsetX, rech*0.54+(rech-rech*0.54-(fsp1+fsp2))/3+fsp1/2+t.OffsetY, 0.5, 0.5)
182192

183193
err = canvas.ParseFontFace(t.TextFontData, fontsize2)
184194
if err != nil {
185195
return
186196
}
187-
canvas.DrawStringAnchored(t.LeftSubtitle, recw/2+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)
197+
canvas.DrawStringAnchored(t.LeftSubtitle, recw/2+t.OffsetX, rech*0.54+(rech-rech*0.54-(fsp1+fsp2))/3*2+fsp1+fsp2/2+t.OffsetY, 0.5, 0.5)
188198

189199
imgs = canvas.Image()
190200
return

title_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestDrawTitle(t *testing.T) {
4242
if err != nil {
4343
t.Fatal(err)
4444
}
45-
assert.Equal(t, "28f9f68f3ef7987a9d189b52b9a8b399", hex.EncodeToString(h.Sum(nil)))
45+
assert.Equal(t, "5450734a22186aa176387ff2a91d9a7e", hex.EncodeToString(h.Sum(nil)))
4646
}
4747

4848
func TestDrawTitleWithText(t *testing.T) {
@@ -77,7 +77,7 @@ func TestDrawTitleWithText(t *testing.T) {
7777
if err != nil {
7878
t.Fatal(err)
7979
}
80-
assert.Equal(t, "bf5ce310c0a565477fa83904a506c08d", hex.EncodeToString(h.Sum(nil)))
80+
assert.Equal(t, "49c0bb06f1b8e16833e73d2bdcbe01ce", hex.EncodeToString(h.Sum(nil)))
8181
}
8282

8383
func TestDrawCard(t *testing.T) {
@@ -112,5 +112,5 @@ func TestDrawCard(t *testing.T) {
112112
if err != nil {
113113
t.Fatal(err)
114114
}
115-
assert.Equal(t, "4e1b4c439a78d833b0931655d736b02f", hex.EncodeToString(h.Sum(nil)))
115+
assert.Equal(t, "deba289674306c6d674c0c07f6ae458c", hex.EncodeToString(h.Sum(nil)))
116116
}

utils.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,12 @@ import (
99
)
1010

1111
// Fillet 将矩形图片裁切为圆角矩形
12-
func Fillet(dst image.Image, r int) image.Image {
13-
dstr := gg.ImageToNRGBA(dst)
14-
mx, my := dst.Bounds().Max.X, dst.Bounds().Max.Y
15-
var xx, yy, rr float64
16-
for y := 0; y < my/2; y++ {
17-
for x := 0; x < mx/2; x++ {
18-
if x <= r && y <= r {
19-
xx, yy, rr = float64(r-x), float64(y-r), float64(r)
20-
if xx*xx+yy*yy >= rr*rr {
21-
dstr.Set(x, y, color.NRGBA{})
22-
dstr.Set(mx-1-x, y, color.NRGBA{})
23-
dstr.Set(x, my-1-y, color.NRGBA{})
24-
dstr.Set(mx-1-x, my-1-y, color.NRGBA{})
25-
}
26-
}
27-
}
28-
}
29-
return dstr
12+
func Fillet(dst image.Image, r float64) image.Image {
13+
canvas := gg.NewContext(dst.Bounds().Dx(), dst.Bounds().Dy())
14+
canvas.DrawRoundedRectangle(0, 0, float64(dst.Bounds().Dx()), float64(dst.Bounds().Dy()), r)
15+
canvas.Clip()
16+
canvas.DrawImage(dst, 0, 0)
17+
return canvas.Image()
3018
}
3119

3220
// Transparency 更改透明度 magnification 倍率值

0 commit comments

Comments
 (0)