prawn 0.13.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (348) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data/.yardopts +10 -0
  4. data/GPLv2 +20 -21
  5. data/Gemfile +3 -16
  6. data/Rakefile +17 -39
  7. data/lib/prawn/document/bounding_box.rb +85 -42
  8. data/lib/prawn/document/column_box.rb +21 -11
  9. data/lib/prawn/document/internals.rb +40 -147
  10. data/lib/prawn/document/span.rb +25 -17
  11. data/lib/prawn/document.rb +286 -245
  12. data/lib/prawn/encoding.rb +68 -101
  13. data/lib/prawn/errors.rb +47 -43
  14. data/lib/prawn/font.rb +204 -155
  15. data/lib/prawn/font_metric_cache.rb +25 -21
  16. data/lib/prawn/fonts/afm.rb +292 -0
  17. data/lib/prawn/{font → fonts}/dfont.rb +7 -13
  18. data/lib/prawn/fonts/otf.rb +11 -0
  19. data/lib/prawn/fonts/ttc.rb +36 -0
  20. data/lib/prawn/{font → fonts}/ttf.rb +142 -80
  21. data/lib/prawn/graphics/blend_mode.rb +65 -0
  22. data/lib/prawn/graphics/cap_style.rb +6 -5
  23. data/lib/prawn/graphics/color.rb +47 -44
  24. data/lib/prawn/graphics/dash.rb +30 -13
  25. data/lib/prawn/graphics/join_style.rb +13 -6
  26. data/lib/prawn/graphics/patterns.rb +221 -90
  27. data/lib/prawn/graphics/transformation.rb +21 -12
  28. data/lib/prawn/graphics/transparency.rb +21 -17
  29. data/lib/prawn/graphics.rb +155 -128
  30. data/lib/prawn/{layout/grid.rb → grid.rb} +110 -47
  31. data/lib/prawn/image_handler.rb +16 -2
  32. data/lib/prawn/images/image.rb +4 -2
  33. data/lib/prawn/images/jpg.rb +39 -30
  34. data/lib/prawn/images/png.rb +132 -169
  35. data/lib/prawn/images.rb +70 -62
  36. data/lib/prawn/measurement_extensions.rb +15 -10
  37. data/lib/prawn/measurements.rb +22 -23
  38. data/lib/prawn/outline.rb +301 -13
  39. data/lib/prawn/repeater.rb +19 -17
  40. data/lib/prawn/security/arcfour.rb +54 -0
  41. data/lib/prawn/security.rb +108 -86
  42. data/lib/prawn/soft_mask.rb +40 -41
  43. data/lib/prawn/stamp.rb +29 -12
  44. data/lib/prawn/text/box.rb +27 -29
  45. data/lib/prawn/text/formatted/arranger.rb +110 -67
  46. data/lib/prawn/text/formatted/box.rb +233 -165
  47. data/lib/prawn/text/formatted/fragment.rb +27 -27
  48. data/lib/prawn/text/formatted/line_wrap.rb +137 -97
  49. data/lib/prawn/text/formatted/parser.rb +149 -127
  50. data/lib/prawn/text/formatted/wrap.rb +57 -37
  51. data/lib/prawn/text/formatted.rb +6 -4
  52. data/lib/prawn/text.rb +105 -73
  53. data/lib/prawn/transformation_stack.rb +44 -0
  54. data/lib/prawn/utilities.rb +11 -21
  55. data/lib/prawn/version.rb +5 -0
  56. data/lib/prawn/view.rb +101 -0
  57. data/lib/prawn.rb +42 -68
  58. data/{data/images/fractal.jpg → manual/absolute_position.pdf} +0 -0
  59. data/manual/basic_concepts/adding_pages.rb +9 -10
  60. data/manual/basic_concepts/basic_concepts.rb +33 -24
  61. data/manual/basic_concepts/creation.rb +10 -11
  62. data/manual/basic_concepts/cursor.rb +9 -10
  63. data/manual/basic_concepts/measurement.rb +10 -11
  64. data/manual/basic_concepts/origin.rb +8 -9
  65. data/manual/basic_concepts/other_cursor_helpers.rb +17 -18
  66. data/manual/basic_concepts/view.rb +48 -0
  67. data/manual/bounding_box/bounding_box.rb +31 -29
  68. data/manual/bounding_box/bounds.rb +17 -18
  69. data/manual/bounding_box/canvas.rb +8 -9
  70. data/manual/bounding_box/creation.rb +8 -9
  71. data/manual/bounding_box/indentation.rb +22 -23
  72. data/manual/bounding_box/nesting.rb +32 -25
  73. data/manual/bounding_box/russian_boxes.rb +19 -19
  74. data/manual/bounding_box/stretchy.rb +18 -20
  75. data/manual/contents.rb +35 -0
  76. data/manual/cover.rb +43 -0
  77. data/manual/document_and_page_options/background.rb +16 -14
  78. data/manual/document_and_page_options/document_and_page_options.rb +26 -23
  79. data/manual/document_and_page_options/metadata.rb +21 -19
  80. data/manual/document_and_page_options/page_margins.rb +20 -22
  81. data/manual/document_and_page_options/page_size.rb +15 -15
  82. data/manual/document_and_page_options/print_scaling.rb +23 -0
  83. data/manual/example_helper.rb +5 -408
  84. data/manual/graphics/blend_mode.rb +52 -0
  85. data/manual/graphics/circle_and_ellipse.rb +8 -9
  86. data/manual/graphics/color.rb +11 -13
  87. data/manual/graphics/common_lines.rb +13 -12
  88. data/manual/graphics/fill_and_stroke.rb +10 -11
  89. data/manual/graphics/fill_rules.rb +13 -12
  90. data/manual/graphics/gradients.rb +28 -22
  91. data/manual/graphics/graphics.rb +52 -46
  92. data/manual/graphics/helper.rb +20 -10
  93. data/manual/graphics/line_width.rb +13 -12
  94. data/manual/graphics/lines_and_curves.rb +13 -14
  95. data/manual/graphics/polygon.rb +10 -12
  96. data/manual/graphics/rectangle.rb +7 -8
  97. data/manual/graphics/rotate.rb +9 -12
  98. data/manual/graphics/scale.rb +19 -18
  99. data/manual/graphics/soft_masks.rb +5 -7
  100. data/manual/graphics/stroke_cap.rb +10 -11
  101. data/manual/graphics/stroke_dash.rb +16 -17
  102. data/manual/graphics/stroke_join.rb +10 -11
  103. data/manual/graphics/translate.rb +13 -13
  104. data/manual/graphics/transparency.rb +11 -13
  105. data/manual/{manual/how_to_read_this_manual.rb → how_to_read_this_manual.rb} +23 -25
  106. data/manual/images/absolute_position.rb +9 -10
  107. data/manual/images/fit.rb +9 -10
  108. data/manual/images/horizontal.rb +13 -14
  109. data/manual/images/images.rb +31 -30
  110. data/manual/images/plain_image.rb +6 -7
  111. data/manual/images/scale.rb +12 -13
  112. data/manual/images/vertical.rb +19 -17
  113. data/manual/images/width_and_height.rb +13 -14
  114. data/manual/layout/boxes.rb +14 -15
  115. data/manual/layout/content.rb +12 -13
  116. data/manual/layout/layout.rb +19 -20
  117. data/manual/layout/simple_grid.rb +8 -9
  118. data/manual/outline/add_subsection_to.rb +26 -27
  119. data/manual/outline/insert_section_after.rb +19 -20
  120. data/manual/outline/outline.rb +23 -22
  121. data/manual/outline/sections_and_pages.rb +24 -25
  122. data/manual/repeatable_content/alternate_page_numbering.rb +36 -0
  123. data/manual/repeatable_content/page_numbering.rb +20 -19
  124. data/manual/repeatable_content/repeatable_content.rb +26 -22
  125. data/manual/repeatable_content/repeater.rb +18 -19
  126. data/manual/repeatable_content/stamp.rb +18 -19
  127. data/manual/security/encryption.rb +8 -11
  128. data/manual/security/permissions.rb +20 -15
  129. data/manual/security/security.rb +20 -20
  130. data/manual/table.rb +16 -0
  131. data/manual/text/alignment.rb +17 -18
  132. data/manual/text/color.rb +13 -13
  133. data/manual/text/column_box.rb +10 -12
  134. data/manual/text/fallback_fonts.rb +29 -25
  135. data/manual/text/font.rb +17 -18
  136. data/manual/text/font_size.rb +21 -22
  137. data/manual/text/font_style.rb +12 -10
  138. data/manual/text/formatted_callbacks.rb +36 -26
  139. data/manual/text/formatted_text.rb +41 -34
  140. data/manual/text/free_flowing_text.rb +28 -29
  141. data/manual/text/inline.rb +23 -26
  142. data/manual/text/kerning_and_character_spacing.rb +20 -21
  143. data/manual/text/leading.rb +10 -11
  144. data/manual/text/line_wrapping.rb +40 -21
  145. data/manual/text/paragraph_indentation.rb +17 -12
  146. data/manual/text/positioned_text.rb +19 -20
  147. data/manual/text/registering_families.rb +33 -30
  148. data/manual/text/rendering_and_color.rb +11 -12
  149. data/manual/text/right_to_left_text.rb +31 -20
  150. data/manual/text/rotation.rb +36 -27
  151. data/manual/text/single_usage.rb +13 -14
  152. data/manual/text/text.rb +62 -62
  153. data/manual/text/text_box_excess.rb +22 -19
  154. data/manual/text/text_box_extensions.rb +21 -18
  155. data/manual/text/text_box_overflow.rb +28 -21
  156. data/manual/text/utf8.rb +16 -17
  157. data/manual/text/win_ansi_charset.rb +29 -26
  158. data/prawn.gemspec +45 -43
  159. data/spec/extensions/encoding_helpers.rb +4 -3
  160. data/spec/prawn/document/bounding_box_spec.rb +550 -0
  161. data/spec/prawn/document/column_box_spec.rb +75 -0
  162. data/spec/prawn/document/security_spec.rb +176 -0
  163. data/spec/prawn/document_annotations_spec.rb +76 -0
  164. data/spec/prawn/document_destinations_spec.rb +15 -0
  165. data/spec/prawn/document_grid_spec.rb +99 -0
  166. data/spec/prawn/document_reference_spec.rb +27 -0
  167. data/spec/prawn/document_span_spec.rb +44 -0
  168. data/spec/prawn/document_spec.rb +805 -0
  169. data/spec/prawn/font_metric_cache_spec.rb +54 -0
  170. data/spec/prawn/font_spec.rb +544 -0
  171. data/spec/prawn/graphics/blend_mode_spec.rb +63 -0
  172. data/spec/prawn/graphics/transparency_spec.rb +81 -0
  173. data/spec/prawn/graphics_spec.rb +872 -0
  174. data/spec/prawn/graphics_stroke_styles_spec.rb +229 -0
  175. data/spec/prawn/image_handler_spec.rb +53 -0
  176. data/spec/prawn/images/jpg_spec.rb +20 -0
  177. data/spec/prawn/images/png_spec.rb +283 -0
  178. data/spec/prawn/images_spec.rb +229 -0
  179. data/spec/prawn/measurements_extensions_spec.rb +24 -0
  180. data/spec/prawn/outline_spec.rb +512 -0
  181. data/spec/prawn/repeater_spec.rb +166 -0
  182. data/spec/prawn/soft_mask_spec.rb +74 -0
  183. data/spec/prawn/stamp_spec.rb +173 -0
  184. data/spec/prawn/text/box_spec.rb +1110 -0
  185. data/spec/prawn/text/formatted/arranger_spec.rb +466 -0
  186. data/spec/prawn/text/formatted/box_spec.rb +849 -0
  187. data/spec/prawn/text/formatted/fragment_spec.rb +343 -0
  188. data/spec/prawn/text/formatted/line_wrap_spec.rb +495 -0
  189. data/spec/prawn/text/formatted/parser_spec.rb +697 -0
  190. data/spec/prawn/text_draw_text_spec.rb +150 -0
  191. data/spec/prawn/text_rendering_mode_spec.rb +48 -0
  192. data/spec/prawn/text_spacing_spec.rb +95 -0
  193. data/spec/prawn/text_spec.rb +603 -0
  194. data/spec/prawn/text_with_inline_formatting_spec.rb +35 -0
  195. data/spec/prawn/transformation_stack_spec.rb +66 -0
  196. data/spec/prawn/view_spec.rb +63 -0
  197. data/spec/prawn_manual_spec.rb +35 -0
  198. data/spec/spec_helper.rb +22 -21
  199. data.tar.gz.sig +0 -0
  200. metadata +168 -307
  201. metadata.gz.sig +0 -0
  202. data/README.md +0 -109
  203. data/data/encodings/win_ansi.txt +0 -29
  204. data/data/images/16bit.alpha +0 -0
  205. data/data/images/16bit.dat +0 -0
  206. data/data/images/16bit.png +0 -0
  207. data/data/images/arrow.png +0 -0
  208. data/data/images/arrow2.png +0 -0
  209. data/data/images/barcode_issue.png +0 -0
  210. data/data/images/dice.alpha +0 -0
  211. data/data/images/dice.dat +0 -0
  212. data/data/images/dice.png +0 -0
  213. data/data/images/dice_interlaced.png +0 -0
  214. data/data/images/indexed_color.dat +0 -0
  215. data/data/images/indexed_color.png +0 -0
  216. data/data/images/letterhead.jpg +0 -0
  217. data/data/images/page_white_text.alpha +0 -0
  218. data/data/images/page_white_text.dat +0 -0
  219. data/data/images/page_white_text.png +0 -0
  220. data/data/images/pigs.jpg +0 -0
  221. data/data/images/prawn.png +0 -0
  222. data/data/images/ruport.png +0 -0
  223. data/data/images/ruport_data.dat +0 -0
  224. data/data/images/ruport_transparent.png +0 -0
  225. data/data/images/ruport_type0.png +0 -0
  226. data/data/images/stef.jpg +0 -0
  227. data/data/images/tru256.bmp +0 -0
  228. data/data/images/web-links.dat +0 -1
  229. data/data/images/web-links.png +0 -0
  230. data/data/pdfs/complex_template.pdf +0 -0
  231. data/data/pdfs/contains_ttf_font.pdf +0 -0
  232. data/data/pdfs/encrypted.pdf +0 -0
  233. data/data/pdfs/form.pdf +1 -819
  234. data/data/pdfs/hexagon.pdf +0 -61
  235. data/data/pdfs/indirect_reference.pdf +0 -86
  236. data/data/pdfs/multipage_template.pdf +0 -127
  237. data/data/pdfs/nested_pages.pdf +0 -118
  238. data/data/pdfs/page_without_mediabox.pdf +0 -193
  239. data/data/pdfs/resources_as_indirect_object.pdf +0 -83
  240. data/data/pdfs/two_hexagons.pdf +0 -90
  241. data/data/pdfs/version_1_6.pdf +0 -61
  242. data/data/shift_jis_text.txt +0 -1
  243. data/lib/pdf/core/annotations.rb +0 -60
  244. data/lib/pdf/core/byte_string.rb +0 -9
  245. data/lib/pdf/core/destinations.rb +0 -90
  246. data/lib/pdf/core/document_state.rb +0 -78
  247. data/lib/pdf/core/filter_list.rb +0 -51
  248. data/lib/pdf/core/filters.rb +0 -36
  249. data/lib/pdf/core/graphics_state.rb +0 -68
  250. data/lib/pdf/core/literal_string.rb +0 -16
  251. data/lib/pdf/core/name_tree.rb +0 -177
  252. data/lib/pdf/core/object_store.rb +0 -320
  253. data/lib/pdf/core/outline.rb +0 -315
  254. data/lib/pdf/core/page.rb +0 -212
  255. data/lib/pdf/core/page_geometry.rb +0 -126
  256. data/lib/pdf/core/pdf_object.rb +0 -124
  257. data/lib/pdf/core/reference.rb +0 -103
  258. data/lib/pdf/core/stream.rb +0 -98
  259. data/lib/pdf/core/text.rb +0 -275
  260. data/lib/pdf/core.rb +0 -35
  261. data/lib/prawn/compatibility.rb +0 -91
  262. data/lib/prawn/document/graphics_state.rb +0 -73
  263. data/lib/prawn/document/snapshot.rb +0 -89
  264. data/lib/prawn/font/afm.rb +0 -203
  265. data/lib/prawn/layout.rb +0 -20
  266. data/lib/prawn/table/cell/image.rb +0 -70
  267. data/lib/prawn/table/cell/in_table.rb +0 -27
  268. data/lib/prawn/table/cell/span_dummy.rb +0 -92
  269. data/lib/prawn/table/cell/subtable.rb +0 -65
  270. data/lib/prawn/table/cell/text.rb +0 -153
  271. data/lib/prawn/table/cell.rb +0 -770
  272. data/lib/prawn/table/cells.rb +0 -295
  273. data/lib/prawn/table.rb +0 -643
  274. data/manual/example_file.rb +0 -116
  275. data/manual/example_package.rb +0 -53
  276. data/manual/example_section.rb +0 -46
  277. data/manual/manual/cover.rb +0 -35
  278. data/manual/manual/foreword.rb +0 -85
  279. data/manual/manual/manual.rb +0 -35
  280. data/manual/syntax_highlight.rb +0 -52
  281. data/manual/table/basic_block.rb +0 -53
  282. data/manual/table/before_rendering_page.rb +0 -26
  283. data/manual/table/cell_border_lines.rb +0 -24
  284. data/manual/table/cell_borders_and_bg.rb +0 -31
  285. data/manual/table/cell_dimensions.rb +0 -30
  286. data/manual/table/cell_text.rb +0 -38
  287. data/manual/table/column_widths.rb +0 -30
  288. data/manual/table/content_and_subtables.rb +0 -39
  289. data/manual/table/creation.rb +0 -27
  290. data/manual/table/filtering.rb +0 -36
  291. data/manual/table/flow_and_header.rb +0 -17
  292. data/manual/table/image_cells.rb +0 -33
  293. data/manual/table/position.rb +0 -29
  294. data/manual/table/row_colors.rb +0 -20
  295. data/manual/table/span.rb +0 -30
  296. data/manual/table/style.rb +0 -22
  297. data/manual/table/table.rb +0 -52
  298. data/manual/table/width.rb +0 -27
  299. data/manual/templates/full_template.rb +0 -25
  300. data/manual/templates/page_template.rb +0 -48
  301. data/manual/templates/templates.rb +0 -27
  302. data/manual/text/group.rb +0 -29
  303. data/spec/acceptance/png.rb +0 -23
  304. data/spec/annotations_spec.rb +0 -74
  305. data/spec/bounding_box_spec.rb +0 -493
  306. data/spec/cell_spec.rb +0 -628
  307. data/spec/column_box_spec.rb +0 -33
  308. data/spec/destinations_spec.rb +0 -15
  309. data/spec/document_spec.rb +0 -761
  310. data/spec/extensions/mocha.rb +0 -44
  311. data/spec/filters_spec.rb +0 -34
  312. data/spec/font_metric_cache_spec.rb +0 -52
  313. data/spec/font_spec.rb +0 -464
  314. data/spec/formatted_text_arranger_spec.rb +0 -421
  315. data/spec/formatted_text_box_spec.rb +0 -650
  316. data/spec/formatted_text_fragment_spec.rb +0 -298
  317. data/spec/graphics_spec.rb +0 -651
  318. data/spec/grid_spec.rb +0 -85
  319. data/spec/image_handler_spec.rb +0 -42
  320. data/spec/images_spec.rb +0 -157
  321. data/spec/inline_formatted_text_parser_spec.rb +0 -564
  322. data/spec/jpg_spec.rb +0 -25
  323. data/spec/line_wrap_spec.rb +0 -333
  324. data/spec/measurement_units_spec.rb +0 -23
  325. data/spec/name_tree_spec.rb +0 -112
  326. data/spec/object_store_spec.rb +0 -170
  327. data/spec/outline_spec.rb +0 -448
  328. data/spec/pdf_object_spec.rb +0 -172
  329. data/spec/png_spec.rb +0 -240
  330. data/spec/reference_spec.rb +0 -82
  331. data/spec/repeater_spec.rb +0 -158
  332. data/spec/security_spec.rb +0 -158
  333. data/spec/snapshot_spec.rb +0 -186
  334. data/spec/soft_mask_spec.rb +0 -117
  335. data/spec/span_spec.rb +0 -44
  336. data/spec/stamp_spec.rb +0 -158
  337. data/spec/stream_spec.rb +0 -58
  338. data/spec/stroke_styles_spec.rb +0 -211
  339. data/spec/table/span_dummy_spec.rb +0 -17
  340. data/spec/table_spec.rb +0 -1355
  341. data/spec/template_spec.rb +0 -351
  342. data/spec/text_at_spec.rb +0 -130
  343. data/spec/text_box_spec.rb +0 -1030
  344. data/spec/text_rendering_mode_spec.rb +0 -45
  345. data/spec/text_spacing_spec.rb +0 -93
  346. data/spec/text_spec.rb +0 -425
  347. data/spec/text_with_inline_formatting_spec.rb +0 -35
  348. data/spec/transparency_spec.rb +0 -89
