Class: Squib::Deck

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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/graphics/hand.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

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::Decks in your deck.rb. You can modify the internals of Squib::Deck (e.g. @cards), but that's not recommended.

Examples:

require 'squib'
Squib::Deck.new do
  text str: 'Hello, World!"
end

Parameters:

  • width (Integer) (defaults to: 825)

    the width of each card in pixels. Supports unit conversion (e.g. '2.5in').

  • height (Integer) (defaults to: 1125)

    the height of each card in pixels. Supports unit conversion (e.g. '3.5in').

  • cards (Integer) (defaults to: 1)

    the number of cards in the deck

  • dpi (Integer) (defaults to: 300)

    the pixels per inch when rendering out to PDF or calculating using inches.

  • config (String) (defaults to: 'config.yml')

    the file used for global settings of this deck

  • layout (String, Array) (defaults to: nil)

    load a YML file of custom layouts. Multiple files are merged sequentially, redefining collisons. See README and sample for details.

  • block (Block)

    the main body of the script.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/squib/deck.rb', line 61

def initialize(width: 825, height: 1125, cards: 1, dpi: 300, config: 'config.yml', layout: nil, &block)
  @dpi           = dpi
  @font          = SYSTEM_DEFAULTS[:default_font]
  @cards         = []
  @conf          = Conf.load(config)
  @progress_bar  = Progress.new(@conf.progress_bars) # FIXME this is evil. Using something different with @ and non-@
  show_info(config, layout)
  @width         = Args::UnitConversion.parse width, dpi
  @height        = Args::UnitConversion.parse height, dpi
  cards.times{ |i| @cards << Squib::Card.new(self, @width, @height, i) }
  @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

Examples:

background color: :white

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

Returns:

  • (nil)

    nothing



13
14
15
16
17
# File 'lib/squib/api/background.rb', line 13

def background(opts = {})
  range = Args::CardRange.new(opts[:range], deck_size: size)
  draw  = Args::Draw.new(custom_colors).load!(opts, expand_by: size, layout: layout, dpi: dpi)
  range.each { |i| @cards[i].background(draw.color[i]) }
end

#circle(opts = {}) ⇒ nil

Draw a circle centered at the given coordinates

Options support Arrays, see Arrays and Singleon Expansion

Examples:

circle x: 0, y: 0, radius: 100

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • range (Enumerable, :all) — default: :all

    the range of cards over which this will be rendered. See Specifying Ranges

  • x (Integer) — default: 0

    the x-coordinate to place. Supports Unit Conversion, see Units.

  • y (Integer) — default: 0

    the y-coordinate to place. Supports Unit Conversion, see Units.

  • radius (Integer) — default: 100

    radius of the circle. Supports Unit Conversion, see Units.

  • fill_color (String) — default: '#0000'

    the color with which to fill the rectangle. See Specifying Colors & Gradients.

  • stroke_color (String) — default: :black

    the color with which to stroke the outside of the rectangle. See Specifying Colors & Gradients.

  • stroke_width (Decimal) — default: 2.0

    the width of the outside stroke. Supports Unit Conversion, see Units.

  • stroke_strategy (:fill_first, :stroke_first) — default: :fill_first

    specify whether the stroke is done before (thinner) or after (thicker) filling the shape.

  • dash (String) — default: '') define a dash pattern for the stroke. Provide a string with space-separated numbers that define the pattern of on-and-off alternating strokes, measured in pixels by defautl. Supports Unit Conversion, see {file:README.md#Units Units} (e.g. `'0.02in 0.02in'`

    .

  • layout (String, Symbol) — default: nil

    entry in the layout to use as defaults for this command. See Custom Layouts

Returns:

  • (nil)

    intended to be void



60
61
62
63
64
65
# File 'lib/squib/api/shapes.rb', line 60

def circle(opts = {})
  range  = Args::CardRange.new(opts[:range], deck_size: size)
  coords = Args::Coords.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
  draw   = Args::Draw.new(custom_colors).load!(opts, expand_by: size, layout: layout, dpi: dpi)
  range.each { |i| @cards[i].circle(coords[i], draw[i]) }
end

#cm(n) ⇒ Decimal

Given cm, returns the number of pixels according to the deck's DPI.

Examples:

cm(1) # 750 (for default Deck::dpi of 300)

Parameters:

  • n (Decimal)

    , the number of centimeters

Returns:

  • (Decimal)

    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



95
96
97
# File 'lib/squib/api/data.rb', line 95

def csv(opts = {})
  Squib.csv(opts)
end

#curve(opts = {}) ⇒ nil

Draw a curve using the given coordinates

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • range (Enumerable, :all) — default: :all

    the range of cards over which this will be rendered. See Specifying Ranges

  • x1 (Integer) — default: 0

    the x-coordinate of the first endpoint. Supports Unit Conversion, see Units.

  • y1 (Integer) — default: 0

    the y-coordinate of the first endpoint. Supports Unit Conversion, see Units.

  • x2 (Integer) — default: 50

    the x-coordinate of the second endpoint. Supports Unit Conversion, see Units.

  • y2 (Integer) — default: 50

    the y-coordinate of the second endpoint. Supports Unit Conversion, see Units.

  • cx1 (Integer) — default: 0

    the x-coordinate of the first control point. Supports Unit Conversion, see Units.

  • cy1 (Integer) — default: 0

    the y-coordinate of the first control point. Supports Unit Conversion, see Units.

  • cx2 (Integer) — default: 50

    the x-coordinate of the second control point. Supports Unit Conversion, see Units.

  • cy2 (Integer) — default: 50

    the y-coordinate of the second control point. Supports Unit Conversion, see Units.

  • stroke_color (String) — default: :black

    the color with which to stroke the line. See Specifying Colors & Gradients.

  • stroke_width (Decimal) — default: 2.0

    the width of the outside stroke. Supports Unit Conversion, see Units.

  • stroke_strategy (:fill_first, :stroke_first) — default: :fill_first

    specify whether the stroke is done before (thinner) or after (thicker) filling the shape.

  • fill_color (String) — default: '#0000'

    the color with which to fill the triangle. See Specifying Colors & Gradients

  • cap (String) — default: 'butt'

    how the end of the line is drawn. Options are "square", "butt", and "round"

  • dash (String) — default: '') define a dash pattern for the stroke. Provide a string with space-separated numbers that define the pattern of on-and-off alternating strokes, measured in pixels by defautl. Supports Unit Conversion, see {file:README.md#Units Units} (e.g. `'0.02in 0.02in'`

    .

  • layout (String, Symbol) — default: nil

    entry in the layout to use as defaults for this command. See Custom Layouts

Returns:

  • (nil)

    intended to be void



168
169
170
171
172
173
# File 'lib/squib/api/shapes.rb', line 168

def curve(opts = {})
  range  = Args::CardRange.new(opts[:range], deck_size: size)
  draw   = Args::Draw.new(custom_colors).load!(opts, expand_by: size, layout: layout, dpi: dpi)
  coords = Args::Coords.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
  range.each { |i| @cards[i].curve(coords[i], draw[i]) }
end

#ellipse(opts = {}) ⇒ nil

Draw an ellipse

Options support Arrays, see Arrays and Singleon Expansion

Examples:

ellipse x: 0, y: 0, width: 825, height: 1125

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • range (Enumerable, :all) — default: :all

    the range of cards over which this will be rendered. See Specifying Ranges

  • x (Integer) — default: 0

    the x-coordinate to place. Supports Unit Conversion, see Units.

  • y (Integer) — default: 0

    the y-coordinate to place. Supports Unit Conversion, see Units.

  • width (Integer)

    the width of the rectangle. Supports Unit Conversion, see Units.

  • height (Integer)

    the height of the rectangle. Supports Unit Conversion, see Units.

  • fill_color (String) — default: '#0000'

    the color with which to fill the rectangle. See Specifying Colors & Gradients

  • stroke_color (String) — default: :black

    the color with which to stroke the outside of the rectangle. Specifying Colors & Gradients

  • stroke_width (Decimal) — default: 2.0

    the width of the outside stroke. Supports Unit Conversion, see Units.

  • stroke_strategy (:fill_first, :stroke_first) — default: :fill_first

    specify whether the stroke is done before (thinner) or after (thicker) filling the shape.

  • dash (String) — default: '') define a dash pattern for the stroke. Provide a string with space-separated numbers that define the pattern of on-and-off alternating strokes, measured in pixels by defautl. Supports Unit Conversion, see {file:README.md#Units Units} (e.g. `'0.02in 0.02in'`

    .

  • layout (String, Symbol) — default: nil

    entry in the layout to use as defaults for this command. See Custom Layouts

Returns:

  • (nil)

    intended to be void



87
88
89
90
91
92
# File 'lib/squib/api/shapes.rb', line 87

def ellipse(opts = {})
  range = Args::CardRange.new(opts[:range], deck_size: size)
  draw  = Args::Draw.new(custom_colors).load!(opts, expand_by: size, layout: layout, dpi: dpi)
  box   = Args::Box.new(self).load!(opts, expand_by: size, layout: layout, dpi: dpi)
  range.each { |i| @cards[i].ellipse(box[i], draw[i]) }
end

#hand(opts = {}) ⇒ nil

Renders a range of cards fanned out as if in a hand. Saves as PNG. See hand for full example

Examples:

hand range: :all, radius: :auto, margin: 20, fill_color: :white,
     angle_range: (Math::PI / -4.0)..(Math::PI / 2),
     dir: '_output', file: 'hand1.png'

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • range (Enumerable, :all) — default: :all

    the range of cards over which this will be rendered. See Specifying Ranges

  • radius (Fixnum) — default: :auto

    the distance from the bottom of each card to the center of the fan. If set to :auto, then it is computed as 30% of the card's height.

  • angle_range: (Range) — default: (Math::PI / -4.0)..(Math::PI / 2)

    . The overall width of the fan, in radians. Angle of zero is a vertical card. Further negative angles widen the fan counter-clockwise and positive angles widen the fan clockwise.

  • trim (Fixnum) — default: 0

    the margin around the card to trim before putting into the image

  • trim_radius (Fixnum) — default: 0

    the rounded rectangle radius around the card to trim before putting into the showcase

  • margin (Fixnum) — default: 75

    the margin around the entire image

  • fill_color (String, Color) — default: :white

    backdrop color. Usually black or white. Supports gradients.

  • dir (String) — default: _output

    the directory for the output to be sent to. Will be created if it doesn't exist.

  • file (String) — default: 'hand.png'

    the file to save in dir. Will be overwritten.

Returns:

  • (nil)

    Returns nothing.



141
142
143
144
145
146
# File 'lib/squib/api/save.rb', line 141

def hand(opts = {})
  range = Args::CardRange.new(opts[:range], deck_size: size)
  hand  = Args::HandSpecial.new(height).load!(opts, expand_by: size, layout: layout, dpi: dpi)
  sheet = Args::Sheet.new(custom_colors, {file: 'hand.png', trim_radius: 0}).load!(opts, expand_by: size, layout: layout, dpi: dpi)
  render_hand(range, sheet, hand)
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

Examples:

hint text: :cyan
hint text: :cyan

Parameters:

  • text (String) (defaults to: :off)

    the color of the text hint. To turn off use :off. @see README.md

Returns:

  • (nil)

    Returns nothing



15
16
17
# File 'lib/squib/api/settings.rb', line 15

def hint(text: :off)
  conf.text_hint = text
end

#inches(n) ⇒ Decimal

Given inches, returns the number of pixels according to the deck's DPI.

Examples:

inches(2.5) # 750 (for default Deck::dpi of 300)

Parameters:

  • n (Decimal)

    , the number of inches

Returns:

  • (Decimal)

    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

Examples:

triangle :x1 => 0, :y1 => 0, :x2 => 50, :y2 => 50

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • range (Enumerable, :all) — default: :all

    the range of cards over which this will be rendered. See Specifying Ranges

  • x1 (Integer) — default: 0

    the x-coordinate to place. Supports Unit Conversion, see Units.

  • y1 (Integer) — default: 0

    the y-coordinate to place. Supports Unit Conversion, see Units.

  • x2 (Integer) — default: 50

    the x-coordinate to place. Supports Unit Conversion, see Units.

  • y2 (Integer) — default: 50

    the y-coordinate to place. Supports Unit Conversion, see Units.

  • stroke_color (String) — default: :black

    the color with which to stroke the line. See Specifying Colors & Gradients.

  • stroke_width (Decimal) — default: 2.0

    the width of the outside stroke. Supports Unit Conversion, see Units.

  • stroke_strategy (:fill_first, :stroke_first) — default: :fill_first

    specify whether the stroke is done before (thinner) or after (thicker) filling the shape.

  • cap (String) — default: 'butt'

    how the end of the line is drawn. Options are "square", "butt", and "round"

  • dash (String) — default: '') define a dash pattern for the stroke. Provide a string with space-separated numbers that define the pattern of on-and-off alternating strokes, measured in pixels by defautl. Supports Unit Conversion, see {file:README.md#Units Units} (e.g. `'0.02in 0.02in'`

    .

  • layout (String, Symbol) — default: nil

    entry in the layout to use as defaults for this command. See Custom Layouts

Returns:

  • (nil)

    intended to be void



141
142
143
144
145
146
# File 'lib/squib/api/shapes.rb', line 141

def line(opts = {})
  range   = Args::CardRange.new(opts[:range], deck_size: size)
  draw    = Args::Draw.new(custom_colors).load!(opts, expand_by: size, layout: layout, dpi: dpi)
  coords  = Args::Coords.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
  range.each { |i| @cards[i].line(coords[i], draw[i]) }
end

#png(opts = {}) ⇒ nil

Renders a png file at the given location.

See samples/image.rb and samples/tgc-overlay.rb as examples.

Examples:

png file: 'img.png', x: 50, y: 50

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • range (Enumerable, :all) — default: :all

    the range of cards over which this will be rendered. See Specifying Ranges

  • file (String) — default: (empty)

    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 Specifying Files. Supports Arrays, see Arrays and Singleon Expansion

  • x (Integer) — default: 0

    the x-coordinate to place. Supports Arrays, see Arrays and Singleon Expansion. Supports Unit Conversion, see Units.

  • y (Integer) — default: 0

    the y-coordinate to place. Supports Arrays, see Arrays and Singleon Expansion. Supports Unit Conversion, see Units.

  • width (Integer, :native, :scale, :deck) — default: :native

    the pixel width that the image should scale to. :deck will scale to the deck width. :scale will use the height to scale and keep native the aspect ratio. Scaling PNGs is not recommended for professional-looking cards. When set to :native, uses the DPI and units of the loaded SVG document. Supports Arrays, see Arrays and Singleon Expansion. Supports Unit Conversion, see Units.

  • height (Integer, :native, :scale, :deck) — default: :native

    the pixel width that the image should scale to. :deck will scale to the deck height. :scale will use the width to scale and keep native the aspect ratio. Scaling PNGs is not recommended for professional-looking cards. When set to :native, uses the DPI and units of the loaded SVG document. Supports Arrays, see Arrays and Singleon Expansion. Supports Unit Conversion, see Units.

  • layout (String, Symbol) — default: nil

    entry in the layout to use as defaults for this command. See Custom Layouts. Supports Arrays, see Arrays and Singleon Expansion

  • alpha (Decimal) — default: 1.0

    the alpha-transparency percentage used to blend this image. Supports Arrays, see Arrays and Singleon Expansion

  • 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) — default: :none

    the composite blend operator used when applying this image. See Blend Modes at http://cairographics.org/operators. Supports Arrays, see Arrays and Singleon Expansion

  • angle (FixNum) — default: 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 Arrays and Singleon Expansion

  • mask (String) — default: nil

    If specified, the image will be used as a mask for the given color/gradient. Transparent pixels are ignored, opaque pixels are the given color. Note: the origin for gradient coordinates is at the given x,y, not at 0,0 as it is most other places.

