Browse Source

Add support for embedding pngs

Refactored into passing a Proc. I love those things.
dev
Andy Meneely 11 years ago
parent
commit
ac2b46921d
  1. 31
      lib/squib/api/text_embed.rb
  2. 3
      lib/squib/graphics/text.rb
  3. 5
      samples/embed_text.rb

31
lib/squib/api/text_embed.rb

@ -10,7 +10,7 @@ module Squib
@rules = {} # store an array of options for later usage @rules = {} # store an array of options for later usage
end end
# Context object for embedding text within a string # Context object for embedding an svg icon within text
# #
# @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 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}
@ -25,7 +25,34 @@ module Squib
def svg(opts) def svg(opts)
opts = Squib::SYSTEM_DEFAULTS.merge(opts) opts = Squib::SYSTEM_DEFAULTS.merge(opts)
# TODO: add input validation here. We need the key for example. # TODO: add input validation here. We need the key for example.
@rules[opts[:key]] = {type: :svg}.merge(opts) rule = {type: :svg}.merge(opts)
rule[:draw] = Proc.new do |card, x,y|
card.svg(rule[:file], rule[:id], x, y, rule[:width], rule[:height],
rule[:alpha], rule[:blend], rule[:angle], rule[:mask])
end end
@rules[opts[:key]] = rule
end
# Context object for embedding a png within text
#
# @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 width [Integer, :native] (:native) the width of the image rendered
# @option opts height [Integer, :native] the height the height of the image rendered
# @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 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
def png(opts)
opts = Squib::SYSTEM_DEFAULTS.merge(opts)
# TODO: add input validation here. We need the key for example.
rule = {type: :png}.merge(opts)
rule[:draw] = Proc.new do |card, x,y|
card.png(rule[:file], x, y, rule[:width], rule[:height],
rule[:alpha], rule[:blend], rule[:angle], rule[:mask])
end
@rules[opts[:key]] = rule
end
end end
end end

3
lib/squib/graphics/text.rb

@ -120,8 +120,7 @@ module Squib
end end
x = Pango.pixels(rect.x + (letter_width / 2)) x = Pango.pixels(rect.x + (letter_width / 2))
y = Pango.pixels(rect.y) y = Pango.pixels(rect.y)
svg(rule[:file], rule[:id], x, y, rule[:width], rule[:height], rule[:draw].call(self, x, y)
rule[:alpha], rule[:blend], rule[:angle], SYSTEM_DEFAULTS[:mask])
end end
end end

5
samples/embed_text.rb

@ -5,12 +5,13 @@ Squib::Deck.new do
rect x: 0, y: 0, width: 825, height: 1125 rect x: 0, y: 0, width: 825, height: 1125
rect x: 0, y: 0, width: 180, height: 180, stroke_color: :red rect x: 0, y: 0, width: 180, height: 180, stroke_color: :red
embed_text = 'Take 1 :tool:and gain 2 :health:. Take 2 :tool: if level 2' embed_text = 'Take 1 :tool:and gain 2 :health:. Take 2 :tool: and gain 3 :purse: if level 2.'
text(str: embed_text, font: 'Sans 18', text(str: embed_text, font: 'Sans 18',
x: 0, y: 0, width: 180, x: 0, y: 0, width: 180,
align: :right, ellipsize: false, justify: false) do |embed| align: :left, ellipsize: false, justify: false) do |embed|
embed.svg key: ':tool:', width: 28, height: 28, file: 'spanner.svg' embed.svg key: ':tool:', width: 28, height: 28, file: 'spanner.svg'
embed.svg key: ':health:', width: 28, height: 28, file: 'glass-heart.svg' embed.svg key: ':health:', width: 28, height: 28, file: 'glass-heart.svg'
embed.png key: ':purse:', width: 28, height: 28, file: 'shiny-purse.png'
end end
save_png prefix: 'embed_' save_png prefix: 'embed_'

Loading…
Cancel
Save