From 3f4e001e63eaf1f2ca9a9f392c78f734d27b392e Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Thu, 17 Jul 2014 10:55:20 -0400 Subject: [PATCH] Adding SVG support --- lib/squib/api/image.rb | 8 +++- lib/squib/deck.rb | 3 ++ lib/squib/graphics/image.rb | 10 +++++ samples/basic.rb | 1 + samples/spanner.svg | 90 +++++++++++++++++++++++++++++++++++++ squib.gemspec | 1 + 6 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 samples/spanner.svg diff --git a/lib/squib/api/image.rb b/lib/squib/api/image.rb index bccbd52..98a171b 100644 --- a/lib/squib/api/image.rb +++ b/lib/squib/api/image.rb @@ -1,11 +1,17 @@ module Squib class Deck - def png(range=:all, file: nil, x: 0, y: 0) + def png(range: :all, file: nil, x: 0, y: 0) range = rangeify(range) file = fileify(file) range.each{ |i| @cards[i].png(file, x, y) } end + def svg(range: :all, file: nil, x: 0, y: 0) + range = rangeify(range) + file = fileify(file) + range.each{ |i| @cards[i].svg(file, x, y) } + end + end end \ No newline at end of file diff --git a/lib/squib/deck.rb b/lib/squib/deck.rb index b43196c..67ae36c 100644 --- a/lib/squib/deck.rb +++ b/lib/squib/deck.rb @@ -27,6 +27,9 @@ module Squib def rangeify (range) range = 0..(@cards.size-1) if range == :all range = range..range if range.is_a? Integer + if range.max > (@cards.size-1) + raise "#{range} is outside of deck range of 0..#{@card.size-1}" + end return range end diff --git a/lib/squib/graphics/image.rb b/lib/squib/graphics/image.rb index ebd7a09..a00e52e 100644 --- a/lib/squib/graphics/image.rb +++ b/lib/squib/graphics/image.rb @@ -8,5 +8,15 @@ module Squib cc.paint end + def svg(file, x, y) + require 'rsvg2' + svg = RSVG::Handle.new_from_file(file) + tmp = Cairo::ImageSurface.new(svg.width, svg.height) + tmp_cc = Cairo::Context.new(tmp) + tmp_cc.render_rsvg_handle(svg, width: 400, height: 400) + cairo_context.set_source(tmp, x, y) + cairo_context.paint + end + end end \ No newline at end of file diff --git a/samples/basic.rb b/samples/basic.rb index 70524ea..389fcb5 100644 --- a/samples/basic.rb +++ b/samples/basic.rb @@ -15,6 +15,7 @@ Squib::Deck.new(width: 825, height: 1125, cards: 3) do text str: longtext, x: 100, y: 600, font: 'Arial 16' png file: 'shiny-purse.png', x: 665, y: 30 + svg range: 1..2, file: 'spanner.svg', x: 665, y: 165 save format: :png end diff --git a/samples/spanner.svg b/samples/spanner.svg new file mode 100644 index 0000000..e4704b7 --- /dev/null +++ b/samples/spanner.svg @@ -0,0 +1,90 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + + + diff --git a/squib.gemspec b/squib.gemspec index 00ba186..6431e8e 100644 --- a/squib.gemspec +++ b/squib.gemspec @@ -20,6 +20,7 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency 'cairo', '~> 1.12.9' spec.add_runtime_dependency 'pango', '~> 2.2.0' spec.add_runtime_dependency 'roo', '~> 1.13.2' + spec.add_runtime_dependency 'rsvg2', '~> 2.2.0' spec.add_development_dependency "bundler", "~> 1.6" spec.add_development_dependency "rake"