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, options = {}) ⇒ Arranger

Returns a new instance of Arranger.



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

#consumedObject

Returns the value of attribute consumed.



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

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



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

def unconsumed
  @unconsumed
end

Instance Method Details

#apply_color_and_font_settings(fragment, &block) ⇒ Object



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

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



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

#finalize_lineObject



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

#finished?Boolean

Returns:

  • (Boolean)


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

def finished?
  @unconsumed.length == 0
end

#font_style(styles) ⇒ Object



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

#format_array=(array) ⇒ Object



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

#initialize_lineObject



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

#lineObject



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

#line_widthObject



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

#next_stringObject



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

#preview_next_stringObject



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

#repack_unretrievedObject



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

#retrieve_fragmentObject



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

#space_countObject



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

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



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