Returns:

  • (nil)

    Returns nil



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/squib/api/image.rb', line 30

def png(opts = {})
  Dir.chdir(img_dir) do
    range = Args::CardRange.new(opts[:range], deck_size: size)
    paint = Args::Paint.new(custom_colors).load!(opts, expand_by: size, layout: layout)
    box   = Args::ScaleBox.new(self).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)
    @progress_bar.start('Loading PNG(s)', range.size) do |bar|
      range.each do |i|
        @cards[i].png(ifile[i].file, box[i], paint[i], trans[i])
        bar.increment
      end
    end
  end
end

#polygon(opts = {}) ⇒ nil

Draw a regular polygon at the given x,y

Examples:

polygon x: 10, y: 10, n: 5, angle: Math::PI / 4, radius: 50,
        fill_color: :green, stroke_color: :burgundy, :stroke_width: 3

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • range (Enumerable, :all) — default: :all

    the range of cards over which this will be rendered. See Specifying Ranges

  • x (Fixnum) — default: 0

    the x-coordinate of the center. Supports Unit Conversion, see Units.

  • y (Fixnum) — default: 0

    the y-coordinate of the center. Supports Unit Conversion, see Units.

  • n (Integer) — default: 5

    the number of points on the star

  • angle (Fixnum) — default: 0

    the angle at which to rotate

  • radius (Fixnum) — default: 0

    the radius from center to corner. Supports Unit conversion.

  • stroke_color (String) — default: :black

    the color with which to stroke the line. See Specifying Colors & Gradients.

  • stroke_width (Decimal) — default: 2.0

    the width of the outside stroke. Supports Unit Conversion, see Units.

  • stroke_strategy (:fill_first, :stroke_first) — default: :fill_first

    specify whether the stroke is done before (thinner) or after (thicker) filling the shape.

  • fill_color (String) — default: '#0000'

    the color with which to fill the triangle. See Specifying Colors & Gradients

  • dash (String) — default: '') define a dash pattern for the stroke. Provide a string with space-separated numbers that define the pattern of on-and-off alternating strokes, measured in pixels by defautl. Supports Unit Conversion, see {file:README.md#Units Units} (e.g. `'0.02in 0.02in'`

    .

  • layout (String, Symbol) — default: nil

    entry in the layout to use as defaults for this command. See Custom Layouts

Returns:

  • (nil)

    intended to be void



221
222
223
224
225
226
227
# File 'lib/squib/api/shapes.rb', line 221

def polygon(opts = {})
  range  = Args::CardRange.new(opts[:range], deck_size: size)
  draw   = Args::Draw.new(custom_colors).load!(opts, expand_by: size, layout: layout, dpi: dpi)
  coords = Args::Coords.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
  trans  = Args::Transform.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
  range.each { |i| @cards[i].polygon(coords[i], trans[i], draw[i]) }
end

#rect(opts = {}) ⇒ nil

Draw a rounded rectangle

Options support Arrays, see Arrays and Singleon Expansion

Examples:

rect x: 0, y: 0, width: 825, height: 1125, radius: 25

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • range (Enumerable, :all) — default: :all

    the range of cards over which this will be rendered. See Specifying Ranges

  • x (Integer) — default: 0

    the x-coordinate to place. Supports Unit Conversion, see Units.

  • y (Integer) — default: 0

    the y-coordinate to place. Supports Unit Conversion, see Units.

  • width (Integer)

    the width of the rectangle. Supports Unit Conversion, see Units.

  • height (Integer)

    the height of the rectangle. Supports Unit Conversion, see Units.

  • x_radius (Integer) — default: 0

    the radius of the rounded corner horiztonally. Zero is a non-rounded corner. Supports Unit Conversion, see Units.

  • y_radius (Integer) — default: 0

    the radius of the rounded corner vertically. Zero is a non-rounded corner. Supports Unit Conversion, see Units.

  • radius (Integer) — default: nil

    when set, overrides both x_radius and y_radius. Supports Unit Conversion, see Units.

  • fill_color (String) — default: '#0000'

    the color with which to fill the rectangle. See Specifying Colors & Gradients

  • stroke_color (String) — default: :black

    the color with which to stroke the outside of the rectangle. Specifying Colors & Gradients

  • stroke_width (Decimal) — default: 2.0

    the width of the outside stroke. Supports Unit Conversion, see Units.

  • stroke_strategy (:fill_first, :stroke_first) — default: :fill_first

    specify whether the stroke is done before (thinner) or after (thicker) filling the shape.

  • join (String) — default: 'miter'

    how corners will be drawn on stroke. Options are 'miter', 'bevel', or 'round'. Note that this is separate from the x_radius and y_radius of the rounded rectangle. Becomes more obvious with wider strokes.

  • dash (String) — default: '') define a dash pattern for the stroke. Provide a string with space-separated numbers that define the pattern of on-and-off alternating strokes, measured in pixels by defautl. Supports Unit Conversion, see {file:README.md#Units Units} (e.g. `'0.02in 0.02in'`

    .

  • layout (String, Symbol) — default: nil

    entry in the layout to use as defaults for this command. See Custom Layouts

Returns:

  • (nil)

    intended to be void



34
35
36
37
38
39
# File 'lib/squib/api/shapes.rb', line 34

def rect(opts = {})
  range = Args::CardRange.new(opts[:range], deck_size: size)
  box   = Args::Box.new(self).load!(opts, expand_by: size, layout: layout, dpi: dpi)
  draw  = Args::Draw.new(custom_colors).load!(opts, expand_by: size, layout: layout, dpi: dpi)
  range.each { |i| @cards[i].rect(box[i], draw[i]) }
end

#save(opts = {}) ⇒ Object

Saves the given range of cards to either PNG or PDF

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • range (Enumerable) — default: :all

    the range of cards over which this will be rendered. See Specifying Ranges

  • dir (String) — default: _output

    the directory for the output to be sent to. Will be created if it doesn't exist.

  • format (Symbol) — default: :png

    the format that this will be rendered too. Options :pdf, :png. Array of both is allowed: [:pdf, :png]

  • prefix (String) — default: card_

    the prefix of the file name to be printed

  • rotate (Boolean) — default: false

    PNG saving only. If true, the saved cards will be rotated 90 degrees clockwise. Intended to rendering landscape instead of portrait.

Returns:

  • self



20
21
22
23
24
# File 'lib/squib/api/save.rb', line 20

def save(opts = {})
  save_png(opts) if Array(opts[:format]).include? :png
  save_pdf(opts) if Array(opts[:format]).include? :pdf
  self
end

#save_pdf(opts = {}) ⇒ nil

Lays out the cards in range and renders a PDF

Examples:

save_pdf file: 'deck.pdf', margin: 75, gap: 5, trim: 37

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • file (String)

    the name of the PDF file to save. See Specifying Files

  • dir (String) — default: _output

    the directory to save to. Created if it doesn't exist.

  • width (Integer) — default: 3300

    the height of the page in pixels. Default is 11in * 300dpi. Supports unit conversion.

  • height (Integer) — default: 2550

    the height of the page in pixels. Default is 8.5in * 300dpi. Supports unit conversion.

  • margin (Integer) — default: 75

    the margin around the outside of the page. Supports unit conversion.

  • gap (Integer) — default: 0

    the space in pixels between the cards. Supports unit conversion.

  • trim (Integer) — default: 0

    the space around the edge of each card to trim (e.g. to cut off the bleed margin for print-and-play). Supports unit conversion.

Returns:

  • (nil)


40
41
42
43
44
# File 'lib/squib/api/save.rb', line 40

def save_pdf(opts = {})
  range = Args::CardRange.new(opts[:range], deck_size: size)
  sheet = Args::Sheet.new(custom_colors, {file: 'output.pdf'}).load!(opts, expand_by: size, layout: layout, dpi: dpi)
  render_pdf(range, sheet)
end

#save_png(opts = {}) ⇒ nil

Saves the given range of cards to a PNG

Options support Arrays, see Arrays and Singleon Expansion

Examples:

save range: 1..8, dir: '_pnp', prefix: 'bw_'

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • range (Enumerable) — default: :all

    the range of cards over which this will be rendered. See Specifying Ranges

  • dir (String) — default: _output

    the directory for the output to be sent to. Will be created if it doesn't exist.

  • prefix (String) — default: card_

    the prefix of the file name to be printed.

  • count_format (String) — default: %02d

    the format string used for formatting the card count (e.g. padding zeros). Uses a Ruby format string (see the Ruby doc for Kernel::sprintf for specifics)

  • rotate (Boolean, :clockwise, :counterclockwise) — default: false

    if true, the saved cards will be rotated 90 degrees clockwise. Or, rotate by the number of radians. Intended to rendering landscape instead of portrait.

Returns:

  • (nil)

    Returns nothing



60
61
62
63
64
65
66
67
68
69
# File 'lib/squib/api/save.rb', line 60

def save_png(opts = {})
  range = Args::CardRange.new(opts[:range], deck_size: size)
  batch = Args::SaveBatch.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
  @progress_bar.start("Saving PNGs to #{batch.summary}", size) do |bar|
    range.each do |i|
      @cards[i].save_png(batch[i])
      bar.increment
    end
  end
end

#save_sheet(opts = {}) ⇒ nil

Lays out the cards in range and renders a stitched PNG sheet

Examples:

save_sheet prefix: 'sheet_', margin: 75, gap: 5, trim: 37

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • range (Enumerable) — default: :all

    the range of cards over which this will be rendered. See Specifying Ranges

  • columns (Integer) — default: 5

    the number of columns in the grid. Must be an integer

  • rows (Integer) — default: :infinite

    the number of rows in the grid. When set to :infinite, the sheet scales to the rows needed. If there are more cards than rows*columns, new sheets are started.

  • prefix (String) — default: card_

    the prefix of the file name(s)

  • count_format (String) — default: %02d

    the format string used for formatting the card count (e.g. padding zeros). Uses a Ruby format string (see the Ruby doc for Kernel::sprintf for specifics)

  • dir (String) — default: _output

    the directory to save to. Created if it doesn't exist.

  • margin (Integer) — default: 0

    the margin around the outside of the sheet.

  • gap (Integer) — default: 0

    the space in pixels between the cards

  • trim (Integer) — default: 0

    the space around the edge of each card to trim (e.g. to cut off the bleed margin for print-and-play)

Returns:

  • (nil)


87
88
89
90
91
92
# File 'lib/squib/api/save.rb', line 87

def save_sheet(opts = {})
  range = Args::CardRange.new(opts[:range], deck_size: size)
  batch = Args::SaveBatch.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
  sheet = Args::Sheet.new(custom_colors, {margin: 0}, size).load!(opts, expand_by: size, layout: layout, dpi: dpi)
  render_sheet(range, batch, sheet)
end

#set(opts = {}) ⇒ nil

Sets various defaults for this deck. Defaults can be overriden by the commands themselves when that command supports it.

Examples:

set font: 'Arial 26'
text 'blah'                     # in Arial 26
text 'blah24', font: 'Arial 24' # in Arial 24
set font: :default              # Back to Squib-wide default

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • font: (Object)

    the font string to set as default. Can also be set to :default to use the Squib-wide default.

Returns:

  • (nil)

    Returns nothing



29
30
31
32
# File 'lib/squib/api/settings.rb', line 29

def set(opts = {})
  raise 'DEPRECATED: As of v0.7 img_dir is no longer supported in "set". Use config.yml instead.' if opts.key? :img_dir
  @font = (opts[:font] == :default) ? Squib::SYSTEM_DEFAULTS[:default_font] : opts[:font]
end

#showcase(opts = {}) ⇒ nil

Renders a range of cards in a showcase as if they are sitting in 3D on a reflective surface See showcase for full example

Examples:

showcase file: 'showcase_output.png', trim: 78, trim_radius: 32

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • range (Enumerable, :all) — default: :all

    the range of cards over which this will be rendered. See Specifying Ranges

  • trim (Fixnum) — default: 0

    the margin around the card to trim before putting into the showcase

  • trim_radius (Fixnum) — default: 38

    the rounded rectangle radius around the card to trim before putting into the showcase

  • margin (Fixnum) — default: 75

    the margin around the entire showcase

  • scale (Fixnum) — default: 0.8

    percentage of original width of each (trimmed) card to scale to. Must be between 0.0 and 1.0, but starts looking bad around 0.6.

  • offset (Fixnum) — default: 1.1

    percentage of the scaled width of each card to shift each offset. e.g. 1.1 is a 10% shift, and 0.95 is overlapping by 5%

  • fill_color (String, Color) — default: :white

    backdrop color. Usually black or white. Supports gradients.

  • reflect_offset (Fixnum) — default: 15

    the number of pixels between the bottom of the card and the reflection. Supports Unit Conversion, see Units.

  • reflect_strength (Fixnum) — default: 0.2

    the starting alpha transparency of the reflection (at the top of the card). Percentage between 0 and 1. Looks more realistic at low values since even shiny surfaces lose a lot of light.

  • reflect_percent (Fixnum) — default: 0.25

    the length of the reflection in percentage of the card. Larger values tend to make the reflection draw just as much attention as the card, which is not good.

  • face (:left, :right) — default: :left

    which direction the cards face. Anything but :right will face left

  • dir (String) — default: _output

    the directory for the output to be sent to. Will be created if it doesn't exist.

  • file (String) — default: 'showcase.png'

    the file to save in dir. Will be overwritten.

Returns:

  • (nil)

    Returns nothing.



115
116
117
118
119
120
# File 'lib/squib/api/save.rb', line 115

def showcase(opts = {})
  range    = Args::CardRange.new(opts[:range], deck_size: size)
  showcase = Args::ShowcaseSpecial.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
  sheet    = Args::Sheet.new(custom_colors, {file: 'showcase.png'}).load!(opts, expand_by: size, layout: layout, dpi: dpi)
  render_showcase(range, sheet, showcase)
end

#star(opts = {}) ⇒ nil

Draw a star at the given x,y

Examples:

star x: 10, y: 10, n: 5, angle: Math::PI / 4, inner_radius: 50, outer_radius: 100,
     fill_color: :green, stroke_color: :burgundy, :stroke_width: 3

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • range (Enumerable, :all) — default: :all

    the range of cards over which this will be rendered. See Specifying Ranges

  • x (Fixnum) — default: 0

    the x-coordinate of the center. Supports Unit Conversion, see Units.

  • y (Fixnum) — default: 0

    the y-coordinate of the center. Supports Unit Conversion, see Units.

  • n (Integer) — default: 5

    the number of points on the star

  • angle (Fixnum) — default: 0

    the angle at which to rotate

  • inner_radius (Fixnum) — default: 0

    the inner radius. Supports Unit conversion.

  • stroke_color (String) — default: :black

    the color with which to stroke the line. See Specifying Colors & Gradients.

  • stroke_width (Decimal) — default: 2.0

    the width of the outside stroke. Supports Unit Conversion, see Units.

  • stroke_strategy (:fill_first, :stroke_first) — default: :fill_first

    specify whether the stroke is done before (thinner) or after (thicker) filling the shape.

  • fill_color (String) — default: '#0000'

    the color with which to fill the triangle. See Specifying Colors & Gradients

  • dash (String) — default: '') define a dash pattern for the stroke. Provide a string with space-separated numbers that define the pattern of on-and-off alternating strokes, measured in pixels by defautl. Supports Unit Conversion, see {file:README.md#Units Units} (e.g. `'0.02in 0.02in'`

    .

  • layout (String, Symbol) — default: nil

    entry in the layout to use as defaults for this command. See Custom Layouts

Returns:

  • (nil)

    intended to be void



194
195
196
197
198
199
200
# File 'lib/squib/api/shapes.rb', line 194

def star(opts = {})
  range  = Args::CardRange.new(opts[:range], deck_size: size)
  draw   = Args::Draw.new(custom_colors).load!(opts, expand_by: size, layout: layout, dpi: dpi)
  coords = Args::Coords.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
  trans  = Args::Transform.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
  range.each { |i| @cards[i].star(coords[i], trans[i], draw[i]) }
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. If neither data nor file are specified for a given card, this method does nothing.

See samples/load-images.rb and samples/tgc-overlay.rb as examples.

Note: if alpha transparency is desired, set that in the SVG.

Examples:

svg range: 1..2, file: 'icon.svg', id: '#stone', x: 50, y:50

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • range (Enumerable, :all) — default: :all

    the range of cards over which this will be rendered. See Specifying Ranges

  • file (String) — default: '') 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}

    '') 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 Specifying Files. Supports Arrays, see Arrays and Singleon Expansion

  • data (String) — default: nil

    render from an SVG XML string. Overrides file if both are specified (a warning is shown) . 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 Specifying Files. Supports Arrays, see Arrays and Singleon Expansion.

  • id (String) — default: 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 Arrays and Singleon Expansion

  • force_id (Boolean) — default: 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 Arrays and Singleon Expansion

  • x (Integer) — default: 0

    the x-coordinate to place. Supports Arrays, see Arrays and Singleon Expansion. Supports Unit Conversion, see Units.

  • y (Integer) — default: 0

    the y-coordinate to place. Supports Arrays, see Arrays and Singleon Expansion. Supports Unit Conversion, see Units.

  • width (Integer) — default: :native

    the pixel width that the image should scale to. :deck will scale to the deck height. :scale will use the width to scale and keep native the aspect ratio. 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. Supports Arrays, see Arrays and Singleon Expansion. Supports Unit Conversion, see Units.

  • height (Integer) — default: :native

    the pixel width that the image should scale to. :deck will scale to the deck height. :scale will use the width to scale and keep native the aspect ratio. 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. Supports Arrays, see Arrays and Singleon Expansion. Supports Unit Conversion, see Units.

  • layout (String, Symbol) — default: nil

    entry in the layout to use as defaults for this command. See Custom Layouts. Supports Arrays, see Arrays and Singleon Expansion

  • 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) — default: :none

    the composite blend operator used when applying this image. See Blend Modes at http://cairographics.org/operators. Supports Arrays, see Arrays and Singleon Expansion

  • angle (FixNum) — default: 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 Arrays and Singleon Expansion

  • mask (String) — default: nil

    If specified, the image will be used as a mask for the given color/gradient. Transparent pixels are ignored, opaque pixels are the given color. Note: the origin for gradient coordinates is at the given x,y, not at 0,0 as it is most other places.

