Class: Squib::Sprue

Inherits:
Object
  • Object
show all
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,
  '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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Args::ColorValidator

#colorify

Constructor Details

#initialize(template_hash, dpi) ⇒ Sprue

Returns a new instance of Sprue



35
36
37
38
39
40
41
# File 'lib/squib/sprues/sprue.rb', line 35

def initialize(template_hash, dpi)
  @template_hash = template_hash
  @dpi = dpi
  @crop_line_default = @template_hash['crop_line'].select do |k, _|
    %w[style width color].include? k
  end
end

Instance Attribute Details

#dpiObject (readonly)



33
34
35
# File 'lib/squib/sprues/sprue.rb', line 33

def dpi
  @dpi
end

Class Method Details

.load(file, dpi) ⇒ Object

Load the template definition file



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/squib/sprues/sprue.rb', line 44

def self.load(file, dpi)
  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
end

.warn_unrecognized(yaml) ⇒ Object

Warn unrecognized options in the template sheet



133
134
135
136
137
138
139
140
# File 'lib/squib/sprues/sprue.rb', line 133

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_rotationObject



84
85
86
# File 'lib/squib/sprues/sprue.rb', line 84

def card_default_rotation
  parse_rotate_param @template_hash['rotate']
end

#card_heightObject



80
81
82
# File 'lib/squib/sprues/sprue.rb', line 80

def card_height
  Args::UnitConversion.parse @template_hash['card_height'], @dpi
end

#card_widthObject



76
77
78
# File 'lib/squib/sprues/sprue.rb', line 76

def card_width
  Args::UnitConversion.parse @template_hash['card_width'], @dpi
end

#cardsObject



103
104
105
106
107
108
109
110
# File 'lib/squib/sprues/sprue.rb', line 103

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_overlayObject



88
89
90
# File 'lib/squib/sprues/sprue.rb', line 88

def crop_line_overlay
  @template_hash['crop_line']['overlay']
end

#crop_linesObject



92
93
94
95
96
97
98
99
100
101
# File 'lib/squib/sprues/sprue.rb', line 92

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

#marginObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/squib/sprues/sprue.rb', line 112

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),
    Args::UnitConversion.parse('0.25mm', @dpi)
  ].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_heightObject



72
73
74
# File 'lib/squib/sprues/sprue.rb', line 72

def sheet_height
  Args::UnitConversion.parse @template_hash['sheet_height'], @dpi
end

#sheet_widthObject



68
69
70
# File 'lib/squib/sprues/sprue.rb', line 68

def sheet_width
  Args::UnitConversion.parse @template_hash['sheet_width'], @dpi
end