From 26e543ac48752269b3e834c0b8d069d1774083e2 Mon Sep 17 00:00:00 2001 From: Tueem Date: Fri, 13 Feb 2026 16:01:06 +0100 Subject: [PATCH] feat(format): switch to resvg backend --- cmd/svg-templater/main.go | 4 ++-- pkg/format/formatconverter.go | 42 +++++++++++++++++------------------ 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/cmd/svg-templater/main.go b/cmd/svg-templater/main.go index e025df7..d441d36 100644 --- a/cmd/svg-templater/main.go +++ b/cmd/svg-templater/main.go @@ -6,8 +6,8 @@ import ( ) func main() { - if !format.CheckInkscape() { - panic("Inkscape not found") + if !format.CheckResvg() { + panic("Resvg not found") } command.PrepareCommandLine() command.HandleCommandline() diff --git a/pkg/format/formatconverter.go b/pkg/format/formatconverter.go index b88530e..1610752 100644 --- a/pkg/format/formatconverter.go +++ b/pkg/format/formatconverter.go @@ -2,6 +2,7 @@ package format import ( "bytes" + "errors" "fmt" "io" "log" @@ -43,26 +44,23 @@ func ConvertReader(svgblob []byte, param ConversionParameters) (io.ReadCloser, e } 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 { case "png": - args = append(args, "--export-type=png") - 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 convertPNG(file, param) } + 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 { 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 - args = append(args, "-o", outFile) args = append(args, input) + args = append(args, outFile) - cmd := exec.Command("inkscape", args...) + cmd := exec.Command("resvg", args...) cmd.Dir = svg.TempDir out, err := cmd.CombinedOutput() log.Println(string(out)) 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) @@ -92,7 +90,7 @@ func runCommand(input string, param ConversionParameters) (io.ReadCloser, error) return file, nil } -func CheckInkscape() bool { - _, err := exec.LookPath("inkscape") +func CheckResvg() bool { + _, err := exec.LookPath("resvg") return err == nil }