prawn 0.12.0 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (282) hide show
  1. checksums.yaml +7 -0
  2. data/COPYING +2 -2
  3. data/Gemfile +18 -0
  4. data/LICENSE +1 -1
  5. data/README.md +17 -4
  6. data/Rakefile +18 -22
  7. data/data/images/indexed_color.dat +0 -0
  8. data/data/images/indexed_color.png +0 -0
  9. data/data/pdfs/nested_pages.pdf +13 -13
  10. data/lib/pdf/core.rb +35 -0
  11. data/lib/{prawn → pdf}/core/annotations.rb +6 -7
  12. data/lib/{prawn → pdf}/core/byte_string.rb +1 -1
  13. data/lib/{prawn → pdf}/core/destinations.rb +23 -23
  14. data/lib/{prawn → pdf}/core/document_state.rb +8 -8
  15. data/lib/pdf/core/filter_list.rb +51 -0
  16. data/lib/pdf/core/filters.rb +36 -0
  17. data/lib/pdf/core/graphics_state.rb +68 -0
  18. data/lib/{prawn → pdf}/core/literal_string.rb +1 -1
  19. data/lib/{prawn → pdf}/core/name_tree.rb +14 -2
  20. data/lib/{prawn → pdf}/core/object_store.rb +80 -24
  21. data/lib/pdf/core/outline.rb +315 -0
  22. data/lib/{prawn → pdf}/core/page.rb +23 -26
  23. data/lib/{prawn/document → pdf/core}/page_geometry.rb +11 -21
  24. data/lib/{prawn → pdf}/core/pdf_object.rb +48 -32
  25. data/lib/{prawn → pdf}/core/reference.rb +35 -44
  26. data/lib/pdf/core/stream.rb +98 -0
  27. data/lib/{prawn → pdf}/core/text.rb +24 -17
  28. data/lib/prawn.rb +95 -17
  29. data/lib/prawn/compatibility.rb +66 -26
  30. data/lib/prawn/document.rb +48 -30
  31. data/lib/prawn/document/bounding_box.rb +3 -3
  32. data/lib/prawn/document/column_box.rb +46 -8
  33. data/lib/prawn/document/graphics_state.rb +10 -73
  34. data/lib/prawn/document/internals.rb +24 -23
  35. data/lib/prawn/document/snapshot.rb +6 -7
  36. data/lib/prawn/document/span.rb +10 -10
  37. data/lib/prawn/encoding.rb +7 -7
  38. data/lib/prawn/errors.rb +18 -29
  39. data/lib/prawn/font.rb +64 -28
  40. data/lib/prawn/font/afm.rb +32 -74
  41. data/lib/prawn/font/dfont.rb +2 -2
  42. data/lib/prawn/font/ttf.rb +28 -57
  43. data/lib/prawn/font_metric_cache.rb +45 -0
  44. data/lib/prawn/graphics.rb +307 -41
  45. data/lib/prawn/graphics/cap_style.rb +3 -3
  46. data/lib/prawn/graphics/color.rb +12 -5
  47. data/lib/prawn/graphics/dash.rb +52 -31
  48. data/lib/prawn/graphics/join_style.rb +7 -7
  49. data/lib/prawn/graphics/patterns.rb +137 -0
  50. data/lib/prawn/graphics/transformation.rb +9 -9
  51. data/lib/prawn/graphics/transparency.rb +1 -1
  52. data/lib/prawn/image_handler.rb +30 -0
  53. data/lib/prawn/images.rb +86 -105
  54. data/lib/prawn/images/image.rb +48 -0
  55. data/lib/prawn/images/jpg.rb +14 -10
  56. data/lib/prawn/images/png.rb +50 -37
  57. data/lib/prawn/layout.rb +2 -2
  58. data/lib/prawn/layout/grid.rb +51 -51
  59. data/lib/prawn/measurement_extensions.rb +5 -5
  60. data/lib/prawn/measurements.rb +25 -21
  61. data/lib/prawn/outline.rb +4 -308
  62. data/lib/prawn/repeater.rb +8 -8
  63. data/lib/prawn/security.rb +50 -36
  64. data/lib/prawn/soft_mask.rb +94 -0
  65. data/lib/prawn/stamp.rb +3 -3
  66. data/lib/prawn/table.rb +292 -118
  67. data/lib/prawn/table/cell.rb +272 -45
  68. data/lib/prawn/table/cell/image.rb +70 -0
  69. data/lib/prawn/table/cell/in_table.rb +2 -2
  70. data/lib/prawn/table/cell/span_dummy.rb +92 -0
  71. data/lib/prawn/table/cell/subtable.rb +2 -2
  72. data/lib/prawn/table/cell/text.rb +42 -24
  73. data/lib/prawn/table/cells.rb +137 -48
  74. data/lib/prawn/text.rb +35 -23
  75. data/lib/prawn/text/box.rb +18 -5
  76. data/lib/prawn/text/formatted.rb +5 -4
  77. data/lib/prawn/text/formatted/arranger.rb +292 -0
  78. data/lib/prawn/text/formatted/box.rb +52 -13
  79. data/lib/prawn/text/formatted/fragment.rb +37 -22
  80. data/lib/prawn/text/formatted/line_wrap.rb +286 -0
  81. data/lib/prawn/text/formatted/parser.rb +14 -6
  82. data/lib/prawn/text/formatted/wrap.rb +151 -0
  83. data/lib/prawn/utilities.rb +44 -0
  84. data/manual/basic_concepts/adding_pages.rb +27 -0
  85. data/manual/basic_concepts/basic_concepts.rb +34 -0
  86. data/manual/basic_concepts/creation.rb +39 -0
  87. data/manual/basic_concepts/cursor.rb +33 -0
  88. data/manual/basic_concepts/measurement.rb +25 -0
  89. data/manual/basic_concepts/origin.rb +38 -0
  90. data/manual/basic_concepts/other_cursor_helpers.rb +40 -0
  91. data/manual/bounding_box/bounding_box.rb +39 -0
  92. data/manual/bounding_box/bounds.rb +49 -0
  93. data/manual/bounding_box/canvas.rb +24 -0
  94. data/manual/bounding_box/creation.rb +23 -0
  95. data/manual/bounding_box/indentation.rb +46 -0
  96. data/manual/bounding_box/nesting.rb +45 -0
  97. data/manual/bounding_box/russian_boxes.rb +40 -0
  98. data/manual/bounding_box/stretchy.rb +31 -0
  99. data/manual/document_and_page_options/background.rb +27 -0
  100. data/manual/document_and_page_options/document_and_page_options.rb +31 -0
  101. data/manual/document_and_page_options/metadata.rb +23 -0
  102. data/manual/document_and_page_options/page_margins.rb +38 -0
  103. data/manual/document_and_page_options/page_size.rb +34 -0
  104. data/manual/example_file.rb +116 -0
  105. data/manual/example_helper.rb +411 -0
  106. data/manual/example_package.rb +53 -0
  107. data/manual/example_section.rb +46 -0
  108. data/manual/graphics/circle_and_ellipse.rb +22 -0
  109. data/manual/graphics/color.rb +24 -0
  110. data/manual/graphics/common_lines.rb +28 -0
  111. data/manual/graphics/fill_and_stroke.rb +42 -0
  112. data/manual/graphics/fill_rules.rb +37 -0
  113. data/manual/graphics/gradients.rb +37 -0
  114. data/manual/graphics/graphics.rb +58 -0
  115. data/manual/graphics/helper.rb +24 -0
  116. data/manual/graphics/line_width.rb +35 -0
  117. data/manual/graphics/lines_and_curves.rb +41 -0
  118. data/manual/graphics/polygon.rb +29 -0
  119. data/manual/graphics/rectangle.rb +21 -0
  120. data/manual/graphics/rotate.rb +28 -0
  121. data/manual/graphics/scale.rb +41 -0
  122. data/manual/graphics/soft_masks.rb +46 -0
  123. data/manual/graphics/stroke_cap.rb +31 -0
  124. data/manual/graphics/stroke_dash.rb +48 -0
  125. data/manual/graphics/stroke_join.rb +30 -0
  126. data/manual/graphics/translate.rb +29 -0
  127. data/manual/graphics/transparency.rb +35 -0
  128. data/manual/images/absolute_position.rb +23 -0
  129. data/manual/images/fit.rb +21 -0
  130. data/manual/images/horizontal.rb +25 -0
  131. data/manual/images/images.rb +40 -0
  132. data/manual/images/plain_image.rb +18 -0
  133. data/manual/images/scale.rb +22 -0
  134. data/manual/images/vertical.rb +28 -0
  135. data/manual/images/width_and_height.rb +25 -0
  136. data/manual/layout/boxes.rb +27 -0
  137. data/manual/layout/content.rb +25 -0
  138. data/manual/layout/layout.rb +28 -0
  139. data/manual/layout/simple_grid.rb +23 -0
  140. data/manual/manual/cover.rb +35 -0
  141. data/manual/manual/foreword.rb +85 -0
  142. data/manual/manual/how_to_read_this_manual.rb +41 -0
  143. data/manual/manual/manual.rb +35 -0
  144. data/manual/outline/add_subsection_to.rb +61 -0
  145. data/manual/outline/insert_section_after.rb +47 -0
  146. data/manual/outline/outline.rb +32 -0
  147. data/manual/outline/sections_and_pages.rb +67 -0
  148. data/manual/repeatable_content/page_numbering.rb +54 -0
  149. data/manual/repeatable_content/repeatable_content.rb +31 -0
  150. data/manual/repeatable_content/repeater.rb +55 -0
  151. data/manual/repeatable_content/stamp.rb +41 -0
  152. data/manual/security/encryption.rb +31 -0
  153. data/manual/security/permissions.rb +38 -0
  154. data/manual/security/security.rb +28 -0
  155. data/manual/syntax_highlight.rb +52 -0
  156. data/manual/table/basic_block.rb +53 -0
  157. data/manual/table/before_rendering_page.rb +26 -0
  158. data/manual/table/cell_border_lines.rb +24 -0
  159. data/manual/table/cell_borders_and_bg.rb +31 -0
  160. data/manual/table/cell_dimensions.rb +30 -0
  161. data/manual/table/cell_text.rb +38 -0
  162. data/manual/table/column_widths.rb +30 -0
  163. data/manual/table/content_and_subtables.rb +39 -0
  164. data/manual/table/creation.rb +27 -0
  165. data/manual/table/filtering.rb +36 -0
  166. data/manual/table/flow_and_header.rb +17 -0
  167. data/manual/table/image_cells.rb +33 -0
  168. data/manual/table/position.rb +29 -0
  169. data/manual/table/row_colors.rb +20 -0
  170. data/manual/table/span.rb +30 -0
  171. data/manual/table/style.rb +22 -0
  172. data/manual/table/table.rb +52 -0
  173. data/manual/table/width.rb +27 -0
  174. data/manual/templates/full_template.rb +25 -0
  175. data/manual/templates/page_template.rb +48 -0
  176. data/manual/templates/templates.rb +27 -0
  177. data/manual/text/alignment.rb +44 -0
  178. data/manual/text/color.rb +24 -0
  179. data/manual/text/column_box.rb +32 -0
  180. data/manual/text/fallback_fonts.rb +37 -0
  181. data/manual/text/font.rb +41 -0
  182. data/manual/text/font_size.rb +45 -0
  183. data/manual/text/font_style.rb +23 -0
  184. data/manual/text/formatted_callbacks.rb +60 -0
  185. data/manual/text/formatted_text.rb +54 -0
  186. data/manual/text/free_flowing_text.rb +51 -0
  187. data/manual/text/group.rb +29 -0
  188. data/manual/text/inline.rb +43 -0
  189. data/manual/text/kerning_and_character_spacing.rb +39 -0
  190. data/manual/text/leading.rb +25 -0
  191. data/manual/text/line_wrapping.rb +41 -0
  192. data/manual/text/paragraph_indentation.rb +26 -0
  193. data/manual/text/positioned_text.rb +38 -0
  194. data/manual/text/registering_families.rb +48 -0
  195. data/manual/text/rendering_and_color.rb +37 -0
  196. data/manual/text/right_to_left_text.rb +43 -0
  197. data/manual/text/rotation.rb +43 -0
  198. data/manual/text/single_usage.rb +37 -0
  199. data/manual/text/text.rb +75 -0
  200. data/manual/text/text_box_excess.rb +32 -0
  201. data/manual/text/text_box_extensions.rb +45 -0
  202. data/manual/text/text_box_overflow.rb +44 -0
  203. data/manual/text/utf8.rb +28 -0
  204. data/{examples/m17n → manual/text}/win_ansi_charset.rb +14 -10
  205. data/prawn.gemspec +18 -12
  206. data/spec/acceptance/png.rb +23 -0
  207. data/spec/annotations_spec.rb +16 -32
  208. data/spec/bounding_box_spec.rb +128 -15
  209. data/spec/cell_spec.rb +169 -38
  210. data/spec/column_box_spec.rb +33 -0
  211. data/spec/destinations_spec.rb +5 -5
  212. data/spec/document_spec.rb +150 -104
  213. data/spec/extensions/encoding_helpers.rb +10 -0
  214. data/spec/extensions/mocha.rb +1 -0
  215. data/spec/filters_spec.rb +34 -0
  216. data/spec/font_metric_cache_spec.rb +52 -0
  217. data/spec/font_spec.rb +183 -97
  218. data/spec/formatted_text_arranger_spec.rb +43 -43
  219. data/spec/formatted_text_box_spec.rb +30 -20
  220. data/spec/formatted_text_fragment_spec.rb +8 -8
  221. data/spec/graphics_spec.rb +158 -69
  222. data/spec/grid_spec.rb +15 -15
  223. data/spec/image_handler_spec.rb +42 -0
  224. data/spec/images_spec.rb +49 -24
  225. data/spec/inline_formatted_text_parser_spec.rb +73 -19
  226. data/spec/jpg_spec.rb +4 -4
  227. data/spec/line_wrap_spec.rb +26 -26
  228. data/spec/measurement_units_spec.rb +6 -6
  229. data/spec/name_tree_spec.rb +21 -21
  230. data/spec/object_store_spec.rb +39 -39
  231. data/spec/outline_spec.rb +93 -53
  232. data/spec/pdf_object_spec.rb +88 -86
  233. data/spec/png_spec.rb +31 -28
  234. data/spec/reference_spec.rb +32 -32
  235. data/spec/repeater_spec.rb +25 -11
  236. data/spec/security_spec.rb +44 -12
  237. data/spec/snapshot_spec.rb +8 -9
  238. data/spec/soft_mask_spec.rb +117 -0
  239. data/spec/span_spec.rb +10 -15
  240. data/spec/spec_helper.rb +25 -8
  241. data/spec/stamp_spec.rb +29 -30
  242. data/spec/stream_spec.rb +58 -0
  243. data/spec/stroke_styles_spec.rb +36 -18
  244. data/spec/table/span_dummy_spec.rb +17 -0
  245. data/spec/table_spec.rb +697 -105
  246. data/spec/template_spec.rb +108 -54
  247. data/spec/text_at_spec.rb +18 -17
  248. data/spec/text_box_spec.rb +111 -62
  249. data/spec/text_rendering_mode_spec.rb +5 -5
  250. data/spec/text_spacing_spec.rb +4 -4
  251. data/spec/text_spec.rb +57 -49
  252. data/spec/transparency_spec.rb +5 -5
  253. metadata +421 -213
  254. data/data/fonts/Action Man.dfont +0 -0
  255. data/data/fonts/Activa.ttf +0 -0
  256. data/data/fonts/Chalkboard.ttf +0 -0
  257. data/data/fonts/DejaVuSans.ttf +0 -0
  258. data/data/fonts/Dustismo_Roman.ttf +0 -0
  259. data/data/fonts/comicsans.ttf +0 -0
  260. data/data/fonts/gkai00mp.ttf +0 -0
  261. data/data/images/rails.dat +0 -0
  262. data/data/images/rails.png +0 -0
  263. data/examples/bounding_box/russian_boxes.rb +0 -37
  264. data/examples/example_helper.rb +0 -11
  265. data/examples/general/context_sensitive_headers.rb +0 -38
  266. data/examples/graphics/cmyk.rb +0 -13
  267. data/examples/graphics/gradient.rb +0 -23
  268. data/examples/graphics/png_types.rb +0 -23
  269. data/examples/graphics/remote_images.rb +0 -13
  270. data/examples/m17n/full_win_ansi_character_list.rb +0 -20
  271. data/examples/m17n/sjis.rb +0 -29
  272. data/examples/table/bill.rb +0 -54
  273. data/examples/table/header.rb +0 -15
  274. data/examples/text/font_calculations.rb +0 -92
  275. data/examples/text/hyphenation.rb +0 -45
  276. data/examples/text/indent_paragraphs.rb +0 -24
  277. data/lib/prawn/core.rb +0 -85
  278. data/lib/prawn/core/text/formatted/arranger.rb +0 -294
  279. data/lib/prawn/core/text/formatted/line_wrap.rb +0 -273
  280. data/lib/prawn/core/text/formatted/wrap.rb +0 -153
  281. data/lib/prawn/graphics/gradient.rb +0 -84
  282. data/lib/prawn/security/arcfour.rb +0 -51