@@ -1,11 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ # grid.rb: Provides a basic grid layout system for Prawn
4
+ #
5
+ # Contributed by Andrew O'Brien in March 2009
6
+ #
7
+ # This is free software. Please see the LICENSE and COPYING files for details.
8
+
1
9
  module Prawn
2
10
  class Document
11
+ # @group Experimental API
3
12
 
4
13
  # Defines the grid system for a particular document. Takes the number of
5
14
  # rows and columns and the width to use for the gutter as the
6
15
  # keys :rows, :columns, :gutter, :row_gutter, :column_gutter
7
16
  #
17
+ # Note that a completely new grid object is built each time define_grid()
18
+ # is called. This means that all subsequent calls to grid() will use
19
+ # the newly defined Grid object -- grids are not nestable like
20
+ # bounding boxes are.
21
+
8
22
  def define_grid(options = {})
23
+ @boxes = nil
9
24
  @grid = Grid.new(self, options)
10
25
  end
11
26
 
@@ -13,36 +28,43 @@ module Prawn
13
28
  # or work with the grid system directly.
14
29
  #
15
30
  # @pdf.grid # Get the Grid directly
16
- # @pdf.grid([0,1]) # Get the box at [0,1]
31
+ # @pdf.grid([0,1]) # Get the GridBox at [0,1]
17
32
  # @pdf.grid([0,1], [1,2]) # Get a multi-box spanning from [0,1] to [1,2]
