prawn 0.15.0 → 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (269) hide show
  1. data/COPYING +2 -2
  2. data/LICENSE +1 -1
  3. data/README.md +96 -0
  4. data/Rakefile +27 -30
  5. data/data/fonts/Action Man.dfont +0 -0
  6. data/data/fonts/Activa.ttf +0 -0
  7. data/data/fonts/Chalkboard.ttf +0 -0
  8. data/data/fonts/DejaVuSans.ttf +0 -0
  9. data/data/fonts/Dustismo_Roman.ttf +0 -0
  10. data/data/fonts/comicsans.ttf +0 -0
  11. data/data/fonts/gkai00mp.ttf +0 -0
  12. data/data/images/16bit.alpha +0 -0
  13. data/data/images/16bit.dat +0 -0
  14. data/data/images/dice.alpha +0 -0
  15. data/data/images/dice.dat +0 -0
  16. data/data/images/page_white_text.alpha +0 -0
  17. data/data/images/page_white_text.dat +0 -0
  18. data/data/images/rails.dat +0 -0
  19. data/data/images/rails.png +0 -0
  20. data/data/pdfs/nested_pages.pdf +13 -13
  21. data/lib/prawn.rb +21 -85
  22. data/lib/prawn/compatibility.rb +51 -0
  23. data/lib/prawn/core.rb +85 -0
  24. data/lib/prawn/core/annotations.rb +61 -0
  25. data/lib/prawn/core/byte_string.rb +9 -0
  26. data/lib/prawn/core/destinations.rb +90 -0
  27. data/lib/prawn/core/document_state.rb +78 -0
  28. data/lib/prawn/core/literal_string.rb +16 -0
  29. data/lib/prawn/core/name_tree.rb +177 -0
  30. data/lib/prawn/core/object_store.rb +264 -0
  31. data/lib/prawn/core/page.rb +215 -0
  32. data/lib/prawn/core/pdf_object.rb +108 -0
  33. data/lib/prawn/core/reference.rb +115 -0
  34. data/lib/prawn/core/text.rb +268 -0
  35. data/lib/prawn/core/text/formatted/arranger.rb +294 -0
  36. data/lib/prawn/core/text/formatted/line_wrap.rb +273 -0
  37. data/lib/prawn/core/text/formatted/wrap.rb +153 -0
  38. data/lib/prawn/document.rb +122 -155
  39. data/lib/prawn/document/bounding_box.rb +7 -36
  40. data/lib/prawn/document/column_box.rb +10 -38
  41. data/lib/prawn/document/graphics_state.rb +74 -11
  42. data/lib/prawn/document/internals.rb +23 -24
  43. data/lib/prawn/document/page_geometry.rb +136 -0
  44. data/lib/prawn/document/snapshot.rb +6 -7
  45. data/lib/prawn/document/span.rb +10 -12
  46. data/lib/prawn/encoding.rb +10 -9
  47. data/lib/prawn/errors.rb +30 -15
  48. data/lib/prawn/font.rb +104 -136
  49. data/lib/prawn/font/afm.rb +44 -46
  50. data/lib/prawn/font/dfont.rb +3 -4
  51. data/lib/prawn/font/ttf.rb +50 -31
  52. data/lib/prawn/graphics.rb +57 -302
  53. data/lib/prawn/graphics/cap_style.rb +3 -4
  54. data/lib/prawn/graphics/color.rb +5 -13
  55. data/lib/prawn/graphics/dash.rb +31 -53
  56. data/lib/prawn/graphics/gradient.rb +84 -0
  57. data/lib/prawn/graphics/join_style.rb +7 -9
  58. data/lib/prawn/graphics/transformation.rb +9 -10
  59. data/lib/prawn/graphics/transparency.rb +1 -3
  60. data/lib/prawn/images.rb +59 -69
  61. data/lib/prawn/images/image.rb +22 -6
  62. data/lib/prawn/images/jpg.rb +14 -20
  63. data/lib/prawn/images/png.rb +118 -61
  64. data/lib/prawn/layout.rb +15 -10
  65. data/lib/prawn/layout/grid.rb +54 -66
  66. data/lib/prawn/measurement_extensions.rb +6 -10
  67. data/lib/prawn/measurements.rb +21 -27
  68. data/lib/prawn/outline.rb +308 -6
  69. data/lib/prawn/repeater.rb +8 -10
  70. data/lib/prawn/security.rb +33 -55
  71. data/lib/prawn/security/arcfour.rb +0 -1
  72. data/lib/prawn/stamp.rb +3 -5
  73. data/lib/prawn/table.rb +60 -188
  74. data/lib/prawn/table/cell.rb +44 -272
  75. data/lib/prawn/table/cell/image.rb +3 -2
  76. data/lib/prawn/table/cell/in_table.rb +2 -4
  77. data/lib/prawn/table/cell/subtable.rb +2 -2
  78. data/lib/prawn/table/cell/text.rb +18 -41
  79. data/lib/prawn/table/cells.rb +48 -142
  80. data/lib/prawn/text.rb +25 -32
  81. data/lib/prawn/text/box.rb +6 -12
  82. data/lib/prawn/text/formatted.rb +4 -5
  83. data/lib/prawn/text/formatted/box.rb +59 -96
  84. data/lib/prawn/text/formatted/fragment.rb +23 -34
  85. data/lib/prawn/text/formatted/parser.rb +5 -15
  86. data/prawn.gemspec +13 -24
  87. data/spec/annotations_spec.rb +32 -16
  88. data/spec/bounding_box_spec.rb +17 -119
  89. data/spec/cell_spec.rb +42 -112
  90. data/spec/destinations_spec.rb +5 -5
  91. data/spec/document_spec.rb +111 -155
  92. data/spec/extensions/mocha.rb +0 -1
  93. data/spec/font_spec.rb +99 -149
  94. data/spec/formatted_text_arranger_spec.rb +43 -43
  95. data/spec/formatted_text_box_spec.rb +44 -43
  96. data/spec/formatted_text_fragment_spec.rb +8 -8
  97. data/spec/graphics_spec.rb +68 -151
  98. data/spec/grid_spec.rb +15 -26
  99. data/spec/images_spec.rb +30 -51
  100. data/spec/inline_formatted_text_parser_spec.rb +20 -69
  101. data/spec/jpg_spec.rb +4 -4
  102. data/spec/line_wrap_spec.rb +28 -28
  103. data/spec/measurement_units_spec.rb +6 -6
  104. data/spec/name_tree_spec.rb +112 -0
  105. data/spec/object_store_spec.rb +106 -17
  106. data/spec/outline_spec.rb +63 -103
  107. data/spec/pdf_object_spec.rb +170 -0
  108. data/spec/png_spec.rb +25 -25
  109. data/spec/reference_spec.rb +65 -8
  110. data/spec/repeater_spec.rb +10 -10
  111. data/spec/security_spec.rb +12 -44
  112. data/spec/snapshot_spec.rb +7 -7
  113. data/spec/span_spec.rb +15 -10
  114. data/spec/spec_helper.rb +8 -32
  115. data/spec/stamp_spec.rb +30 -29
  116. data/spec/stroke_styles_spec.rb +18 -36
  117. data/spec/table_spec.rb +111 -706
  118. data/spec/template_spec.rb +297 -0
  119. data/spec/text_at_spec.rb +33 -19
  120. data/spec/text_box_spec.rb +64 -100
  121. data/spec/text_rendering_mode_spec.rb +5 -5
  122. data/spec/text_spacing_spec.rb +4 -4
  123. data/spec/text_spec.rb +64 -84
  124. data/spec/transparency_spec.rb +5 -5
  125. metadata +290 -463
  126. checksums.yaml +0 -7
  127. data/.yardopts +0 -10
  128. data/Gemfile +0 -11
  129. data/data/images/16bit.color +0 -0
  130. data/data/images/dice.color +0 -0
  131. data/data/images/indexed_color.dat +0 -0
  132. data/data/images/indexed_color.png +0 -0
  133. data/data/images/page_white_text.color +0 -0
  134. data/lib/prawn/font_metric_cache.rb +0 -47
  135. data/lib/prawn/graphics/patterns.rb +0 -138
  136. data/lib/prawn/image_handler.rb +0 -36
  137. data/lib/prawn/soft_mask.rb +0 -96
  138. data/lib/prawn/table/cell/span_dummy.rb +0 -93
  139. data/lib/prawn/table/column_width_calculator.rb +0 -61
  140. data/lib/prawn/text/formatted/arranger.rb +0 -290
  141. data/lib/prawn/text/formatted/line_wrap.rb +0 -266
  142. data/lib/prawn/text/formatted/wrap.rb +0 -150
  143. data/lib/prawn/utilities.rb +0 -46
  144. data/manual/basic_concepts/adding_pages.rb +0 -27
  145. data/manual/basic_concepts/basic_concepts.rb +0 -34
  146. data/manual/basic_concepts/creation.rb +0 -39
  147. data/manual/basic_concepts/cursor.rb +0 -33
  148. data/manual/basic_concepts/measurement.rb +0 -25
  149. data/manual/basic_concepts/origin.rb +0 -38
  150. data/manual/basic_concepts/other_cursor_helpers.rb +0 -40
  151. data/manual/bounding_box/bounding_box.rb +0 -39
  152. data/manual/bounding_box/bounds.rb +0 -49
  153. data/manual/bounding_box/canvas.rb +0 -24
  154. data/manual/bounding_box/creation.rb +0 -23
  155. data/manual/bounding_box/indentation.rb +0 -46
  156. data/manual/bounding_box/nesting.rb +0 -45
  157. data/manual/bounding_box/russian_boxes.rb +0 -40
  158. data/manual/bounding_box/stretchy.rb +0 -31
  159. data/manual/document_and_page_options/background.rb +0 -27
  160. data/manual/document_and_page_options/document_and_page_options.rb +0 -32
  161. data/manual/document_and_page_options/metadata.rb +0 -23
  162. data/manual/document_and_page_options/page_margins.rb +0 -38
  163. data/manual/document_and_page_options/page_size.rb +0 -34
  164. data/manual/document_and_page_options/print_scaling.rb +0 -20
  165. data/manual/example_file.rb +0 -111
  166. data/manual/example_helper.rb +0 -411
  167. data/manual/example_package.rb +0 -53
  168. data/manual/example_section.rb +0 -46
  169. data/manual/graphics/circle_and_ellipse.rb +0 -22
  170. data/manual/graphics/color.rb +0 -24
  171. data/manual/graphics/common_lines.rb +0 -30
  172. data/manual/graphics/fill_and_stroke.rb +0 -42
  173. data/manual/graphics/fill_rules.rb +0 -37
  174. data/manual/graphics/gradients.rb +0 -37
  175. data/manual/graphics/graphics.rb +0 -58
  176. data/manual/graphics/helper.rb +0 -24
  177. data/manual/graphics/line_width.rb +0 -35
  178. data/manual/graphics/lines_and_curves.rb +0 -41
  179. data/manual/graphics/polygon.rb +0 -29
  180. data/manual/graphics/rectangle.rb +0 -21
  181. data/manual/graphics/rotate.rb +0 -28
  182. data/manual/graphics/scale.rb +0 -41
  183. data/manual/graphics/soft_masks.rb +0 -46
  184. data/manual/graphics/stroke_cap.rb +0 -31
  185. data/manual/graphics/stroke_dash.rb +0 -48
  186. data/manual/graphics/stroke_join.rb +0 -30
  187. data/manual/graphics/translate.rb +0 -29
  188. data/manual/graphics/transparency.rb +0 -35
  189. data/manual/images/absolute_position.rb +0 -23
  190. data/manual/images/fit.rb +0 -21
  191. data/manual/images/horizontal.rb +0 -25
  192. data/manual/images/images.rb +0 -40
  193. data/manual/images/plain_image.rb +0 -18
  194. data/manual/images/scale.rb +0 -22
  195. data/manual/images/vertical.rb +0 -28
  196. data/manual/images/width_and_height.rb +0 -25
  197. data/manual/layout/boxes.rb +0 -27
  198. data/manual/layout/content.rb +0 -25
  199. data/manual/layout/layout.rb +0 -28
  200. data/manual/layout/simple_grid.rb +0 -23
  201. data/manual/manual/cover.rb +0 -36
  202. data/manual/manual/foreword.rb +0 -85
  203. data/manual/manual/how_to_read_this_manual.rb +0 -41
  204. data/manual/manual/manual.rb +0 -34
  205. data/manual/outline/add_subsection_to.rb +0 -61
  206. data/manual/outline/insert_section_after.rb +0 -47
  207. data/manual/outline/outline.rb +0 -32
  208. data/manual/outline/sections_and_pages.rb +0 -67
  209. data/manual/repeatable_content/page_numbering.rb +0 -54
  210. data/manual/repeatable_content/repeatable_content.rb +0 -31
  211. data/manual/repeatable_content/repeater.rb +0 -55
  212. data/manual/repeatable_content/stamp.rb +0 -41
  213. data/manual/security/encryption.rb +0 -31
  214. data/manual/security/permissions.rb +0 -38
  215. data/manual/security/security.rb +0 -28
  216. data/manual/syntax_highlight.rb +0 -52
  217. data/manual/table/basic_block.rb +0 -53
  218. data/manual/table/before_rendering_page.rb +0 -26
  219. data/manual/table/cell_border_lines.rb +0 -24
  220. data/manual/table/cell_borders_and_bg.rb +0 -31
  221. data/manual/table/cell_dimensions.rb +0 -30
  222. data/manual/table/cell_text.rb +0 -38
  223. data/manual/table/column_widths.rb +0 -30
  224. data/manual/table/content_and_subtables.rb +0 -39
  225. data/manual/table/creation.rb +0 -27
  226. data/manual/table/filtering.rb +0 -36
  227. data/manual/table/flow_and_header.rb +0 -17
  228. data/manual/table/image_cells.rb +0 -33
  229. data/manual/table/position.rb +0 -29
  230. data/manual/table/row_colors.rb +0 -20
  231. data/manual/table/span.rb +0 -30
  232. data/manual/table/style.rb +0 -22
  233. data/manual/table/table.rb +0 -52
  234. data/manual/table/width.rb +0 -27
  235. data/manual/text/alignment.rb +0 -44
  236. data/manual/text/color.rb +0 -24
  237. data/manual/text/column_box.rb +0 -32
  238. data/manual/text/fallback_fonts.rb +0 -37
  239. data/manual/text/font.rb +0 -41
  240. data/manual/text/font_size.rb +0 -45
  241. data/manual/text/font_style.rb +0 -23
  242. data/manual/text/formatted_callbacks.rb +0 -60
  243. data/manual/text/formatted_text.rb +0 -54
  244. data/manual/text/free_flowing_text.rb +0 -51
  245. data/manual/text/group.rb +0 -31
  246. data/manual/text/inline.rb +0 -43
  247. data/manual/text/kerning_and_character_spacing.rb +0 -39
  248. data/manual/text/leading.rb +0 -25
  249. data/manual/text/line_wrapping.rb +0 -41
  250. data/manual/text/paragraph_indentation.rb +0 -26
  251. data/manual/text/positioned_text.rb +0 -38
  252. data/manual/text/registering_families.rb +0 -48
  253. data/manual/text/rendering_and_color.rb +0 -37
  254. data/manual/text/right_to_left_text.rb +0 -43
  255. data/manual/text/rotation.rb +0 -43
  256. data/manual/text/single_usage.rb +0 -37
  257. data/manual/text/text.rb +0 -75
  258. data/manual/text/text_box_excess.rb +0 -32
  259. data/manual/text/text_box_extensions.rb +0 -45
  260. data/manual/text/text_box_overflow.rb +0 -44
  261. data/manual/text/utf8.rb +0 -28
  262. data/manual/text/win_ansi_charset.rb +0 -59
  263. data/spec/acceptance/png.rb +0 -23
  264. data/spec/column_box_spec.rb +0 -65
  265. data/spec/extensions/encoding_helpers.rb +0 -9
  266. data/spec/font_metric_cache_spec.rb +0 -52
  267. data/spec/image_handler_spec.rb +0 -54
  268. data/spec/soft_mask_spec.rb +0 -117
  269. data/spec/table/span_dummy_spec.rb +0 -17
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # encoding: utf-8
2
2
 