@@ -5,15 +5,17 @@
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
- require "prawn/core/text"
9
- require "prawn/text/formatted"
10
- require "prawn/text/box"
8
+
11
9
  require "zlib"
12
10
 
11
+ require_relative "../pdf/core/text"
12
+ require_relative "text/formatted"
13
+ require_relative "text/box"
14
+
13
15
  module Prawn
14
16
  module Text
15
17
 
16
- include Prawn::Core::Text
18
+ include PDF::Core::Text
17
19
  include Prawn::Text::Formatted
18
20
 
19
21
  # No-Break Space
@@ -27,7 +29,7 @@ module Prawn
27
29
  # method to use. If, instead, if you want to place bounded text outside of
28
30
  # the flow of a document (for captions, labels, charts, etc.), use Text::Box
29
31
  # or its convenience method text_box.
30
- #
32
+ #
31
33
  # Draws text on the page. Prawn attempts to wrap the text to fit within your
32
34
  # current bounding box (or margin_box if no bounding box is being used).
33
35
  # Text will flow onto the next page when it reaches the bottom of the
@@ -49,28 +51,29 @@ module Prawn
49
51
  # entire document, set default_kerning = false for that document
50
52
  #
51
53
  # === Text Positioning Details
52
- #
54
+ #
53
55
  # The text is positioned at font.ascender below the baseline,