18
33
  #
19
34
  def grid(*args)
20
35
  @boxes ||= {}
21
- @boxes[args] ||= if args.empty?
22
- @grid
23
- else
24
- g1, g2 = args
25
- if(g1.class == Array && g2.class == Array &&
26
- g1.length == 2 && g2.length == 2)
27
- multi_box(single_box(*g1), single_box(*g2))
28
- else
29
- single_box(g1, g2)
36
+ @boxes[args] ||=
37
+ begin
38
+ if args.empty?
39
+ @grid
40
+ else
41
+ g1, g2 = args
42
+
43
+ if g1.is_a?(Array) && g2.is_a?(Array) &&
44
+ g1.length == 2 && g2.length == 2
45
+ multi_box(single_box(*g1), single_box(*g2))
46
+ else
47
+ single_box(g1, g2)
48
+ end
49
+ end
30
50
  end
31
- end
32
51
  end
33
52
 
34
53
  # A Grid represents the entire grid system of a Page and calculates
35
54
  # the column width and row height of the base box.
55
+ #
56
+ # @group Experimental API
36
57
  class Grid
37
58
  attr_reader :pdf, :columns, :rows, :gutter, :row_gutter, :column_gutter
59
+
38
60
  def initialize(pdf, options = {}) # :nodoc:
39
- valid_options = [:columns, :rows, :gutter, :row_gutter, :column_gutter]
61
+ valid_options = %i[columns rows gutter row_gutter column_gutter]
40
62
  Prawn.verify_options valid_options, options
41
63
 
42
64
  @pdf = pdf
43
65
  @columns = options[:columns]
44
66
  @rows = options[:rows]
45
- set_gutter(options)
67
+ apply_gutter(options)
46
68
  end
47
69
 
48
70
  # Calculates the base width of boxes.
@@ -52,14 +74,14 @@ module Prawn
52
74
 
53
75
  # Calculates the base height of boxes.
54
76
  def row_height
55
- @row_height ||= subdivide(pdf.bounds.height, rows, row_gutter)
77
+ @row_height ||= subdivide(pdf.bounds.height, rows, row_gutter)
56
78
  end
57
79
 
58
80
  # Diagnostic tool to show all of the grids. Defaults to gray.
59
- def show_all(color = "CCCCCC")
60
- self.rows.times do |i|
61
- self.columns.times do |j|
62
- pdf.grid(i,j).show(color)
81
+ def show_all(color = 'CCCCCC')
82
+ rows.times do |row|
83
+ columns.times do |column|
84
+ pdf.grid(row, column).show(color)
63
85
  end
64
86
  end
