Skip to content

Added: Image can now load SVG files and data #406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed

Added: Image can now load SVG files and data #406

wants to merge 9 commits into from

Conversation

petarov
Copy link

@petarov petarov commented Apr 23, 2014

Hi,

This PR extends Image with the ability to load svg files. The functionality depends on librsvg. What I originally wanted to do was to convert dynamically generated svg images to png files at server side. This patch should now provide the ability to do so:

Here's a short example:

function test_svg2png(imgSrc, w, h) {
    var data = fs.readFileSync(imgSrc);
    var b64data = data.toString('base64');
    var src = "data:image/svg+xml;base64," + b64data;
    var canvas = new Canvas(w, h)
       , ctx = canvas.getContext('2d');

    var img = new Image;
    img.onload = function(){
      ctx.drawImage(img, 0, 0);
      var pngData = canvas.toBuffer();
    };

    img.src = src;
}

Build & Support

Test cases are provided for both make test and make test-server targets.

Windows is currently not supported mainly because I have no access to a Windows installation to test. But it should run fine, if someone is willing to work further on that.

petarov added 9 commits April 23, 2014 01:29
Only compile with librsvg support if the library is present on the
system (using /util/has_lib.sh for lookup) and if the OS is not
Windows.
There are 3 new methods added in Image.cc:
  * isSVG - Attempts to identify if buffer data contains an SVG
image. It checks if the initial bytes start either with 'svg'
or 'xml'. Lower case letters only!
  * loadSVGFromBuffer - Load SVG image from buffer and renders to
Cairo surface.
  * loadSVG - Load a complete SVG file and then calls loadSVGFromBuffer.
This is pretty much the same as loadGIF.
This test loads an SVG buffered file data into an Image and then
draws the buffered data onto a Canvas context. After that it
converts the Canvas data to PNG data.
@petarov petarov changed the title Added: Image can now load SVG files and buffer data Added: Image can now load SVG files and data Apr 23, 2014
@petarov
Copy link
Author

petarov commented Jun 6, 2014

@TooTallNate does this patch make sense? Just wanted to get some feedback because I'd like to use node-canvas for another project I'm working on.

@foysavas
Copy link
Contributor

I'm also looking to use librsvg with node-canvas.

@petarov why was this closed?

@jakeg jakeg mentioned this pull request Sep 10, 2016
@petarov
Copy link
Author

petarov commented Sep 10, 2016

@foysavas Hi, I think there was just no interest in this patch at that time and after 6 or 7 months it was no longer compatible with the code base.

I did use this functionality in a project though, but since I abandoned that project, I decided it was best to close the PR as well.

If someone's interest in working at this, I can help with a review.

@foysavas
Copy link
Contributor

@petarov Wanting to do the same, I was seriously delighted to find your pull request. I'll fork off the current master and try to reapply your code. When it's ready, I'll comment back here with a link for review. Thanks so much for your help and code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants