metapict icon indicating copy to clipboard operation
metapict copied to clipboard

Any way to import/compose SVG files?

Open nickclare opened this issue 1 year ago • 3 comments

I was wondering if there was a way to create objects from existing SVG images, or any existing converter that would allow me to create new functions from SVG files?

For reference, I'm trying to see if MetaPict can be useful for creating system architecture diagrams, particularly using the AWS Architecture Icons, which they provide in SVG format.

I've only just started looking at metapict and racket, and its been many years since I wrote any lisp code at all, so while I'm willing to dive in and try make this work myself, I wanted to check if there were any existing ways before I wasted my time

nickclare avatar Nov 23 '22 14:11 nickclare

Den ons. 23. nov. 2022 kl. 15.18 skrev Nicholas Clare < @.***>:

I was wondering if there was a way to create objects from existing SVG images, or any existing converter that would allow me to create new functions from SVG files?

For reference, I'm trying to see if MetaPict can be useful for creating system architecture diagrams, particularly using the AWS Architecture Icons, which they provide in SVG format.

I've only just started looking at metapict and racket, and its been many years since I wrote any lisp code at all, so while I'm willing to dive in and try make this work myself, I wanted to check if there were any existing ways before I wasted my time

This is a very good question. After looking into it, I must admit, the story isn't particularly good at the moment. Realistically I think the best bet is to turn the SVGs into PNGs. A PNG can then without any problems be used as a pict.

For the diagrams I recommend using "nodes" in Metapict: https://youtu.be/7Kf8Kk8NPFw?t=577 The various node functions are listed here: https://github.com/soegaard/metapict/blob/master/metapict/node.rkt#L8

Back to SVGs: As far as I know there is no SVG drawing library using Racket.

Metapict works on top of pict which is a way of representing drawings in Racket. The pict library is built using the graphics library Cairo (implemented in C).

Years ago the solution was to use the Racket library rsvg which included a way to turn an svg file into a pict. The pict could then be used with, say Metapict.

It might be possible to get rsvg running again. It consists of bindings for librsvg which was a C library that could render SVGs using Cairo. Time has passed and now librsvg is implemented in Rust - but I believe there is still a C interface.

Rendering SVGs using Racket would be a good thing - so I might look at the problem later. I can't promise an immediate solution though.

But ... maybe I have missed a way to render SVGs. Try asking the Discourse on how to draw SVGs with Racket.

/Jens Axel

soegaard avatar Nov 23 '22 16:11 soegaard

@nickclare

I am happy to report, that librsvg still works!

Turns out the C api has changed in years, so installing a new version of librsvg using Homebrew worked for me.

I installed C librarylibrsvg with brew install librsvg

And the Racket library rsvg with: raco pkg install rsvg

A small test with random svg file worked fine:

#lang racket
(require racket/runtime-path
         rsvg
         metapict)

(define-runtime-path logo.svg "python-logo.svg")
(define logo (svg-file->pict logo.svg))

(set-curve-pict-size 400 400)
logo

(draw (rotate (scale 0.5 logo) (rad 15))
      (text-color "white" (text "Python"))
      (text "Python"))
image

soegaard avatar Nov 23 '22 20:11 soegaard

Thanks so much for the quick response. I'll definitely try this out and let you know how it goes. Ideally what I want to do is have a library of high-level functions that draw the AWS components according to their style guide, but being able to draw the SVG files they give is a huge first step.

nickclare avatar Nov 24 '22 07:11 nickclare