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

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, options = {}) ⇒ Arranger

Returns a new instance of Arranger.



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

def initialize(document, options={})
  @document = document
  @fragments = []
  @unconsumed = []
  @kerning = options[:kerning]
end

Instance Attribute Details

#consumedObject

Returns the value of attribute consumed.



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

def consumed
  @consumed
end

#current_format_stateObject (readonly)

Returns the value of attribute current_format_state.



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

def current_format_state
  @current_format_state
end

#fragmentsObject (readonly)

Returns the value of attribute fragments.



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

def fragments
  @fragments
end

#max_ascenderObject (readonly)

Returns the value of attribute max_ascender.



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

def max_ascender
  @max_ascender
end

#max_descenderObject (readonly)

Returns the value of attribute max_descender.



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

def max_descender
  @max_descender
end

#max_line_heightObject (readonly)

Returns the value of attribute max_line_height.



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

def max_line_height
  @max_line_height
end

#unconsumedObject (readonly)

The following present only for testing purposes



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

def unconsumed
  @unconsumed
end

Instance Method Details

#apply_color_and_font_settings(fragment, &block) ⇒ Object



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

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



136
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
# File 'lib/prawn/text/formatted/arranger.rb', line 136

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



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

def finalize_line
  @unfinalized_line = false
  omit_trailing_whitespace_from_line_width
  @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)


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

def finished?
  @unconsumed.length == 0
end

#font_style(styles) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/prawn/text/formatted/arranger.rb', line 198

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



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

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



86
87
88
89
90
91
92
93
94
# File 'lib/prawn/text/formatted/arranger.rb', line 86

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
56
57
# File 'lib/prawn/text/formatted/arranger.rb', line 50

def line
  if @unfinalized_line
    raise "Lines must be finalized before calling #line"
  end
  @fragments.collect do |fragment|
    fragment.text.dup.force_encoding(::Encoding::UTF_8)
  end.join
end

#line_widthObject



41
42
43
44
45
46
47
48
# File 'lib/prawn/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



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

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



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

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

#repack_unretrievedObject



189
190
191
192
193
194
195
196
# File 'lib/prawn/text/formatted/arranger.rb', line 189

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

#retrieve_fragmentObject



182
183
184
185
186
187
# File 'lib/prawn/text/formatted/arranger.rb', line 182

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/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.space_count
  end
end

#update_last_string(printed, unprinted, normalized_soft_hyphen = nil) ⇒ Object



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

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

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

  load_previous_format_state if printed.empty?
end