65
87
  end
@@ -70,12 +92,13 @@ module Prawn
70
92
  (total.to_f - (gutter * (num - 1).to_f)) / num.to_f
71
93
  end
72
94
 
73
- def set_gutter(options)
74
- if options.has_key?(:gutter)
95
+ def apply_gutter(options)
96
+ if options.key?(:gutter)
75
97
  @gutter = options[:gutter].to_f
76
- @row_gutter, @column_gutter = @gutter, @gutter
98
+ @row_gutter = @gutter
99
+ @column_gutter = @gutter
77
100
  else
78
- @row_gutter = options[:row_gutter].to_f
101
+ @row_gutter = options[:row_gutter].to_f
79
102
  @column_gutter = options[:column_gutter].to_f
80
103
  @gutter = 0
81
104
  end
@@ -86,20 +109,21 @@ module Prawn
86
109
  # A Grid object has methods that allow easy access to the coordinates of
87
110
  # its corners, which can be plugged into most existing prawnmethods.
88
111
  #
89
- class Box #:nodoc:
112
+ # @group Experimental API
113
+ class GridBox
90
114
  attr_reader :pdf
91
115
 
92
- def initialize(pdf, i, j)
116
+ def initialize(pdf, rows, columns)
93
117
  @pdf = pdf
94
- @i = i
95
- @j = j
118
+ @rows = rows
119
+ @columns = columns
96
120
  end