3
3
  # cap_style.rb : Implements stroke cap styling
4
4
  #
@@ -9,7 +9,6 @@
9
9
  module Prawn
10
10
  module Graphics
11
11
  module CapStyle
12
- # @group Stable API
13
12
 
14
13
  CAP_STYLES = { :butt => 0, :round => 1, :projecting_square => 2 }
15
14
 
@@ -31,11 +30,11 @@ module Prawn
31
30
 
32
31
  private
33
32
 
34
- def current_cap_style
33
+ def current_cap_style
35
34
  graphic_state.cap_style
36
35
  end
37
36
 
38
- def current_cap_style=(style)
37
+ def current_cap_style=(style)
39
38
  graphic_state.cap_style = style
40
39
  end
41
40
 
@@ -9,7 +9,6 @@
9
9
  module Prawn
10
10
  module Graphics
11
11
  module Color
12
- # @group Stable API
13
12
 
14
13
  # Sets or returns the fill color.
15
14
  #
@@ -96,14 +95,7 @@ module Prawn
96
95
  when String
97
96
  :RGB
98
97
  when Array
99
- case color.length
100
- when 3
101
- :RGB
102
- when 4
103
- :CMYK
104
- else
105
- raise ArgumentError, "Unknown type of color: #{color.inspect}"
106
- end
98
+ :CMYK
107
99
  end
