nurettin-prawn 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (303) hide show
  1. data/COPYING +2 -0
  2. data/GPLv2 +340 -0
  3. data/GPLv3 +674 -0
  4. data/Gemfile +18 -0
  5. data/LICENSE +56 -0
  6. data/README.md +98 -0
  7. data/Rakefile +45 -0
  8. data/data/encodings/win_ansi.txt +29 -0
  9. data/data/fonts/Action Man.dfont +0 -0
  10. data/data/fonts/Activa.ttf +0 -0
  11. data/data/fonts/Chalkboard.ttf +0 -0
  12. data/data/fonts/Courier-Bold.afm +342 -0
  13. data/data/fonts/Courier-BoldOblique.afm +342 -0
  14. data/data/fonts/Courier-Oblique.afm +342 -0
  15. data/data/fonts/Courier.afm +342 -0
  16. data/data/fonts/DejaVuSans.ttf +0 -0
  17. data/data/fonts/Dustismo_Roman.ttf +0 -0
  18. data/data/fonts/Helvetica-Bold.afm +2827 -0
  19. data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
  20. data/data/fonts/Helvetica-Oblique.afm +3051 -0
  21. data/data/fonts/Helvetica.afm +3051 -0
  22. data/data/fonts/MustRead.html +19 -0
  23. data/data/fonts/Symbol.afm +213 -0
  24. data/data/fonts/Times-Bold.afm +2588 -0
  25. data/data/fonts/Times-BoldItalic.afm +2384 -0
  26. data/data/fonts/Times-Italic.afm +2667 -0
  27. data/data/fonts/Times-Roman.afm +2419 -0
  28. data/data/fonts/ZapfDingbats.afm +225 -0
  29. data/data/fonts/comicsans.ttf +0 -0
  30. data/data/fonts/gkai00mp.ttf +0 -0
  31. data/data/images/16bit.alpha +0 -0
  32. data/data/images/16bit.dat +0 -0
  33. data/data/images/16bit.png +0 -0
  34. data/data/images/arrow.png +0 -0
  35. data/data/images/arrow2.png +0 -0
  36. data/data/images/barcode_issue.png +0 -0
  37. data/data/images/dice.alpha +0 -0
  38. data/data/images/dice.dat +0 -0
  39. data/data/images/dice.png +0 -0
  40. data/data/images/dice_interlaced.png +0 -0
  41. data/data/images/fractal.jpg +0 -0
  42. data/data/images/letterhead.jpg +0 -0
  43. data/data/images/page_white_text.alpha +0 -0
  44. data/data/images/page_white_text.dat +0 -0
  45. data/data/images/page_white_text.png +0 -0
  46. data/data/images/pigs.jpg +0 -0
  47. data/data/images/prawn.png +0 -0
  48. data/data/images/rails.dat +0 -0
  49. data/data/images/rails.png +0 -0
  50. data/data/images/ruport.png +0 -0
  51. data/data/images/ruport_data.dat +0 -0
  52. data/data/images/ruport_transparent.png +0 -0
  53. data/data/images/ruport_type0.png +0 -0
  54. data/data/images/stef.jpg +0 -0
  55. data/data/images/tru256.bmp +0 -0
  56. data/data/images/web-links.dat +1 -0
  57. data/data/images/web-links.png +0 -0
  58. data/data/pdfs/complex_template.pdf +0 -0
  59. data/data/pdfs/contains_ttf_font.pdf +0 -0
  60. data/data/pdfs/encrypted.pdf +0 -0
  61. data/data/pdfs/form.pdf +820 -0
  62. data/data/pdfs/hexagon.pdf +61 -0
  63. data/data/pdfs/indirect_reference.pdf +86 -0
  64. data/data/pdfs/multipage_template.pdf +127 -0
  65. data/data/pdfs/nested_pages.pdf +118 -0
  66. data/data/pdfs/page_without_mediabox.pdf +193 -0
  67. data/data/pdfs/resources_as_indirect_object.pdf +83 -0
  68. data/data/pdfs/two_hexagons.pdf +90 -0
  69. data/data/pdfs/version_1_6.pdf +61 -0
  70. data/data/shift_jis_text.txt +1 -0
  71. data/lib/prawn.rb +29 -0
  72. data/lib/prawn/compatibility.rb +87 -0
  73. data/lib/prawn/core.rb +87 -0
  74. data/lib/prawn/core/annotations.rb +61 -0
  75. data/lib/prawn/core/byte_string.rb +9 -0
  76. data/lib/prawn/core/destinations.rb +90 -0
  77. data/lib/prawn/core/document_state.rb +79 -0
  78. data/lib/prawn/core/literal_string.rb +16 -0
  79. data/lib/prawn/core/name_tree.rb +177 -0
  80. data/lib/prawn/core/object_store.rb +320 -0
  81. data/lib/prawn/core/page.rb +212 -0
  82. data/lib/prawn/core/pdf_object.rb +125 -0
  83. data/lib/prawn/core/reference.rb +119 -0
  84. data/lib/prawn/core/text.rb +268 -0
  85. data/lib/prawn/core/text/formatted/arranger.rb +294 -0
  86. data/lib/prawn/core/text/formatted/line_wrap.rb +288 -0
  87. data/lib/prawn/core/text/formatted/wrap.rb +153 -0
  88. data/lib/prawn/document.rb +707 -0
  89. data/lib/prawn/document/bounding_box.rb +510 -0
  90. data/lib/prawn/document/column_box.rb +132 -0
  91. data/lib/prawn/document/graphics_state.rb +136 -0
  92. data/lib/prawn/document/internals.rb +173 -0
  93. data/lib/prawn/document/page_geometry.rb +136 -0
  94. data/lib/prawn/document/snapshot.rb +89 -0
  95. data/lib/prawn/document/span.rb +55 -0
  96. data/lib/prawn/encoding.rb +121 -0
  97. data/lib/prawn/errors.rb +99 -0
  98. data/lib/prawn/font.rb +386 -0
  99. data/lib/prawn/font/afm.rb +203 -0
  100. data/lib/prawn/font/dfont.rb +42 -0
  101. data/lib/prawn/font/ttf.rb +343 -0
  102. data/lib/prawn/graphics.rb +523 -0
  103. data/lib/prawn/graphics/cap_style.rb +46 -0
  104. data/lib/prawn/graphics/color.rb +231 -0
  105. data/lib/prawn/graphics/dash.rb +82 -0
  106. data/lib/prawn/graphics/join_style.rb +47 -0
  107. data/lib/prawn/graphics/patterns.rb +137 -0
  108. data/lib/prawn/graphics/transformation.rb +156 -0
  109. data/lib/prawn/graphics/transparency.rb +99 -0
  110. data/lib/prawn/images.rb +196 -0
  111. data/lib/prawn/images/image.rb +65 -0
  112. data/lib/prawn/images/jpg.rb +85 -0
  113. data/lib/prawn/images/png.rb +362 -0
  114. data/lib/prawn/layout.rb +20 -0
  115. data/lib/prawn/layout/grid.rb +259 -0
  116. data/lib/prawn/measurement_extensions.rb +46 -0
  117. data/lib/prawn/measurements.rb +71 -0
  118. data/lib/prawn/outline.rb +326 -0
  119. data/lib/prawn/repeater.rb +122 -0
  120. data/lib/prawn/security.rb +269 -0
  121. data/lib/prawn/soft_mask.rb +94 -0
  122. data/lib/prawn/stamp.rb +134 -0
  123. data/lib/prawn/table.rb +609 -0
  124. data/lib/prawn/table/cell.rb +776 -0
  125. data/lib/prawn/table/cell/image.rb +70 -0
  126. data/lib/prawn/table/cell/in_table.rb +27 -0
  127. data/lib/prawn/table/cell/span_dummy.rb +88 -0
  128. data/lib/prawn/table/cell/subtable.rb +65 -0
  129. data/lib/prawn/table/cell/text.rb +152 -0
  130. data/lib/prawn/table/cells.rb +260 -0
  131. data/lib/prawn/text.rb +420 -0
  132. data/lib/prawn/text/box.rb +141 -0
  133. data/lib/prawn/text/formatted.rb +4 -0
  134. data/lib/prawn/text/formatted/box.rb +563 -0
  135. data/lib/prawn/text/formatted/fragment.rb +253 -0
  136. data/lib/prawn/text/formatted/parser.rb +217 -0
  137. data/lib/prawn/utilities.rb +44 -0
  138. data/manual/basic_concepts/adding_pages.rb +27 -0
  139. data/manual/basic_concepts/basic_concepts.rb +34 -0
  140. data/manual/basic_concepts/creation.rb +39 -0
  141. data/manual/basic_concepts/cursor.rb +33 -0
  142. data/manual/basic_concepts/measurement.rb +25 -0
  143. data/manual/basic_concepts/origin.rb +38 -0
  144. data/manual/basic_concepts/other_cursor_helpers.rb +40 -0
  145. data/manual/bounding_box/bounding_box.rb +39 -0
  146. data/manual/bounding_box/bounds.rb +49 -0
  147. data/manual/bounding_box/canvas.rb +24 -0
  148. data/manual/bounding_box/creation.rb +23 -0
  149. data/manual/bounding_box/indentation.rb +46 -0
  150. data/manual/bounding_box/nesting.rb +45 -0
  151. data/manual/bounding_box/russian_boxes.rb +40 -0
  152. data/manual/bounding_box/stretchy.rb +31 -0
  153. data/manual/document_and_page_options/background.rb +27 -0
  154. data/manual/document_and_page_options/document_and_page_options.rb +31 -0
  155. data/manual/document_and_page_options/metadata.rb +23 -0
  156. data/manual/document_and_page_options/page_margins.rb +38 -0
  157. data/manual/document_and_page_options/page_size.rb +34 -0
  158. data/manual/example_file.rb +116 -0
  159. data/manual/example_helper.rb +430 -0
  160. data/manual/example_package.rb +53 -0
  161. data/manual/example_section.rb +46 -0
  162. data/manual/graphics/circle_and_ellipse.rb +22 -0
  163. data/manual/graphics/color.rb +24 -0
  164. data/manual/graphics/common_lines.rb +28 -0
  165. data/manual/graphics/fill_and_stroke.rb +42 -0
  166. data/manual/graphics/fill_rules.rb +37 -0
  167. data/manual/graphics/gradients.rb +37 -0
  168. data/manual/graphics/graphics.rb +58 -0
  169. data/manual/graphics/helper.rb +17 -0
  170. data/manual/graphics/line_width.rb +35 -0
  171. data/manual/graphics/lines_and_curves.rb +41 -0
  172. data/manual/graphics/polygon.rb +29 -0
  173. data/manual/graphics/rectangle.rb +21 -0
  174. data/manual/graphics/rotate.rb +28 -0
  175. data/manual/graphics/scale.rb +41 -0
  176. data/manual/graphics/soft_masks.rb +46 -0
  177. data/manual/graphics/stroke_cap.rb +31 -0
  178. data/manual/graphics/stroke_dash.rb +43 -0
  179. data/manual/graphics/stroke_join.rb +30 -0
  180. data/manual/graphics/translate.rb +29 -0
  181. data/manual/graphics/transparency.rb +35 -0
  182. data/manual/images/absolute_position.rb +23 -0
  183. data/manual/images/fit.rb +21 -0
  184. data/manual/images/horizontal.rb +25 -0
  185. data/manual/images/images.rb +40 -0
  186. data/manual/images/plain_image.rb +18 -0
  187. data/manual/images/scale.rb +22 -0
  188. data/manual/images/vertical.rb +28 -0
  189. data/manual/images/width_and_height.rb +25 -0
  190. data/manual/layout/boxes.rb +27 -0
  191. data/manual/layout/content.rb +25 -0
  192. data/manual/layout/layout.rb +28 -0
  193. data/manual/layout/simple_grid.rb +23 -0
  194. data/manual/manual/cover.rb +26 -0
  195. data/manual/manual/foreword.rb +13 -0
  196. data/manual/manual/how_to_read_this_manual.rb +41 -0
  197. data/manual/manual/manual.rb +36 -0
  198. data/manual/outline/add_subsection_to.rb +61 -0
  199. data/manual/outline/insert_section_after.rb +47 -0
  200. data/manual/outline/outline.rb +32 -0
  201. data/manual/outline/sections_and_pages.rb +67 -0
  202. data/manual/repeatable_content/page_numbering.rb +54 -0
  203. data/manual/repeatable_content/repeatable_content.rb +31 -0
  204. data/manual/repeatable_content/repeater.rb +55 -0
  205. data/manual/repeatable_content/stamp.rb +41 -0
  206. data/manual/security/encryption.rb +31 -0
  207. data/manual/security/permissions.rb +38 -0
  208. data/manual/security/security.rb +28 -0
  209. data/manual/syntax_highlight.rb +52 -0
  210. data/manual/table/basic_block.rb +53 -0
  211. data/manual/table/before_rendering_page.rb +26 -0
  212. data/manual/table/cell_border_lines.rb +24 -0
  213. data/manual/table/cell_borders_and_bg.rb +31 -0
  214. data/manual/table/cell_dimensions.rb +30 -0
  215. data/manual/table/cell_text.rb +38 -0
  216. data/manual/table/column_widths.rb +30 -0
  217. data/manual/table/content_and_subtables.rb +39 -0
  218. data/manual/table/creation.rb +27 -0
  219. data/manual/table/filtering.rb +36 -0
  220. data/manual/table/flow_and_header.rb +17 -0
  221. data/manual/table/image_cells.rb +33 -0
  222. data/manual/table/position.rb +29 -0
  223. data/manual/table/row_colors.rb +20 -0
  224. data/manual/table/span.rb +30 -0
  225. data/manual/table/style.rb +22 -0
  226. data/manual/table/table.rb +52 -0
  227. data/manual/table/width.rb +27 -0
  228. data/manual/templates/full_template.rb +23 -0
  229. data/manual/templates/page_template.rb +47 -0
  230. data/manual/templates/templates.rb +26 -0
  231. data/manual/text/alignment.rb +44 -0
  232. data/manual/text/color.rb +24 -0
  233. data/manual/text/column_box.rb +32 -0
  234. data/manual/text/fallback_fonts.rb +37 -0
  235. data/manual/text/font.rb +41 -0
  236. data/manual/text/font_size.rb +45 -0
  237. data/manual/text/font_style.rb +23 -0
  238. data/manual/text/formatted_callbacks.rb +60 -0
  239. data/manual/text/formatted_text.rb +50 -0
  240. data/manual/text/free_flowing_text.rb +51 -0
  241. data/manual/text/group.rb +29 -0
  242. data/manual/text/inline.rb +43 -0
  243. data/manual/text/kerning_and_character_spacing.rb +39 -0
  244. data/manual/text/leading.rb +25 -0
  245. data/manual/text/line_wrapping.rb +41 -0
  246. data/manual/text/paragraph_indentation.rb +26 -0
  247. data/manual/text/positioned_text.rb +38 -0
  248. data/manual/text/registering_families.rb +48 -0
  249. data/manual/text/rendering_and_color.rb +37 -0
  250. data/manual/text/right_to_left_text.rb +43 -0
  251. data/manual/text/rotation.rb +43 -0
  252. data/manual/text/single_usage.rb +37 -0
  253. data/manual/text/text.rb +75 -0
  254. data/manual/text/text_box_excess.rb +32 -0
  255. data/manual/text/text_box_extensions.rb +45 -0
  256. data/manual/text/text_box_overflow.rb +44 -0
  257. data/manual/text/utf8.rb +28 -0
  258. data/manual/text/win_ansi_charset.rb +59 -0
  259. data/prawn.gemspec +46 -0
  260. data/spec/annotations_spec.rb +90 -0
  261. data/spec/bounding_box_spec.rb +493 -0
  262. data/spec/cell_spec.rb +584 -0
  263. data/spec/column_box_spec.rb +33 -0
  264. data/spec/data/curves.pdf +66 -0
  265. data/spec/destinations_spec.rb +15 -0
  266. data/spec/document_spec.rb +736 -0
  267. data/spec/extensions/encoding_helpers.rb +6 -0
  268. data/spec/extensions/mocha.rb +44 -0
  269. data/spec/font_spec.rb +433 -0
  270. data/spec/formatted_text_arranger_spec.rb +421 -0
  271. data/spec/formatted_text_box_spec.rb +640 -0
  272. data/spec/formatted_text_fragment_spec.rb +298 -0
  273. data/spec/graphics_spec.rb +651 -0
  274. data/spec/grid_spec.rb +85 -0
  275. data/spec/images_spec.rb +140 -0
  276. data/spec/inline_formatted_text_parser_spec.rb +515 -0
  277. data/spec/jpg_spec.rb +25 -0
  278. data/spec/line_wrap_spec.rb +333 -0
  279. data/spec/measurement_units_spec.rb +23 -0
  280. data/spec/name_tree_spec.rb +112 -0
  281. data/spec/object_store_spec.rb +170 -0
  282. data/spec/outline_spec.rb +426 -0
  283. data/spec/pdf_object_spec.rb +172 -0
  284. data/spec/png_spec.rb +240 -0
  285. data/spec/reference_spec.rb +105 -0
  286. data/spec/repeater_spec.rb +158 -0
  287. data/spec/security_spec.rb +126 -0
  288. data/spec/snapshot_spec.rb +186 -0
  289. data/spec/soft_mask_spec.rb +117 -0
  290. data/spec/span_spec.rb +49 -0
  291. data/spec/spec_helper.rb +36 -0
  292. data/spec/stamp_spec.rb +159 -0
  293. data/spec/stroke_styles_spec.rb +193 -0
  294. data/spec/table_spec.rb +1209 -0
  295. data/spec/template_spec.rb +351 -0
  296. data/spec/text_at_spec.rb +129 -0
  297. data/spec/text_box_spec.rb +1029 -0
  298. data/spec/text_rendering_mode_spec.rb +45 -0
  299. data/spec/text_spacing_spec.rb +93 -0
  300. data/spec/text_spec.rb +421 -0
  301. data/spec/text_with_inline_formatting_spec.rb +35 -0
  302. data/spec/transparency_spec.rb +89 -0
  303. metadata +544 -0