97
121
 
98
122
  # Mostly diagnostic method that outputs the name of a box as
99
123
  # col_num, row_num
100
124
  #
101
125
  def name
102
- "#{@i.to_s},#{@j.to_s}"
126
+ "#{@rows},#{@columns}"
103
127
  end
104
128
 
105
129
  # :nodoc
@@ -124,7 +148,7 @@ module Prawn
124
148
 
125
149
  # x-coordinate of left side
126
150
  def left
127
- @left ||= (width + grid.column_gutter) * @j.to_f
151
+ @left ||= (width + grid.column_gutter) * @columns.to_f
128
152
  end
129
153
 
130
154
  # x-coordinate of right side
@@ -134,7 +158,7 @@ module Prawn
134
158
 
135
159
  # y-coordinate of the top
136
160
  def top
137
- @top ||= total_height - ((height + grid.row_gutter) * @i.to_f)
161
+ @top ||= total_height - ((height + grid.row_gutter) * @rows.to_f)
138
162
  end
139
163
 
140
164
  # y-coordinate of the bottom
@@ -164,16 +188,16 @@ module Prawn
164
188
 
165
189
  # Creates a standard bounding box based on the grid box.
166
190
  def bounding_box(&blk)
167
- pdf.bounding_box(top_left, :width => width, :height => height, &blk)
191
+ pdf.bounding_box(top_left, width: width, height: height, &blk)
168
192
  end
169
193
 
170
194
  # Diagnostic method
171
- def show(grid_color = "CCCCCC")
172
- self.bounding_box do
195
+ def show(grid_color = 'CCCCCC')
196
+ bounding_box do
173
197
  original_stroke_color = pdf.stroke_color
174
198
 
175
199
  pdf.stroke_color = grid_color
176
- pdf.text self.name
200
+ pdf.text name
177
201
  pdf.stroke_bounds
178
202
 
179
203
  pdf.stroke_color = original_stroke_color
@@ -181,24 +205,29 @@ module Prawn
181
205
  end
182
206
 
183
207
  private
208
+
184
209
  def grid
185
210
  pdf.grid
186
211
  end
187
212
  end
188
213
 
189
214
  # A MultiBox is specified by 2 Boxes and spans the areas between.
