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
@@ -8,6 +8,69 @@
8
8
  #
9
9
 
10
10
  module Prawn
11
+ class GraphicStateStack
12
+ attr_accessor :stack
13
+
14
+ def initialize(previous_state = nil)
15
+ self.stack = [GraphicState.new(previous_state)]
16
+ end
17
+
18
+ def save_graphic_state(graphic_state = nil)
19
+ stack.push(GraphicState.new(graphic_state || current_state))
20
+ end
21
+
22
+ def restore_graphic_state
23
+ if stack.empty?
24
+ raise Prawn::Errors::EmptyGraphicStateStack,
25
+ "\n You have reached the end of the graphic state stack"
26
+ end
27
+ stack.pop
28
+ end
29
+
30
+ def current_state
31
+ stack.last
32
+ end
33
+
34
+ def present?
35
+ stack.size > 0
36
+ end
37
+
38
+ def empty?
39
+ stack.empty?
40
+ end
41
+
42
+ end
43
+
44
+ class GraphicState
45
+ attr_accessor :color_space, :dash, :cap_style, :join_style, :line_width, :fill_color, :stroke_color
46
+
47
+ def initialize(previous_state = nil)
48
+ @color_space = previous_state ? previous_state.color_space.dup : {}
49
+ @fill_color = previous_state ? previous_state.fill_color : "000000"
50
+ @stroke_color = previous_state ? previous_state.stroke_color : "000000"
51
+ @dash = previous_state ? previous_state.dash : { :dash => nil, :space => nil, :phase => 0 }
52
+ @cap_style = previous_state ? previous_state.cap_style : :butt
53
+ @join_style = previous_state ? previous_state.join_style : :miter
54
+ @line_width = previous_state ? previous_state.line_width : 1
55
+ end
56
+
57
+ def dash_setting
58
+ "[#{@dash[:dash]} #{@dash[:space]}] #{@dash[:phase]} d"
59
+ end
60
+ end
61
+
62
+ module Core
63
+ class Page
64
+ module GraphicsState
65
+
66
+ def graphic_state
67
+ stack.current_state
68
+ end
69
+
70
+ end
71
+ end
72
+ end
73
+
11
74
  class Document
12
75
  module GraphicsState
13
76
 
@@ -30,8 +93,18 @@ module Prawn
30
93
  # text "rotated text"
31
94
  # end
32
95
  #
33
- def save_graphics_state
96
+
97
+ def open_graphics_state
34
98
  add_content "q"
99
+ end
100
+
101
+ def close_graphics_state
102
+ add_content "Q"
103
+ end
104
+
105
+ def save_graphics_state(graphic_state = nil)
106
+ graphic_stack.save_graphic_state(graphic_state)
107
+ open_graphics_state
35
108
  if block_given?
36
109
  yield
37
110
  restore_graphics_state
@@ -41,8 +114,23 @@ module Prawn
41
114
  # Pops the last saved graphics state off the graphics state stack and
42
115
  # restores the state to those values
43
116
  def restore_graphics_state
44
- add_content "Q"
117
+ if graphic_stack.empty?
118
+ raise Prawn::Errors::EmptyGraphicStateStack,
119
+ "\n You have reached the end of the graphic state stack"
120
+ end
121
+ close_graphics_state
122
+ graphic_stack.restore_graphic_state
123
+ end
124
+
125
+ def graphic_stack
126
+ state.page.stack
127
+ end
128
+
129
+ def graphic_state
130
+ save_graphics_state unless graphic_stack.current_state
131
+ graphic_stack.current_state
45
132
  end
133
+
46
134
  end
47
135
  end
48
136
  end
@@ -54,6 +54,7 @@ module Prawn
54
54
  # pdf.add_content("S") # stroke
55
55
  #
56
56
  def add_content(str)
57
+ save_graphics_state if graphic_state.nil?
57
58
  state.page.content << str << "\n"
58
59
  end
