prawn 0.15.0 → 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (269) hide show
  1. data/COPYING +2 -2
  2. data/LICENSE +1 -1
  3. data/README.md +96 -0
  4. data/Rakefile +27 -30
  5. data/data/fonts/Action Man.dfont +0 -0
  6. data/data/fonts/Activa.ttf +0 -0
  7. data/data/fonts/Chalkboard.ttf +0 -0
  8. data/data/fonts/DejaVuSans.ttf +0 -0
  9. data/data/fonts/Dustismo_Roman.ttf +0 -0
  10. data/data/fonts/comicsans.ttf +0 -0
  11. data/data/fonts/gkai00mp.ttf +0 -0
  12. data/data/images/16bit.alpha +0 -0
  13. data/data/images/16bit.dat +0 -0
  14. data/data/images/dice.alpha +0 -0
  15. data/data/images/dice.dat +0 -0
  16. data/data/images/page_white_text.alpha +0 -0
  17. data/data/images/page_white_text.dat +0 -0
  18. data/data/images/rails.dat +0 -0
  19. data/data/images/rails.png +0 -0
  20. data/data/pdfs/nested_pages.pdf +13 -13
  21. data/lib/prawn.rb +21 -85
  22. data/lib/prawn/compatibility.rb +51 -0
  23. data/lib/prawn/core.rb +85 -0
  24. data/lib/prawn/core/annotations.rb +61 -0
  25. data/lib/prawn/core/byte_string.rb +9 -0
  26. data/lib/prawn/core/destinations.rb +90 -0
  27. data/lib/prawn/core/document_state.rb +78 -0
  28. data/lib/prawn/core/literal_string.rb +16 -0
  29. data/lib/prawn/core/name_tree.rb +177 -0
  30. data/lib/prawn/core/object_store.rb +264 -0
  31. data/lib/prawn/core/page.rb +215 -0
  32. data/lib/prawn/core/pdf_object.rb +108 -0
  33. data/lib/prawn/core/reference.rb +115 -0
  34. data/lib/prawn/core/text.rb +268 -0
  35. data/lib/prawn/core/text/formatted/arranger.rb +294 -0
  36. data/lib/prawn/core/text/formatted/line_wrap.rb +273 -0
  37. data/lib/prawn/core/text/formatted/wrap.rb +153 -0
  38. data/lib/prawn/document.rb +122 -155
  39. data/lib/prawn/document/bounding_box.rb +7 -36
  40. data/lib/prawn/document/column_box.rb +10 -38
  41. data/lib/prawn/document/graphics_state.rb +74 -11
  42. data/lib/prawn/document/internals.rb +23 -24
  43. data/lib/prawn/document/page_geometry.rb +136 -0
  44. data/lib/prawn/document/snapshot.rb +6 -7
  45. data/lib/prawn/document/span.rb +10 -12
  46. data/lib/prawn/encoding.rb +10 -9
  47. data/lib/prawn/errors.rb +30 -15
  48. data/lib/prawn/font.rb +104 -136
  49. data/lib/prawn/font/afm.rb +44 -46
  50. data/lib/prawn/font/dfont.rb +3 -4
  51. data/lib/prawn/font/ttf.rb +50 -31
  52. data/lib/prawn/graphics.rb +57 -302
  53. data/lib/prawn/graphics/cap_style.rb +3 -4
  54. data/lib/prawn/graphics/color.rb +5 -13
  55. data/lib/prawn/graphics/dash.rb +31 -53
  56. data/lib/prawn/graphics/gradient.rb +84 -0
  57. data/lib/prawn/graphics/join_style.rb +7 -9
  58. data/lib/prawn/graphics/transformation.rb +9 -10
  59. data/lib/prawn/graphics/transparency.rb +1 -3
  60. data/lib/prawn/images.rb +59 -69
  61. data/lib/prawn/images/image.rb +22 -6
  62. data/lib/prawn/images/jpg.rb +14 -20
  63. data/lib/prawn/images/png.rb +118 -61
  64. data/lib/prawn/layout.rb +15 -10
  65. data/lib/prawn/layout/grid.rb +54 -66
  66. data/lib/prawn/measurement_extensions.rb +6 -10
  67. data/lib/prawn/measurements.rb +21 -27
  68. data/lib/prawn/outline.rb +308 -6
  69. data/lib/prawn/repeater.rb +8 -10
  70. data/lib/prawn/security.rb +33 -55
  71. data/lib/prawn/security/arcfour.rb +0 -1
  72. data/lib/prawn/stamp.rb +3 -5
  73. data/lib/prawn/table.rb +60 -188
  74. data/lib/prawn/table/cell.rb +44 -272
  75. data/lib/prawn/table/cell/image.rb +3 -2
  76. data/lib/prawn/table/cell/in_table.rb +2 -4
  77. data/lib/prawn/table/cell/subtable.rb +2 -2
  78. data/lib/prawn/table/cell/text.rb +18 -41
  79. data/lib/prawn/table/cells.rb +48 -142
  80. data/lib/prawn/text.rb +25 -32
  81. data/lib/prawn/text/box.rb +6 -12
  82. data/lib/prawn/text/formatted.rb +4 -5
  83. data/lib/prawn/text/formatted/box.rb +59 -96
  84. data/lib/prawn/text/formatted/fragment.rb +23 -34
  85. data/lib/prawn/text/formatted/parser.rb +5 -15
  86. data/prawn.gemspec +13 -24
  87. data/spec/annotations_spec.rb +32 -16
  88. data/spec/bounding_box_spec.rb +17 -119
  89. data/spec/cell_spec.rb +42 -112
  90. data/spec/destinations_spec.rb +5 -5
  91. data/spec/document_spec.rb +111 -155
  92. data/spec/extensions/mocha.rb +0 -1
  93. data/spec/font_spec.rb +99 -149
  94. data/spec/formatted_text_arranger_spec.rb +43 -43
  95. data/spec/formatted_text_box_spec.rb +44 -43
  96. data/spec/formatted_text_fragment_spec.rb +8 -8
  97. data/spec/graphics_spec.rb +68 -151
  98. data/spec/grid_spec.rb +15 -26
  99. data/spec/images_spec.rb +30 -51
  100. data/spec/inline_formatted_text_parser_spec.rb +20 -69
  101. data/spec/jpg_spec.rb +4 -4
  102. data/spec/line_wrap_spec.rb +28 -28
  103. data/spec/measurement_units_spec.rb +6 -6
  104. data/spec/name_tree_spec.rb +112 -0
  105. data/spec/object_store_spec.rb +106 -17
  106. data/spec/outline_spec.rb +63 -103
  107. data/spec/pdf_object_spec.rb +170 -0
  108. data/spec/png_spec.rb +25 -25
  109. data/spec/reference_spec.rb +65 -8
  110. data/spec/repeater_spec.rb +10 -10
  111. data/spec/security_spec.rb +12 -44
  112. data/spec/snapshot_spec.rb +7 -7
  113. data/spec/span_spec.rb +15 -10
  114. data/spec/spec_helper.rb +8 -32
  115. data/spec/stamp_spec.rb +30 -29
  116. data/spec/stroke_styles_spec.rb +18 -36
  117. data/spec/table_spec.rb +111 -706
  118. data/spec/template_spec.rb +297 -0
  119. data/spec/text_at_spec.rb +33 -19
  120. data/spec/text_box_spec.rb +64 -100
  121. data/spec/text_rendering_mode_spec.rb +5 -5
  122. data/spec/text_spacing_spec.rb +4 -4
  123. data/spec/text_spec.rb +64 -84
  124. data/spec/transparency_spec.rb +5 -5
  125. metadata +290 -463
  126. checksums.yaml +0 -7
  127. data/.yardopts +0 -10
  128. data/Gemfile +0 -11
  129. data/data/images/16bit.color +0 -0
  130. data/data/images/dice.color +0 -0
  131. data/data/images/indexed_color.dat +0 -0
  132. data/data/images/indexed_color.png +0 -0
  133. data/data/images/page_white_text.color +0 -0
  134. data/lib/prawn/font_metric_cache.rb +0 -47
  135. data/lib/prawn/graphics/patterns.rb +0 -138
  136. data/lib/prawn/image_handler.rb +0 -36
  137. data/lib/prawn/soft_mask.rb +0 -96
  138. data/lib/prawn/table/cell/span_dummy.rb +0 -93
  139. data/lib/prawn/table/column_width_calculator.rb +0 -61
  140. data/lib/prawn/text/formatted/arranger.rb +0 -290
  141. data/lib/prawn/text/formatted/line_wrap.rb +0 -266
  142. data/lib/prawn/text/formatted/wrap.rb +0 -150
  143. data/lib/prawn/utilities.rb +0 -46
  144. data/manual/basic_concepts/adding_pages.rb +0 -27
  145. data/manual/basic_concepts/basic_concepts.rb +0 -34
  146. data/manual/basic_concepts/creation.rb +0 -39
  147. data/manual/basic_concepts/cursor.rb +0 -33
  148. data/manual/basic_concepts/measurement.rb +0 -25
  149. data/manual/basic_concepts/origin.rb +0 -38
  150. data/manual/basic_concepts/other_cursor_helpers.rb +0 -40
  151. data/manual/bounding_box/bounding_box.rb +0 -39
  152. data/manual/bounding_box/bounds.rb +0 -49
  153. data/manual/bounding_box/canvas.rb +0 -24
  154. data/manual/bounding_box/creation.rb +0 -23
  155. data/manual/bounding_box/indentation.rb +0 -46
  156. data/manual/bounding_box/nesting.rb +0 -45
  157. data/manual/bounding_box/russian_boxes.rb +0 -40
  158. data/manual/bounding_box/stretchy.rb +0 -31
  159. data/manual/document_and_page_options/background.rb +0 -27
  160. data/manual/document_and_page_options/document_and_page_options.rb +0 -32
  161. data/manual/document_and_page_options/metadata.rb +0 -23
  162. data/manual/document_and_page_options/page_margins.rb +0 -38
  163. data/manual/document_and_page_options/page_size.rb +0 -34
  164. data/manual/document_and_page_options/print_scaling.rb +0 -20
  165. data/manual/example_file.rb +0 -111
  166. data/manual/example_helper.rb +0 -411
  167. data/manual/example_package.rb +0 -53
  168. data/manual/example_section.rb +0 -46
  169. data/manual/graphics/circle_and_ellipse.rb +0 -22
  170. data/manual/graphics/color.rb +0 -24
  171. data/manual/graphics/common_lines.rb +0 -30
  172. data/manual/graphics/fill_and_stroke.rb +0 -42
  173. data/manual/graphics/fill_rules.rb +0 -37
  174. data/manual/graphics/gradients.rb +0 -37
  175. data/manual/graphics/graphics.rb +0 -58
  176. data/manual/graphics/helper.rb +0 -24
  177. data/manual/graphics/line_width.rb +0 -35
  178. data/manual/graphics/lines_and_curves.rb +0 -41
  179. data/manual/graphics/polygon.rb +0 -29
  180. data/manual/graphics/rectangle.rb +0 -21
  181. data/manual/graphics/rotate.rb +0 -28
  182. data/manual/graphics/scale.rb +0 -41
  183. data/manual/graphics/soft_masks.rb +0 -46
  184. data/manual/graphics/stroke_cap.rb +0 -31
  185. data/manual/graphics/stroke_dash.rb +0 -48
  186. data/manual/graphics/stroke_join.rb +0 -30
  187. data/manual/graphics/translate.rb +0 -29
  188. data/manual/graphics/transparency.rb +0 -35
  189. data/manual/images/absolute_position.rb +0 -23
  190. data/manual/images/fit.rb +0 -21
  191. data/manual/images/horizontal.rb +0 -25
  192. data/manual/images/images.rb +0 -40
  193. data/manual/images/plain_image.rb +0 -18
  194. data/manual/images/scale.rb +0 -22
  195. data/manual/images/vertical.rb +0 -28
  196. data/manual/images/width_and_height.rb +0 -25
  197. data/manual/layout/boxes.rb +0 -27
  198. data/manual/layout/content.rb +0 -25
  199. data/manual/layout/layout.rb +0 -28
  200. data/manual/layout/simple_grid.rb +0 -23
  201. data/manual/manual/cover.rb +0 -36
  202. data/manual/manual/foreword.rb +0 -85
  203. data/manual/manual/how_to_read_this_manual.rb +0 -41
  204. data/manual/manual/manual.rb +0 -34
  205. data/manual/outline/add_subsection_to.rb +0 -61
  206. data/manual/outline/insert_section_after.rb +0 -47
  207. data/manual/outline/outline.rb +0 -32
  208. data/manual/outline/sections_and_pages.rb +0 -67
  209. data/manual/repeatable_content/page_numbering.rb +0 -54
  210. data/manual/repeatable_content/repeatable_content.rb +0 -31
  211. data/manual/repeatable_content/repeater.rb +0 -55
  212. data/manual/repeatable_content/stamp.rb +0 -41
  213. data/manual/security/encryption.rb +0 -31
  214. data/manual/security/permissions.rb +0 -38
  215. data/manual/security/security.rb +0 -28
  216. data/manual/syntax_highlight.rb +0 -52
  217. data/manual/table/basic_block.rb +0 -53
  218. data/manual/table/before_rendering_page.rb +0 -26
  219. data/manual/table/cell_border_lines.rb +0 -24
  220. data/manual/table/cell_borders_and_bg.rb +0 -31
  221. data/manual/table/cell_dimensions.rb +0 -30
  222. data/manual/table/cell_text.rb +0 -38
  223. data/manual/table/column_widths.rb +0 -30
  224. data/manual/table/content_and_subtables.rb +0 -39
  225. data/manual/table/creation.rb +0 -27
  226. data/manual/table/filtering.rb +0 -36
  227. data/manual/table/flow_and_header.rb +0 -17
  228. data/manual/table/image_cells.rb +0 -33
  229. data/manual/table/position.rb +0 -29
  230. data/manual/table/row_colors.rb +0 -20
  231. data/manual/table/span.rb +0 -30
  232. data/manual/table/style.rb +0 -22
  233. data/manual/table/table.rb +0 -52
  234. data/manual/table/width.rb +0 -27
  235. data/manual/text/alignment.rb +0 -44
  236. data/manual/text/color.rb +0 -24
  237. data/manual/text/column_box.rb +0 -32
  238. data/manual/text/fallback_fonts.rb +0 -37
  239. data/manual/text/font.rb +0 -41
  240. data/manual/text/font_size.rb +0 -45
  241. data/manual/text/font_style.rb +0 -23
  242. data/manual/text/formatted_callbacks.rb +0 -60
  243. data/manual/text/formatted_text.rb +0 -54
  244. data/manual/text/free_flowing_text.rb +0 -51
  245. data/manual/text/group.rb +0 -31
  246. data/manual/text/inline.rb +0 -43
  247. data/manual/text/kerning_and_character_spacing.rb +0 -39
  248. data/manual/text/leading.rb +0 -25
  249. data/manual/text/line_wrapping.rb +0 -41
  250. data/manual/text/paragraph_indentation.rb +0 -26
  251. data/manual/text/positioned_text.rb +0 -38
  252. data/manual/text/registering_families.rb +0 -48
  253. data/manual/text/rendering_and_color.rb +0 -37
  254. data/manual/text/right_to_left_text.rb +0 -43
  255. data/manual/text/rotation.rb +0 -43
  256. data/manual/text/single_usage.rb +0 -37
  257. data/manual/text/text.rb +0 -75
  258. data/manual/text/text_box_excess.rb +0 -32
  259. data/manual/text/text_box_extensions.rb +0 -45
  260. data/manual/text/text_box_overflow.rb +0 -44
  261. data/manual/text/utf8.rb +0 -28
  262. data/manual/text/win_ansi_charset.rb +0 -59
  263. data/spec/acceptance/png.rb +0 -23
  264. data/spec/column_box_spec.rb +0 -65
  265. data/spec/extensions/encoding_helpers.rb +0 -9
  266. data/spec/font_metric_cache_spec.rb +0 -52
  267. data/spec/image_handler_spec.rb +0 -54
  268. data/spec/soft_mask_spec.rb +0 -117
  269. data/spec/table/span_dummy_spec.rb +0 -17
