Browse Source

Implemented new args design for svg and png

As part of that implementation, text_embed now supports singleton expansion. Closes #54
dev
Andy Meneely 11 years ago
parent
commit
e20c644a9a
  1. 6
      CHANGELOG.md
  2. 41
      lib/squib/api/image.rb
  3. 2
      lib/squib/api/text.rb
  4. 66
      lib/squib/api/text_embed.rb
  5. 6
      lib/squib/args/box.rb
  6. 4
      lib/squib/args/card_range.rb
  7. 25
      lib/squib/args/embed_adjust.rb
  8. 34
      lib/squib/args/input_file.rb
  9. 37
      lib/squib/args/svg_special.rb
  10. 3
      lib/squib/card.rb
  11. 64
      lib/squib/graphics/image.rb
  12. 12
      lib/squib/graphics/text.rb
  13. 10
      samples/embed_text.rb
  14. 14
      spec/args/box_spec.rb
  15. 7
      spec/data/samples/embed_text.rb.txt
  16. 16
      spec/data/samples/hand.rb.txt

6
CHANGELOG.md

@ -8,12 +8,14 @@ Features
* Added `join` option to all drawing operations (e.g. `rect`, `star`, even outlines for `text`) to define how corners are drawn. (#42) * Added `join` option to all drawing operations (e.g. `rect`, `star`, even outlines for `text`) to define how corners are drawn. (#42)
* Added `dash` option to all drawing operations (e.g. `rect`, `star`, even outlines for `text`) so you can specify your own dash pattern. Just specify a string with space-separated numbers to specify the on-and-off alternating pattern (e.g. `dash: '2 2'` with a stroke width of 2 is evenly spaced dots). Supports unit conversion (e.g. `dash: '0.02in 0.02in'`) (#42) * Added `dash` option to all drawing operations (e.g. `rect`, `star`, even outlines for `text`) so you can specify your own dash pattern. Just specify a string with space-separated numbers to specify the on-and-off alternating pattern (e.g. `dash: '2 2'` with a stroke width of 2 is evenly spaced dots). Supports unit conversion (e.g. `dash: '0.02in 0.02in'`) (#42)
* Added an idiom to the `ranges.rb` sample for drawing a different number of images based on the column in a table (e.g. 2 arrows to indicate 2 actions). Based on question #90. There are probably even cleaner, Ruby-ish ways to do this too - pull requests are welcome. * Added an idiom to the `ranges.rb` sample for drawing a different number of images based on the column in a table (e.g. 2 arrows to indicate 2 actions). Based on question #90. There are probably even cleaner, Ruby-ish ways to do this too - pull requests are welcome.
* The `text` method and several other methods will throw errors on invalid input. This means your scripts will be more likely to break if you provide bad input * The `text` method and several other methods will throw errors on invalid input. This means your scripts will be more likely to break if you provided bad input in the past.
* The `text` embedding icon now allows singleton expansion, which means that you can have icons have different sizes on different cards. The sample `embed_text.rb` demonstrates this. (#54)
Compatibility: Compatibility:
* All drawn shapes (e.g. circle, triangle, star) will now draw their stroke on top of the fill. This was not consistent before, and now it is (because Squib is more DRY about it!). This means that your `stroke_width` might render wider than before, but now it's accurate. * All drawn shapes (e.g. circle, triangle, star) will now draw their stroke on top of the fill. This was not consistent before, and now it is (because Squib is more DRY about it!). This means that your `stroke_width` might render wider than before, but now it's accurate.
* The `width` and `height` options for `text` have changed their defaults away from `:native` to `:auto`. This is to differentiate them from `:native` widths that default elsewhere. The behavior is the same, just new names. * The `width` and `height` options for `text` have changed their defaults from `:native` to `:auto`. This is to differentiate them from `:native` widths that default elsewhere. Additionally, `width` and `height` for shapes now default to `:deck`, and get interpreted as the deck width and height. The `:native` options are interpreted for SVG and PNG images as their original values. The behavior is all the same, just with more specific names.
* Removed `img_dir` from the `set` method. You can still set `img_dir` in the configuration file (e.g. `config.yml`). Added a deprecation error. * Removed `img_dir` from the `set` method. You can still set `img_dir` in the configuration file (e.g. `config.yml`). Added a deprecation error.
* Default `width` and `height` for text embedding `png` and `svg` have changed from 32 to `:native` to be more consistent with the rest of the system
Bugs: Bugs:
* Fixed a `Cairo::WriteError` on `save_sheet` (#56, PR #96 thank you @meltheadorable!) * Fixed a `Cairo::WriteError` on `save_sheet` (#56, PR #96 thank you @meltheadorable!)

41
lib/squib/api/image.rb

@ -1,5 +1,10 @@
require 'squib/args/paint'
require 'squib/args/card_range' require 'squib/args/card_range'
require 'squib/args/paint'
require 'squib/args/box'
require 'squib/args/transform'
require 'squib/args/input_file'
require 'squib/args/svg_special'
module Squib module Squib
class Deck class Deck
@ -25,18 +30,13 @@ module Squib
def png(opts = {}) def png(opts = {})
range = Args::CardRange.new(opts[:range], deck_size: size) range = Args::CardRange.new(opts[:range], deck_size: size)
paint = Args::Paint.new(custom_colors).load!(opts, expand_by: size, layout: layout) paint = Args::Paint.new(custom_colors).load!(opts, expand_by: size, layout: layout)
opts = needs(opts, [:range, box = Args::Box.new(self, {width: :native, height: :native}).load!(opts, expand_by: size, layout: layout, dpi: dpi)
:files, trans = Args::Transform.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
:x, :y, :width, :height, ifile = Args::InputFile.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
:angle,
:alpha, :blend, :mask,
:layout])
Dir.chdir(img_dir) do Dir.chdir(img_dir) do
@progress_bar.start('Loading PNG(s)', opts[:range].size) do |bar| @progress_bar.start('Loading PNG(s)', range.size) do |bar|
opts[:range].each do |i| range.each do |i|
@cards[i].png(opts[:file][i], @cards[i].png(ifile[i].file, box[i], paint[i], trans[i])
opts[:x][i], opts[:y][i], opts[:width][i], opts[:height][i],
opts[:alpha][i], opts[:blend][i], opts[:angle][i], opts[:mask][i])
bar.increment bar.increment
end end
end end
@ -66,14 +66,17 @@ module Squib
# @return [nil] Returns nil # @return [nil] Returns nil
# @api public # @api public
def svg(opts = {}) def svg(opts = {})
p = needs(opts,[:range, :files, :svgdata, :svgid, :force_svgid, :x, :y, :width, :height, range = Args::CardRange.new(opts[:range], deck_size: size)
:layout, :alpha, :blend, :angle, :mask]) paint = Args::Paint.new(custom_colors).load!(opts, expand_by: size, layout: layout)
box = Args::Box.new(self, {width: :native, height: :native}).load!(opts, expand_by: size, layout: layout, dpi: dpi)
trans = Args::Transform.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
ifile = Args::InputFile.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
svg_args = Args::SvgSpecial.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
Dir.chdir(img_dir) do Dir.chdir(img_dir) do
@progress_bar.start('Loading SVG(s)', p[:range].size) do |bar| @progress_bar.start('Loading SVG(s)', range.size) do |bar|
p[:range].each do |i| range.each do |i|
unless p[:force_id][i] && p[:id][i].to_s.empty? if svg_args.render?(i)
@cards[i].svg(p[:file][i], p[:data][i], p[:id][i], p[:x][i], p[:y][i], @cards[i].svg(ifile[i].file, svg_args[i], box[i], paint[i], trans[i])
p[:width][i], p[:height][i], p[:alpha][i], p[:blend][i], p[:angle][i],p[:mask][i])
end end
bar.increment bar.increment
end end

2
lib/squib/api/text.rb

@ -53,7 +53,7 @@ module Squib
box = Args::Box.new(self, {width: :auto, height: :auto}).load!(opts, expand_by: size, layout: layout, dpi: dpi) box = Args::Box.new(self, {width: :auto, height: :auto}).load!(opts, expand_by: size, layout: layout, dpi: dpi)
trans = Args::Transform.new.load!(opts, expand_by: size, layout: layout, dpi: dpi) trans = Args::Transform.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
draw = Args::Draw.new(custom_colors, {stroke_width: 0.0}).load!(opts, expand_by: size, layout: layout, dpi: dpi) draw = Args::Draw.new(custom_colors, {stroke_width: 0.0}).load!(opts, expand_by: size, layout: layout, dpi: dpi)
embed = TextEmbed.new embed = TextEmbed.new(size, custom_colors, layout, dpi)
yield(embed) if block_given? #store the opts for later use yield(embed) if block_given? #store the opts for later use
extents = Array.new(@cards.size) extents = Array.new(@cards.size)
range.each { |i| extents[i] = @cards[i].text(embed, para[i], box[i], trans[i], draw[i]) } range.each { |i| extents[i] = @cards[i].text(embed, para[i], box[i], trans[i], draw[i]) }

66
lib/squib/api/text_embed.rb

@ -1,3 +1,10 @@
require 'squib/args/box'
require 'squib/args/card_range'
require 'squib/args/embed_adjust'
require 'squib/args/input_file'
require 'squib/args/paint'
require 'squib/args/transform'
module Squib module Squib
class TextEmbed class TextEmbed
# :nodoc: # :nodoc:
@ -6,7 +13,11 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def initialize def initialize(deck_size, custom_colors, layout, dpi)
@deck_size = deck_size
@custom_colors = custom_colors
@layout = layout
@dpi = dpi
@rules = {} # store an array of options for later usage @rules = {} # store an array of options for later usage
end end
@ -17,22 +28,29 @@ module Squib
# @option opts id [String] (nil) if set, then only render the SVG element with the given id. Prefix '#' is optional. Note: the x-y coordinates are still relative to the SVG document's page. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion} # @option opts id [String] (nil) if set, then only render the SVG element with the given id. Prefix '#' is optional. Note: the x-y coordinates are still relative to the SVG document's page. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
# @option opts force_id [Boolean] (false) if set, then this svg will not be rendered at all if the id is empty or nil. If not set, the entire SVG is rendered. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion} # @option opts force_id [Boolean] (false) if set, then this svg will not be rendered at all if the id is empty or nil. If not set, the entire SVG is rendered. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
# @option opts layout [String, Symbol] (nil) entry in the layout to use as defaults for this command. See {file:README.md#Custom_Layouts Custom Layouts}. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion} # @option opts layout [String, Symbol] (nil) entry in the layout to use as defaults for this command. See {file:README.md#Custom_Layouts Custom Layouts}. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
# @option opts width [Integer] (32) the width of the image rendered. # @option opts width [Integer] (:native) the width of the image rendered.
# @option opts height [Integer] (32) the height the height of the image rendered. # @option opts height [Integer] (:native) the height the height of the image rendered.
# @option opts dx [Integer] (0) "delta x", or adjust the icon horizontally by x pixels # @option opts dx [Integer] (0) "delta x", or adjust the icon horizontally by x pixels
# @option opts dy [Integer] (0) "delta y", or adjust the icon vertically by y pixels # @option opts dy [Integer] (0) "delta y", or adjust the icon vertically by y pixels
# @option opts alpha [Decimal] (1.0) the alpha-transparency percentage used to blend this image. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion} # @option opts alpha [Decimal] (1.0) the alpha-transparency percentage used to blend this image. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
# @option opts blend [:none, :multiply, :screen, :overlay, :darken, :lighten, :color_dodge, :color_burn, :hard_light, :soft_light, :difference, :exclusion, :hsl_hue, :hsl_saturation, :hsl_color, :hsl_luminosity] (:none) the composite blend operator used when applying this image. See Blend Modes at http://cairographics.org/operators. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion} # @option opts blend [:none, :multiply, :screen, :overlay, :darken, :lighten, :color_dodge, :color_burn, :hard_light, :soft_light, :difference, :exclusion, :hsl_hue, :hsl_saturation, :hsl_color, :hsl_luminosity] (:none) the composite blend operator used when applying this image. See Blend Modes at http://cairographics.org/operators. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
# @option opts angle [FixNum] (0) Rotation of the in radians. Note that this rotates around the upper-left corner, making the placement of x-y coordinates slightly tricky. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion} # @option opts angle [FixNum] (0) Rotation of the in radians. Note that this rotates around the upper-left corner, making the placement of x-y coordinates slightly tricky. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
# @api public # @api public
def svg(opts) def svg(opts = {})
opts = { width: 32, height: 32 }.merge(opts) # TODO: add input validation for key here
opts = Squib::SYSTEM_DEFAULTS.merge(opts) range = Args::CardRange.new(opts[:range], deck_size: @deck_size)
# TODO: add input validation here. We need the key for example. paint = Args::Paint.new(@custom_colors).load!(opts, expand_by: @deck_size, layout: @layout)
rule = {type: :svg}.merge(opts) box = Args::Box.new(self, {width: :native, height: :native}).load!(opts, expand_by: @deck_size, layout: @layout, dpi: @dpi)
rule[:draw] = Proc.new do |card, x,y| adjust= Args::EmbedAdjust.new.load!(opts, expand_by: @deck_size, layout: @layout, dpi: @dpi)
card.svg(rule[:file], rule[:data], rule[:id], x, y, rule[:width], rule[:height], trans = Args::Transform.new.load!(opts, expand_by: @deck_size, layout: @layout, dpi: @dpi)
rule[:alpha], rule[:blend], rule[:angle], rule[:mask]) ifile = Args::InputFile.new.load!(opts, expand_by: @deck_size, layout: @layout, dpi: @dpi)
svg_args = Args::SvgSpecial.new.load!(opts, expand_by: @deck_size, layout: @layout, dpi: @dpi)
rule = { type: :png, file: ifile, box: box, paint: paint, trans: trans, adjust: adjust }
rule[:draw] = Proc.new do |card, x, y|
i = card.index
b = box[i]
b.x, b.y = x, y
card.svg(ifile[i].file, svg_args[i], b, paint[i], trans[i])
end end
@rules[opts[:key]] = rule @rules[opts[:key]] = rule
end end
@ -42,22 +60,28 @@ module Squib
# @option opts key [String] ('*') the string to replace with the graphic. Can be multiple letters, e.g. ':tool:' # @option opts key [String] ('*') the string to replace with the graphic. Can be multiple letters, e.g. ':tool:'
# @option opts file [String] ('') 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:README.md#Specifying_Files Specifying Files}. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion} # @option opts file [String] ('') 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:README.md#Specifying_Files Specifying Files}. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
# @option opts layout [String, Symbol] (nil) entry in the layout to use as defaults for this command. See {file:README.md#Custom_Layouts Custom Layouts}. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion} # @option opts layout [String, Symbol] (nil) entry in the layout to use as defaults for this command. See {file:README.md#Custom_Layouts Custom Layouts}. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
# @option opts width [Fixnum] (32) the width of the image rendered # @option opts width [Fixnum] (:native) the width of the image rendered
# @option opts height [Fixnum] (32) the height of the image rendered # @option opts height [Fixnum] (:native) the height of the image rendered
# @option opts dx [Integer] (0) "delta x", or adjust the icon horizontally by x pixels # @option opts dx [Integer] (0) "delta x", or adjust the icon horizontally by x pixels
# @option opts dy [Integer] (0) "delta y", or adjust the icon vertically by y pixels # @option opts dy [Integer] (0) "delta y", or adjust the icon vertically by y pixels
# @option opts alpha [Decimal] (1.0) the alpha-transparency percentage used to blend this image. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion} # @option opts alpha [Decimal] (1.0) the alpha-transparency percentage used to blend this image. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
# @option opts blend [:none, :multiply, :screen, :overlay, :darken, :lighten, :color_dodge, :color_burn, :hard_light, :soft_light, :difference, :exclusion, :hsl_hue, :hsl_saturation, :hsl_color, :hsl_luminosity] (:none) the composite blend operator used when applying this image. See Blend Modes at http://cairographics.org/operators. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion} # @option opts blend [:none, :multiply, :screen, :overlay, :darken, :lighten, :color_dodge, :color_burn, :hard_light, :soft_light, :difference, :exclusion, :hsl_hue, :hsl_saturation, :hsl_color, :hsl_luminosity] (:none) the composite blend operator used when applying this image. See Blend Modes at http://cairographics.org/operators. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
# @option opts angle [FixNum] (0) Rotation of the in radians. Note that this rotates around the upper-left corner, making the placement of x-y coordinates slightly tricky. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion} # @option opts angle [FixNum] (0) Rotation of the in radians. Note that this rotates around the upper-left corner, making the placement of x-y coordinates slightly tricky. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
# @api public # @api public
def png(opts) def png(opts = {})
opts = { width: 32, height: 32 }.merge(opts) # TODO: add input validation for key here
opts = Squib::SYSTEM_DEFAULTS.merge(opts) range = Args::CardRange.new(opts[:range], deck_size: @deck_size)
# TODO: add input validation here. We need the key for example. paint = Args::Paint.new(@custom_colors).load!(opts, expand_by: @deck_size, layout: @layout)
rule = {type: :png}.merge(opts) box = Args::Box.new(self, {width: :native, height: :native}).load!(opts, expand_by: @deck_size, layout: @layout, dpi: @dpi)
rule[:draw] = Proc.new do |card, x,y| adjust= Args::EmbedAdjust.new.load!(opts, expand_by: @deck_size, layout: @layout, dpi: @dpi)
card.png(rule[:file], x, y, rule[:width], rule[:height], trans = Args::Transform.new.load!(opts, expand_by: @deck_size, layout: @layout, dpi: @dpi)
rule[:alpha], rule[:blend], rule[:angle], rule[:mask]) ifile = Args::InputFile.new.load!(opts, expand_by: @deck_size, layout: @layout, dpi: @dpi)
rule = { type: :png, file: ifile, box: box, paint: paint, trans: trans, adjust: adjust }
rule[:draw] = Proc.new do |card, x, y|
i = card.index
b = box[i]
b.x, b.y = x, y
card.png(ifile[i].file, b, paint[i], trans[i])
end end
@rules[opts[:key]] = rule @rules[opts[:key]] = rule
end end

6
lib/squib/args/box.rb

@ -14,7 +14,7 @@ module Squib
def self.parameters def self.parameters
{ x: 0, y: 0, { x: 0, y: 0,
width: :native, height: :native, width: :deck, height: :deck,
radius: nil, x_radius: 0, y_radius: 0 radius: nil, x_radius: 0, y_radius: 0
} }
end end
@ -29,13 +29,13 @@ module Squib
def validate_width(arg, _i) def validate_width(arg, _i)
return arg if @deck.nil? return arg if @deck.nil?
return @deck.width if arg == :native return @deck.width if arg == :deck
arg arg
end end
def validate_height(arg, _i) def validate_height(arg, _i)
return arg if @deck.nil? return arg if @deck.nil?
return @deck.height if arg == :native return @deck.height if arg == :deck
arg arg
end end

4
lib/squib/args/card_range.rb

@ -13,6 +13,10 @@ module Squib
@range.each { |i| block.call(i) } @range.each { |i| block.call(i) }
end end
def size
@range.size
end
private private
def validate(input, deck_size) def validate(input, deck_size)
input ||= :all # default input ||= :all # default

25
lib/squib/args/embed_adjust.rb

@ -0,0 +1,25 @@
require 'squib/args/arg_loader'
module Squib
# @api private
module Args
class EmbedAdjust
include ArgLoader
def self.parameters
{ dx: 0, dy: 0 }
end
def self.expanding_parameters
parameters.keys # all of them
end
def self.params_with_units
parameters.keys # all of them
end
end
end
end

34
lib/squib/args/input_file.rb

@ -0,0 +1,34 @@
require 'squib/args/arg_loader'
module Squib
# @api private
module Args
class InputFile
include ArgLoader
def initialize
end
def self.parameters
{ file: nil }
end
def self.expanding_parameters
parameters.keys # all of them
end
def self.params_with_units
[] # none of them
end
def validate_file(arg, _i)
return nil if arg.nil?
raise "File #{File.expand_path(arg)} does not exist!" unless File.exists?(arg)
File.expand_path(arg)
end
end
end
end

37
lib/squib/args/svg_special.rb

@ -0,0 +1,37 @@
require 'squib/args/arg_loader'
module Squib
# @api private
module Args
class SvgSpecial
include ArgLoader
def self.parameters
{ data: nil, id: nil, force_id: false }
end
def self.expanding_parameters
parameters.keys # all of them
end
def self.params_with_units
[]
end
def validate_id(arg, _i)
return nil if arg.nil?
arg = '#' << arg unless arg.start_with? '#'
arg
end
# Only render if we have an ID specified, or we are forcing an ID
def render?(i)
return false if force_id[i] && id[i].to_s.empty?
return true
end
end
end
end

3
lib/squib/card.rb

@ -8,7 +8,7 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
attr_reader :width, :height, :backend, :svgfile attr_reader :width, :height, :backend, :svgfile, :index
# :nodoc: # :nodoc:
# @api private # @api private
@ -21,6 +21,7 @@ module Squib
@width = width @width = width
@height = height @height = height
@backend = deck.backend @backend = deck.backend
@index = index
@svgfile = "#{deck.dir}/#{deck.prefix}#{deck.count_format % index}.svg" @svgfile = "#{deck.dir}/#{deck.prefix}#{deck.count_format % index}.svg"
@cairo_surface = make_surface(@svgfile, @backend) @cairo_surface = make_surface(@svgfile, @backend)
@cairo_context = Squib::Graphics::CairoContextWrapper.new(Cairo::Context.new(@cairo_surface)) @cairo_context = Squib::Graphics::CairoContextWrapper.new(Cairo::Context.new(@cairo_surface))

64
lib/squib/graphics/image.rb

@ -14,56 +14,56 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def png(file, x, y, width, height, alpha, blend, angle, mask) def png(file, box, paint, trans)
Squib.logger.debug {"Rendering: #{file} @#{x},#{y} #{width}x#{height}, alpha: #{alpha}, blend: #{blend}, angle: #{angle}, mask: #{mask}"} Squib.logger.debug {"RENDERING PNG: \n file: #{file}\n box: #{box}\n paint: #{paint}\n trans: #{trans}"}
return if file.nil? or file.eql? '' return if file.nil? or file.eql? ''
png = Squib.cache_load_image(file) png = Squib.cache_load_image(file)
use_cairo do |cc| use_cairo do |cc|
cc.translate(x, y) cc.translate(box.x, box.y)
if width != :native || height != :native if box.width != :native || box.height != :native
width == :native && width = png.width.to_f box.width == :native && box.width = png.width.to_f
height == :native && height = png.height.to_f box.height == :native && box.height = png.height.to_f
Squib.logger.warn "PNG scaling results in antialiasing." Squib.logger.warn "PNG scaling results in antialiasing."
cc.scale(width.to_f / png.width.to_f, height.to_f / png.height.to_f) cc.scale(box.width.to_f / png.width.to_f, box.height.to_f / png.height.to_f)
end end
cc.rotate(angle) cc.rotate(trans.angle)
cc.translate(-x, -y) cc.translate(-box.x, -box.y)
cc.set_source(png, x, y) cc.set_source(png, box.x, box.y)
cc.operator = blend unless blend == :none cc.operator = paint.blend unless paint.blend == :none
if mask.nil? if paint.mask.empty?
cc.paint(alpha) cc.paint(paint.alpha)
else else
cc.set_source_squibcolor(mask) cc.set_source_squibcolor(paint.mask)
cc.mask(png, x, y) cc.mask(png, box.x, box.y)
end end
end end
end end
# :nodoc: # :nodoc:
# @api private # @api private
def svg(file, data, id, x, y, width, height, alpha, blend, angle, mask) def svg(file, svg_args, box, paint, trans)
Squib.logger.debug {"Rendering: #{file}, id: #{id} @#{x},#{y} #{width}x#{height}, alpha: #{alpha}, blend: #{blend}, angle: #{angle}, mask: #{mask}"} Squib.logger.debug {"Rendering: #{file}, id: #{id} @#{x},#{y} #{width}x#{height}, alpha: #{alpha}, blend: #{blend}, angle: #{angle}, mask: #{mask}"}
Squib.logger.warn 'Both an SVG file and SVG data were specified' unless file.to_s.empty? or data.to_s.empty? Squib.logger.warn 'Both an SVG file and SVG data were specified' unless file.to_s.empty? || svg_args.data.to_s.empty?
return if (file.nil? or file.eql? '') and data.nil? # nothing specified return if (file.nil? or file.eql? '') and svg_args.data.nil? # nothing specified TODO Move this out to arg validator
data = File.read(file) if data.to_s.empty? svg_args.data = File.read(file) if svg_args.data.to_s.empty?
svg = RSVG::Handle.new_from_data(data) svg = RSVG::Handle.new_from_data(svg_args.data)
width = svg.width if width == :native box.width = svg.width if box.width == :native
height = svg.height if height == :native box.height = svg.height if box.height == :native
scale_width = width.to_f / svg.width.to_f scale_width = box.width.to_f / svg.width.to_f
scale_height = height.to_f / svg.height.to_f scale_height = box.height.to_f / svg.height.to_f
use_cairo do |cc| use_cairo do |cc|
cc.translate(x, y) cc.translate(box.x, box.y)
cc.rotate(angle) cc.rotate(trans.angle)
cc.scale(scale_width, scale_height) cc.scale(scale_width, scale_height)
cc.operator = blend unless blend == :none cc.operator = paint.blend unless paint.blend == :none
#FIXME Alpha is no longer used since we are not using cc.paint anymore #FIXME Alpha is no longer used since we are not using cc.paint anymore
if mask.nil? if paint.mask.to_s.empty?
cc.render_rsvg_handle(svg, id) cc.render_rsvg_handle(svg, svg_args.id)
else else
tmp = Cairo::ImageSurface.new(width / scale_width, height / scale_height) tmp = Cairo::ImageSurface.new(box.width / scale_width, box.height / scale_height)
tmp_cc = Cairo::Context.new(tmp) tmp_cc = Cairo::Context.new(tmp)
tmp_cc.render_rsvg_handle(svg, id) tmp_cc.render_rsvg_handle(svg, svg_args.id)
cc.set_source_squibcolor(mask) cc.set_source_squibcolor(paint.mask)
cc.mask(tmp, 0, 0) cc.mask(tmp, 0, 0)
end end
end end

12
lib/squib/graphics/text.rb

@ -85,18 +85,18 @@ module Squib
searches = [] searches = []
while (key = next_embed(embed.rules.keys, clean_str)) != nil while (key = next_embed(embed.rules.keys, clean_str)) != nil
rule = embed.rules[key] rule = embed.rules[key]
spacing = rule[:width] * Pango::SCALE spacing = rule[:box].width[@index] * Pango::SCALE
index = clean_str.index(key) kindex = clean_str.index(key)
index = clean_str[0..index].bytesize #convert to byte index (bug #57) kindex = clean_str[0..kindex].bytesize #convert to byte index (bug #57)
str = str.sub(key, "<span size=\"#{ZERO_WIDTH_CHAR_SIZE}\">a<span letter_spacing=\"#{spacing.to_i}\">a</span>a</span>") str = str.sub(key, "<span size=\"#{ZERO_WIDTH_CHAR_SIZE}\">a<span letter_spacing=\"#{spacing.to_i}\">a</span>a</span>")
layout.markup = str layout.markup = str
clean_str = layout.text clean_str = layout.text
searches << { index: index, rule: rule } searches << { index: kindex, rule: rule }
end end
searches.each do |search| searches.each do |search|
rect = layout.index_to_pos(search[:index]) rect = layout.index_to_pos(search[:index])
x = Pango.pixels(rect.x) + search[:rule][:dx] x = Pango.pixels(rect.x) + search[:rule][:adjust].dx[@index]
y = Pango.pixels(rect.y) + search[:rule][:dy] y = Pango.pixels(rect.y) + search[:rule][:adjust].dy[@index]
draw_calls << {x: x, y: y, draw: search[:rule][:draw]} # defer drawing until we've valigned draw_calls << {x: x, y: y, draw: search[:rule][:draw]} # defer drawing until we've valigned
end end
return draw_calls return draw_calls

10
samples/embed_text.rb

@ -77,14 +77,12 @@ Squib::Deck.new do
end end
Squib::Deck.new(cards: 3) do Squib::Deck.new(cards: 3) do
str = 'Take 1 :tool: and gain 2 :health:.'
embed_text = 'Take 1 :tool: and gain 2 :health:.' text(str: str, font: 'Sans', font_size: [18, 26, 35],
text(str: embed_text, font: 'Sans', font_size: [18, 32, 45],
x: 0, y: 0, width: 180, height: 300, valign: :bottom, x: 0, y: 0, width: 180, height: 300, valign: :bottom,
align: :left, ellipsize: false, justify: false, hint: :cyan) do |embed| align: :left, ellipsize: false, justify: false, hint: :cyan) do |embed|
embed.svg key: ':tool:', width: 28, height: 28, file: 'spanner.svg' embed.svg key: ':tool:', width: [28, 42, 56], height: [28, 42, 56], file: 'spanner.svg'
embed.svg key: ':health:', width: 28, height: 28, file: 'glass-heart.svg' embed.svg key: ':health:', width: [28, 42, 56], height: [28, 42, 56], file: 'glass-heart.svg'
end end
save_png prefix: 'embed_multi_'
save_sheet prefix: 'embed_multisheet_', columns: 3 save_sheet prefix: 'embed_multisheet_', columns: 3
end end

14
spec/args/box_spec.rb

@ -3,7 +3,7 @@ require 'squib/args/box'
describe Squib::Args::Box do describe Squib::Args::Box do
subject(:box) { Squib::Args::Box.new } subject(:box) { Squib::Args::Box.new }
let(:expected_defaults) { {x: [0], y: [0], width: [:native], height: [:native] } } let(:expected_defaults) { {x: [0], y: [0], width: [:deck], height: [:deck] } }
it 'intitially has no params set' do it 'intitially has no params set' do
expect(box).not_to respond_to(:x, :y, :width, :height) expect(box).not_to respond_to(:x, :y, :width, :height)
@ -16,7 +16,7 @@ describe Squib::Args::Box do
it 'extracts what is specified and fills in defaults from Box' do it 'extracts what is specified and fills in defaults from Box' do
box.load!(x: 4, width: 40) box.load!(x: 4, width: 40)
expect(box).to have_attributes(x: [4], width: [40], y: [0], height: [:native]) expect(box).to have_attributes(x: [4], width: [40], y: [0], height: [:deck])
end end
it 'extracts the defaults from Box on an empty hash' do it 'extracts the defaults from Box on an empty hash' do
@ -32,8 +32,8 @@ describe Squib::Args::Box do
expect(box).to have_attributes({ expect(box).to have_attributes({
x: [1, 2], x: [1, 2],
y: [3, 3], y: [3, 3],
height: [:native, :native], height: [:deck, :deck],
width: [:native, :native] width: [:deck, :deck]
}) })
end end
@ -41,8 +41,8 @@ describe Squib::Args::Box do
expect(box[0]).to have_attributes({ expect(box[0]).to have_attributes({
x: 1, x: 1,
y: 3, y: 3,
height: :native, height: :deck,
width: :native width: :deck
}) })
end end
end end
@ -98,7 +98,7 @@ describe Squib::Args::Box do
context 'validation' do context 'validation' do
it 'replaces with deck width and height' do it 'replaces with deck width and height' do
args = {width: :native, height: :native} args = {width: :deck, height: :deck}
deck = OpenStruct.new(width: 123, height: 456) deck = OpenStruct.new(width: 123, height: 456)
box = Squib::Args::Box.new(deck) box = Squib::Args::Box.new(deck)
box.load!(args, expand_by: 1) box.load!(args, expand_by: 1)

7
spec/data/samples/embed_text.rb.txt

@ -240,7 +240,7 @@ cairo: set_source_color(["black"])
cairo: translate([0, 0]) cairo: translate([0, 0])
cairo: rotate([0]) cairo: rotate([0])
cairo: move_to([0, 0]) cairo: move_to([0, 0])
pango font: size=([32768]) pango font: size=([26624])
pango: font_description=([MockDouble]) pango: font_description=([MockDouble])
pango: text=(["Take 1 :tool: and gain 2 :health:."]) pango: text=(["Take 1 :tool: and gain 2 :health:."])
pango: width=([184320]) pango: width=([184320])
@ -265,7 +265,7 @@ cairo: set_source_color(["black"])
cairo: translate([0, 0]) cairo: translate([0, 0])
cairo: rotate([0]) cairo: rotate([0])
cairo: move_to([0, 0]) cairo: move_to([0, 0])
pango font: size=([46080]) pango font: size=([35840])
pango: font_description=([MockDouble]) pango: font_description=([MockDouble])
pango: text=(["Take 1 :tool: and gain 2 :health:."]) pango: text=(["Take 1 :tool: and gain 2 :health:."])
pango: width=([184320]) pango: width=([184320])
@ -285,9 +285,6 @@ cairo: set_source_color([:cyan])
cairo: set_line_width([2.0]) cairo: set_line_width([2.0])
cairo: stroke([]) cairo: stroke([])
cairo: restore([]) cairo: restore([])
surface: write_to_png(["_output/embed_multi_00.png"])
surface: write_to_png(["_output/embed_multi_01.png"])
surface: write_to_png(["_output/embed_multi_02.png"])
cairo: set_source([MockDouble, 0, 0]) cairo: set_source([MockDouble, 0, 0])
cairo: paint([]) cairo: paint([])
cairo: set_source([MockDouble, 100, 0]) cairo: set_source([MockDouble, 100, 0])

16
spec/data/samples/hand.rb.txt

@ -221,6 +221,8 @@ cairo: rotate([0])
cairo: move_to([0, 0]) cairo: move_to([0, 0])
pango: font_description=([MockDouble]) pango: font_description=([MockDouble])
pango: text=(["A"]) pango: text=(["A"])
pango: width=([204800])
pango: height=([204800])
pango: wrap=([#<Pango::Layout::WrapMode word-char>]) pango: wrap=([#<Pango::Layout::WrapMode word-char>])
pango: ellipsize=([#<Pango::Layout::EllipsizeMode end>]) pango: ellipsize=([#<Pango::Layout::EllipsizeMode end>])
pango: alignment=([#<Pango::Layout::Alignment left>]) pango: alignment=([#<Pango::Layout::Alignment left>])
@ -238,6 +240,8 @@ cairo: rotate([0])
cairo: move_to([0, 0]) cairo: move_to([0, 0])
pango: font_description=([MockDouble]) pango: font_description=([MockDouble])
pango: text=(["B"]) pango: text=(["B"])
pango: width=([204800])
pango: height=([204800])
pango: wrap=([#<Pango::Layout::WrapMode word-char>]) pango: wrap=([#<Pango::Layout::WrapMode word-char>])
pango: ellipsize=([#<Pango::Layout::EllipsizeMode end>]) pango: ellipsize=([#<Pango::Layout::EllipsizeMode end>])
pango: alignment=([#<Pango::Layout::Alignment left>]) pango: alignment=([#<Pango::Layout::Alignment left>])
@ -255,6 +259,8 @@ cairo: rotate([0])
cairo: move_to([0, 0]) cairo: move_to([0, 0])
pango: font_description=([MockDouble]) pango: font_description=([MockDouble])
pango: text=(["C"]) pango: text=(["C"])
pango: width=([204800])
pango: height=([204800])
pango: wrap=([#<Pango::Layout::WrapMode word-char>]) pango: wrap=([#<Pango::Layout::WrapMode word-char>])
pango: ellipsize=([#<Pango::Layout::EllipsizeMode end>]) pango: ellipsize=([#<Pango::Layout::EllipsizeMode end>])
pango: alignment=([#<Pango::Layout::Alignment left>]) pango: alignment=([#<Pango::Layout::Alignment left>])
@ -272,6 +278,8 @@ cairo: rotate([0])
cairo: move_to([0, 0]) cairo: move_to([0, 0])
pango: font_description=([MockDouble]) pango: font_description=([MockDouble])
pango: text=(["D"]) pango: text=(["D"])
pango: width=([204800])
pango: height=([204800])
pango: wrap=([#<Pango::Layout::WrapMode word-char>]) pango: wrap=([#<Pango::Layout::WrapMode word-char>])
pango: ellipsize=([#<Pango::Layout::EllipsizeMode end>]) pango: ellipsize=([#<Pango::Layout::EllipsizeMode end>])
pango: alignment=([#<Pango::Layout::Alignment left>]) pango: alignment=([#<Pango::Layout::Alignment left>])
@ -289,6 +297,8 @@ cairo: rotate([0])
cairo: move_to([0, 0]) cairo: move_to([0, 0])
pango: font_description=([MockDouble]) pango: font_description=([MockDouble])
pango: text=(["E"]) pango: text=(["E"])
pango: width=([204800])
pango: height=([204800])
pango: wrap=([#<Pango::Layout::WrapMode word-char>]) pango: wrap=([#<Pango::Layout::WrapMode word-char>])
pango: ellipsize=([#<Pango::Layout::EllipsizeMode end>]) pango: ellipsize=([#<Pango::Layout::EllipsizeMode end>])
pango: alignment=([#<Pango::Layout::Alignment left>]) pango: alignment=([#<Pango::Layout::Alignment left>])
@ -306,6 +316,8 @@ cairo: rotate([0])
cairo: move_to([0, 0]) cairo: move_to([0, 0])
pango: font_description=([MockDouble]) pango: font_description=([MockDouble])
pango: text=(["F"]) pango: text=(["F"])
pango: width=([204800])
pango: height=([204800])
pango: wrap=([#<Pango::Layout::WrapMode word-char>]) pango: wrap=([#<Pango::Layout::WrapMode word-char>])
pango: ellipsize=([#<Pango::Layout::EllipsizeMode end>]) pango: ellipsize=([#<Pango::Layout::EllipsizeMode end>])
pango: alignment=([#<Pango::Layout::Alignment left>]) pango: alignment=([#<Pango::Layout::Alignment left>])
@ -323,6 +335,8 @@ cairo: rotate([0])
cairo: move_to([0, 0]) cairo: move_to([0, 0])
pango: font_description=([MockDouble]) pango: font_description=([MockDouble])
pango: text=(["G"]) pango: text=(["G"])
pango: width=([204800])
pango: height=([204800])
pango: wrap=([#<Pango::Layout::WrapMode word-char>]) pango: wrap=([#<Pango::Layout::WrapMode word-char>])
pango: ellipsize=([#<Pango::Layout::EllipsizeMode end>]) pango: ellipsize=([#<Pango::Layout::EllipsizeMode end>])
pango: alignment=([#<Pango::Layout::Alignment left>]) pango: alignment=([#<Pango::Layout::Alignment left>])
@ -340,6 +354,8 @@ cairo: rotate([0])
cairo: move_to([0, 0]) cairo: move_to([0, 0])
pango: font_description=([MockDouble]) pango: font_description=([MockDouble])
pango: text=(["H"]) pango: text=(["H"])
pango: width=([204800])
pango: height=([204800])
pango: wrap=([#<Pango::Layout::WrapMode word-char>]) pango: wrap=([#<Pango::Layout::WrapMode word-char>])
pango: ellipsize=([#<Pango::Layout::EllipsizeMode end>]) pango: ellipsize=([#<Pango::Layout::EllipsizeMode end>])
pango: alignment=([#<Pango::Layout::Alignment left>]) pango: alignment=([#<Pango::Layout::Alignment left>])

Loading…
Cancel
Save