Class: Cahdmaker::Maker

Inherits:
Object
  • Object
show all
Defined in:
lib/cahdmaker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Maker

Returns a new instance of Maker.



9
10
11
# File 'lib/cahdmaker.rb', line 9

def initialize(opts={})
  @opts = { text_size: 60, blank_size: 10, bleed: 0, resolution: 300, font: 'Helvetica-Bold' }.merge(opts)
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



7
8
9
# File 'lib/cahdmaker.rb', line 7

def opts
  @opts
end

Instance Method Details

#black(text, pick = 1, &block) ⇒ Object



13
14
15
# File 'lib/cahdmaker.rb', line 13

def black(text, pick=1, &block)
  make_card(text, "black#{pick}".to_sym, &block)
end

#black2(text, &block) ⇒ Object



17
18
19
# File 'lib/cahdmaker.rb', line 17

def black2(text, &block)
  black(text, 2, &block)
end

#black3(text, &block) ⇒ Object



21
22
23
# File 'lib/cahdmaker.rb', line 21

def black3(text, &block)
  black(text, 3, &block)
end

#logo(black_card) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cahdmaker.rb', line 34

def (black_card)
   = Magick::Draw.new
  .stroke_color('black')
  .stroke_opacity(0.8)
  .stroke_linecap('round')
  .stroke_linejoin('round')
  .stroke_width((0.75/72)*opts[:resolution])
  
  fan_x = px(0.03,0)
  fan_y = px(0.01,0)
  
  .translate(px(0.40),px(3.20))
  
  .translate(-fan_x,fan_y)
  .rotate(-15)
  .fill(black_card ? 'grey49' : 'black')
  .rectangle(px(-0.075,0), px(-0.150,0), px(0.075,0), 0)
  
  .rotate(15)
  .translate(fan_x,-fan_y)
  .fill(black_card ? 'grey69' : 'grey49')
  .rectangle(px(-0.075,0), px(-0.150,0), px(0.075,0), 0)

  .translate(fan_x,fan_y)
  .rotate(15)
  .fill('white')
  .rectangle(px(-0.075,0), px(-0.150,0), px(0.075,0), 0)
end

#make_card(text, source_card) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/cahdmaker.rb', line 63

def make_card(text, source_card)
  bleed = opts[:bleed].to_i
  resolution = opts[:resolution].to_i
  text_size = opts[:text_size].to_i
  black_card = source_card == :white ? false : true
  background_color = black_card ? 'black' : 'white'
  color = black_card ? 'white' : 'black'
  font = opts[:font]
  text.gsub! /\b_+\b/, ('_' * opts[:blank_size])
  card_width = px(2.0, bleed);
  multiplier = resolution / 300
  
  cap = Magick::Image.read(%{caption:#{text}}) do
    self.size = "#{card_width}x"
    self.background_color = 'transparent'
    self.pointsize = text_size.to_f * multiplier
    self['interline-spacing'] = (text_size/3).to_f * multiplier
    self.font = font
    self.fill = color
  end.first
  cap.x_resolution = cap.y_resolution = resolution

  img = Magick::Image.new(px(2.5,bleed),px(3.5,bleed)) do
    self.background_color = background_color
  end
  img.x_resolution = img.y_resolution = resolution
  (black_card).draw(img)
  img.composite!(cap, px(0.25), px(0.25), Magick::OverCompositeOp)
  if block_given?
    yield(img)
  else
    img
  end
end

#px(ins, bleed = nil) ⇒ Object



29
30
31
32
# File 'lib/cahdmaker.rb', line 29

def px(ins, bleed=nil)
  bleed ||= opts[:bleed]/2
  (ins * opts[:resolution]) + bleed
end

#white(text, &block) ⇒ Object



25
26
27
# File 'lib/cahdmaker.rb', line 25

def white(text, &block)
  make_card(text, :white, &block)
end