prawn 0.8.4 → 0.11.1.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (290) hide show
  1. data/COPYING +340 -0
  2. data/HACKING +50 -0
  3. data/LICENSE +56 -0
  4. data/README +141 -0
  5. data/Rakefile +52 -0
  6. data/data/encodings/win_ansi.txt +29 -0
  7. data/data/fonts/Action Man.dfont +0 -0
  8. data/data/fonts/Activa.ttf +0 -0
  9. data/data/fonts/Chalkboard.ttf +0 -0
  10. data/data/fonts/Courier-Bold.afm +342 -0
  11. data/data/fonts/Courier-BoldOblique.afm +342 -0
  12. data/data/fonts/Courier-Oblique.afm +342 -0
  13. data/data/fonts/Courier.afm +342 -0
  14. data/data/fonts/DejaVuSans.ttf +0 -0
  15. data/data/fonts/Dustismo_Roman.ttf +0 -0
  16. data/data/fonts/Helvetica-Bold.afm +2827 -0
  17. data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
  18. data/data/fonts/Helvetica-Oblique.afm +3051 -0
  19. data/data/fonts/Helvetica.afm +3051 -0
  20. data/data/fonts/MustRead.html +19 -0
  21. data/data/fonts/Symbol.afm +213 -0
  22. data/data/fonts/Times-Bold.afm +2588 -0
  23. data/data/fonts/Times-BoldItalic.afm +2384 -0
  24. data/data/fonts/Times-Italic.afm +2667 -0
  25. data/data/fonts/Times-Roman.afm +2419 -0
  26. data/data/fonts/ZapfDingbats.afm +225 -0
  27. data/data/fonts/comicsans.ttf +0 -0
  28. data/data/fonts/gkai00mp.ttf +0 -0
  29. data/data/images/16bit.alpha +0 -0
  30. data/data/images/16bit.dat +0 -0
  31. data/data/images/16bit.png +0 -0
  32. data/data/images/arrow.png +0 -0
  33. data/data/images/arrow2.png +0 -0
  34. data/data/images/barcode_issue.png +0 -0
  35. data/data/images/dice.alpha +0 -0
  36. data/data/images/dice.dat +0 -0
  37. data/data/images/dice.png +0 -0
  38. data/data/images/dice_interlaced.png +0 -0
  39. data/data/images/fractal.jpg +0 -0
  40. data/data/images/letterhead.jpg +0 -0
  41. data/data/images/page_white_text.alpha +0 -0
  42. data/data/images/page_white_text.dat +0 -0
  43. data/data/images/page_white_text.png +0 -0
  44. data/data/images/pigs.jpg +0 -0
  45. data/data/images/rails.dat +0 -0
  46. data/data/images/rails.png +0 -0
  47. data/data/images/ruport.png +0 -0
  48. data/data/images/ruport_data.dat +0 -0
  49. data/data/images/ruport_transparent.png +0 -0
  50. data/data/images/ruport_type0.png +0 -0
  51. data/data/images/stef.jpg +0 -0
  52. data/data/images/tru256.bmp +0 -0
  53. data/data/images/web-links.dat +1 -0
  54. data/data/images/web-links.png +0 -0
  55. data/data/pdfs/complex_template.pdf +0 -0
  56. data/data/pdfs/contains_ttf_font.pdf +0 -0
  57. data/data/pdfs/encrypted.pdf +0 -0
  58. data/data/pdfs/hexagon.pdf +61 -0
  59. data/data/pdfs/indirect_reference.pdf +86 -0
  60. data/data/pdfs/nested_pages.pdf +118 -0
  61. data/data/pdfs/page_without_mediabox.pdf +193 -0
  62. data/data/pdfs/resources_as_indirect_object.pdf +83 -0
  63. data/data/pdfs/two_hexagons.pdf +90 -0
  64. data/data/pdfs/version_1_6.pdf +61 -0
  65. data/data/shift_jis_text.txt +1 -0
  66. data/examples/bounding_box/bounding_boxes.rb +43 -0
  67. data/examples/bounding_box/indentation.rb +34 -0
  68. data/examples/bounding_box/russian_boxes.rb +36 -0
  69. data/examples/bounding_box/stretched_nesting.rb +67 -0
  70. data/examples/example_helper.rb +8 -0
  71. data/examples/general/background.rb +23 -0
  72. data/examples/general/canvas.rb +15 -0
  73. data/examples/general/context_sensitive_headers.rb +37 -0
  74. data/examples/general/float.rb +11 -0
  75. data/examples/general/margin.rb +36 -0
  76. data/examples/general/measurement_units.rb +51 -0
  77. data/examples/general/metadata-info.rb +16 -0
  78. data/examples/general/multi_page_layout.rb +18 -0
  79. data/examples/general/outlines.rb +66 -0
  80. data/examples/general/page_geometry.rb +31 -0
  81. data/examples/general/page_numbering.rb +15 -0
  82. data/examples/general/repeaters.rb +47 -0
  83. data/examples/general/stamp.rb +41 -0
  84. data/examples/general/templates.rb +13 -0
  85. data/examples/graphics/basic_images.rb +23 -0
  86. data/examples/graphics/cmyk.rb +12 -0
  87. data/examples/graphics/curves.rb +11 -0
  88. data/examples/graphics/hexagon.rb +13 -0
  89. data/examples/graphics/image_fit.rb +15 -0
  90. data/examples/graphics/image_flow.rb +37 -0
  91. data/examples/graphics/image_position.rb +17 -0
  92. data/examples/graphics/line.rb +32 -0
  93. data/examples/graphics/png_types.rb +22 -0
  94. data/examples/graphics/polygons.rb +16 -0
  95. data/examples/graphics/remote_images.rb +12 -0
  96. data/examples/graphics/rounded_polygons.rb +19 -0
  97. data/examples/graphics/rounded_rectangle.rb +20 -0
  98. data/examples/graphics/ruport_style_helpers.rb +19 -0
  99. data/examples/graphics/stroke_bounds.rb +20 -0
  100. data/examples/graphics/stroke_cap_and_join.rb +45 -0
  101. data/examples/graphics/stroke_dash.rb +42 -0
  102. data/examples/graphics/transformations.rb +52 -0
  103. data/examples/graphics/transparency.rb +26 -0
  104. data/examples/grid/bounding_boxes.rb +21 -0
  105. data/examples/grid/column_gutter_grid.rb +20 -0
  106. data/examples/grid/multi_boxes.rb +51 -0
  107. data/examples/grid/show_grid.rb +13 -0
  108. data/examples/grid/simple_grid.rb +20 -0
  109. data/examples/m17n/chinese_text_wrapping.rb +17 -0
  110. data/examples/m17n/euro.rb +15 -0
  111. data/examples/m17n/sjis.rb +28 -0
  112. data/examples/m17n/utf8.rb +13 -0
  113. data/examples/m17n/win_ansi_charset.rb +54 -0
  114. data/examples/security/hello_foo.rb +8 -0
  115. data/examples/table/bill.rb +53 -0
  116. data/examples/table/cell.rb +12 -0
  117. data/examples/table/checkerboard.rb +22 -0
  118. data/examples/table/header.rb +14 -0
  119. data/examples/table/inline_format_table.rb +12 -0
  120. data/examples/table/multi_page_table.rb +9 -0
  121. data/examples/table/simple_table.rb +24 -0
  122. data/examples/table/subtable.rb +12 -0
  123. data/examples/table/widths.rb +20 -0
  124. data/examples/text/alignment.rb +18 -0
  125. data/examples/text/character_spacing.rb +12 -0
  126. data/examples/text/dfont.rb +48 -0
  127. data/examples/text/family_based_styling.rb +24 -0
  128. data/examples/text/font_calculations.rb +91 -0
  129. data/examples/text/font_size.rb +33 -0
  130. data/examples/text/hyphenation.rb +45 -0
  131. data/examples/text/indent_paragraphs.rb +22 -0
  132. data/examples/text/inline_format.rb +103 -0
  133. data/examples/text/kerning.rb +30 -0
  134. data/examples/text/rotated.rb +98 -0
  135. data/examples/text/shaped_text_box.rb +31 -0
  136. data/examples/text/simple_text.rb +17 -0
  137. data/examples/text/simple_text_ttf.rb +17 -0
  138. data/examples/text/span.rb +29 -0
  139. data/examples/text/text_box.rb +88 -0
  140. data/examples/text/text_box_returning_excess.rb +51 -0
  141. data/examples/text/text_flow.rb +67 -0
  142. data/lib/prawn.rb +25 -1
  143. data/lib/prawn/compatibility.rb +51 -0
  144. data/lib/prawn/core.rb +85 -0
  145. data/lib/prawn/core/annotations.rb +61 -0
  146. data/lib/prawn/core/byte_string.rb +9 -0
  147. data/lib/prawn/core/destinations.rb +90 -0
  148. data/lib/prawn/core/document_state.rb +78 -0
  149. data/lib/prawn/core/literal_string.rb +16 -0
  150. data/lib/prawn/core/name_tree.rb +165 -0
  151. data/lib/prawn/core/object_store.rb +236 -0
  152. data/lib/prawn/core/page.rb +199 -0
  153. data/lib/prawn/core/pdf_object.rb +108 -0
  154. data/lib/prawn/core/reference.rb +112 -0
  155. data/lib/prawn/core/text.rb +140 -0
  156. data/lib/prawn/core/text/formatted/arranger.rb +266 -0
  157. data/lib/prawn/core/text/formatted/line_wrap.rb +127 -0
  158. data/lib/prawn/core/text/formatted/wrap.rb +112 -0
  159. data/lib/prawn/core/text/line_wrap.rb +211 -0
  160. data/lib/prawn/core/text/wrap.rb +82 -0
  161. data/lib/prawn/document.rb +574 -0
  162. data/lib/prawn/document/bounding_box.rb +425 -0
  163. data/lib/prawn/document/graphics_state.rb +48 -0
  164. data/lib/prawn/document/internals.rb +170 -0
  165. data/lib/prawn/document/page_geometry.rb +136 -0
  166. data/lib/prawn/document/snapshot.rb +87 -0
  167. data/lib/prawn/document/span.rb +55 -0
  168. data/lib/prawn/encoding.rb +121 -0
  169. data/lib/prawn/errors.rb +86 -0
  170. data/lib/prawn/font.rb +368 -0
  171. data/lib/prawn/font/afm.rb +225 -0
  172. data/lib/prawn/font/dfont.rb +42 -0
  173. data/lib/prawn/font/ttf.rb +350 -0
  174. data/lib/prawn/graphics.rb +325 -0
  175. data/lib/prawn/graphics/cap_style.rb +38 -0
  176. data/lib/prawn/graphics/color.rb +205 -0
  177. data/lib/prawn/graphics/dash.rb +71 -0
  178. data/lib/prawn/graphics/join_style.rb +38 -0
  179. data/lib/prawn/graphics/transformation.rb +156 -0
  180. data/lib/prawn/graphics/transparency.rb +99 -0
  181. data/lib/prawn/images.rb +361 -0
  182. data/lib/prawn/images/jpg.rb +46 -0
  183. data/lib/prawn/images/png.rb +226 -0
  184. data/lib/prawn/layout.rb +20 -0
  185. data/lib/prawn/layout/grid.rb +259 -0
  186. data/lib/prawn/measurement_extensions.rb +46 -0
  187. data/lib/prawn/measurements.rb +71 -0
  188. data/lib/prawn/outline.rb +326 -0
  189. data/lib/prawn/repeater.rb +129 -0
  190. data/lib/prawn/security.rb +262 -0
  191. data/lib/prawn/security/arcfour.rb +51 -0
  192. data/lib/prawn/stamp.rb +126 -0
  193. data/lib/prawn/table.rb +421 -0
  194. data/lib/prawn/table/cell.rb +360 -0
  195. data/lib/prawn/table/cell/in_table.rb +27 -0
  196. data/lib/prawn/table/cell/subtable.rb +65 -0
  197. data/lib/prawn/table/cell/text.rb +125 -0
  198. data/lib/prawn/table/cells.rb +189 -0
  199. data/lib/prawn/text.rb +449 -0
  200. data/lib/prawn/text/box.rb +385 -0
  201. data/lib/prawn/text/formatted.rb +4 -0
  202. data/lib/prawn/text/formatted/box.rb +222 -0
  203. data/lib/prawn/text/formatted/fragment.rb +181 -0
  204. data/lib/prawn/text/formatted/parser.rb +213 -0
  205. data/prawn.gemspec +28 -0
  206. data/spec/annotations_spec.rb +90 -0
  207. data/spec/bounding_box_spec.rb +190 -0
  208. data/spec/cell_spec.rb +359 -0
  209. data/spec/destinations_spec.rb +15 -0
  210. data/spec/document_spec.rb +476 -0
  211. data/spec/font_spec.rb +324 -0
  212. data/spec/formatted_text_arranger_spec.rb +426 -0
  213. data/spec/formatted_text_box_spec.rb +756 -0
  214. data/spec/formatted_text_fragment_spec.rb +211 -0
  215. data/spec/graphics_spec.rb +446 -0
  216. data/spec/grid_spec.rb +85 -0
  217. data/spec/images_spec.rb +117 -0
  218. data/spec/inline_formatted_text_parser_spec.rb +502 -0
  219. data/spec/jpg_spec.rb +25 -0
  220. data/spec/line_wrap_spec.rb +341 -0
  221. data/spec/measurement_units_spec.rb +23 -0
  222. data/spec/name_tree_spec.rb +112 -0
  223. data/spec/object_store_spec.rb +160 -0
  224. data/spec/outline_spec.rb +404 -0
  225. data/spec/pdf_object_spec.rb +170 -0
  226. data/spec/png_spec.rb +237 -0
  227. data/spec/reference_spec.rb +82 -0
  228. data/spec/repeater_spec.rb +96 -0
  229. data/spec/security_spec.rb +120 -0
  230. data/spec/snapshot_spec.rb +154 -0
  231. data/spec/span_spec.rb +49 -0
  232. data/spec/spec_helper.rb +26 -0
  233. data/spec/stamp_spec.rb +108 -0
  234. data/spec/stroke_styles_spec.rb +163 -0
  235. data/spec/table_spec.rb +598 -0
  236. data/spec/template_spec.rb +165 -0
  237. data/spec/text_at_spec.rb +119 -0
  238. data/spec/text_box_spec.rb +742 -0
  239. data/spec/text_spacing_spec.rb +75 -0
  240. data/spec/text_spec.rb +342 -0
  241. data/spec/text_with_inline_formatting_spec.rb +193 -0
  242. data/spec/transparency_spec.rb +89 -0
  243. data/vendor/pdf-inspector/README +18 -0
  244. data/vendor/pdf-inspector/lib/pdf/inspector.rb +26 -0
  245. data/vendor/pdf-inspector/lib/pdf/inspector/extgstate.rb +18 -0
  246. data/vendor/pdf-inspector/lib/pdf/inspector/graphics.rb +131 -0
  247. data/vendor/pdf-inspector/lib/pdf/inspector/page.rb +25 -0
  248. data/vendor/pdf-inspector/lib/pdf/inspector/text.rb +46 -0
  249. data/vendor/pdf-inspector/lib/pdf/inspector/xobject.rb +19 -0
  250. data/vendor/ttfunk/data/fonts/DejaVuSans.ttf +0 -0
  251. data/vendor/ttfunk/data/fonts/comicsans.ttf +0 -0
  252. data/vendor/ttfunk/example.rb +45 -0
  253. data/vendor/ttfunk/lib/ttfunk.rb +102 -0
  254. data/vendor/ttfunk/lib/ttfunk/directory.rb +17 -0
  255. data/vendor/ttfunk/lib/ttfunk/encoding/mac_roman.rb +88 -0
  256. data/vendor/ttfunk/lib/ttfunk/encoding/windows_1252.rb +69 -0
  257. data/vendor/ttfunk/lib/ttfunk/reader.rb +44 -0
  258. data/vendor/ttfunk/lib/ttfunk/resource_file.rb +78 -0
  259. data/vendor/ttfunk/lib/ttfunk/subset.rb +18 -0
  260. data/vendor/ttfunk/lib/ttfunk/subset/base.rb +141 -0
  261. data/vendor/ttfunk/lib/ttfunk/subset/mac_roman.rb +50 -0
  262. data/vendor/ttfunk/lib/ttfunk/subset/unicode.rb +48 -0
  263. data/vendor/ttfunk/lib/ttfunk/subset/unicode_8bit.rb +63 -0
  264. data/vendor/ttfunk/lib/ttfunk/subset/windows_1252.rb +55 -0
  265. data/vendor/ttfunk/lib/ttfunk/subset_collection.rb +72 -0
  266. data/vendor/ttfunk/lib/ttfunk/table.rb +46 -0
  267. data/vendor/ttfunk/lib/ttfunk/table/cmap.rb +34 -0
  268. data/vendor/ttfunk/lib/ttfunk/table/cmap/format00.rb +54 -0
  269. data/vendor/ttfunk/lib/ttfunk/table/cmap/format04.rb +126 -0
  270. data/vendor/ttfunk/lib/ttfunk/table/cmap/subtable.rb +79 -0
  271. data/vendor/ttfunk/lib/ttfunk/table/glyf.rb +64 -0
  272. data/vendor/ttfunk/lib/ttfunk/table/glyf/compound.rb +81 -0
  273. data/vendor/ttfunk/lib/ttfunk/table/glyf/simple.rb +37 -0
  274. data/vendor/ttfunk/lib/ttfunk/table/head.rb +44 -0
  275. data/vendor/ttfunk/lib/ttfunk/table/hhea.rb +41 -0
  276. data/vendor/ttfunk/lib/ttfunk/table/hmtx.rb +47 -0
  277. data/vendor/ttfunk/lib/ttfunk/table/kern.rb +79 -0
  278. data/vendor/ttfunk/lib/ttfunk/table/kern/format0.rb +62 -0
  279. data/vendor/ttfunk/lib/ttfunk/table/loca.rb +43 -0
  280. data/vendor/ttfunk/lib/ttfunk/table/maxp.rb +40 -0
  281. data/vendor/ttfunk/lib/ttfunk/table/name.rb +125 -0
  282. data/vendor/ttfunk/lib/ttfunk/table/os2.rb +78 -0
  283. data/vendor/ttfunk/lib/ttfunk/table/post.rb +91 -0
  284. data/vendor/ttfunk/lib/ttfunk/table/post/format10.rb +43 -0
  285. data/vendor/ttfunk/lib/ttfunk/table/post/format20.rb +35 -0
  286. data/vendor/ttfunk/lib/ttfunk/table/post/format25.rb +23 -0
  287. data/vendor/ttfunk/lib/ttfunk/table/post/format30.rb +17 -0
  288. data/vendor/ttfunk/lib/ttfunk/table/post/format40.rb +17 -0
  289. data/vendor/ttfunk/lib/ttfunk/table/simple.rb +14 -0
  290. metadata +337 -48
