prawn 2.3.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (203) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/lib/prawn/document/bounding_box.rb +223 -143
  4. data/lib/prawn/document/column_box.rb +61 -26
  5. data/lib/prawn/document/internals.rb +25 -16
  6. data/lib/prawn/document/span.rb +21 -18
  7. data/lib/prawn/document.rb +273 -182
  8. data/lib/prawn/encoding.rb +2 -5
  9. data/lib/prawn/errors.rb +23 -34
  10. data/lib/prawn/font.rb +254 -139
  11. data/lib/prawn/font_metric_cache.rb +18 -16
  12. data/lib/prawn/fonts/afm.rb +99 -57
  13. data/lib/prawn/fonts/dfont.rb +7 -1
  14. data/lib/prawn/fonts/otf.rb +4 -1
  15. data/lib/prawn/fonts/to_unicode_cmap.rb +151 -0
  16. data/lib/prawn/fonts/ttc.rb +7 -2
  17. data/lib/prawn/fonts/ttf.rb +345 -107
  18. data/lib/prawn/fonts.rb +14 -0
  19. data/lib/prawn/graphics/blend_mode.rb +25 -28
  20. data/lib/prawn/graphics/cap_style.rb +9 -12
  21. data/lib/prawn/graphics/color.rb +75 -50
  22. data/lib/prawn/graphics/dash.rb +45 -42
  23. data/lib/prawn/graphics/join_style.rb +18 -12
  24. data/lib/prawn/graphics/patterns.rb +239 -110
  25. data/lib/prawn/graphics/transformation.rb +51 -44
  26. data/lib/prawn/graphics/transparency.rb +16 -40
  27. data/lib/prawn/graphics.rb +370 -260
  28. data/lib/prawn/grid.rb +219 -57
  29. data/lib/prawn/image_handler.rb +27 -10
  30. data/lib/prawn/images/image.rb +8 -10
  31. data/lib/prawn/images/jpg.rb +46 -20
  32. data/lib/prawn/images/png.rb +94 -42
  33. data/lib/prawn/images.rb +70 -81
  34. data/lib/prawn/measurement_extensions.rb +39 -8
  35. data/lib/prawn/measurements.rb +60 -5
  36. data/lib/prawn/outline.rb +120 -113
  37. data/lib/prawn/repeater.rb +52 -36
  38. data/lib/prawn/security/arcfour.rb +4 -4
  39. data/lib/prawn/security.rb +106 -98
  40. data/lib/prawn/soft_mask.rb +42 -30
  41. data/lib/prawn/stamp.rb +38 -42
  42. data/lib/prawn/text/box.rb +156 -105
  43. data/lib/prawn/text/formatted/arranger.rb +121 -41
  44. data/lib/prawn/text/formatted/box.rb +239 -163
  45. data/lib/prawn/text/formatted/fragment.rb +130 -14
  46. data/lib/prawn/text/formatted/line_wrap.rb +49 -38
  47. data/lib/prawn/text/formatted/parser.rb +116 -74
  48. data/lib/prawn/text/formatted/wrap.rb +25 -26
  49. data/lib/prawn/text/formatted.rb +75 -0
  50. data/lib/prawn/text.rb +456 -211
  51. data/lib/prawn/transformation_stack.rb +29 -10
  52. data/lib/prawn/utilities.rb +13 -13
  53. data/lib/prawn/version.rb +2 -1
  54. data/lib/prawn/view.rb +69 -54
  55. data/lib/prawn.rb +24 -18
  56. data.tar.gz.sig +0 -0
  57. metadata +55 -262
  58. metadata.gz.sig +3 -4
  59. data/.yardopts +0 -10
  60. data/Gemfile +0 -5
  61. data/Rakefile +0 -54
  62. data/manual/absolute_position.pdf +0 -0
  63. data/manual/basic_concepts/adding_pages.rb +0 -26
  64. data/manual/basic_concepts/basic_concepts.rb +0 -43
  65. data/manual/basic_concepts/creation.rb +0 -38
  66. data/manual/basic_concepts/cursor.rb +0 -32
  67. data/manual/basic_concepts/measurement.rb +0 -24
  68. data/manual/basic_concepts/origin.rb +0 -37
  69. data/manual/basic_concepts/other_cursor_helpers.rb +0 -39
  70. data/manual/basic_concepts/view.rb +0 -48
  71. data/manual/bounding_box/bounding_box.rb +0 -41
  72. data/manual/bounding_box/bounds.rb +0 -48
  73. data/manual/bounding_box/canvas.rb +0 -23
  74. data/manual/bounding_box/creation.rb +0 -22
  75. data/manual/bounding_box/indentation.rb +0 -45
  76. data/manual/bounding_box/nesting.rb +0 -52
  77. data/manual/bounding_box/russian_boxes.rb +0 -40
  78. data/manual/bounding_box/stretchy.rb +0 -29
  79. data/manual/contents.rb +0 -35
  80. data/manual/cover.rb +0 -43
  81. data/manual/document_and_page_options/background.rb +0 -25
  82. data/manual/document_and_page_options/document_and_page_options.rb +0 -34
  83. data/manual/document_and_page_options/metadata.rb +0 -25
  84. data/manual/document_and_page_options/page_margins.rb +0 -36
  85. data/manual/document_and_page_options/page_size.rb +0 -34
  86. data/manual/document_and_page_options/print_scaling.rb +0 -22
  87. data/manual/example_helper.rb +0 -8
  88. data/manual/graphics/blend_mode.rb +0 -52
  89. data/manual/graphics/circle_and_ellipse.rb +0 -21
  90. data/manual/graphics/color.rb +0 -22
  91. data/manual/graphics/common_lines.rb +0 -29
  92. data/manual/graphics/fill_and_stroke.rb +0 -41
  93. data/manual/graphics/fill_rules.rb +0 -37
  94. data/manual/graphics/gradients.rb +0 -43
  95. data/manual/graphics/graphics.rb +0 -64
  96. data/manual/graphics/helper.rb +0 -27
  97. data/manual/graphics/line_width.rb +0 -36
  98. data/manual/graphics/lines_and_curves.rb +0 -40
  99. data/manual/graphics/polygon.rb +0 -27
  100. data/manual/graphics/rectangle.rb +0 -20
  101. data/manual/graphics/rotate.rb +0 -25
  102. data/manual/graphics/scale.rb +0 -42
  103. data/manual/graphics/soft_masks.rb +0 -44
  104. data/manual/graphics/stroke_cap.rb +0 -30
  105. data/manual/graphics/stroke_dash.rb +0 -47
  106. data/manual/graphics/stroke_join.rb +0 -29
  107. data/manual/graphics/translate.rb +0 -28
  108. data/manual/graphics/transparency.rb +0 -33
  109. data/manual/how_to_read_this_manual.rb +0 -39
  110. data/manual/images/absolute_position.rb +0 -22
  111. data/manual/images/fit.rb +0 -20
  112. data/manual/images/horizontal.rb +0 -24
  113. data/manual/images/images.rb +0 -41
  114. data/manual/images/plain_image.rb +0 -17
  115. data/manual/images/scale.rb +0 -21
  116. data/manual/images/vertical.rb +0 -27
  117. data/manual/images/width_and_height.rb +0 -24
  118. data/manual/layout/boxes.rb +0 -26
  119. data/manual/layout/content.rb +0 -24
  120. data/manual/layout/layout.rb +0 -27
  121. data/manual/layout/simple_grid.rb +0 -22
  122. data/manual/outline/add_subsection_to.rb +0 -60
  123. data/manual/outline/insert_section_after.rb +0 -46
  124. data/manual/outline/outline.rb +0 -33
  125. data/manual/outline/sections_and_pages.rb +0 -66
  126. data/manual/repeatable_content/alternate_page_numbering.rb +0 -36
  127. data/manual/repeatable_content/page_numbering.rb +0 -55
  128. data/manual/repeatable_content/repeatable_content.rb +0 -35
  129. data/manual/repeatable_content/repeater.rb +0 -54
  130. data/manual/repeatable_content/stamp.rb +0 -40
  131. data/manual/security/encryption.rb +0 -28
  132. data/manual/security/permissions.rb +0 -41
  133. data/manual/security/security.rb +0 -28
  134. data/manual/table.rb +0 -16
  135. data/manual/text/alignment.rb +0 -43
  136. data/manual/text/color.rb +0 -24
  137. data/manual/text/column_box.rb +0 -30
  138. data/manual/text/fallback_fonts.rb +0 -41
  139. data/manual/text/font.rb +0 -40
  140. data/manual/text/font_size.rb +0 -44
  141. data/manual/text/font_style.rb +0 -22
  142. data/manual/text/formatted_callbacks.rb +0 -65
  143. data/manual/text/formatted_text.rb +0 -58
  144. data/manual/text/free_flowing_text.rb +0 -50
  145. data/manual/text/inline.rb +0 -40
  146. data/manual/text/kerning_and_character_spacing.rb +0 -38
  147. data/manual/text/leading.rb +0 -24
  148. data/manual/text/line_wrapping.rb +0 -60
  149. data/manual/text/paragraph_indentation.rb +0 -32
  150. data/manual/text/positioned_text.rb +0 -37
  151. data/manual/text/registering_families.rb +0 -51
  152. data/manual/text/rendering_and_color.rb +0 -36
  153. data/manual/text/right_to_left_text.rb +0 -54
  154. data/manual/text/rotation.rb +0 -47
  155. data/manual/text/single_usage.rb +0 -36
  156. data/manual/text/text.rb +0 -75
  157. data/manual/text/text_box_excess.rb +0 -35
  158. data/manual/text/text_box_extensions.rb +0 -48
  159. data/manual/text/text_box_overflow.rb +0 -49
  160. data/manual/text/utf8.rb +0 -27
  161. data/manual/text/win_ansi_charset.rb +0 -62
  162. data/prawn.gemspec +0 -57
  163. data/spec/data/curves.pdf +0 -66
  164. data/spec/extensions/encoding_helpers.rb +0 -11
  165. data/spec/prawn/document/bounding_box_spec.rb +0 -546
  166. data/spec/prawn/document/column_box_spec.rb +0 -75
  167. data/spec/prawn/document/security_spec.rb +0 -176
  168. data/spec/prawn/document_annotations_spec.rb +0 -76
  169. data/spec/prawn/document_destinations_spec.rb +0 -15
  170. data/spec/prawn/document_grid_spec.rb +0 -99
  171. data/spec/prawn/document_reference_spec.rb +0 -27
  172. data/spec/prawn/document_span_spec.rb +0 -36
  173. data/spec/prawn/document_spec.rb +0 -802
  174. data/spec/prawn/font_metric_cache_spec.rb +0 -54
  175. data/spec/prawn/font_spec.rb +0 -542
  176. data/spec/prawn/graphics/blend_mode_spec.rb +0 -63
  177. data/spec/prawn/graphics/transparency_spec.rb +0 -81
  178. data/spec/prawn/graphics_spec.rb +0 -837
  179. data/spec/prawn/graphics_stroke_styles_spec.rb +0 -229
  180. data/spec/prawn/image_handler_spec.rb +0 -53
  181. data/spec/prawn/images/jpg_spec.rb +0 -20
  182. data/spec/prawn/images/png_spec.rb +0 -283
  183. data/spec/prawn/images_spec.rb +0 -224
  184. data/spec/prawn/measurements_extensions_spec.rb +0 -24
  185. data/spec/prawn/outline_spec.rb +0 -412
  186. data/spec/prawn/repeater_spec.rb +0 -165
  187. data/spec/prawn/soft_mask_spec.rb +0 -74
  188. data/spec/prawn/stamp_spec.rb +0 -172
  189. data/spec/prawn/text/box_spec.rb +0 -1112
  190. data/spec/prawn/text/formatted/arranger_spec.rb +0 -466
  191. data/spec/prawn/text/formatted/box_spec.rb +0 -846
  192. data/spec/prawn/text/formatted/fragment_spec.rb +0 -343
  193. data/spec/prawn/text/formatted/line_wrap_spec.rb +0 -494
  194. data/spec/prawn/text/formatted/parser_spec.rb +0 -697
  195. data/spec/prawn/text_draw_text_spec.rb +0 -149
  196. data/spec/prawn/text_rendering_mode_spec.rb +0 -48
  197. data/spec/prawn/text_spacing_spec.rb +0 -95
  198. data/spec/prawn/text_spec.rb +0 -603
  199. data/spec/prawn/text_with_inline_formatting_spec.rb +0 -35
  200. data/spec/prawn/transformation_stack_spec.rb +0 -66
  201. data/spec/prawn/view_spec.rb +0 -63
  202. data/spec/prawn_manual_spec.rb +0 -35
  203. data/spec/spec_helper.rb +0 -48
