prawn 0.11.1 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (353) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +10 -0
  3. data/COPYING +2 -2
  4. data/GPLv2 +340 -0
  5. data/GPLv3 +674 -0
  6. data/Gemfile +11 -0
  7. data/LICENSE +1 -1
  8. data/Rakefile +29 -38
  9. data/data/images/16bit.alpha +0 -0
  10. data/data/images/16bit.color +0 -0
  11. data/data/images/dice.alpha +0 -0
  12. data/data/images/dice.color +0 -0
  13. data/data/images/indexed_color.dat +0 -0
  14. data/data/images/indexed_color.png +0 -0
  15. data/data/images/page_white_text.alpha +0 -0
  16. data/data/images/page_white_text.color +0 -0
  17. data/data/pdfs/nested_pages.pdf +13 -13
  18. data/lib/prawn/document/bounding_box.rb +87 -12
  19. data/lib/prawn/document/column_box.rb +57 -28
  20. data/lib/prawn/document/graphics_state.rb +11 -74
  21. data/lib/prawn/document/internals.rb +25 -23
  22. data/lib/prawn/document/snapshot.rb +11 -8
  23. data/lib/prawn/document/span.rb +12 -10
  24. data/lib/prawn/document.rb +250 -194
  25. data/lib/prawn/encoding.rb +9 -10
  26. data/lib/prawn/errors.rb +18 -29
  27. data/lib/prawn/font/afm.rb +52 -41
  28. data/lib/prawn/font/dfont.rb +4 -3
  29. data/lib/prawn/font/ttf.rb +44 -48
  30. data/lib/prawn/font.rb +138 -88
  31. data/lib/prawn/font_metric_cache.rb +47 -0
  32. data/lib/prawn/graphics/cap_style.rb +4 -3
  33. data/lib/prawn/graphics/color.rb +13 -5
  34. data/lib/prawn/graphics/dash.rb +53 -31
  35. data/lib/prawn/graphics/join_style.rb +9 -7
  36. data/lib/prawn/graphics/patterns.rb +138 -0
  37. data/lib/prawn/graphics/transformation.rb +10 -9
  38. data/lib/prawn/graphics/transparency.rb +3 -1
  39. data/lib/prawn/graphics.rb +316 -61
  40. data/lib/prawn/image_handler.rb +36 -0
  41. data/lib/prawn/images/image.rb +49 -0
  42. data/lib/prawn/images/jpg.rb +21 -15
  43. data/lib/prawn/images/png.rb +62 -119
  44. data/lib/prawn/images.rb +89 -108
  45. data/lib/prawn/layout/grid.rb +66 -54
  46. data/lib/prawn/layout.rb +10 -15
  47. data/lib/prawn/measurement_extensions.rb +10 -6
  48. data/lib/prawn/measurements.rb +27 -21
  49. data/lib/prawn/outline.rb +6 -308
  50. data/lib/prawn/repeater.rb +11 -9
  51. data/lib/prawn/security/arcfour.rb +1 -0
  52. data/lib/prawn/security.rb +55 -33
  53. data/lib/prawn/soft_mask.rb +96 -0
  54. data/lib/prawn/stamp.rb +5 -3
  55. data/lib/prawn/table/cell/image.rb +69 -0
  56. data/lib/prawn/table/cell/in_table.rb +4 -2
  57. data/lib/prawn/table/cell/span_dummy.rb +93 -0
  58. data/lib/prawn/table/cell/subtable.rb +2 -2
  59. data/lib/prawn/table/cell/text.rb +44 -26
  60. data/lib/prawn/table/cell.rb +302 -50
  61. data/lib/prawn/table/cells.rb +147 -49
  62. data/lib/prawn/table/column_width_calculator.rb +61 -0
  63. data/lib/prawn/table.rb +297 -118
  64. data/lib/prawn/text/box.rb +21 -5
  65. data/lib/prawn/text/formatted/arranger.rb +290 -0
  66. data/lib/prawn/text/formatted/box.rb +103 -59
  67. data/lib/prawn/text/formatted/fragment.rb +34 -23
  68. data/lib/prawn/text/formatted/line_wrap.rb +266 -0
  69. data/lib/prawn/text/formatted/parser.rb +15 -5
  70. data/lib/prawn/text/formatted/wrap.rb +150 -0
  71. data/lib/prawn/text/formatted.rb +5 -4
  72. data/lib/prawn/text.rb +38 -24
  73. data/lib/prawn/utilities.rb +46 -0
  74. data/lib/prawn.rb +85 -20
  75. data/manual/basic_concepts/adding_pages.rb +27 -0
  76. data/manual/basic_concepts/basic_concepts.rb +34 -0
  77. data/manual/basic_concepts/creation.rb +39 -0
  78. data/manual/basic_concepts/cursor.rb +33 -0
  79. data/manual/basic_concepts/measurement.rb +25 -0
  80. data/manual/basic_concepts/origin.rb +38 -0
  81. data/manual/basic_concepts/other_cursor_helpers.rb +40 -0
  82. data/manual/bounding_box/bounding_box.rb +39 -0
  83. data/manual/bounding_box/bounds.rb +49 -0
  84. data/manual/bounding_box/canvas.rb +24 -0
  85. data/manual/bounding_box/creation.rb +23 -0
  86. data/manual/bounding_box/indentation.rb +46 -0
  87. data/manual/bounding_box/nesting.rb +45 -0
  88. data/manual/bounding_box/russian_boxes.rb +40 -0
  89. data/manual/bounding_box/stretchy.rb +31 -0
  90. data/manual/document_and_page_options/background.rb +27 -0
  91. data/manual/document_and_page_options/document_and_page_options.rb +32 -0
  92. data/manual/document_and_page_options/metadata.rb +23 -0
  93. data/manual/document_and_page_options/page_margins.rb +38 -0
  94. data/manual/document_and_page_options/page_size.rb +34 -0
  95. data/manual/document_and_page_options/print_scaling.rb +20 -0
  96. data/manual/example_file.rb +111 -0
  97. data/manual/example_helper.rb +411 -0
  98. data/manual/example_package.rb +53 -0
  99. data/manual/example_section.rb +46 -0
  100. data/manual/graphics/circle_and_ellipse.rb +22 -0
  101. data/manual/graphics/color.rb +24 -0
  102. data/manual/graphics/common_lines.rb +30 -0
  103. data/manual/graphics/fill_and_stroke.rb +42 -0
  104. data/manual/graphics/fill_rules.rb +37 -0
  105. data/manual/graphics/gradients.rb +37 -0
  106. data/manual/graphics/graphics.rb +58 -0
  107. data/manual/graphics/helper.rb +24 -0
  108. data/manual/graphics/line_width.rb +35 -0
  109. data/manual/graphics/lines_and_curves.rb +41 -0
  110. data/manual/graphics/polygon.rb +29 -0
  111. data/manual/graphics/rectangle.rb +21 -0
  112. data/manual/graphics/rotate.rb +28 -0
  113. data/manual/graphics/scale.rb +41 -0
  114. data/manual/graphics/soft_masks.rb +46 -0
  115. data/manual/graphics/stroke_cap.rb +31 -0
  116. data/manual/graphics/stroke_dash.rb +48 -0
  117. data/manual/graphics/stroke_join.rb +30 -0
  118. data/manual/graphics/translate.rb +29 -0
  119. data/manual/graphics/transparency.rb +35 -0
  120. data/manual/images/absolute_position.rb +23 -0
  121. data/manual/images/fit.rb +21 -0
  122. data/manual/images/horizontal.rb +25 -0
  123. data/manual/images/images.rb +40 -0
  124. data/manual/images/plain_image.rb +18 -0
  125. data/manual/images/scale.rb +22 -0
  126. data/manual/images/vertical.rb +28 -0
  127. data/manual/images/width_and_height.rb +25 -0
  128. data/manual/layout/boxes.rb +27 -0
  129. data/manual/layout/content.rb +25 -0
  130. data/manual/layout/layout.rb +28 -0
  131. data/manual/layout/simple_grid.rb +23 -0
  132. data/manual/manual/cover.rb +36 -0
  133. data/manual/manual/foreword.rb +85 -0
  134. data/manual/manual/how_to_read_this_manual.rb +41 -0
  135. data/manual/manual/manual.rb +34 -0
  136. data/manual/outline/add_subsection_to.rb +61 -0
  137. data/manual/outline/insert_section_after.rb +47 -0
  138. data/manual/outline/outline.rb +32 -0
  139. data/manual/outline/sections_and_pages.rb +67 -0
  140. data/manual/repeatable_content/page_numbering.rb +54 -0
  141. data/manual/repeatable_content/repeatable_content.rb +31 -0
  142. data/manual/repeatable_content/repeater.rb +55 -0
  143. data/manual/repeatable_content/stamp.rb +41 -0
  144. data/manual/security/encryption.rb +31 -0
  145. data/manual/security/permissions.rb +38 -0
  146. data/manual/security/security.rb +28 -0
  147. data/manual/syntax_highlight.rb +52 -0
  148. data/manual/table/basic_block.rb +53 -0
  149. data/manual/table/before_rendering_page.rb +26 -0
  150. data/manual/table/cell_border_lines.rb +24 -0
  151. data/manual/table/cell_borders_and_bg.rb +31 -0
  152. data/manual/table/cell_dimensions.rb +30 -0
  153. data/manual/table/cell_text.rb +38 -0
  154. data/manual/table/column_widths.rb +30 -0
  155. data/manual/table/content_and_subtables.rb +39 -0
  156. data/manual/table/creation.rb +27 -0
  157. data/manual/table/filtering.rb +36 -0
  158. data/manual/table/flow_and_header.rb +17 -0
  159. data/manual/table/image_cells.rb +33 -0
  160. data/manual/table/position.rb +29 -0
  161. data/manual/table/row_colors.rb +20 -0
  162. data/manual/table/span.rb +30 -0
  163. data/manual/table/style.rb +22 -0
  164. data/manual/table/table.rb +52 -0
  165. data/manual/table/width.rb +27 -0
  166. data/manual/text/alignment.rb +44 -0
  167. data/manual/text/color.rb +24 -0
  168. data/manual/text/column_box.rb +32 -0
  169. data/manual/text/fallback_fonts.rb +37 -0
  170. data/manual/text/font.rb +41 -0
  171. data/manual/text/font_size.rb +45 -0
  172. data/manual/text/font_style.rb +23 -0
  173. data/manual/text/formatted_callbacks.rb +60 -0
  174. data/manual/text/formatted_text.rb +54 -0
  175. data/manual/text/free_flowing_text.rb +51 -0
  176. data/manual/text/group.rb +31 -0
  177. data/manual/text/inline.rb +43 -0
  178. data/manual/text/kerning_and_character_spacing.rb +39 -0
  179. data/manual/text/leading.rb +25 -0
  180. data/manual/text/line_wrapping.rb +41 -0
  181. data/manual/text/paragraph_indentation.rb +26 -0
  182. data/manual/text/positioned_text.rb +38 -0
  183. data/manual/text/registering_families.rb +48 -0
  184. data/manual/text/rendering_and_color.rb +37 -0
  185. data/manual/text/right_to_left_text.rb +43 -0
  186. data/manual/text/rotation.rb +43 -0
  187. data/manual/text/single_usage.rb +37 -0
  188. data/manual/text/text.rb +75 -0
  189. data/manual/text/text_box_excess.rb +32 -0
  190. data/manual/text/text_box_extensions.rb +45 -0
  191. data/manual/text/text_box_overflow.rb +44 -0
  192. data/manual/text/utf8.rb +28 -0
  193. data/{examples/m17n → manual/text}/win_ansi_charset.rb +14 -10
  194. data/prawn.gemspec +27 -17
  195. data/spec/acceptance/png.rb +23 -0
  196. data/spec/annotations_spec.rb +16 -32
  197. data/spec/bounding_box_spec.rb +284 -2
  198. data/spec/cell_spec.rb +169 -38
  199. data/spec/column_box_spec.rb +65 -0
  200. data/spec/data/curves.pdf +66 -0
  201. data/spec/destinations_spec.rb +5 -5
  202. data/spec/document_spec.rb +212 -113
  203. data/spec/extensions/encoding_helpers.rb +9 -0
  204. data/spec/extensions/mocha.rb +2 -3
  205. data/spec/font_metric_cache_spec.rb +52 -0
  206. data/spec/font_spec.rb +205 -95
  207. data/spec/formatted_text_arranger_spec.rb +43 -43
  208. data/spec/formatted_text_box_spec.rb +63 -24
  209. data/spec/formatted_text_fragment_spec.rb +8 -8
  210. data/spec/graphics_spec.rb +175 -68
  211. data/spec/grid_spec.rb +26 -15
  212. data/spec/image_handler_spec.rb +54 -0
  213. data/spec/images_spec.rb +58 -30
  214. data/spec/inline_formatted_text_parser_spec.rb +73 -19
  215. data/spec/jpg_spec.rb +4 -4
  216. data/spec/line_wrap_spec.rb +28 -28
  217. data/spec/measurement_units_spec.rb +6 -6
  218. data/spec/object_store_spec.rb +17 -106
  219. data/spec/outline_spec.rb +103 -63
  220. data/spec/png_spec.rb +25 -25
  221. data/spec/reference_spec.rb +8 -65
  222. data/spec/repeater_spec.rb +25 -11
  223. data/spec/security_spec.rb +44 -12
  224. data/spec/snapshot_spec.rb +38 -6
  225. data/spec/soft_mask_spec.rb +117 -0
  226. data/spec/span_spec.rb +10 -15
  227. data/spec/spec_helper.rb +32 -8
  228. data/spec/stamp_spec.rb +29 -30
  229. data/spec/stroke_styles_spec.rb +36 -18
  230. data/spec/table/span_dummy_spec.rb +17 -0
  231. data/spec/table_spec.rb +850 -104
  232. data/spec/text_at_spec.rb +19 -33
  233. data/spec/text_box_spec.rb +117 -64
  234. data/spec/text_rendering_mode_spec.rb +5 -5
  235. data/spec/text_spacing_spec.rb +20 -2
  236. data/spec/text_spec.rb +111 -59
  237. data/spec/transparency_spec.rb +5 -5
  238. metadata +477 -328
  239. data/HACKING +0 -50
  240. data/README +0 -141
  241. data/data/fonts/Action Man.dfont +0 -0
  242. data/data/fonts/Activa.ttf +0 -0
  243. data/data/fonts/Chalkboard.ttf +0 -0
  244. data/data/fonts/DejaVuSans.ttf +0 -0
  245. data/data/fonts/Dustismo_Roman.ttf +0 -0
  246. data/data/fonts/comicsans.ttf +0 -0
  247. data/data/fonts/gkai00mp.ttf +0 -0
  248. data/data/images/16bit.dat +0 -0
  249. data/data/images/dice.dat +0 -0
  250. data/data/images/page_white_text.dat +0 -0
  251. data/data/images/rails.dat +0 -0
  252. data/data/images/rails.png +0 -0
  253. data/examples/bounding_box/bounding_boxes.rb +0 -44
  254. data/examples/bounding_box/indentation.rb +0 -35
  255. data/examples/bounding_box/russian_boxes.rb +0 -37
  256. data/examples/bounding_box/stretched_nesting.rb +0 -68
  257. data/examples/example_helper.rb +0 -8
  258. data/examples/general/background.rb +0 -24
  259. data/examples/general/canvas.rb +0 -16
  260. data/examples/general/context_sensitive_headers.rb +0 -38
  261. data/examples/general/float.rb +0 -12
  262. data/examples/general/margin.rb +0 -37
  263. data/examples/general/measurement_units.rb +0 -52
  264. data/examples/general/metadata-info.rb +0 -17
  265. data/examples/general/multi_page_layout.rb +0 -19
  266. data/examples/general/outlines.rb +0 -67
  267. data/examples/general/page_geometry.rb +0 -32
  268. data/examples/general/page_numbering.rb +0 -40
  269. data/examples/general/page_templates.rb +0 -20
  270. data/examples/general/repeaters.rb +0 -48
  271. data/examples/general/stamp.rb +0 -42
  272. data/examples/general/templates.rb +0 -14
  273. data/examples/graphics/basic_images.rb +0 -24
  274. data/examples/graphics/cmyk.rb +0 -13
  275. data/examples/graphics/curves.rb +0 -12
  276. data/examples/graphics/gradient.rb +0 -23
  277. data/examples/graphics/hexagon.rb +0 -14
  278. data/examples/graphics/image_fit.rb +0 -16
  279. data/examples/graphics/image_flow.rb +0 -38
  280. data/examples/graphics/image_position.rb +0 -18
  281. data/examples/graphics/line.rb +0 -33
  282. data/examples/graphics/png_types.rb +0 -23
  283. data/examples/graphics/polygons.rb +0 -17
  284. data/examples/graphics/remote_images.rb +0 -13
  285. data/examples/graphics/rounded_polygons.rb +0 -20
  286. data/examples/graphics/rounded_rectangle.rb +0 -21
  287. data/examples/graphics/ruport_style_helpers.rb +0 -20
  288. data/examples/graphics/stroke_bounds.rb +0 -21
  289. data/examples/graphics/stroke_cap_and_join.rb +0 -46
  290. data/examples/graphics/stroke_dash.rb +0 -43
  291. data/examples/graphics/transformations.rb +0 -53
  292. data/examples/graphics/transparency.rb +0 -27
  293. data/examples/grid/bounding_boxes.rb +0 -22
  294. data/examples/grid/column_gutter_grid.rb +0 -21
  295. data/examples/grid/multi_boxes.rb +0 -52
  296. data/examples/grid/show_grid.rb +0 -14
  297. data/examples/grid/simple_grid.rb +0 -21
  298. data/examples/m17n/chinese_text_wrapping.rb +0 -18
  299. data/examples/m17n/euro.rb +0 -16
  300. data/examples/m17n/full_win_ansi_character_list.rb +0 -20
  301. data/examples/m17n/sjis.rb +0 -29
  302. data/examples/m17n/utf8.rb +0 -14
  303. data/examples/security/hello_foo.rb +0 -9
  304. data/examples/table/bill.rb +0 -54
  305. data/examples/table/borders.rb +0 -25
  306. data/examples/table/cell.rb +0 -13
  307. data/examples/table/checkerboard.rb +0 -23
  308. data/examples/table/header.rb +0 -15
  309. data/examples/table/inline_format_table.rb +0 -13
  310. data/examples/table/multi_page_table.rb +0 -10
  311. data/examples/table/simple_table.rb +0 -25
  312. data/examples/table/subtable.rb +0 -13
  313. data/examples/table/widths.rb +0 -21
  314. data/examples/text/alignment.rb +0 -19
  315. data/examples/text/character_spacing.rb +0 -13
  316. data/examples/text/dfont.rb +0 -49
  317. data/examples/text/family_based_styling.rb +0 -25
  318. data/examples/text/font_calculations.rb +0 -92
  319. data/examples/text/font_size.rb +0 -34
  320. data/examples/text/hyphenation.rb +0 -45
  321. data/examples/text/indent_paragraphs.rb +0 -24
  322. data/examples/text/inline_format.rb +0 -104
  323. data/examples/text/kerning.rb +0 -31
  324. data/examples/text/rendering_mode.rb +0 -21
  325. data/examples/text/rotated.rb +0 -99
  326. data/examples/text/shaped_text_box.rb +0 -32
  327. data/examples/text/simple_text.rb +0 -18
  328. data/examples/text/simple_text_ttf.rb +0 -18
  329. data/examples/text/span.rb +0 -30
  330. data/examples/text/text_box.rb +0 -90
  331. data/examples/text/text_box_returning_excess.rb +0 -52
  332. data/examples/text/text_flow.rb +0 -68
  333. data/lib/prawn/compatibility.rb +0 -51
  334. data/lib/prawn/core/annotations.rb +0 -61
  335. data/lib/prawn/core/byte_string.rb +0 -9
  336. data/lib/prawn/core/destinations.rb +0 -90
  337. data/lib/prawn/core/document_state.rb +0 -78
  338. data/lib/prawn/core/literal_string.rb +0 -16
  339. data/lib/prawn/core/name_tree.rb +0 -165
  340. data/lib/prawn/core/object_store.rb +0 -264
  341. data/lib/prawn/core/page.rb +0 -213
  342. data/lib/prawn/core/pdf_object.rb +0 -108
  343. data/lib/prawn/core/reference.rb +0 -112
  344. data/lib/prawn/core/text/formatted/arranger.rb +0 -293
  345. data/lib/prawn/core/text/formatted/line_wrap.rb +0 -272
  346. data/lib/prawn/core/text/formatted/wrap.rb +0 -149
  347. data/lib/prawn/core/text.rb +0 -268
  348. data/lib/prawn/core.rb +0 -85
  349. data/lib/prawn/document/page_geometry.rb +0 -136
  350. data/lib/prawn/graphics/gradient.rb +0 -84
  351. data/spec/name_tree_spec.rb +0 -112
  352. data/spec/pdf_object_spec.rb +0 -170
  353. data/spec/template_spec.rb +0 -291
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # encoding: utf-8
2
2
 