54
56
  # making it easy to use this method within bounding boxes and spans.
55
57
  #
56
58
  # == Encoding
57
59
  #
58
60
  # Note that strings passed to this function should be encoded as UTF-8.
59
- # If you get unexpected characters appearing in your rendered document,
61
+ # If you get unexpected characters appearing in your rendered document,
60
62
  # check this.
61
63
  #
62
64
  # If the current font is a built-in one, although the string must be
63
65
  # encoded as UTF-8, only characters that are available in WinAnsi
64
66
  # are allowed.
65
67
  #
66
- # If an empty box is rendered to your PDF instead of the character you
68
+ # If an empty box is rendered to your PDF instead of the character you
67
69
  # wanted it usually means the current font doesn't include that character.
68
70
  #
69
71
  # == Options (default values marked in [])
70
72
  #
71
73
  # <tt>:inline_format</tt>::
72
74
  # <tt>boolean</tt>. If true, then the string parameter is interpreted
73
- # as a HTML-esque string that recognizes the following tags:
75
+ # as a HTML-esque string that recognizes the following tags
76
+ # (assuming the default text formatter is used):
74
77
  # <tt>\<b></b></tt>:: bold
75
78
  # <tt>\<i></i></tt>:: italic
76
79
  # <tt>\<u></u></tt>:: underline
