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

- (Arranger) initialize(document, options = {})



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

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

Instance Attribute Details

- (Object) consumed

Returns the value of attribute consumed



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

def consumed
  @consumed
end

- (Object) current_format_state (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

- (Object) fragments (readonly)

Returns the value of attribute fragments



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

def fragments
  @fragments
end

- (Object) max_ascender (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

- (Object) max_descender (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

- (Object) max_line_height (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

- (Object) unconsumed (readonly)

The following present only for testing purposes



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

def unconsumed
  @unconsumed
end

Instance Method Details

- (Object) apply_color_and_font_settings(fragment, &block)



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/prawn/core/text/formatted/arranger.rb', line 127

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

- (Object) apply_font_settings(fragment = nil, &block)



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

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

- (Object) finalize_line



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/prawn/core/text/formatted/arranger.rb', line 64

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

- (Boolean) finished?



101
102
103
# File 'lib/prawn/core/text/formatted/arranger.rb', line 101

def finished?
  @unconsumed.length == 0
end

- (Object) font_style(styles)



203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/prawn/core/text/formatted/arranger.rb', line 203

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

- (Object) format_array=(array)



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

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

- (Object) initialize_line



91
92
93
94
95
96
97
98
99
# File 'lib/prawn/core/text/formatted/arranger.rb', line 91

def initialize_line
  @unfinalized_line = true
  @max_line_height = 0
  @max_descender = 0
  @max_ascender = 0

  @consumed = []
  @fragments = []
end

- (Object) line



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/prawn/core/text/formatted/arranger.rb', line 51

def line
  if @unfinalized_line
    raise "Lines must be finalized before calling #line"
  end
  @fragments.collect do |fragment|
    if ruby_18 { true }
      fragment.text
    else
      fragment.text.dup.force_encoding("utf-8")
    end
  end.join
end

- (Object) line_width



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

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

- (Object) next_string



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/prawn/core/text/formatted/arranger.rb', line 105

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

- (Object) preview_next_string



120
121
122
123
124
125
# File 'lib/prawn/core/text/formatted/arranger.rb', line 120

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

- (Object) repack_unretrieved



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

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

- (Object) retrieve_fragment



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

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

- (Object) space_count



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

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

- (Object) update_last_string(printed, unprinted, normalized_soft_hyphen = nil)



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/prawn/core/text/formatted/arranger.rb', line 169

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