Class: Squib::Deck
- Inherits:
-
Object
- Object
- Squib::Deck
- Includes:
- Enumerable
- Defined in:
- lib/squib/deck.rb,
lib/squib/api/data.rb,
lib/squib/api/save.rb,
lib/squib/api/text.rb,
lib/squib/api/image.rb,
lib/squib/api/units.rb,
lib/squib/api/shapes.rb,
lib/squib/api/settings.rb,
lib/squib/api/background.rb,
lib/squib/graphics/save_doc.rb,
lib/squib/graphics/showcase.rb
Overview
The main interface to Squib. Provides a front-end porcelain whereas the Card class interacts with the graphics plumbing.
Instance Method Summary collapse
-
#background(opts = {}) ⇒ nil
Fills the background with the given color Options support Arrays, see Arrays and Singleon Expansion.
-
#circle(opts = {}) ⇒ nil
Draw a circle centered at the given coordinates.
-
#cm(n) ⇒ Decimal
Given cm, returns the number of pixels according to the deck's DPI.
-
#csv(opts = {}) ⇒ Object
Convenience call on deck goes to the module function.
-
#hint(text: :off) ⇒ nil
Toggle hints globally.
-
#inches(n) ⇒ Decimal
Given inches, returns the number of pixels according to the deck's DPI.
-
#initialize(width: 825, height: 1125, cards: 1, dpi: 300, config: 'config.yml', layout: nil, &block) ⇒ Deck
constructor
Squib's constructor that sets the immutable properties.
-
#line(opts = {}) ⇒ nil
Draw a line using the given coordinates.
- #perspective(src, scale, face_right) ⇒ Object
-
#png(opts = {}) ⇒ nil
Renders a png file at the given location.
-
#rect(opts = {}) ⇒ nil
Draw a rounded rectangle.
-
#save(opts = {}) ⇒ Object
Saves the given range of cards to either PNG or PDF.
-
#save_pdf(opts = {}) ⇒ nil
Lays out the cards in range and renders a PDF.
-
#save_png(opts = {}) ⇒ nil
Saves the given range of cards to a PNG.
-
#save_sheet(opts = {}) ⇒ nil
Lays out the cards in range and renders a stitched PNG sheet.
-
#set(opts = {}) ⇒ nil
Sets various defaults for this deck.
-
#showcase(opts = {}) ⇒ nil
Renders a range of files in a showcase as if they are sitting on a reflective surface See showcase for full example.
-
#svg(opts = {}) ⇒ nil
Renders an entire svg file at the given location.
-
#text(opts = {}) ⇒ Array
Renders a string at a given location, width, alignment, font, etc.
-
#triangle(opts = {}) ⇒ nil
Draw a triangle using the given coordinates.
-
#xlsx(opts = {}) ⇒ Object
Convenience call on deck goes to the module function.
Constructor Details
#initialize(width: 825, height: 1125, cards: 1, dpi: 300, config: 'config.yml', layout: nil, &block) ⇒ Deck
Squib's constructor that sets the immutable properties.
This is the starting point for Squib. In providing a block to the constructor, you have access to all of Deck's instance methods.
The documented methods in Deck are the ones intended for use by most users.
If your game requires multiple different sizes or orientations, I recommend using multiple Squib::Deck
s in your deck.rb
. You can modify the internals of Squib::Deck
(e.g. @cards
), but that's not recommended.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/squib/deck.rb', line 58 def initialize(width: 825, height: 1125, cards: 1, dpi: 300, config: 'config.yml', layout: nil, &block) @dpi = dpi @width = Args::UnitConversion.parse width, dpi @height = Args::UnitConversion.parse height, dpi @font = Squib::SYSTEM_DEFAULTS[:default_font] @cards = [] @custom_colors = {} @img_dir = '.' @progress_bar = Squib::Progress.new(false) @text_hint = :off cards.times{ @cards << Squib::Card.new(self, @width, @height) } show_info(config, layout) load_config(config) @layout = LayoutParser.load_layout(layout) if block_given? instance_eval(&block) # here we go. wheeeee! end end |
Instance Method Details
#background(opts = {}) ⇒ nil
Fills the background with the given color Options support Arrays, see Arrays and Singleon Expansion
13 14 15 16 |
# File 'lib/squib/api/background.rb', line 13 def background(opts = {}) opts = needs(opts,[:range, :color]) opts[:range].each { |i| @cards[i].background(opts[:color][i]) } end |
#circle(opts = {}) ⇒ nil
Draw a circle centered at the given coordinates
Options support Arrays, see Arrays and Singleon Expansion
52 53 54 55 56 57 58 59 60 |
# File 'lib/squib/api/shapes.rb', line 52 def circle(opts = {}) opts = {radius: 100}.merge(opts) # overriding the non-system default opts = needs(opts, [:range, :x, :y, :circle_radius, :layout, :fill_color, :stroke_color, :stroke_width]) opts[:range].each do |i| @cards[i].circle(opts[:x][i], opts[:y][i], opts[:radius][i], opts[:fill_color][i], opts[:stroke_color][i], opts[:stroke_width][i]) end end |
#cm(n) ⇒ Decimal
Given cm, returns the number of pixels according to the deck's DPI.
26 27 28 |
# File 'lib/squib/api/units.rb', line 26 def cm(n) @dpi * Squib::INCHES_IN_CM * n.to_f end |
#csv(opts = {}) ⇒ Object
Convenience call on deck goes to the module function
94 95 96 |
# File 'lib/squib/api/data.rb', line 94 def csv(opts = {}) Squib.csv(opts) end |
#hint(text: :off) ⇒ nil
Toggle hints globally.
Text hints are rectangles around where the text will be laid out. They are intended to be temporary. Setting a hint to nil or to :off will disable hints. @see samples/text.rb
15 16 17 |
# File 'lib/squib/api/settings.rb', line 15 def hint(text: :off) @text_hint = text end |
#inches(n) ⇒ Decimal
Given inches, returns the number of pixels according to the deck's DPI.
14 15 16 |
# File 'lib/squib/api/units.rb', line 14 def inches(n) @dpi * n.to_f end |
#line(opts = {}) ⇒ nil
Draw a line using the given coordinates
107 108 109 110 111 112 113 114 |
# File 'lib/squib/api/shapes.rb', line 107 def line(opts = {}) opts = needs(opts, [:range, :x1, :y1, :x2, :y2, :layout, :stroke_color, :stroke_width]) opts[:range].each do |i| @cards[i].line(opts[:x1][i], opts[:y1][i], opts[:x2][i], opts[:y2][i], opts[:stroke_color][i], opts[:stroke_width][i]) end end |
#perspective(src, scale, face_right) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/squib/graphics/showcase.rb', line 63 def perspective(src, scale, face_right) dest_cxt = Cairo::Context.new(Cairo::ImageSurface.new(src.width * scale, src.height)) in_thickness = 1 # Take strip 1 pixel-width at a time out_thickness = 3 # Scale it to 3 pixels wider to cover any gaps (0..src.width).step(in_thickness) do |i| percentage = i / src.width.to_f i = src.width - i if face_right factor = scale + (percentage * (1.0 - scale)) #linear interpolation dest_cxt.save dest_cxt.translate 0, src.height / 2.0 * (1.0 - factor) dest_cxt.scale factor * scale, factor dest_cxt.set_source src, 0, 0 dest_cxt.rounded_rectangle i, 0, out_thickness, src.height, 0,0 dest_cxt.fill dest_cxt.restore end return dest_cxt.target end |
#png(opts = {}) ⇒ nil
Renders a png file at the given location.
See samples/image.rb and samples/tgc-overlay.rb as examples.
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/squib/api/image.rb', line 23 def png(opts = {}) opts = needs(opts, [:range, :files, :x, :y, :width, :height, :alpha, :layout, :blend, :angle, :mask]) Dir.chdir(@img_dir) do @progress_bar.start('Loading PNG(s)', opts[:range].size) do || opts[:range].each do |i| @cards[i].png(opts[:file][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]) .increment end end end end |
#rect(opts = {}) ⇒ nil
Draw a rounded rectangle
Options support Arrays, see Arrays and Singleon Expansion
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/squib/api/shapes.rb', line 25 def rect(opts = {}) opts = needs(opts, [:range, :x, :y, :width, :height, :rect_radius, :x_radius, :y_radius, :fill_color, :stroke_color, :stroke_width, :layout]) opts[:range].each do |i| @cards[i].rect(opts[:x][i], opts[:y][i], opts[:width][i], opts[:height][i], opts[:x_radius][i], opts[:y_radius][i], opts[:fill_color][i], opts[:stroke_color][i], opts[:stroke_width][i]) end end |
#save(opts = {}) ⇒ Object
Saves the given range of cards to either PNG or PDF
13 14 15 16 17 18 |
# File 'lib/squib/api/save.rb', line 13 def save(opts = {}) opts = needs(opts, [:range, :creatable_dir, :formats, :prefix, :rotate]) save_png(opts) if opts[:format].include? :png save_pdf(opts) if opts[:format].include? :pdf self end |
#save_pdf(opts = {}) ⇒ nil
Lays out the cards in range and renders a PDF
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/squib/graphics/save_doc.rb', line 18 def save_pdf(opts = {}) opts = {width: 3300, height: 2550}.merge(opts) p = needs(opts, [:range, :paper_width, :paper_height, :file_to_save, :creatable_dir, :margin, :gap, :trim]) cc = Cairo::Context.new(Cairo::PDFSurface.new("#{p[:dir]}/#{p[:file]}", p[:width], p[:height])) x = p[:margin] y = p[:margin] @progress_bar.start("Saving PDF to #{p[:dir]}/#{p[:file]}", p[:range].size) do || p[:range].each do |i| surface = trim(@cards[i].cairo_surface, p[:trim], @width, @height) cc.set_source(surface, x, y) cc.paint .increment x += surface.width + p[:gap] if x > (p[:width] - surface.width - p[:margin]) x = p[:margin] y += surface.height + p[:gap] if y > (p[:height] - surface.height - p[:margin]) x = p[:margin] y = p[:margin] cc.show_page #next page end end end end end |
#save_png(opts = {}) ⇒ nil
Saves the given range of cards to a PNG
32 33 34 35 36 37 38 39 40 |
# File 'lib/squib/api/save.rb', line 32 def save_png(opts = {}) opts = needs(opts,[:range, :creatable_dir, :prefix, :count_format, :rotate]) @progress_bar.start("Saving PNGs to #{opts[:dir]}/#{opts[:prefix]}*", @cards.size) do || opts[:range].each do |i| @cards[i].save_png(i, opts[:dir], opts[:prefix], opts[:count_format], opts[:rotate], opts[:angle]) .increment end end end |
#save_sheet(opts = {}) ⇒ nil
Lays out the cards in range and renders a stitched PNG sheet
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/squib/graphics/save_doc.rb', line 60 def save_sheet(opts = {}) opts = {margin: 0}.merge(opts) # overriding the non-system default p = needs(opts, [:range, :prefix, :count_format, :creatable_dir, :margin, :gap, :trim, :rows, :columns]) # EXTRACT METHOD HERE sheet_width = (p[:columns] * (@width + 2 * p[:gap] - 2 * p[:trim])) + (2 * p[:margin]) sheet_height = (p[:rows] * (@height + 2 * p[:gap] - 2 * p[:trim])) + (2 * p[:margin]) cc = Cairo::Context.new(Cairo::ImageSurface.new(sheet_width, sheet_height)) num_this_sheet = 0 sheet_num = 0 x, y = p[:margin], p[:margin] @progress_bar.start("Saving PNG sheet to #{p[:dir]}/#{p[:prefix]}_*", @cards.size + 1) do || p[:range].each do |i| if num_this_sheet >= (p[:columns] * p[:rows]) # new sheet filename = "#{p[:dir]}/#{p[:prefix]}#{p[:count_format] % sheet_num}.png" cc.target.write_to_png(filename) new_sheet = false num_this_sheet = 0 sheet_num += 1 x, y = p[:margin], p[:margin] cc = Cairo::Context.new(Cairo::ImageSurface.new(sheet_width, sheet_height)) end surface = trim(@cards[i].cairo_surface, p[:trim], @width, @height) cc.set_source(surface, x, y) cc.paint .increment num_this_sheet += 1 x += surface.width + p[:gap] if num_this_sheet % p[:columns] == 0 # new row x = p[:margin] y += surface.height + p[:gap] end .increment end cc.target.write_to_png("#{p[:dir]}/#{p[:prefix]}#{p[:count_format] % sheet_num}.png") end end |
#set(opts = {}) ⇒ nil
Sets various defaults for this deck. Defaults can be overriden by the commands themselves when that command supports it.
30 31 32 33 34 |
# File 'lib/squib/api/settings.rb', line 30 def set(opts = {}) opts = needs(opts, [:font, :img_dir]) @font = opts[:font][0] #was expanded - just need the first @img_dir = opts[:img_dir] end |
#showcase(opts = {}) ⇒ nil
Renders a range of files in a showcase as if they are sitting on a reflective surface See showcase for full example
63 64 65 66 67 68 69 70 71 |
# File 'lib/squib/api/save.rb', line 63 def showcase(opts = {}) opts = {file: 'showcase.png', fill_color: :white}.merge(opts) opts = needs(opts,[:range, :margin, :trim, :trim_radius, :creatable_dir, :file_to_save, :face]) render_showcase(opts[:range], opts[:trim], opts[:trim_radius], opts[:scale], opts[:offset], opts[:fill_color], opts[:reflect_offset], opts[:reflect_percent], opts[:reflect_strength], opts[:margin], opts[:face], opts[:dir], opts[:file]) end |
#svg(opts = {}) ⇒ nil
Renders an entire svg file at the given location. Uses the SVG-specified units and DPI to determine the pixel width and height.
See samples/load-images.rb and samples/tgc-overlay.rb as examples.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/squib/api/image.rb', line 58 def svg(opts = {}) p = needs(opts,[:range, :files, :svgid, :force_svgid, :x, :y, :width, :height, :layout, :alpha, :blend, :angle, :mask]) Dir.chdir(@img_dir) do @progress_bar.start('Loading SVG(s)', p[:range].size) do || p[:range].each do |i| unless p[:force_id][i] && p[:id][i].to_s.empty? @cards[i].svg(p[:file][i], p[:id][i], p[:x][i], p[:y][i], p[:width][i], p[:height][i], p[:alpha][i], p[:blend][i], p[:angle][i],p[:mask][i]) end .increment end end end end |
#text(opts = {}) ⇒ Array
Renders a string at a given location, width, alignment, font, etc.
Unix-like newlines are interpreted even on Windows. See the samples/text.rb for a lengthy example.
Options support Arrays, see Arrays and Singleon Expansion
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/squib/api/text.rb', line 41 def text(opts = {}) opts = needs(opts, [:range, :str, :font, :font_size, :x, :y, :width, :height, :color, :wrap, :align, :justify, :spacing, :valign, :markup, :ellipsize, :hint, :layout, :angle]) extents = Array.new(@cards.size) opts[:range].each do |i| extents[i] = @cards[i].text(opts[:str][i], opts[:font][i], opts[:font_size][i], opts[:color][i], opts[:x][i], opts[:y][i], opts[:width][i], opts[:height][i], opts[:markup][i], opts[:justify][i], opts[:wrap][i], opts[:ellipsize][i], opts[:spacing][i], opts[:align][i], opts[:valign][i], opts[:hint][i], opts[:angle][i]) end return extents end |
#triangle(opts = {}) ⇒ nil
Draw a triangle using the given coordinates
Options support Arrays, see Arrays and Singleon Expansion
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/squib/api/shapes.rb', line 81 def triangle(opts = {}) opts = needs(opts, [:range, :x1, :y1, :x2, :y2, :x3, :y3, :layout, :fill_color, :stroke_color, :stroke_width]) opts[:range].each do |i| @cards[i].triangle(opts[:x1][i], opts[:y1][i], opts[:x2][i], opts[:y2][i], opts[:x3][i], opts[:y3][i], opts[:fill_color][i], opts[:stroke_color][i], opts[:stroke_width][i]) end end |