3
3
  # cell.rb: Table cell drawing.
4
4
  #
@@ -6,10 +6,13 @@
6
6
  #
7
7
  # This is free software. Please see the LICENSE and COPYING files for details.
8
8
 
9
+ require 'date'
9
10
  module Prawn
10
11
  class Document
11
12
 
12
- # Instantiates and draws a cell on the document.
13
+ # @group Experimental API
14
+
15
+ # Instantiates and draws a cell on the document.
13
16
  #
14
17
  # cell(:content => "Hello world!", :at => [12, 34])
15
18
  #
@@ -20,9 +23,9 @@ module Prawn
20
23
  cell.draw
21
24
  cell
22
25
  end
23
-
24
- # Set up, but do not draw, a cell. Useful for creating cells with
25
- # formatting options to be inserted into a Table. Call +draw+ on the
26
+
27
+ # Set up, but do not draw, a cell. Useful for creating cells with
28
+ # formatting options to be inserted into a Table. Call +draw+ on the
26
29
  # resulting Cell to ink it.
27
30
  #
28
31
  # See the documentation on Prawn::Cell for details on the arguments.
@@ -34,7 +37,7 @@ module Prawn
34
37
  end
35
38
 
36
39
  class Table
37
-
40
+
38
41
  # A Cell is a rectangular area of the page into which content is drawn. It
