Class: Prawn::Format::Instructions::Text

Inherits:
Base
  • Object
show all
Defined in:
lib/prawn/format/instructions/text.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#ascent, #descent, #state

Instance Method Summary collapse

Methods inherited from Base

#end_verbatim?, #force_break?, #start_verbatim?, #style

Constructor Details

#initialize(state, text, options = {}) ⇒ Text

Returns a new instance of Text.


12
13
14
15
16
17
18
# File 'lib/prawn/format/instructions/text.rb', line 12

def initialize(state, text, options={})
  super(state)
  @text = text
  @break = options.key?(:break) ? options[:break] : text.index(/[-\xE2\x80\x94\s]/)
  @discardable = options.key?(:discardable) ? options[:discardable] : text.index(/\s/)
  @text = state.font.normalize_encoding(@text) if options.fetch(:normalize, true)
end

Instance Attribute Details

#textObject (readonly)

Returns the value of attribute text


10
11
12
# File 'lib/prawn/format/instructions/text.rb', line 10

def text
  @text
end

Instance Method Details

#accumulate(list) ⇒ Object


25
26
27
28
29
30
31
32
33
# File 'lib/prawn/format/instructions/text.rb', line 25

def accumulate(list)
  if list.last.is_a?(Text) && list.last.state == state
    list.last.text << @text
  else
    list.push(dup)
  end

  return list
end

#break?Boolean

Returns:

  • (Boolean)

47
48
49
# File 'lib/prawn/format/instructions/text.rb', line 47

def break?
  @break
end

#compatible?(with) ⇒ Boolean

Returns:

  • (Boolean)

55
56
57
# File 'lib/prawn/format/instructions/text.rb', line 55

def compatible?(with)
  with.is_a?(self.class) && with.state == state
end

#discardable?Boolean

Returns:

  • (Boolean)

51
52
53
# File 'lib/prawn/format/instructions/text.rb', line 51

def discardable?
  @discardable
end

#draw(document, draw_state, options = {}) ⇒ Object


73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/prawn/format/instructions/text.rb', line 73

def draw(document, draw_state, options={})
  @state.apply!(draw_state[:text], draw_state[:cookies])

  encoded_text = @state.font.encode_text(@text, :kerning => @state.kerning?)
  encoded_text.each do |subset, chunk|
    @state.apply_font!(draw_state[:text], draw_state[:cookies], subset)
    draw_state[:text].show(chunk)
  end
  draw_state[:dx] += width

  draw_state[:dx] += draw_state[:padding] * spaces if draw_state[:padding]
end

#dupObject


20
21
22
23
# File 'lib/prawn/format/instructions/text.rb', line 20

def dup
  self.class.new(state, @text.dup, :normalize => false,
    :break => @break, :discardable => @discardable)
end

#height(ignore_discardable = false) ⇒ Object


39
40
41
42
43
44
45
# File 'lib/prawn/format/instructions/text.rb', line 39

def height(ignore_discardable=false)
  if ignore_discardable && discardable?
    0
  else
    @height
  end
end

#spacesObject


35
36
37
# File 'lib/prawn/format/instructions/text.rb', line 35

def spaces
  @spaces ||= @text.scan(/ /).length
end

#to_sObject


69
70
71
# File 'lib/prawn/format/instructions/text.rb', line 69

def to_s
  @text
end

#width(type = :all) ⇒ Object


59
60
61
62
63
64
65
66
67
# File 'lib/prawn/format/instructions/text.rb', line 59

def width(type=:all)
  @width ||= @state.font.compute_width_of(@text, :size => @state.font_size, :kerning => @state.kerning?)

  case type
  when :discardable then discardable? ? @width : 0
  when :nondiscardable then discardable? ? 0 : @width
  else @width
  end
end