@@ -1,39 +1,79 @@
1
1
  # encoding: ASCII-8BIT
2
-
3
2
  # frozen_string_literal: true
4
3
 
5
- # png.rb : Extracts the data from a PNG that is needed for embedding
6
- #
7
- # Based on some similar code in PDF::Writer by Austin Ziegler
8
- #
9
- # Copyright April 2008, James Healy. All Rights Reserved.
10
- #
11
- # This is free software. Please see the LICENSE and COPYING files for details.
12
-
13
4
  require 'stringio'
14
5
  module Prawn
15
- module Images
16
- # A convenience class that wraps the logic for extracting the parts
17
- # of a PNG image that we need to embed them in a PDF
18
- #
6
+ module Images # rubocop: disable Style/Documentation
7
+ # A convenience class that wraps the logic for extracting the parts of a PNG
8
+ # image that we need to embed them in a PDF.
19
9
  class PNG < Image
20
10
  # @group Extension API
21
11
 
22
- attr_reader :palette, :img_data, :transparency
23
- attr_reader :width, :height, :bits
24
- attr_reader :color_type, :compression_method, :filter_method
25
- attr_reader :interlace_method, :alpha_channel
26
- attr_accessor :scaled_width, :scaled_height
12
+ # Palette data.
13
+ # @return [String]
14
+ attr_reader :palette
15
+
16
+ # Image data.
17
+ # @return [String]
18
+ attr_reader :img_data
19
+
20
+ # Transparency data.
21
+ # @return [Hash{Symbol => String}]
22
+ attr_reader :transparency
23
+
24
+ # Image width in pixels.
25
+ # @return [Integer]
26
+ attr_reader :width
27
+
28
+ # Image height in pixels.
29
+ # @return [Integer]
30
+ attr_reader :height
31
+
32
+ # Bits per sample or per palette index.
33
+ # @return [Integer]
34
+ attr_reader :bits
35
+
36
+ # Color type.
37
+ # @return [Integer]
38
+ attr_reader :color_type
39
+
40
+ # Compression method.
41
+ # @return [Integer]
42
+ attr_reader :compression_method
43
+
44
+ # Filter method.
45
+ # @return [Integer]
46
+ attr_reader :filter_method
27
47
 
