Class: Prawn::Core::Text::Formatted::Arranger

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/core/text/formatted/arranger.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Arranger

Returns a new instance of Arranger.



26
27
28
29
30
# File 'lib/prawn/core/text/formatted/arranger.rb', line 26

def initialize(document)
  @document = document
  @fragments = []
  @unconsumed = []
end

Instance Attribute Details

#consumedObject (readonly)

Returns the value of attribute consumed.



22
23
24
# File 'lib/prawn/core/text/formatted/arranger.rb', line 22

def consumed
  @consumed
end

#current_format_stateObject (readonly)

Returns the value of attribute current_format_state.



24
25
26
# File 'lib/prawn/core/text/formatted/arranger.rb', line 24

def current_format_state
  @current_format_state
end

#fragmentsObject (readonly)

Returns the value of attribute fragments.



23
24
25
# File 'lib/prawn/core/text/formatted/arranger.rb', line 23

def fragments
  @fragments
end

#max_ascenderObject (readonly)

Returns the value of attribute max_ascender.



18
19
20
# File 'lib/prawn/core/text/formatted/arranger.rb', line 18

def max_ascender
  @max_ascender
end

#max_descenderObject (readonly)

Returns the value of attribute max_descender.



17
18
19
# File 'lib/prawn/core/text/formatted/arranger.rb', line 17

def max_descender
  @max_descender
end

#max_line_heightObject (readonly)

Returns the value of attribute max_line_height.



16
17
18
# File 'lib/prawn/core/text/formatted/arranger.rb', line 16

def max_line_height
  @max_line_height
end

#unconsumedObject (readonly)

The following present only for testing purposes



21
22
23
# File 'lib/prawn/core/text/formatted/arranger.rb', line 21

def unconsumed
  @unconsumed
end

Instance Method Details

#apply_color_and_font_settings(fragment, &block) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/prawn/core/text/formatted/arranger.rb', line 123

def apply_color_and_font_settings(fragment, &block)
  if fragment.color
    original_fill_color = @document.fill_color
    original_stroke_color = @document.stroke_color
    @document.fill_color(*fragment.color)
    @document.stroke_color(*fragment.color)
    apply_font_settings(fragment, &block)
    @document.stroke_color = original_stroke_color
    @document.fill_color = original_fill_color
  else
    apply_font_settings(fragment, &block)
  end
end

#apply_font_settings(fragment = nil, &block) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/prawn/core/text/formatted/arranger.rb', line 137

def apply_font_settings(fragment=nil, &block)
  if fragment.nil?
    font = current_format_state[:font]
    size = current_format_state[:size]
    character_spacing = current_format_state[:character_spacing] ||
                        @document.character_spacing
    styles = current_format_state[:styles]
    font_style = font_style(styles)
  else
    font = fragment.font
    size = fragment.size
    character_spacing = fragment.character_spacing
    styles = fragment.styles
    font_style = font_style(styles)
  end

  @document.character_spacing(character_spacing) do
    if font || font_style != :normal
      raise "Bad font family" unless @document.font.family
      @document.font(font || @document.font.family, :style => font_style) do
        apply_font_size(size, styles, &block)
      end
    else
      apply_font_size(size, styles, &block)
    end
  end
end

#finalize_lineObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/prawn/core/text/formatted/arranger.rb', line 57

def finalize_line
  @unfinalized_line = false
  remove_trailing_whitespace_from_consumed
  @fragments = []
  @consumed.each do |hash|
    text = hash[:text]
    format_state = hash.dup
    format_state.delete(:text)
    fragment = Prawn::Text::Formatted::Fragment.new(text,
                                                    format_state,
                                                    @document)
    @fragments << fragment
    set_fragment_measurements(fragment)
    set_line_measurement_maximums(fragment)
  end
end

#finished?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/prawn/core/text/formatted/arranger.rb', line 93

def finished?
  @unconsumed.length == 0
end

#font_style(styles) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/prawn/core/text/formatted/arranger.rb', line 193

def font_style(styles)
  if styles.nil?
    :normal
  elsif styles.include?(:bold) && styles.include?(:italic)
    :bold_italic
  elsif styles.include?(:bold)
    :bold
  elsif styles.include?(:italic)
    :italic
  else
    :normal
  end
end

#format_array=(array) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/prawn/core/text/formatted/arranger.rb', line 74

def format_array=(array)
  initialize_line
  @unconsumed = []
  array.each do |hash|
    hash[:text].scan(/[^\n]+|\n/) do |line|
      @unconsumed << hash.merge(:text => line)
    end
  end
end

#initialize_lineObject



84
85
86
87
88
89
90
91
# File 'lib/prawn/core/text/formatted/arranger.rb', line 84

def initialize_line
  @unfinalized_line = true
  @max_line_height = 0
  @max_descender = 0
  @max_ascender = 0
  @consumed = []
  @fragments = []
end

#lineObject



50
51
52
53
54
55
# File 'lib/prawn/core/text/formatted/arranger.rb', line 50

def line
  if @unfinalized_line
    raise "Lines must be finalized before calling #line"
  end
  @fragments.collect { |fragment| fragment.text }.join("")
end

#line_widthObject



41
42
43
44
45
46
47
48
# File 'lib/prawn/core/text/formatted/arranger.rb', line 41

def line_width
  if @unfinalized_line
    raise "Lines must be finalized before calling #line_width"
  end
  @fragments.inject(0) do |sum, fragment|
    sum + fragment.width
  end
end

#next_stringObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/prawn/core/text/formatted/arranger.rb', line 101

def next_string
  unless @unfinalized_line
    raise "Lines must not be finalized when calling #next_string"
  end
  hash = @unconsumed.shift
  if hash.nil?
    nil
  else
    @consumed << hash.dup
    @current_format_state = hash.dup
    @current_format_state.delete(:text)
    hash[:text]
  end
end

#preview_next_stringObject



116
117
118
119
120
121
# File 'lib/prawn/core/text/formatted/arranger.rb', line 116

def preview_next_string
  hash = @unconsumed.first
  if hash.nil? then nil
  else hash[:text]
  end
end

#repack_unretrievedObject



185
186
187
188
189
190
191
# File 'lib/prawn/core/text/formatted/arranger.rb', line 185

def repack_unretrieved
  new_unconsumed = []
  while fragment = retrieve_fragment
    new_unconsumed << fragment.format_state.merge(:text => fragment.text)
  end
  @unconsumed = new_unconsumed.concat(@unconsumed)
end

#retrieve_fragmentObject



178
179
180
181
182
183
# File 'lib/prawn/core/text/formatted/arranger.rb', line 178

def retrieve_fragment
  if @unfinalized_line
    raise "Lines must be finalized before fragments can be retrieved"
  end
  @fragments.shift
end

#space_countObject



32
33
34
35
36
37
38
39
# File 'lib/prawn/core/text/formatted/arranger.rb', line 32

def space_count
  if @unfinalized_line
    raise "Lines must be finalized before calling #space_count"
  end
  @fragments.inject(0) do |sum, fragment|
    sum + fragment.text.count(" ")
  end
end

#unfinished?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/prawn/core/text/formatted/arranger.rb', line 97

def unfinished?
  @unconsumed.length > 0
end

#update_last_string(printed, unprinted) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/prawn/core/text/formatted/arranger.rb', line 165

def update_last_string(printed, unprinted)
  return if printed.nil?
  if printed.empty?
    @consumed.pop
  else
    @consumed.last[:text] = printed
  end

  unless unprinted.empty?
    @unconsumed.unshift(@current_format_state.merge(:text => unprinted))
  end
end