Class: Squib::Sprue
- Inherits:
-
Object
- Object
- Squib::Sprue
- Includes:
- Args::ColorValidator
- Defined in:
- lib/squib/sprues/sprue.rb
Constant Summary collapse
- DEFAULTS =
Defaults are set for poker sized deck on a A4 sheet, with no cards
{ 'sheet_width' => nil, 'sheet_height' => nil, 'card_width' => nil, 'card_height' => nil, 'dpi' => 300, 'cell_px' => 37.5, 'position_reference' => :topleft, 'rotate' => 0.0, 'crop_line' => { 'style' => :solid, 'width' => '0.02mm', 'color' => :black, 'overlay' => :on_margin, 'lines' => [] }, 'cards' => [] }.freeze
Instance Attribute Summary collapse
- #dpi ⇒ Object readonly
Class Method Summary collapse
-
.load(file, dpi, cell_px) ⇒ Object
Load the template definition file.
-
.warn_unrecognized(yaml) ⇒ Object
Warn unrecognized options in the template sheet.
Instance Method Summary collapse
- #card_default_rotation ⇒ Object
- #card_height ⇒ Object
- #card_width ⇒ Object
- #cards ⇒ Object
- #crop_line_overlay ⇒ Object
- #crop_lines ⇒ Object
-
#initialize(template_hash, dpi, cell_px) ⇒ Sprue
constructor
A new instance of Sprue.
- #margin ⇒ Object
- #sheet_height ⇒ Object
- #sheet_width ⇒ Object
Methods included from Args::ColorValidator
Constructor Details
#initialize(template_hash, dpi, cell_px) ⇒ Sprue
Returns a new instance of Sprue.
36 37 38 39 40 41 42 43 |
# File 'lib/squib/sprues/sprue.rb', line 36 def initialize(template_hash, dpi, cell_px) @template_hash = template_hash @dpi = dpi @cell_px = cell_px @crop_line_default = @template_hash['crop_line'].select do |k, _| %w[style width color].include? k end end |
Instance Attribute Details
#dpi ⇒ Object (readonly)
34 35 36 |
# File 'lib/squib/sprues/sprue.rb', line 34 def dpi @dpi end |
Class Method Details
.load(file, dpi, cell_px) ⇒ Object
Load the template definition file
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/squib/sprues/sprue.rb', line 46 def self.load(file, dpi, cell_px) yaml = {} 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 sprue new_hash = DEFAULTS.merge(yaml) 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) end Sprue.new new_hash, dpi, cell_px end |
.warn_unrecognized(yaml) ⇒ Object
Warn unrecognized options in the template sheet
135 136 137 138 139 140 141 142 |
# File 'lib/squib/sprues/sprue.rb', line 135 def self.warn_unrecognized(yaml) unrec = yaml.keys - DEFAULTS.keys return unless unrec.any? Squib.logger.warn( "Unrecognized configuration option(s): #{unrec.join(',')}" ) end |
Instance Method Details
#card_default_rotation ⇒ Object
86 87 88 |
# File 'lib/squib/sprues/sprue.rb', line 86 def card_default_rotation parse_rotate_param @template_hash['rotate'] end |
#card_height ⇒ Object
82 83 84 |
# File 'lib/squib/sprues/sprue.rb', line 82 def card_height Args::UnitConversion.parse @template_hash['card_height'], @dpi, @cell_px end |
#card_width ⇒ Object
78 79 80 |
# File 'lib/squib/sprues/sprue.rb', line 78 def card_width Args::UnitConversion.parse @template_hash['card_width'], @dpi, @cell_px end |
#cards ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/squib/sprues/sprue.rb', line 105 def cards parsed_cards = @template_hash['cards'].map(&method(:parse_card)) if block_given? parsed_cards.each { |v| yield v } else parsed_cards end end |
#crop_line_overlay ⇒ Object
90 91 92 |
# File 'lib/squib/sprues/sprue.rb', line 90 def @template_hash['crop_line']['overlay'] end |
#crop_lines ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/squib/sprues/sprue.rb', line 94 def crop_lines lines = @template_hash['crop_line']['lines'].map( &method(:parse_crop_line) ) if block_given? lines.each { |v| yield v } else lines end end |
#margin ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/squib/sprues/sprue.rb', line 114 def margin # NOTE: There's a baseline of 0.25mm that we can 100% make sure that we # can overlap really thin lines on the PDF crop_line_width = [ Args::UnitConversion.parse(@template_hash['crop_line']['width'], @dpi, @cell_px), Args::UnitConversion.parse('0.25mm', @dpi, @cell_px) ].max parsed_cards = cards left, right = parsed_cards.minmax { |a, b| a['x'] <=> b['x'] } top, bottom = parsed_cards.minmax { |a, b| a['y'] <=> b['y'] } { left: left['x'] - crop_line_width, right: right['x'] + card_width + crop_line_width, top: top['y'] - crop_line_width, bottom: bottom['y'] + card_height + crop_line_width } end |
#sheet_height ⇒ Object
74 75 76 |
# File 'lib/squib/sprues/sprue.rb', line 74 def sheet_height Args::UnitConversion.parse @template_hash['sheet_height'], @dpi, @cell_px end |
#sheet_width ⇒ Object
70 71 72 |
# File 'lib/squib/sprues/sprue.rb', line 70 def sheet_width Args::UnitConversion.parse @template_hash['sheet_width'], @dpi, @cell_px end |