190
- class MultiBox < Box #:nodoc:
191
- def initialize(pdf, b1, b2)
215
+ #
216
+ # @group Experimental API
217
+ class MultiBox
218
+ def initialize(pdf, box1, box2)
192
219
  @pdf = pdf
193
- @bs = [b1, b2]
220
+ @boxes = [box1, box2]
194
221
  end
195
222
 
223
+ attr_reader :pdf
224
+
196
225
  def name
197
- @bs.map {|b| b.name}.join(":")
226
+ @boxes.map(&:name).join(':')
198
227
  end
199
228
 
200
229
  def total_height
201
- @bs[0].total_height
230
+ @boxes[0].total_height
202
231
  end
203
232
 
204
233
  def width
@@ -210,7 +239,7 @@ module Prawn
210
239
  end
211
240
 
212
241
  def gutter
213
- @bs[0].gutter
242
+ @boxes[0].gutter
214
243
  end
215
244
 
216
245
  def left
@@ -229,31 +258,65 @@ module Prawn
229
258
  bottom_box.bottom
230
259
  end
231
260
 
261
+ def top_left
262
+ [left, top]
263
+ end
264
+
265
+ def top_right
266
+ [right, top]
267
+ end
268
+
269
+ def bottom_left
270
+ [left, bottom]
271
+ end
272
+
273
+ def bottom_right
274
+ [right, bottom]
275
+ end
276
+
277
+ def bounding_box(&blk)
278
+ pdf.bounding_box(top_left, width: width, height: height, &blk)
279
+ end
280
+
281
+ def show(grid_color = 'CCCCCC')
282
+ bounding_box do
283
+ original_stroke_color = pdf.stroke_color
284
+
285
+ pdf.stroke_color = grid_color
286
+ pdf.text name
287
+ pdf.stroke_bounds
288
+
289
+ pdf.stroke_color = original_stroke_color
290
+ end
291
+ end
292
+
232
293
  private
294
+
233
295
  def left_box
234
- @left_box ||= @bs.min {|a,b| a.left <=> b.left}
296
+ @left_box ||= @boxes.min_by(&:left)
235
297
  end
236
298
 
237
299
  def right_box
238
- @right_box ||= @bs.max {|a,b| a.right <=> b.right}
300
+ @right_box ||= @boxes.max_by(&:right)
239
301
  end
240
302
 
241
303
  def top_box
242
- @top_box ||= @bs.max {|a,b| a.top <=> b.top}
304
+ @top_box ||= @boxes.max_by(&:top)
243
305
  end
244
306
 
245
307
  def bottom_box
246
- @bottom_box ||= @bs.min {|a,b| a.bottom <=> b.bottom}
308
+ @bottom_box ||= @boxes.min_by(&:bottom)
247
309
  end
248
310
  end
249
311
 
250
312
  private
251
- def single_box(i, j)
252
- Box.new(self, i, j)
313
+
314
+ def single_box(rows, columns)
315
+ GridBox.new(self, rows, columns)
253
316
  end
254
317
 
255
- def multi_box(b1, b2)
256
- MultiBox.new(self, b1, b2)
318
+ def multi_box(box1, box2)
319
+ MultiBox.new(self, box1, box2)
257
320
  end
258
321
  end
259
322
  end
@@ -1,4 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ # ImageHandler provides a way to register image processors with Prawn
4
+ #
5
+ # Contributed by Evan Sharp in November 2013.
6
+ #
7
+ # This is free software. Please see the LICENSE and COPYING files for details.
8
+
1
9
  module Prawn
10
+ # @group Extension API
11
+
2
12
  def self.image_handler
3
13
  @image_handler ||= ImageHandler.new
4
14
  end
@@ -18,13 +28,17 @@ module Prawn
18
28
  @handlers.unshift handler
19
29
  end
20
30
 
31
+ def unregister(handler)
32
+ @handlers.reject! { |h| h == handler }
33
+ end
34
+
21
35
  def find(image_blob)
22
- handler = @handlers.find{ |h| h.can_render? image_blob }
36
+ handler = @handlers.find { |h| h.can_render? image_blob }
23
37
 
24
38
  return handler if handler
25
39
 
26
40
  raise Prawn::Errors::UnsupportedImageType,
27
- "image file is an unrecognised format"
41
+ 'image file is an unrecognised format'
28
42
  end
29
43
  end
30
44
  end
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  # image.rb : Base class for image info objects
3
4
  #
4
5
  # Copyright September 2011, Brad Ediger. All rights reserved.
@@ -10,6 +11,7 @@ require 'digest/sha1'
10
11
  module Prawn
11
12
  module Images
12
13
  class Image
14
+ # @group Extension API
13
15
 
14
16
  def calc_image_dimensions(options)
15
17
  w = options[:width] || width
@@ -41,7 +43,7 @@ module Prawn
41
43
  self.scaled_width = w
42
44
  self.scaled_height = h
43
45
 
44
- [w,h]
46
+ [w, h]
45
47
  end
46
48
  end
47
49
  end