48
+ # Interlace method.
49
+ # @return [Integer]
50
+ attr_reader :interlace_method
51
+
52
+ # Extracted alpha-channel.
53
+ # @return [String, nil]
54
+ attr_reader :alpha_channel
55
+
56
+ # Scaled width of the image in PDF points.
57
+ # @return [Number]
58
+ attr_accessor :scaled_width
59
+
60
+ # Scaled height of the image in PDF points.
61
+ # @return [Number]
62
+ attr_accessor :scaled_height
63
+
64
+ # Can this image handler process this image?
65
+ #
66
+ # @param image_blob [String]
67
+ # @return [Boolean]
28
68
  def self.can_render?(image_blob)
29
69
  image_blob[0, 8].unpack('C*') == [137, 80, 78, 71, 13, 10, 26, 10]
30
70
  end
31
71
 
32
72
  # Process a new PNG image
33
73
  #
34
- # <tt>data</tt>:: A binary string of PNG data
35
- #
74
+ # @param data [String] A binary string of PNG data.
36
75
  def initialize(data)
76
+ super()
37
77
  data = StringIO.new(data.dup)
38
78
 
39
79
  data.read(8) # Skip the default header
@@ -92,8 +132,9 @@ module Prawn
92
132
  @img_data = Zlib::Inflate.inflate(@img_data)