59
60
 
@@ -97,8 +98,7 @@ module Prawn
97
98
  go_to_page i
98
99
  state.page.new_content_stream
99
100
  apply_margin_options(options)
100
- use_graphic_settings
101
- save_graphics_state
101
+ use_graphic_settings(options[:template])
102
102
  end
103
103
  end
104
104
 
@@ -106,7 +106,9 @@ module Prawn
106
106
  (1..page_count).each do |i|
107
107
  go_to_page i
108
108
  repeaters.each { |r| r.run(i) }
109
- restore_graphics_state
109
+ while graphic_stack.present?
110
+ restore_graphics_state
111
+ end
110
112
  state.page.finalize
111
113
  end
112
114
  end
data/lib/prawn/errors.rb CHANGED
@@ -82,5 +82,10 @@ module Prawn
82
82
  # This error is raised when Prawn fails to load a template file
83
83
  #
84
84
  TemplateError = Class.new(StandardError)
85
+
86
+ # This error is raise when trying to restore a graphic state that
87
+ #
88
+ EmptyGraphicStateStack = Class.new(StandardError)
89
+
85
90
  end
86
91
  end
data/lib/prawn/font.rb CHANGED
@@ -240,10 +240,10 @@ module Prawn
240
240
  #
241
241
  def self.load(document,name,options={})
242
242
  case name
243
- when /\.ttf$/ then TTF.new(document, name, options)
244
- when /\.dfont$/ then DFont.new(document, name, options)
245
- when /\.afm$/ then AFM.new(document, name, options)
246
- else AFM.new(document, name, options)
243
+ when /\.ttf$/i then TTF.new(document, name, options)
244
+ when /\.dfont$/i then DFont.new(document, name, options)
245
+ when /\.afm$/i then AFM.new(document, name, options)
246
+ else AFM.new(document, name, options)
247
247
  end
248
248
  end
249
249
 
@@ -89,6 +89,9 @@ module Prawn
89
89
  def normalize_encoding(text)
90
90
  enc = Prawn::Encoding::WinAnsi.new
91
91
  text.unpack("U*").collect { |i| enc[i] }.pack("C*")
92
+ rescue ArgumentError
93
+ raise Prawn::Errors::IncompatibleStringEncoding,
94
+ "Arguments to text methods must be UTF-8 encoded"
92
95
  end
93
96
 
94
97
  # Perform any changes to the string that need to happen
@@ -107,6 +110,14 @@ module Prawn
107
110
  [[0, options[:kerning] ? kern(text) : text]]
108
111
  end
109
112
 
113
+ def glyph_present?(char)
114
+ if char == "_"
115
+ true
116
+ else
117
+ normalize_encoding(char) != "_"
118
+ end
119
+ end
120
+
110
121
  private
111
122
 
112
123
  def register(subset)
@@ -185,6 +185,11 @@ module Prawn
185
185
  end
186
186
  end
187
187
 
188
+ def glyph_present?(char)
189
+ code = char.unpack("U*").first
190
+ cmap[code] > 0
191
+ end
192
+
188
193
  private
189
194
 
190
195
  def cmap
@@ -12,6 +12,7 @@ require "prawn/graphics/cap_style"
12
12
  require "prawn/graphics/join_style"
13
13
  require "prawn/graphics/transparency"
14
14
  require "prawn/graphics/transformation"
15
+ require "prawn/graphics/gradient"
15
16
 
16
17
  module Prawn
17
18
 
@@ -29,6 +30,7 @@ module Prawn
29
30
  include JoinStyle
30
31
  include Transparency
31
32
  include Transformation
33
+ include Gradient
32
34
 
33
35
  #######################################################################
34
36
  # Low level drawing operations must map the point to absolute coords! #
@@ -100,8 +102,8 @@ module Prawn
100
102
  # Sets line thickness to the <tt>width</tt> specified.
101
103
  #
102
104
  def line_width=(width)
