Class: Prune::Shapes::TextBox

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

Instance Attribute Summary collapse

Attributes inherited from Base

#id, #x, #y

Instance Method Summary collapse

Methods included from Functions

#mm_to_pt, #pt_to_mm

Constructor Details

#initialize(page, string, options = {}) ⇒ TextBox

Initialize.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/prune/shapes/text_box.rb', line 10

def initialize(page, string, options = {})
  super(page)
  # Set instance variables.
  @id = options[:id] if options.key?(:id)
  @x = @page.x
  @y = @page.y
  @options = options
  # Erase "\r\n" and "\r" line feeds.
  @string = string.gsub(/\r\n/, "\n").gsub(/\r/, "\n")
  @string << "\n" unless /\n\z/ === @string
  # Parse font options.
  Elements::Page::FONT_OPTIONS.each do |sym|
    @page.current_font[sym] = @page.default_font[sym]
    if options.key?(:font) && options[:font].key?(sym)
      @page.current_font[sym] = options[:font][sym]
    end
  end
  # Get font.
  @font = get_font(@page.current_font[:name],
    :bold => @page.current_font[:bold],
    :italic => @page.current_font[:italic])
  raise FontNotSpecifiedError if @font.nil?
  @font_size = @page.current_font[:size]
  # Text align.
  @text_align = options[:text_align] || :left
  @width = get_width(@string, @font, @font_size, options[:width])
  @height = get_height(@string, @font, @font_size, options[:height])
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



7
8
9
# File 'lib/prune/shapes/text_box.rb', line 7

def height
  @height
end

#widthObject

Returns the value of attribute width.



6
7
8
# File 'lib/prune/shapes/text_box.rb', line 6

def width
  @width
end

Instance Method Details

#renderObject

Render shape.



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
# File 'lib/prune/shapes/text_box.rb', line 40

def render
  # Define local variables.
  current_x = @x
  current_y = @y
  boundary = [@x, @y, @width, @height]

  # Render background color.
  render_background_color(boundary)
  # Render borders.
  render_rectangle_border(boundary)
  render_top_border(boundary)
  render_left_border(boundary)
  render_right_border(boundary)
  render_bottom_border(boundary)

  # Render String.
  stream = []
  current_y -= @font_size
  stream << "q"
  # Set colors.
  stroke_color = @page.current_font[:stroke_color]
  fill_color = @page.current_font[:fill_color]
  mode = font_mode(@page.current_font[:mode])
  stream << "%s RG" % convert_color_str(stroke_color)
  stream << "%s rg" % convert_color_str(fill_color)

  # Write String.
  x_before_align = current_x
  @string.scan(/([^\n]*)\n/) do |token|
    line = $1
    line_width = @font.width(line, @font_size)
    current_x =
      x_of_text_align(line_width, @width, x_before_align, @text_align)
    # Set to stream.
    if line.size > 0
      stream << "BT"
      stream << "%s %d Tf" % [@font.name, @font_size]
      stream << "%.2f %.2f Td" % [current_x, current_y]
      stream << "%d Tr" % mode
      stream << "%s Tj" % @font.decode(line)
      stream << "ET"
    end
    current_y -= @font_size
  end
  stream << "Q"
  super(stream)
end