Skip to content

Commit e60ee4a

Browse files
committed
fix(kmeans): wg wait
1 parent 068f91e commit e60ee4a

2 files changed

Lines changed: 58 additions & 14 deletions

File tree

examples/fonts_test.go

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
package main
22

3-
import "runtime"
3+
import (
4+
"os"
5+
"runtime"
6+
)
7+
8+
// firstExisting returns the first path that exists on disk, or the last path as fallback.
9+
func firstExisting(paths ...string) string {
10+
for _, p := range paths {
11+
if _, err := os.Stat(p); err == nil {
12+
return p
13+
}
14+
}
15+
return paths[len(paths)-1]
16+
}
417

518
// fontPath returns the OS-specific file path for a given font name.
619
// Supported names: "Impact", "Arial", "Arial Bold".
@@ -9,29 +22,62 @@ func fontPath(name string) string {
922
case "windows":
1023
switch name {
1124
case "Impact":
12-
return `C:\Windows\Fonts\impact.ttf`
25+
return firstExisting(
26+
`C:\Windows\Fonts\impact.ttf`,
27+
`C:\Windows\Fonts\DejaVuSans-Bold.ttf`,
28+
)
1329
case "Arial":
14-
return `C:\Windows\Fonts\arial.ttf`
30+
return firstExisting(
31+
`C:\Windows\Fonts\arial.ttf`,
32+
`C:\Windows\Fonts\LiberationSans-Regular.ttf`,
33+
)
1534
case "Arial Bold":
16-
return `C:\Windows\Fonts\arialbd.ttf`
35+
return firstExisting(
36+
`C:\Windows\Fonts\arialbd.ttf`,
37+
`C:\Windows\Fonts\LiberationSans-Bold.ttf`,
38+
)
1739
}
1840
case "darwin":
1941
switch name {
2042
case "Impact":
21-
return "/System/Library/Fonts/Supplemental/Impact.ttf"
43+
return firstExisting(
44+
"/System/Library/Fonts/Supplemental/Impact.ttf",
45+
"/System/Library/Fonts/Impact.ttf",
46+
"/Library/Fonts/Impact.ttf",
47+
)
2248
case "Arial":
23-
return "/System/Library/Fonts/Supplemental/Arial.ttf"
49+
return firstExisting(
50+
"/System/Library/Fonts/Supplemental/Arial.ttf",
51+
"/System/Library/Fonts/Arial.ttf",
52+
"/Library/Fonts/Arial.ttf",
53+
)
2454
case "Arial Bold":
25-
return "/System/Library/Fonts/Supplemental/Arial Bold.ttf"
55+
return firstExisting(
56+
"/System/Library/Fonts/Supplemental/Arial Bold.ttf",
57+
"/System/Library/Fonts/Arial Bold.ttf",
58+
"/Library/Fonts/Arial Bold.ttf",
59+
)
2660
}
2761
default: // linux, freebsd, etc.
2862
switch name {
2963
case "Impact":
30-
return "/usr/share/fonts/truetype/msttcorefonts/Impact.ttf"
64+
return firstExisting(
65+
"/usr/share/fonts/truetype/msttcorefonts/Impact.ttf",
66+
"/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
67+
"/usr/share/fonts/TTF/DejaVuSans-Bold.ttf",
68+
)
3169
case "Arial":
32-
return "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf"
70+
return firstExisting(
71+
"/usr/share/fonts/truetype/msttcorefonts/Arial.ttf",
72+
"/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
73+
"/usr/share/fonts/TTF/LiberationSans-Regular.ttf",
74+
)
3375
case "Arial Bold":
34-
return "/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf"
76+
return firstExisting(
77+
"/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf",
78+
"/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf",
79+
"/usr/share/fonts/TTF/LiberationSans-Bold.ttf",
80+
)
3581
}
3682
}
3783
return name

kmeans.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,11 @@ func (ki *kmeansImage) assign() error {
9090

9191
n := runtime.NumCPU()
9292
batchcnt := len(ki.pixels) / n
93-
rem := len(ki.pixels) % n
9493
wg := sync.WaitGroup{}
9594
wg.Add(n)
96-
if rem < 0 {
97-
wg.Add(1)
98-
}
9995
for batch := range n {
10096
go func(batch int) {
97+
defer wg.Done()
10198
base := batch * batchcnt
10299
for i, pixel := range ki.pixels[base : base+batchcnt] {
103100
minDistance := math.MaxFloat64
@@ -126,6 +123,7 @@ func (ki *kmeansImage) assign() error {
126123
}
127124
ki.clusterAssignments[base+i] = assign
128125
}
126+
wg.Wait()
129127
return nil
130128
}
131129

0 commit comments

Comments
 (0)