Class: Squib::Deck Private

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/groups.rb,
lib/squib/api/shapes.rb,
lib/squib/api/settings.rb,
lib/squib/graphics/hand.rb,
lib/squib/api/background.rb,
lib/squib/sample_helpers.rb,
lib/squib/graphics/save_doc.rb,
lib/squib/graphics/showcase.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Some helper methods specifically for samples :nodoc:

Instance Attribute Summary collapse

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::Deck`s 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
76
# 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          = 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.new(dpi).load_layout(layout)
  enable_groups_from_env!
  if block_given?
    instance_eval(&block) # here we go. wheeeee!
  end
end

Instance Attribute Details

#cardsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Attributes for the width, height (in pixels) and number of cards These are expected to be immuatble for the life of Deck



29
30
31
# File 'lib/squib/deck.rb', line 29

def cards
  @cards
end

#confObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

:nodoc:



36
37
38
# File 'lib/squib/deck.rb', line 36

def conf
  @conf
end

#dpiObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

:nodoc:



36
37
38
# File 'lib/squib/deck.rb', line 36

def dpi
  @dpi
end

#fontObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

:nodoc:



36
37
38
# File 'lib/squib/deck.rb', line 36

def font
  @font
end

#heightObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Attributes for the width, height (in pixels) and number of cards These are expected to be immuatble for the life of Deck



29
30
31
# File 'lib/squib/deck.rb', line 29

def height
  @height
end

#layoutObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

:nodoc:



36
37
38
# File 'lib/squib/deck.rb', line 36

def layout
  @layout
end

#widthObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Attributes for the width, height (in pixels) and number of cards These are expected to be immuatble for the life of Deck



29
30
31
# File 'lib/squib/deck.rb', line 29

def width
  @width
end

Instance Method Details

#[](key) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Directly accesses the array of cards in the deck



81
82
83
# File 'lib/squib/deck.rb', line 81

def [](key)
  @cards[key]
end

#background(opts = {}) ⇒ Object

DSL method. See squib.readthedocs.io



8
9
10
11
12
# File 'lib/squib/api/background.rb', line 8

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

#build(grp = :all, &block) ⇒ Object

DSL method. See squib.readthedocs.io



16
17
18
19
# File 'lib/squib/api/groups.rb', line 16

def build grp = :all, &block
  raise 'Please provide a block' unless block_given?
  block.yield if build_groups.include? grp
end

#build_groupsObject

DSL method. See squib.readthedocs.io



34
35
36
# File 'lib/squib/api/groups.rb', line 34

def build_groups
  @build_groups ||= Set.new.add(:all)
end

#circle(opts = {}) ⇒ Object

DSL method. See squib.readthedocs.io



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

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) ⇒ Object

DSL method. See squib.readthedocs.io



12
13
14
# File 'lib/squib/api/units.rb', line 12

def cm(n)
  @dpi * Squib::INCHES_IN_CM * n.to_f
end

#csv(opts = {}) ⇒ Object

DSL method. See squib.readthedocs.io



102
103
104
# File 'lib/squib/api/data.rb', line 102

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

#curve(opts = {}) ⇒ Object

DSL method. See squib.readthedocs.io



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

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

#disable_build(grp) ⇒ Object

DSL method. See squib.readthedocs.io



28
29
30
31
# File 'lib/squib/api/groups.rb', line 28

def disable_build grp
  build_groups # make sure it's initialized
  @build_groups.delete grp
end

#draw_graph_paper(width, height) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Draw graph paper for samples



9
10
11
12
13
14
15
16
# File 'lib/squib/sample_helpers.rb', line 9

def draw_graph_paper(width, height)
  background color: 'white'
  grid width: 50,  height: 50,  stroke_color: '#659ae9', stroke_width: 1.5
  grid width: 200, height: 200, stroke_color: '#659ae9', stroke_width: 3, x: 50, y: 50
  (50..height).step(200) do |y|
    text str: "y=#{y}", x: 3, y: y - 18, font: 'Open Sans, Sans 10'
  end
end

#each(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Iterates over each card in the deck



88
89
90
# File 'lib/squib/deck.rb', line 88

def each(&block)
  @cards.each { |card| block.call(card) }
end

#ellipse(opts = {}) ⇒ Object

DSL method. See squib.readthedocs.io



28
29
30
31
32
33
34
# File 'lib/squib/api/shapes.rb', line 28

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, { width: '0.25in', height: '0.25in' }).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].ellipse(box[i], draw[i], trans[i]) }
end

#enable_build(grp) ⇒ Object

DSL method. See squib.readthedocs.io



22
23
24
25
# File 'lib/squib/api/groups.rb', line 22

def enable_build grp
  build_groups # make sure it's initialized
  @build_groups << grp
end

#enable_groups_from_env!Object

Not a DSL method, but initialized from Deck.new



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

def enable_groups_from_env!
  return if ENV['SQUIB_BUILD'].nil?
  ENV['SQUIB_BUILD'].split(',').each do |grp|
    enable_build grp.strip.to_sym
  end
end

#grid(opts = {}) ⇒ Object

DSL method. See squib.readthedocs.io



37
38
39
40
41
42
# File 'lib/squib/api/shapes.rb', line 37

def grid(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].grid(box[i], draw[i]) }
end

#hand(opts = {}) ⇒ Object

DSL method. See squib.readthedocs.io



53
54
55
56
57
58
# File 'lib/squib/api/save.rb', line 53

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) ⇒ Object

DSL method. See squib.readthedocs.io



5
6
7
# File 'lib/squib/api/settings.rb', line 5

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

#inches(n) ⇒ Object

DSL method. See squib.readthedocs.io



7
8
9
# File 'lib/squib/api/units.rb', line 7

def inches(n)
  @dpi * n.to_f
end

#line(opts = {}) ⇒ Object

DSL method. See squib.readthedocs.io



53
54
55
56
57
58
# File 'lib/squib/api/shapes.rb', line 53

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

#mm(n) ⇒ Object

DSL method. See squib.readthedocs.io



17
18
19
# File 'lib/squib/api/units.rb', line 17

def mm(n)
  @dpi * Squib::INCHES_IN_CM * n.to_f / 10.0
end

#perspective(src, scale, face_right) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

:nodoc:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/squib/graphics/showcase.rb', line 65

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 = {}) ⇒ Object

DSL method. See squib.readthedocs.io



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/squib/api/image.rb', line 12

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(self).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 = {}) ⇒ Object

DSL method. See squib.readthedocs.io



78
79
80
81
82
83
84
# File 'lib/squib/api/shapes.rb', line 78

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 = {}) ⇒ Object

DSL method. See squib.readthedocs.io



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

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)
  trans  = Args::Transform.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
  range.each { |i| @cards[i].rect(box[i], draw[i], trans[i]) }
end

#reflect(src, roffset, rpercent, rstrength) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

:nodoc:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/squib/graphics/showcase.rb', line 46

def reflect(src, roffset, rpercent, rstrength)
  tmp_cc = Cairo::Context.new(Cairo::ImageSurface.new(src.width, src.height * (1.0 + rpercent) + roffset))
  tmp_cc.set_source(src, 0, 0)
  tmp_cc.paint
  # Flip affine magic from: http://cairographics.org/matrix_transform/
  matrix = Cairo::Matrix.new(1, 0, 0, -1, 0, 2 * src.height + roffset)
  tmp_cc.transform(matrix) # flips the coordinate system
  top_y    = src.height    # top of the reflection
  bottom_y = src.height * (1.0 - rpercent) + roffset # bottom of the reflection
  gradient = Cairo::LinearPattern.new(0, top_y, 0, bottom_y)
  gradient.add_color_stop_rgba(0.0, 0, 0, 0, rstrength) # start a little reflected
  gradient.add_color_stop_rgba(1.0, 0, 0, 0, 0.0)       # fade to nothing
  tmp_cc.set_source(src, 0, 0)
  tmp_cc.mask(gradient)
  return tmp_cc.target
end

#render_hand(range, sheet, hand) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Draw cards in a fan.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/squib/graphics/hand.rb', line 8

def render_hand(range, sheet, hand)
  cards       = range.collect { |i| @cards[i] }
  center_x    = width / 2.0
  center_y    = hand.radius + height
  out_size    = 3.0 * center_y
  angle_delta = (hand.angle_range.last - hand.angle_range.first) / cards.size
  cxt = Cairo::Context.new(Cairo::RecordingSurface.new(0, 0, out_size, out_size))
  cxt.translate(out_size / 2.0, out_size / 2.0)
  cxt.rotate(hand.angle_range.first)
  cxt.translate(-width, -width)
  cards.each_with_index do |card, i|
    cxt.translate(center_x, center_y)
    cxt.rotate(angle_delta)
    cxt.translate(-center_x, -center_y)
    card.use_cairo do |card_cxt|
      cxt.rounded_rectangle(sheet.trim, sheet.trim,
                            width - (2 * sheet.trim), height - (2 * sheet.trim),
                            sheet.trim_radius, sheet.trim_radius)
      cxt.clip
      cxt.set_source(card_cxt.target)
      cxt.paint
      cxt.reset_clip
    end
  end
  x, y, w, h = cxt.target.ink_extents # I love Ruby assignment ;)
  png_cxt = Squib::Graphics::CairoContextWrapper.new(Cairo::Context.new(Cairo::ImageSurface.new(w + 2 * sheet.margin, h + 2 * sheet.margin)))
  png_cxt.set_source_squibcolor(sheet.fill_color)
  png_cxt.paint
  png_cxt.translate(-x + sheet.margin, -y + sheet.margin)
  png_cxt.set_source(cxt.target)
  png_cxt.paint
  png_cxt.target.write_to_png sheet.full_filename
end

#render_pdf(range, sheet) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

:nodoc:



6
7
8
9
10
11
12
13
14
15
16
17
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
43
44
45
46
47
# File 'lib/squib/graphics/save_doc.rb', line 6

def render_pdf(range, sheet)
  file         = "#{sheet.dir}/#{sheet.file}"
  cc           = Cairo::Context.new(Cairo::PDFSurface.new(file, sheet.width * 72.0 / @dpi, sheet.height * 72.0 / @dpi))
  cc.scale(72.0 / @dpi, 72.0 / @dpi) # for bug #62
  x, y         = sheet.margin, sheet.margin
  card_width   = @width  - 2 * sheet.trim
  card_height  = @height - 2 * sheet.trim
  @progress_bar.start("Saving PDF to #{file}", range.size) do |bar|
    range.each do |i|
      card = @cards[i]
      cc.translate(x, y)
      cc.rectangle(sheet.trim, sheet.trim, card_width, card_height)
      cc.clip
      case card.backend.downcase.to_sym
      when :memory
        cc.set_source(card.cairo_surface, 0, 0)
        cc.paint
      when :svg
        card.cairo_surface.finish
        cc.save
        cc.scale(0.8, 0.8) # I really don't know why I needed to do this at all. But 0.8 is the magic number to get this to scale right
        cc.render_rsvg_handle(RSVG::Handle.new_from_file(card.svgfile), nil)
        cc.restore
      else
        abort "No such back end supported for save_pdf: #{backend}"
      end
      bar.increment
      cc.reset_clip
      cc.translate(-x, -y)
      x += card.width + sheet.gap - 2 * sheet.trim
      if x > (sheet.width - card_width - sheet.margin)
        x = sheet.margin
        y += card.height + sheet.gap - 2 * sheet.trim
        if y > (sheet.height - card_height - sheet.margin)
          cc.show_page # next page
          x, y = sheet.margin, sheet.margin
        end
      end
    end
  end
  cc.target.finish
end

#render_sheet(range, batch, sheet) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

:nodoc:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/squib/graphics/save_doc.rb', line 51

def render_sheet(range, batch, sheet)
  sheet_width = (sheet.columns * (@width + 2 * sheet.gap - 2 * sheet.trim)) + (2 * sheet.margin)
  sheet_height = (sheet.rows * (@height + 2 * sheet.gap - 2 * sheet.trim)) + (2 * sheet.margin)
  cc = Cairo::Context.new(Cairo::ImageSurface.new(sheet_width, sheet_height))
  num_this_sheet = 0
  sheet_num = 0
  x, y = sheet.margin, sheet.margin
  @progress_bar.start("Saving PNG sheet to #{batch.summary}", @cards.size + 1) do |bar|
    range.each do |i|
      if num_this_sheet >= (sheet.columns * sheet.rows) # new sheet
        filename = batch.full_filename(sheet_num)
        cc.target.write_to_png(filename)
        new_sheet = false
        num_this_sheet = 0
        sheet_num += 1
        x, y = sheet.margin, sheet.margin
        cc = Cairo::Context.new(Cairo::ImageSurface.new(sheet_width, sheet_height))
      end
      surface = trim(@cards[i].cairo_surface, sheet.trim, @width, @height)
      cc.set_source(surface, x, y)
      cc.paint
      num_this_sheet += 1
      x += surface.width + sheet.gap
      if num_this_sheet % sheet.columns == 0 # new row
        x = sheet.margin
        y += surface.height + sheet.gap
      end
      bar.increment
    end
    cc.target.write_to_png(batch.full_filename(sheet_num))
  end
end

#render_showcase(range, sheet, showcase) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

So the Cairo people have said over and over again that they won’t support the 3x3 matrices that would handle perspective transforms. Since our perspective transform needs are a bit simpler, we can use a “striping method” that does the job for us. It’s a little bit involved, but it works well enough for limited ranges of our parameters. These were also helpful:

http://kapo-cpp.blogspot.com/2008/01/perspective-effect-using-cairo.html
http://zetcode.com/gui/pygtk/drawingII/

:nodoc:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/squib/graphics/showcase.rb', line 14

def render_showcase(range, sheet, showcase)
  out_width = range.size * ((@width - 2 * sheet.trim) * showcase.scale * showcase.offset) + 2 * sheet.margin
  out_height = showcase.reflect_offset + (1.0 + showcase.reflect_percent) * (@height - 2 * sheet.trim) + 2 * sheet.margin
  out_cc = Cairo::Context.new(Cairo::ImageSurface.new(out_width, out_height))
  wrapper = Squib::Graphics::CairoContextWrapper.new(out_cc)
  wrapper.set_source_squibcolor(sheet.fill_color)
  wrapper.paint

  cards = range.collect { |i| @cards[i] }
  cards.each_with_index do |card, i|
    trimmed = trim_rounded(card.cairo_surface, sheet.trim, sheet.trim_radius)
    reflected = reflect(trimmed, showcase.reflect_offset, showcase.reflect_percent, showcase.reflect_strength)
    perspectived = perspective(reflected, showcase.scale, showcase.face_right?)
    out_cc.set_source(perspectived, sheet.margin + i * perspectived.width * showcase.offset, sheet.margin)
    out_cc.paint
  end
  out_cc.target.write_to_png("#{sheet.dir}/#{sheet.file}")
end

#sample(str) {|@sample_x, @sample_y| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Define a set of samples on some graph paper

Yields:

  • (@sample_x, @sample_y)


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/squib/sample_helpers.rb', line 19

def sample(str)
  @sample_x ||= 100
  @sample_y ||= 100
  rect x: 460, y: @sample_y - 40, width: 600,
       height: 180, fill_color: '#FFD655', stroke_color: 'black', radius: 15
  text str: str, x: 460, y: @sample_y - 40,
       width: 540, height: 180,
       valign: 'middle', align: 'center',
       font: 'Times New Roman,Serif 24'
  yield @sample_x, @sample_y
  @sample_y += 200
end

#save(opts = {}) ⇒ Object

DSL method. See squib.readthedocs.io



11
12
13
14
15
# File 'lib/squib/api/save.rb', line 11

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 = {}) ⇒ Object

DSL method. See squib.readthedocs.io



18
19
20
21
22
# File 'lib/squib/api/save.rb', line 18

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 = {}) ⇒ Object

DSL method. See squib.readthedocs.io



25
26
27
28
29
30
31
32
33
34
# File 'lib/squib/api/save.rb', line 25

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 = {}) ⇒ Object

DSL method. See squib.readthedocs.io



37
38
39
40
41
42
# File 'lib/squib/api/save.rb', line 37

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 = {}) ⇒ Object

DSL method. See squib.readthedocs.io



10
11
12
13
# File 'lib/squib/api/settings.rb', line 10

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::DEFAULT_FONT : opts[:font]
end

#show_info(config, layout) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Use Logger to show more detail on the run :nodoc:



95
96
97
98
99
# File 'lib/squib/deck.rb', line 95

def show_info(config, layout)
  Squib::logger.info "Squib v#{Squib::VERSION}"
  Squib::logger.info "  building #{@cards.size} #{@width}x#{@height} cards"
  Squib::logger.info "  using #{@backend}"
end

#showcase(opts = {}) ⇒ Object

DSL method. See squib.readthedocs.io



45
46
47
48
49
50
# File 'lib/squib/api/save.rb', line 45

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 = {}) ⇒ Object

DSL method. See squib.readthedocs.io



69
70
71
72
73
74
75
# File 'lib/squib/api/shapes.rb', line 69

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 = {}) ⇒ Object

DSL method. See squib.readthedocs.io



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

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(self).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| ... } ⇒ Object

DSL method. See squib.readthedocs.io

Yields:

  • (embed)


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/squib/api/text.rb', line 11

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 = {}) ⇒ Object

DSL method. See squib.readthedocs.io



45
46
47
48
49
50
# File 'lib/squib/api/shapes.rb', line 45

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

#trim(surface, trim, width, height) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return a new Cairo::ImageSurface that is trimmed from the original

:nodoc:

Parameters:

  • surface

    The surface to trim

  • trim

    The number of pixels around the edge to trim

  • width

    The width of the surface prior to the trim

  • height

    The height of the surface prior to the trim



92
93
94
95
96
97
98
99
100
101
# File 'lib/squib/graphics/save_doc.rb', line 92

def trim(surface, trim, width, height)
  if trim > 0
    tmp = Cairo::ImageSurface.new(width - 2 * trim, height - 2 * trim)
    cc = Cairo::Context.new(tmp)
    cc.set_source(surface, -1 * trim, -1 * trim)
    cc.paint
    surface = tmp
  end
  surface
end

#trim_rounded(src, trim, radius) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

:nodoc:



35
36
37
38
39
40
41
42
# File 'lib/squib/graphics/showcase.rb', line 35

def trim_rounded(src, trim, radius)
  trim_cc = Cairo::Context.new(Cairo::ImageSurface.new(src.width - 2.0 * trim, src.height - 2.0 * trim))
  trim_cc.rounded_rectangle(0, 0, trim_cc.target.width, trim_cc.target.height, radius, radius)
  trim_cc.set_source(src, -1 * trim, -1 * trim)
  trim_cc.clip
  trim_cc.paint
  return trim_cc.target
end

#use_layout(file: 'layout.yml') ⇒ Object

DSL method. See squib.readthedocs.io



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

def use_layout(file: 'layout.yml')
  @layout = LayoutParser.new(@dpi).load_layout(file, @layout)
end

#xlsx(opts = {}) ⇒ Object

DSL method. See squib.readthedocs.io



97
98
99
# File 'lib/squib/api/data.rb', line 97

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