93
133
  end
94
134
 
95
- # number of color components to each pixel
135
+ # Number of color components to each pixel.
96
136
  #
137
+ # @return [Integer]
97
138
  def colors
98
139
  case color_type
99
140
  when 0, 3, 4
@@ -103,9 +144,11 @@ module Prawn
103
144
  end
104
145
  end
105
146
 
106
- # split the alpha channel data from the raw image data in images
107
- # where it's required.
147
+ # Split the alpha channel data from the raw image data in images where
148
+ # it's required.
108
149
  #
150
+ # @private
151
+ # @return [void]
109
152
  def split_alpha_channel!
110
153
  if alpha_channel?
111
154
  if color_type == 3
@@ -116,6 +159,9 @@ module Prawn
116
159
  end
117
160
  end
118
161
 
162
+ # Is there an alpha-channel in this image?
163
+ #
164
+ # @return [Boolean]
119
165
  def alpha_channel?
120
166
  return true if color_type == 4 || color_type == 6
121
167
  return @transparency.any? if color_type == 3
@@ -123,9 +169,11 @@ module Prawn
123
169
  false
124
170
  end
125
171
 
126
- # Build a PDF object representing this image in +document+, and return
172
+ # Build a PDF object representing this image in `document`, and return
127
173
  # a Reference to it.
128
174
  #
175
+ # @param document [Prawn::Document]
176
+ # @return [PDF::Core::Reference]
129
177
  def build_pdf_object(document)
130
178
  if compression_method != 0
131
179
  raise Errors::UnsupportedImageType,
@@ -162,7 +210,7 @@ module Prawn
162
210
  Subtype: :Image,
163
211
  Height: height,
164
212
  Width: width,
165
- BitsPerComponent: bits
213
+ BitsPerComponent: bits,
166
214
  )
167
215
 
168
216
  # append the actual image data to the object as a stream
@@ -173,8 +221,8 @@ module Prawn
173
221
  Predictor: 15,
174
222
  Colors: colors,
175
223
  BitsPerComponent: bits,
176
- Columns: width
177
- }
224
+ Columns: width,
225
+ },
178
226
  }
179
227
 
180
228
  # sort out the colours of the image
@@ -190,7 +238,7 @@ module Prawn
190
238
  :Indexed,