@@ -0,0 +1,125 @@
1
+ # encoding: utf-8
2
+
3
+ # text.rb: Text table cells.
4
+ #
5
+ # Copyright December 2009, Gregory Brown and Brad Ediger. All Rights Reserved.
6
+ #
7
+ # This is free software. Please see the LICENSE and COPYING files for details.
8
+ module Prawn
9
+ class Table
10
+ class Cell
11
+
12
+ # A Cell that contains text. Has some limited options to set font family,
13
+ # size, and style.
14
+ #
15
+ class Text < Cell
16
+
17
+ TextOptions = [:inline_format, :kerning, :size, :style,
18
+ :align, :valign, :rotate, :rotate_around, :leading, :single_line,
19
+ :skip_encoding, :overflow, :min_font_size]
20
+
21
+ TextOptions.each do |option|
22
+ define_method("#{option}=") { |v| @text_options[option] = v }
23
+ define_method(option) { @text_options[option] }
24
+ end
25
+
26
+ attr_writer :font, :text_color
27
+
28
+ def initialize(pdf, point, options={})
29
+ @text_options = {}
30
+ super
31
+
32
+ # Sets a reasonable minimum width. If the cell has any content, make
33
+ # sure we have enough width to be at least one character wide. This is
34
+ # a bit of a hack, but it should work well enough.
35
+ min_content_width = [natural_content_width, styled_width_of("M")].min
36
+ @min_width = padding_left + padding_right + min_content_width
37
+ end
38
+
39
+ # Returns the font that will be used to draw this cell.
40
+ #
41
+ def font
42
+ with_font { @pdf.font }
43
+ end
44
+
45
+ # Returns the width of this text with no wrapping. This will be far off
46
+ # from the final width if the text is long.
47
+ #
48
+ def natural_content_width
49
+ [styled_width_of(@content), @pdf.bounds.width].min
50
+ end
51
+
52
+ # Returns the natural height of this block of text, wrapped to the
53
+ # preset width.
54
+ #
55
+ def natural_content_height
56
+ with_font do
57
+ b = text_box(:width => content_width + FPTolerance)
58
+ b.render(:dry_run => true)
59
+ b.height
60
+ end
61
+ end
62
+
63
+ # Draws the text content into its bounding box.
64
+ #
65
+ def draw_content
66
+ with_font do
67
+ @pdf.move_down((@pdf.font.line_gap + @pdf.font.descender)/2)
68
+ with_text_color do
69
+ text_box(:width => content_width + FPTolerance,
70
+ :height => content_height + FPTolerance,
71
+ :at => [0, @pdf.cursor]).render
72
+ end
73
+ end
74
+ end
75
+
76
+ protected
77
+
78
+ def with_font
79
+ @pdf.save_font do
80
+ options = {}
81
+ options[:style] = @text_options[:style] if @text_options[:style]
82
+
83
+ @pdf.font(@font || @pdf.font.name, options)
84
+
85
+ yield
86
+ end
87
+ end
88
+
89
+ def with_text_color
90
+ old_color = @pdf.fill_color || '000000'
91
+ @pdf.fill_color(@text_color) if @text_color
92
+ yield
93
+ ensure
94
+ @pdf.fill_color(old_color)
95
+ end
96
+
97
+ def text_box(extra_options={})
98
+ if @text_options[:inline_format]
99
+ options = @text_options.dup
100
+ options.delete(:inline_format)
101
+
102
+ array = ::Prawn::Text::Formatted::Parser.to_array(@content)
103
+ ::Prawn::Text::Formatted::Box.new(array,
104
+ options.merge(extra_options).merge(:document => @pdf))
105
+ else
106
+ ::Prawn::Text::Box.new(@content, @text_options.merge(extra_options).
107
+ merge(:document => @pdf))
108
+ end
109
+ end
110
+
111
+ # Returns the width of +text+ under the given text options.
112
+ #
113
+ def styled_width_of(text)
114
+ with_font do
115
+ options = {}
116
+ options[:size] = @text_options[:size] if @text_options[:size]
117
+
118
+ @pdf.font.compute_width_of(text, options)
119
+ end
120
+ end
121
+
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,189 @@
1
+ # encoding: utf-8
2
+
3
+ # cells.rb: Methods for accessing rows, columns, and cells of a Prawn::Table.
4
+ #
5
+ # Copyright December 2009, Brad Ediger. All Rights Reserved.
6
+ #
7
+ # This is free software. Please see the LICENSE and COPYING files for details.
8
+
9
+ module Prawn
10
+ class Table
11
+
12
+ # Returns a Cells object that can be used to select and style cells. See
13
+ # the Cells documentation for things you can do with cells.
14
+ #
15
+ def cells
16
+ @cell_proxy ||= Cells.new(@cells)
17
+ end
18
+
19
+ # Selects the given rows (0-based) for styling. Returns a Cells object --
20
+ # see the documentation on Cells for things you can do with cells.
21
+ #
22
+ def rows(row_spec)
23
+ cells.rows(row_spec)
24
+ end
25
+ alias_method :row, :rows
26
+
27
+ # Selects the given columns (0-based) for styling. Returns a Cells object
28
+ # -- see the documentation on Cells for things you can do with cells.
29
+ #
30
+ def columns(col_spec)
31
+ cells.columns(col_spec)
32
+ end
33
+ alias_method :column, :columns
34
+
35
+ # Represents a selection of cells to be styled. Operations on a CellProxy
36
+ # can be chained, and cell properties can be set one-for-all on the proxy.
37
+ #
38
+ # To set vertical borders only:
39
+ #
40
+ # table.cells.borders = [:left, :right]
41
+ #
42
+ # To highlight a rectangular area of the table:
43
+ #
44
+ # table.rows(1..3).columns(2..4).background_color = 'ff0000'
45
+ #
46
+ class Cells < Array
47
+
48
+ # Limits selection to the given row or rows. +row_spec+ can be anything
49
+ # that responds to the === operator selecting a set of 0-based row
50
+ # numbers; most commonly a number or a range.
51
+ #
52
+ # table.row(0) # selects first row
53
+ # table.rows(3..4) # selects rows four and five
54
+ #
55
+ def rows(row_spec)
56
+ index_cells unless @indexed
57
+ Cells.new(@rows[row_spec] ||= select{ |c| row_spec === c.row })
58
+ end
59
+ alias_method :row, :rows
60
+
61
+ # Limits selection to the given column or columns. +col_spec+ can be
62
+ # anything that responds to the === operator selecting a set of 0-based
63
+ # column numbers; most commonly a number or a range.
64
+ #
65
+ # table.column(0) # selects first column
66
+ # table.columns(3..4) # selects columns four and five
67
+ #
68
+ def columns(row_spec)
69
+ index_cells unless @indexed
70
+ Cells.new(@columns[row_spec] ||= select{ |c| row_spec === c.column })
71
+ end
72
+ alias_method :column, :columns
73
+
74
+ # Allows you to filter the given cells by arbitrary properties.
75
+ #
76
+ # table.column(4).filter { |cell| cell.content =~ /Yes/ }.
77
+ # background_color = '00ff00'
78
+ #
79
+ def filter(&block)
80
+ Cells.new(select(&block))
81
+ end
82
+
83
+ # Retrieves a cell based on its 0-based row and column. Returns an
84
+ # individual Cell, not a Cells collection.
85
+ #
86
+ # table.cells[0, 0].content # => "First cell content"
87
+ #
88
+ def [](row, col)
89
+ find { |c| c.row == row && c.column == col }
90
+ end
91
+
92
+ # Supports setting multiple properties at once.
93
+ #
94
+ # table.cells.style(:padding => 0, :border_width => 2)
95
+ #
96
+ # is the same as:
97
+ #
98
+ # table.cells.padding = 0
99
+ # table.cells.border_width = 2
100
+ #
101
+ # You can also pass a block, which will be called for each cell in turn.
102
+ # This allows you to set more complicated properties:
103
+ #
104
+ # table.cells.style { |cell| cell.border_width += 12 }
105
+ #
106
+ def style(options={}, &block)
107
+ each do |cell|
108
+ options.each { |k, v| cell.send("#{k}=", v) }
109
+ block.call(cell) if block
110
+ end
111
+ end
112
+
113
+ # Returns the total width of all columns in the selected set.
114
+ #
115
+ def width
116
+ column_widths = {}
117
+ each do |cell|
118
+ column_widths[cell.column] =
119
+ [column_widths[cell.column], cell.width].compact.max
120
+ end
121
+ column_widths.values.inject(0) { |sum, width| sum + width }
122
+ end
123
+
124
+ # Returns minimum width required to contain cells in the set.
125
+ #
126
+ def min_width
127
+ column_min_widths = {}
128
+ each do |cell|
129
+ column_min_widths[cell.column] =
130
+ [column_min_widths[cell.column], cell.min_width].compact.max
131
+ end
132
+ column_min_widths.values.inject(0) { |sum, width| sum + width }
133
+ end
134
+
135
+ # Returns maximum width that can contain cells in the set.
136
+ #
137
+ def max_width
138
+ column_max_widths = {}
139
+ each do |cell|
140
+ column_max_widths[cell.column] =
141
+ [column_max_widths[cell.column], cell.max_width].compact.min
142
+ end
143
+ column_max_widths.values.inject(0) { |sum, width| sum + width }
144
+ end
145
+
146
+ # Returns the total height of all rows in the selected set.
147
+ #
148
+ def height
149
+ row_heights = {}
150
+ each do |cell|
151
+ row_heights[cell.row] =
152
+ [row_heights[cell.row], cell.height].compact.max
153
+ end
154
+ row_heights.values.inject(0) { |sum, width| sum + width }
155
+ end
156
+
157
+ # Supports setting arbitrary properties on a group of cells.
158
+ #
159
+ # table.cells.row(3..6).background_color = 'cc0000'
160
+ #
161
+ def method_missing(id, *args, &block)
162
+ each { |c| c.send(id, *args, &block) }
163
+ end
164
+
165
+ protected
166
+
167
+ # Defers indexing until rows() or columns() is actually called on the
168
+ # Cells object. Without this, we would needlessly index the leaf nodes of
169
+ # the object graph, the ones that are only there to be iterated over.
170
+ #
171
+ # Make sure to call this before using @rows or @columns.
172
+ #
173
+ def index_cells
174
+ @rows = {}
175
+ @columns = {}
176
+
177
+ each do |cell|
178
+ @rows[cell.row] ||= []
179
+ @rows[cell.row] << cell
180
+
181
+ @columns[cell.column] ||= []
182
+ @columns[cell.column] << cell
183
+ end
184
+
185
+ @indexed = true
186
+ end
187
+ end
188
+ end
189
+ end
@@ -0,0 +1,449 @@
1
+ # encoding: utf-8
2
+
3
+ # text.rb : Implements PDF text primitives
4
+ #
5
+ # Copyright May 2008, Gregory Brown. All Rights Reserved.
6
+ #
7
+ # This is free software. Please see the LICENSE and COPYING files for details.
8
+ require "prawn/core/text"
9
+ require "prawn/core/text/wrap"
10
+ require "prawn/text/box"
11
+ require "prawn/text/formatted"
12
+ require "zlib"
13
+
14
+ module Prawn
15
+ module Text
16
+
17
+ include Prawn::Core::Text
18
+ include Prawn::Text::Formatted
19
+
20
+ Prawn::Text::NBSP = " "
21
+
22
+ # If you want text to flow onto a new page or between columns, this is the
23
+ # method to use. If, instead, if you want to place bounded text outside of
24
+ # the flow of a document (for captions, labels, charts, etc.), use Text::Box
25
+ # or its convenience method text_box.
26
+ #
27
+ # Draws text on the page. Prawn attempts to wrap the text to fit within your
28
+ # current bounding box (or margin_box if no bounding box is being used).
29
+ # Text will flow onto the next page when it reaches the bottom of the
30
+ # bounding box. Text wrap in Prawn does not re-flow linebreaks, so if you
31
+ # want fully automated text wrapping, be sure to remove newlines before
32
+ # attempting to draw your string.
33
+ #
34
+ # == Examples
35
+ #
36
+ # pdf.text "Will be wrapped when it hits the edge of your bounding box"
37
+ # pdf.text "This will be centered", :align => :center
38
+ # pdf.text "This will be right aligned", :align => :right
39
+ # pdf.text "This <i>includes <b>inline</b></i> <font size='24'>" +
40
+ # "formatting</font>", :inline_format => true
41
+ #
42
+ # If your font contains kerning pair data that Prawn can parse, the
43
+ # text will be kerned by default. You can disable kerning by including
44
+ # a false <tt>:kerning</tt> option. If you want to disable kerning on an
45
+ # entire document, set default_kerning = false for that document
46
+ #
47
+ # === Text Positioning Details
48
+ #
49
+ # The text is positioned at font.ascender below the baseline,
50
+ # making it easy to use this method within bounding boxes and spans.
51
+ #
52
+ # == Encoding
53
+ #
54
+ # Note that strings passed to this function should be encoded as UTF-8.
55
+ # If you get unexpected characters appearing in your rendered document,
56
+ # check this.
57
+ #
58
+ # If the current font is a built-in one, although the string must be
59
+ # encoded as UTF-8, only characters that are available in WinAnsi
60
+ # are allowed.
61
+ #
62
+ # If an empty box is rendered to your PDF instead of the character you
63
+ # wanted it usually means the current font doesn't include that character.
64
+ #
65
+ # == Options (default values marked in [])
66
+ #
67
+ # <tt>:inline_format</tt>::
68
+ # <tt>boolean</tt>. If true, then the string parameter is interpreted
69
+ # as a HTML-esque string that recognizes the following tags:
70
+ # <tt>\<b></b></tt>:: bold
71
+ # <tt>\<i></i></tt>:: italic
72
+ # <tt>\<u></u></tt>:: underline
73
+ # <tt>\<strikethrough></strikethrough></tt>:: strikethrough
74
+ # <tt>\<sub></sub></tt>:: subscript
75
+ # <tt>\<sup></sup></tt>:: superscript
76
+ # <tt>\<font></font></tt>::
77
+ # with the following attributes (using double or single quotes)
78
+ # <tt>size="24"</tt>::
79
+ # attribute for setting size
80
+ # <tt>character_spacing="2.5"</tt>::
81
+ # attribute for setting character spacing
82
+ # <tt>name="Helvetica"</tt>::
83
+ # attribute for setting the font. The font name must be an
84
+ # AFM font with the desired faces or must be a font that is
85
+ # already registered using Prawn::Document#font_families
86
+ # <tt>\<color></color></tt>::
87
+ # with the following attributes
88
+ # <tt>rgb="ffffff" or rgb="#ffffff"</tt>::
89
+ # <tt>c="100" m="100" y="100" k="100"</tt>::
90
+ # <tt>\<link></link></tt>::
91
+ # with the following attributes
92
+ # <tt>href="http://example.com"</tt>:: an external link
93
+ # <tt>anchor="ToC"</tt>::
94
+ # where the value of the anchor attribute is the name of a
95
+ # destination that has already been or will be registered
96
+ # using Prawn::Core::Destinations#add_dest. A clickable link
97
+ # will be created to that destination.
98
+ # Note that you must explicitly underline and color using the
99
+ # appropriate tags if you which to draw attention to the link
100
+ #
101
+ # <tt>:kerning</tt>:: <tt>boolean</tt>. Whether or not to use kerning (if it
102
+ # is available with the current font)
103
+ # [value of default_kerning?]
104
+ # <tt>:size</tt>:: <tt>number</tt>. The font size to use. [current font
105
+ # size]
106
+ # <tt>:character_spacing</tt>:: <tt>number</tt>. The amount of space to add
107
+ # to or remove from the default character
108
+ # spacing. [0]
109
+ # <tt>:style</tt>:: The style to use. The requested style must be part of
110
+ # the current font familly. [current style]
111
+ # <tt>:indent_paragraphs</tt>:: <tt>number</tt>. The amount to indent the
112
+ # first line of each paragraph. Omit this
113
+ # option if you do not want indenting
114
+ # <tt>:align</tt>:: <tt>:left</tt>, <tt>:center</tt>, <tt>:right</tt>, or
115
+ # <tt>:justify</tt> Alignment within the bounding box [:left]
116
+ # <tt>:valign</tt>:: <tt>:top</tt>, <tt>:center</tt>, or <tt>:bottom</tt>.
117
+ # Vertical alignment within the bounding box [:top]
118
+ # <tt>:leading</tt>:: <tt>number</tt>. Additional space between lines [0]
119
+ # <tt>:final_gap</tt>:: <tt>boolean</tt>. If true, then the space between
120
+ # each line is included below the last line;
121
+ # otherwise, document.y is placed just below the
122
+ # descender of the last line printed [true]
123
+ #
124
+ # == Exceptions
125
+ #
126
+ # Raises <tt>ArgumentError</tt> if <tt>:at</tt> option included
127
+ #
128
+ # Raises <tt>Prawn::Errrors::CannotFit</tt> if not wide enough to print
129
+ # any text
130
+ #
131
+ def text(string, options={})
132
+ # we modify the options. don't change the user's hash
133
+ options = options.dup
134
+
135
+ if options[:inline_format]
136
+ options.delete(:inline_format)
137
+ array = Text::Formatted::Parser.to_array(string)
138
+ formatted_text(array, options)
139
+ return
140
+ end
141
+
142
+ inspect_options_for_text(options)
143
+
144
+ if @indent_paragraphs
145
+ string.split("\n").each do |paragraph|
146
+ options[:skip_encoding] = false
147
+ remaining_text = draw_indented_line(paragraph, options)
148
+ options[:skip_encoding] = true
149
+ if remaining_text == paragraph
150
+ # we were too close to the bottom of the page to print even one line
151
+ @bounding_box.move_past_bottom
152
+ remaining_text = draw_indented_line(paragraph, options)
153
+ end
154
+ remaining_text = fill_text_box(remaining_text, options)
155
+ draw_remaining_text_on_new_pages(remaining_text, options)
156
+ end
157
+ else
158
+ remaining_text = fill_text_box(string, options)
159
+ options[:skip_encoding] = true
160
+ draw_remaining_text_on_new_pages(remaining_text, options)
161
+ end
162
+ end
163
+
164
+
165
+ # Draws formatted text to the page.
166
+ # Formatted text is comprised of an array of hashes, where each hash defines
167
+ # text and format information. See Text::Formatted#formatted_text_box for
168
+ # more information on the structure of this array
169
+ #
170
+ # == Example
171
+ #
172
+ # text([{ :text => "hello" },
173
+ # { :text => "world",
174
+ # :size => 24,
175
+ # :style => [:bold, :italic] }])
176
+ #
177
+ # == Options
178
+ #
179
+ # Accepts the same options as #text
180
+ #
181
+ # == Exceptions
182
+ #
183
+ # Same as for #text
184
+ #
185
+ def formatted_text(array, options={})
186
+ # we modify the options. don't change the user's hash
187
+ options = options.dup
188
+
189
+ inspect_options_for_text(options)
190
+
191
+ if @indent_paragraphs
192
+ Text::Formatted::Parser.array_paragraphs(array).each do |paragraph|
193
+ options[:skip_encoding] = false
194
+ remaining_text = draw_indented_formatted_line(paragraph, options)
195
+ options[:skip_encoding] = true
196
+ if remaining_text == paragraph
197
+ # we were too close to the bottom of the page to print even one line
198
+ @bounding_box.move_past_bottom
199
+ remaining_text = draw_indented_formatted_line(paragraph, options)
200
+ end
201
+ remaining_text = fill_formatted_text_box(remaining_text, options)
202
+ draw_remaining_formatted_text_on_new_pages(remaining_text, options)
203
+ end
204
+ else
205
+ remaining_text = fill_formatted_text_box(array, options)
206
+ options[:skip_encoding] = true
207
+ draw_remaining_formatted_text_on_new_pages(remaining_text, options)
208
+ end
209
+ end
210
+
211
+ # Draws text on the page, beginning at the point specified by the :at option
212
+ # the string is assumed to be pre-formatted to properly fit the page.
213
+ #
214
+ # pdf.draw_text "Hello World", :at => [100,100]
215
+ # pdf.draw_text "Goodbye World", :at => [50,50], :size => 16
216
+ #
217
+ # If your font contains kerning pair data that Prawn can parse, the
218
+ # text will be kerned by default. You can disable kerning by including
219
+ # a false <tt>:kerning</tt> option. If you want to disable kerning on an
220
+ # entire document, set default_kerning = false for that document
221
+ #
222
+ # === Text Positioning Details:
223
+ #
224
+ # Prawn will position your text by the left-most edge of its baseline, and
225
+ # flow along a single line. (This means that :align will not work)
226
+ #
227
+ # == Rotation
228
+ #
229
+ # Text can be rotated before it is placed on the canvas by specifying the
230
+ # <tt>:rotate</tt> option with a given angle. Rotation occurs counter-clockwise.
231
+ #
232
+ # == Encoding
233
+ #
234
+ # Note that strings passed to this function should be encoded as UTF-8.
235
+ # If you get unexpected characters appearing in your rendered document,
236
+ # check this.
237
+ #
238
+ # If the current font is a built-in one, although the string must be
239
+ # encoded as UTF-8, only characters that are available in WinAnsi
240
+ # are allowed.
241
+ #
242
+ # If an empty box is rendered to your PDF instead of the character you
243
+ # wanted it usually means the current font doesn't include that character.
244
+ #
245
+ # == Options (default values marked in [])
246
+ #
247
+ # <tt>:at</tt>:: <tt>[x, y]</tt>(required). The position at which to start the text
248
+ # <tt>:kerning</tt>:: <tt>boolean</tt>. Whether or not to use kerning (if it
249
+ # is available with the current font)
250
+ # [value of default_kerning?]
251
+ # <tt>:size</tt>:: <tt>number</tt>. The font size to use. [current font
252
+ # size]
253
+ # <tt>:style</tt>:: The style to use. The requested style must be part of
254
+ # the current font familly. [current style]
255
+ #
256
+ # <tt>:rotate</tt>:: <tt>number</tt>. The angle to which to rotate text
257
+ #
258
+ # == Exceptions
259
+ #
260
+ # Raises <tt>ArgumentError</tt> if <tt>:at</tt> option omitted
261
+ #
262
+ # Raises <tt>ArgumentError</tt> if <tt>:align</tt> option included
263
+ #
264
+ def draw_text(text, options)
265
+ # we modify the options. don't change the user's hash
266
+ options = options.dup
267
+ inspect_options_for_draw_text(options)
268
+ # dup because normalize_encoding changes the string
269
+ text = text.to_s.dup
270
+ save_font do
271
+ process_text_options(options)
272
+ font.normalize_encoding!(text) unless @skip_encoding
273
+ font_size(options[:size]) { draw_text!(text, options) }
274
+ end
275
+ end
276
+
277
+ # Gets height of text in PDF points.
278
+ # Same options as #text, except as noted.
279
+ # Not compatible with :indent_paragraphs option
280
+ #
281
+ # ==Example
282
+ #
283
+ # height_of("hello\nworld")
284
+ #
285
+ # == Exceptions
286
+ #
287
+ # Raises <tt>NotImplementedError</tt> if <tt>:indent_paragraphs</tt>
288
+ # option included
289
+ #
290
+ # Raises <tt>Prawn::Errrors::CannotFit</tt> if not wide enough to print
291
+ # any text
292
+ #
293
+ def height_of(string, options={})
294
+ if options[:indent_paragraphs]
295
+ raise NotImplementedError, ":indent_paragraphs option not available" +
296
+ "with height_of"
297
+ end
298
+ process_final_gap_option(options)
299
+ box = Text::Box.new(string,
300
+ options.merge(:height => 100000000,
301
+ :document => self))
302
+ printed = box.render(:dry_run => true)
303
+
304
+ height = box.height - (box.line_height - box.ascender)
305
+ height += box.line_height + box.leading - box.ascender if @final_gap
306
+ height
307
+ end
308
+
309
+ # Gets height of formatted text in PDF points.
310
+ # See documentation for #height_of.
311
+ #
312
+ # ==Example
313
+ #
314
+ # height_of_formatted([{ :text => "hello" },
315
+ # { :text => "world",
316
+ # :size => 24,
317
+ # :style => [:bold, :italic] }])
318
+ #
319
+ def height_of_formatted(array, options={})
320
+ if options[:indent_paragraphs]
321
+ raise NotImplementedError, ":indent_paragraphs option not available" +
322
+ "with height_of"
323
+ end
324
+ process_final_gap_option(options)
325
+ box = Text::Formatted::Box.new(array,
326
+ options.merge(:height => 100000000,
327
+ :document => self))
328
+ printed = box.render(:dry_run => true)
329
+
330
+ height = box.height - (box.line_height - box.ascender)
331
+ height += box.line_height + box.leading - box.ascender if @final_gap
332
+ height
333
+ end
334
+
335
+ private
336
+
337
+ def draw_remaining_text_on_new_pages(remaining_text, options)
338
+ while remaining_text.length > 0
339
+ @bounding_box.move_past_bottom
340
+ previous_remaining_text = remaining_text
341
+ remaining_text = fill_text_box(remaining_text, options)
342
+ break if remaining_text == previous_remaining_text
343
+ end
344
+ end
345
+
346
+ def draw_indented_line(string, options)
347
+ indent(@indent_paragraphs) do
348
+ fill_text_box(string, options.dup.merge(:single_line => true))
349
+ end
350
+ end
351
+
352
+ def fill_text_box(text, options)
353
+ merge_text_box_positioning_options(options)
354
+
355
+ box = Text::Box.new(text, options)
356
+ remaining_text = box.render
357
+
358
+ self.y -= box.height - (box.line_height - box.ascender)
359
+ if @final_gap
360
+ self.y -= box.line_height + box.leading - box.ascender
361
+ end
362
+ remaining_text
363
+ end
364
+
365
+
366
+
367
+
368
+ def draw_remaining_formatted_text_on_new_pages(remaining_text, options)
369
+ while remaining_text.length > 0
370
+ @bounding_box.move_past_bottom
371
+ previous_remaining_text = remaining_text
372
+ remaining_text = fill_formatted_text_box(remaining_text, options)
373
+ break if remaining_text == previous_remaining_text
374
+ end
375
+ end
376
+
377
+ def draw_indented_formatted_line(string, options)
378
+ indent(@indent_paragraphs) do
379
+ fill_formatted_text_box(string, options.dup.merge(:single_line => true))
380
+ end
381
+ end
382
+
383
+ def fill_formatted_text_box(text, options)
384
+ merge_text_box_positioning_options(options)
385
+ box = Text::Formatted::Box.new(text, options)
386
+ remaining_text = box.render
387
+
388
+ self.y -= box.height - (box.line_height - box.ascender)
389
+ if @final_gap
390
+ self.y -= box.line_height + box.leading - box.ascender
391
+ end
392
+ remaining_text
393
+ end
394
+
395
+
396
+
397
+ def merge_text_box_positioning_options(options)
398
+ bottom = @bounding_box.stretchy? ? @margin_box.absolute_bottom :
399
+ @bounding_box.absolute_bottom
400
+
401
+ options[:height] = y - bottom
402
+ options[:width] = bounds.width
403
+ options[:at] = [@bounding_box.left_side - @bounding_box.absolute_left,
404
+ y - @bounding_box.absolute_bottom]
405
+ end
406
+
407
+ def inspect_options_for_draw_text(options)
408
+ if options[:at].nil?
409
+ raise ArgumentError, "The :at option is required for draw_text"
410
+ elsif options[:align]
411
+ raise ArgumentError, "The :align option does not work with draw_text"
412
+ end
413
+ if options[:kerning].nil? then
414
+ options[:kerning] = default_kerning?
415
+ end
416
+ valid_options = Prawn::Core::Text::VALID_OPTIONS + [:at, :rotate]
417
+ Prawn.verify_options(valid_options, options)
418
+ end
419
+
420
+ def inspect_options_for_text(options)
421
+ if options[:at]
422
+ raise ArgumentError, ":at is no longer a valid option with text." +
423
+ "use draw_text or text_box instead"
424
+ end
425
+ process_final_gap_option(options)
426
+ process_indent_paragraphs_option(options)
427
+ options[:document] = self
428
+ end
429
+
430
+ def process_final_gap_option(options)
431
+ @final_gap = options[:final_gap].nil? || options[:final_gap]
432
+ options.delete(:final_gap)
433
+ end
434
+
435
+ def process_indent_paragraphs_option(options)
436
+ @indent_paragraphs = options[:indent_paragraphs]
437
+ options.delete(:indent_paragraphs)
438
+ end
439
+
440
+ def move_text_position(dy)
441
+ bottom = @bounding_box.stretchy? ? @margin_box.absolute_bottom :
442
+ @bounding_box.absolute_bottom
443
+
444
+ @bounding_box.move_past_bottom if (y - dy) < bottom
445
+
446
+ self.y -= dy
447
+ end
448
+ end
449
+ end