Class: TextBox
- Inherits:
-
Object
- Object
- TextBox
- Defined in:
- lib/text_box.rb
Instance Attribute Summary collapse
-
#generator ⇒ Object
readonly
Returns the value of attribute generator.
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#valign ⇒ Object
Returns the value of attribute valign.
-
#width ⇒ Object
Returns the value of attribute width.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #cr ⇒ Object
- #draw ⇒ Object
- #draw_line(x, y, text, options) ⇒ Object
- #height ⇒ Object
-
#initialize(generator, x, y, width, height, valign) ⇒ TextBox
constructor
A new instance of TextBox.
- #line(text, options = {}) ⇒ Object
- #move(x, y) ⇒ Object
- #set_options(options) ⇒ Object
- #split_lines(text, options) ⇒ Object
- #wrap(text) ⇒ Object
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
#generator ⇒ Object (readonly)
Returns the value of attribute generator.
2 3 4 |
# File 'lib/text_box.rb', line 2 def generator @generator end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
2 3 4 |
# File 'lib/text_box.rb', line 2 def lines @lines end |
#valign ⇒ Object
Returns the value of attribute valign.
2 3 4 |
# File 'lib/text_box.rb', line 2 def valign @valign end |
#width ⇒ Object
Returns the value of attribute width.
2 3 4 |
# File 'lib/text_box.rb', line 2 def width @width end |
#x ⇒ Object
Returns the value of attribute x.
2 3 4 |
# File 'lib/text_box.rb', line 2 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
2 3 4 |
# File 'lib/text_box.rb', line 2 def y @y end |
Instance Method Details
#cr ⇒ Object
9 10 11 |
# File 'lib/text_box.rb', line 9 def cr generator.cr end |
#draw ⇒ Object
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, )| () baseline = line_y + cr.font_extents.ascent# - cr.font_extents.descent draw_line(x, baseline, line, ) # cr.move_to(x, baseline); cr.line_to(x+cr.text_extents(line).width, baseline) # cr.stroke line_y += [: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, ) start = [:align] == :center ? (x + width/2 - cr.text_extents(text).width/2) : x cr.move_to(start, y) cr.show_text(text) cr.fill if [: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 |
#height ⇒ Object
91 92 93 94 95 |
# File 'lib/text_box.rb', line 91 def height lines.sum do |(line, )| [: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, ={}) return if text.empty? return split_lines(text, ) if text.include?("\n") () extents = cr.text_extents(text) [:line_height] ||= cr.font_extents.height if @width && extents.width > width lines.push(*wrap(text).map {|line| [line, ]}) else lines << [text, ] 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 () cr.select_font_face([:face] || "Arial", [:italic] ? Cairo::FontSlant::ITALIC : Cairo::FontSlant::NORMAL, [:bold] ? Cairo::FontWeight::BOLD : Cairo::FontWeight::NORMAL) cr.set_font_size([:size]) if [:size] end |
#split_lines(text, options) ⇒ Object
13 14 15 16 17 |
# File 'lib/text_box.rb', line 13 def split_lines(text, ) text.split(/\r?\n/).each do |line| line(line, ) 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 |