191
239
  :DeviceRGB,
192
240
  (palette.size / 3) - 1,
193
- palette_obj
241
+ palette_obj,
194
242
  ]
195
243
  end
196
244
 
@@ -212,12 +260,12 @@ module Prawn
212
260
  # - An array with N elements, where N is two times the number of color
213
261
  # components.
214
262
  rgb = transparency[:rgb]
215
- obj.data[:Mask] = rgb.collect { |x| [x, x] }.flatten
263
+ obj.data[:Mask] = rgb.map { |x| [x, x] }.flatten
216
264
  end
217
265
 
218
266
  # For PNG color types 4 and 6, the transparency data is stored as
219
267
  # a alpha channel mixed in with the main image data. The PNG class
220
- # seperates it out for us and makes it available via the alpha_channel
268
+ # separates it out for us and makes it available via the alpha_channel
221
269
  # attribute
222
270
  if alpha_channel?
223
271
  smask_obj = document.ref!(
@@ -227,7 +275,7 @@ module Prawn
227
275
  Width: width,
228
276
  BitsPerComponent: bits,
229
277
  ColorSpace: :DeviceGray,
230
- Decode: [0, 1]
278
+ Decode: [0, 1],
231
279
  )
232
280
  smask_obj.stream << alpha_channel
233
281
 
@@ -236,8 +284,8 @@ module Prawn
236
284
  Predictor: 15,
237
285
  Colors: 1,
238
286
  BitsPerComponent: bits,
239
- Columns: width
240
- }
287
+ Columns: width,
288
+ },
241
289
  }
242
290
  obj.data[:SMask] = smask_obj
243
291
  end
@@ -246,6 +294,8 @@ module Prawn
246
294
  end
247
295
 
248
296
  # Returns the minimum PDF version required to support this image.
297
+ #
298
+ # @return [Float]
249
299
  def min_pdf_version
250
300
  if bits > 8
251
301
  # 16-bit color only supported in 1.5+ (ISO 32000-1:2008 8.9.5.1)
@@ -264,18 +314,18 @@ module Prawn
264
314
  alpha_bytes = bits / 8
265
315
  color_bytes = colors * bits / 8
266
316
 
267
- scanline_length = (color_bytes + alpha_bytes) * width + 1
317
+ scanline_length = ((color_bytes + alpha_bytes) * width) + 1
268
318
  scanlines = @img_data.bytesize / scanline_length
269
319
  pixels = width * height
270
320
 
271
321
  data = StringIO.new(@img_data)
272
322
  data.binmode
273
323
 
274
- color_data = [0x00].pack('C') * (pixels * color_bytes + scanlines)
324
+ color_data = [0x00].pack('C') * ((pixels * color_bytes) + scanlines)
275
325
  color = StringIO.new(color_data)
276
326
  color.binmode
277
327
 
278
- @alpha_channel = [0x00].pack('C') * (pixels * alpha_bytes + scanlines)
328
+ @alpha_channel = [0x00].pack('C') * ((pixels * alpha_bytes) + scanlines)
279
329
  alpha = StringIO.new(@alpha_channel)
280
330
  alpha.binmode
281
331
 
@@ -284,12 +334,12 @@ module Prawn
284
334
 
285
335
  filter = data.getbyte
286
336
 
287
- color.putc filter
288
- alpha.putc filter
337
+ color.putc(filter)
338
+ alpha.putc(filter)
289
339
 
290
340
  width.times do
291
- color.write data.read(color_bytes)
292
- alpha.write data.read(alpha_bytes)
341
+ color.write(data.read(color_bytes))
342
+ alpha.write(data.read(alpha_bytes))
293
343
  end
294
344
  end
295
345
 
@@ -318,14 +368,16 @@ module Prawn
318
368
 
319
369
  filter = data.getbyte
320
370
 
321
- alpha.putc filter
371
+ alpha.putc(filter)
322
372
 
323
373
  width.times do
324
374
  color = data.read(1).unpack1('C')
325
- alpha.putc alpha_palette[color]
375
+ alpha.putc(alpha_palette[color])
326
376
  end
327
377
  end
328
378
  end
329
379
  end
380
+
381
+ Prawn.image_handler.register(Prawn::Images::PNG)
330
382
  end
331
383
  end
data/lib/prawn/images.rb CHANGED
@@ -1,75 +1,63 @@
1
1
  # encoding: ASCII-8BIT
2
-
3
2
  # frozen_string_literal: true
4
3
 
5
- # images.rb : Implements PDF image embedding
6
- #
7
- # Copyright April 2008, James Healy, Gregory Brown. All Rights Reserved.
8
- #
9
- # This is free software. Please see the LICENSE and COPYING files for details.
10
-
11
4
  require 'digest/sha1'