data/lib/prawn/text.rb CHANGED
@@ -5,18 +5,15 @@
5
5
  # Copyright May 2008, Gregory Brown. All Rights Reserved.
6
6
  #
7
7
  # This is free software. Please see the LICENSE and COPYING files for details.
8
-
8
+ require "prawn/core/text"
9
+ require "prawn/text/formatted"
10
+ require "prawn/text/box"
9
11
  require "zlib"
10
12
 
11
- require "pdf/core/text"
12
-
13
- require_relative "text/formatted"
14
- require_relative "text/box"
15
-
16
13
  module Prawn
17
14
  module Text
18
15
 
19
- include PDF::Core::Text
16
+ include Prawn::Core::Text
20
17
  include Prawn::Text::Formatted
21
18
 
22
19
  # No-Break Space
@@ -26,13 +23,11 @@ module Prawn
26
23
  # Soft Hyphen (invisible, except when causing a line break)
27
24
  Prawn::Text::SHY = "­"
28
25
 
29
- # @group Stable API
30
-
31
26
  # If you want text to flow onto a new page or between columns, this is the
32
27
  # method to use. If, instead, if you want to place bounded text outside of
33
28
  # the flow of a document (for captions, labels, charts, etc.), use Text::Box
34
29
  # or its convenience method text_box.