39
42
  # has a framework for sizing itself and adding padding and simple styling.
40
43
  # There are several standard Cell subclasses that handle things like text,
@@ -53,20 +56,58 @@ module Prawn
53
56
  #
54
57
  attr_reader :padding
55
58
 
56
- # If provided, the minimum width that this cell will permit.
57
- #
58
- def min_width
59
+ # If provided, the minimum width that this cell in its column will permit.
60
+ #
61
+ def min_width_ignoring_span
59
62
  set_width_constraints
60
63
  @min_width
61
64
  end
62
-
63
- # If provided, the maximum width that this cell can be drawn in.
65
+
66
+ # Minimum width of the entire span group this cell controls.
64
67
  #
65
- def max_width
68
+ def min_width
69
+ return min_width_ignoring_span if @colspan == 1
70
+
71
+ # Sum up the largest min-width from each column, including myself.
72
+ min_widths = Hash.new(0)
73
+ dummy_cells.each do |cell|
74
+ min_widths[cell.column] =
75
+ [min_widths[cell.column], cell.min_width].max
76
+ end
77
+ min_widths[column] = [min_widths[column], min_width_ignoring_span].max
78
+ min_widths.values.inject(0, &:+)
79
+ end
80
+
81
+ # Min-width of the span divided by the number of columns.
82
+ #
83
+ def avg_spanned_min_width
84
+ min_width.to_f / colspan
85
+ end
86
+
87
+ # If provided, the maximum width that this cell can be drawn in, within
88
+ # its column.
89
+ #
90
+ def max_width_ignoring_span
66
91
  set_width_constraints