12
5
  require 'pathname'
13
6
 
14
7
  module Prawn
8
+ # PDF image embedding.
15
9
  module Images
16
10
  # @group Stable API
17
11
 
18
- # Add the image at filename to the current page. Currently only
19
- # JPG and PNG files are supported. (Note that processing PNG
20
- # images with alpha channels can be processor and memory intensive.)
12
+ # Add the image at `file` to the current page. Currently only JPG and
13
+ # PNG files are supported. (Note that processing PNG images with alpha
14
+ # channels can be processor and memory intensive.)
21
15
  #
22
- # Arguments:
23
- # <tt>file</tt>:: path to file or an object that responds to #read and
24
- # #rewind
16
+ # If only one of `:width` or `:height` are provided, the image will be
17
+ # scaled proportionally. When both are provided, the image will be stretched
18
+ # to fit the dimensions without maintaining the aspect ratio.
25
19
  #
26
- # Options:
27
- # <tt>:at</tt>:: an array [x,y] with the location of the top left corner of
28
- # the image.
29
- # <tt>:position</tt>:: One of (:left, :center, :right) or an x-offset
30
- # <tt>:vposition</tt>:: One of (:top, :center, :bottom) or an y-offset
31
- # <tt>:height</tt>:: the height of the image [actual height of the image]
32
- # <tt>:width</tt>:: the width of the image [actual width of the image]
33
- # <tt>:scale</tt>:: scale the dimensions of the image proportionally
34
- # <tt>:fit</tt>:: scale the dimensions of the image proportionally to fit
35
- # inside [width,height]
36
- #
37
- # Prawn::Document.generate("image2.pdf", :page_layout => :landscape) do
20
+ # @example
21
+ # Prawn::Document.generate("image2.pdf", page_layout: :landscape) do
38
22
  # pigs = "#{Prawn::DATADIR}/images/pigs.jpg"
39
- # image pigs, :at => [50,450], :width => 450
23
+ # image pigs, at: [50,450], width: 450
40
24
  #
41
25
  # dice = "#{Prawn::DATADIR}/images/dice.png"
42
- # image dice, :at => [50, 450], :scale => 0.75
43
- # end
44
- #
45
- # If only one of :width / :height are provided, the image will be scaled
46
- # proportionally. When both are provided, the image will be stretched to
47
- # fit the dimensions without maintaining the aspect ratio.
48
- #
49
- #
50
- # If :at is provided, the image will be place in the current page but
51
- # the text position will not be changed.
52
- #
53
- #
54
- # If instead of an explicit filename, an object with a read method is
55
- # passed as +file+, you can embed images from IO objects and things
56
- # that act like them (including Tempfiles and open-uri objects).
57
- #
58
- # require "open-uri"
59
- #
60
- # Prawn::Document.generate("remote_images.pdf") do
61
- # image open("http://prawnpdf.org/media/prawn_logo.png")
26
+ # image dice, at: [50, 450], scale: 0.75
62
27
  # end
63
28
  #
64
- # This method returns an image info object which can be used to check the
65
- # dimensions of an image object if needed.
66
- # (See also: Prawn::Images::PNG , Prawn::Images::JPG)
29
+ # @param file [String, IO]
30
+ # Path to file or an object that responds to `#read` and `#rewind`.
31
+ # @param options [Hash{Symbol => any}]
32
+ # @option options :at [Array(Number, Number)]
33
+ # The location of the top left corner of the image. If provided,
34
+ # the image will be place in the current page but the text position will
35
+ # not be changed.
36
+ # @option options :position [:left, :center, :right, Number]
37
+ # Horizontal position relative to the current bounding box.
38
+ # @option options :vposition [:topm :center, :bottom, Number]
39
+ # Vertical position relative to the current bounding box.
40
+ # @option options :height [Number] (actual height of the image)
41
+ # The height of the image.
42
+ # @option options :width [Number] (actual width of the image)
43
+ # The width of the image.
44
+ # @option options :scale [Number]
45
+ # Scale the dimensions of the image proportionally.
46
+ # @option options :fit [Array(Number, Number)]
47
+ # Scale the dimensions of the image proportionally to fit
48
+ # inside the rectangle of specified size (width, height).
49
+ # @return [Prawn::Images::Image]
50
+ # An image handler. All image handlers provided by Prawn are subclasses of
51
+ # {Prawn::Images::Image}. This object can be used to check the image
52
+ # dimensions and get other format-specific information.
67
53
  #
54
+ # @see Prawn::Images::PNG
55
+ # @see Prawn::Images::JPG
68
56
  def image(file, options = {})