103
- @line_width = width
104
- add_content("#{width} w")
105
+ self.current_line_width = width
106
+ write_line_width
105
107
  end
106
108
 
107
109
  # When called without an argument, returns the current line thickness.
@@ -116,7 +118,7 @@ module Prawn
116
118
  if width
117
119
  self.line_width = width
118
120
  else
119
- (defined?(@line_width) && @line_width) || 1
121
+ current_line_width
120
122
  end
121
123
  end
122
124
 
@@ -179,15 +181,28 @@ module Prawn
179
181
  #
180
182
  KAPPA = 4.0 * ((Math.sqrt(2) - 1.0) / 3.0)
181
183
 
182
- # Draws a circle of radius <tt>:radius</tt> with the centre-point at <tt>point</tt>
184
+ # <b>DEPRECATED:</b> Please use <tt>circle</tt> instead.
185
+ def circle_at(point, options)
186
+ warn "[DEPRECATION] 'circle_at' is deprecated in favor of 'circle'. " +
187
+ "'circle_at' will be removed in release 1.1"
188
+ circle(point, options[:radius])
189
+ end
190
+
191
+ # Draws a circle of radius <tt>radius</tt> with the centre-point at <tt>point</tt>
183
192
  # as a complete subpath. The drawing point will be moved to the
184
193
  # centre-point upon completion of the drawing the circle.
185
194
  #
186
- # pdf.circle_at [100,100], :radius => 25
195
+ # pdf.circle [100,100], 25
187
196
  #
188
- def circle_at(point, options)
189
- x,y = point
190
- ellipse_at [x, y], options[:radius]
197
+ def circle(center, radius)
198
+ ellipse(center, radius, radius)
199
+ end
200
+
201
+ # <b>DEPRECATED:</b> Please use <tt>ellipse</tt> instead.
202
+ def ellipse_at(point, r1, r2=r1)
203
+ warn "[DEPRECATION] 'ellipse_at' is deprecated in favor of 'ellipse'. " +
204
+ "'ellipse_at' will be removed in release 1.1"
205
+ ellipse(point, r1, r2)
191
206
  end
192
207
 
193
208
  # Draws an ellipse of +x+ radius <tt>r1</tt> and +y+ radius <tt>r2</tt>
@@ -196,9 +211,9 @@ module Prawn
196
211
  # drawing the ellipse.
197
212
  #
198
213
  # # draws an ellipse with x-radius 25 and y-radius 50
199
- # pdf.ellipse_at [100,100], 25, 50
214
+ # pdf.ellipse [100,100], 25, 50
200
215
  #
201
- def ellipse_at(point, r1, r2 = r1)
216
+ def ellipse(point, r1, r2 = r1)
202
217
  x, y = point
203
218
  l1 = r1 * KAPPA
204
219
  l2 = r2 * KAPPA
@@ -268,12 +283,21 @@ module Prawn
268
283
  curve_to(radial_point_2, :bounds => [bezier_point_1, bezier_point_2])
269
284
  end
270
285
 
271
- # Strokes and closes the current path. See Graphic::Color for color details
286
+ # Strokes the current path. If a block is provided, yields to the block
287
+ # before closing the path. See Graphics::Color for color details.
272
288
  #
273
289
  def stroke
274
290
  yield if block_given?
275
291
  add_content "S"
276
292
  end
293
+
294
+ # Closes and strokes the current path. If a block is provided, yields to
295
+ # the block before closing the path. See Graphics::Color for color details.
296
+ #
297
+ def close_and_stroke
298
+ yield if block_given?
299
+ add_content "s"
300
+ end
277
301
 
278
302
  # Draws and strokes a rectangle represented by the current bounding box
279
303
  #
@@ -281,21 +305,60 @@ module Prawn
281
305
  stroke_rectangle bounds.top_left, bounds.width, bounds.height
282
306
  end
283
307
 