@@ -0,0 +1,776 @@
1
+ # encoding: utf-8
2
+
3
+ # cell.rb: Table cell drawing.
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
+
9
+ require 'date'
10
+ module Prawn
11
+ class Document
12
+
13
+ # Instantiates and draws a cell on the document.
14
+ #
15
+ # cell(:content => "Hello world!", :at => [12, 34])
16
+ #
17
+ # See Prawn::Table::Cell.make for full options.
18
+ #
19
+ def cell(options={})
20
+ cell = Table::Cell.make(self, options.delete(:content), options)
21
+ cell.draw
22
+ cell
23
+ end
24
+
25
+ # Set up, but do not draw, a cell. Useful for creating cells with
26
+ # formatting options to be inserted into a Table. Call +draw+ on the
27
+ # resulting Cell to ink it.
28
+ #
29
+ # See the documentation on Prawn::Cell for details on the arguments.
30
+ #
31
+ def make_cell(content, options={})
32
+ Prawn::Table::Cell.make(self, content, options)
33
+ end
34
+
35
+ end
36
+
37
+ class Table
38
+
39
+ # A Cell is a rectangular area of the page into which content is drawn. It
40
+ # has a framework for sizing itself and adding padding and simple styling.
41
+ # There are several standard Cell subclasses that handle things like text,
42
+ # Tables, and (in the future) stamps, images, and arbitrary content.
43
+ #
44
+ # Cells are a basic building block for table support (see Prawn::Table).
45
+ #
46
+ # Please subclass me if you want new content types! I'm designed to be very
47
+ # extensible. See the different standard Cell subclasses in
48
+ # lib/prawn/table/cell/*.rb for a template.
49
+ #
50
+ class Cell
51
+
52
+ # Amount of dead space (in PDF points) inside the borders but outside the
53
+ # content. Padding defaults to 5pt.
54
+ #
55
+ attr_reader :padding
56
+
57
+ # If provided, the minimum width that this cell in its column will permit.
58
+ #
59
+ def min_width_ignoring_span
60
+ set_width_constraints
61
+ @min_width
62
+ end
63
+
64
+ # Minimum width of the entire span group this cell controls.
65
+ #
66
+ def min_width
67
+ return min_width_ignoring_span if @colspan == 1
68
+
69
+ # Sum up the largest min-width from each column, including myself.
70
+ min_widths = Hash.new(0)
71
+ dummy_cells.each do |cell|
72
+ min_widths[cell.column] =
73
+ [min_widths[cell.column], cell.min_width].max
74
+ end
75
+ min_widths[column] = [min_widths[column], min_width_ignoring_span].max
76
+ min_widths.values.inject(0, &:+)
77
+ end
78
+
79
+ # Min-width of the span divided by the number of columns.
80
+ #
81
+ def avg_spanned_min_width
82
+ min_width.to_f / colspan
83
+ end
84
+
85
+ # If provided, the maximum width that this cell can be drawn in, within
86
+ # its column.
87
+ #
88
+ def max_width_ignoring_span
89
+ set_width_constraints
90
+ @max_width
91
+ end
92
+
93
+ # Maximum width of the entire span group this cell controls.
94
+ #
95
+ def max_width
96
+ return max_width_ignoring_span if @colspan == 1
97
+
98
+ # Sum the smallest max-width from each column in the group, including
99
+ # myself.
100
+ max_widths = Hash.new(0)
101
+ dummy_cells.each do |cell|
102
+ max_widths[cell.column] =
103
+ [max_widths[cell.column], cell.max_width].min
104
+ end
105
+ max_widths[column] = [max_widths[column], max_width_ignoring_span].min
106
+ max_widths.values.inject(0, &:+)
107
+ end
108
+
109
+ # Manually specify the cell's height.
110
+ #
111
+ attr_writer :height
112
+
113
+ # Specifies which borders to enable. Must be an array of zero or more of:
114
+ # <tt>[:left, :right, :top, :bottom]</tt>.
115
+ #
116
+ attr_accessor :borders
117
+
118
+ # Width, in PDF points, of the cell's borders: [top, right, bottom, left].
119
+ #
120
+ attr_reader :border_widths
121
+
122
+ # HTML RGB-format ("ccffff") border colors: [top, right, bottom, left].
123
+ #
124
+ attr_reader :border_colors
125
+
126
+ # Line style
127
+ #
128
+ attr_reader :border_lines
129
+
130
+ # Specifies the content for the cell. Must be a "cellable" object. See the
131
+ # "Data" section of the Prawn::Table documentation for details on cellable
132
+ # objects.
133
+ #
134
+ attr_accessor :content
135
+
136
+ # The background color, if any, for this cell. Specified in HTML RGB
137
+ # format, e.g., "ccffff". The background is drawn under the whole cell,
138
+ # including any padding.
139
+ #
140
+ attr_accessor :background_color
141
+
142
+ # Number of columns this cell spans. Defaults to 1.
143
+ #
144
+ attr_reader :colspan
145
+
146
+ # Number of rows this cell spans. Defaults to 1.
147
+ #
148
+ attr_reader :rowspan
149
+
150
+ # Array of SpanDummy cells (if any) that represent the other cells in
151
+ # this span group. They know their own width / height, but do not draw
152
+ # anything.
153
+ #
154
+ attr_reader :dummy_cells # :nodoc:
155
+
156
+ # Instantiates a Cell based on the given options. The particular class of
157
+ # cell returned depends on the :content argument. See the Prawn::Table
158
+ # documentation under "Data" for allowable content types.
159
+ #
160
+ def self.make(pdf, content, options={})
161
+ at = options.delete(:at) || [0, pdf.cursor]
162
+ content = content.to_s if content.nil? || content.kind_of?(Numeric) ||
163
+ content.kind_of?(Date)
164
+
165
+ if content.is_a?(Hash)
166
+ if img = content[:image]
167
+ return Cell::Image.new(pdf, at, content)
168
+ end
169
+ options.update(content)
170
+ content = options[:content]
171
+ else
172
+ options[:content] = content
173
+ end
174
+
175
+ options[:content] = content = "" if content.nil?
176
+
177
+ case content
178
+ when Prawn::Table::Cell
179
+ content
180
+ when String
181
+ Cell::Text.new(pdf, at, options)
182
+ when Prawn::Table
183
+ Cell::Subtable.new(pdf, at, options)
184
+ when Array
185
+ subtable = Prawn::Table.new(options[:content], pdf, {})
186
+ Cell::Subtable.new(pdf, at, options.merge(:content => subtable))
187
+ else
188
+ raise Errors::UnrecognizedTableContent
189
+ end
190
+ end
191
+
192
+ # A small amount added to the bounding box width to cover over floating-
193
+ # point errors when round-tripping from content_width to width and back.
194
+ # This does not change cell positioning; it only slightly expands each
195
+ # cell's bounding box width so that rounding error does not prevent a cell
196
+ # from rendering.
197
+ #
198
+ FPTolerance = 1
199
+
200
+ # Sets up a cell on the document +pdf+, at the given x/y location +point+,
201
+ # with the given +options+. Cell, like Table, follows the "options set
202
+ # accessors" paradigm (see "Options" under the Table documentation), so
203
+ # any cell accessor <tt>cell.foo = :bar</tt> can be set by providing the
204
+ # option <tt>:foo => :bar</tt> here.
205
+ #
206
+ def initialize(pdf, point, options={})
207
+ @pdf = pdf
208
+ @point = point
209
+
210
+ # Set defaults; these can be changed by options
211
+ @padding = [5, 5, 5, 5]
212
+ @borders = [:top, :bottom, :left, :right]
213
+ @border_widths = [1] * 4
214
+ @border_colors = ['000000'] * 4
215
+ @border_lines = [:solid] * 4
216
+ @colspan = 1
217
+ @rowspan = 1
218
+ @dummy_cells = []
219
+
220
+ options.each { |k, v| send("#{k}=", v) }
221
+
222
+ @initializer_run = true
223
+ end
224
+
225
+ # Supports setting multiple properties at once.
226
+ #
227
+ # cell.style(:padding => 0, :border_width => 2)
228
+ #
229
+ # is the same as:
230
+ #
231
+ # cell.padding = 0
232
+ # cell.border_width = 2
233
+ #
234
+ def style(options={}, &block)
235
+ options.each { |k, v| send("#{k}=", v) }
236
+
237
+ # The block form supports running a single block for multiple cells, as
238
+ # in Cells#style.
239
+ block.call(self) if block
240
+ end
241
+
242
+ # Returns the width of the cell in its first column alone, ignoring any
243
+ # colspans.
244
+ #
245
+ def width_ignoring_span
246
+ # We can't ||= here because the FP error accumulates on the round-trip
247
+ # from #content_width.
248
+ @width || (content_width + padding_left + padding_right)
249
+ end
250
+
251
+ # Returns the cell's width in points, inclusive of padding. If the cell is
252
+ # the master cell of a colspan, returns the width of the entire span
253
+ # group.
254
+ #
255
+ def width
256
+ return width_ignoring_span if @colspan == 1 && @rowspan == 1
257
+
258
+ # We're in a span group; get the maximum width per column (including
259
+ # the master cell) and sum each column.
260
+ column_widths = Hash.new(0)
261
+ dummy_cells.each do |cell|
262
+ column_widths[cell.column] =
263
+ [column_widths[cell.column], cell.width].max
264
+ end
265
+ column_widths[column] = [column_widths[column], width_ignoring_span].max
266
+ column_widths.values.inject(0, &:+)
267
+ end
268
+
269
+ # Manually sets the cell's width, inclusive of padding.
270
+ #
271
+ def width=(w)
272
+ @width = @min_width = @max_width = w
273
+ end
274
+
275
+ # Returns the width of the bare content in the cell, excluding padding.
276
+ #
277
+ def content_width
278
+ if @width # manually set
279
+ return @width - padding_left - padding_right
280
+ end
281
+
282
+ natural_content_width
283
+ end
284
+
285
+ # Width of the entire span group.
286
+ #
287
+ def spanned_content_width
288
+ width - padding_left - padding_right
289
+ end
290
+
291
+ # Returns the width this cell would naturally take on, absent other
292
+ # constraints. Must be implemented in subclasses.
293
+ #
294
+ def natural_content_width
295
+ raise NotImplementedError,
296
+ "subclasses must implement natural_content_width"
297
+ end
298
+
299
+ # Returns the cell's height in points, inclusive of padding, in its first
300
+ # row only.
301
+ #
302
+ def height_ignoring_span
303
+ # We can't ||= here because the FP error accumulates on the round-trip
304
+ # from #content_height.
305
+ @height || (content_height + padding_top + padding_bottom)
306
+ end
307
+
308
+ # Returns the cell's height in points, inclusive of padding. If the cell
309
+ # is the master cell of a rowspan, returns the width of the entire span
310
+ # group.
311
+ #
312
+ def height
313
+ return height_ignoring_span if @colspan == 1 && @rowspan == 1
314
+
315
+ # We're in a span group; get the maximum height per row (including the
316
+ # master cell) and sum each row.
317
+ row_heights = Hash.new(0)
318
+ dummy_cells.each do |cell|
319
+ row_heights[cell.row] = [row_heights[cell.row], cell.height].max
320
+ end
321
+ row_heights[row] = [row_heights[row], height_ignoring_span].max
322
+ row_heights.values.inject(0, &:+)
323
+ end
324
+
325
+ # Returns the height of the bare content in the cell, excluding padding.
326
+ #
327
+ def content_height
328
+ if @height # manually set
329
+ return @height - padding_top - padding_bottom
330
+ end
331
+
332
+ natural_content_height
333
+ end
334
+
335
+ # Height of the entire span group.
336
+ #
337
+ def spanned_content_height
338
+ height - padding_top - padding_bottom
339
+ end
340
+
341
+ # Returns the height this cell would naturally take on, absent
342
+ # constraints. Must be implemented in subclasses.
343
+ #
344
+ def natural_content_height
345
+ raise NotImplementedError,
346
+ "subclasses must implement natural_content_height"
347
+ end
348
+
349
+ # Indicates the number of columns that this cell is to span. Defaults to
350
+ # 1.
351
+ #
352
+ # This must be provided as part of the table data, like so:
353
+ #
354
+ # pdf.table([["foo", {:content => "bar", :colspan => 2}]])
355
+ #
356
+ # Setting colspan from the initializer block is invalid because layout
357
+ # has already run. For example, this will NOT work:
358
+ #
359
+ # pdf.table([["foo", "bar"]]) { cells[0, 1].colspan = 2 }
360
+ #
361
+ def colspan=(span)
362
+ if @initializer_run
363
+ raise Prawn::Errors::InvalidTableSpan,
364
+ "colspan must be provided in the table's structure, never in the " +
365
+ "initialization block. See Prawn's documentation for details."
366
+ end
367
+
368
+ @colspan = span
369
+ end
370
+
371
+ # Indicates the number of rows that this cell is to span. Defaults to 1.
372
+ #
373
+ # This must be provided as part of the table data, like so:
374
+ #
375
+ # pdf.table([["foo", {:content => "bar", :rowspan => 2}], ["baz"]])
376
+ #
377
+ # Setting rowspan from the initializer block is invalid because layout
378
+ # has already run. For example, this will NOT work:
379
+ #
380
+ # pdf.table([["foo", "bar"], ["baz"]]) { cells[0, 1].rowspan = 2 }
381
+ #
382
+ def rowspan=(span)
383
+ if @initializer_run
384
+ raise Prawn::Errors::InvalidTableSpan,
385
+ "rowspan must be provided in the table's structure, never in the " +
386
+ "initialization block. See Prawn's documentation for details."
387
+ end
388
+
389
+ @rowspan = span
390
+ end
391
+
392
+ # Draws the cell onto the document. Pass in a point [x,y] to override the
393
+ # location at which the cell is drawn.
394
+ #
395
+ # If drawing a group of cells at known positions, look into
396
+ # Cell.draw_cells, which ensures that the backgrounds, borders, and
397
+ # content are all drawn in correct order so as not to overlap.
398
+ #
399
+ def draw(pt=[x, y])
400
+ Prawn::Table::Cell.draw_cells([[self, pt]])
401
+ end
402
+
403
+ # Given an array of pairs [cell, pt], draws each cell at its
404
+ # corresponding pt, making sure all backgrounds are behind all borders
405
+ # and content.
406
+ #
407
+ def self.draw_cells(cells)
408
+ cells.each do |cell, pt|
409
+ cell.set_width_constraints
410
+ cell.draw_background(pt)
411
+ end
412
+
413
+ cells.each do |cell, pt|
414
+ cell.draw_borders(pt)
415
+ cell.draw_bounded_content(pt)
416
+ end
417
+ end
418
+
419
+ # Draws the cell's content at the point provided.
420
+ #
421
+ def draw_bounded_content(pt)
422
+ @pdf.float do
423
+ @pdf.bounding_box([pt[0] + padding_left, pt[1] - padding_top],
424
+ :width => spanned_content_width + FPTolerance,
425
+ :height => spanned_content_height + FPTolerance) do
426
+ draw_content
427
+ end
428
+ end
429
+ end
430
+
431
+ # x-position of the cell within the parent bounds.
432
+ #
433
+ def x
434
+ @point[0]
435
+ end
436
+
437
+ # Set the x-position of the cell within the parent bounds.
438
+ #
439
+ def x=(val)
440
+ @point[0] = val
441
+ end
442
+
443
+ # y-position of the cell within the parent bounds.
444
+ #
445
+ def y
446
+ @point[1]
447
+ end
448
+
449
+ # Set the y-position of the cell within the parent bounds.
450
+ #
451
+ def y=(val)
452
+ @point[1] = val
453
+ end
454
+
455
+ # Sets padding on this cell. The argument can be one of:
456
+ #
457
+ # * an integer (sets all padding)
458
+ # * a two-element array [vertical, horizontal]
459
+ # * a three-element array [top, horizontal, bottom]
460
+ # * a four-element array [top, right, bottom, left]
461
+ #
462
+ def padding=(pad)
463
+ @padding = case
464
+ when pad.nil?
465
+ [0, 0, 0, 0]
466
+ when Numeric === pad # all padding
467
+ [pad, pad, pad, pad]
468
+ when pad.length == 2 # vert, horiz
469
+ [pad[0], pad[1], pad[0], pad[1]]
470
+ when pad.length == 3 # top, horiz, bottom
471
+ [pad[0], pad[1], pad[2], pad[1]]
472
+ when pad.length == 4 # top, right, bottom, left
473
+ [pad[0], pad[1], pad[2], pad[3]]
474
+ else
475
+ raise ArgumentError, ":padding must be a number or an array [v,h] " +
476
+ "or [t,r,b,l]"
477
+ end
478
+ end
479
+
480
+ def padding_top
481
+ @padding[0]
482
+ end
483
+
484
+ def padding_top=(val)
485
+ @padding[0] = val
486
+ end
487
+
488
+ def padding_right
489
+ @padding[1]
490
+ end
491
+
492
+ def padding_right=(val)
493
+ @padding[1] = val
494
+ end
495
+
496
+ def padding_bottom
497
+ @padding[2]
498
+ end
499
+
500
+ def padding_bottom=(val)
501
+ @padding[2] = val
502
+ end
503
+
504
+ def padding_left
505
+ @padding[3]
506
+ end
507
+
508
+ def padding_left=(val)
509
+ @padding[3] = val
510
+ end
511
+
512
+ # Sets border colors on this cell. The argument can be one of:
513
+ #
514
+ # * an integer (sets all colors)
515
+ # * a two-element array [vertical, horizontal]
516
+ # * a three-element array [top, horizontal, bottom]
517
+ # * a four-element array [top, right, bottom, left]
518
+ #
519
+ def border_color=(color)
520
+ @border_colors = case
521
+ when color.nil?
522
+ ["000000"] * 4
523
+ when String === color # all colors
524
+ [color, color, color, color]
525
+ when color.length == 2 # vert, horiz
526
+ [color[0], color[1], color[0], color[1]]
527
+ when color.length == 3 # top, horiz, bottom
528
+ [color[0], color[1], color[2], color[1]]
529
+ when color.length == 4 # top, right, bottom, left
530
+ [color[0], color[1], color[2], color[3]]
531
+ else
532
+ raise ArgumentError, ":border_color must be a string " +
533
+ "or an array [v,h] or [t,r,b,l]"
534
+ end
535
+ end
536
+ alias_method :border_colors=, :border_color=
537
+
538
+ def border_top_color
539
+ @border_colors[0]
540
+ end
541
+
542
+ def border_top_color=(val)
543
+ @border_colors[0] = val
544
+ end
545
+
546
+ def border_top_color
547
+ @border_colors[0]
548
+ end
549
+
550
+ def border_top_color=(val)
551
+ @border_colors[0] = val
552
+ end
553
+
554
+ def border_right_color
555
+ @border_colors[1]
556
+ end
557
+
558
+ def border_right_color=(val)
559
+ @border_colors[1] = val
560
+ end
561
+
562
+ def border_bottom_color
563
+ @border_colors[2]
564
+ end
565
+
566
+ def border_bottom_color=(val)
567
+ @border_colors[2] = val
568
+ end
569
+
570
+ def border_left_color
571
+ @border_colors[3]
572
+ end
573
+
574
+ def border_left_color=(val)
575
+ @border_colors[3] = val
576
+ end
577
+
578
+ # Sets border widths on this cell. The argument can be one of:
579
+ #
580
+ # * an integer (sets all widths)
581
+ # * a two-element array [vertical, horizontal]
582
+ # * a three-element array [top, horizontal, bottom]
583
+ # * a four-element array [top, right, bottom, left]
584
+ #
585
+ def border_width=(width)
586
+ @border_widths = case
587
+ when width.nil?
588
+ ["000000"] * 4
589
+ when Numeric === width # all widths
590
+ [width, width, width, width]
591
+ when width.length == 2 # vert, horiz
592
+ [width[0], width[1], width[0], width[1]]
593
+ when width.length == 3 # top, horiz, bottom
594
+ [width[0], width[1], width[2], width[1]]
595
+ when width.length == 4 # top, right, bottom, left
596
+ [width[0], width[1], width[2], width[3]]
597
+ else
598
+ raise ArgumentError, ":border_width must be a string " +
599
+ "or an array [v,h] or [t,r,b,l]"
600
+ end
601
+ end
602
+ alias_method :border_widths=, :border_width=
603
+
604
+ def border_top_width
605
+ @borders.include?(:top) ? @border_widths[0] : 0
606
+ end
607
+
608
+ def border_top_width=(val)
609
+ @border_widths[0] = val
610
+ end
611
+
612
+ def border_right_width
613
+ @borders.include?(:right) ? @border_widths[1] : 0
614
+ end
615
+
616
+ def border_right_width=(val)
617
+ @border_widths[1] = val
618
+ end
619
+
620
+ def border_bottom_width
621
+ @borders.include?(:bottom) ? @border_widths[2] : 0
622
+ end
623
+
624
+ def border_bottom_width=(val)
625
+ @border_widths[2] = val
626
+ end
627
+
628
+ def border_left_width
629
+ @borders.include?(:left) ? @border_widths[3] : 0
630
+ end
631
+
632
+ def border_left_width=(val)
633
+ @border_widths[3] = val
634
+ end
635
+
636
+ # Sets the cell's minimum and maximum width. Deferred until requested
637
+ # because padding and size can change.
638
+ #
639
+ def set_width_constraints
640
+ @min_width ||= padding_left + padding_right
641
+ @max_width ||= @pdf.bounds.width
642
+ end
643
+
644
+ # Sets border line style on this cell. The argument can be one of:
645
+ #
646
+ # Possible values are: :solid, :dashed, :dotted
647
+ #
648
+ # * one value (sets all lines)
649
+ # * a two-element array [vertical, horizontal]
650
+ # * a three-element array [top, horizontal, bottom]
651
+ # * a four-element array [top, right, bottom, left]
652
+ #
653
+ def border_line=(line)
654
+ @border_lines = case
655
+ when line.nil?
656
+ [:solid] * 4
657
+ when line.length == 1 # all lines
658
+ [line[0]] * 4
659
+ when line.length == 2
660
+ [line[0], line[1], line[0], line[1]]
661
+ when line.length == 3
662
+ [line[0], line[1], line[2], line[1]]
663
+ when line.length == 4
664
+ [line[0], line[1], line[2], line[3]]
665
+ else
666
+ raise ArgumentError, "border_line must be one of :solid, :dashed, "
667
+ ":dotted or an array [v,h] or [t,r,b,l]"
668
+ end
669
+ end
670
+ alias_method :border_lines=, :border_line=
671
+
672
+ def border_top_line
673
+ @borders.include?(:top) ? @border_lines[0] : 0
674
+ end
675
+
676
+ def border_top_line=(val)
677
+ @border_lines[0] = val
678
+ end
679
+
680
+ def border_right_line
681
+ @borders.include?(:right) ? @border_lines[1] : 0
682
+ end
683
+
684
+ def border_right_line=(val)
685
+ @border_lines[1] = val
686
+ end
687
+
688
+ def border_bottom_line
689
+ @borders.include?(:bottom) ? @border_lines[2] : 0
690
+ end
691
+
692
+ def border_bottom_line=(val)
693
+ @border_lines[2] = val
694
+ end
695
+
696
+ def border_left_line
697
+ @borders.include?(:left) ? @border_lines[3] : 0
698
+ end
699
+
700
+ def border_left_line=(val)
701
+ @border_lines[3] = val
702
+ end
703
+
704
+ # Draws the cell's background color.
705
+ #
706
+ def draw_background(pt)
707
+ if @background_color
708
+ @pdf.mask(:fill_color) do
709
+ @pdf.fill_color @background_color
710
+ @pdf.fill_rectangle pt, width, height
711
+ end
712
+ end
713
+ end
714
+
715
+ # Draws borders around the cell. Borders are centered on the bounds of
716
+ # the cell outside of any padding, so the caller is responsible for
717
+ # setting appropriate padding to ensure the border does not overlap with
718
+ # cell content.
719
+ #
720
+ def draw_borders(pt)
721
+ x, y = pt
722
+
723
+ @pdf.mask(:line_width, :stroke_color) do
724
+ @borders.each do |border|
725
+ idx = {:top => 0, :right => 1, :bottom => 2, :left => 3}[border]
726
+ border_color = @border_colors[idx]
727
+ border_width = @border_widths[idx]
728
+ border_line = @border_lines[idx]
729
+
730
+ next if border_width <= 0
731
+
732
+ # Left and right borders are drawn one-half border beyond the center
733
+ # of the corner, so that the corners end up square.
734
+ from, to = case border
735
+ when :top
736
+ [[x, y], [x+width, y]]
737
+ when :bottom
738
+ [[x, y-height], [x+width, y-height]]
739
+ when :left
740
+ [[x, y + (border_top_width / 2.0)],
741
+ [x, y - height - (border_bottom_width / 2.0)]]
742
+ when :right
743
+ [[x+width, y + (border_top_width / 2.0)],
744
+ [x+width, y - height - (border_bottom_width / 2.0)]]
745
+ end
746
+
747
+ case border_line
748
+ when :dashed
749
+ @pdf.dash border_width * 4
750
+ when :dotted
751
+ @pdf.dash border_width, :space => border_width * 2
752
+ when :solid
753
+ # normal line style
754
+ else
755
+ raise ArgumentError, "border_line must be :solid, :dotted or" +
756
+ " :dashed"
757
+ end
758
+
759
+ @pdf.line_width = border_width
760
+ @pdf.stroke_color = border_color
761
+ @pdf.stroke_line(from, to)
762
+ @pdf.undash
763
+ end
764
+ end
765
+ end
766
+
767
+ # Draws cell content within the cell's bounding box. Must be implemented
768
+ # in subclasses.
769
+ #
770
+ def draw_content
771
+ raise NotImplementedError, "subclasses must implement draw_content"
772
+ end
773
+
774
+ end
775
+ end
776
+ end