@@ -97,8 +100,8 @@ module Prawn
97
100
  # <tt>anchor="ToC"</tt>::
98
101
  # where the value of the anchor attribute is the name of a
99
102
  # destination that has already been or will be registered
100
- # using Prawn::Core::Destinations#add_dest. A clickable link
101
- # will be created to that destination.
103
+ # using PDF::Core::Destinations#add_dest. A clickable link
104
+ # will be created to that destination.
102
105
  # Note that you must explicitly underline and color using the
103
106
  # appropriate tags if you which to draw attention to the link
104
107
  #
@@ -107,6 +110,7 @@ module Prawn
107
110
  # [value of document.default_kerning?]
108
111
  # <tt>:size</tt>:: <tt>number</tt>. The font size to use. [current font
109
112
  # size]
113
+ # <tt>:color</tt>:: an RGB color ("ff0000") or CMYK array [10, 20, 30, 40].
110
114
  # <tt>:character_spacing</tt>:: <tt>number</tt>. The amount of space to add
111
115
  # to or remove from the default character
112
116
  # spacing. [0]
@@ -142,7 +146,7 @@ module Prawn
142
146
  # text should render with the fill color, stroke color or
143
147
  # both. See the comments to text_rendering_mode() to see
144
148
  # a list of valid options. [0]