35
- #
30
+ #
36
31
  # Draws text on the page. Prawn attempts to wrap the text to fit within your
37
32
  # current bounding box (or margin_box if no bounding box is being used).
38
33
  # Text will flow onto the next page when it reaches the bottom of the
@@ -54,29 +49,28 @@ module Prawn
54
49
  # entire document, set default_kerning = false for that document
55
50
  #
56
51
  # === Text Positioning Details
57
- #
52
+ #
58
53
  # The text is positioned at font.ascender below the baseline,
59
54
  # making it easy to use this method within bounding boxes and spans.
60
55
  #
61
56
  # == Encoding
62
57
  #
63
58
  # Note that strings passed to this function should be encoded as UTF-8.
64
- # If you get unexpected characters appearing in your rendered document,
59
+ # If you get unexpected characters appearing in your rendered document,
65
60
  # check this.
66
61
  #
67
62
  # If the current font is a built-in one, although the string must be
68
63
  # encoded as UTF-8, only characters that are available in WinAnsi
69
64
  # are allowed.
70
65
  #
71
- # If an empty box is rendered to your PDF instead of the character you
66
+ # If an empty box is rendered to your PDF instead of the character you
72
67
  # wanted it usually means the current font doesn't include that character.
73
68
  #
74
69
  # == Options (default values marked in [])
