Skip to content

Commit eb13e57

Browse files
committed
Support compiling for TinyGo
The issue was that os.UserCacheDir is missing
1 parent acb59e3 commit eb13e57

File tree

4 files changed

+45
-13
lines changed

4 files changed

+45
-13
lines changed

fontscan/fontmap.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package fontscan
33
import (
44
"fmt"
55
"log"
6-
"os"
76
"path/filepath"
8-
"runtime"
97
"sync"
108

119
"github.com/go-text/typesetting/font"
@@ -165,17 +163,7 @@ func cacheDir(userProvided string) (string, error) {
165163
return userProvided, nil
166164
}
167165
// load an existing index
168-
if runtime.GOOS == "android" {
169-
// There is no stable way to infer the proper place to store the cache
170-
// with access to the Java runtime for the application. Rather than
171-
// clutter our API with that, require the caller to provide a path.
172-
return "", fmt.Errorf("user must provide cache directory on android")
173-
}
174-
configDir, err := os.UserCacheDir()
175-
if err != nil {
176-
return "", fmt.Errorf("resolving index cache path: %s", err)
177-
}
178-
return configDir, nil
166+
return platformCacheDir()
179167
}
180168

181169
// initSystemFonts scan the system fonts and update `SystemFonts`.

fontscan/fontmap_cache_android.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//go:build android
2+
3+
package fontscan
4+
5+
func platformCacheDir() (string, error) {
6+
// There is no stable way to infer the proper place to store the cache
7+
// with access to the Java runtime for the application. Rather than
8+
// clutter our API with that, require the caller to provide a path.
9+
return "", fmt.Errorf("user must provide cache directory on android")
10+
}

fontscan/fontmap_cache_other.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//go:build !android && !tinygo
2+
3+
package fontscan
4+
5+
import (
6+
"fmt"
7+
"os"
8+
)
9+
10+
func platformCacheDir() (string, error) {
11+
configDir, err := os.UserCacheDir()
12+
if err != nil {
13+
return "", fmt.Errorf("resolving index cache path: %s", err)
14+
}
15+
return configDir, nil
16+
}

fontscan/fontmap_cache_tinygo.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//go:build tinygo
2+
3+
package fontscan
4+
5+
import (
6+
"fmt"
7+
"os"
8+
"path/filepath"
9+
)
10+
11+
func platformCacheDir() (string, error) {
12+
// if no path is provided we cannot get cache dir with tinygo, so just make one up.
13+
homeDir, err := os.UserHomeDir()
14+
if err != nil {
15+
return "", fmt.Errorf("resolving index cache path: %s", err)
16+
}
17+
return filepath.Join(homeDir, ".cache"), nil
18+
}

0 commit comments

Comments
 (0)