shithub: svg

Download patch

ref: 6843eef01c520cc8add0f8ab5e17f17c1fe4895a
parent: 006cab3e7f612d5ec48764fce408a5f34b7972cb
author: phil9 <[email protected]>
date: Fri May 14 02:27:51 EDT 2021

scale rendering to fit window

	when displayed in a window, the image is scaled to fit the window dimensions.

--- a/svg.c
+++ b/svg.c
@@ -14,12 +14,14 @@
 Image *svg;
 
 void
-rasterize(void)
+rasterize(int scale)
 {
 	NSVGimage *image;
 	struct NSVGrasterizer *rast;
 	uchar *data;
 	int w, h;
+	float s;
+	Rectangle r;
 
 	image = nsvgParseFromFile(filename, "px", 96);
 	if(image==nil)
@@ -26,9 +28,19 @@
 		sysfatal("svg parse: %r");
 	w = image->width;
 	h = image->height;
+	s = 1.0f;
+	if(scale){
+		r = insetrect(screen->r, 10);
+		w = Dx(r);
+		h = Dy(r);
+		if(w < h)
+			s = (float)w/image->width;
+		else
+			s = (float)h/image->height;
+	}
 	rast = nsvgCreateRasterizer();
 	data = malloc(w*h*4);
-	nsvgRasterize(rast, image, 0, 0, 1, data, w, h, w*4);
+	nsvgRasterize(rast, image, 0, 0, s, data, w, h, w*4);
 	nsvgDelete(image);
 	nsvgDeleteRasterizer(rast);
 	svg = allocimage(display, Rect(0, 0, w, h), ABGR32, 0, DNofill);
@@ -42,6 +54,8 @@
 
 	if(new && getwindow(display, Refnone)<0)
 		sysfatal("cannot reattach: %r");
+	freeimage(svg);
+	rasterize(1);
 	p.x = (Dx(screen->r) - Dx(svg->r))/2;
 	p.y = (Dy(screen->r) - Dy(svg->r))/2;
 	draw(screen, screen->r, display->white, nil, ZP);
@@ -76,8 +90,8 @@
 	filename = *argv;
 	if(initdraw(nil, nil, "svg")<0)
 		sysfatal("initdraw: %r");
-	rasterize();
 	if(nineflag){
+		rasterize(0);
 		writeimage(1, svg, 0);
 		freeimage(svg);
 		exits(nil);