WIP: Add SVG Templating #10

Draft
tueem wants to merge 14 commits from feat/template into dev
2 changed files with 22 additions and 24 deletions
Showing only changes of commit 26e543ac48 - Show all commits

View File

@@ -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()

View File

@@ -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
}