69
- Prawn.verify_options %i[
70
- at position vposition height
71
- width scale fit
72
- ], options
57
+ Prawn.verify_options(
58
+ %i[at position vposition height width scale fit],
59
+ options,
60
+ )
73
61
 
74
62
  pdf_obj, info = build_image_object(file)
75
63
  embed_image(pdf_obj, info, options)
@@ -106,10 +94,9 @@ module Prawn
106
94
  [image_obj, info]
107
95
  end
108
96
 
109
- # Given a PDF image resource <tt>pdf_obj</tt> that has been added to the
110
- # page's resources and an <tt>info</tt> object (the pair returned from
111
- # build_image_object), embed the image according to the <tt>options</tt>
112
- # given.
97
+ # Given a PDF image resource `pdf_obj` that has been added to the page's
98
+ # resources and an `info` object (the pair returned from
99
+ # {build_image_object}), embed the image according to the `options` given.
113
100
  #
114
101
  # @private
115
102
  def embed_image(pdf_obj, info, options)
@@ -120,7 +107,7 @@ module Prawn
120
107
  x, y = map_to_absolute(options[:at])
121
108
  else
122
109
  x, y = image_position(w, h, options)
123
- move_text_position h
110
+ move_text_position(h)
124
111
  end
125
112
 
126
113
  # add a reference to the image object to the current page
@@ -156,29 +143,31 @@ module Prawn
156
143
  def image_position(width, height, options)
157
144
  options[:position] ||= :left
158
145
 
159
- y = case options[:vposition]
160
- when :top
161
- bounds.absolute_top
162
- when :center
163
- bounds.absolute_top - (bounds.height - height) / 2.0
164
- when :bottom
165
- bounds.absolute_bottom + height
166
- when Numeric
167
- bounds.absolute_top - options[:vposition]
168
- else
169
- determine_y_with_page_flow(height)
170
- end
171
-
172
- x = case options[:position]
173
- when :left
174
- bounds.left_side
175
- when :center
176
- bounds.left_side + (bounds.width - width) / 2.0
177
- when :right
178
- bounds.right_side - width
179
- when Numeric
180
- options[:position] + bounds.left_side
181
- end
146
+ y =
147
+ case options[:vposition]
148
+ when :top
149
+ bounds.absolute_top
150
+ when :center
151
+ bounds.absolute_top - ((bounds.height - height) / 2.0)
152
+ when :bottom
153
+ bounds.absolute_bottom + height
154
+ when Numeric
155
+ bounds.absolute_top - options[:vposition]
156
+ else
157
+ determine_y_with_page_flow(height)
158
+ end
159
+
160
+ x =
161
+ case options[:position]
162
+ when :left
163
+ bounds.left_side
164
+ when :center
165
+ bounds.left_side + ((bounds.width - width) / 2.0)
166
+ when :right
167
+ bounds.right_side - width
168
+ when Numeric
169
+ options[:position] + bounds.left_side
170
+ end
182
171
 
183
172
  [x, y]
184
173
  end
@@ -1,50 +1,81 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # measurement_extensions.rb: Core extensions for Prawn::Measurements
4
- #
5
- # Copyright December 2008, Florian Witteler. All Rights Reserved.
6
- #
7
- # This is free software. Please see the LICENSE and COPYING files for details.
8
-
9
3
  require_relative 'measurements'
10
4
 
11
5
  # @group Stable API
12
6
 
7
+ # Core extensions for {Prawn::Measurements}.
8
+ #
9
+ # This mainly enables measurements DSL.
10
+ #
11
+ # You have to explicitly require "prawn/measurement_extensions" to enable these.
12
+ #
13
+ # ```ruby
14
+ # require 'prawn/measurement_extensions'
15
+ #
16
+ # 12.mm
17
+ # 2.cm
18
+ # 0.5.in
19
+ # 4.yd + 2.ft
20
+ # ```
13
21
  class Numeric
14
22
  include Prawn::Measurements
15
- # prawns' basic unit is PostScript-Point
16
- # 72 points per inch
23
+ # Prawn's basic unit is PostScript-Point: 72 points per inch.
17
24
 
18
25
  # @group Experimental API
19
26
 
27
+ # Convert from millimeters to points.
28
+ #
29
+ # @return [Number]
20
30
  def mm
21
31
  mm2pt(self)
22
32
  end
23
33
 
34
+ # Convert from centimeters to points.
35
+ #
36
+ # @return [Number]
24
37
  def cm
25
38
  cm2pt(self)
26
39
  end
27
40
 
41
+ # Convert from decimeters to points.
42
+ #
43
+ # @return [Number]
28
44
  def dm
29
45
  dm2pt(self)
30
46
  end
31
47
 
