From c2ec7947df2a5c9aa4b691272b1347e62fe9ff96 Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Tue, 22 Jul 2014 14:54:09 -0400 Subject: [PATCH] SVG scaling is now supported! --- lib/squib/api/image.rb | 11 ++++++----- lib/squib/graphics/image.rb | 9 ++++++--- lib/squib/input_helpers.rb | 11 +++++------ samples/basic.rb | 4 ++-- samples/load-images.rb | 16 ++++++++++++++++ 5 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 samples/load-images.rb diff --git a/lib/squib/api/image.rb b/lib/squib/api/image.rb index ae9078c..1381f5a 100644 --- a/lib/squib/api/image.rb +++ b/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 diff --git a/lib/squib/graphics/image.rb b/lib/squib/graphics/image.rb index 8741727..7f6b4a4 100644 --- a/lib/squib/graphics/image.rb +++ b/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 diff --git a/lib/squib/input_helpers.rb b/lib/squib/input_helpers.rb index 8278e95..ce1f9e0 100644 --- a/lib/squib/input_helpers.rb +++ b/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 diff --git a/samples/basic.rb b/samples/basic.rb index 14b0d5c..2bcdd41 100644 --- a/samples/basic.rb +++ b/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, diff --git a/samples/load-images.rb b/samples/load-images.rb new file mode 100644 index 0000000..caef06b --- /dev/null +++ b/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!" \ No newline at end of file