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

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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 [email protected]
  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
  @cards.each { |c| c.finish! }
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

#progress_barObject (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 progress_bar
  @progress_bar
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


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

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


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

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


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

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


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

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

#csv(opts = {}) ⇒ Object

DSL method. See squib.readthedocs.io


128
129
130
# File 'lib/squib/api/data.rb', line 128

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

#cut_zone(opts = {}) ⇒ Object

DSL method. See squib.readthedocs.io


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/squib/api/shapes.rb', line 106

def cut_zone(opts = {})
  safe_defaults = {
    margin: '0.125in',
    radius: '0.125in',
    stroke_color: :red,
    fill_color: '#0000',
    stroke_width: 2.0,
  }
  new_opts = safe_defaults.merge(opts)
  margin = Args::UnitConversion.parse new_opts[:margin]
  new_opts[:x] = margin
  new_opts[:y] = margin
  new_opts[:width] = width - (2 * margin)
  new_opts[:height] = height - (2 * margin)
  rect new_opts
end

#disable_build(grp) ⇒ Object

DSL method. See squib.readthedocs.io


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

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


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

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


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

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


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

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


75
76
77
78
79
80
# File 'lib/squib/api/save.rb', line 75

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


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

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

#points(n) ⇒ Object

DSL method. See squib.readthedocs.io


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

def points(n)
  @dpi / Squib::POINTS_PER_IN * n.to_f
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_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:


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
# File 'lib/squib/graphics/save_doc.rb', line 6

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
  y = sheet.margin
  x = sheet.rtl ? (sheet_width - sheet.margin - sheet.gap - @width) : 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
        y = sheet.margin
        x = sheet.rtl ? (sheet_width - sheet.margin - sheet.gap - @width) : 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) * (sheet.rtl ? -1 : 1)
      if num_this_sheet % sheet.columns == 0 # new row
        x = sheet.rtl ? (sheet_width - sheet.margin - sheet.gap - @width) : 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

#safe_zone(opts = {}) ⇒ Object

DSL method. See squib.readthedocs.io


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/squib/api/shapes.rb', line 87

def safe_zone(opts = {})
  safe_defaults = {
    margin: '0.25in',
    radius: '0.125in',
    stroke_color: :blue,
    fill_color: '#0000',
    stroke_width: 1.0,
    dash: '3 3',
  }
  new_opts = safe_defaults.merge(opts)
  margin = Args::UnitConversion.parse new_opts[:margin]
  new_opts[:x] = margin
  new_opts[:y] = margin
  new_opts[:width] = width - (2 * margin)
  new_opts[:height] = height - (2 * margin)
  rect new_opts
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 8'
  yield @sample_x, @sample_y
  @sample_y += 200
end

#save(opts = {}) ⇒ Object

DSL method. See squib.readthedocs.io


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

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


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

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)
  sprue_file = Args::SprueFile.new.load!(opts, expand_by: size)

  if sprue_file.sprue.nil?
    Graphics::SavePDF.new(self).render_pdf(range, sheet)
  else
    tmpl = Sprue.load sprue_file.sprue, dpi
    Graphics::SaveSpruePDF.
      new(self, tmpl, sheet).
      render_sheet(range)
  end
end

#save_png(opts = {}) ⇒ Object

DSL method. See squib.readthedocs.io


38
39
40
41
42
43
44
45
46
47
# File 'lib/squib/api/save.rb', line 38

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


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

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)
  sprue_file = Args::SprueFile.new.load!(opts, expand_by: size)

  if sprue_file.sprue.nil?
    render_sheet(range, batch, sheet)
  else
    tmpl = Sprue.load sprue_file.sprue, dpi
    Graphics::SaveSpruePNG.
      new(self, tmpl, sheet).
      render_sheet(range)
  end
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:


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

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


67
68
69
70
71
72
# File 'lib/squib/api/save.rb', line 67

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], dpi) }
  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:


49
50
51
52
53
54
55
56
57
58
# File 'lib/squib/graphics/save_doc.rb', line 49

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(@width - 2.0 * trim, @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


123
124
125
# File 'lib/squib/api/data.rb', line 123

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

#yaml(opts = {}) ⇒ Object

DSL method. See squib.readthedocs.io


133
134
135
# File 'lib/squib/api/data.rb', line 133

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