108
100
  end
109
101
 
@@ -167,7 +159,7 @@ module Prawn
167
159
  if options[:pattern]
168
160
  set_color_space type, :Pattern
169
161
  add_content "/#{color} #{operator}"
170
- else
162
+ else
171
163
  set_color_space type, color_space(color)
172
164
  color = color_to_s(color)
173
165
  write_color(color, operator)
@@ -175,7 +167,7 @@ module Prawn
175
167
  end
176
168
 
177
169
  def set_fill_color(color = nil)
178
- set_color :fill, color || current_fill_color
170
+ set_color :fill, color || current_fill_color
179
171
  end
180
172
 
181
173
  def set_stroke_color(color = nil)
@@ -193,7 +185,7 @@ module Prawn
193
185
  graphic_state.color_space[type]
194
186
  end
195
187
 
196
- def set_current_color_space(color_space, type)
188
+ def set_current_color_space(color_space, type)
197
189
  save_graphics_state if graphic_state.nil?
198
190
  graphic_state.color_space[type] = color_space
199
191
  end
@@ -202,7 +194,7 @@ module Prawn
202
194
  graphic_state.fill_color
203
195
  end
204
196
 
205
- def current_fill_color=(color)
197
+ def current_fill_color=(color)
206
198
  graphic_state.fill_color = color
