mirror of
https://github.com/Dadido3/go-typst.git
synced 2025-11-20 03:49:34 +00:00
Add options to Fonts command
This commit is contained in:
parent
a075fb41bf
commit
83d5fc35ea
@ -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.
|
||||
|
||||
10
cli.go
10
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
|
||||
|
||||
14
cli_test.go
14
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
|
||||
|
||||
10
docker.go
10
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
|
||||
|
||||
@ -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
|
||||
|
||||
40
options.go
40
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).
|
||||
|
||||
Loading…
Reference in New Issue
Block a user