48
+ # Convert from meters to points.
49
+ #
50
+ # @return [Number]
32
51
  def m
33
52
  m2pt(self)
34
53
  end
35
54
 
55
+ # Convert from inches to points.
56
+ #
57
+ # @return [Number]
36
58
  def in
37
59
  in2pt(self)
38
60
  end
39
61
 
62
+ # Convert from yards to points.
63
+ #
64
+ # @return [Number]
40
65
  def yd
41
66
  yd2pt(self)
42
67
  end
43
68
 
69
+ # Convert from feet to points.
70
+ #
71
+ # @return [Number]
44
72
  def ft
45
73
  ft2pt(self)
46
74
  end
47
75
 
76
+ # Convert from points to points.
77
+ #
78
+ # @return [Number]
48
79
  def pt
49
80
  pt2pt(self)
50
81
  end
@@ -1,71 +1,126 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # measurements.rb: Conversions from other measurements to PDF points
4
- #
5
- # Copyright December 2008, Florian Witteler. All Rights Reserved.
6
- #
7
-
8
3
  # rubocop: disable Naming/MethodParameterName
9
4
  module Prawn
10
5
  # @group Stable API
11
6
 
7
+ # Distance unit conversions between metric, imperial, and PDF.
12
8
  module Measurements
13
9
  # metric conversions
10
+
11
+ # Convert centimeter to millimeters.
12
+ #
13
+ # @param cm [Number]
14
+ # @return [Number]
14
15
  def cm2mm(cm)
15
16
  cm * 10
16
17
  end
17
18
 
19
+ # Convert decimeters to millimeters.
20
+ #
21
+ # @param dm [Number]
22
+ # @return [Number]
18
23
  def dm2mm(dm)
19
24
  dm * 100
20
25
  end
21
26
 
27
+ # Convert meters to millimeters.
28
+ #
29
+ # @param m [Number]
30
+ # @return [Number]
22
31
  def m2mm(m)
23
32
  m * 1000
24
33
  end
25
34
 
26
35
  # imperial conversions
27
36
  # from http://en.wikipedia.org/wiki/Imperial_units
37
+
38
+ # Convert feet to inches.
39
+ #
40
+ # @param ft [Number]
41
+ # @return [Number]
28
42
  def ft2in(ft)
29
43
  ft * 12
30
44
  end
31
45
 
46
+ # Convert yards to inches.
47
+ #
48
+ # @param yd [Number]
49
+ # @return [Number]
32
50
  def yd2in(yd)
33
51
  yd * 36
34
52
  end
35
53
 
36
54
  # PostscriptPoint-converisons
55
+
56
+ # Convert points to points. For completeness.
57
+ #
58
+ # @param pt [Number]
59
+ # @return [Number]
37
60
  def pt2pt(pt)
38
61
  pt
39
62
  end
40
63
 
64
+ # Convert inches to points.
65
+ #
66
+ # @param inch [Number]
67
+ # @return [Number]
41
68
  def in2pt(inch)
42
69
  inch * 72
43
70
  end
44
71
 
72
+ # Convert feet to points.
73
+ #
74
+ # @param ft [Number]
75
+ # @return [Number]
45
76
  def ft2pt(ft)
46
77
  in2pt(ft2in(ft))
47
78
  end
48
79
 
80
+ # Convert yards to points.
81
+ #
82
+ # @param yd [Number]
83
+ # @return [Number]
49
84
  def yd2pt(yd)
50
85
  in2pt(yd2in(yd))
51
86
  end
52
87
 
88
+ # Convert millimeters to points.
89
+ #
90
+ # @param mm [Number]
91
+ # @return [Number]
53
92
  def mm2pt(mm)
54
93
  mm * (72 / 25.4)
55
94
  end
56
95
 
96
+ # Convert centimeters to points.
97
+ #
98
+ # @param cm [Number]
99
+ # @return [Number]
57
100
  def cm2pt(cm)
58
101
  mm2pt(cm2mm(cm))
59
102
  end
60
103
 
104
+ # Convert decimeters to points.
105
+ #
106
+ # @param dm [Number]
107
+ # @return [Number]
61
108
  def dm2pt(dm)
62
109
  mm2pt(dm2mm(dm))
63
110
  end
64
111
 
112
+ # Convert meters to points.
113
+ #
114
+ # @param m [Number]
115
+ # @return [Number]
65
116
  def m2pt(m)
66
117
  mm2pt(m2mm(m))
67
118
  end
68
119
 
120
+ # Convert points to millimeters.
121
+ #
122
+ # @param pt [Number]
123
+ # @return [Number]
69
124
  def pt2mm(pt)
70
125
  pt * 1 / mm2pt(1) # (25.4 / 72)
71
126
  end