67
92
  @max_width
68
93
  end
69
94
 
95
+ # Maximum width of the entire span group this cell controls.
96
+ #
97
+ def max_width
98
+ return max_width_ignoring_span if @colspan == 1
99
+
100
+ # Sum the smallest max-width from each column in the group, including
101
+ # myself.
102
+ max_widths = Hash.new(0)
103
+ dummy_cells.each do |cell|
104
+ max_widths[cell.column] =
105
+ [max_widths[cell.column], cell.max_width].min
106
+ end
107
+ max_widths[column] = [max_widths[column], max_width_ignoring_span].min
108
+ max_widths.values.inject(0, &:+)
109
+ end
110
+
70
111
  # Manually specify the cell's height.
71
112
  #
72
113
  attr_writer :height
@@ -84,11 +125,15 @@ module Prawn
84
125
  #
85
126
  attr_reader :border_colors
86
127
 
128
+ # Line style
129
+ #
130
+ attr_reader :border_lines
131
+
87
132
  # Specifies the content for the cell. Must be a "cellable" object. See the
88
133
  # "Data" section of the Prawn::Table documentation for details on cellable
89
134
  # objects.
90
135
  #
91
- attr_accessor :content
136
+ attr_accessor :content
92
137
 
93
138
  # The background color, if any, for this cell. Specified in HTML RGB
