Browse Source

Adding SVG support

dev
Andy Meneely 12 years ago
parent
commit
3f4e001e63
  1. 8
      lib/squib/api/image.rb
  2. 3
      lib/squib/deck.rb
  3. 10
      lib/squib/graphics/image.rb
  4. 1
      samples/basic.rb
  5. 90
      samples/spanner.svg
  6. 1
      squib.gemspec

8
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

3
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

10
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

1
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

90
samples/spanner.svg

@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
viewBox="0 0 128 128"
id="svg2"
version="1.1"
inkscape:version="0.48.4 r9939"
width="100%"
height="100%"
sodipodi:docname="spanner.svg">
<metadata
id="metadata18">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
</cc:Work>
</rdf:RDF>
</metadata>
<defs
id="defs16" />
<sodipodi:namedview
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1"
objecttolerance="10"
gridtolerance="10"
guidetolerance="10"
inkscape:pageopacity="0"
inkscape:pageshadow="2"
inkscape:window-width="1680"
inkscape:window-height="988"
id="namedview14"
showgrid="false"
inkscape:zoom="2.8284271"
inkscape:cx="98.928938"
inkscape:cy="62.194589"
inkscape:window-x="1432"
inkscape:window-y="109"
inkscape:window-maximized="1"
inkscape:current-layer="g3767"
inkscape:document-units="in"
units="px"
showguides="true"
inkscape:guide-bbox="true" />
<g
id="g3767"
transform="matrix(0.24961486,0,0,0.24961486,0.197194,96.049335)">
<rect
ry="67.108368"
y="-378.68747"
x="5.3126979"
height="501.3746"
width="501.3746"
id="rect3765"
style="fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:10.62539577;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:0" />
<path
style="fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none"
d="M 36.267098,-7.709219 C 16.393265,5.1318032 3.2161811,27.462365 3.2161811,52.884128 c 0,39.825874 32.2852369,72.111092 72.1110909,72.111092 23.870397,0 44.964938,-11.67699 58.089488,-29.545518 -11.26063,7.275808 -24.65607,11.517748 -39.060173,11.517748 -39.825853,0 -72.11109,-32.285221 -72.11109,-72.111095 0,-15.95546 5.248882,-30.6218942 14.021601,-42.565574 z"
id="path3790"
inkscape:connector-curvature="0" />
<g
id="g6"
transform="matrix(18.68858,0,0,18.68858,-3385.6437,-2608.3849)">
<path
style="fill:#ffffff"
inkscape:connector-curvature="0"
id="path8"
d="m 192.2325,129.3203 -10,10 v 5.5625 h 5.375 l 10.0938,-10.0938 -5.4688,-5.4688 z" />
<path
style="fill:#ffffff;stroke:#000000"
inkscape:connector-curvature="0"
id="path10"
d="m 181.7796,141.8591 15.6196,-15.6178 m 3.349,3.3486 -15.8051,15.8032" />
<path
style="fill:#ffffff"
inkscape:connector-curvature="0"
id="path12"
d="m 201.2023,119.7104 c -2.8283,-0.73 -5.9623,-0.004 -8.1759,2.2097 -3.3293,3.3292 -3.3293,8.7358 -2e-5,12.065 3.32928,3.3292 8.7358,3.3292 12.065,0 2.216,-2.216 2.9436,-5.3452 2.2097,-8.1759 l -5.6348,5.6348 -4.8172,-1.2816 -1.2816,-4.8171 5.6348,-5.6348 z" />
</g>
</g>
</svg>

After

Width:  |  Height:  |  Size: 3.3 KiB

1
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"

Loading…
Cancel
Save