145
- #
149
+ #
146
150
  # == Exceptions
147
151
  #
148
152
  # Raises <tt>ArgumentError</tt> if <tt>:at</tt> option included
@@ -151,12 +155,14 @@ module Prawn
151
155
  # any text
152
156
  #
153
157
  def text(string, options={})
158
+ return false if string.nil?
154
159
  # we modify the options. don't change the user's hash
155
160
  options = options.dup
156
161
 
157
- if options[:inline_format]
162
+ if p = options[:inline_format]
163
+ p = [] unless p.is_a?(Array)
158
164
  options.delete(:inline_format)
159
- array = Text::Formatted::Parser.to_array(string)
165
+ array = self.text_formatter.format(string, *p)
160
166
  else
161
167
  array = [{ :text => string }]
162
168
  end
@@ -175,7 +181,7 @@ module Prawn
175
181
  # text([{ :text => "hello" },
176
182
  # { :text => "world",
177
183
  # :size => 24,
178
- # :style => [:bold, :italic] }])
184
+ # :styles => [:bold, :italic] }])
179
185
  #
180
186
  # == Options
181
187
  #
@@ -188,8 +194,14 @@ module Prawn
188
194
  def formatted_text(array, options={})
189
195
  options = inspect_options_for_text(options.dup)
190
196
 
197
+ if color = options.delete(:color)
198
+ array = array.map do |fragment|
199
+ fragment[:color] ? fragment : fragment.merge(:color => color)
200
+ end
201
+ end
202
+
191
203
  if @indent_paragraphs
192
- Text::Formatted::Parser.array_paragraphs(array).each do |paragraph|
204
+ self.text_formatter.array_paragraphs(array).each do |paragraph|
193
205
  options[:skip_encoding] = false
194
206
  remaining_text = draw_indented_formatted_line(paragraph, options)