75
70
  #
76
71
  # <tt>:inline_format</tt>::
77
72
  # <tt>boolean</tt>. If true, then the string parameter is interpreted
78
- # as a HTML-esque string that recognizes the following tags
79
- # (assuming the default text formatter is used):
73
+ # as a HTML-esque string that recognizes the following tags:
80
74
  # <tt>\<b></b></tt>:: bold
81
75
  # <tt>\<i></i></tt>:: italic
82
76
  # <tt>\<u></u></tt>:: underline
@@ -103,8 +97,8 @@ module Prawn
103
97
  # <tt>anchor="ToC"</tt>::
104
98
  # where the value of the anchor attribute is the name of a
105
99
  # destination that has already been or will be registered
106
- # using PDF::Core::Destinations#add_dest. A clickable link
107
- # will be created to that destination.
100
+ # using Prawn::Core::Destinations#add_dest. A clickable link
101
+ # will be created to that destination.
108
102
  # Note that you must explicitly underline and color using the
109
103
  # appropriate tags if you which to draw attention to the link
110
104
  #
@@ -149,7 +143,7 @@ module Prawn
149
143
  # text should render with the fill color, stroke color or
150
144
  # both. See the comments to text_rendering_mode() to see
151
145
  # a list of valid options. [0]
152
- #
146
+ #
153
147
  # == Exceptions
