Exemple #1
0
func TestParseSVG(t *testing.T) {
	if testing.Short() {
		t.Skip("Skipping lengthier tests during short test run.")
	}

	var tests = []string{
		"rectfull",
		"rectfullwithtext",
		"tworects",
		"threerects",
	}

	for _, tt := range tests {
		f, err := os.Open("testsvgs/" + tt + ".svg")
		if err != nil {
			t.Errorf("Failed to open %s.svg for reading: %s", tt, err)
			continue
		}
		defer f.Close()

		p := ssvgc.NewParser(f)
		svg, err := p.ParseSVG()
		if err != nil {
			t.Errorf("ParseSVG(%s.svg) failed => %s", tt, err)
			continue
		}

		img := svg.Draw()
		draw2dimg.SaveToPngFile("output/"+tt+".png", img)
	}
}
Exemple #2
0
func TestImageRendering(t *testing.T) {
	var tests = []StringMap{
		{
			"name": "tuxcentered",
			"xml": `<svg width="200" height="200" fill="white">
<image href="testimages/tux-construction.png" width="150" x="25" y="25" />
</svg>`,
		},
		{
			"name": "tuxbottomright",
			"xml": `<svg width="200" height="200" fill="white">
<image href="testimages/tux-construction.png" width="150" x="50" y="50" />
</svg>`,
		},
		{
			"name": "tuxfullsize",
			"xml": `<svg width="200" height="200" fill="white">
<image href="testimages/tux-construction.png" />
</svg>`,
		},
	}

	for _, tt := range tests {
		r := strings.NewReader(tt["xml"])
		p := ssvgc.NewParser(r)
		svg, err := p.ParseSVG()
		if err != nil {
			t.Errorf("ParseSVG() failed => %s", err)
			continue
		}

		draw2dimg.SaveToPngFile("output/"+tt["name"]+".png", svg.Draw())
	}
}
Exemple #3
0
func main() {
	if len(os.Args) < 3 {
		log.Fatal(`Error!  ssvgc requires two arguments.

$ ssvgc <in.svg> <out.png>`)
	}

	r, f := LoadSVG(os.Args[1])
	defer f.Close()
	p := ssvgc.NewParser(r)
	svg, err := p.ParseSVG()
	if err != nil {
		log.Fatal("Error parsing SVG.", err)
	}

	SavePNG(os.Args[2], svg.Draw())
}
Exemple #4
0
func TestTextRendering(t *testing.T) {
	var tests = []StringMap{
		{
			"name": "textredbanana",
			"text-value": `<svg width="200" height="200" fill="white">
<text ttf-font="/tmp/DejaVuSans.ttf" x="15" y="30">You are
    <tspan fill="red">not</tspan>
    a banana
</text>
</svg>`,
		},
		{
			"name": "textbananashiftedright",
			"text-value": `<svg width="200" height="200" fill="white">
<text ttf-font="/tmp/DejaVuSans.ttf" x="45" y="30">You are
    <tspan>not</tspan>
    a banana
</text>
</svg>`,
		},
		{
			"name": "textbananashifteddown",
			"text-value": `<svg width="200" height="200" fill="white">
<text ttf-font="/tmp/DejaVuSans.ttf" font-size="18" x="1" y="70">You are
    <tspan>not</tspan>
    a banana
</text>
</svg>`,
		},
		{
			"name": "textbananaanchormiddle",
			"text-value": `<svg width="500" height="200" fill="white">
<text ttf-font="/tmp/DejaVuSans.ttf" font-size="18" x="250" y="70" text-anchor="middle">You are
    <tspan>not</tspan>
    a banana
</text>
</svg>`,
		},
		{
			"name": "textbananaanchorend",
			"text-value": `<svg width="500" height="200" fill="white">
<text ttf-font="/tmp/DejaVuSans.ttf" font-size="18" x="250" y="70" text-anchor="end">You are
    <tspan>not</tspan>
    a banana
</text>
</svg>`,
		},
	}

	for _, tt := range tests {
		r := strings.NewReader(tt["text-value"])
		p := ssvgc.NewParser(r)
		svg, err := p.ParseSVG()
		if err != nil {
			t.Errorf("ParseSVG() failed => %s", err)
			continue
		}

		draw2dimg.SaveToPngFile("output/"+tt["name"]+".png", svg.Draw())
	}
}