Browse Source

Better handling of empty files

dev
Andy Meneely 12 years ago
parent
commit
5c6149ad5a
  1. 4
      lib/squib/api/image.rb
  2. 2
      lib/squib/graphics/image.rb
  3. 10
      lib/squib/input_helpers.rb

4
lib/squib/api/image.rb

@ -9,7 +9,7 @@ module Squib
# png file: 'img.png', x: 50, y: 50 # png file: 'img.png', x: 50, y: 50
# #
# @option opts range [Enumerable, :all] (:all) the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges} # @option opts range [Enumerable, :all] (:all) the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges}
# @option opts file [String, Array] ('') file(s) to read in. If it's a single file, then it's use for every card in range. If the parameter is an Array of files, then each file is looked up for each card. See {file:API.md#Specifying+Files Specifying Files} # @option opts file [String, Array] ('') file(s) to read in. If it's a single file, then it's use for every card in range. If the parameter is an Array of files, then each file is looked up for each card. If any of them are nil or '', nothing is done. See {file:API.md#Specifying+Files Specifying Files}
# @option opts x [Integer] (0) the x-coordinate to place # @option opts x [Integer] (0) the x-coordinate to place
# @option opts y [Integer] (0) the y-coordinate to place # @option opts y [Integer] (0) the y-coordinate to place
# @option opts layout [String, Symbol] (nil) entry in the layout to use as defaults for this command. See {file:API.md#label-Custom+Layouts Custom Layouts} # @option opts layout [String, Symbol] (nil) entry in the layout to use as defaults for this command. See {file:API.md#label-Custom+Layouts Custom Layouts}
@ -30,7 +30,7 @@ module Squib
# svg 1..2, 'icon.svg', '#stone', x: 50, y:50 # svg 1..2, 'icon.svg', '#stone', x: 50, y:50
# #
# @option opts range [Enumerable, :all] (:all) the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges} # @option opts range [Enumerable, :all] (:all) the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges}
# @option opts file [String, Array] ('') file(s) to read in. If it's a single file, then it's use for every card in range. If the parameter is an Array of files, then each file is looked up for each card. See {file:API.md#Specifying+Files Specifying Files} # @option opts file [String, Array] ('') file(s) to read in. If it's a single file, then it's use for every card in range. If the parameter is an Array of files, then each file is looked up for each card. If any of them are nil or '', nothing is done. See {file:API.md#Specifying+Files Specifying Files}
# @option opts x [Integer] (0) the x-coordinate to place # @option opts x [Integer] (0) the x-coordinate to place
# @option opts y [Integer] (0) the y-coordinate to place # @option opts y [Integer] (0) the y-coordinate to place
# @option opts width [Integer] (:native) 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. # @option opts width [Integer] (:native) 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.

2
lib/squib/graphics/image.rb

@ -4,6 +4,7 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def png(file, x, y, alpha) def png(file, x, y, alpha)
return if file.nil? or file.eql? ''
cc = cairo_context cc = cairo_context
png = Cairo::ImageSurface.from_png(file) png = Cairo::ImageSurface.from_png(file)
cc.set_source(png, x, y) cc.set_source(png, x, y)
@ -13,6 +14,7 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def svg(file, id, x, y, width, height) def svg(file, id, x, y, width, height)
return if file.nil? or file.eql? ''
svg = RSVG::Handle.new_from_file(file) svg = RSVG::Handle.new_from_file(file)
width = svg.width if width == :native width = svg.width if width == :native
height = svg.height if height == :native height = svg.height if height == :native

10
lib/squib/input_helpers.rb

@ -12,8 +12,8 @@ module Squib
opts = Squib::SYSTEM_DEFAULTS.merge(opts) opts = Squib::SYSTEM_DEFAULTS.merge(opts)
opts = rangeify(opts) if params.include? :range opts = rangeify(opts) if params.include? :range
opts = fileify(opts) if params.include? :file opts = fileify(opts) if params.include? :file
opts = fileify(opts, false, true) if params.include? :file_to_save opts = fileify(opts, false, false) if params.include? :file_to_save
opts = fileify(opts, true) if params.include? :files opts = fileify(opts, true, false) if params.include? :files
opts = colorify(opts) if params.include? :color opts = colorify(opts) if params.include? :color
opts = colorify(opts, true) if params.include? :nillable_color opts = colorify(opts, true) if params.include? :nillable_color
opts = dirify(opts) if params.include? :dir opts = dirify(opts) if params.include? :dir
@ -68,11 +68,11 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def fileify(opts, expand_singletons=false, allow_non_exist=false) def fileify(opts, expand_singletons=false, file_must_exist=true)
opts[:file] = [opts[:file]] * @cards.size if expand_singletons opts[:file] = [opts[:file]] * @cards.size if expand_singletons && !(opts[:file].respond_to? :each)
files = [opts[:file]].flatten files = [opts[:file]].flatten
files.each do |file| files.each do |file|
unless File.exists?(file) || allow_non_exist if file_must_exist and !File.exists?(file)
raise "File #{File.expand_path(file)} does not exist!" raise "File #{File.expand_path(file)} does not exist!"
end end
end end

Loading…
Cancel
Save