207
199
  end
208
200
 
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # encoding: utf-8
2
2
 
3
3
  # dash.rb : Implements stroke dashing
4
4
  #
@@ -9,59 +9,35 @@
9
9
  module Prawn
10
10
  module Graphics
11
11
  module Dash
12
- # @group Stable API
13
12
 
14
- # Sets the dash pattern for stroked lines and curves or return the
15
- # current dash pattern setting if +length+ is nil.
13
+ # Sets the dash pattern for stroked lines and curves
16
14
  #
17
- # There are two ways to set the dash pattern:
15
+ # length is the length of the dash. If options is not present,
16
+ # or options[:space] is nil, then length is also the length of
17
+ # the space between dashes
18
18
  #
19
- # * If the parameter +length+ is an Integer/Float, it specifies
20
- # the length of the dash and of the gap. The length of the gap
21
- # can be customized by setting the :space option.
19
+ # options may contain :space and :phase
20
+ # :space is the space between the dashes
21
+ # :phase is where in the cycle to begin dashing. For
22
+ # example, a phase of 0 starts at the beginning of
23
+ # the dash; whereas, if the phase is equal to the
24
+ # length of the dash, then stroking will begin at
25
+ # the beginning of the space. Default is 0
22
26
  #
23
- # Examples:
27
+ # integers or floats may be used for length and the options
24
28
  #
