Class: Label

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLabel

margin top devo considerare la linea del template circa un cm.2 meno



6
7
8
9
10
11
12
# File 'lib/printing_labels.rb', line 6

def initialize
  @p_height = 296
  @p_width = 210
  @labels = []
  @start_number = 1
  @label_layout = LabelLayout.new(:type => "Tipo A", :border => true, :n_row => 7, :n_col=> 2, :pm_top => 25, :pm_left => 5, :pm_bottom => 24, :pm_right => 5.5, :l_width => 98, :l_height => 35.5, :lm_left => 0, :lm_right => 0, :lm_top => 0, :lm_bottom => 0, :label_padding => 8, :lm_inter => 2, :text_size => 14, :line_height => 7)       
end

Instance Attribute Details

#label_layoutObject (readonly)

Returns the value of attribute label_layout.



3
4
5
# File 'lib/printing_labels.rb', line 3

def label_layout
  @label_layout
end

#labelsObject (readonly)

Returns the value of attribute labels.



3
4
5
# File 'lib/printing_labels.rb', line 3

def labels
  @labels
end

#p_heightObject (readonly)

Returns the value of attribute p_height.



3
4
5
# File 'lib/printing_labels.rb', line 3

def p_height
  @p_height
end

#p_widthObject (readonly)

Returns the value of attribute p_width.



3
4
5
# File 'lib/printing_labels.rb', line 3

def p_width
  @p_width
end

#startnumberObject

Returns the value of attribute startnumber.



4
5
6
# File 'lib/printing_labels.rb', line 4

def startnumber
  @startnumber
end

#templateObject (readonly)

Returns the value of attribute template.



3
4
5
# File 'lib/printing_labels.rb', line 3

def template
  @template
end

Instance Method Details

#add(text) ⇒ Object



22
23
24
25
# File 'lib/printing_labels.rb', line 22

def add(text)
  item = @labels.find {|i| i.include?("#{text}")}
  @labels << text unless item
end

#empty!Object



31
32
33
# File 'lib/printing_labels.rb', line 31

def empty!
  @labels = []
end

#present?(text) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
112
113
# File 'lib/printing_labels.rb', line 109

def present? text
  item = @labels.find {|i| true if i.inspect == text.inspect}
  return true if item
  return false unless item
end


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
62
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
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/printing_labels.rb', line 35

def print
  pdf = PDF::Writer.new(:paper => "A4")   
  @counter = -1
  
  #pdf configuration
  pdf.stroke_color  Color::RGB::Grey
  
  
  # Set page margins in point
  set_page_margins_in_pt pdf
  
  #Get initial pointer position
  x = pdf.absolute_left_margin
  y = pdf.absolute_top_margin
  
  #label dimension in point
  label_width = pdf.mm2pts(self.label_layout.l_width)
  label_height = pdf.mm2pts(self.label_layout.l_height)
  label_wrap_text = pdf.mm2pts(self.label_layout.l_width) - pdf.mm2pts(self.label_layout.label_padding)
  
  check = false
  
  start_point = self.startnumber.divmod(self.label_layout.n_col)
  # cicle an rows
  (1..self.label_layout.n_row).collect do |r|  
    if r > start_point[0]
      if check == true or start_point[1] == 0
        @counter += 1
        # Show the labels border if :border = true
        pdf.fill_color  Color::RGB::White
        pdf.rounded_rectangle(x, y, label_width, label_height, 5).close_fill_stroke if self.label_layout.border
        pdf.fill_color Color::RGB::Black
        
        ny = y
        self.labels[@counter].split("\n").each do |nl|
          ny -= pdf.mm2pts(self.label_layout.line_height)
          pdf.add_text_wrap(pdf.mm2pts(self.label_layout.label_padding), ny, label_wrap_text, "#{nl}", size = self.label_layout.text_size)
        end unless self.labels[@counter] == nil
        
      end
      # cicle an columns
      (1..self.label_layout.n_col - 1).collect do |i|
        if check == false and i == start_point[1]
          check = true
          @counter += 1
          pdf.fill_color  Color::RGB::White
          pdf.rounded_rectangle(x + (pdf.mm2pts(self.label_layout.l_width + self.label_layout.lm_inter) * i), y, pdf.mm2pts(self.label_layout.l_width), pdf.mm2pts(self.label_layout.l_height), 5).close_fill_stroke if self.label_layout.border
          pdf.fill_color Color::RGB::Black

          ny = y
          self.labels[@counter].split("\n").each do |nl|
            ny -= pdf.mm2pts(self.label_layout.line_height)
            pdf.add_text_wrap((pdf.mm2pts(self.label_layout.l_width + self.label_layout.lm_inter) * i) + pdf.mm2pts(self.label_layout.label_padding), ny, label_wrap_text, "#{nl}", size = self.label_layout.text_size)
          end unless self.labels[@counter] == nil

        else
          @counter += 1
          pdf.fill_color Color::RGB::White
          pdf.rounded_rectangle(x + (pdf.mm2pts(self.label_layout.l_width + self.label_layout.lm_inter) * i), y, pdf.mm2pts(self.label_layout.l_width), pdf.mm2pts(self.label_layout.l_height), 5).close_fill_stroke if self.label_layout.border
          pdf.fill_color Color::RGB::Black

          ny = y
          self.labels[@counter].split("\n").each do |nl|
            ny -= pdf.mm2pts(self.label_layout.line_height)
            pdf.add_text_wrap((pdf.mm2pts(self.label_layout.l_width  + self.label_layout.lm_inter) * i) + pdf.mm2pts(self.label_layout.label_padding), ny, label_wrap_text, "#{nl}", size = self.label_layout.text_size)
          end unless self.labels[@counter] == nil
        end    
      end 
    end
    y -= (pdf.mm2pts(self.label_layout.l_height) + pdf.mm2pts(self.label_layout.lm_bottom))
  end
  return pdf
end

#remove(index) ⇒ Object



27
28
29
# File 'lib/printing_labels.rb', line 27

def remove(index)
  @labels.delete_at(index.to_i)
end

#set_template(args) ⇒ Object



18
19
20
# File 'lib/printing_labels.rb', line 18

def set_template(args)
  @label_layout = LabelLayout.new(args)       
end

#which_template?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/printing_labels.rb', line 14

def which_template?
  @template = @label_layout.type
end