154
148
  #
155
149
  # Raises <tt>ArgumentError</tt> if <tt>:at</tt> option included
@@ -158,14 +152,12 @@ module Prawn
158
152
  # any text
159
153
  #
160
154
  def text(string, options={})
161
- return false if string.nil?
162
155
  # we modify the options. don't change the user's hash
163
156
  options = options.dup
164
157
 
165
- if p = options[:inline_format]
166
- p = [] unless p.is_a?(Array)
158
+ if options[:inline_format]
167
159
  options.delete(:inline_format)
168
- array = self.text_formatter.format(string, *p)
160
+ array = Text::Formatted::Parser.to_array(string)
169
161
  else
170
162
  array = [{ :text => string }]
171
163
  end
@@ -173,6 +165,7 @@ module Prawn
173
165
  formatted_text(array, options)
174
166
  end
175
167
 
168
+
176
169
  # Draws formatted text to the page.
177
170
  # Formatted text is comprised of an array of hashes, where each hash defines
178
171
  # text and format information. See Text::Formatted#formatted_text_box for
@@ -183,7 +176,7 @@ module Prawn
183
176
  # text([{ :text => "hello" },
184
177
  # { :text => "world",
185
178
  # :size => 24,
186
- # :styles => [:bold, :italic] }])
179
+ # :style => [:bold, :italic] }])
187
180
  #
188
181
  # == Options
189
182
  #
@@ -197,13 +190,13 @@ module Prawn
197
190
  options = inspect_options_for_text(options.dup)
198
191
 
199
192
  if color = options.delete(:color)
200
- array = array.map do |fragment|
193
+ array = array.map do |fragment|
201
194
  fragment[:color] ? fragment : fragment.merge(:color => color)
202
195
  end
203
196
  end
204
197
 
205
198
  if @indent_paragraphs
206
- self.text_formatter.array_paragraphs(array).each do |paragraph|
199
+ Text::Formatted::Parser.array_paragraphs(array).each do |paragraph|
207
200
  options[:skip_encoding] = false
208
201
  remaining_text = draw_indented_formatted_line(paragraph, options)
209
202
  options[:skip_encoding] = true
@@ -230,7 +223,7 @@ module Prawn
230
223
 
231
224
  # Draws text on the page, beginning at the point specified by the :at option
232
225
  # the string is assumed to be pre-formatted to properly fit the page.
233
- #
226
+ #
234
227
  # pdf.draw_text "Hello World", :at => [100,100]
235
228
  # pdf.draw_text "Goodbye World", :at => [50,50], :size => 16
236
229
  #
@@ -252,14 +245,14 @@ module Prawn
252
245
  # == Encoding
253
246
  #
254
247
  # Note that strings passed to this function should be encoded as UTF-8.
255
- # If you get unexpected characters appearing in your rendered document,
248
+ # If you get unexpected characters appearing in your rendered document,
256
249
  # check this.
257
250
  #
258
251
  # If the current font is a built-in one, although the string must be
259
252
  # encoded as UTF-8, only characters that are available in WinAnsi
260
253
  # are allowed.
261
254
  #
262
- # If an empty box is rendered to your PDF instead of the character you
255
+ # If an empty box is rendered to your PDF instead of the character you
263
256
  # wanted it usually means the current font doesn't include that character.
264
257
  #
265
258
  # == Options (default values marked in [])
@@ -288,7 +281,7 @@ module Prawn
288
281
  text = text.to_s.dup
289
282
  save_font do
290
283
  process_text_options(options)
291
- font.normalize_encoding!(text)
284
+ font.normalize_encoding!(text) unless @skip_encoding
292
285
  font_size(options[:size]) { draw_text!(text, options) }