25
- # length = 3
26
- # 3 on, 3 off, 3 on, 3 off, ...
27
- # length = 3, :space =2
28
- # 3 on, 2 off, 3 on, 2 off, ...
29
- #
30
- # * If the parameter +length+ is an array, it specifies the
31
- # lengths of alternating dashes and gaps. The :space option is
32
- # ignored in this case.
33
- #
34
- # Examples:
35
- #
36
- # length = [2, 1]
37
- # 2 on, 1 off, 2 on, 1 off, ...
38
- # length = [3, 1, 2, 3]
39
- # 3 on, 1 off, 2 on, 3 off, 3 on, 1 off, ...
40
- #
41
- # Options may contain the keys :space and :phase
42
- #
43
- # :space:: The space between the dashes (only used when +length+
44
- # is not an array)
45
- #
46
- # :phase:: The distance into the dash pattern at which to start
47
- # the dash. For example, a phase of 0 starts at the
48
- # beginning of the dash; whereas, if the phase is equal
49
- # to the length of the dash, then stroking will begin at
50
- # the beginning of the space. Default is 0.
51
- #
52
- # Integers or Floats may be used for length and the option values.
53
- # Dash units are in PDF points (1/72 inch).
54
- #
55
- def dash(length=nil, options={})
56
- return current_dash_state if length.nil?
29
+ # dash units are in PDF points ( 1/72 in )
30
+ #
31
+ def dash(length=nil, options={})
32
+ return current_dash_state || undash_hash if length.nil?
57
33
 