Returns:

  • (nil)

    Returns nil



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/squib/api/image.rb', line 70

def svg(opts = {})
  Dir.chdir(img_dir) do
    range = Args::CardRange.new(opts[:range], deck_size: size)
    paint = Args::Paint.new(custom_colors).load!(opts, expand_by: size, layout: layout)
    box   = Args::ScaleBox.new(self).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)
    @progress_bar.start('Loading SVG(s)', range.size) do |bar|
      range.each do |i|
        if svg_args.render?(i)
          @cards[i].svg(ifile[i].file, svg_args[i], box[i], paint[i], trans[i])
        end
        bar.increment
      end
    end
  end
end

#text(opts = {}) {|embed| ... } ⇒ 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

Examples:

text str: 'hello'
text str: 'hello', x: 50, y:50, align: center

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • range (Enumerable, :all) — default: :all

    the range of cards over which this will be rendered. See Specifying Ranges

  • str (String, Array) — default: '') the string to be rendered. Must support `#to_s`. If the card responds to `#each`, it's mapped out one at a time across the cards.

    '') the string to be rendered. Must support #to_s. If the card responds to #each, it's mapped out one at a time across the cards.

  • font (String) — default: Arial 36 or whatever was set with `set`

    the Font description string, including family, styles, and size. (e.g. 'Arial bold italic 12') For the official documentation, see the Pango docs. This description is also quite good. See the samples/text.rb as well.

  • font_size (Integer) — default: nil

    an override of font string description, for scaling the font according to the size of the string

  • x (Integer) — default: 0

    the x-coordinate to place. Supports Unit Conversion, see Units.

  • y (Integer) — default: 0

    the y-coordinate to place. Supports Unit Conversion, see Units.

  • color (String) — default: :black

    the color the font will render to. Gradients supported. See Specifying Colors

  • markup: (Boolean) — default: false

    Enable markup parsing of str using the HTML-like Pango Markup syntax, defined here and here. Also does other replacements, such as smart quotes, curly apostraphes, en- and em-dashes, and explict ellipses (not to be confused with ellipsize option). See README for full explanation.

  • width (Integer, :auto) — default: :auto

    the width of the box the string will be placed in. Stretches to the content by default.. Supports Unit Conversion, see Units.

  • height (Integer, :auto)

    the height of the box the string will be placed in. Stretches to the content by default. Supports Unit Conversion, see Units.

  • layout (String, Symbol) — default: nil

    entry in the layout to use as defaults for this command. See Custom Layouts

  • wrap (:none, :word, :char, :word_char, true, false) — default: :word_char

    When height is set, determines the behavior of how the string wraps. The :word_char option will break at words, but then fall back to characters when the word cannot fit. # Options are :none, :word, :char, :word_char. Also: true is the same as :word_char, false is the same as :none. Default :word_char

  • spacing (Integer) — default: 0

    Adjust the spacing when the text is multiple lines. No effect when the text does not wrap.

  • align (:left, right, :center) — default: :left

    The alignment of the text

  • justify (Boolean) — default: false

    toggles whether or not the text is justified or not.

  • valign (:top, :middle, :bottom) — default: :top

    When width and height are set, align text vertically according to the ink extents of the text.

  • ellipsize (:none, :start, :middle, :end, true, false) — default: :end

    When width and height are set, determines the behavior of overflowing text. Also: true maps to :end and false maps to :none. Default :end

  • angle (FixNum) — default: 0

    Rotation of the text in radians. Note that this rotates around the upper-left corner of the text box, making the placement of x-y coordinates slightly tricky.

  • stroke_width (Decimal) — default: 0.0

    the width of the outside stroke. Supports Unit Conversion, see Units.

  • stroke_color (String) — default: :black

    the color with which to stroke the outside of the rectangle. Specifying Colors & Gradients

  • stroke_strategy (:fill_first, :stroke_first) — default: :fill_first

    specify whether the stroke is done before (thinner) or after (thicker) filling the shape.

  • dash (String) — default: '') define a dash pattern for the stroke. Provide a string with space-separated numbers that define the pattern of on-and-off alternating strokes, measured in pixels by defautl. Supports Unit Conversion, see {file:README.md#Units Units} (e.g. `'0.02in 0.02in'`

    .

  • hint (String) — default: :nil

    draw a rectangle around the text with the given color. Overrides global hints (see #hint).

Yields:

  • (embed)

Returns:

  • (Array)

    Returns an Array of hashes keyed by :width and :height that mark the ink extents of the text rendered.



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/squib/api/text.rb', line 51

def text(opts = {})
  range = Args::CardRange.new(opts[:range], deck_size: size)
  para  = Args::Paragraph.new(font).load!(opts, expand_by: size, layout: layout)
  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)
  draw  = Args::Draw.new(custom_colors, {stroke_width: 0.0}).load!(opts, expand_by: size, layout: layout, dpi: dpi)
  embed = TextEmbed.new(size, custom_colors, layout, dpi, img_dir)
  yield(embed) if block_given? #store the opts for later use
  extents = Array.new(@cards.size)
  range.each { |i| extents[i] = @cards[i].text(embed, para[i], box[i], trans[i], draw[i]) }
  return extents
end

#triangle(opts = {}) ⇒ nil

Draw a triangle using the given coordinates

Options support Arrays, see Arrays and Singleon Expansion

Examples:

triangle :x1 => 0, :y1 => 0, :x2 => 50, :y2 => 50, :x3 => 0, :y3 => 50

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • range (Enumerable, :all) — default: :all

    the range of cards over which this will be rendered. See Specifying Ranges

  • x1 (Integer) — default: 0

    the x-coordinate to place. Supports Unit Conversion, see Units.

  • y1 (Integer) — default: 0

    the y-coordinate to place. Supports Unit Conversion, see Units.

  • x2 (Integer) — default: 50

    the x-coordinate to place. Supports Unit Conversion, see Units.

  • y2 (Integer) — default: 50

    the y-coordinate to place. Supports Unit Conversion, see Units.

  • x3 (Integer) — default: 0

    the x-coordinate to place. Supports Unit Conversion, see Units.

  • y3 (Integer) — default: 50

    the y-coordinate to place. Supports Unit Conversion, see Units.

  • fill_color (String) — default: '#0000'

    the color with which to fill the triangle. See Specifying Colors & Gradients

  • stroke_color (String) — default: :black

    the color with which to stroke the outside of the triangle. See Specifying Colors & Gradients

  • stroke_width (Decimal) — default: 2.0

    the width of the outside stroke. Supports Unit Conversion, see Units.

  • stroke_strategy (:fill_first, :stroke_first) — default: :fill_first

    specify whether the stroke is done before (thinner) or after (thicker) filling the shape.

  • dash (String) — default: '') define a dash pattern for the stroke. Provide a string with space-separated numbers that define the pattern of on-and-off alternating strokes, measured in pixels by defautl. Supports Unit Conversion, see {file:README.md#Units Units} (e.g. `'0.02in 0.02in'`

    .

  • layout (String, Symbol) — default: nil

    entry in the layout to use as defaults for this command. See Custom Layouts

Returns:

  • (nil)

    intended to be void



116
117
118
119
120
121
# File 'lib/squib/api/shapes.rb', line 116

def triangle(opts = {})
  range  = Args::CardRange.new(opts[:range], deck_size: size)
  draw   = Args::Draw.new(custom_colors).load!(opts, expand_by: size, layout: layout, dpi: dpi)
  coords = Args::Coords.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
  range.each { |i| @cards[i].triangle(coords[i], draw[i]) }
end

#xlsx(opts = {}) ⇒ Object

Convenience call on deck goes to the module function



90
91
92
# File 'lib/squib/api/data.rb', line 90

def xlsx(opts = {})
  Squib.xlsx(opts)
end