Browse Source

SVG scaling is now supported!

dev
Andy Meneely 12 years ago
parent
commit
c2ec7947df
  1. 11
      lib/squib/api/image.rb
  2. 9
      lib/squib/graphics/image.rb
  3. 11
      lib/squib/input_helpers.rb
  4. 4
      samples/basic.rb
  5. 16
      samples/load-images.rb

11
lib/squib/api/image.rb

@ -3,7 +3,7 @@ module Squib
# Renders a png file at the given location.
# See {file:samples/image.rb samples/image.rb} and {file:samples/tgc-overlay.rb samples/tgc-overlay.rb} as examples.
# Note: scaling not currently supported.
# Note: scaling not currently supported for PNGs.
#
# @param range: the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges}
# @param file: the . See {file:API.md#Specifying+Files Specifying Files}
@ -17,17 +17,18 @@ module Squib
end
# Renders an entire svg file at the given location. Uses the SVG-specified units and DPI to determine the pixel width and height.
# See {file:samples/image.rb samples/image.rb} and {file:samples/tgc-overlay.rb samples/tgc-overlay.rb} as examples.
# Note: scaling not currently supported.
# See {file:samples/load-images.rb samples/load-images.rb} and {file:samples/tgc-overlay.rb samples/tgc-overlay.rb} as examples.
#
# @param range: the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges}
# @param file: the . See {file:API.md#Specifying+Files Specifying Files}
# @param x: the x-coordinate to place
# @param y: the y-coordinate to place
def svg(range: :all, file: nil, x: 0, y: 0)
# @param width: the pixel width that the image should scale to. SVG scaling is done with vectors, so the scaling should be smooth. When set to `:native`, uses the DPI and units of the loaded SVG document.
# @param height: the pixel width that the image should scale to. SVG scaling is done with vectors, so the scaling should be smooth. When set to `:native`, uses the DPI and units of the loaded SVG document.
def svg(range: :all, file: nil, x: 0, y: 0, width: :native, height: :native)
range = rangeify(range)
file = fileify(file)
range.each{ |i| @cards[i].svg(file, x, y) }
range.each{ |i| @cards[i].svg(file, x, y, width, height) }
end
end

9
lib/squib/graphics/image.rb

@ -8,11 +8,14 @@ module Squib
cc.paint(alpha)
end
def svg(file, x, y)
require 'rsvg2'
def svg(file, x, y, width, height)
svg = RSVG::Handle.new_from_file(file)
tmp = Cairo::ImageSurface.new(svg.width, svg.height)
width = svg.width if width == :native
height = svg.height if height == :native
puts "Width and height #{width} #{height}, svg was #{svg.width} #{svg.height}"
tmp = Cairo::ImageSurface.new(width, height)
tmp_cc = Cairo::Context.new(tmp)
tmp_cc.scale(width.to_f / svg.width.to_f, height.to_f / svg.height.to_f)
tmp_cc.render_rsvg_handle(svg)
cairo_context.set_source(tmp, x, y)
cairo_context.paint

11
lib/squib/input_helpers.rb

@ -50,14 +50,13 @@ module Squib
module_function :fontify
def radiusify(radius, x_radius, y_radius)
unless radius.nil?
ret_x = radius
ret_y = radius
if radius.nil?
return x_radius, y_radius
else
return radius,radius
end
ret_x = x_radius unless x_radius.nil?
rex_y = y_radius unless y_radius.nil?
return ret_x,ret_y
end
module_function :radiusify
def xyify
#TODO: Allow negative numbers that subtract from the card width & height

4
samples/basic.rb

@ -6,8 +6,8 @@ data = {'name' => ['Thief', 'Grifter', 'Mastermind'],
Squib::Deck.new(width: 825, height: 1125, cards: 3) do
background color: :white
rect x: 38, y: 38, width: 750, height: 1050, x_radius: 38, y_radius: 38
rect x: 75, y: 75, width: 128, height: 128, x_radius: 25, y_radius: 25
rect x: 38, y: 38, width: 750, height: 1050, radius: 38
rect x: 75, y: 75, width: 128, height: 128, radius: 25
text str: data['name'], x: 220, y: 78, font: 'Arial 54'
text str: data['level'], x: 75, y: 85, width: 128,

16
samples/load-images.rb

@ -0,0 +1,16 @@
require 'squib'
Squib::Deck.new(width: 825, height: 1125, cards: 1) do
background color: :white
rect x: 38, y: 38, width: 750, height: 1050, x_radius: 38, y_radius: 38
png file: 'shiny-purse.png', x: 620, y: 75
svg file: 'spanner.svg', x: 620, y: 218
# SVGs can be scaled too
svg file: 'spanner.svg', x: 50, y: 50, width: 250, height: 250
save prefix: 'load_images_', format: :png
end
puts "Done!"
Loading…
Cancel
Save