58
- self.current_dash_state = { :dash => length,
59
- :space => length.kind_of?(Array) ? nil : options[:space] || length,
34
+ self.current_dash_state = { :dash => length,
35
+ :space => options[:space] || length,
60
36
  :phase => options[:phase] || 0 }
61
37
 
62
38
  write_stroke_dash
63
39
  end
64
-
40
+
65
41
  alias_method :dash=, :dash
66
42
 
67
43
  # Stops dashing, restoring solid stroked lines and curves
@@ -70,35 +46,37 @@ module Prawn
70
46
  self.current_dash_state = undashed_setting
71
47
  write_stroke_dash
72
48
  end
73
-
49
+
74
50
  # Returns when stroke is dashed, false otherwise
75
51
  #
76
52
  def dashed?
77
53
  current_dash_state != undashed_setting
78
54
  end
79
-
80
- private
81
-
55
+
82
56
  def write_stroke_dash
83
57
  add_content dash_setting
84
58
  end
85
59
 
60
+ private
61
+
86
62
  def undashed_setting
87
63
  { :dash => nil, :space => nil, :phase => 0 }
88
64
  end
89
-
90
- def current_dash_state=(dash_options)
65
+
66
+ private
67
+
68
+ def current_dash_state=(dash_options)
91
69
  graphic_state.dash = dash_options
92
70
  end
93
-
71
+
94
72
  def current_dash_state
95
73
  graphic_state.dash
96
74
  end
97
-
75
+
98
76
  def dash_setting
99
77
  graphic_state.dash_setting
100
78
  end
101
-
79
+
102
80
  end
103
81
  end
104
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
+
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # encoding: utf-8
2
2
 
3
3
  # join_style.rb : Implements stroke join styling
4
4
  #
@@ -10,9 +10,7 @@ module Prawn
10
10
  module Graphics
11
11
  module JoinStyle
12
12
  JOIN_STYLES = { :miter => 0, :round => 1, :bevel => 2 }
13
-
14
- # @group Stable API
15
-
13
+
16
14
  # Sets the join style for stroked lines and curves
17
15
  #
18
16
  # style is one of :miter, :round, or :bevel
@@ -27,19 +25,19 @@ module Prawn
27
25
 
28
26
  write_stroke_join_style
29
27
  end
30
-
28
+
31
29
  alias_method :join_style=, :join_style
32
30
 
33
31
  private
34
-
35
- def current_join_style
32
+
33
+ def current_join_style
36
34
  graphic_state.join_style
37
35
  end
38
36
 
39
- def current_join_style=(style)
37
+ def current_join_style=(style)
40
38
  graphic_state.join_style = style
41
39
  end
42
-
40
+
43
41
 
44
42
  def write_stroke_join_style
45
43
  add_content "#{JOIN_STYLES[current_join_style]} j"
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
  #
3
3
  # transformation.rb: Implements rotate, translate, skew, scale and a generic
4
- # transformation_matrix
4
+ # transformation_matrix
5
5
  #
6
6
  # Copyright January 2010, Michael Witrant. All Rights Reserved.
7
7
  #
@@ -10,8 +10,7 @@
10
10
  module Prawn
11
11
  module Graphics
12
12
  module Transformation
13
- # @group Stable API
14
-
13
+
15
14
  # Rotate the user space. If a block is not provided, then you must save
16
15
  # and restore the graphics state yourself.
17
16
  #
@@ -23,7 +22,7 @@ module Prawn
23
22
  # provided, but no block is given
24
23
  #
25
24
  # Example without a block:
26
- #
25
+ #
27
26
  # save_graphics_state
28
27
  # rotate 30
29
28
  # text "rotated text"
@@ -63,7 +62,7 @@ module Prawn
63
62
  # and restore the graphics state yourself.
64
63
  #
65
64
  # Example without a block: move the text up and over 10
66
- #
65
+ #
67
66
  # save_graphics_state
68
67
  # translate(10, 10)
69
68
  # text "scaled text"
@@ -83,7 +82,7 @@ module Prawn
83
82
  def translate(x, y, &block)
84
83
  transformation_matrix(1, 0, 0, 1, x, y, &block)
85
84
  end
86
-
85
+
87
86
  # Scale the user space. If a block is not provided, then you must save
88
87
  # and restore the graphics state yourself.
89
88
  #
@@ -95,7 +94,7 @@ module Prawn
95
94
  # provided, but no block is given
96
95
  #
97
96
  # Example without a block:
98
- #
97
+ #
99
98
  # save_graphics_state
100
99
  # scale 1.5
101
100
  # text "scaled text"
@@ -127,7 +126,7 @@ module Prawn
127
126
  end
128
127
  end
129
128
  end
130
-
129
+
131
130
  # The following definition of skew would only work in a clearly
132
131
  # predicatable manner when if the document had no margin. don't provide
133
132
  # this shortcut until it behaves in a clearly understood manner
@@ -138,7 +137,7 @@ module Prawn
138
137
  # Math.tan(degree_to_rad(b)),
139
138
  # 1, 0, 0, &block)
140
139
  # end
141
-
140
+
142
141
  # Transform the user space (see notes for rotate regarding graphics state)
143
142
  # Generally, one would use the rotate, scale, translate, and skew
144
143
  # convenience methods instead of calling transformation_matrix directly
@@ -151,7 +150,7 @@ module Prawn
151
150
  restore_graphics_state
152
151
  end
153
152
  end
154
-
153
+
155
154
  end
156
155
  end
157
156
  end
@@ -30,8 +30,6 @@ module Prawn
30
30
  #
31
31
  module Transparency
32
32
 
33
- # @group Stable API
34
-
35
33
  # Sets the <tt>opacity</tt> and <tt>stroke_opacity</tt> for all
36
34
  # the content within the <tt>block</tt>
37
35
  # If <tt>stroke_opacity</tt> is not provided, then it takes on
@@ -88,7 +86,7 @@ module Prawn
88
86
  )
