Add options to Fonts command

This commit is contained in:
David Vogel 2025-11-16 12:35:13 +00:00
parent a075fb41bf
commit 83d5fc35ea
6 changed files with 84 additions and 7 deletions

View File

@ -17,7 +17,8 @@ type Caller interface {
VersionString() (string, error) VersionString() (string, error)
// Fonts returns all fonts that are available to Typst. // 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. // 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. // The options parameter is optional, and can be nil.

10
cli.go
View File

@ -53,7 +53,8 @@ func (c CLI) VersionString() (string, error) {
} }
// Fonts returns all fonts that are available to Typst. // 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. // Get path of executable.
execPath := ExecutablePath execPath := ExecutablePath
if c.ExecutablePath != "" { if c.ExecutablePath != "" {
@ -63,7 +64,12 @@ func (c CLI) Fonts() ([]string, error) {
return nil, fmt.Errorf("not supported on this platform") 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 cmd.Dir = c.WorkingDirectory
var output, errBuffer bytes.Buffer var output, errBuffer bytes.Buffer

View File

@ -28,7 +28,7 @@ func TestCLI_VersionString(t *testing.T) {
func TestCLI_Fonts(t *testing.T) { func TestCLI_Fonts(t *testing.T) {
caller := typst.CLI{} caller := typst.CLI{}
result, err := caller.Fonts() result, err := caller.Fonts(nil)
if err != nil { if err != nil {
t.Fatalf("Failed to get available fonts: %v.", err) 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. // Test basic compile functionality.
func TestCLI_Compile(t *testing.T) { func TestCLI_Compile(t *testing.T) {
const inches = 1 const inches = 1

View File

@ -64,13 +64,19 @@ func (d Docker) VersionString() (string, error) {
} }
// Fonts returns all fonts that are available to Typst. // 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 image := DockerDefaultImage
if d.Image != "" { if d.Image != "" {
image = 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 var output, errBuffer bytes.Buffer
cmd.Stdout = &output cmd.Stdout = &output

View File

@ -27,7 +27,7 @@ func TestDocker_VersionString(t *testing.T) {
func TestDocker_Fonts(t *testing.T) { func TestDocker_Fonts(t *testing.T) {
caller := typst.Docker{} caller := typst.Docker{}
result, err := caller.Fonts() result, err := caller.Fonts(nil)
if err != nil { if err != nil {
t.Fatalf("Failed to get available fonts: %v.", err) 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. // Test basic compile functionality.
func TestDocker_Compile(t *testing.T) { func TestDocker_Compile(t *testing.T) {
const inches = 1 const inches = 1

View File

@ -45,6 +45,46 @@ const (
PDFStandardUA_1 PDFStandard = "ua-1" // PDF/UA-1 (Available since Typst 0.14.0) 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. // OptionsCompile contains all parameters for the compile command.
type OptionsCompile struct { type OptionsCompile struct {
Root string // Configures the project root (for absolute paths). Root string // Configures the project root (for absolute paths).