prawn 0.11.1.pre → 0.11.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (204) hide show
  1. data/COPYING +2 -340
  2. data/HACKING +1 -1
  3. data/LICENSE +3 -3
  4. data/Rakefile +17 -6
  5. data/data/encodings/win_ansi.txt +1 -1
  6. data/data/images/prawn.png +0 -0
  7. data/data/pdfs/form.pdf +820 -0
  8. data/data/pdfs/multipage_template.pdf +127 -0
  9. data/examples/bounding_box/bounding_boxes.rb +4 -3
  10. data/examples/bounding_box/indentation.rb +2 -1
  11. data/examples/bounding_box/russian_boxes.rb +3 -2
  12. data/examples/bounding_box/stretched_nesting.rb +2 -1
  13. data/examples/general/background.rb +2 -1
  14. data/examples/general/canvas.rb +2 -1
  15. data/examples/general/context_sensitive_headers.rb +2 -1
  16. data/examples/general/float.rb +2 -1
  17. data/examples/general/margin.rb +2 -1
  18. data/examples/general/measurement_units.rb +2 -1
  19. data/examples/general/metadata-info.rb +2 -1
  20. data/examples/general/multi_page_layout.rb +2 -1
  21. data/examples/general/outlines.rb +2 -1
  22. data/examples/general/page_geometry.rb +2 -1
  23. data/examples/general/page_numbering.rb +27 -2
  24. data/examples/general/page_templates.rb +20 -0
  25. data/examples/general/repeaters.rb +2 -1
  26. data/examples/general/stamp.rb +4 -3
  27. data/examples/general/templates.rb +2 -1
  28. data/examples/graphics/basic_images.rb +2 -1
  29. data/examples/graphics/cmyk.rb +2 -1
  30. data/examples/graphics/curves.rb +4 -3
  31. data/examples/graphics/gradient.rb +23 -0
  32. data/examples/graphics/hexagon.rb +3 -2
  33. data/examples/graphics/image_fit.rb +3 -2
  34. data/examples/graphics/image_flow.rb +2 -1
  35. data/examples/graphics/image_position.rb +3 -2
  36. data/examples/graphics/line.rb +2 -1
  37. data/examples/graphics/png_types.rb +3 -2
  38. data/examples/graphics/polygons.rb +3 -2
  39. data/examples/graphics/remote_images.rb +2 -1
  40. data/examples/graphics/rounded_polygons.rb +2 -1
  41. data/examples/graphics/rounded_rectangle.rb +2 -1
  42. data/examples/graphics/ruport_style_helpers.rb +3 -2
  43. data/examples/graphics/stroke_bounds.rb +2 -1
  44. data/examples/graphics/stroke_cap_and_join.rb +2 -1
  45. data/examples/graphics/stroke_dash.rb +2 -1
  46. data/examples/graphics/transformations.rb +2 -1
  47. data/examples/graphics/transparency.rb +4 -3
  48. data/examples/grid/bounding_boxes.rb +2 -1
  49. data/examples/grid/column_gutter_grid.rb +2 -1
  50. data/examples/grid/multi_boxes.rb +2 -1
  51. data/examples/grid/show_grid.rb +2 -1
  52. data/examples/grid/simple_grid.rb +2 -1
  53. data/examples/m17n/chinese_text_wrapping.rb +2 -1
  54. data/examples/m17n/euro.rb +3 -2
  55. data/examples/m17n/full_win_ansi_character_list.rb +20 -0
  56. data/examples/m17n/sjis.rb +2 -1
  57. data/examples/m17n/utf8.rb +3 -2
  58. data/examples/m17n/win_ansi_charset.rb +2 -1
  59. data/examples/security/hello_foo.rb +2 -1
  60. data/examples/table/bill.rb +2 -1
  61. data/examples/table/borders.rb +25 -0
  62. data/examples/table/cell.rb +3 -2
  63. data/examples/table/checkerboard.rb +2 -1
  64. data/examples/table/header.rb +3 -2
  65. data/examples/table/inline_format_table.rb +2 -1
  66. data/examples/table/multi_page_table.rb +2 -1
  67. data/examples/table/simple_table.rb +2 -1
  68. data/examples/table/subtable.rb +2 -1
  69. data/examples/table/widths.rb +2 -1
  70. data/examples/text/alignment.rb +2 -1
  71. data/examples/text/character_spacing.rb +2 -1
  72. data/examples/text/dfont.rb +2 -1
  73. data/examples/text/family_based_styling.rb +3 -2
  74. data/examples/text/font_calculations.rb +2 -1
  75. data/examples/text/font_size.rb +2 -1
  76. data/examples/text/hyphenation.rb +2 -2
  77. data/examples/text/indent_paragraphs.rb +7 -5
  78. data/examples/text/inline_format.rb +7 -6
  79. data/examples/text/kerning.rb +2 -1
  80. data/examples/text/rendering_mode.rb +21 -0
  81. data/examples/text/rotated.rb +2 -1
  82. data/examples/text/shaped_text_box.rb +2 -1
  83. data/examples/text/simple_text.rb +2 -1
  84. data/examples/text/simple_text_ttf.rb +2 -1
  85. data/examples/text/span.rb +3 -2
  86. data/examples/text/text_box.rb +7 -5
  87. data/examples/text/text_box_returning_excess.rb +4 -3
  88. data/examples/text/text_flow.rb +2 -1
  89. data/lib/prawn.rb +1 -1
  90. data/lib/prawn/core/object_store.rb +42 -14
  91. data/lib/prawn/core/page.rb +22 -8
  92. data/lib/prawn/core/text.rb +141 -13
  93. data/lib/prawn/core/text/formatted/arranger.rb +39 -12
  94. data/lib/prawn/core/text/formatted/line_wrap.rb +205 -60
  95. data/lib/prawn/core/text/formatted/wrap.rb +72 -35
  96. data/lib/prawn/document.rb +174 -70
  97. data/lib/prawn/document/bounding_box.rb +122 -83
  98. data/lib/prawn/document/column_box.rb +113 -0
  99. data/lib/prawn/document/graphics_state.rb +90 -2
  100. data/lib/prawn/document/internals.rb +5 -3
  101. data/lib/prawn/errors.rb +5 -0
  102. data/lib/prawn/font.rb +4 -4
  103. data/lib/prawn/font/afm.rb +11 -0
  104. data/lib/prawn/font/ttf.rb +5 -0
  105. data/lib/prawn/graphics.rb +77 -14
  106. data/lib/prawn/graphics/cap_style.rb +13 -5
  107. data/lib/prawn/graphics/color.rb +54 -35
  108. data/lib/prawn/graphics/dash.rb +27 -16
  109. data/lib/prawn/graphics/gradient.rb +84 -0
  110. data/lib/prawn/graphics/join_style.rb +12 -3
  111. data/lib/prawn/graphics/transparency.rb +4 -4
  112. data/lib/prawn/images.rb +18 -160
  113. data/lib/prawn/images/jpg.rb +39 -0
  114. data/lib/prawn/images/png.rb +130 -0
  115. data/lib/prawn/repeater.rb +6 -13
  116. data/lib/prawn/security.rb +6 -1
  117. data/lib/prawn/stamp.rb +12 -4
  118. data/lib/prawn/table.rb +36 -4
  119. data/lib/prawn/table/cell.rb +224 -63
  120. data/lib/prawn/table/cell/text.rb +20 -10
  121. data/lib/prawn/table/cells.rb +23 -6
  122. data/lib/prawn/text.rb +54 -91
  123. data/lib/prawn/text/box.rb +29 -283
  124. data/lib/prawn/text/formatted/box.rb +349 -24
  125. data/lib/prawn/text/formatted/fragment.rb +63 -2
  126. data/lib/prawn/text/formatted/parser.rb +2 -1
  127. data/prawn.gemspec +21 -5
  128. data/spec/bounding_box_spec.rb +61 -28
  129. data/spec/cell_spec.rb +168 -30
  130. data/spec/document_spec.rb +187 -3
  131. data/spec/extensions/mocha.rb +45 -0
  132. data/spec/font_spec.rb +32 -1
  133. data/spec/formatted_text_arranger_spec.rb +35 -40
  134. data/spec/formatted_text_box_spec.rb +287 -443
  135. data/spec/formatted_text_fragment_spec.rb +87 -0
  136. data/spec/graphics_spec.rb +128 -12
  137. data/spec/grid_spec.rb +1 -1
  138. data/spec/images_spec.rb +11 -3
  139. data/spec/inline_formatted_text_parser_spec.rb +8 -0
  140. data/spec/line_wrap_spec.rb +200 -208
  141. data/spec/object_store_spec.rb +10 -0
  142. data/spec/outline_spec.rb +7 -3
  143. data/spec/repeater_spec.rb +58 -10
  144. data/spec/security_spec.rb +6 -0
  145. data/spec/spec_helper.rb +12 -8
  146. data/spec/stamp_spec.rb +52 -1
  147. data/spec/stroke_styles_spec.rb +30 -0
  148. data/spec/table_spec.rb +93 -3
  149. data/spec/template_spec.rb +132 -6
  150. data/spec/text_at_spec.rb +14 -4
  151. data/spec/text_box_spec.rb +309 -70
  152. data/spec/text_rendering_mode_spec.rb +45 -0
  153. data/spec/text_spec.rb +60 -17
  154. data/spec/text_with_inline_formatting_spec.rb +4 -162
  155. metadata +241 -241
  156. data/lib/prawn/core/text/line_wrap.rb +0 -211
  157. data/lib/prawn/core/text/wrap.rb +0 -82
  158. data/vendor/pdf-inspector/README +0 -18
  159. data/vendor/pdf-inspector/lib/pdf/inspector.rb +0 -26
  160. data/vendor/pdf-inspector/lib/pdf/inspector/extgstate.rb +0 -18
  161. data/vendor/pdf-inspector/lib/pdf/inspector/graphics.rb +0 -131
  162. data/vendor/pdf-inspector/lib/pdf/inspector/page.rb +0 -25
  163. data/vendor/pdf-inspector/lib/pdf/inspector/text.rb +0 -46
  164. data/vendor/pdf-inspector/lib/pdf/inspector/xobject.rb +0 -19
  165. data/vendor/ttfunk/data/fonts/DejaVuSans.ttf +0 -0
  166. data/vendor/ttfunk/data/fonts/comicsans.ttf +0 -0
  167. data/vendor/ttfunk/example.rb +0 -45
  168. data/vendor/ttfunk/lib/ttfunk.rb +0 -102
  169. data/vendor/ttfunk/lib/ttfunk/directory.rb +0 -17
  170. data/vendor/ttfunk/lib/ttfunk/encoding/mac_roman.rb +0 -88
  171. data/vendor/ttfunk/lib/ttfunk/encoding/windows_1252.rb +0 -69
  172. data/vendor/ttfunk/lib/ttfunk/reader.rb +0 -44
  173. data/vendor/ttfunk/lib/ttfunk/resource_file.rb +0 -78
  174. data/vendor/ttfunk/lib/ttfunk/subset.rb +0 -18
  175. data/vendor/ttfunk/lib/ttfunk/subset/base.rb +0 -141
  176. data/vendor/ttfunk/lib/ttfunk/subset/mac_roman.rb +0 -50
  177. data/vendor/ttfunk/lib/ttfunk/subset/unicode.rb +0 -48
  178. data/vendor/ttfunk/lib/ttfunk/subset/unicode_8bit.rb +0 -63
  179. data/vendor/ttfunk/lib/ttfunk/subset/windows_1252.rb +0 -55
  180. data/vendor/ttfunk/lib/ttfunk/subset_collection.rb +0 -72
  181. data/vendor/ttfunk/lib/ttfunk/table.rb +0 -46
  182. data/vendor/ttfunk/lib/ttfunk/table/cmap.rb +0 -34
  183. data/vendor/ttfunk/lib/ttfunk/table/cmap/format00.rb +0 -54
  184. data/vendor/ttfunk/lib/ttfunk/table/cmap/format04.rb +0 -126
  185. data/vendor/ttfunk/lib/ttfunk/table/cmap/subtable.rb +0 -79
  186. data/vendor/ttfunk/lib/ttfunk/table/glyf.rb +0 -64
  187. data/vendor/ttfunk/lib/ttfunk/table/glyf/compound.rb +0 -81
  188. data/vendor/ttfunk/lib/ttfunk/table/glyf/simple.rb +0 -37
  189. data/vendor/ttfunk/lib/ttfunk/table/head.rb +0 -44
  190. data/vendor/ttfunk/lib/ttfunk/table/hhea.rb +0 -41
  191. data/vendor/ttfunk/lib/ttfunk/table/hmtx.rb +0 -47
  192. data/vendor/ttfunk/lib/ttfunk/table/kern.rb +0 -79
  193. data/vendor/ttfunk/lib/ttfunk/table/kern/format0.rb +0 -62
  194. data/vendor/ttfunk/lib/ttfunk/table/loca.rb +0 -43
  195. data/vendor/ttfunk/lib/ttfunk/table/maxp.rb +0 -40
  196. data/vendor/ttfunk/lib/ttfunk/table/name.rb +0 -125
  197. data/vendor/ttfunk/lib/ttfunk/table/os2.rb +0 -78
  198. data/vendor/ttfunk/lib/ttfunk/table/post.rb +0 -91
  199. data/vendor/ttfunk/lib/ttfunk/table/post/format10.rb +0 -43
  200. data/vendor/ttfunk/lib/ttfunk/table/post/format20.rb +0 -35
  201. data/vendor/ttfunk/lib/ttfunk/table/post/format25.rb +0 -23
  202. data/vendor/ttfunk/lib/ttfunk/table/post/format30.rb +0 -17
  203. data/vendor/ttfunk/lib/ttfunk/table/post/format40.rb +0 -17
  204. data/vendor/ttfunk/lib/ttfunk/table/simple.rb +0 -14