94
139
  # format, e.g., "ccffff". The background is drawn under the whole cell,
@@ -96,6 +141,21 @@ module Prawn
96
141
  #
97
142
  attr_accessor :background_color
98
143
 
144
+ # Number of columns this cell spans. Defaults to 1.
145
+ #
146
+ attr_reader :colspan
147
+
148
+ # Number of rows this cell spans. Defaults to 1.
149
+ #
150
+ attr_reader :rowspan
151
+
152
+ # Array of SpanDummy cells (if any) that represent the other cells in
153
+ # this span group. They know their own width / height, but do not draw
154
+ # anything.
155
+ #
156
+ # @private
157
+ attr_reader :dummy_cells
158
+
99
159
  # Instantiates a Cell based on the given options. The particular class of
100
160
  # cell returned depends on the :content argument. See the Prawn::Table
101
161
  # documentation under "Data" for allowable content types.
@@ -104,14 +164,19 @@ module Prawn
104
164
  at = options.delete(:at) || [0, pdf.cursor]
105
165
  content = content.to_s if content.nil? || content.kind_of?(Numeric) ||
106
166
  content.kind_of?(Date)
107
-
167
+
108
168
  if content.is_a?(Hash)
169
+ if content[:image]
170
+ return Cell::Image.new(pdf, at, content)
171
+ end
109
172
  options.update(content)