293
286
  end
294
287
  end
@@ -321,7 +314,7 @@ module Prawn
321
314
  # height_of_formatted([{ :text => "hello" },
322
315
  # { :text => "world",
323
316
  # :size => 24,
324
- # :styles => [:bold, :italic] }])
317
+ # :style => [:bold, :italic] }])
325
318
  #
326
319
  def height_of_formatted(array, options={})
327
320
  if options[:indent_paragraphs]
@@ -332,7 +325,7 @@ module Prawn
332
325
  box = Text::Formatted::Box.new(array,
333
326
  options.merge(:height => 100000000,
334
327
  :document => self))
335
- box.render(:dry_run => true)
328
+ printed = box.render(:dry_run => true)
336
329
 
337
330
  height = box.height
338
331
  height += box.line_gap + box.leading if @final_gap
@@ -388,7 +381,7 @@ module Prawn
388
381
  if options[:kerning].nil? then
389
382
  options[:kerning] = default_kerning?
390
383
  end
391
- valid_options = PDF::Core::Text::VALID_OPTIONS + [:at, :rotate]
384
+ valid_options = Prawn::Core::Text::VALID_OPTIONS + [:at, :rotate]
392
385
  Prawn.verify_options(valid_options, options)
393
386
  options
394
387
  end
@@ -7,11 +7,8 @@
7
7
  # This is free software. Please see the LICENSE and COPYING files for details.
8
8
  #
9
9
 
10
- require_relative "formatted/box"
11
-
12
10
  module Prawn
13
11
  module Text
14
- # @group Stable API
15
12
 
16
13
  # Draws the requested text into a box. When the text overflows
17
14
  # the rectangle, you shrink to fit, or truncate the text. Text
@@ -20,14 +17,14 @@ module Prawn
20
17
  # == Encoding
21
18
  #
22
19
  # Note that strings passed to this function should be encoded as UTF-8.
23
- # If you get unexpected characters appearing in your rendered document,
20
+ # If you get unexpected characters appearing in your rendered document,
24
21
  # check this.
25
22
  #
26
23
  # If the current font is a built-in one, although the string must be
27
24
  # encoded as UTF-8, only characters that are available in WinAnsi
28
25
  # are allowed.
29
26
  #
30
- # If an empty box is rendered to your PDF instead of the character you
27
+ # If an empty box is rendered to your PDF instead of the character you
31
28
  # wanted it usually means the current font doesn't include that character.
32
29
  #
33
30
  # == Options (default values marked in [])
@@ -70,7 +67,7 @@ module Prawn
70
67
  # <tt>:valign</tt>::
71
68
  # <tt>:top</tt>, <tt>:center</tt>, or <tt>:bottom</tt>. Vertical
72
69
  # alignment within the bounding box [:top]
73
- #
70
+ #
74
71
  # <tt>:rotate</tt>::
75
72
  # <tt>number</tt>. The angle to rotate the text
76
73
  # <tt>:rotate_around</tt>::
@@ -111,9 +108,8 @@ module Prawn
111
108
  options = options.dup
112
109
  options[:document] = self
113
110
 
114
- box = if p = options.delete(:inline_format)
115
- p = [] unless p.is_a?(Array)
116
- array = self.text_formatter.format(string, *p)
111
+ box = if options.delete(:inline_format)
112
+ array = Text::Formatted::Parser.to_array(string)
117
113
  Text::Formatted::Box.new(array, options)
118
114
  else
119
115
  Text::Box.new(string, options)
@@ -122,10 +118,8 @@ module Prawn
122
118
  box.render
123
119
  end
124
120
 
125
- # @group Experimental API
126
-
127
121
  # Generally, one would use the Prawn::Text#text_box convenience
128
- # method. However, using Text::Box.new in conjunction with
122
+ # method. However, using Text::Box.new in conjunction with
129
123
  # #render(:dry_run=> true) enables one to do look-ahead calculations prior
130
124
  # to placing text on the page, or to determine how much vertical space was
131
125
  # consumed by the printed text
@@ -1,5 +1,4 @@
1
- require_relative "formatted/wrap"
2
-
3
- require_relative "formatted/box"
4
- require_relative "formatted/parser"
5
- require_relative "formatted/fragment"
1
+ require "prawn/core/text/formatted/wrap"
2
+ require "prawn/text/formatted/box"
3
+ require "prawn/text/formatted/parser"
4
+ require "prawn/text/formatted/fragment"
@@ -10,8 +10,7 @@
10
10
  module Prawn
11
11
  module Text
12
12
  module Formatted
13
- # @group Stable API
14
-
13
+
15
14
  # Draws the requested formatted text into a box. When the text overflows
16
15
  # the rectangle shrink to fit or truncate the text. Text boxes are
17
16
  # independent of the document y position.
