prawn 0.11.1.pre → 0.11.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (204) hide show
  1. data/COPYING +2 -340
  2. data/HACKING +1 -1
  3. data/LICENSE +3 -3
  4. data/Rakefile +17 -6
  5. data/data/encodings/win_ansi.txt +1 -1
  6. data/data/images/prawn.png +0 -0
  7. data/data/pdfs/form.pdf +820 -0
  8. data/data/pdfs/multipage_template.pdf +127 -0
  9. data/examples/bounding_box/bounding_boxes.rb +4 -3
  10. data/examples/bounding_box/indentation.rb +2 -1
  11. data/examples/bounding_box/russian_boxes.rb +3 -2
  12. data/examples/bounding_box/stretched_nesting.rb +2 -1
  13. data/examples/general/background.rb +2 -1
  14. data/examples/general/canvas.rb +2 -1
  15. data/examples/general/context_sensitive_headers.rb +2 -1
  16. data/examples/general/float.rb +2 -1
  17. data/examples/general/margin.rb +2 -1
  18. data/examples/general/measurement_units.rb +2 -1
  19. data/examples/general/metadata-info.rb +2 -1
  20. data/examples/general/multi_page_layout.rb +2 -1
  21. data/examples/general/outlines.rb +2 -1
  22. data/examples/general/page_geometry.rb +2 -1
  23. data/examples/general/page_numbering.rb +27 -2
  24. data/examples/general/page_templates.rb +20 -0
  25. data/examples/general/repeaters.rb +2 -1
  26. data/examples/general/stamp.rb +4 -3
  27. data/examples/general/templates.rb +2 -1
  28. data/examples/graphics/basic_images.rb +2 -1
  29. data/examples/graphics/cmyk.rb +2 -1
  30. data/examples/graphics/curves.rb +4 -3
  31. data/examples/graphics/gradient.rb +23 -0
  32. data/examples/graphics/hexagon.rb +3 -2
  33. data/examples/graphics/image_fit.rb +3 -2
  34. data/examples/graphics/image_flow.rb +2 -1
  35. data/examples/graphics/image_position.rb +3 -2
  36. data/examples/graphics/line.rb +2 -1
  37. data/examples/graphics/png_types.rb +3 -2
  38. data/examples/graphics/polygons.rb +3 -2
  39. data/examples/graphics/remote_images.rb +2 -1
  40. data/examples/graphics/rounded_polygons.rb +2 -1
  41. data/examples/graphics/rounded_rectangle.rb +2 -1
  42. data/examples/graphics/ruport_style_helpers.rb +3 -2
  43. data/examples/graphics/stroke_bounds.rb +2 -1
  44. data/examples/graphics/stroke_cap_and_join.rb +2 -1
  45. data/examples/graphics/stroke_dash.rb +2 -1
  46. data/examples/graphics/transformations.rb +2 -1
  47. data/examples/graphics/transparency.rb +4 -3
  48. data/examples/grid/bounding_boxes.rb +2 -1
  49. data/examples/grid/column_gutter_grid.rb +2 -1
  50. data/examples/grid/multi_boxes.rb +2 -1
  51. data/examples/grid/show_grid.rb +2 -1
  52. data/examples/grid/simple_grid.rb +2 -1
  53. data/examples/m17n/chinese_text_wrapping.rb +2 -1
  54. data/examples/m17n/euro.rb +3 -2
  55. data/examples/m17n/full_win_ansi_character_list.rb +20 -0
  56. data/examples/m17n/sjis.rb +2 -1
  57. data/examples/m17n/utf8.rb +3 -2
  58. data/examples/m17n/win_ansi_charset.rb +2 -1
  59. data/examples/security/hello_foo.rb +2 -1
  60. data/examples/table/bill.rb +2 -1
  61. data/examples/table/borders.rb +25 -0
  62. data/examples/table/cell.rb +3 -2
  63. data/examples/table/checkerboard.rb +2 -1
  64. data/examples/table/header.rb +3 -2
  65. data/examples/table/inline_format_table.rb +2 -1
  66. data/examples/table/multi_page_table.rb +2 -1
  67. data/examples/table/simple_table.rb +2 -1
  68. data/examples/table/subtable.rb +2 -1
  69. data/examples/table/widths.rb +2 -1
  70. data/examples/text/alignment.rb +2 -1
  71. data/examples/text/character_spacing.rb +2 -1
  72. data/examples/text/dfont.rb +2 -1
  73. data/examples/text/family_based_styling.rb +3 -2
  74. data/examples/text/font_calculations.rb +2 -1
  75. data/examples/text/font_size.rb +2 -1
  76. data/examples/text/hyphenation.rb +2 -2
  77. data/examples/text/indent_paragraphs.rb +7 -5
  78. data/examples/text/inline_format.rb +7 -6
  79. data/examples/text/kerning.rb +2 -1
  80. data/examples/text/rendering_mode.rb +21 -0
  81. data/examples/text/rotated.rb +2 -1
  82. data/examples/text/shaped_text_box.rb +2 -1
  83. data/examples/text/simple_text.rb +2 -1
  84. data/examples/text/simple_text_ttf.rb +2 -1
  85. data/examples/text/span.rb +3 -2
  86. data/examples/text/text_box.rb +7 -5
  87. data/examples/text/text_box_returning_excess.rb +4 -3
  88. data/examples/text/text_flow.rb +2 -1
  89. data/lib/prawn.rb +1 -1
  90. data/lib/prawn/core/object_store.rb +42 -14
  91. data/lib/prawn/core/page.rb +22 -8
  92. data/lib/prawn/core/text.rb +141 -13
  93. data/lib/prawn/core/text/formatted/arranger.rb +39 -12
  94. data/lib/prawn/core/text/formatted/line_wrap.rb +205 -60
  95. data/lib/prawn/core/text/formatted/wrap.rb +72 -35
  96. data/lib/prawn/document.rb +174 -70
  97. data/lib/prawn/document/bounding_box.rb +122 -83
  98. data/lib/prawn/document/column_box.rb +113 -0
  99. data/lib/prawn/document/graphics_state.rb +90 -2
  100. data/lib/prawn/document/internals.rb +5 -3
  101. data/lib/prawn/errors.rb +5 -0
  102. data/lib/prawn/font.rb +4 -4
  103. data/lib/prawn/font/afm.rb +11 -0
  104. data/lib/prawn/font/ttf.rb +5 -0
  105. data/lib/prawn/graphics.rb +77 -14
  106. data/lib/prawn/graphics/cap_style.rb +13 -5
  107. data/lib/prawn/graphics/color.rb +54 -35
  108. data/lib/prawn/graphics/dash.rb +27 -16
  109. data/lib/prawn/graphics/gradient.rb +84 -0
  110. data/lib/prawn/graphics/join_style.rb +12 -3
  111. data/lib/prawn/graphics/transparency.rb +4 -4
  112. data/lib/prawn/images.rb +18 -160
  113. data/lib/prawn/images/jpg.rb +39 -0
  114. data/lib/prawn/images/png.rb +130 -0
  115. data/lib/prawn/repeater.rb +6 -13
  116. data/lib/prawn/security.rb +6 -1
  117. data/lib/prawn/stamp.rb +12 -4
  118. data/lib/prawn/table.rb +36 -4
  119. data/lib/prawn/table/cell.rb +224 -63
  120. data/lib/prawn/table/cell/text.rb +20 -10
  121. data/lib/prawn/table/cells.rb +23 -6
  122. data/lib/prawn/text.rb +54 -91
  123. data/lib/prawn/text/box.rb +29 -283
  124. data/lib/prawn/text/formatted/box.rb +349 -24
  125. data/lib/prawn/text/formatted/fragment.rb +63 -2
  126. data/lib/prawn/text/formatted/parser.rb +2 -1
  127. data/prawn.gemspec +21 -5
  128. data/spec/bounding_box_spec.rb +61 -28
  129. data/spec/cell_spec.rb +168 -30
  130. data/spec/document_spec.rb +187 -3
  131. data/spec/extensions/mocha.rb +45 -0
  132. data/spec/font_spec.rb +32 -1
  133. data/spec/formatted_text_arranger_spec.rb +35 -40
  134. data/spec/formatted_text_box_spec.rb +287 -443
  135. data/spec/formatted_text_fragment_spec.rb +87 -0
  136. data/spec/graphics_spec.rb +128 -12
  137. data/spec/grid_spec.rb +1 -1
  138. data/spec/images_spec.rb +11 -3
  139. data/spec/inline_formatted_text_parser_spec.rb +8 -0
  140. data/spec/line_wrap_spec.rb +200 -208
  141. data/spec/object_store_spec.rb +10 -0
  142. data/spec/outline_spec.rb +7 -3
  143. data/spec/repeater_spec.rb +58 -10
  144. data/spec/security_spec.rb +6 -0
  145. data/spec/spec_helper.rb +12 -8
  146. data/spec/stamp_spec.rb +52 -1
  147. data/spec/stroke_styles_spec.rb +30 -0
  148. data/spec/table_spec.rb +93 -3
  149. data/spec/template_spec.rb +132 -6
  150. data/spec/text_at_spec.rb +14 -4
  151. data/spec/text_box_spec.rb +309 -70
  152. data/spec/text_rendering_mode_spec.rb +45 -0
  153. data/spec/text_spec.rb +60 -17
  154. data/spec/text_with_inline_formatting_spec.rb +4 -162
  155. metadata +241 -241
  156. data/lib/prawn/core/text/line_wrap.rb +0 -211
  157. data/lib/prawn/core/text/wrap.rb +0 -82
  158. data/vendor/pdf-inspector/README +0 -18
  159. data/vendor/pdf-inspector/lib/pdf/inspector.rb +0 -26
  160. data/vendor/pdf-inspector/lib/pdf/inspector/extgstate.rb +0 -18
  161. data/vendor/pdf-inspector/lib/pdf/inspector/graphics.rb +0 -131
  162. data/vendor/pdf-inspector/lib/pdf/inspector/page.rb +0 -25
  163. data/vendor/pdf-inspector/lib/pdf/inspector/text.rb +0 -46
  164. data/vendor/pdf-inspector/lib/pdf/inspector/xobject.rb +0 -19
  165. data/vendor/ttfunk/data/fonts/DejaVuSans.ttf +0 -0
  166. data/vendor/ttfunk/data/fonts/comicsans.ttf +0 -0
  167. data/vendor/ttfunk/example.rb +0 -45
  168. data/vendor/ttfunk/lib/ttfunk.rb +0 -102
  169. data/vendor/ttfunk/lib/ttfunk/directory.rb +0 -17
  170. data/vendor/ttfunk/lib/ttfunk/encoding/mac_roman.rb +0 -88
  171. data/vendor/ttfunk/lib/ttfunk/encoding/windows_1252.rb +0 -69
  172. data/vendor/ttfunk/lib/ttfunk/reader.rb +0 -44
  173. data/vendor/ttfunk/lib/ttfunk/resource_file.rb +0 -78
  174. data/vendor/ttfunk/lib/ttfunk/subset.rb +0 -18
  175. data/vendor/ttfunk/lib/ttfunk/subset/base.rb +0 -141
  176. data/vendor/ttfunk/lib/ttfunk/subset/mac_roman.rb +0 -50
  177. data/vendor/ttfunk/lib/ttfunk/subset/unicode.rb +0 -48
  178. data/vendor/ttfunk/lib/ttfunk/subset/unicode_8bit.rb +0 -63
  179. data/vendor/ttfunk/lib/ttfunk/subset/windows_1252.rb +0 -55
  180. data/vendor/ttfunk/lib/ttfunk/subset_collection.rb +0 -72
  181. data/vendor/ttfunk/lib/ttfunk/table.rb +0 -46
  182. data/vendor/ttfunk/lib/ttfunk/table/cmap.rb +0 -34
  183. data/vendor/ttfunk/lib/ttfunk/table/cmap/format00.rb +0 -54
  184. data/vendor/ttfunk/lib/ttfunk/table/cmap/format04.rb +0 -126
  185. data/vendor/ttfunk/lib/ttfunk/table/cmap/subtable.rb +0 -79
  186. data/vendor/ttfunk/lib/ttfunk/table/glyf.rb +0 -64
  187. data/vendor/ttfunk/lib/ttfunk/table/glyf/compound.rb +0 -81
  188. data/vendor/ttfunk/lib/ttfunk/table/glyf/simple.rb +0 -37
  189. data/vendor/ttfunk/lib/ttfunk/table/head.rb +0 -44
  190. data/vendor/ttfunk/lib/ttfunk/table/hhea.rb +0 -41
  191. data/vendor/ttfunk/lib/ttfunk/table/hmtx.rb +0 -47
  192. data/vendor/ttfunk/lib/ttfunk/table/kern.rb +0 -79
  193. data/vendor/ttfunk/lib/ttfunk/table/kern/format0.rb +0 -62
  194. data/vendor/ttfunk/lib/ttfunk/table/loca.rb +0 -43
  195. data/vendor/ttfunk/lib/ttfunk/table/maxp.rb +0 -40
  196. data/vendor/ttfunk/lib/ttfunk/table/name.rb +0 -125
  197. data/vendor/ttfunk/lib/ttfunk/table/os2.rb +0 -78
  198. data/vendor/ttfunk/lib/ttfunk/table/post.rb +0 -91
  199. data/vendor/ttfunk/lib/ttfunk/table/post/format10.rb +0 -43
  200. data/vendor/ttfunk/lib/ttfunk/table/post/format20.rb +0 -35
  201. data/vendor/ttfunk/lib/ttfunk/table/post/format25.rb +0 -23
  202. data/vendor/ttfunk/lib/ttfunk/table/post/format30.rb +0 -17
  203. data/vendor/ttfunk/lib/ttfunk/table/post/format40.rb +0 -17
  204. data/vendor/ttfunk/lib/ttfunk/table/simple.rb +0 -14