110
173
  content = options[:content]
111
174
  else
112
175
  options[:content] = content
113
176
  end
114
177
 
178
+ options[:content] = content = "" if content.nil?
179
+
115
180
  case content
116
181
  when Prawn::Table::Cell
117
182
  content
@@ -123,8 +188,7 @@ module Prawn
123
188
  subtable = Prawn::Table.new(options[:content], pdf, {})
124
189
  Cell::Subtable.new(pdf, at, options.merge(:content => subtable))
125
190
  else
126
- # TODO: other types of content
127
- raise ArgumentError, "Content type not recognized: #{content.inspect}"
191
+ raise Errors::UnrecognizedTableContent
128
192
  end
129
193
  end
130
194
 
@@ -151,8 +215,14 @@ module Prawn
151
215
  @borders = [:top, :bottom, :left, :right]
152
216
  @border_widths = [1] * 4
153
217
  @border_colors = ['000000'] * 4
218
+ @border_lines = [:solid] * 4
219
+ @colspan = 1
220
+ @rowspan = 1
221
+ @dummy_cells = []
154
222
 
155
223
  options.each { |k, v| send("#{k}=", v) }
224
+
225
+ @initializer_run = true
156
226
  end
157
227
 
158
228
  # Supports setting multiple properties at once.
@@ -165,19 +235,40 @@ module Prawn
165
235
  # cell.border_width = 2
166
236
  #
167
237
  def style(options={}, &block)
168
- options.each { |k, v| send("#{k}=", v) }
238
+ options.each do |k, v|
239
+ send("#{k}=", v) if respond_to?("#{k}=")
240
+ end
169
241
 
170
242
  # The block form supports running a single block for multiple cells, as
171
243
  # in Cells#style.
172
244
  block.call(self) if block
173
245
  end
174
246
 
175
- # Returns the cell's width in points, inclusive of padding.
247
+ # Returns the width of the cell in its first column alone, ignoring any
248
+ # colspans.
176
249
  #
177
- def width
250
+ def width_ignoring_span
178
251
  # We can't ||= here because the FP error accumulates on the round-trip
179
252
  # from #content_width.
180
- @width || (content_width + padding_left + padding_right)
253
+ defined?(@width) && @width || (content_width + padding_left + padding_right)
254
+ end
255
+
256
+ # Returns the cell's width in points, inclusive of padding. If the cell is
257
+ # the master cell of a colspan, returns the width of the entire span
258
+ # group.
259
+ #
260
+ def width
261
+ return width_ignoring_span if @colspan == 1 && @rowspan == 1
262
+
263
+ # We're in a span group; get the maximum width per column (including
264
+ # the master cell) and sum each column.
265
+ column_widths = Hash.new(0)
266
+ dummy_cells.each do |cell|
267
+ column_widths[cell.column] =
268
+ [column_widths[cell.column], cell.width].max
269
+ end
270
+ column_widths[column] = [column_widths[column], width_ignoring_span].max
271
+ column_widths.values.inject(0, &:+)
181
272
  end
182
273
 
183
274
  # Manually sets the cell's width, inclusive of padding.
@@ -189,59 +280,156 @@ module Prawn
189
280
  # Returns the width of the bare content in the cell, excluding padding.
190
281
  #
191
282
  def content_width
192
- if @width # manually set
283
+ if defined?(@width) && @width # manually set
193
284
  return @width - padding_left - padding_right
194
285
  end
195
286
 
196
287
  natural_content_width
197
288
  end
198
289
 
290
+ # Width of the entire span group.
291
+ #
292
+ def spanned_content_width
293
+ width - padding_left - padding_right
294
+ end
295
+
199
296
  # Returns the width this cell would naturally take on, absent other
200
297
  # constraints. Must be implemented in subclasses.
201
298
  #
202
299
  def natural_content_width
203
- raise NotImplementedError,
300
+ raise NotImplementedError,
204
301
  "subclasses must implement natural_content_width"
205
302
  end
206
303
 
207
- # Returns the cell's height in points, inclusive of padding.
304
+ # Returns the cell's height in points, inclusive of padding, in its first
305
+ # row only.
208
306
  #
209
- def height
307
+ def height_ignoring_span
210
308
  # We can't ||= here because the FP error accumulates on the round-trip
211
309
  # from #content_height.