@@ -46,19 +45,10 @@ module Prawn
46
45
  # the appropriate tags if you which to draw attention to the link
47
46
  # <tt>:anchor</tt>::
48
47
  # a destination that has already been or will be registered using
49
- # PDF::Core::Destinations#add_dest. A clickable link will be
48
+ # Prawn::Core::Destinations#add_dest. A clickable link will be
50
49
  # created to that destination. Note that you must explicitly underline
51
50
  # and color using the appropriate tags if you which to draw attention
52
51
  # to the link
53
- # <tt>:local</tt>::
54
- # a file or application to be opened locally. A clickable link will be
55
- # created to the provided local file or application. If the file is
56
- # another PDF, it will be opened in a new window. Note that you must
57
- # explicitly underline and color using the appropriate tags if you which
58
- # to draw attention to the link
59
- # <tt>:draw_text_callback</tt>:
60
- # if provided, this Proc will be called instead of #draw_text! once
61
- # per fragment for every low-level addition of text to the page.
62
52
  # <tt>:callback</tt>::
63
53
  # an object (or array of such objects) with two methods:
64
54
  # #render_behind and #render_in_front, which are called immediately
@@ -99,9 +89,20 @@ module Prawn
99
89
  # vertical space was consumed by the printed text
100
90
  #
101
91
  class Box
102
- include Prawn::Text::Formatted::Wrap
92
+ include Prawn::Core::Text::Formatted::Wrap
103
93
 
104
- # @group Experimental API
94
+ def valid_options
95
+ Prawn::Core::Text::VALID_OPTIONS + [:at, :height, :width,
96
+ :align, :valign,
97
+ :rotate, :rotate_around,
98
+ :overflow, :min_font_size,
99
+ :leading, :character_spacing,
100
+ :mode, :single_line,
101
+ :skip_encoding,
102
+ :document,
103
+ :direction,
104
+ :fallback_fonts]
105
+ end
105
106
 
106
107
  # The text that was successfully printed (or, if <tt>dry_run</tt> was
107
108
  # used, the text that would have been successfully printed)
@@ -134,6 +135,42 @@ module Prawn
134
135
  line_height - (ascender + descender)
135
136
  end
136
137
 
138
+ #
139
+ # Example (see Prawn::Text::Core::Formatted::Wrap for what is required
140
+ # of the wrap method if you want to override the default wrapping
141
+ # algorithm):
142
+ #
143
+ #
144
+ # module MyWrap
145
+ #
146
+ # def wrap(array)
147
+ # initialize_wrap([{ :text => 'all your base are belong to us' }])
148
+ # @line_wrap.wrap_line(:document => @document,
149
+ # :kerning => @kerning,
150
+ # :width => 10000,
151
+ # :arranger => @arranger)
152
+ # fragment = @arranger.retrieve_fragment
153
+ # format_and_draw_fragment(fragment, 0, @line_wrap.width, 0)
154
+ # []
155
+ # end
156
+ #
157
+ # end
158
+ #
159
+ # Prawn::Text::Formatted::Box.extensions << MyWrap
160
+ #
161
+ # box = Prawn::Text::Formatted::Box.new('hello world')
162
+ # box.render('why can't I print anything other than' +
163
+ # '"all your base are belong to us"?')
164
+ #
165
+ #
166
+ def self.extensions
167
+ @extensions ||= []
168
+ end
169
+
170
+ def self.inherited(base) #:nodoc:
171
+ extensions.each { |e| base.extensions << e }
172
+ end
173
+
137
174
  # See Prawn::Text#text_box for valid options
138
175
  #
139
176
  def initialize(formatted_text, options={})
@@ -168,12 +205,6 @@ module Prawn
168
205
  @rotate_around = options[:rotate_around] || :upper_left
169
206
  @single_line = options[:single_line]
170
207
  @skip_encoding = options[:skip_encoding] || @document.skip_encoding
171
- @draw_text_callback = options[:draw_text_callback]
172
-
173
- # if the text rendering mode is :unknown, force it back to :fill
174
- if @mode == :unknown
175
- @mode = :fill
176
- end
177
208
 
178
209
  if @overflow == :expand
179
210
  # if set to expand, then we simply set the bottom
@@ -242,7 +273,7 @@ module Prawn
242
273
  end
243
274
 
244
275
  # The height actually used during the previous <tt>render</tt>
245
- #
276
+ #
246
277
  def height
247
278
  return 0 if @baseline_y.nil? || @descender.nil?
248
279
  (@baseline_y - @descender).abs
@@ -279,71 +310,14 @@ module Prawn
279
310
  draw_fragment_underlays(fragment)
280
311
 
