]> gitweb.michael.orlitzky.com - libsvgtiny-pixbuf.git/commitdiff
example.c: render to diagram width/height instead of the viewport's.
authorMichael Orlitzky <michael@orlitzky.com>
Fri, 4 Aug 2023 12:21:01 +0000 (08:21 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Fri, 4 Aug 2023 12:21:01 +0000 (08:21 -0400)
example.c

index bc09e4c6236c0383d7a4cf92d624e14b968b32d2..74a66581f04bf04733795b8adc3ab9967a6c5da5 100644 (file)
--- a/example.c
+++ b/example.c
@@ -6,14 +6,32 @@
 #include <gdk-pixbuf/gdk-pixbuf.h>
 #include <svgtiny.h>
 
+/*
+ * The width and height of the viewport that we'll render the SVG
+ * into. The final "picture" may not actually be this size; based on
+ * the height, width, viewBox, and preserveAspectRatio attributes in
+ * the SVG itself, libsvgtiny may scale, stretch, offset, etc. the
+ * paths to make them fit nicely into the viewport.
+ */
+#define VIEWPORT_WIDTH 512
+#define VIEWPORT_HEIGHT 512
+
 /* Convenient typedefs for libsvgtiny */
 typedef struct svgtiny_diagram diagram_t;
+typedef struct svgtiny_shape shape_t;
 
 
 /**
- * Render an svgtiny path using cairo.
+ * @brief Render an svgtiny path using cairo.
+ *
+ * @param cr
+ *   A pointer to a valid cairo context.
+ *
+ * @param path
+ *   A pointer to an svgtiny shape that will be rendered on the
+ *   cairo context's target surface.
  */
-void render_path(cairo_t *cr, struct svgtiny_shape *path) {
+static void render_path(cairo_t* cr, shape_t* path) {
   unsigned int j;
 
   cairo_new_path(cr);
@@ -67,7 +85,7 @@ void render_path(cairo_t *cr, struct svgtiny_shape *path) {
   }
 }
 
-/*
+/**
  * @brief Parse an SVG file into a diagram_t structure.
  *
  * @param fp
@@ -77,11 +95,11 @@ void render_path(cairo_t *cr, struct svgtiny_shape *path) {
  *   returned; if not, @c NULL is returned. You are expected to @c
  *   svgtiny_free the result if it is valid.
  */
-diagram_t* svgtiny_diagram_from_file(FILE* fp, int width, int height) {
-  diagram_t *diagram;
+static diagram_t* svgtiny_diagram_from_file(FILE* fp, int width, int height) {
+  diagram_tdiagram;
 
   size_t bytecount;
-  char *buffer;
+  charbuffer;
   size_t bytesread;
   svgtiny_code code;
 
@@ -141,7 +159,7 @@ diagram_t* svgtiny_diagram_from_file(FILE* fp, int width, int height) {
   return diagram;
 }
 
-/*
+/**
  * @brief Create a cairo context from a libsvgtiny diagram.
  *
  * @param diagram
@@ -151,7 +169,7 @@ diagram_t* svgtiny_diagram_from_file(FILE* fp, int width, int height) {
  *   is returned; if not, @c NULL is returned. You are expected to @c
  *   cairo_destroy the result if it is valid.
  */
-cairo_t* cairo_context_from_diagram(diagram_t* diagram) {
+static cairo_t* cairo_context_from_diagram(diagram_t* diagram) {
   cairo_t* cr;
   cairo_surface_t* surface;
   cairo_status_t crs;
@@ -232,11 +250,10 @@ cairo_t* cairo_context_from_diagram(diagram_t* diagram) {
 int main(int argc, char** argv) {
   char* svgpath;
   char* pngpath;
-  int pngwidth = 1024, pngheight = 1024;
   FILE* fp;
 
   diagram_t* diagram;
-  cairo_t *cr = 0;
+  cairo_tcr = 0;
 
   GdkPixbuf* pb;
 
@@ -256,7 +273,7 @@ int main(int argc, char** argv) {
     return 1;
   }
 
-  diagram = svgtiny_diagram_from_file(fp, pngwidth, pngheight);
+  diagram = svgtiny_diagram_from_file(fp, VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
   if (!diagram) {
     return 1;
   }
@@ -267,11 +284,18 @@ int main(int argc, char** argv) {
     return 1;
   }
 
+  /* We're using the diagram width and height and not the viewport
+   * width/height for the image. This has the potential to create an
+   * image with a different size and aspect ratio than the viewport,
+   * but since our viewport is entirely made-up... I don't know. This
+   * relies on libsvgtiny being good at scaling/stretching/etc an SVG
+   * that may only have partial width/height/viewBox information.
+   */
   pb = gdk_pixbuf_get_from_surface(cairo_get_target(cr),
                                   0,
                                   0,
-                                  pngwidth,
-                                  pngheight);
+                                  diagram->width,
+                                  diagram->height);
 
 
   if (pb) {