diff --git a/caller.go b/caller.go index b70955b..190be86 100644 --- a/caller.go +++ b/caller.go @@ -17,7 +17,8 @@ type Caller interface { VersionString() (string, error) // Fonts returns all fonts that are available to Typst. - Fonts() ([]string, error) + // The options parameter is optional, and can be nil. + Fonts(options *OptionsFonts) ([]string, error) // Compile takes a Typst document from the supplied input reader, and renders it into the output writer. // The options parameter is optional, and can be nil. diff --git a/cli.go b/cli.go index 0c7d629..51f2ac6 100644 --- a/cli.go +++ b/cli.go @@ -53,7 +53,8 @@ func (c CLI) VersionString() (string, error) { } // Fonts returns all fonts that are available to Typst. -func (c CLI) Fonts() ([]string, error) { +// The options parameter is optional, and can be nil. +func (c CLI) Fonts(options *OptionsFonts) ([]string, error) { // Get path of executable. execPath := ExecutablePath if c.ExecutablePath != "" { @@ -63,7 +64,12 @@ func (c CLI) Fonts() ([]string, error) { return nil, fmt.Errorf("not supported on this platform") } - cmd := exec.Command(execPath, "fonts") + args := []string{"fonts"} + if options != nil { + args = append(args, options.Args()...) + } + + cmd := exec.Command(execPath, args...) cmd.Dir = c.WorkingDirectory var output, errBuffer bytes.Buffer diff --git a/cli_test.go b/cli_test.go index 4f51566..5e02ec2 100644 --- a/cli_test.go +++ b/cli_test.go @@ -28,7 +28,7 @@ func TestCLI_VersionString(t *testing.T) { func TestCLI_Fonts(t *testing.T) { caller := typst.CLI{} - result, err := caller.Fonts() + result, err := caller.Fonts(nil) if err != nil { t.Fatalf("Failed to get available fonts: %v.", err) } @@ -37,6 +37,18 @@ func TestCLI_Fonts(t *testing.T) { } } +func TestCLI_FontsWithOptions(t *testing.T) { + caller := typst.CLI{} + + result, err := caller.Fonts(&typst.OptionsFonts{IgnoreSystemFonts: true, IgnoreEmbeddedFonts: true}) + if err != nil { + t.Fatalf("Failed to get available fonts: %v.", err) + } + if len(result) != 0 { + t.Errorf("Unexpected number of detected fonts. Got %d, want %d.", len(result), 0) + } +} + // Test basic compile functionality. func TestCLI_Compile(t *testing.T) { const inches = 1 diff --git a/docker.go b/docker.go index cb00930..5c297fe 100644 --- a/docker.go +++ b/docker.go @@ -64,13 +64,19 @@ func (d Docker) VersionString() (string, error) { } // Fonts returns all fonts that are available to Typst. -func (d Docker) Fonts() ([]string, error) { +// The options parameter is optional, and can be nil. +func (d Docker) Fonts(options *OptionsFonts) ([]string, error) { image := DockerDefaultImage if d.Image != "" { image = d.Image } - cmd := exec.Command("docker", "run", "-i", image, "fonts") + args := []string{"run", "-i", image, "fonts"} + if options != nil { + args = append(args, options.Args()...) + } + + cmd := exec.Command("docker", args...) var output, errBuffer bytes.Buffer cmd.Stdout = &output diff --git a/docker_test.go b/docker_test.go index d3e9b58..93c15d7 100644 --- a/docker_test.go +++ b/docker_test.go @@ -27,7 +27,7 @@ func TestDocker_VersionString(t *testing.T) { func TestDocker_Fonts(t *testing.T) { caller := typst.Docker{} - result, err := caller.Fonts() + result, err := caller.Fonts(nil) if err != nil { t.Fatalf("Failed to get available fonts: %v.", err) } @@ -36,6 +36,18 @@ func TestDocker_Fonts(t *testing.T) { } } +func TestDocker_FontsWithOptions(t *testing.T) { + caller := typst.Docker{} + + result, err := caller.Fonts(&typst.OptionsFonts{IgnoreSystemFonts: true, IgnoreEmbeddedFonts: true}) + if err != nil { + t.Fatalf("Failed to get available fonts: %v.", err) + } + if len(result) != 0 { + t.Errorf("Unexpected number of detected fonts. Got %d, want %d.", len(result), 0) + } +} + // Test basic compile functionality. func TestDocker_Compile(t *testing.T) { const inches = 1 diff --git a/options.go b/options.go index e40fb05..42279f6 100644 --- a/options.go +++ b/options.go @@ -45,6 +45,46 @@ const ( PDFStandardUA_1 PDFStandard = "ua-1" // PDF/UA-1 (Available since Typst 0.14.0) ) +// OptionsFonts contains all parameters for the fonts command. +type OptionsFonts struct { + FontPaths []string // Adds additional directories that are recursively searched for fonts. + IgnoreSystemFonts bool // Ensures system fonts won't be searched, unless explicitly included via FontPaths. + IgnoreEmbeddedFonts bool // Disables the use of fonts embedded into the Typst binary. (Available since Typst 0.14.0) + Variants bool // Also lists style variants of each font family. + + Custom []string // Custom command line options go here. +} + +// Args returns a list of CLI arguments that should be passed to the executable. +func (o *OptionsFonts) Args() (result []string) { + if len(o.FontPaths) > 0 { + var paths string + for i, path := range o.FontPaths { + if i > 0 { + paths += string(os.PathListSeparator) + } + paths += path + } + result = append(result, "--font-path", paths) + } + + if o.IgnoreSystemFonts { + result = append(result, "--ignore-system-fonts") + } + + if o.IgnoreEmbeddedFonts { + result = append(result, "--ignore-embedded-fonts") + } + + if o.Variants { + result = append(result, "--variants") + } + + result = append(result, o.Custom...) + + return +} + // OptionsCompile contains all parameters for the compile command. type OptionsCompile struct { Root string // Configures the project root (for absolute paths).