Class: Squib::Deck Private
- Inherits:
-
Object
- Object
- Squib::Deck
- 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
-
#cards ⇒ Object
readonly
private
Attributes for the width, height (in pixels) and number of cards These are expected to be immuatble for the life of Deck.
-
#conf ⇒ Object
readonly
private
:nodoc:.
-
#dpi ⇒ Object
readonly
private
:nodoc:.
-
#font ⇒ Object
readonly
private
:nodoc:.
-
#height ⇒ Object
readonly
private
Attributes for the width, height (in pixels) and number of cards These are expected to be immuatble for the life of Deck.
-
#layout ⇒ Object
readonly
private
:nodoc:.
-
#width ⇒ Object
readonly
private
Attributes for the width, height (in pixels) and number of cards These are expected to be immuatble for the life of Deck.
Instance Method Summary collapse
-
#[](key) ⇒ Object
private
Directly accesses the array of cards in the deck.
-
#background(opts = {}) ⇒ Object
DSL method.
-
#build(grp = :all, &block) ⇒ Object
DSL method.
-
#build_groups ⇒ Object
DSL method.
-
#circle(opts = {}) ⇒ Object
DSL method.
-
#cm(n) ⇒ Object
DSL method.
-
#csv(opts = {}) ⇒ Object
DSL method.
-
#curve(opts = {}) ⇒ Object
DSL method.
-
#disable_build(grp) ⇒ Object
DSL method.
-
#draw_graph_paper(width, height) ⇒ Object
private
Draw graph paper for samples.
-
#each(&block) ⇒ Object
private
Iterates over each card in the deck.
-
#ellipse(opts = {}) ⇒ Object
DSL method.
-
#enable_build(grp) ⇒ Object
DSL method.
-
#enable_groups_from_env! ⇒ Object
Not a DSL method, but initialized from Deck.new.
-
#grid(opts = {}) ⇒ Object
DSL method.
-
#hand(opts = {}) ⇒ Object
DSL method.
-
#hint(text: :off) ⇒ Object
DSL method.
-
#inches(n) ⇒ Object
DSL method.
-
#initialize(width: 825, height: 1125, cards: 1, dpi: 300, config: 'config.yml', layout: nil, &block) ⇒ Deck
constructor
Squib’s constructor that sets the immutable properties.
-
#line(opts = {}) ⇒ Object
DSL method.
-
#perspective(src, scale, face_right) ⇒ Object
private
:nodoc:.
-
#png(opts = {}) ⇒ Object
DSL method.
-
#polygon(opts = {}) ⇒ Object
DSL method.
-
#rect(opts = {}) ⇒ Object
DSL method.
-
#reflect(src, roffset, rpercent, rstrength) ⇒ Object
private
:nodoc:.
-
#render_hand(range, sheet, hand) ⇒ Object
private
Draw cards in a fan.
-
#render_pdf(range, sheet) ⇒ Object
private
:nodoc:.
-
#render_sheet(range, batch, sheet) ⇒ Object
private
:nodoc:.
-
#render_showcase(range, sheet, showcase) ⇒ Object
private
So the Cairo people have said over and over again that they won’t support the 3x3 matrices that would handle perspective transforms.
-
#sample(str) {|@sample_x, @sample_y| ... } ⇒ Object
private
Define a set of samples on some graph paper.
-
#save(opts = {}) ⇒ Object
DSL method.
-
#save_pdf(opts = {}) ⇒ Object
DSL method.
-
#save_png(opts = {}) ⇒ Object
DSL method.
-
#save_sheet(opts = {}) ⇒ Object
DSL method.
-
#set(opts = {}) ⇒ Object
DSL method.
-
#show_info(config, layout) ⇒ Object
private
Use Logger to show more detail on the run :nodoc:.
-
#showcase(opts = {}) ⇒ Object
DSL method.
-
#star(opts = {}) ⇒ Object
DSL method.
-
#svg(opts = {}) ⇒ Object
DSL method.
-
#text(opts = {}) {|embed| ... } ⇒ Object
DSL method.
-
#triangle(opts = {}) ⇒ Object
DSL method.
-
#trim(surface, trim, width, height) ⇒ Object
private
Return a new Cairo::ImageSurface that is trimmed from the original.
-
#trim_rounded(src, trim, radius) ⇒ Object
private
:nodoc:.
-
#use_layout(file: 'layout.yml') ⇒ Object
DSL method.
-
#xlsx(opts = {}) ⇒ Object
DSL method.
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.
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.) # 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) enable_groups_from_env! if block_given? instance_eval(&block) # here we go. wheeeee! end end |
Instance Attribute Details
#cards ⇒ Object (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 |
#conf ⇒ Object (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 |
#dpi ⇒ Object (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 |
#font ⇒ Object (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 |
#height ⇒ Object (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 |
#layout ⇒ Object (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 |
#width ⇒ Object (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.org
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.org
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_groups ⇒ Object
DSL method. See squib.readthedocs.org
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.org
19 20 21 22 23 24 |
# File 'lib/squib/api/shapes.rb', line 19 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.org
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.org
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.org
59 60 61 62 63 64 |
# File 'lib/squib/api/shapes.rb', line 59 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.org
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.org
27 28 29 30 31 32 |
# File 'lib/squib/api/shapes.rb', line 27 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) range.each { |i| @cards[i].ellipse(box[i], draw[i]) } end |
#enable_build(grp) ⇒ Object
DSL method. See squib.readthedocs.org
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.org
35 36 37 38 39 40 |
# File 'lib/squib/api/shapes.rb', line 35 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.org
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.org
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.org
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.org
51 52 53 54 55 56 |
# File 'lib/squib/api/shapes.rb', line 51 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 |
#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.org
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 || range.each do |i| @cards[i].png(ifile[i].file, box[i], paint[i], trans[i]) .increment end end end end |
#polygon(opts = {}) ⇒ Object
DSL method. See squib.readthedocs.org
76 77 78 79 80 81 82 |
# File 'lib/squib/api/shapes.rb', line 76 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.org
11 12 13 14 15 16 |
# 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) range.each { |i| @cards[i].rect(box[i], draw[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 || 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 .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 || 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 .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
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.org
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.org
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.org
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 || range.each do |i| @cards[i].save_png(batch[i]) .increment end end end |
#save_sheet(opts = {}) ⇒ Object
DSL method. See squib.readthedocs.org
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.org
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.org
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.org
67 68 69 70 71 72 73 |
# File 'lib/squib/api/shapes.rb', line 67 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.org
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 || 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 .increment end end end end |
#text(opts = {}) {|embed| ... } ⇒ Object
DSL method. See squib.readthedocs.org
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) = TextEmbed.new(size, custom_colors, layout, dpi, img_dir) yield() if block_given? # store the opts for later use extents = Array.new(@cards.size) range.each { |i| extents[i] = @cards[i].text(, para[i], box[i], trans[i], draw[i]) } return extents end |
#triangle(opts = {}) ⇒ Object
DSL method. See squib.readthedocs.org
43 44 45 46 47 48 |
# File 'lib/squib/api/shapes.rb', line 43 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:
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.org
16 17 18 |
# File 'lib/squib/api/settings.rb', line 16 def use_layout(file: 'layout.yml') @layout = LayoutParser.load_layout(file, @layout) end |
#xlsx(opts = {}) ⇒ Object
DSL method. See squib.readthedocs.org
97 98 99 |
# File 'lib/squib/api/data.rb', line 97 def xlsx(opts = {}) Squib.xlsx(opts) end |