Skip to content

Commit e7201d6

Browse files
committed
fix(examples): font loading on different os
1 parent 2479795 commit e7201d6

File tree

9 files changed

+54
-9
lines changed

9 files changed

+54
-9
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ jobs:
6161
find . -name "*.cpp" -type f -delete
6262
shell: bash
6363

64+
- name: Install Microsoft core fonts
65+
if: runner.os == 'Linux'
66+
run: |
67+
echo ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true | sudo debconf-set-selections
68+
sudo apt-get update -q
69+
sudo apt-get install -y ttf-mscorefonts-installer
70+
6471
- name: Run examples
6572
shell: bash
6673
run: go test -v -count=1 ./...

examples/fonts_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import "runtime"
4+
5+
// fontPath returns the OS-specific file path for a given font name.
6+
// Supported names: "Impact", "Arial", "Arial Bold".
7+
func fontPath(name string) string {
8+
switch runtime.GOOS {
9+
case "windows":
10+
switch name {
11+
case "Impact":
12+
return `C:\Windows\Fonts\impact.ttf`
13+
case "Arial":
14+
return `C:\Windows\Fonts\arial.ttf`
15+
case "Arial Bold":
16+
return `C:\Windows\Fonts\arialbd.ttf`
17+
}
18+
case "darwin":
19+
switch name {
20+
case "Impact":
21+
return "/System/Library/Fonts/Supplemental/Impact.ttf"
22+
case "Arial":
23+
return "/System/Library/Fonts/Supplemental/Arial.ttf"
24+
case "Arial Bold":
25+
return "/System/Library/Fonts/Supplemental/Arial Bold.ttf"
26+
}
27+
default: // linux, freebsd, etc.
28+
switch name {
29+
case "Impact":
30+
return "/usr/share/fonts/truetype/msttcorefonts/Impact.ttf"
31+
case "Arial":
32+
return "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf"
33+
case "Arial Bold":
34+
return "/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf"
35+
}
36+
}
37+
return name
38+
}

examples/gradient-text_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestGT(*testing.T) {
1616

1717
// draw text
1818
dc.SetRGB(0, 0, 0)
19-
if err := dc.LoadFontFace("/System/Library/Fonts/Supplemental/Impact.ttf", 128); err != nil {
19+
if err := dc.LoadFontFace(fontPath("Impact"), 128); err != nil {
2020
panic(err)
2121
}
2222
dc.DrawStringAnchored("Gradient Text", W/2, H/2, 0.5, 0.5)

examples/meme_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func TestMeme(*testing.T) {
1111
dc := gg.NewContext(S, S)
1212
dc.SetRGB(1, 1, 1)
1313
dc.Clear()
14-
if err := dc.LoadFontFace("/System/Library/Fonts/Supplemental/Impact.ttf", 96); err != nil {
14+
if err := dc.LoadFontFace(fontPath("Impact"), 96); err != nil {
1515
panic(err)
1616
}
1717
dc.SetRGB(0, 0, 0)

examples/quadratic_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestQuadratic(*testing.T) {
5050
dc.SetLineWidth(4)
5151
dc.Stroke()
5252

53-
if err := dc.LoadFontFace("/System/Library/Fonts/Supplemental/Arial.ttf", 200); err != nil {
53+
if err := dc.LoadFontFace(fontPath("Arial"), 200); err != nil {
5454
panic(err)
5555
}
5656
dc.DrawStringAnchored("g", -5, 5, 0.5, 0.5)

examples/scatter_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ func TestScatter(*testing.T) {
5555
// draw text
5656
dc.Identity()
5757
dc.SetRGB(0, 0, 0)
58-
if err := dc.LoadFontFace("/System/Library/Fonts/Supplemental/Arial Bold.ttf", 24); err != nil {
58+
if err := dc.LoadFontFace(fontPath("Arial Bold"), 24); err != nil {
5959
panic(err)
6060
}
6161
dc.DrawStringAnchored("Chart Title", S/2, P/2, 0.5, 0.5)
62-
if err := dc.LoadFontFace("/System/Library/Fonts/Supplemental/Arial.ttf", 18); err != nil {
62+
if err := dc.LoadFontFace(fontPath("Arial"), 18); err != nil {
6363
panic(err)
6464
}
6565
dc.DrawStringAnchored("X Axis Title", S/2, S-P/2, 0.5, 0.5)

examples/text_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestText(*testing.T) {
1313
dc.Clear()
1414
dc.SetRGB(0, 0, 0)
1515
// 字体路径
16-
if err := dc.LoadFontFace("/System/Library/Fonts/Supplemental/Arial.ttf", 96); err != nil {
16+
if err := dc.LoadFontFace(fontPath("Arial"), 96); err != nil {
1717
panic(err)
1818
}
1919
dc.DrawStringAnchored("Hello, world!", S/2, S/2, 0.5, 0.5)

examples/unicode_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestUnicode(*testing.T) {
1414
dc.SetRGB(1, 1, 1)
1515
dc.Clear()
1616
dc.SetRGB(0, 0, 0)
17-
if err := dc.LoadFontFace("/System/Library/Fonts/Supplemental/Impact.ttf", F); err != nil {
17+
if err := dc.LoadFontFace(fontPath("Impact"), F); err != nil {
1818
panic(err)
1919
}
2020
for r := range 256 {

examples/wrap_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestWrap(*testing.T) {
2222
dc.SetLineWidth(3)
2323
dc.Stroke()
2424
dc.SetRGB(0, 0, 0)
25-
if err := dc.LoadFontFace("/System/Library/Fonts/Supplemental/Arial Bold.ttf", 18); err != nil {
25+
if err := dc.LoadFontFace(fontPath("Arial Bold"), 18); err != nil {
2626
panic(err)
2727
}
2828
dc.DrawStringWrapped("UPPER LEFT", P, P, 0, 0, 0, 1.5, gg.AlignLeft)
@@ -33,7 +33,7 @@ func TestWrap(*testing.T) {
3333
dc.DrawStringWrapped("LOWER MIDDLE", W/2, H-P, 0.5, 1, 0, 1.5, gg.AlignCenter)
3434
dc.DrawStringWrapped("LEFT MIDDLE", P, H/2, 0, 0.5, 0, 1.5, gg.AlignLeft)
3535
dc.DrawStringWrapped("RIGHT MIDDLE", W-P, H/2, 1, 0.5, 0, 1.5, gg.AlignRight)
36-
if err := dc.LoadFontFace("/System/Library/Fonts/Supplemental/Arial.ttf", 12); err != nil {
36+
if err := dc.LoadFontFace(fontPath("Arial"), 12); err != nil {
3737
panic(err)
3838
}
3939
dc.DrawStringWrapped(TEXT, W/2-P, H/2-P, 1, 1, W/3, 1.75, gg.AlignLeft)

0 commit comments

Comments
 (0)