89
87
 
90
88
  dictionary_name = "Tr#{next_opacity_dictionary_id}"
91
- opacity_dictionary_registry[key] = { :name => dictionary_name,
89
+ opacity_dictionary_registry[key] = { :name => dictionary_name,
92
90
  :obj => dictionary }
93
91
  end
94
92
 
data/lib/prawn/images.rb CHANGED
@@ -6,16 +6,18 @@
6
6
  # This is free software. Please see the LICENSE and COPYING files for details.
7
7
 
8
8
  require 'digest/sha1'
9
- require 'pathname'
10
9
 
11
10
  module Prawn
12
11
 
13
12
  module Images
14
- # @group Stable API
15
13
 
16
14
  # Add the image at filename to the current page. Currently only
17
- # JPG and PNG files are supported. (Note that processing PNG
18
- # images with alpha channels can be processor and memory intensive.)
15
+ # JPG and PNG files are supported.
16
+ #
17
+ # NOTE: Prawn is very slow at rendering PNGs with alpha channels. The
18
+ # workaround for those who don't mind installing RMagick is to use:
19
+ #
20
+ # http://github.com/amberbit/prawn-fast-png
19
21
  #
20
22
  # Arguments:
21
23
  # <tt>file</tt>:: path to file or an object that responds to #read
@@ -23,22 +25,22 @@ module Prawn
23
25
  # Options:
24
26
  # <tt>:at</tt>:: an array [x,y] with the location of the top left corner of the image.
25
27
  # <tt>:position</tt>:: One of (:left, :center, :right) or an x-offset
26
- # <tt>:vposition</tt>:: One of (:top, :center, :center) or an y-offset
28
+ # <tt>:vposition</tt>:: One of (:top, :center, :center) or an y-offset
27
29
  # <tt>:height</tt>:: the height of the image [actual height of the image]
28
30
  # <tt>:width</tt>:: the width of the image [actual width of the image]
29
31
  # <tt>:scale</tt>:: scale the dimensions of the image proportionally
30
32
  # <tt>:fit</tt>:: scale the dimensions of the image proportionally to fit inside [width,height]
33
+ #
34
+ # Prawn::Document.generate("image2.pdf", :page_layout => :landscape) do
35
+ # pigs = "#{Prawn::BASEDIR}/data/images/pigs.jpg"
36
+ # image pigs, :at => [50,450], :width => 450
31
37
  #
32
- # Prawn::Document.generate("image2.pdf", :page_layout => :landscape) do
33
- # pigs = "#{Prawn::DATADIR}/images/pigs.jpg"
34
- # image pigs, :at => [50,450], :width => 450
35
- #
36
- # dice = "#{Prawn::DATADIR}/images/dice.png"
37
- # image dice, :at => [50, 450], :scale => 0.75
38
- # end
38
+ # dice = "#{Prawn::BASEDIR}/data/images/dice.png"
39
+ # image dice, :at => [50, 450], :scale => 0.75
40
+ # end
39
41
  #
40
42
  # If only one of :width / :height are provided, the image will be scaled
41
- # proportionally. When both are provided, the image will be stretched to
43
+ # proportionally. When both are provided, the image will be stretched to
42
44
  # fit the dimensions without maintaining the aspect ratio.
43
45
  #
44
46
  #
@@ -52,16 +54,16 @@ module Prawn
52
54
  #
53
55
  # require "open-uri"
54
56
  #
55
- # Prawn::Document.generate("remote_images.pdf") do
57
+ # Prawn::Document.generate("remote_images.pdf") do
56
58
  # image open("http://prawn.majesticseacreature.com/media/prawn_logo.png")
57
59
  # end
58
60
  #
59
61
  # This method returns an image info object which can be used to check the
60
- # dimensions of an image object if needed.
62
+ # dimensions of an image object if needed.
61
63
  # (See also: Prawn::Images::PNG , Prawn::Images::JPG)
62
- #
64
+ #
63
65
  def image(file, options={})
64
- Prawn.verify_options [:at, :position, :vposition, :height,
66
+ Prawn.verify_options [:at, :position, :vposition, :height,
65
67
  :width, :scale, :fit], options
66
68
 
67
69
  pdf_obj, info = build_image_object(file)
@@ -70,14 +72,17 @@ module Prawn
70
72
  info
71
73
  end
72
74
 
73
-
74
75
  # Builds an info object (Prawn::Images::*) and a PDF reference representing
75
76
  # the given image. Return a pair: [pdf_obj, info].
76
77
  #
77
- # @private
78
78
  def build_image_object(file)
79
- io = verify_and_open_image(file)
80
- image_content = io.read
79
+ if file.respond_to?(:read)
80
+ image_content = file.read
81
+ else
82
+ raise ArgumentError, "#{file} not found" unless File.file?(file)
83
+ image_content = File.binread(file)
84
+ end
85
+
81
86
  image_sha1 = Digest::SHA1.hexdigest(image_content)
82
87
 
83
88
  # if this image has already been embedded, just reuse it
@@ -86,7 +91,11 @@ module Prawn
86
91
  image_obj = image_registry[image_sha1][:obj]
87
92
  else
88
93
  # Build the image object
89
- info = Prawn.image_handler.find(image_content).new(image_content)
94
+ klass = case Image.detect_image_format(image_content)
95
+ when :jpg then Prawn::Images::JPG
96
+ when :png then Prawn::Images::PNG
97
+ end
98
+ info = klass.new(image_content)
90
99
 
91
100
  # Bump PDF version if the image requires it
92
101
  min_version(info.min_pdf_version) if info.respond_to?(:min_pdf_version)
@@ -104,16 +113,15 @@ module Prawn
104
113
  # build_image_object), embed the image according to the <tt>options</tt>
105
114
  # given.
106
115
  #
107
- # @private
108
116
  def embed_image(pdf_obj, info, options)
109
- # find where the image will be placed and how big it will be
117
+ # find where the image will be placed and how big it will be
110
118
  w,h = info.calc_image_dimensions(options)
111
119
 
112
- if options[:at]
113
- x,y = map_to_absolute(options[:at])
114
- else
115
- x,y = image_position(w,h,options)
116
- move_text_position h
120
+ if options[:at]
121
+ x,y = map_to_absolute(options[:at])
122
+ else
123
+ x,y = image_position(w,h,options)
124
+ move_text_position h
117
125
  end
118
126
 
119
127
  # add a reference to the image object to the current page
@@ -125,30 +133,22 @@ module Prawn
125
133
  instruct = "\nq\n%.3f 0 0 %.3f %.3f %.3f cm\n/%s Do\nQ"
126
134
  add_content instruct % [ w, h, x, y - h, label ]
127
135
  end
128
-
129
- private
130
-
131
- def verify_and_open_image(io_or_path)
132
- # File or IO
133
- if io_or_path.respond_to?(:rewind)
134
- io = io_or_path
135
- # Rewind if the object we're passed is an IO, so that multiple embeds of
136
- # the same IO object will work
137
- io.rewind
138
- # read the file as binary so the size is calculated correctly
139
- # guard binmode because some objects acting io-like don't implement it
140
- io.binmode if io.respond_to?(:binmode)
141
- return io
142
- end
143
- # String or Pathname
144
- io_or_path = Pathname.new(io_or_path)
145
- raise ArgumentError, "#{io_or_path} not found" unless io_or_path.file?
146
- io = io_or_path.open('rb')
147
- io
148
- end
136
+
137
+ private
149
138
 
150
139
  def image_position(w,h,options)
151
140
  options[:position] ||= :left
141
+
142
+ x = case options[:position]
143
+ when :left
144
+ bounds.absolute_left
145
+ when :center
146
+ bounds.absolute_left + (bounds.width - w) / 2.0
147
+ when :right
148
+ bounds.absolute_right - w
149
+ when Numeric
150
+ options[:position] + bounds.absolute_left
151
+ end
152
152
 
153
153
  y = case options[:vposition]
154
154
  when :top
@@ -162,30 +162,20 @@ module Prawn
162
162
  else
163
163
  determine_y_with_page_flow(h)
164
164
  end
165
-
166
- x = case options[:position]
167
- when :left
168
- bounds.left_side
169
- when :center
170
- bounds.left_side + (bounds.width - w) / 2.0
171
- when :right
172
- bounds.right_side - w
173
- when Numeric
174
- options[:position] + bounds.left_side
175
- end
176
-
177
165
  return [x,y]
178
- end
179
-
166
+ end
167
+
180
168
  def determine_y_with_page_flow(h)
181
169
  if overruns_page?(h)
182
- bounds.move_past_bottom
170
+ start_new_page
171
+ bounds.absolute_top
172
+ else
173
+ self.y
183
174
  end
184
- self.y
185
- end
186
-
175
+ end
176
+
187
177
  def overruns_page?(h)
188
- (self.y - h) < reference_bounds.absolute_bottom
178
+ (self.y - h) < reference_bounds.absolute_bottom
189
179
  end
190
180
 
191
181
  def image_registry