Class: MusicBox::LabelMaker

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLabelMaker

Returns a new instance of LabelMaker.



11
12
13
14
15
16
17
# File 'lib/musicbox/label_maker.rb', line 11

def initialize
  @font_dir = Path.new('~/Fonts/D/DejaVu Sans')
  @pdf = Prawn::Document.new(page_size: [3.5.in, 1.14.in], margin: 0)
  @pdf.font_families.update(font_families)
  @pdf.font('DejaVuSans')
  @pdf.font_size(12)
end

Class Method Details

.make_labels(*labels, output_file:) ⇒ Object



5
6
7
8
9
# File 'lib/musicbox/label_maker.rb', line 5

def self.make_labels(*labels, output_file:)
  label_maker = new
  label_maker.make_labels(labels)
  label_maker.write(output_file)
end

Instance Method Details

#font_familiesObject



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/musicbox/label_maker.rb', line 46

def font_families
  {
    'DejaVuSans' => {
      normal: 'DejaVuSans',
      italic: 'DejaVuSans-Oblique',
      bold: 'DejaVuSans-Bold',
      bold_italic: 'DejaVuSans-BoldOblique',
    }.map { |style, file|
      [style, (@font_dir / file).add_extension('.ttf').realpath.to_s ]
    }.to_h
  }
end

#make_labels(labels) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/musicbox/label_maker.rb', line 19

def make_labels(labels)
  labels.sort_by { |l| l.values_at(:key, :year) }.each_with_index do |label, i|
    @pdf.start_new_page if i > 0
    @pdf.bounding_box([0, 1.in], width: 2.5.in, height: 1.in) do
      # ;;@pdf.transparent(0.5) { @pdf.stroke_bounds }
      @pdf.text_box <<~END, inline_format: true
        <b>#{label[:artist]}</b>
        <i>#{label[:title]}</i>
      END
    end
    @pdf.bounding_box([2.7.in, 1.in], width: 0.8.in, height: 1.in) do
      # ;;@pdf.transparent(0.5) { @pdf.stroke_bounds }
      @pdf.text_box <<~END, align: :right, inline_format: true
        <b>#{label[:key]}
        #{label[:year]}</b>

        #{label[:format]}
        #{label[:id]}
      END
    end
  end
end

#write(output_file) ⇒ Object



42
43
44
# File 'lib/musicbox/label_maker.rb', line 42

def write(output_file)
  @pdf.render_file(output_file.to_s)
end