sprues: polishing up the feature

Lots of examples, some better checking, lots of documentation, sanity tests. Lots of little stuff.
This commit is contained in:
Andy Meneely
2017-10-05 15:48:57 -04:00
parent 3ccc6432ab
commit 06970b77a6
19 changed files with 208 additions and 170 deletions
@@ -0,0 +1,25 @@
---
sheet_width: 8.5in
sheet_height: 11in
card_width: 2.5in
card_height: 3.5in
position_reference: :center
cards:
- x: 1.75in
y: 2.000in
- x: 4.250in
y: 2.000in
- x: 6.750in
y: 2.000in
- x: 1.75in
y: 5.500in
- x: 4.250in
y: 5.500in
- x: 6.750in
y: 5.500in
- x: 1.75in
y: 9.000in
- x: 4.250in
y: 9.000in
- x: 6.750in
y: 9.000in
@@ -0,0 +1,52 @@
---
sheet_width: 11in
sheet_height: 8.5in
card_width: 2.5in
card_height: 3.5in
cards:
- x: 0.35in
y: 0.225in
- x: 0.35in
y: 4.800in
rotate: :turnaround
- x: 2.95in
y: 0.225in
- x: 2.95in
y: 4.800in
rotate: :turnaround
- x: 5.55in
y: 0.225in
- x: 5.55in
y: 4.800in
rotate: :turnaround
- x: 8.15in
y: 0.225in
- x: 8.15in
y: 4.800in
rotate: :turnaround
crop_line:
lines:
- type: :vertical
position: 0.35in
- type: :vertical
position: 2.85in
- type: :vertical
position: 2.95in
- type: :vertical
position: 5.45in
- type: :vertical
position: 5.55in
- type: :vertical
position: 8.05in
- type: :vertical
position: 8.15in
- type: :vertical
position: 10.65in
- type: :horizontal
position: 0.225in
- type: :horizontal
position: 3.725in
- type: :horizontal
position: 3.825in
- type: :horizontal
position: 7.325in
+16 -17
View File
@@ -15,6 +15,8 @@ module Squib
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
@@ -26,7 +28,7 @@ module Squib
track_progress(range) do |bar|
range.each do |i|
next_page_if_needed(cc, i, per_sheet)
cc = next_page_if_needed(cc, i, per_sheet)
card = @deck.cards[i]
slot = card_set[i % per_sheet]
@@ -45,7 +47,7 @@ module Squib
end
draw_overlay_above_cards cc
draw_page cc
cc = draw_page cc
cc.target.finish
end
end
@@ -68,12 +70,13 @@ module Squib
private
def next_page_if_needed(cc, i, per_sheet)
return unless (i != 0) && (i % per_sheet).zero?
return cc unless (i != 0) && (i % per_sheet).zero?
draw_overlay_above_cards cc
cc = draw_page cc
draw_overlay_below_cards cc
@page_number += 1
cc
end
def track_progress(range)
@@ -129,8 +132,8 @@ module Squib
def check_card_orientation
clockwise = 1.5 * Math::PI
# Simple detection
if @deck.width == @tmpl.card_width && @deck.height == @tmpl.card_height
if @deck.width <= @tmpl.card_width &&
@deck.height <= @tmpl.card_height
return 0
elsif (
@deck.width == @tmpl.card_height &&
@@ -141,20 +144,11 @@ module Squib
return clockwise
end
# If the card dimensions doesn't match, warns the user...
Squib.logger.warn {
'Card size does not match the template\'s expected card size. '\
'Card size is larger than sprue\'s expected card size. '\
'Cards may overlap.'
}
# ... but still try to auto-orient the cards anyway
is_tmpl_card_landscape = @tmpl.card_width > @tmpl.card_height
is_deck_card_landscape = @deck.width > @deck.height
if is_tmpl_card_landscape == is_deck_card_landscape
clockwise
else
0
end
return 0
end
def draw_rotated_card(cc, card, x, y, angle)
@@ -201,6 +195,8 @@ module Squib
def draw_page(cc)
cc.show_page
cc.set_source_color(:white) # white backdrop TODO make option
cc.paint
cc
end
@@ -209,7 +205,7 @@ module Squib
end
end
# Templated sheet renderer in PDF format.
# Templated sheet renderer in PNG format.
class SaveSpruePNG < SaveSprue
def init_cc
surface = Cairo::ImageSurface.new @tmpl.sheet_width, @tmpl.sheet_height
@@ -219,6 +215,9 @@ module Squib
def draw_page(cc)
cc.target.write_to_png(full_filename)
init_cc
cc.set_source_color(:white) # white backdrop TODO make option
cc.paint
cc
end
def full_filename
+2 -2
View File
@@ -136,10 +136,10 @@ module Squib
font_desc.size = para.font_size * Pango::SCALE unless para.font_size.nil?
layout = cc.create_pango_layout
layout.font_description = font_desc
layout.text = para.str
layout.text = para.str.to_s
if para.markup
para.str = @deck.typographer.process(layout.text)
layout.markup = para.str
layout.markup = para.str.to_s
end
set_font_rendering_opts!(layout)
+9 -8
View File
@@ -43,20 +43,21 @@ module Squib
# Load the template definition file
def self.load(file, dpi)
yaml = {}
thefile = File.exist?(file) ? file : builtin(file)
thefile = file if File.exist?(file) # use custom first
thefile = builtin(file) if File.exist?(builtin(file)) # then builtin
unless File.exist?(thefile)
Squib::logger.error("Sprue not found: #{file}. Falling back to defaults.")
end
yaml = YAML.load_file(thefile) || {} if File.exist? thefile
# Bake the default values into our template
# Bake the default values into our sprue
new_hash = DEFAULTS.merge(yaml)
new_hash['crop_line'] = DEFAULTS['crop_line'].merge(
new_hash['crop_line']
)
# Create a new template file
new_hash['crop_line'] = DEFAULTS['crop_line'].
merge(new_hash['crop_line'])
warn_unrecognized(yaml)
# Validate
begin
require 'benchmark'
ClassyHash.validate(new_hash, Sprues::SCHEMA)
rescue ClassyHash::SchemaViolationError => e
raise Sprues::InvalidSprueDefinition.new(thefile, e)