Skip to content
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

Combine existing SVG files #53

Closed
00dav00 opened this issue Aug 26, 2020 · 3 comments
Closed

Combine existing SVG files #53

00dav00 opened this issue Aug 26, 2020 · 3 comments

Comments

@00dav00
Copy link

00dav00 commented Aug 26, 2020

Hi there, I have a use case that I'm not sure how to address with this gem.
I need to conbine 2 SVG files:

  • An SVG logo
  • An SVG QR code generated by this gem

I've checked victor-cli a little bit and I think it can help with me with this problem, just not sure how.
Is there a tool or method I'm missing that can accomplish that?

Thanks!

@DannyBen
Copy link
Owner

DannyBen commented Aug 26, 2020

Victor was not designed for this, it was designed to generate SVG from within Ruby, from scratch.

However, you might be able to use the #append (aka << operator) feature, which allows you to merge any object that responds to #to_s, and returns an SVG string.

Notice, you will need to obtain the SVG string without any of its surrounding <svg> tags - only the "body".

Here is a sample, to hopefully get you on the right track. This takes two SVG strings, and merges them into one SVG object, while transforming (moving) one of them to a certain position.

require 'victor'
include Victor

logo = '<rect x="0" y="0" width="50" height="50" fill="red"/>'
qrcode = '<circle cx="50" cy="60" r="20" fill="yellow"/>'

svg = SVG.new viewBox: '0 0 100 100', style: { background: "#ddd" }
svg.build do
  append logo
  g(transform: "translate(10  10)") { append qrcode }
end

svg.save 'merged'

Remember, the #append method only needs a string, so if you are using the QR code gem, and if it has a method that returns the SVG string itself (without root node), you may be able to do it all in "one pass" from within Ruby - instantiate the QR object, get (not save) its string, and merge it with your logo that you obtain via File.read or however.

@00dav00
Copy link
Author

00dav00 commented Aug 26, 2020

Thanks for the quick reply @DannyBen, the info helps a lot.

I've found this method in victor-cli which I think will help me too:

Victor::CLI::SVGNode.load_file

@00dav00
Copy link
Author

00dav00 commented Aug 26, 2020

Closing the issue, never the less I think it would be nice to be able to open existing SVG to append things to them. ;)

@00dav00 00dav00 closed this as completed Aug 26, 2020
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

No branches or pull requests

2 participants