feat(format): switch to resvg backend
All checks were successful
build / Go-Build (push) Successful in 28s
build / Go-Build (pull_request) Successful in 27s

This commit is contained in:
2026-02-13 16:01:06 +01:00
parent 0118a53d3e
commit 26e543ac48
2 changed files with 22 additions and 24 deletions

View File

@@ -6,8 +6,8 @@ import (
) )
func main() { func main() {
if !format.CheckInkscape() { if !format.CheckResvg() {
panic("Inkscape not found") panic("Resvg not found")
} }
command.PrepareCommandLine() command.PrepareCommandLine()
command.HandleCommandline() command.HandleCommandline()

View File

@@ -2,6 +2,7 @@ package format
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"io" "io"
"log" "log"
@@ -43,26 +44,23 @@ func ConvertReader(svgblob []byte, param ConversionParameters) (io.ReadCloser, e
} }
defer os.Remove(file) defer os.Remove(file)
return runCommand(file, param)
}
func runCommand(input string, param ConversionParameters) (io.ReadCloser, error) {
var args []string
var outExt string
switch param.Format { switch param.Format {
case "png": case "png":
args = append(args, "--export-type=png") return convertPNG(file, param)
outExt = ".png"
case "jpg":
args = append(args, "--export-type=png") // inkscape doesn't export jpg directly
outExt = ".png"
case "pdf":
args = append(args, "--export-type=pdf")
outExt = ".pdf"
default:
return nil, fmt.Errorf("format not supported: %s", param.Format)
} }
return nil, errors.New("Format not supported")
}
func convertPNG(input string, param ConversionParameters) (io.ReadCloser, error) {
var args []string
var outExt string
fontsdir, err := svg.Storage.GetFontsDir()
if err != nil {
return nil, err
}
args = append(args, "--skip-system-fonts")
args = append(args, "--use-fonts-dir", fontsdir)
if param.Width > 0 { if param.Width > 0 {
args = append(args, "-w", strconv.Itoa(param.Width)) args = append(args, "-w", strconv.Itoa(param.Width))
@@ -72,16 +70,16 @@ func runCommand(input string, param ConversionParameters) (io.ReadCloser, error)
} }
outFile := strings.TrimSuffix(input, filepath.Ext(input)) + outExt outFile := strings.TrimSuffix(input, filepath.Ext(input)) + outExt
args = append(args, "-o", outFile)
args = append(args, input) args = append(args, input)
args = append(args, outFile)
cmd := exec.Command("inkscape", args...) cmd := exec.Command("resvg", args...)
cmd.Dir = svg.TempDir cmd.Dir = svg.TempDir
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
log.Println(string(out)) log.Println(string(out))
if err != nil { if err != nil {
return nil, fmt.Errorf("inkscape failed: %w: %s", err, string(out)) return nil, fmt.Errorf("resvg failed: %w: %s", err, string(out))
} }
file, err := os.Open(outFile) file, err := os.Open(outFile)
@@ -92,7 +90,7 @@ func runCommand(input string, param ConversionParameters) (io.ReadCloser, error)
return file, nil return file, nil
} }
func CheckInkscape() bool { func CheckResvg() bool {
_, err := exec.LookPath("inkscape") _, err := exec.LookPath("resvg")
return err == nil return err == nil
} }