195
207
  options[:skip_encoding] = true
@@ -216,7 +228,7 @@ module Prawn
216
228
 
217
229
  # Draws text on the page, beginning at the point specified by the :at option
218
230
  # the string is assumed to be pre-formatted to properly fit the page.
219
- #
231
+ #
220
232
  # pdf.draw_text "Hello World", :at => [100,100]
221
233
  # pdf.draw_text "Goodbye World", :at => [50,50], :size => 16
222
234
  #
@@ -238,14 +250,14 @@ module Prawn
238
250
  # == Encoding
239
251
  #
240
252
  # Note that strings passed to this function should be encoded as UTF-8.
241
- # If you get unexpected characters appearing in your rendered document,
253
+ # If you get unexpected characters appearing in your rendered document,
242
254
  # check this.
243
255
  #
244
256
  # If the current font is a built-in one, although the string must be
245
257
  # encoded as UTF-8, only characters that are available in WinAnsi
246
258
  # are allowed.
247
259
  #
248
- # If an empty box is rendered to your PDF instead of the character you
260
+ # If an empty box is rendered to your PDF instead of the character you
249
261
  # wanted it usually means the current font doesn't include that character.
250
262
  #
251
263
  # == Options (default values marked in [])
@@ -274,7 +286,7 @@ module Prawn
274
286
  text = text.to_s.dup
275
287
  save_font do
276
288
  process_text_options(options)
277
- font.normalize_encoding!(text) unless @skip_encoding
289
+ font.normalize_encoding!(text)
278
290
  font_size(options[:size]) { draw_text!(text, options) }
279
291
  end
280
292
  end
@@ -307,7 +319,7 @@ module Prawn
307
319
  # height_of_formatted([{ :text => "hello" },
308
320
  # { :text => "world",
309
321
  # :size => 24,
310
- # :style => [:bold, :italic] }])
322
+ # :styles => [:bold, :italic] }])
311
323
  #
312
324
  def height_of_formatted(array, options={})
313
325
  if options[:indent_paragraphs]
@@ -318,7 +330,7 @@ module Prawn
318
330
  box = Text::Formatted::Box.new(array,
319
331
  options.merge(:height => 100000000,
320
332
  :document => self))
321
- printed = box.render(:dry_run => true)
333
+ box.render(:dry_run => true)
322
334
 
323
335
  height = box.height
324
336
  height += box.line_gap + box.leading if @final_gap
@@ -374,7 +386,7 @@ module Prawn
374
386
  if options[:kerning].nil? then
375
387
  options[:kerning] = default_kerning?
376
388
  end
377
- valid_options = Prawn::Core::Text::VALID_OPTIONS + [:at, :rotate]
389
+ valid_options = PDF::Core::Text::VALID_OPTIONS + [:at, :rotate]
378
390
  Prawn.verify_options(valid_options, options)
379
391
  options
380
392
  end
@@ -7,6 +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
+
10
12
  module Prawn
11
13
  module Text
12
14
 
@@ -17,14 +19,14 @@ module Prawn
17
19
  # == Encoding
18
20
  #
19
21
  # Note that strings passed to this function should be encoded as UTF-8.
20
- # If you get unexpected characters appearing in your rendered document,
22
+ # If you get unexpected characters appearing in your rendered document,
21
23
  # check this.
22
24
  #
23
25
  # If the current font is a built-in one, although the string must be
24
26
  # encoded as UTF-8, only characters that are available in WinAnsi
25
27
  # are allowed.
26
28
  #
27
- # If an empty box is rendered to your PDF instead of the character you
29
+ # If an empty box is rendered to your PDF instead of the character you
28
30
  # wanted it usually means the current font doesn't include that character.
29
31
  #
30
32
  # == Options (default values marked in [])
@@ -67,7 +69,7 @@ module Prawn
67
69
  # <tt>:valign</tt>::
68
70
  # <tt>:top</tt>, <tt>:center</tt>, or <tt>:bottom</tt>. Vertical
69
71
  # alignment within the bounding box [:top]
70
- #
72
+ #
71
73
  # <tt>:rotate</tt>::
72
74
  # <tt>number</tt>. The angle to rotate the text
73
75
  # <tt>:rotate_around</tt>::
@@ -105,11 +107,22 @@ module Prawn
105
107
  # any text
106
108
  #
107
109
  def text_box(string, options={})
108
- Text::Box.new(string, options.merge(:document => self)).render
110
+ options = options.dup
111
+ options[:document] = self
112
+
113
+ box = if p = options.delete(:inline_format)
114
+ p = [] unless p.is_a?(Array)
115
+ array = self.text_formatter.format(string, *p)
116
+ Text::Formatted::Box.new(array, options)
117
+ else
118
+ Text::Box.new(string, options)
119
+ end
120
+
121
+ box.render
109
122
  end
