Class: TextBox

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(generator, x, y, width, height, valign) ⇒ TextBox

Returns a new instance of TextBox.



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

def initialize(generator, x, y, width, height, valign)
  @generator, @x, @y, @width, @height, @valign = generator, x, y, width, height, valign
  @lines = []
end

Instance Attribute Details

#generatorObject (readonly)

Returns the value of attribute generator.



2
3
4
# File 'lib/text_box.rb', line 2

def generator
  @generator
end

#linesObject (readonly)

Returns the value of attribute lines.



2
3
4
# File 'lib/text_box.rb', line 2

def lines
  @lines
end

#valignObject

Returns the value of attribute valign.



2
3
4
# File 'lib/text_box.rb', line 2

def valign
  @valign
end

#widthObject

Returns the value of attribute width.



2
3
4
# File 'lib/text_box.rb', line 2

def width
  @width
end

#xObject

Returns the value of attribute x.



2
3
4
# File 'lib/text_box.rb', line 2

def x
  @x
end

#yObject

Returns the value of attribute y.



2
3
4
# File 'lib/text_box.rb', line 2

def y
  @y
end

Instance Method Details

#crObject



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

def cr
  generator.cr
end

#drawObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/text_box.rb', line 54

def draw
  line_y = valign == :center ? y + (@height - height)/2 : y
  cr.line_width = 1
  # cr.rectangle(x, line_y, width, height)
  # cr.stroke
  lines.each do |(line, options)|
    set_options(options)
    baseline = line_y + cr.font_extents.ascent# - cr.font_extents.descent
    draw_line(x, baseline, line, options)
    # cr.move_to(x, baseline); cr.line_to(x+cr.text_extents(line).width, baseline)
    # cr.stroke
    line_y += options[:line_height]
  end
end

#draw_line(x, y, text, options) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/text_box.rb', line 69

def draw_line(x, y, text, options)
  start = options[:align] == :center ? (x + width/2 - cr.text_extents(text).width/2) : x
  cr.move_to(start, y)
  cr.show_text(text)
  cr.fill
  if options[:underline]
    cr.line_width = 1
    underline = y + 3
    cr.move_to(start, underline)
    cr.line_to(start + cr.text_extents(text).width, underline)
    cr.stroke
  end
end

#heightObject



91
92
93
94
95
# File 'lib/text_box.rb', line 91

def height
  lines.sum do |(line, options)|
    options[:line_height]
  end
end

#line(text, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/text_box.rb', line 19

def line(text, options={})
  return if text.empty?
  return split_lines(text, options) if text.include?("\n")
  set_options(options)
  extents = cr.text_extents(text)
  options[:line_height] ||= cr.font_extents.height
  if @width && extents.width > width
    lines.push(*wrap(text).map {|line| [line, options]})
  else
    lines << [text, options]
  end
end

#move(x, y) ⇒ Object



83
84
85
# File 'lib/text_box.rb', line 83

def move(x, y)
  @x, @y = x, y
end

#set_options(options) ⇒ Object



32
33
34
35
36
37
# File 'lib/text_box.rb', line 32

def set_options(options)
  cr.select_font_face(options[:face] || "Arial", 
    options[:italic] ? Cairo::FontSlant::ITALIC : Cairo::FontSlant::NORMAL, 
    options[:bold] ? Cairo::FontWeight::BOLD : Cairo::FontWeight::NORMAL)
  cr.set_font_size(options[:size]) if options[:size]
end

#split_lines(text, options) ⇒ Object



13
14
15
16
17
# File 'lib/text_box.rb', line 13

def split_lines(text, options)
  text.split(/\r?\n/).each do |line|
    line(line, options)
  end
end

#wrap(text) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/text_box.rb', line 39

def wrap(text)
  split = text.split(' ')
  lines = []
  line = []
  until split.empty?
    until cr.text_extents(line.join(' ')).width > width || split.empty?
      line << split.shift
    end
    split.unshift(line.pop) if cr.text_extents(line.join(' ')).width > width && line.size > 1
    lines << line.join(' ')
    line = []
  end
  lines
end