@@ -1,5 +1,7 @@
1
1
  # encoding: ASCII-8BIT
2
2
 
3
+ # frozen_string_literal: true
4
+
3
5
  # jpg.rb : Extracts the data from a JPG that is needed for embedding
4
6
  #
5
7
  # Copyright April 2008, James Healy. All Rights Reserved.
@@ -14,14 +16,20 @@ module Prawn
14
16
  # of a JPG image that we need to embed them in a PDF
15
17
  #
16
18
  class JPG < Image
19
+ class FormatError < StandardError; end
20
+
21
+ # @group Extension API
22
+
17
23
  attr_reader :width, :height, :bits, :channels
18
24
  attr_accessor :scaled_width, :scaled_height
19
25
 
20
- JPEG_SOF_BLOCKS = %W(\xc0 \xc1 \xc2 \xc3 \xc5 \xc6 \xc7 \xc9 \xca \xcb \xcd \xce \xcf)
21
- JPEG_APP_BLOCKS = %W(\xe0 \xe1 \xe2 \xe3 \xe4 \xe5 \xe6 \xe7 \xe8 \xe9 \xea \xeb \xec \xed \xee \xef)
26
+ JPEG_SOF_BLOCKS = [
27
+ 0xC0, 0xC1, 0xC2, 0xC3, 0xC5, 0xC6, 0xC7, 0xC9, 0xCA, 0xCB, 0xCD, 0xCE,
28
+ 0xCF
29
+ ].freeze
22
30
 
23
31
  def self.can_render?(image_blob)
24
- image_blob[0, 3].unpack("C*") == [255, 216, 255]
32
+ image_blob[0, 3].unpack('C*') == [255, 216, 255]
25
33
  end
26
34
 
27
35
  # Process a new JPG image
@@ -29,22 +37,23 @@ module Prawn
29
37
  # <tt>:data</tt>:: A binary string of JPEG data
30
38
  #
31
39
  def initialize(data)
32
- @data = data.dup
33
- ruby_19 { data.force_encoding("binary") }
34
- data = StringIO.new(data)
40
+ super()
41
+ @data = data
42
+ d = StringIO.new(@data)
43
+ d.binmode
35
44
 
36
- c_marker = "\xff" # Section marker.
37
- data.read(2) # Skip the first two bytes of JPEG identifier.
45
+ c_marker = 0xff # Section marker.
46
+ d.seek(2) # Skip the first two bytes of JPEG identifier.
38
47
  loop do
39
- marker, code, length = data.read(4).unpack('aan')
40
- raise "JPEG marker not found!" if marker != c_marker
48
+ marker, code, length = d.read(4).unpack('CCn')
49
+ raise FormatError, 'JPEG marker not found!' if marker != c_marker
41
50
 
42
51
  if JPEG_SOF_BLOCKS.include?(code)
43
- @bits, @height, @width, @channels = data.read(6).unpack("CnnC")
52
+ @bits, @height, @width, @channels = d.read(6).unpack('CnnC')
44
53
  break
45
54
  end
46
55
 
47
- data.read(length - 2)
56
+ d.seek(length - 2, IO::SEEK_CUR)
48
57
  end
49
58
  end
50
59
 
@@ -52,38 +61,38 @@ module Prawn
52
61
  # a Reference to it.
53
62
  #
54
63
  def build_pdf_object(document)
55
- color_space = case channels
56
- when 1
57
- :DeviceGray
58
- when 3
59
- :DeviceRGB
60
- when 4
61
- :DeviceCMYK
62
- else
63
- raise ArgumentError, 'JPG uses an unsupported number of channels'
64
- end
64
+ color_space =
65
+ case channels
66
+ when 1
67
+ :DeviceGray
68
+ when 3
69
+ :DeviceRGB
70
+ when 4
71
+ :DeviceCMYK
72
+ else
73
+ raise ArgumentError, 'JPG uses an unsupported number of channels'
74
+ end
65
75
 
66
76
  obj = document.ref!(
67
- :Type => :XObject,
68
- :Subtype => :Image,
69
- :ColorSpace => color_space,
70
- :BitsPerComponent => bits,
71
- :Width => width,
72
- :Height => height
77
+ Type: :XObject,
78
+ Subtype: :Image,
79
+ ColorSpace: color_space,
80
+ BitsPerComponent: bits,
81
+ Width: width,
82
+ Height: height
73
83
  )
74
84
 
75
85
  # add extra decode params for CMYK images. By swapping the
76
86
  # min and max values from the default, we invert the colours. See
77
87
  # section 4.8.4 of the spec.
78
88
  if color_space == :DeviceCMYK
79
- obj.data[:Decode] = [ 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 ]
89
+ obj.data[:Decode] = [1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0]
80
90
  end
81
91
 
82
92
  obj.stream << @data
83
93
  obj.stream.filters << :DCTDecode
84
94
  obj
85
95
  end
86
-
87
96
  end
88
97
  end
89
98
  end