110
123
 
111
124
  # Generally, one would use the Prawn::Text#text_box convenience
112
- # method. However, using Text::Box.new in conjunction with
125
+ # method. However, using Text::Box.new in conjunction with
113
126
  # #render(:dry_run=> true) enables one to do look-ahead calculations prior
114
127
  # to placing text on the page, or to determine how much vertical space was
115
128
  # consumed by the printed text
@@ -1,4 +1,5 @@
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"
1
+ require_relative "formatted/wrap"
2
+
3
+ require_relative "formatted/box"
4
+ require_relative "formatted/parser"
5
+ require_relative "formatted/fragment"
@@ -0,0 +1,292 @@
1
+ # encoding: utf-8
2
+
3
+ # core/text/formatted/arranger.rb : Implements a data structure for 2-stage
4
+ # processing of lines of formatted text
5
+ #
6
+ # Copyright February 2010, Daniel Nelson. All Rights Reserved.
7
+ #
8
+ # This is free software. Please see the LICENSE and COPYING files for details.
9
+
10
+ module Prawn
11
+ module Text
12
+ module Formatted #:nodoc:
13
+
14
+ class Arranger #:nodoc:
15
+ attr_reader :max_line_height
16
+ attr_reader :max_descender
17
+ attr_reader :max_ascender
18
+ attr_accessor :consumed
19
+
20
+ # The following present only for testing purposes
21
+ attr_reader :unconsumed
22
+ attr_reader :fragments
23
+ attr_reader :current_format_state
24
+
25
+ def initialize(document, options={})
26
+ @document = document
27
+ @fragments = []
28
+ @unconsumed = []
29
+ @kerning = options[:kerning]
30
+ end
31
+
32
+ def space_count
33
+ if @unfinalized_line
34
+ raise "Lines must be finalized before calling #space_count"
35
+ end
36
+ @fragments.inject(0) do |sum, fragment|
37
+ sum + fragment.space_count
38
+ end
39
+ end
40
+
41
+ def line_width
42
+ if @unfinalized_line
43
+ raise "Lines must be finalized before calling #line_width"
44
+ end
45
+ @fragments.inject(0) do |sum, fragment|
46
+ sum + fragment.width
47
+ end
48
+ end
49
+
50
+ def line
51
+ if @unfinalized_line
52
+ raise "Lines must be finalized before calling #line"
53
+ end
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
61
+ end
62
+
63
+ def finalize_line
64
+ @unfinalized_line = false
65
+ omit_trailing_whitespace_from_line_width
66
+ @fragments = []
67
+ @consumed.each do |hash|
68
+ text = hash[:text]
69
+ format_state = hash.dup
70
+ format_state.delete(:text)
71
+ fragment = Prawn::Text::Formatted::Fragment.new(text,
72
+ format_state,
73
+ @document)
74
+ @fragments << fragment
75
+ set_fragment_measurements(fragment)
76
+ set_line_measurement_maximums(fragment)
77
+ end
78
+ end
79
+
80
+ def format_array=(array)
81
+ initialize_line
82
+ @unconsumed = []
83
+ array.each do |hash|
84
+ hash[:text].scan(/[^\n]+|\n/) do |line|
85
+ @unconsumed << hash.merge(:text => line)
86
+ end
87
+ end
88
+ end
89
+
90
+ def initialize_line
91
+ @unfinalized_line = true
92
+ @max_line_height = 0
93
+ @max_descender = 0
94
+ @max_ascender = 0
95
+
96
+ @consumed = []
97
+ @fragments = []
98
+ end
99
+
100
+ def finished?
101
+ @unconsumed.length == 0
102
+ end
103
+
104
+ def next_string
105
+ unless @unfinalized_line
106
+ raise "Lines must not be finalized when calling #next_string"
107
+ end
108
+ hash = @unconsumed.shift
109
+ if hash.nil?
110
+ nil
111
+ else
112
+ @consumed << hash.dup
113
+ @current_format_state = hash.dup
114
+ @current_format_state.delete(:text)
115
+ hash[:text]
116
+ end
117
+ end
118
+
119
+ def preview_next_string
120
+ hash = @unconsumed.first
121
+ if hash.nil? then nil
122
+ else hash[:text]
123
+ end
124
+ end
125
+
126
+ def apply_color_and_font_settings(fragment, &block)
127
+ if fragment.color
128
+ original_fill_color = @document.fill_color
129
+ original_stroke_color = @document.stroke_color
130
+ @document.fill_color(*fragment.color)
131
+ @document.stroke_color(*fragment.color)
132
+ apply_font_settings(fragment, &block)
133
+ @document.stroke_color = original_stroke_color
134
+ @document.fill_color = original_fill_color
135
+ else
136
+ apply_font_settings(fragment, &block)
137
+ end
138
+ end
139
+
140
+ def apply_font_settings(fragment=nil, &block)
141
+ if fragment.nil?
142
+ font = current_format_state[:font]
143
+ size = current_format_state[:size]
144
+ character_spacing = current_format_state[:character_spacing] ||
145
+ @document.character_spacing
146
+ styles = current_format_state[:styles]
147
+ font_style = font_style(styles)
148
+ else
149
+ font = fragment.font
150
+ size = fragment.size
151
+ character_spacing = fragment.character_spacing
152
+ styles = fragment.styles
153
+ font_style = font_style(styles)
154
+ end
155
+
156
+ @document.character_spacing(character_spacing) do
157
+ if font || font_style != :normal
158
+ raise "Bad font family" unless @document.font.family
159
+ @document.font(font || @document.font.family, :style => font_style) do
160
+ apply_font_size(size, styles, &block)
161
+ end
162
+ else
163
+ apply_font_size(size, styles, &block)
164
+ end
165
+ end
166
+ end
167
+
168
+ def update_last_string(printed, unprinted, normalized_soft_hyphen=nil)
169
+ return if printed.nil?
170
+ if printed.empty?
171
+ @consumed.pop
172
+ else
173
+ @consumed.last[:text] = printed
174
+ if normalized_soft_hyphen
175
+ @consumed.last[:normalized_soft_hyphen] = normalized_soft_hyphen
176
+ end
177
+ end
178
+
179
+ unless unprinted.empty?
180
+ @unconsumed.unshift(@current_format_state.merge(:text => unprinted))
181
+ end
182
+
183
+ load_previous_format_state if printed.empty?
184
+ end
185
+
186
+ def retrieve_fragment
187
+ if @unfinalized_line
188
+ raise "Lines must be finalized before fragments can be retrieved"
189
+ end
190
+ @fragments.shift
191
+ end
192
+
193
+ def repack_unretrieved
194
+ new_unconsumed = []
195
+ while fragment = retrieve_fragment
196
+ fragment.include_trailing_white_space!
197
+ new_unconsumed << fragment.format_state.merge(:text => fragment.text)
198
+ end
199
+ @unconsumed = new_unconsumed.concat(@unconsumed)
200
+ end
201
+
202
+ def font_style(styles)
203
+ if styles.nil?
204
+ :normal
205
+ elsif styles.include?(:bold) && styles.include?(:italic)
206
+ :bold_italic
207
+ elsif styles.include?(:bold)
208
+ :bold
209
+ elsif styles.include?(:italic)
210
+ :italic
211
+ else
212
+ :normal
213
+ end
214
+ end
215
+
216
+ private
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
+
228
+ def apply_font_size(size, styles)
229
+ if subscript?(styles) || superscript?(styles)
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
236
+ end
237
+ if size.nil?
238
+ yield
239
+ else
240
+ @document.font_size(size) { yield }
241
+ end
242
+ end
243
+
244
+ def subscript?(styles)
245
+ if styles.nil? then false
246
+ else styles.include?(:subscript)
247
+ end
248
+ end
249
+
250
+ def superscript?(styles)
251
+ if styles.nil? then false
252
+ else styles.include?(:superscript)
253
+ end
254
+ end
255
+
256
+ def omit_trailing_whitespace_from_line_width
257
+ @consumed.reverse_each do |hash|
258
+ if hash[:text] == "\n"
259
+ break
260
+ elsif hash[:text].strip.empty? && @consumed.length > 1
261
+ # this entire fragment is trailing white space
262
+ hash[:exclude_trailing_white_space] = true
263
+ else
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
267
+ break
268
+ end
269
+ end
270
+ end
271
+
272
+ def set_fragment_measurements(fragment)
273
+ apply_font_settings(fragment) do
274
+ fragment.width = @document.width_of(fragment.text,
275
+ :kerning => @kerning)
276
+ fragment.line_height = @document.font.height
277
+ fragment.descender = @document.font.descender
278
+ fragment.ascender = @document.font.ascender
279
+ end
280
+ end
281
+
282
+ def set_line_measurement_maximums(fragment)
283
+ @max_line_height = [defined?(@max_line_height) && @max_line_height, fragment.line_height].compact.max
284
+ @max_descender = [defined?(@max_descender) && @max_descender, fragment.descender].compact.max
285
+ @max_ascender = [defined?(@max_ascender) && @max_ascender, fragment.ascender].compact.max
286
+ end
287
+
288
+ end
289
+
290
+ end
291
+ end
292
+ end