284
- # Fills and closes the current path. See Graphic::Color for color details
308
+ # Closes and fills the current path. See Graphics::Color for color details.
285
309
  #
286
310
  def fill
287
311
  yield if block_given?
288
312
  add_content "f"
289
313
  end
290
314
 
291
- # Fills, strokes, and closes the current path. See Graphic::Color for color details
315
+ # Closes, fills, and strokes the current path. If a block is provided,
316
+ # yields to the block before closing the path. See Graphics::Color for
317
+ # color details.
292
318
  #
293
319
  def fill_and_stroke
294
320
  yield if block_given?
295
321
  add_content "b"
296
322
  end
297
-
323
+
324
+ # Closes the current path.
325
+ #
326
+ def close_path
327
+ add_content "h"
328
+ end
329
+
330
+ # Provides the following shortcuts:
331
+ #
332
+ # stroke_some_method(*args) #=> some_method(*args); stroke
333
+ # fill_some_method(*args) #=> some_method(*args); fill
334
+ # fill_and_stroke_some_method(*args) #=> some_method(*args); fill_and_stroke
335
+ #
336
+ def method_missing(id,*args,&block)
337
+ case(id.to_s)
338
+ when /^fill_and_stroke_(.*)/
339
+ send($1,*args,&block); fill_and_stroke
340
+ when /^stroke_(.*)/
341
+ send($1,*args,&block); stroke
342
+ when /^fill_(.*)/
343
+ send($1,*args,&block); fill
344
+ else
345
+ super
346
+ end
347
+ end
348
+
298
349
  private
350
+
351
+ def current_line_width
352
+ graphic_state.line_width
353
+ end
354
+
355
+ def current_line_width=(width)
356
+ graphic_state.line_width = width
357
+ end
358
+
359
+ def write_line_width
360
+ add_content("#{current_line_width} w")
361
+ end
299
362
 
300
363
  def map_to_absolute(*point)
301
364
  x,y = point.flatten
@@ -11,7 +11,7 @@ module Prawn
11
11
  module CapStyle
12
12
 
13
13
  CAP_STYLES = { :butt => 0, :round => 1, :projecting_square => 2 }
14
-
14
+
15
15
  # Sets the cap style for stroked lines and curves
16
16
  #
17
17
  # style is one of :butt, :round, or :projecting_square
@@ -19,19 +19,27 @@ module Prawn
19
19
  # NOTE: If this method is never called, :butt will be used by default.
20
20
  #
21
21
  def cap_style(style=nil)
22
- return @cap_style || :butt if style.nil?
22
+ return current_cap_style || :butt if style.nil?
23
23
 
24
- @cap_style = style
24
+ self.current_cap_style = style
25
25
 
26
26
  write_stroke_cap_style
27
27
  end
28
-
28
+
29
29
  alias_method :cap_style=, :cap_style
30
30
 
31
31
  private
32
32
 
33
+ def current_cap_style
34
+ graphic_state.cap_style
35
+ end
36
+
37
+ def current_cap_style=(style)
38
+ graphic_state.cap_style = style
39
+ end
40
+
33
41
  def write_stroke_cap_style
34
- add_content "#{CAP_STYLES[@cap_style]} J"
42
+ add_content "#{CAP_STYLES[current_cap_style]} J"
35
43
  end
36
44
  end
37
45
  end
@@ -25,8 +25,8 @@ module Prawn
25
25
  # pdf.fill_color 0, 99, 95, 0
26
26
  #
27
27
  def fill_color(*color)
28
- return @fill_color if color.empty?
29
- @fill_color = process_color(*color)
28
+ return current_fill_color if color.empty?
29
+ self.current_fill_color = process_color(*color)
30
30
  set_fill_color
31
31
  end
32
32
 
@@ -47,32 +47,14 @@ module Prawn
47
47
  # pdf.stroke_color 0, 99, 95, 0
48
48
  #
49
49
  def stroke_color(*color)