@@ -6,10 +6,16 @@
6
6
  #
7
7
  # This is free software. Please see the LICENSE and COPYING files for details.
8
8
  #
9
+
10
+ require 'prawn/document/graphics_state'
11
+
9
12
  module Prawn
10
13
  module Core
11
14
  class Page #:nodoc:
12
- attr_accessor :document, :content, :dictionary, :margins
15
+
16
+ include Prawn::Core::Page::GraphicsState
17
+
18
+ attr_accessor :document, :content, :dictionary, :margins, :stack
13
19
 
14
20
  def initialize(document, options={})
15
21
  @document = document
@@ -17,12 +23,12 @@ module Prawn
17
23
  :right => 36,
18
24
  :top => 36,
19
25
  :bottom => 36 }
20
-
26
+ @stack = Prawn::GraphicStateStack.new(options[:graphic_state])
21
27
  if options[:object_id]
22
28
  init_from_object(options)
23
29
  else
24
30
  init_new_page(options)
25
- end
31
+ end
26
32
  end
27
33
 
28
34
  def layout
@@ -47,10 +53,15 @@ module Prawn
47
53
  def stamp_stream(dictionary)
48
54
  @stamp_stream = ""
49
55
  @stamp_dictionary = dictionary
56
+ graphic_stack_size = stack.stack.size
50
57
 