212
- @height || (content_height + padding_top + padding_bottom)
310
+ defined?(@height) && @height || (content_height + padding_top + padding_bottom)
311
+ end
312
+
313
+ # Returns the cell's height in points, inclusive of padding. If the cell
314
+ # is the master cell of a rowspan, returns the width of the entire span
315
+ # group.
316
+ #
317
+ def height
318
+ return height_ignoring_span if @colspan == 1 && @rowspan == 1
319
+
320
+ # We're in a span group; get the maximum height per row (including the
321
+ # master cell) and sum each row.
322
+ row_heights = Hash.new(0)
323
+ dummy_cells.each do |cell|
324
+ row_heights[cell.row] = [row_heights[cell.row], cell.height].max
325
+ end
326
+ row_heights[row] = [row_heights[row], height_ignoring_span].max
327
+ row_heights.values.inject(0, &:+)
213
328
  end
214
329
 
215
330
  # Returns the height of the bare content in the cell, excluding padding.
216
331
  #
217
332
  def content_height
218
- if @height # manually set
333
+ if defined?(@height) && @height # manually set
219
334
  return @height - padding_top - padding_bottom
220
335
  end
221
-
336
+
222
337
  natural_content_height
223
338
  end
224
339
 
340
+ # Height of the entire span group.
341
+ #
342
+ def spanned_content_height
343
+ height - padding_top - padding_bottom
344
+ end
345
+
225
346
  # Returns the height this cell would naturally take on, absent
226
347
  # constraints. Must be implemented in subclasses.
227
348
  #
228
349
  def natural_content_height
229
- raise NotImplementedError,
350
+ raise NotImplementedError,
230
351
  "subclasses must implement natural_content_height"
231
352
  end
232
353
 
354
+ # Indicates the number of columns that this cell is to span. Defaults to
355
+ # 1.
356
+ #
357
+ # This must be provided as part of the table data, like so:
358
+ #
359
+ # pdf.table([["foo", {:content => "bar", :colspan => 2}]])
360
+ #
361
+ # Setting colspan from the initializer block is invalid because layout
362
+ # has already run. For example, this will NOT work:
363
+ #
364
+ # pdf.table([["foo", "bar"]]) { cells[0, 1].colspan = 2 }
365
+ #
366
+ def colspan=(span)
367
+ if defined?(@initializer_run) && @initializer_run
368
+ raise Prawn::Errors::InvalidTableSpan,
369
+ "colspan must be provided in the table's structure, never in the " +
370
+ "initialization block. See Prawn's documentation for details."
371
+ end
372
+
373
+ @colspan = span
374
+ end
375
+
376
+ # Indicates the number of rows that this cell is to span. Defaults to 1.
377
+ #
378
+ # This must be provided as part of the table data, like so:
379
+ #
380
+ # pdf.table([["foo", {:content => "bar", :rowspan => 2}], ["baz"]])
381
+ #
382
+ # Setting rowspan from the initializer block is invalid because layout
383
+ # has already run. For example, this will NOT work:
384
+ #
385
+ # pdf.table([["foo", "bar"], ["baz"]]) { cells[0, 1].rowspan = 2 }
386
+ #
387
+ def rowspan=(span)
388
+ if defined?(@initializer_run) && @initializer_run
389
+ raise Prawn::Errors::InvalidTableSpan,
390
+ "rowspan must be provided in the table's structure, never in the " +
391
+ "initialization block. See Prawn's documentation for details."
392
+ end
393
+
394
+ @rowspan = span
395
+ end
396
+
233
397
  # Draws the cell onto the document. Pass in a point [x,y] to override the
234
398
  # location at which the cell is drawn.
235
399
  #
400
+ # If drawing a group of cells at known positions, look into
401
+ # Cell.draw_cells, which ensures that the backgrounds, borders, and
402
+ # content are all drawn in correct order so as not to overlap.
403
+ #
236
404
  def draw(pt=[x, y])
237
- set_width_constraints
405
+ Prawn::Table::Cell.draw_cells([[self, pt]])
406
+ end
238
407
 
239
- draw_background(pt)
240
- draw_borders(pt)
241
- @pdf.bounding_box([pt[0] + padding_left, pt[1] - padding_top],
242
- :width => content_width + FPTolerance,
243
- :height => content_height + FPTolerance) do
244
- draw_content
408
+ # Given an array of pairs [cell, pt], draws each cell at its
409
+ # corresponding pt, making sure all backgrounds are behind all borders
410
+ # and content.
411
+ #
412
+ def self.draw_cells(cells)
413
+ cells.each do |cell, pt|
414
+ cell.set_width_constraints
415
+ cell.draw_background(pt)
416
+ end
417
+
418
+ cells.each do |cell, pt|
419
+ cell.draw_borders(pt)
420
+ cell.draw_bounded_content(pt)
421
+ end
422
+ end
423
+
424
+ # Draws the cell's content at the point provided.
425
+ #
426
+ def draw_bounded_content(pt)
427
+ @pdf.float do
428
+ @pdf.bounding_box([pt[0] + padding_left, pt[1] - padding_top],
429
+ :width => spanned_content_width + FPTolerance,
430
+ :height => spanned_content_height + FPTolerance) do
431
+ draw_content
432
+ end
245
433
  end
246
434
  end
247
435
 
