lib/squib/graphics/save_sprue.rb in squib-0.14.beta1 vs lib/squib/graphics/save_sprue.rb in squib-0.14.0
- old
+ new
@@ -1,49 +1,40 @@
module Squib
module Graphics
# Helper class to generate templated sheet.
class SaveSprue
- def initialize(deck, tmpl, outfile)
+ def initialize(deck, tmpl, sheet_args)
@deck = deck
@tmpl = tmpl
@page_number = 1
- @outfile = outfile
- @rotated_delta = (@tmpl.card_width - @deck.width).abs / 2
+ @sheet_args = sheet_args # might be Args::Sheet or Args::SaveBatch
@overlay_lines = @tmpl.crop_lines.select do |line|
line['overlay_on_cards']
end
end
def render_sheet(range)
cc = init_cc
cc.set_source_color(:white) # white backdrop TODO make option
cc.paint
- card_set = @tmpl.cards
- per_sheet = card_set.size
- default_angle = @tmpl.card_default_rotation
- if default_angle.zero?
- default_angle = check_card_orientation
- end
+ slots = @tmpl.cards
+ per_sheet = slots.size
+ check_oversized_card
draw_overlay_below_cards cc if range.size
track_progress(range) do |bar|
range.each do |i|
cc = next_page_if_needed(cc, i, per_sheet)
card = @deck.cards[i]
- slot = card_set[i % per_sheet]
- x = slot['x']
- y = slot['y']
- angle = slot['rotate'] != 0 ? slot['rotate'] : default_angle
+ slot = slots[i % per_sheet]
- if angle != 0
- draw_rotated_card cc, card, x, y, angle
- else
- cc.set_source card.cairo_surface, x, y
- end
- cc.paint
+ draw_card cc, card,
+ slot['x'], slot['y'],
+ slot['rotate'],
+ @sheet_args.trim, @sheet_args.trim_radius
bar.increment
end
draw_overlay_above_cards cc
@@ -128,54 +119,40 @@
cc.set_dash(line['style'].pattern) if line['style'].pattern
cc.stroke
end
end
- def check_card_orientation
- clockwise = 1.5 * Math::PI
- if @deck.width <= @tmpl.card_width &&
- @deck.height <= @tmpl.card_height
- return 0
- elsif (
- @deck.width == @tmpl.card_height &&
- @deck.height == @tmpl.card_width)
- Squib.logger.warn {
- 'Rotating cards to match card orientation in template.'
- }
- return clockwise
- end
-
+ def check_oversized_card
Squib.logger.warn {
- 'Card size is larger than sprue\'s expected card size. '\
- 'Cards may overlap.'
- }
- return 0
+ "Card size is larger than sprue's expected card size "\
+ "of #{@tmpl.card_width}x#{@tmpl.card_height}. Cards may overlap."
+ } if (@deck.width - 2.0 * @sheet_args.trim) > @tmpl.card_width ||
+ (@deck.height - 2.0 * @sheet_args.trim) > @tmpl.card_height
end
- def draw_rotated_card(cc, card, x, y, angle)
+ def draw_card(cc, card, x, y, angle, trim, trim_radius)
+ # Compute the true size of the card after trimming
+ w = @deck.width - 2.0 * trim
+ h = @deck.height - 2.0 * trim
+
# Normalize the angles first
+ # TODO do this in the args class
angle = angle % (2 * Math::PI)
angle = 2 * Math::PI - angle if angle < 0
- # Determine what's the delta we need to translate our cards
- delta_shift = @deck.width < @deck.height ? 1 : -1
- if angle.zero? || angle == Math::PI
- delta = 0
- elsif angle < Math::PI
- delta = -delta_shift * @rotated_delta
- else
- delta = delta_shift * @rotated_delta
- end
-
# Perform the actual rotation and drawing
mat = cc.matrix # Save the transformation matrix to revert later
cc.translate x, y
- cc.translate @deck.width / 2, @deck.height / 2
+ cc.translate @deck.width / 2.0, @deck.height / 2.0
cc.rotate angle
- cc.translate(-@deck.width / 2 + delta, -@deck.height / 2 + delta)
+ cc.translate -@deck.width / 2.0, -@deck.height / 2.0
+ cc.rounded_rectangle(trim, trim, w, h, trim_radius, trim_radius) # clip
+ cc.clip
cc.set_source card.cairo_surface, 0, 0
cc.matrix = mat
+ cc.paint
+ cc.reset_clip
end
end
# Templated sheet renderer in PDF format.
class SaveSpruePDF < SaveSprue
@@ -199,11 +176,11 @@
cc.paint
cc
end
def full_filename
- @outfile.full_filename
+ @sheet_args.full_filename
end
end
# Templated sheet renderer in PNG format.
class SaveSpruePNG < SaveSprue
@@ -219,10 +196,10 @@
cc.paint
cc
end
def full_filename
- @outfile.full_filename @page_number
+ @sheet_args.full_filename @page_number
end
end
end
end