51
- document.send(:update_colors)
58
+ document.save_graphics_state
59
+ document.send(:freeze_stamp_graphics)
52
60
  yield if block_given?
53
- document.send(:update_colors)
61
+
62
+ until graphic_stack_size == stack.stack.size
63
+ document.restore_graphics_state
64
+ end
54
65
 
55
66
  @stamp_dictionary.data[:Length] = @stamp_stream.length + 1
56
67
  @stamp_dictionary << @stamp_stream
@@ -75,6 +86,7 @@ module Prawn
75
86
  end
76
87
  @content = document.ref(:Length => 0)
77
88
  dictionary.data[:Contents] << document.state.store[@content]
89
+ document.open_graphics_state
78
90
  end
79
91
 
80
92
  def dictionary
@@ -158,10 +170,11 @@ module Prawn
158
170
  end
159
171
 
160
172
  def init_new_page(options)
161
- @size = options[:size] || "LETTER"
162
- @layout = options[:layout] || :portrait
163
-
173
+ @size = options[:size] || "LETTER"
174
+ @layout = options[:layout] || :portrait
175
+
164
176
  @content = document.ref(:Length => 0)
177
+ content << "q" << "\n"
165
178
  @dictionary = document.ref(:Type => :Page,
166
179
  :Parent => document.state.store.pages,
167
180
  :MediaBox => dimensions,
@@ -194,6 +207,7 @@ module Prawn
194
207
  end
195
208
 
196
209
  end
210
+
197
211
  end
198
212
  end
199
213
 
@@ -6,8 +6,6 @@
6
6
  #
7
7
  # This is free software. Please see the LICENSE and COPYING files for details.
8
8
 
9
- ruby_18 { $KCODE="U" }
10
-
11
9
  module Prawn
12
10
  module Core
13
11
  module Text #:nodoc:
@@ -15,6 +13,9 @@ module Prawn
15
13
  # These should be used as a base. Extensions may build on this list
16
14
  #
17
15
  VALID_OPTIONS = [:kerning, :size, :style]
16
+ MODES = { :fill => 0, :stroke => 1, :fill_stroke => 2, :invisible => 3,
17
+ :fill_clip => 4, :stroke_clip => 5, :fill_stroke_clip => 6,
18
+ :clip => 7 }
18
19
 
19
20
  attr_reader :skip_encoding
20
21
 
@@ -43,35 +44,162 @@ module Prawn
43
44
  options[:size] ||= font_size
44
45
  end
45
46
 
46
- # Document wide setting of whether or not to use kerning with text
47
+ # Retrieve the current default kerning setting.
48
+ #
47
49
  # Defaults to true
48
- # Can be overridden using the :kerning text option
49
50
  #
50
51
  def default_kerning?
51
52
  return true if @default_kerning.nil?
52
53
  @default_kerning
53
54
  end
54
55
 
56
+ # Call with a boolean to set the document-wide kerning setting. This can be
57
+ # overridden using the :kerning text option when drawing text or a text
58
+ # box.
59
+ #
60
+ # pdf.default_kerning = false
61
+ # pdf.text("hello world") # text is not kerned
62
+ # pdf.text("hello world", :kerning => true) # text is kerned
63
+ #
55
64
  def default_kerning(boolean)
56
65
  @default_kerning = boolean
57
66
  end
58
67
 
59
68
  alias_method :default_kerning=, :default_kerning
60
69
 
61
- # Document wide setting of leading
70
+ # Call with no argument to retrieve the current default leading.
71
+ #
72
+ # Call with a number to set the document-wide text leading. This can be
73
+ # overridden using the :leading text option when drawing text or a text
74
+ # box.
75
+ #
76
+ # pdf.default_leading = 7
77
+ # pdf.text("hello world") # a leading of 7 is used
78
+ # pdf.text("hello world", :leading => 0) # a leading of 0 is used
79
+ #
62
80
  # Defaults to 0
63
- # Can be overridden using the :leading text option
64
81
  #
65
- def default_leading?
66
- return 0 if @default_leading.nil?
67
- @default_leading
82
+ def default_leading(number=nil)
83
+ if number.nil?
84
+ return 0 if @default_leading.nil?
85
+ @default_leading
86
+ else
87
+ @default_leading = number
88
+ end
68
89
  end
69
90
 
70
- def default_leading(number)
71
- @default_leading = number
91
+ alias_method :default_leading=, :default_leading
92
+
93
+ # Call with no argument to retrieve the current text direction.
94
+ #
95
+ # Call with a symbol to set the document-wide text direction. This can be
96
+ # overridden using the :direction text option when drawing text or a text
97
+ # box.
98
+ #
99
+ # pdf.text_direction = :rtl
100
+ # pdf.text("hello world") # prints "dlrow olleh"
101
+ # pdf.text("hello world", :direction => :ltr) # prints "hello world"
102
+ #
103
+ # Valid directions are:
104
+ #
105
+ # * :ltr - left-to-right (default)
106
+ # * :rtl - right-to-left
107
+ #
108
+ # Side effects:
109
+ #
110
+ # * When printing left-to-right, the default text alignment is :left
111
+ # * When printing right-to-left, the default text alignment is :right
112
+ #
113
+ def text_direction(direction=nil)
114
+ if direction.nil?
115
+ return :ltr if @text_direction.nil?
116
+ @text_direction
117
+ else
118
+ @text_direction = direction
119
+ end
72
120
  end
73
121
 
74
- alias_method :default_leading=, :default_leading
122
+ alias_method :text_direction=, :text_direction
123
+
124
+ # Call with no argument to retrieve the current fallback fonts.
125
+ #
126
+ # Call with an array of font names. Each name must be the name of an AFM
127
+ # font or the name that was used to register a family of TTF fonts (see
128
+ # Prawn::Document#font_families). If present, then each glyph will be
129
+ # rendered using the first font that includes the glyph, starting with the
130
+ # current font and then moving through :fallback_fonts from left to right.
131
+ #
132
+ # Call with an empty array to turn off fallback fonts
133
+ #
134
+ # file = "#{Prawn::BASEDIR}/data/fonts/gkai00mp.ttf"
135
+ # font_families["Kai"] = {
136
+ # :normal => { :file => file, :font => "Kai" }
137
+ # }
138
+ # file = "#{Prawn::BASEDIR}/data/fonts/Action Man.dfont"
139
+ # font_families["Action Man"] = {
140
+ # :normal => { :file => file, :font => "ActionMan" },
141
+ # }
142
+ # fallback_fonts ["Times-Roman", "Kai"]
143
+ # font "Action Man"
144
+ # text "hello ƒ 你好"
145
+ # > hello prints in Action Man
146
+ # > ƒ prints in Times-Roman
147
+ # > 你好 prints in Kai
148
+ #
149
+ # fallback_fonts [] # clears document-wide fallback fonts
150
+ #
151
+ # Side effects:
152
+ #
153
+ # * Increased overhead when fallback fonts are declared as each glyph is
154
+ # checked to see whether it exists in the current font
155
+ #
156
+ def fallback_fonts(fallback_fonts=nil)
157
+ if fallback_fonts.nil?
158
+ return [] if @fallback_fonts.nil?
159
+ @fallback_fonts
160
+ else
161
+ @fallback_fonts = fallback_fonts
162
+ end
163
+ end
164
+
165
+ alias_method :fallback_fonts=, :fallback_fonts
166
+
167
+ # Call with no argument to retrieve the current text rendering mode.
168
+ #
169
+ # Call with a symbol and block to temporarily change the current
170
+ # text rendering mode.
171
+ #
172
+ # pdf.text_rendering_mode(:stroke) do
173
+ # pdf.text("Outlined Text")
174
+ # end
175
+ #
176
+ # Valid modes are:
177
+ #
178
+ # * :fill - fill text (default)
179
+ # * :stroke - stroke text
180
+ # * :fill_stroke - fill, then stroke text
181
+ # * :invisible - invisible text
182
+ # * :fill_clip - fill text then add to path for clipping
183
+ # * :stroke_clip - stroke text then add to path for clipping
184
+ # * :fill_stroke_clip - fill then stroke text, then add to path for clipping
185
+ # * :clip - add text to path for clipping
186
+ #
187
+ def text_rendering_mode(mode=nil)
188
+ return @text_rendering_mode || :fill if mode.nil?
189
+ unless MODES.keys.include?(mode)
190
+ raise ArgumentError, "mode must be between one of #{MODES.keys.join(', ')} (#{mode})"
191
+ end
192
+ original_mode = text_rendering_mode
193
+ if original_mode == mode
194
+ yield
195
+ else
196
+ @text_rendering_mode = mode
197
+ add_content "\n#{MODES[mode]} Tr"
198
+ yield
199
+ add_content "\n#{MODES[original_mode]} Tr"
200
+ @text_rendering_mode = original_mode
201
+ end
202
+ end
75
203
 
76
204
  # Increases or decreases the space between characters.
77
205
  # For horizontal text, a positive value will increase the space.
@@ -117,7 +245,7 @@ module Prawn
117
245
  add_content "\nBT"
118
246
 
119
247
  if options[:rotate]
120
- rad = options[:rotate].to_i * Math::PI / 180
248
+ rad = options[:rotate].to_f * Math::PI / 180
121
249
  arr = [ Math.cos(rad), Math.sin(rad), -Math.sin(rad), Math.cos(rad), x, y ]
122
250
  add_content "%.3f %.3f %.3f %.3f %.3f %.3f Tm" % arr
123
251
  else
@@ -34,7 +34,7 @@ module Prawn
34
34
  raise "Lines must be finalized before calling #space_count"
35
35
  end
36
36
  @fragments.inject(0) do |sum, fragment|
37
- sum + fragment.text.count(" ")
37
+ sum + fragment.space_count
38
38
  end
39
39
  end
40
40
 
@@ -51,12 +51,18 @@ module Prawn
51
51
  if @unfinalized_line
52
52
  raise "Lines must be finalized before calling #line"
53
53
  end
54
- @fragments.collect { |fragment| fragment.text }.join("")
54
+ @fragments.collect do |fragment|
55
+ if ruby_18 { true }
56
+ fragment.text
57
+ else
58
+ fragment.text.dup.force_encoding("utf-8")
59
+ end
60
+ end.join
55
61
  end
56
62
 
57
63
  def finalize_line
58
64
  @unfinalized_line = false
59
- remove_trailing_whitespace_from_consumed
65
+ omit_trailing_whitespace_from_line_width
60
66
  @fragments = []
61
67
  @consumed.each do |hash|
62
68
  text = hash[:text]
@@ -86,6 +92,7 @@ module Prawn
86
92
  @max_line_height = 0
87
93
  @max_descender = 0
88
94
  @max_ascender = 0
95
+
89
96
  @consumed = []
90
97
  @fragments = []
91
98
  end
@@ -94,10 +101,6 @@ module Prawn
94
101
  @unconsumed.length == 0
95
102
  end
96
103
 
97
- def unfinished?
98
- @unconsumed.length > 0
99
- end
100
-
101
104
  def next_string
102
105
  unless @unfinalized_line
103
106
  raise "Lines must not be finalized when calling #next_string"
@@ -162,17 +165,22 @@ module Prawn
162
165
  end
163
166
  end
164
167
 
165
- def update_last_string(printed, unprinted)
168
+ def update_last_string(printed, unprinted, normalized_soft_hyphen=nil)
166
169
  return if printed.nil?
167
170
  if printed.empty?
168
171
  @consumed.pop
169
172
  else
170
173
  @consumed.last[:text] = printed
174
+ if normalized_soft_hyphen
175
+ @consumed.last[:normalized_soft_hyphen] = normalized_soft_hyphen
176
+ end
171
177
  end
172
178
 
173
179
  unless unprinted.empty?
174
180
  @unconsumed.unshift(@current_format_state.merge(:text => unprinted))
175
181
  end
182
+
183
+ load_previous_format_state if printed.empty?
176
184
  end
177
185
 
178
186
  def retrieve_fragment
@@ -185,6 +193,7 @@ module Prawn
185
193
  def repack_unretrieved
186
194
  new_unconsumed = []
187
195
  while fragment = retrieve_fragment
196
+ fragment.include_trailing_white_space!
188
197
  new_unconsumed << fragment.format_state.merge(:text => fragment.text)
189
198
  end
190
199
  @unconsumed = new_unconsumed.concat(@unconsumed)
@@ -206,9 +215,24 @@ module Prawn
206
215
 
207
216
  private
208
217
 
218
+ def load_previous_format_state
219
+ if @consumed.empty?
220
+ @current_format_state = {}
221
+ else
222
+ hash = @consumed.last
223
+ @current_format_state = hash.dup
224
+ @current_format_state.delete(:text)
225
+ end
226
+ end
227
+
209
228
  def apply_font_size(size, styles)
210
229
  if subscript?(styles) || superscript?(styles)
211
- size = @document.font_size * 0.583
230
+ relative_size = 0.583
231
+ if size.nil?
232
+ size = @document.font_size * relative_size
233
+ else
234
+ size = size * relative_size
235
+ end
212
236
  end
213
237
  if size.nil?
214
238
  yield
@@ -229,14 +253,17 @@ module Prawn
229
253
  end
230
254
  end
231
255
 
232
- def remove_trailing_whitespace_from_consumed
256
+ def omit_trailing_whitespace_from_line_width
233
257
  @consumed.reverse_each do |hash|
234
258
  if hash[:text] == "\n"
235
259
  break
236
260
  elsif hash[:text].strip.empty? && @consumed.length > 1
237
- @consumed.pop
261
+ # this entire fragment is trailing white space
262
+ hash[:exclude_trailing_white_space] = true
238
263
  else
239
- hash[:text].rstrip!
264
+ # this fragment contains the first non-white space we have
265
+ # encountered since the end of the line
266
+ hash[:exclude_trailing_white_space] = true
240
267
  break
241
268
  end
242
269
  end
@@ -13,7 +13,21 @@ module Prawn
13
13
  module Text
14
14
  module Formatted #:nodoc:
15
15
 
16
- class LineWrap < Prawn::Core::Text::LineWrap #:nodoc:
16
+ class LineWrap #:nodoc:
17
+
18
+ # The width of the last wrapped line
19
+ #
20
+ def width
21
+ @accumulated_width || 0
22
+ end
23
+
24
+ # The number of spaces in the last wrapped line
25
+ attr_reader :space_count
26
+
27
+ # Whether this line is the last line in the paragraph
28
+ def paragraph_finished?
29
+ @newline_encountered || is_next_string_newline? || @arranger.finished?
30
+ end
17
31
 
18
32
  # Work in conjunction with the Prawn::Core::Formatted::Arranger
19
33
  # defined in the :arranger option to determine what formatted text
@@ -23,25 +37,15 @@ module Prawn
23
37
  initialize_line(options)
24
38
 
25
39
  while fragment = @arranger.next_string
26
- @output = ""
27
- preview = @arranger.preview_next_string
28
-
29
- fragment.lstrip! if @line_output.empty? && fragment != "\n"
30
- if @line_output.empty? && fragment.empty? && preview == "\n"
31
- # this line was just whitespace followed by a newline, which is
32
- # equivalent to just a newline
33
- @arranger.update_last_string("", "")
34
- next
35
- end
36
-
37
- if !add_fragment_to_line(fragment)
38
- fragment_finished(fragment, true)
40
+ @fragment_output = ""
41
+
42
+ fragment.lstrip! if first_fragment_on_this_line?(fragment)
43
+ next if empty_line?(fragment)
44
+
45
+ unless apply_font_settings_and_add_fragment_to_line(fragment)
39
46
  break
40
47
  end
41
-
42
- fragment_finished(fragment, preview == "\n" || preview.nil?)
43
48
  end
44
-
45
49
  @arranger.finalize_line
46
50
  @accumulated_width = @arranger.line_width
47
51
  @space_count = @arranger.space_count
@@ -50,76 +54,217 @@ module Prawn
50
54
 
51
55
  private
52
56
 
53
- def initialize_line(options)
54
- @document = options[:document]
55
- @kerning = options[:kerning]
56
- @width = options[:width]
57
-
58
- @scan_pattern = scan_pattern
59
- @word_division_scan_pattern = word_division_scan_pattern
60
-
61
- @accumulated_width = 0
62
- @line_output = ""
57
+ def first_fragment_on_this_line?(fragment)
58
+ line_empty? && fragment != "\n"
59
+ end
63
60
 
64
- @arranger = options[:arranger]
65
- @arranger.initialize_line
61
+ def empty_line?(fragment)
62
+ empty = line_empty? && fragment.empty? && is_next_string_newline?
63
+ @arranger.update_last_string("", "", soft_hyphen) if empty
64
+ empty
66
65
  end
67
66
 
68
- def fragment_finished(fragment, finished_line)
69
- if fragment == "\n"
70
- @line_output = "\n" if @line_output.empty?
71
- else
72
- update_output_based_on_last_fragment(fragment, finished_line)
73
- @line_output += @output
74
- end
67
+ def is_next_string_newline?
68
+ @arranger.preview_next_string == "\n"
75
69
  end
76
70
 
77
- def update_output_based_on_last_fragment(fragment, finished_line)
78
- remaining_text = fragment.slice(@output.length..fragment.length)
79
- @output.rstrip! if finished_line
80
- raise Errors::CannotFit if finished_line && @line_output.empty? &&
81
- @output.empty? && !fragment.strip.empty?
82
- @arranger.update_last_string(@output, remaining_text)
71
+ def apply_font_settings_and_add_fragment_to_line(fragment)
72
+ result = nil
73
+ @arranger.apply_font_settings do
74
+ result = add_fragment_to_line(fragment)
75
+ end
76
+ result
83
77
  end
84
78
 
85
79
  # returns true iff all text was printed without running into the end of
86
80
  # the line
87
81
  #
88
82
  def add_fragment_to_line(fragment)
89
- return true if fragment == ""
90
- return false if fragment == "\n"
91
- previous_segment = nil
92
- fragment.scan(@scan_pattern).each do |segment|
93
- @arranger.apply_font_settings do
94
- segment_width = @document.width_of(segment, :kerning => @kerning)
83
+ if fragment == ""
84
+ true
85
+ elsif fragment == "\n"
86
+ @newline_encountered = true
87
+ false
88
+ else
89
+ fragment.scan(scan_pattern).each do |segment|
90
+ if segment == zero_width_space
91
+ segment_width = 0
92
+ else
93
+ segment_width = @document.width_of(segment, :kerning => @kerning)
94
+ end
95
95
 
96
96
  if @accumulated_width + segment_width <= @width
97
97
  @accumulated_width += segment_width
98
- @output += segment
98
+ @fragment_output += segment
99
99
  else
100
- end_of_the_line(segment)
100
+ end_of_the_line_reached(segment)
101
+ fragment_finished(fragment)
101
102
  return false
102
103
  end
103
104
  end
104
- previous_segment = segment
105
+
106
+ fragment_finished(fragment)
107
+ true
105
108
  end
106
- true
107
109
  end
108
110
 
109
- # If there is more than one word on the line, then clean up the last
110
- # word on the line; otherwise, wrap by character
111
+ # The pattern used to determine chunks of text to place on a given line
111
112
  #
112
- def end_of_the_line(segment)
113
- if (@line_output + @output) =~ @word_division_scan_pattern
114
- if segment =~ new_regexp("^#{hyphen}") &&
115
- @output !~ new_regexp("[#{break_chars}]$")
116
- remove_last_output_word
113
+ def scan_pattern
114
+ pattern = "[^#{break_chars}]+#{soft_hyphen}|" +
115
+ "[^#{break_chars}]+#{hyphen}+|" +
116
+ "[^#{break_chars}]+|" +
117
+ "[#{whitespace}]+|" +
118
+ "#{hyphen}+[^#{break_chars}]*|" +
119
+ "#{soft_hyphen}"
120
+ new_regexp(pattern)
121
+ end
122
+
123
+ # The pattern used to determine whether any word breaks exist on a
124
+ # current line, which in turn determines whether character level
125
+ # word breaking is needed
126
+ #
127
+ def word_division_scan_pattern
128
+ new_regexp("\\s|[#{zero_width_space}#{soft_hyphen}#{hyphen}]")
129
+ end
130
+
131
+ def break_chars
132
+ "#{whitespace}#{soft_hyphen}#{hyphen}"
133
+ end
134
+
135
+ def whitespace
136
+ " \\t#{zero_width_space}"
137
+ end
138
+
139
+ def hyphen
140
+ "-"
141
+ end
142
+
143
+ def soft_hyphen
144
+ @document.font.normalize_encoding(Prawn::Text::SHY)
145
+ end
146
+
147
+ def zero_width_space
148
+ @document.font.unicode? ? Prawn::Text::ZWSP : ""
149
+ end
150
+
151
+ def line_empty?
152
+ @line_empty && @accumulated_width == 0
153
+ end
154
+
155
+ def initialize_line(options)
156
+ @document = options[:document]
157
+ @kerning = options[:kerning]
158
+ @width = options[:width]
159
+
160
+ @accumulated_width = 0
161
+ @line_empty = true
162
+ @line_contains_more_than_one_word = false
163
+
164
+ @arranger = options[:arranger]
165
+ @arranger.initialize_line
166
+
167
+ @newline_encountered = false
168
+ @line_full = false
169
+ end
170
+
171
+ def fragment_finished(fragment)
172
+ if fragment == "\n"
173
+ @newline_encountered = true
174
+ @line_empty = false
175
+ else
176
+ update_output_based_on_last_fragment(fragment, soft_hyphen)
177
+ update_line_status_based_on_last_output
178
+ determine_whether_to_pull_preceding_fragment_to_join_this_one(fragment)
179
+ end
180
+ remember_this_fragment_for_backward_looking_ops
181
+ end
182
+
183
+ def update_output_based_on_last_fragment(fragment, normalized_soft_hyphen=nil)
184
+ remaining_text = fragment.slice(@fragment_output.length..fragment.length)
185
+ raise Errors::CannotFit if line_finished? && line_empty? &&
186
+ @fragment_output.empty? && !fragment.strip.empty?
187
+ @arranger.update_last_string(@fragment_output, remaining_text, normalized_soft_hyphen)
188
+ end
189
+
190
+ def determine_whether_to_pull_preceding_fragment_to_join_this_one(current_fragment)
191
+ if @fragment_output.empty? &&
192
+ !current_fragment.empty? &&
193
+ @line_contains_more_than_one_word
194
+ unless previous_fragment_ended_with_breakable? ||
195
+ fragment_begins_with_breakable?(current_fragment)
196
+ @fragment_output = @previous_fragment_output_without_last_word
197
+ update_output_based_on_last_fragment(@previous_fragment)
198
+ end
199
+ end
200
+ end
201
+
202
+ def remember_this_fragment_for_backward_looking_ops
203
+ @previous_fragment = @fragment_output.dup
204
+ pf = @previous_fragment
205
+ @previous_fragment_ended_with_breakable = pf =~ /[#{break_chars}]$/
206
+ last_word_length = pf.slice(/[^#{break_chars}]*$/).length
207
+ @previous_fragment_output_without_last_word = pf.slice(0, pf.length - last_word_length)
208
+ end
209
+
210
+ def previous_fragment_ended_with_breakable?
211
+ @previous_fragment_ended_with_breakable
212
+ end
213
+
214
+ def fragment_begins_with_breakable?(fragment)
215
+ fragment =~ /^[#{break_chars}]/
216
+ end
217
+
218
+ def line_finished?
219
+ @line_full || paragraph_finished?
220
+ end
221
+
222
+ def update_line_status_based_on_last_output
223
+ @line_contains_more_than_one_word = true if @fragment_output =~ word_division_scan_pattern
224
+ end
225
+
226
+ def end_of_the_line_reached(segment)
227
+ update_line_status_based_on_last_output
228
+ wrap_by_char(segment) unless @line_contains_more_than_one_word
229
+ @line_full = true
230
+ end
231
+
232
+ def wrap_by_char(segment)
233
+ if @document.font.unicode?
234
+ segment.unpack("U*").each do |char_int|
235
+ break unless append_char([char_int].pack("U"))
117
236
  end
118
237
  else
119
- wrap_by_char(segment)
238
+ segment.each_char do |char|
239
+ break unless append_char(char)
240
+ end
120
241
  end
121
242
  end
122
243
 
244
+ def append_char(char)
245
+ # kerning doesn't make sense in the context of a single character
246
+ char_width = @document.width_of(char)
247
+
248
+ if @accumulated_width + char_width <= @width
249
+ @accumulated_width += char_width
250
+ @fragment_output << char
251
+ true
252
+ else
253
+ false
254
+ end
255
+ end
256
+
257
+ def new_regexp(pattern)
258
+ regexp = ruby_19 {
259
+ Regexp.new(pattern)
260
+ }
261
+ regexp = regexp || ruby_18 {
262
+ lang = @document.font.unicode? ? 'U' : 'N'
263
+ Regexp.new(pattern, 0, lang)
264
+ }
265
+ regexp
266
+ end
267
+
123
268
  end
124
269
  end
125
270
  end