50
- return @stroke_color if color.empty?
51
- @stroke_color = process_color(*color)
52
- set_stroke_color
50
+ return current_stroke_color if color.empty?
51
+ color = process_color(*color)
52
+ self.current_stroke_color = color
53
+ set_stroke_color(color)
53
54
  end
54
55
 
55
56
  alias_method :stroke_color=, :stroke_color
56
57
 
57
- # Provides the following shortcuts:
58
- #
59
- # stroke_some_method(*args) #=> some_method(*args); stroke
60
- # fill_some_method(*args) #=> some_method(*args); fill
61
- # fill_and_stroke_some_method(*args) #=> some_method(*args); fill_and_stroke
62
- #
63
- def method_missing(id,*args,&block)
64
- case(id.to_s)
65
- when /^fill_and_stroke_(.*)/
66
- send($1,*args,&block); fill_and_stroke
67
- when /^stroke_(.*)/
68
- send($1,*args,&block); stroke
69
- when /^fill_(.*)/
70
- send($1,*args,&block); fill
71
- else
72
- super
73
- end
74
- end
75
-
76
58
  module_function
77
59
 
78
60
  # Converts RGB value array to hex string suitable for use with fill_color
@@ -145,8 +127,8 @@ module Prawn
145
127
 
146
128
  def set_color_space(type, color_space)
147
129
  # don't set the same color space again
148
- return if @color_space[type] == color_space
149
- @color_space[type] = color_space
130
+ return if current_color_space(type) == color_space && !state.page.in_stamp_stream?
131
+ set_current_color_space(color_space, type)
150
132
 
151
133
  unless COLOR_SPACES.include?(color_space)
152
134
  raise ArgumentError, "unknown color space: '#{color_space}'"
@@ -177,28 +159,65 @@ module Prawn
177
159
  if options[:pattern]
178
160
  set_color_space type, :Pattern
179
161
  add_content "/#{color} #{operator}"
180
- else
162
+ else
181
163
  set_color_space type, color_space(color)
182
164
  color = color_to_s(color)
183
- add_content "#{color} #{operator}"
165
+ write_color(color, operator)
184
166
  end
185
167
  end
186
168
 
187
- def set_fill_color
188
- set_color :fill, @fill_color
169
+ def set_fill_color(color = nil)
170
+ set_color :fill, color || current_fill_color
189
171
  end
190
172
 
191
- def set_stroke_color
192
- set_color :stroke, @stroke_color
173
+ def set_stroke_color(color = nil)
174
+ set_color :stroke, color || current_stroke_color
193
175
  end
194
176
 
195
177
  def update_colors
196
- @color_space = {}
197
- @fill_color ||= "000000"
198
- @stroke_color ||= "000000"
199
178
  set_fill_color
200
179
  set_stroke_color
201
180
  end
181
+
182
+ private
183
+
184
+ def current_color_space(type)
185
+ graphic_state.color_space[type]
186
+ end
187
+
188
+ def set_current_color_space(color_space, type)
189
+ save_graphics_state if graphic_state.nil?
190
+ graphic_state.color_space[type] = color_space
191
+ end
192
+
193
+ def current_fill_color
194
+ graphic_state.fill_color
195
+ end
196
+
197
+ def current_fill_color=(color)
198
+ graphic_state.fill_color = color
199
+ end
200
+
201
+ def current_stroke_color
202
+ graphic_state.stroke_color
203
+ end
204
+
205
+ def current_stroke_color=(color)
206
+ graphic_state.stroke_color = color
207
+ end
208
+
209
+ def write_fill_color
210
+ write_color(current_fill_color, 'scn')
211
+ end
212
+
213
+ def write_stroke_color
214
+ write_color(current_fill_color, 'SCN')
215
+ end
216
+
217
+ def write_color(color, operator)
218
+ add_content "#{color} #{operator}"
219
+ end
220
+
202
221
  end
203
222
  end
204
223
  end