@@ -297,7 +485,7 @@ module Prawn
297
485
  def padding_top
298
486
  @padding[0]
299
487
  end
300
-
488
+
301
489
  def padding_top=(val)
302
490
  @padding[0] = val
303
491
  end
@@ -305,7 +493,7 @@ module Prawn
305
493
  def padding_right
306
494
  @padding[1]
307
495
  end
308
-
496
+
309
497
  def padding_right=(val)
310
498
  @padding[1] = val
311
499
  end
@@ -313,7 +501,7 @@ module Prawn
313
501
  def padding_bottom
314
502
  @padding[2]
315
503
  end
316
-
504
+
317
505
  def padding_bottom=(val)
318
506
  @padding[2] = val
319
507
  end
@@ -321,7 +509,7 @@ module Prawn
321
509
  def padding_left
322
510
  @padding[3]
323
511
  end
324
-
512
+
325
513
  def padding_left=(val)
326
514
  @padding[3] = val
327
515
  end
@@ -360,14 +548,6 @@ module Prawn
360
548
  @border_colors[0] = val
361
549
  end
362
550
 
363
- def border_top_color
364
- @border_colors[0]
365
- end
366
-
367
- def border_top_color=(val)
368
- @border_colors[0] = val
369
- end
370
-
371
551
  def border_right_color
372
552
  @border_colors[1]
373
553
  end
@@ -450,8 +630,6 @@ module Prawn
450
630
  @border_widths[3] = val
451
631
  end
452
632
 
453
- protected
454
-
455
633
  # Sets the cell's minimum and maximum width. Deferred until requested
456
634
  # because padding and size can change.
457
635
  #
@@ -460,10 +638,70 @@ module Prawn
460
638
  @max_width ||= @pdf.bounds.width
461
639
  end
462
640
 
641
+ # Sets border line style on this cell. The argument can be one of:
642
+ #
643
+ # Possible values are: :solid, :dashed, :dotted
644
+ #
645
+ # * one value (sets all lines)
646
+ # * a two-element array [vertical, horizontal]
647
+ # * a three-element array [top, horizontal, bottom]
648
+ # * a four-element array [top, right, bottom, left]
649
+ #
650
+ def border_line=(line)
651
+ @border_lines = case
652
+ when line.nil?
653
+ [:solid] * 4
654
+ when line.length == 1 # all lines
655
+ [line[0]] * 4
656
+ when line.length == 2
657
+ [line[0], line[1], line[0], line[1]]
658
+ when line.length == 3
659
+ [line[0], line[1], line[2], line[1]]
660
+ when line.length == 4
661
+ [line[0], line[1], line[2], line[3]]
662
+ else
663
+ raise ArgumentError, "border_line must be one of :solid, :dashed, "
664
+ ":dotted or an array [v,h] or [t,r,b,l]"
665
+ end
666
+ end
667
+ alias_method :border_lines=, :border_line=
668
+
669
+ def border_top_line
670
+ @borders.include?(:top) ? @border_lines[0] : 0
671
+ end
672
+
673
+ def border_top_line=(val)
674
+ @border_lines[0] = val
675
+ end
676
+
677
+ def border_right_line
678
+ @borders.include?(:right) ? @border_lines[1] : 0
679
+ end
680
+
681
+ def border_right_line=(val)
682
+ @border_lines[1] = val
683
+ end
684
+
685
+ def border_bottom_line
686
+ @borders.include?(:bottom) ? @border_lines[2] : 0
687
+ end
688
+
689
+ def border_bottom_line=(val)
690
+ @border_lines[2] = val
691
+ end
692
+
693
+ def border_left_line
694
+ @borders.include?(:left) ? @border_lines[3] : 0
695
+ end
696
+
697
+ def border_left_line=(val)
698
+ @border_lines[3] = val
699
+ end
700
+
463
701
  # Draws the cell's background color.
464
702
  #
465
703
  def draw_background(pt)
466
- if @background_color
704
+ if defined?(@background_color) && @background_color
467
705
  @pdf.mask(:fill_color) do
468
706
  @pdf.fill_color @background_color
469
707
  @pdf.fill_rectangle pt, width, height
@@ -484,6 +722,7 @@ module Prawn
484
722
  idx = {:top => 0, :right => 1, :bottom => 2, :left => 3}[border]
485
723
  border_color = @border_colors[idx]
486
724
  border_width = @border_widths[idx]
725
+ border_line = @border_lines[idx]
487
726
 
488
727
  next if border_width <= 0
489
728
 
@@ -502,9 +741,22 @@ module Prawn
502
741
  [x+width, y - height - (border_bottom_width / 2.0)]]
503
742
  end
504
743
 
744
+ case border_line
745
+ when :dashed
746
+ @pdf.dash border_width * 4
747
+ when :dotted
748
+ @pdf.dash border_width, :space => border_width * 2
749
+ when :solid
750
+ # normal line style
751
+ else
752
+ raise ArgumentError, "border_line must be :solid, :dotted or" +
753
+ " :dashed"
754
+ end
755
+
505
756
  @pdf.line_width = border_width
506
757
  @pdf.stroke_color = border_color
507
758
  @pdf.stroke_line(from, to)
759
+ @pdf.undash
508
760
  end
509
761
  end
510
762
  end