Skip to content

Commit ca43caf

Browse files
committed
test fonts
1 parent b5926ca commit ca43caf

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

modules/badge/badge.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,23 @@ func (t Text) TextLength() int {
3636
}
3737

3838
type Badge struct {
39-
IDPrefix string
40-
Color string
41-
FontSize int
42-
Label Text
43-
Message Text
39+
IDPrefix string
40+
FontFamily string
41+
Color string
42+
FontSize int
43+
Label Text
44+
Message Text
4445
}
4546

4647
func (b Badge) Width() int {
4748
return b.Label.width + b.Message.width
4849
}
4950

5051
const (
51-
defaultOffset = 10
52-
defaultFontSize = 11
53-
DefaultColor = "#9f9f9f" // Grey
52+
defaultOffset = 10
53+
defaultFontSize = 11
54+
DefaultColor = "#9f9f9f" // Grey
55+
DefaultFontFamily = "DejaVu Sans,Verdana,Geneva,sans-serif"
5456
)
5557

5658
var StatusColorMap = map[actions_model.Status]string{
@@ -72,6 +74,7 @@ func GenerateBadge(label, message, color string) Badge {
7274
lx := lw * 5
7375
mx := lw*10 + mw*5 - 10
7476
return Badge{
77+
FontFamily: DefaultFontFamily,
7578
Label: Text{
7679
text: label,
7780
width: lw,

routers/web/devtest/devtest.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"code.gitea.io/gitea/modules/badge"
1919
"code.gitea.io/gitea/modules/git"
2020
"code.gitea.io/gitea/modules/templates"
21+
"code.gitea.io/gitea/modules/util"
2122
"code.gitea.io/gitea/services/context"
2223
)
2324

@@ -125,24 +126,31 @@ func prepareMockDataBadgeCommitSign(ctx *context.Context) {
125126
}
126127

127128
func prepareMockDataBadgeActionsSvg(ctx *context.Context) {
129+
fontFamilyNames := strings.Split(badge.DefaultFontFamily, ",")
130+
selectedFontFamilyName := ctx.FormString("font", fontFamilyNames[0])
128131
var badges []badge.Badge
132+
badges = append(badges, badge.GenerateBadge("啊啊啊啊啊啊啊啊啊啊啊啊", "🌞🌞🌞🌞🌞", "green"))
129133
for r := rune(0); r < 256; r++ {
130134
if unicode.IsPrint(r) {
131135
s := strings.Repeat(string(r), 15)
132-
badges = append(badges, badge.GenerateBadge(s, s, "green"))
136+
badges = append(badges, badge.GenerateBadge(s, util.TruncateRunes(s, 7), "green"))
133137
}
134138
}
135-
var mockSVGs []template.HTML
139+
140+
var badgeSVGs []template.HTML
136141
for i, b := range badges {
137142
b.IDPrefix = "devtest-" + strconv.FormatInt(int64(i), 10) + "-"
143+
b.FontFamily = selectedFontFamilyName
138144
h, err := ctx.RenderToHTML("shared/actions/runner_badge", map[string]any{"Badge": b})
139145
if err != nil {
140146
ctx.ServerError("RenderToHTML", err)
141147
return
142148
}
143-
mockSVGs = append(mockSVGs, h)
149+
badgeSVGs = append(badgeSVGs, h)
144150
}
145-
ctx.Data["MockSVGs"] = mockSVGs
151+
ctx.Data["BadgeSVGs"] = badgeSVGs
152+
ctx.Data["BadgeFontFamilyNames"] = fontFamilyNames
153+
ctx.Data["SelectedFontFamilyName"] = selectedFontFamilyName
146154
}
147155

148156
func prepareMockData(ctx *context.Context) {

templates/devtest/badge-actions-svg.tmpl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@
22
<div class="page-content devtest ui container">
33
<div>
44
<h1>Actions SVG</h1>
5-
{{range $badgeSVG := .MockSVGs}}
6-
<div class="tw-my-2">{{$badgeSVG}}</div>
5+
<form class="tw-my-3">
6+
{{range $fontName := .BadgeFontFamilyNames}}
7+
<label><input name="font" type="radio" value="{{$fontName}}" {{Iif (eq $.SelectedFontFamilyName $fontName) "checked"}}>{{$fontName}}</label>
8+
{{end}}
9+
<button>submit</button>
10+
</form>
11+
<div class="flex-text-block tw-flex-wrap">
12+
{{range $badgeSVG := .BadgeSVGs}}
13+
<div>{{$badgeSVG}}</div>
714
{{end}}
15+
</div>
816
</div>
917
</div>
1018
{{template "devtest/devtest-footer"}}

templates/shared/actions/runner_badge.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<rect x="{{.Badge.Label.Width}}" width="{{.Badge.Message.Width}}" height="20" fill="{{.Badge.Color}}" />
1414
<rect width="{{.Badge.Width}}" height="20" fill="url(#{{.Badge.IDPrefix}}s)" />
1515
</g>
16-
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif"
16+
<g fill="#fff" text-anchor="middle" font-family="{{.Badge.FontFamily}}"
1717
text-rendering="geometricPrecision" font-size="{{.Badge.FontSize}}">
1818
<text aria-hidden="true" x="{{.Badge.Label.X}}" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)"
1919
textLength="{{.Badge.Label.TextLength}}">{{.Badge.Label.Text}}</text>

0 commit comments

Comments
 (0)