281
312
  @document.word_spacing(word_spacing) {
282
- if @draw_text_callback
283
- @draw_text_callback.call(fragment.text, :at => [x, y],
284
- :kerning => @kerning)
285
- else
286
- @document.draw_text!(fragment.text, :at => [x, y],
287
- :kerning => @kerning)
288
- end
313
+ @document.draw_text!(fragment.text, :at => [x, y],
314
+ :kerning => @kerning)
289
315
  }
290
316
 
291
317
  draw_fragment_overlays(fragment)
292
318
  end
293
319
  end
294
320
 
295
- # @group Extension API
296
-
297
- # Example (see Prawn::Text::Core::Formatted::Wrap for what is required
298
- # of the wrap method if you want to override the default wrapping
299
- # algorithm):
300
- #
301
- #
302
- # module MyWrap
303
- #
304
- # def wrap(array)
305
- # initialize_wrap([{ :text => 'all your base are belong to us' }])
306
- # @line_wrap.wrap_line(:document => @document,
307
- # :kerning => @kerning,
308
- # :width => 10000,
309
- # :arranger => @arranger)
310
- # fragment = @arranger.retrieve_fragment
311
- # format_and_draw_fragment(fragment, 0, @line_wrap.width, 0)
312
- # []
313
- # end
314
- #
315
- # end
316
- #
317
- # Prawn::Text::Formatted::Box.extensions << MyWrap
318
- #
319
- # box = Prawn::Text::Formatted::Box.new('hello world')
320
- # box.render('why can't I print anything other than' +
321
- # '"all your base are belong to us"?')
322
- #
323
- #
324
- def self.extensions
325
- @extensions ||= []
326
- end
327
-
328
- # @private
329
- def self.inherited(base)
330
- extensions.each { |e| base.extensions << e }
331
- end
332
-
333
- def valid_options
334
- PDF::Core::Text::VALID_OPTIONS + [:at, :height, :width,
335
- :align, :valign,
336
- :rotate, :rotate_around,
337
- :overflow, :min_font_size,
338
- :leading, :character_spacing,
339
- :mode, :single_line,
340
- :skip_encoding,
341
- :document,
342
- :direction,
343
- :fallback_fonts,
344
- :draw_text_callback]
345
- end
346
-
347
321
  private
348
322
 
349
323
  def original_text
@@ -397,7 +371,8 @@ module Prawn
397
371
  # all fonts
398
372
  fallback_fonts << fragment_font
399
373
 
400
- hash[:text].each_char do |char|
374
+ hash[:text].unpack("U*").each do |char_int|
375
+ char = [char_int].pack("U")
401
376
  @document.font(fragment_font)
402
377
  font_glyph_pairs << [find_font_for_this_glyph(char,
403
378
  @document.font.family,
@@ -466,7 +441,7 @@ module Prawn
466
441
  # we need to wait until render() is called so that the fonts are set
467
442
  # up properly for wrapping. So guard with a boolean to ensure this is
468
443
  # only run once.
469
- return if defined?(@vertical_alignment_processed) && @vertical_alignment_processed
444
+ return if @vertical_alignment_processed
470
445
  @vertical_alignment_processed = true
471
446
 
472
447
  return if @vertical_align == :top
@@ -537,7 +512,6 @@ module Prawn
537
512
  draw_fragment_overlay_styles(fragment)
538
513
  draw_fragment_overlay_link(fragment)
539
514
  draw_fragment_overlay_anchor(fragment)
540
- draw_fragment_overlay_local(fragment)
541
515
  fragment.callback_objects.each do |obj|
542
516
  obj.render_in_front(fragment) if obj.respond_to?(:render_in_front)
543
517
  end
@@ -550,7 +524,7 @@ module Prawn
550
524
  :Border => [0, 0, 0],
551
525
  :A => { :Type => :Action,
552
526
  :S => :URI,
553
- :URI => PDF::Core::LiteralString.new(fragment.link) })
527
+ :URI => Prawn::Core::LiteralString.new(fragment.link) })
554
528
  end
555
529
 
556
530
  def draw_fragment_overlay_anchor(fragment)
@@ -561,23 +535,12 @@ module Prawn
561
535
  :Dest => fragment.anchor)
562
536
  end
563
537
 
564
- def draw_fragment_overlay_local(fragment)
565
- return unless fragment.local
566
- box = fragment.absolute_bounding_box
567
- @document.link_annotation(box,
568
- :Border => [0, 0, 0],
569
- :A => { :Type => :Action,
570
- :S => :Launch,
571
- :F => PDF::Core::LiteralString.new(fragment.local),
572
- :NewWindow => true })
573
- end
574
-
575
538
  def draw_fragment_overlay_styles(fragment)
576
539
  underline = fragment.styles.include?(:underline)
577
540
  if underline
578
541
  @document.stroke_line(fragment.underline_points)
579
542
  end
580
-
543
+
581
544
  strikethrough = fragment.styles.include?(:strikethrough)
582
545
  if strikethrough
583
546
  @document.stroke_line(fragment.strikethrough_points)