@@ -28,10 +28,10 @@ module Prawn
28
28
  #
29
29
  # dash units are in PDF points ( 1/72 in )
30
30
  #
31
- def dash(length=nil, options={})
32
- return @dash || undash_hash if length.nil?
31
+ def dash(length=nil, options={})
32
+ return current_dash_state || undash_hash if length.nil?
33
33
 
34
- @dash = { :dash => length,
34
+ self.current_dash_state = { :dash => length,
35
35
  :space => options[:space] || length,
36
36
  :phase => options[:phase] || 0 }
37
37
 
@@ -43,29 +43,40 @@ module Prawn
43
43
  # Stops dashing, restoring solid stroked lines and curves
44
44
  #
45
45
  def undash
46
- @dash = undash_hash
46
+ self.current_dash_state = undashed_setting
47
47
  write_stroke_dash
48
48
  end
49
49
 
50
50
  # Returns when stroke is dashed, false otherwise
51
51
  #
52
52
  def dashed?
53
- dash != undash_hash
53
+ current_dash_state != undashed_setting
54
+ end
55
+
56
+ def write_stroke_dash
57
+ add_content dash_setting
54
58
  end
55
59
 
56
- private
57
-
58
- def undash_hash
60
+ private
61
+
62
+ def undashed_setting
59
63
  { :dash => nil, :space => nil, :phase => 0 }
60
64
  end
61
-
62
- def write_stroke_dash
63
- if @dash[:dash].nil?
64
- add_content "[] 0 d"
65
- return
66
- end
67
- add_content "[#{@dash[:dash]} #{@dash[:space]}] #{@dash[:phase]} d"
68
- end
65
+
66
+ private
67
+
68
+ def current_dash_state=(dash_options)
69
+ graphic_state.dash = dash_options
70
+ end
71
+
72
+ def current_dash_state
73
+ graphic_state.dash
74
+ end
75
+
76
+ def dash_setting
77
+ graphic_state.dash_setting
78
+ end
79
+
69
80
  end
70
81
  end
71
82
  end
@@ -0,0 +1,84 @@
1
+ # encoding: utf-8
2
+
3
+ # gradient.rb : Implements axial gradient
4
+ #
5
+ # Contributed by Wojciech Piekutowski. November, 2009
6
+ #
7
+ # This is free software. Please see the LICENSE and COPYING files for details.
8
+ #
9
+ module Prawn
10
+ module Graphics
11
+ module Gradient
12
+ # Sets the fill gradient from color1 to color2.
13
+ #
14
+ # It accepts CMYK and RGB colors, like #fill_color. Both colors must be
15
+ # of the same type.
16
+ #
17
+ # point, width and height define a bounding box in which the gradient
18
+ # will be rendered. For example, if you want to have page full of text
19
+ # with gradually changing color:
20
+ #
21
+ # pdf.fill_gradient [0, pdf.bounds.height], pdf.bounds.width,
22
+ # pdf.bounds.height, 'FF0000', '0000FF'
23
+ # pdf.text 'lots of text'*1000
24
+ #
25
+ # <tt>:stroke_bounds</tt> - draw gradient bounds
26
+ def fill_gradient(point, width, height, color1, color2, options = {})
27
+ set_gradient(:fill, point, width, height, color1, color2, options)
28
+ end
29
+
30
+ # Sets the stroke gradient from color1 to color2.
31
+ #
32
+ # See #fill_gradient for details.
33
+ def stroke_gradient(point, width, height, color1, color2, options = {})
34
+ set_gradient(:stroke, point, width, height, color1, color2, options)
35
+ end
36
+
37
+ private
38
+
39
+ def set_gradient(type, point, width, height, color1, color2, options)
40
+ if options[:stroke_bounds]
41
+ stroke_color 0, 0, 0, 100
42
+ stroke_rectangle point, width, height
43
+ end
44
+
45
+ if color_type(color1) != color_type(color2)
46
+ raise ArgumentError, 'both colors must be of the same type: RGB or CMYK'
47
+ end
48
+
49
+ process_color color1
50
+ process_color color2
51
+
52
+ shader = ref!({
53
+ :FunctionType => 2,
54
+ :Domain => [0.0, 1.0],
55
+ :C0 => normalize_color(color1),
56
+ :C1 => normalize_color(color2),
57
+ :N => 1,
58
+ })
59
+
60
+ shading = ref!({
61
+ :ShadingType => 2, # axial shading
62
+ :ColorSpace => color_type(color1) == :RGB ? :DeviceRGB : :DeviceCMYK,
63
+ :Coords => [0.0, 0.0, 1.0, 0.0],
64
+ :Function => shader,
65
+ :Extend => [true, true],
66
+ })
67
+
68
+ x, y = *point
69
+ shading_pattern = ref!({
70
+ :PatternType => 2, # shading pattern
71
+ :Shading => shading,
72
+ :Matrix => [0,-height, -width, 0, x, y],
73
+ })
74
+
75
+ patterns = page.resources[:Pattern] ||= {}
76
+ id = patterns.empty? ? 'SP1' : patterns.keys.sort.last.succ
77
+ patterns[id] = shading_pattern
78
+
79
+ set_color type, id, :pattern => true
80
+ end
81
+ end
82
+ end
83
+ end
84
+
@@ -19,9 +19,9 @@ module Prawn
19
19
  # throughout the document
20
20
  #
21
21
  def join_style(style=nil)
22
- return @join_style || :miter if style.nil?
22
+ return current_join_style || :miter if style.nil?
23
23
 
24
- @join_style = style
24
+ self.current_join_style = style
25
25
 
26
26
  write_stroke_join_style
27
27
  end
@@ -29,9 +29,18 @@ module Prawn
29
29
  alias_method :join_style=, :join_style
30
30
 
31
31
  private
32
+
33
+ def current_join_style
34
+ graphic_state.join_style
35
+ end
36
+
37
+ def current_join_style=(style)
38
+ graphic_state.join_style = style
39
+ end
40
+
32
41
 
33
42
  def write_stroke_join_style
34
- add_content "#{JOIN_STYLES[@join_style]} j"
43
+ add_content "#{JOIN_STYLES[current_join_style]} j"
35
44
  end
36
45
  end
37
46
  end
@@ -18,14 +18,14 @@ module Prawn
18
18
  # # both the fill and stroke will be at 50% opacity
19
19
  # pdf.transparent(0.5) do
20
20
  # pdf.text("hello world")
21
- # pdf.fill_and_stroke_circle_at([x, y], :radius => 25)
21
+ # pdf.fill_and_stroke_circle([x, y], 25)
22
22
  # end
23
23
  #
24
24
  # # the fill will be at 50% opacity, but the stroke will
25
25
  # # be at 75% opacity
26
26
  # pdf.transparent(0.5, 0.75) do
27
27
  # pdf.text("hello world")
28
- # pdf.fill_and_stroke_circle_at([x, y], :radius => 25)
28
+ # pdf.fill_and_stroke_circle([x, y], 25)
29
29
  # end
30
30
  #
31
31
  module Transparency
@@ -41,14 +41,14 @@ module Prawn
41
41
  # # both the fill and stroke will be at 50% opacity
42
42
  # pdf.transparent(0.5) do
43
43
  # pdf.text("hello world")
44
- # pdf.fill_and_stroke_circle_at([x, y], :radius => 25)
44
+ # pdf.fill_and_stroke_circle([x, y], 25)
45
45
  # end
46
46
  #
47
47
  # # the fill will be at 50% opacity, but the stroke will
48
48
  # # be at 75% opacity
49
49
  # pdf.transparent(0.5, 0.75) do
50
50
  # pdf.text("hello world")
51
- # pdf.fill_and_stroke_circle_at([x, y], :radius => 25)
51
+ # pdf.fill_and_stroke_circle([x, y], 25)
52
52
  # end
53
53
  #
54
54
  def transparent(opacity, stroke_opacity=opacity, &block)
data/lib/prawn/images.rb CHANGED
@@ -68,9 +68,9 @@ module Prawn
68
68
 
69
69
  if file.respond_to?(:read)
70
70
  image_content = file.read
71
- else
71
+ else
72
72
  raise ArgumentError, "#{file} not found" unless File.file?(file)
73
- image_content = File.binread(file)
73
+ image_content = File.binread(file)
74
74
  end
75
75
 
76
76
  image_sha1 = Digest::SHA1.hexdigest(image_content)
@@ -80,21 +80,18 @@ module Prawn
80
80
  info = image_registry[image_sha1][:info]
81
81
  image_obj = image_registry[image_sha1][:obj]
82
82
  else
83
- # build the image object and embed the raw data
84
- image_obj = case detect_image_format(image_content)
85
- when :jpg then
86
- info = Prawn::Images::JPG.new(image_content)
87
- build_jpg_object(image_content, info)
88
- when :png then
89
- info = Prawn::Images::PNG.new(image_content)
90
-
91
- # 16-bit color only supported in 1.5+ (ISO 32000-1:2008 8.9.5.1)
92
- if info.bits > 8
93
- min_version 1.5
94
- end
95
-
96
- build_png_object(image_content, info)
97
- end
83
+ # Build the image object
84
+ klass = case detect_image_format(image_content)
85
+ when :jpg then Prawn::Images::JPG
86
+ when :png then Prawn::Images::PNG
87
+ end
88
+ info = klass.new(image_content)
89
+
90
+ # Bump PDF version if the image requires it
91
+ min_version(info.min_pdf_version) if info.respond_to?(:min_pdf_version)
92
+
93
+ # Add the image to the PDF and register it in case we see it again.
94
+ image_obj = info.build_pdf_object(self)
98
95
  image_registry[image_sha1] = {:obj => image_obj, :info => info}
99
96
  end
100
97
 
@@ -164,147 +161,6 @@ module Prawn
164
161
  (self.y - h) < bounds.absolute_bottom
165
162
  end
166
163
 
167
- def build_jpg_object(data, jpg)
168
- color_space = case jpg.channels
169
- when 1
170
- :DeviceGray
171
- when 3
172
- :DeviceRGB
173
- when 4
174
- :DeviceCMYK
175
- else
176
- raise ArgumentError, 'JPG uses an unsupported number of channels'
177
- end
178
- obj = ref!(:Type => :XObject,
179
- :Subtype => :Image,
180
- :Filter => :DCTDecode,
181
- :ColorSpace => color_space,
182
- :BitsPerComponent => jpg.bits,
183
- :Width => jpg.width,
184
- :Height => jpg.height,
185
- :Length => data.size )
186
-
187
- # add extra decode params for CMYK images. By swapping the
188
- # min and max values from the default, we invert the colours. See
189
- # section 4.8.4 of the spec.
190
- if color_space == :DeviceCMYK
191
- obj.data[:Decode] = [ 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 ]
192
- end
193
-
194
- obj << data
195
- return obj
196
- end
197
-
198
- def build_png_object(data, png)
199
-
200
- if png.compression_method != 0
201
- raise Errors::UnsupportedImageType, 'PNG uses an unsupported compression method'
202
- end
203
-
204
- if png.filter_method != 0
205
- raise Errors::UnsupportedImageType, 'PNG uses an unsupported filter method'
206
- end
207
-
208
- if png.interlace_method != 0
209
- raise Errors::UnsupportedImageType, 'PNG uses unsupported interlace method'
210
- end
211
-
212
- # some PNG types store the colour and alpha channel data together,
213
- # which the PDF spec doesn't like, so split it out.
214
- png.split_alpha_channel!
215
-
216
- case png.colors
217
- when 1
218
- color = :DeviceGray
219
- when 3
220
- color = :DeviceRGB
221
- else
222
- raise Errors::UnsupportedImageType, "PNG uses an unsupported number of colors (#{png.colors})"
223
- end
224
-
225
- # build the image dict
226
- obj = ref!(:Type => :XObject,
227
- :Subtype => :Image,
228
- :Height => png.height,
229
- :Width => png.width,
230
- :BitsPerComponent => png.bits,
231
- :Length => png.img_data.size,
232
- :Filter => :FlateDecode
233
- )
234
-
235
- unless png.alpha_channel
236
- obj.data[:DecodeParms] = {:Predictor => 15,
237
- :Colors => png.colors,
238
- :BitsPerComponent => png.bits,
239
- :Columns => png.width}
240
- end
241
-
242
- # append the actual image data to the object as a stream
243
- obj << png.img_data
244
-
245
- # sort out the colours of the image
246
- if png.palette.empty?
247
- obj.data[:ColorSpace] = color
248
- else
249
- # embed the colour palette in the PDF as a object stream
250
- palette_obj = ref!(:Length => png.palette.size)
251
- palette_obj << png.palette
252
-
253
- # build the color space array for the image
254
- obj.data[:ColorSpace] = [:Indexed,
255
- :DeviceRGB,
256
- (png.palette.size / 3) -1,
257
- palette_obj]
258
- end
259
-
260
- # *************************************
261
- # add transparency data if necessary
262
- # *************************************
263
-
264
- # For PNG color types 0, 2 and 3, the transparency data is stored in
265
- # a dedicated PNG chunk, and is exposed via the transparency attribute
266
- # of the PNG class.
267
- if png.transparency[:grayscale]
268
- # Use Color Key Masking (spec section 4.8.5)
269
- # - An array with N elements, where N is two times the number of color
270
- # components.
271
- val = png.transparency[:grayscale]
272
- obj.data[:Mask] = [val, val]
273
- elsif png.transparency[:rgb]
274
- # Use Color Key Masking (spec section 4.8.5)
275
- # - An array with N elements, where N is two times the number of color
276
- # components.
277
- rgb = png.transparency[:rgb]
278
- obj.data[:Mask] = rgb.collect { |x| [x,x] }.flatten
279
- elsif png.transparency[:indexed]
280
- # TODO: broken. I was attempting to us Color Key Masking, but I think
281
- # we need to construct an SMask i think. Maybe do it inside
282
- # the PNG class, and store it in alpha_channel
283
- #obj.data[:Mask] = png.transparency[:indexed]
284
- end
285
-
286
- # For PNG color types 4 and 6, the transparency data is stored as a alpha
287
- # channel mixed in with the main image data. The PNG class seperates
288
- # it out for us and makes it available via the alpha_channel attribute
289
- if png.alpha_channel
290
- min_version 1.4
291
- smask_obj = ref!(:Type => :XObject,
292
- :Subtype => :Image,
293
- :Height => png.height,
294
- :Width => png.width,
295
- :BitsPerComponent => png.alpha_channel_bits,
296
- :Length => png.alpha_channel.size,
297
- :Filter => :FlateDecode,
298
- :ColorSpace => :DeviceGray,
299
- :Decode => [0, 1]
300
- )
301
- smask_obj << png.alpha_channel
302
- obj.data[:SMask] = smask_obj
303
- end
304
-
305
- return obj
306
- end
307
-
308
164
  def calc_image_dimensions(info, options)
309
165
  w = options[:width] || info.width
310
166
  h = options[:height] || info.height
@@ -340,9 +196,11 @@ module Prawn
340
196
  def detect_image_format(content)
341
197
  top = content[0,128]
342
198
 
343
- if top[0, 3] == "\xff\xd8\xff"
199
+ # Unpack before comparing for JPG header, so as to avoid having to worry
200
+ # about the source string encoding. We just want a byte-by-byte compare.
201
+ if top[0, 3].unpack("C*") == [255, 216, 255]
344
202
  return :jpg
345
- elsif top[0, 8] == "\x89PNG\x0d\x0a\x1a\x0a"
203
+ elsif top[0, 8].unpack("C*") == [137, 80, 78, 71, 13, 10, 26, 10]
346
204
  return :png
347
205
  else
348
206
  raise Errors::UnsupportedImageType, "image file is an unrecognised format"
@@ -25,6 +25,7 @@ module Prawn
25
25
  # <tt>:data</tt>:: A binary string of JPEG data
26
26
  #
27
27
  def initialize(data)
28
+ @data = data
28
29
  data = StringIO.new(data.dup)
29
30
 
30
31
  c_marker = "\xff" # Section marker.
@@ -41,6 +42,44 @@ module Prawn
41
42
  buffer = data.read(length - 2)
42
43
  end
43
44
  end
45
+
46
+ # Build a PDF object representing this image in +document+, and return
47
+ # a Reference to it.
48
+ #
49
+ def build_pdf_object(document)
50
+ color_space = case channels
51
+ when 1
52
+ :DeviceGray
53
+ when 3
54
+ :DeviceRGB
55
+ when 4
56
+ :DeviceCMYK
57
+ else
58
+ raise ArgumentError, 'JPG uses an unsupported number of channels'
59
+ end
60
+
61
+ obj = document.ref!(
62
+ :Type => :XObject,
63
+ :Subtype => :Image,
64
+ :Filter => :DCTDecode,
65
+ :ColorSpace => color_space,
66
+ :BitsPerComponent => bits,
67
+ :Width => width,
68
+ :Height => height,
69
+ :Length => @data.size
70
+ )
71
+
72
+ # add extra decode params for CMYK images. By swapping the
73
+ # min and max values from the default, we invert the colours. See
74
+ # section 4.8.4 of the spec.
75
+ if color_space == :DeviceCMYK
76
+ obj.data[:Decode] = [ 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 ]
77
+ end
78
+
79
+ obj << @data
80
+ obj
81
+ end
82
+
44
83
  end
45
84
  end
46
85
  end