piglop-prawn 0.10.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (276) hide show
  1. data/COPYING +340 -0
  2. data/HACKING +49 -0
  3. data/LICENSE +56 -0
  4. data/README +140 -0
  5. data/Rakefile +52 -0
  6. data/data/encodings/win_ansi.txt +29 -0
  7. data/data/fonts/Action Man.dfont +0 -0
  8. data/data/fonts/Activa.ttf +0 -0
  9. data/data/fonts/Chalkboard.ttf +0 -0
  10. data/data/fonts/Courier-Bold.afm +342 -0
  11. data/data/fonts/Courier-BoldOblique.afm +342 -0
  12. data/data/fonts/Courier-Oblique.afm +342 -0
  13. data/data/fonts/Courier.afm +342 -0
  14. data/data/fonts/DejaVuSans.ttf +0 -0
  15. data/data/fonts/Dustismo_Roman.ttf +0 -0
  16. data/data/fonts/Helvetica-Bold.afm +2827 -0
  17. data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
  18. data/data/fonts/Helvetica-Oblique.afm +3051 -0
  19. data/data/fonts/Helvetica.afm +3051 -0
  20. data/data/fonts/MustRead.html +19 -0
  21. data/data/fonts/Symbol.afm +213 -0
  22. data/data/fonts/Times-Bold.afm +2588 -0
  23. data/data/fonts/Times-BoldItalic.afm +2384 -0
  24. data/data/fonts/Times-Italic.afm +2667 -0
  25. data/data/fonts/Times-Roman.afm +2419 -0
  26. data/data/fonts/ZapfDingbats.afm +225 -0
  27. data/data/fonts/comicsans.ttf +0 -0
  28. data/data/fonts/gkai00mp.ttf +0 -0
  29. data/data/images/16bit.alpha +0 -0
  30. data/data/images/16bit.dat +0 -0
  31. data/data/images/16bit.png +0 -0
  32. data/data/images/arrow.png +0 -0
  33. data/data/images/arrow2.png +0 -0
  34. data/data/images/barcode_issue.png +0 -0
  35. data/data/images/dice.alpha +0 -0
  36. data/data/images/dice.dat +0 -0
  37. data/data/images/dice.png +0 -0
  38. data/data/images/dice_interlaced.png +0 -0
  39. data/data/images/fractal.jpg +0 -0
  40. data/data/images/letterhead.jpg +0 -0
  41. data/data/images/page_white_text.alpha +0 -0
  42. data/data/images/page_white_text.dat +0 -0
  43. data/data/images/page_white_text.png +0 -0
  44. data/data/images/pigs.jpg +0 -0
  45. data/data/images/rails.dat +0 -0
  46. data/data/images/rails.png +0 -0
  47. data/data/images/ruport.png +0 -0
  48. data/data/images/ruport_data.dat +0 -0
  49. data/data/images/ruport_transparent.png +0 -0
  50. data/data/images/ruport_type0.png +0 -0
  51. data/data/images/stef.jpg +0 -0
  52. data/data/images/tru256.bmp +0 -0
  53. data/data/images/web-links.dat +1 -0
  54. data/data/images/web-links.png +0 -0
  55. data/data/pdfs/complex_template.pdf +0 -0
  56. data/data/pdfs/contains_ttf_font.pdf +0 -0
  57. data/data/pdfs/encrypted.pdf +0 -0
  58. data/data/pdfs/hexagon.pdf +61 -0
  59. data/data/pdfs/indirect_reference.pdf +86 -0
  60. data/data/pdfs/nested_pages.pdf +118 -0
  61. data/data/pdfs/resources_as_indirect_object.pdf +83 -0
  62. data/data/pdfs/two_hexagons.pdf +90 -0
  63. data/data/pdfs/version_1_6.pdf +61 -0
  64. data/data/shift_jis_text.txt +1 -0
  65. data/examples/bounding_box/bounding_boxes.rb +43 -0
  66. data/examples/bounding_box/indentation.rb +34 -0
  67. data/examples/bounding_box/russian_boxes.rb +36 -0
  68. data/examples/bounding_box/stretched_nesting.rb +67 -0
  69. data/examples/example_helper.rb +4 -0
  70. data/examples/general/background.rb +23 -0
  71. data/examples/general/canvas.rb +15 -0
  72. data/examples/general/context_sensitive_headers.rb +37 -0
  73. data/examples/general/float.rb +11 -0
  74. data/examples/general/margin.rb +36 -0
  75. data/examples/general/measurement_units.rb +51 -0
  76. data/examples/general/metadata-info.rb +16 -0
  77. data/examples/general/multi_page_layout.rb +18 -0
  78. data/examples/general/outlines.rb +50 -0
  79. data/examples/general/page_geometry.rb +31 -0
  80. data/examples/general/page_numbering.rb +15 -0
  81. data/examples/general/repeaters.rb +47 -0
  82. data/examples/general/stamp.rb +41 -0
  83. data/examples/general/templates.rb +13 -0
  84. data/examples/graphics/basic_images.rb +23 -0
  85. data/examples/graphics/cmyk.rb +12 -0
  86. data/examples/graphics/curves.rb +11 -0
  87. data/examples/graphics/hexagon.rb +13 -0
  88. data/examples/graphics/image_fit.rb +15 -0
  89. data/examples/graphics/image_flow.rb +37 -0
  90. data/examples/graphics/image_position.rb +17 -0
  91. data/examples/graphics/line.rb +32 -0
  92. data/examples/graphics/png_types.rb +22 -0
  93. data/examples/graphics/polygons.rb +16 -0
  94. data/examples/graphics/remote_images.rb +12 -0
  95. data/examples/graphics/rounded_polygons.rb +19 -0
  96. data/examples/graphics/rounded_rectangle.rb +20 -0
  97. data/examples/graphics/ruport_style_helpers.rb +19 -0
  98. data/examples/graphics/stroke_bounds.rb +20 -0
  99. data/examples/graphics/stroke_cap_and_join.rb +45 -0
  100. data/examples/graphics/stroke_dash.rb +42 -0
  101. data/examples/graphics/transformations.rb +52 -0
  102. data/examples/graphics/transparency.rb +26 -0
  103. data/examples/m17n/chinese_text_wrapping.rb +17 -0
  104. data/examples/m17n/euro.rb +15 -0
  105. data/examples/m17n/sjis.rb +28 -0
  106. data/examples/m17n/utf8.rb +13 -0
  107. data/examples/m17n/win_ansi_charset.rb +54 -0
  108. data/examples/security/hello_foo.rb +8 -0
  109. data/examples/table/bill.rb +53 -0
  110. data/examples/table/cell.rb +12 -0
  111. data/examples/table/checkerboard.rb +22 -0
  112. data/examples/table/header.rb +14 -0
  113. data/examples/table/inline_format_table.rb +12 -0
  114. data/examples/table/multi_page_table.rb +9 -0
  115. data/examples/table/simple_table.rb +24 -0
  116. data/examples/table/subtable.rb +12 -0
  117. data/examples/table/widths.rb +20 -0
  118. data/examples/text/alignment.rb +18 -0
  119. data/examples/text/dfont.rb +48 -0
  120. data/examples/text/family_based_styling.rb +24 -0
  121. data/examples/text/font_calculations.rb +91 -0
  122. data/examples/text/font_size.rb +33 -0
  123. data/examples/text/hyphenation.rb +45 -0
  124. data/examples/text/indent_paragraphs.rb +18 -0
  125. data/examples/text/inline_format.rb +72 -0
  126. data/examples/text/kerning.rb +30 -0
  127. data/examples/text/rotated.rb +98 -0
  128. data/examples/text/shaped_text_box.rb +31 -0
  129. data/examples/text/simple_text.rb +17 -0
  130. data/examples/text/simple_text_ttf.rb +17 -0
  131. data/examples/text/text_box.rb +88 -0
  132. data/examples/text/text_box_returning_excess.rb +51 -0
  133. data/examples/text/text_flow.rb +67 -0
  134. data/lib/prawn/compatibility.rb +51 -0
  135. data/lib/prawn/core/annotations.rb +61 -0
  136. data/lib/prawn/core/byte_string.rb +9 -0
  137. data/lib/prawn/core/destinations.rb +90 -0
  138. data/lib/prawn/core/document_state.rb +84 -0
  139. data/lib/prawn/core/literal_string.rb +16 -0
  140. data/lib/prawn/core/name_tree.rb +165 -0
  141. data/lib/prawn/core/object_store.rb +219 -0
  142. data/lib/prawn/core/page.rb +179 -0
  143. data/lib/prawn/core/pdf_object.rb +83 -0
  144. data/lib/prawn/core/reference.rb +112 -0
  145. data/lib/prawn/core/text/formatted/arranger.rb +260 -0
  146. data/lib/prawn/core/text/formatted/line_wrap.rb +127 -0
  147. data/lib/prawn/core/text/formatted/wrap.rb +113 -0
  148. data/lib/prawn/core/text/line_wrap.rb +198 -0
  149. data/lib/prawn/core/text/wrap.rb +80 -0
  150. data/lib/prawn/core/text.rb +115 -0
  151. data/lib/prawn/core.rb +89 -0
  152. data/lib/prawn/document/bounding_box.rb +425 -0
  153. data/lib/prawn/document/graphics_state.rb +48 -0
  154. data/lib/prawn/document/internals.rb +176 -0
  155. data/lib/prawn/document/page_geometry.rb +136 -0
  156. data/lib/prawn/document/snapshot.rb +87 -0
  157. data/lib/prawn/document.rb +573 -0
  158. data/lib/prawn/encoding.rb +121 -0
  159. data/lib/prawn/errors.rb +94 -0
  160. data/lib/prawn/font/afm.rb +225 -0
  161. data/lib/prawn/font/dfont.rb +42 -0
  162. data/lib/prawn/font/ttf.rb +350 -0
  163. data/lib/prawn/font.rb +342 -0
  164. data/lib/prawn/graphics/cap_style.rb +38 -0
  165. data/lib/prawn/graphics/color.rb +205 -0
  166. data/lib/prawn/graphics/dash.rb +71 -0
  167. data/lib/prawn/graphics/join_style.rb +38 -0
  168. data/lib/prawn/graphics/transformation.rb +156 -0
  169. data/lib/prawn/graphics/transparency.rb +99 -0
  170. data/lib/prawn/graphics.rb +321 -0
  171. data/lib/prawn/images/jpg.rb +46 -0
  172. data/lib/prawn/images/png.rb +226 -0
  173. data/lib/prawn/images.rb +348 -0
  174. data/lib/prawn/measurement_extensions.rb +46 -0
  175. data/lib/prawn/measurements.rb +71 -0
  176. data/lib/prawn/outline.rb +278 -0
  177. data/lib/prawn/repeater.rb +129 -0
  178. data/lib/prawn/security/arcfour.rb +51 -0
  179. data/lib/prawn/security.rb +262 -0
  180. data/lib/prawn/stamp.rb +126 -0
  181. data/lib/prawn/table/accessors.rb +180 -0
  182. data/lib/prawn/table/cell/in_table.rb +27 -0
  183. data/lib/prawn/table/cell/subtable.rb +65 -0
  184. data/lib/prawn/table/cell/text.rb +128 -0
  185. data/lib/prawn/table/cell.rb +330 -0
  186. data/lib/prawn/table.rb +421 -0
  187. data/lib/prawn/text/box.rb +380 -0
  188. data/lib/prawn/text/formatted/box.rb +215 -0
  189. data/lib/prawn/text/formatted/fragment.rb +127 -0
  190. data/lib/prawn/text/formatted/parser.rb +202 -0
  191. data/lib/prawn/text/formatted.rb +4 -0
  192. data/lib/prawn/text.rb +442 -0
  193. data/lib/prawn.rb +25 -0
  194. data/spec/annotations_spec.rb +90 -0
  195. data/spec/bounding_box_spec.rb +190 -0
  196. data/spec/cell_spec.rb +337 -0
  197. data/spec/destinations_spec.rb +15 -0
  198. data/spec/document_spec.rb +461 -0
  199. data/spec/font_spec.rb +314 -0
  200. data/spec/formatted_text_arranger_spec.rb +400 -0
  201. data/spec/formatted_text_box_spec.rb +659 -0
  202. data/spec/formatted_text_fragment_spec.rb +211 -0
  203. data/spec/graphics_spec.rb +446 -0
  204. data/spec/images_spec.rb +96 -0
  205. data/spec/inline_formatted_text_parser_spec.rb +446 -0
  206. data/spec/jpg_spec.rb +25 -0
  207. data/spec/line_wrap_spec.rb +341 -0
  208. data/spec/measurement_units_spec.rb +23 -0
  209. data/spec/name_tree_spec.rb +112 -0
  210. data/spec/object_store_spec.rb +160 -0
  211. data/spec/outline_spec.rb +229 -0
  212. data/spec/pdf_object_spec.rb +127 -0
  213. data/spec/png_spec.rb +237 -0
  214. data/spec/reference_spec.rb +82 -0
  215. data/spec/repeater_spec.rb +96 -0
  216. data/spec/security_spec.rb +120 -0
  217. data/spec/snapshot_spec.rb +138 -0
  218. data/spec/spec_helper.rb +26 -0
  219. data/spec/stamp_spec.rb +108 -0
  220. data/spec/stroke_styles_spec.rb +163 -0
  221. data/spec/table_spec.rb +583 -0
  222. data/spec/template_spec.rb +158 -0
  223. data/spec/text_at_spec.rb +119 -0
  224. data/spec/text_box_spec.rb +675 -0
  225. data/spec/text_spacing_spec.rb +57 -0
  226. data/spec/text_spec.rb +328 -0
  227. data/spec/text_with_inline_formatting_spec.rb +193 -0
  228. data/spec/transparency_spec.rb +75 -0
  229. data/vendor/pdf-inspector/README +18 -0
  230. data/vendor/pdf-inspector/lib/pdf/inspector/extgstate.rb +18 -0
  231. data/vendor/pdf-inspector/lib/pdf/inspector/graphics.rb +131 -0
  232. data/vendor/pdf-inspector/lib/pdf/inspector/page.rb +25 -0
  233. data/vendor/pdf-inspector/lib/pdf/inspector/text.rb +46 -0
  234. data/vendor/pdf-inspector/lib/pdf/inspector/xobject.rb +19 -0
  235. data/vendor/pdf-inspector/lib/pdf/inspector.rb +26 -0
  236. data/vendor/ttfunk/data/fonts/DejaVuSans.ttf +0 -0
  237. data/vendor/ttfunk/data/fonts/comicsans.ttf +0 -0
  238. data/vendor/ttfunk/example.rb +45 -0
  239. data/vendor/ttfunk/lib/ttfunk/directory.rb +17 -0
  240. data/vendor/ttfunk/lib/ttfunk/encoding/mac_roman.rb +88 -0
  241. data/vendor/ttfunk/lib/ttfunk/encoding/windows_1252.rb +69 -0
  242. data/vendor/ttfunk/lib/ttfunk/reader.rb +44 -0
  243. data/vendor/ttfunk/lib/ttfunk/resource_file.rb +78 -0
  244. data/vendor/ttfunk/lib/ttfunk/subset/base.rb +141 -0
  245. data/vendor/ttfunk/lib/ttfunk/subset/mac_roman.rb +50 -0
  246. data/vendor/ttfunk/lib/ttfunk/subset/unicode.rb +48 -0
  247. data/vendor/ttfunk/lib/ttfunk/subset/unicode_8bit.rb +63 -0
  248. data/vendor/ttfunk/lib/ttfunk/subset/windows_1252.rb +55 -0
  249. data/vendor/ttfunk/lib/ttfunk/subset.rb +18 -0
  250. data/vendor/ttfunk/lib/ttfunk/subset_collection.rb +72 -0
  251. data/vendor/ttfunk/lib/ttfunk/table/cmap/format00.rb +54 -0
  252. data/vendor/ttfunk/lib/ttfunk/table/cmap/format04.rb +126 -0
  253. data/vendor/ttfunk/lib/ttfunk/table/cmap/subtable.rb +79 -0
  254. data/vendor/ttfunk/lib/ttfunk/table/cmap.rb +34 -0
  255. data/vendor/ttfunk/lib/ttfunk/table/glyf/compound.rb +81 -0
  256. data/vendor/ttfunk/lib/ttfunk/table/glyf/simple.rb +37 -0
  257. data/vendor/ttfunk/lib/ttfunk/table/glyf.rb +64 -0
  258. data/vendor/ttfunk/lib/ttfunk/table/head.rb +44 -0
  259. data/vendor/ttfunk/lib/ttfunk/table/hhea.rb +41 -0
  260. data/vendor/ttfunk/lib/ttfunk/table/hmtx.rb +47 -0
  261. data/vendor/ttfunk/lib/ttfunk/table/kern/format0.rb +62 -0
  262. data/vendor/ttfunk/lib/ttfunk/table/kern.rb +79 -0
  263. data/vendor/ttfunk/lib/ttfunk/table/loca.rb +43 -0
  264. data/vendor/ttfunk/lib/ttfunk/table/maxp.rb +40 -0
  265. data/vendor/ttfunk/lib/ttfunk/table/name.rb +125 -0
  266. data/vendor/ttfunk/lib/ttfunk/table/os2.rb +78 -0
  267. data/vendor/ttfunk/lib/ttfunk/table/post/format10.rb +43 -0
  268. data/vendor/ttfunk/lib/ttfunk/table/post/format20.rb +35 -0
  269. data/vendor/ttfunk/lib/ttfunk/table/post/format25.rb +23 -0
  270. data/vendor/ttfunk/lib/ttfunk/table/post/format30.rb +17 -0
  271. data/vendor/ttfunk/lib/ttfunk/table/post/format40.rb +17 -0
  272. data/vendor/ttfunk/lib/ttfunk/table/post.rb +91 -0
  273. data/vendor/ttfunk/lib/ttfunk/table/simple.rb +14 -0
  274. data/vendor/ttfunk/lib/ttfunk/table.rb +46 -0
  275. data/vendor/ttfunk/lib/ttfunk.rb +102 -0
  276. metadata +364 -0
@@ -0,0 +1,156 @@
1
+ # encoding: utf-8
2
+ #
3
+ # transformation.rb: Implements rotate, translate, skew, scale and a generic
4
+ # transformation_matrix
5
+ #
6
+ # Copyright January 2010, Michael Witrant. All Rights Reserved.
7
+ #
8
+ # This is free software. Please see the LICENSE and COPYING files for details.
9
+
10
+ module Prawn
11
+ module Graphics
12
+ module Transformation
13
+
14
+ # Rotate the user space. If a block is not provided, then you must save
15
+ # and restore the graphics state yourself.
16
+ #
17
+ # == Options
18
+ # <tt>:origin</tt>:: <tt>[number, number]</tt>. The point around which to
19
+ # rotate. A block must be provided if using the :origin
20
+ #
21
+ # raises <tt>Prawn::Errors::BlockRequired</tt> if an :origin option is
22
+ # provided, but no block is given
23
+ #
24
+ # Example without a block:
25
+ #
26
+ # save_graphics_state
27
+ # rotate 30
28
+ # text "rotated text"
29
+ # restore_graphics_state
30
+ #
31
+ # Example with a block: rotating a rectangle around its upper-left corner
32
+ #
33
+ # x = 300
34
+ # y = 300
35
+ # width = 150
36
+ # height = 200
37
+ # angle = 30
38
+ # pdf.rotate(angle, :origin => [x, y]) do
39
+ # pdf.stroke_rectangle([x, y], width, height)
40
+ # end
41
+ #
42
+ def rotate(angle, options={}, &block)
43
+ Prawn.verify_options(:origin, options)
44
+ rad = degree_to_rad(angle)
45
+ cos = Math.cos(rad)
46
+ sin = Math.sin(rad)
47
+ if options[:origin].nil?
48
+ transformation_matrix(cos, sin, -sin, cos, 0, 0, &block)
49
+ else
50
+ raise Prawn::Errors::BlockRequired unless block_given?
51
+ x = options[:origin][0] + bounds.absolute_left
52
+ y = options[:origin][1] + bounds.absolute_bottom
53
+ x_prime = x * cos - y * sin
54
+ y_prime = x * sin + y * cos
55
+ translate(x - x_prime, y - y_prime) do
56
+ transformation_matrix(cos, sin, -sin, cos, 0, 0, &block)
57
+ end
58
+ end
59
+ end
60
+
61
+ # Translate the user space. If a block is not provided, then you must save
62
+ # and restore the graphics state yourself.
63
+ #
64
+ # Example without a block: move the text up and over 10
65
+ #
66
+ # save_graphics_state
67
+ # translate(10, 10)
68
+ # text "scaled text"
69
+ # restore_graphics_state
70
+ #
71
+ # Example with a block: draw a rectangle with its upper-left corner at
72
+ # x + 10, y + 10
73
+ #
74
+ # x = 300
75
+ # y = 300
76
+ # width = 150
77
+ # height = 200
78
+ # pdf.translate(10, 10) do
79
+ # pdf.stroke_rectangle([x, y], width, height)
80
+ # end
81
+ #
82
+ def translate(x, y, &block)
83
+ transformation_matrix(1, 0, 0, 1, x, y, &block)
84
+ end
85
+
86
+ # Scale the user space. If a block is not provided, then you must save
87
+ # and restore the graphics state yourself.
88
+ #
89
+ # == Options
90
+ # <tt>:origin</tt>:: <tt>[number, number]</tt>. The point from which to
91
+ # scale. A block must be provided if using the :origin
92
+ #
93
+ # raises <tt>Prawn::Errors::BlockRequired</tt> if an :origin option is
94
+ # provided, but no block is given
95
+ #
96
+ # Example without a block:
97
+ #
98
+ # save_graphics_state
99
+ # scale 1.5
100
+ # text "scaled text"
101
+ # restore_graphics_state
102
+ #
103
+ # Example with a block: scale a rectangle from its upper-left corner
104
+ #
105
+ # x = 300
106
+ # y = 300
107
+ # width = 150
108
+ # height = 200
109
+ # factor = 1.5
110
+ # pdf.scale(angle, :origin => [x, y]) do
111
+ # pdf.stroke_rectangle([x, y], width, height)
112
+ # end
113
+ #
114
+ def scale(factor, options={}, &block)
115
+ Prawn.verify_options(:origin, options)
116
+ if options[:origin].nil?
117
+ transformation_matrix(factor, 0, 0, factor, 0, 0, &block)
118
+ else
119
+ raise Prawn::Errors::BlockRequired unless block_given?
120
+ x = options[:origin][0] + bounds.absolute_left
121
+ y = options[:origin][1] + bounds.absolute_bottom
122
+ x_prime = factor * x
123
+ y_prime = factor * y
124
+ translate(x - x_prime, y - y_prime) do
125
+ transformation_matrix(factor, 0, 0, factor, 0, 0, &block)
126
+ end
127
+ end
128
+ end
129
+
130
+ # The following definition of skew would only work in a clearly
131
+ # predicatable manner when if the document had no margin. don't provide
132
+ # this shortcut until it behaves in a clearly understood manner
133
+ #
134
+ # def skew(a, b, &block)
135
+ # transformation_matrix(1,
136
+ # Math.tan(degree_to_rad(a)),
137
+ # Math.tan(degree_to_rad(b)),
138
+ # 1, 0, 0, &block)
139
+ # end
140
+
141
+ # Transform the user space (see notes for rotate regarding graphics state)
142
+ # Generally, one would use the rotate, scale, translate, and skew
143
+ # convenience methods instead of calling transformation_matrix directly
144
+ def transformation_matrix(a, b, c, d, e, f)
145
+ values = [a, b, c, d, e, f].map { |x| "%.5f" % x }.join(" ")
146
+ save_graphics_state if block_given?
147
+ add_content "#{values} cm"
148
+ if block_given?
149
+ yield
150
+ restore_graphics_state
151
+ end
152
+ end
153
+
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,99 @@
1
+ # encoding: utf-8
2
+ #
3
+ # transparency.rb : Implements transparency
4
+ #
5
+ # Copyright October 2009, Daniel Nelson. All Rights Reserved.
6
+ #
7
+ # This is free software. Please see the LICENSE and COPYING files for details.
8
+ #
9
+
10
+ module Prawn
11
+ module Graphics
12
+
13
+ # The Prawn::Transparency module is used to place transparent
14
+ # content on the page. It has the capacity for separate
15
+ # transparency values for stroked content and all other content.
16
+ #
17
+ # Example:
18
+ # # both the fill and stroke will be at 50% opacity
19
+ # pdf.transparent(0.5) do
20
+ # pdf.text("hello world")
21
+ # pdf.fill_and_stroke_circle_at([x, y], :radius => 25)
22
+ # end
23
+ #
24
+ # # the fill will be at 50% opacity, but the stroke will
25
+ # # be at 75% opacity
26
+ # pdf.transparent(0.5, 0.75) do
27
+ # pdf.text("hello world")
28
+ # pdf.fill_and_stroke_circle_at([x, y], :radius => 25)
29
+ # end
30
+ #
31
+ module Transparency
32
+
33
+ # Sets the <tt>opacity</tt> and <tt>stroke_opacity</tt> for all
34
+ # the content within the <tt>block</tt>
35
+ # If <tt>stroke_opacity</tt> is not provided, then it takes on
36
+ # the same value as <tt>opacity</tt>
37
+ #
38
+ # Valid ranges for both paramters are 0.0 to 1.0
39
+ #
40
+ # Example:
41
+ # # both the fill and stroke will be at 50% opacity
42
+ # pdf.transparent(0.5) do
43
+ # pdf.text("hello world")
44
+ # pdf.fill_and_stroke_circle_at([x, y], :radius => 25)
45
+ # end
46
+ #
47
+ # # the fill will be at 50% opacity, but the stroke will
48
+ # # be at 75% opacity
49
+ # pdf.transparent(0.5, 0.75) do
50
+ # pdf.text("hello world")
51
+ # pdf.fill_and_stroke_circle_at([x, y], :radius => 25)
52
+ # end
53
+ #
54
+ def transparent(opacity, stroke_opacity=opacity, &block)
55
+ min_version(1.4)
56
+
57
+ opacity = [[opacity, 0.0].max, 1.0].min
58
+ stroke_opacity = [[stroke_opacity, 0.0].max, 1.0].min
59
+
60
+ save_graphics_state
61
+ add_content "/#{opacity_dictionary_name(opacity, stroke_opacity)} gs"
62
+ yield if block_given?
63
+ restore_graphics_state
64
+ end
65
+
66
+ private
67
+
68
+ def opacity_dictionary_registry
69
+ @opacity_dictionary_registry ||= {}
70
+ end
71
+
72
+ def next_opacity_dictionary_id
73
+ opacity_dictionary_registry.length + 1
74
+ end
75
+
76
+ def opacity_dictionary_name(opacity, stroke_opacity)
77
+ key = "#{opacity}_#{stroke_opacity}"
78
+
79
+ if opacity_dictionary_registry[key]
80
+ dictionary = opacity_dictionary_registry[key][:obj]
81
+ dictionary_name = opacity_dictionary_registry[key][:name]
82
+ else
83
+ dictionary = ref!(:Type => :ExtGState,
84
+ :CA => stroke_opacity,
85
+ :ca => opacity
86
+ )
87
+
88
+ dictionary_name = "Tr#{next_opacity_dictionary_id}"
89
+ opacity_dictionary_registry[key] = { :name => dictionary_name,
90
+ :obj => dictionary }
91
+ end
92
+
93
+ page.ext_gstates.merge!(dictionary_name => dictionary)
94
+ dictionary_name
95
+ end
96
+
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,321 @@
1
+ # encoding: utf-8
2
+
3
+ # graphics.rb : Implements PDF drawing primitives
4
+ #
5
+ # Copyright April 2008, Gregory Brown. All Rights Reserved.
6
+ #
7
+ # This is free software. Please see the LICENSE and COPYING files for details.
8
+
9
+ require "prawn/graphics/color"
10
+ require "prawn/graphics/dash"
11
+ require "prawn/graphics/cap_style"
12
+ require "prawn/graphics/join_style"
13
+ require "prawn/graphics/transparency"
14
+ require "prawn/graphics/transformation"
15
+
16
+ module Prawn
17
+
18
+ # Implements the drawing facilities for Prawn::Document.
19
+ # Use this to draw the most beautiful imaginable things.
20
+ #
21
+ # This file lifts and modifies several of PDF::Writer's graphics functions
22
+ # ruby-pdf.rubyforge.org
23
+ #
24
+ module Graphics
25
+
26
+ include Color
27
+ include Dash
28
+ include CapStyle
29
+ include JoinStyle
30
+ include Transparency
31
+ include Transformation
32
+
33
+ #######################################################################
34
+ # Low level drawing operations must map the point to absolute coords! #
35
+ #######################################################################
36
+
37
+ # Moves the drawing position to a given point. The point can be
38
+ # specified as a tuple or a flattened argument list
39
+ #
40
+ # pdf.move_to [100,50]
41
+ # pdf.move_to(100,50)
42
+ #
43
+ def move_to(*point)
44
+ x,y = map_to_absolute(point)
45
+ add_content("%.3f %.3f m" % [ x, y ])
46
+ end
47
+
48
+ # Draws a line from the current drawing position to the specified point.
49
+ # The destination may be described as a tuple or a flattened list:
50
+ #
51
+ # pdf.line_to [50,50]
52
+ # pdf.line_to(50,50)
53
+ #
54
+ def line_to(*point)
55
+ x,y = map_to_absolute(point)
56
+ add_content("%.3f %.3f l" % [ x, y ])
57
+ end
58
+
59
+ # Draws a Bezier curve from the current drawing position to the
60
+ # specified point, bounded by two additional points.
61
+ #
62
+ # pdf.curve_to [100,100], :bounds => [[90,90],[75,75]]
63
+ #
64
+ def curve_to(dest,options={})
65
+ options[:bounds] or raise Prawn::Errors::InvalidGraphicsPath,
66
+ "Bounding points for bezier curve must be specified "+
67
+ "as :bounds => [[x1,y1],[x2,y2]]"
68
+
69
+ curve_points = (options[:bounds] << dest).map { |e| map_to_absolute(e) }
70
+ add_content("%.3f %.3f %.3f %.3f %.3f %.3f c" %
71
+ curve_points.flatten )
72
+ end
73
+
74
+ # Draws a rectangle given <tt>point</tt>, <tt>width</tt> and
75
+ # <tt>height</tt>. The rectangle is bounded by its upper-left corner.
76
+ #
77
+ # pdf.rectangle [300,300], 100, 200
78
+ #
79
+ def rectangle(point,width,height)
80
+ x,y = map_to_absolute(point)
81
+ add_content("%.3f %.3f %.3f %.3f re" % [ x, y - height, width, height ])
82
+ end
83
+
84
+ # Draws a rounded rectangle given <tt>point</tt>, <tt>width</tt> and
85
+ # <tt>height</tt> and <tt>radius</tt> for the rounded corner. The rectangle
86
+ # is bounded by its upper-left corner.
87
+ #
88
+ # pdf.rounded_rectangle [300,300], 100, 200, 10
89
+ #
90
+ def rounded_rectangle(point,width,height,radius)
91
+ x, y = point
92
+ rounded_polygon(radius, point, [x + width, y], [x + width, y - height], [x, y - height])
93
+ end
94
+
95
+
96
+ ###########################################################
97
+ # Higher level functions: May use relative coords #
98
+ ###########################################################
99
+
100
+ # Sets line thickness to the <tt>width</tt> specified.
101
+ #
102
+ def line_width=(width)
103
+ @line_width = width
104
+ add_content("#{width} w")
105
+ end
106
+
107
+ # When called without an argument, returns the current line thickness.
108
+ # When called with an argument, sets the line thickness to the specified
109
+ # value (in PDF points)
110
+ #
111
+ # pdf.line_width #=> 1
112
+ # pdf.line_width(5)
113
+ # pdf.line_width #=> 5
114
+ #
115
+ def line_width(width=nil)
116
+ if width
117
+ self.line_width = width
118
+ else
119
+ (defined?(@line_width) && @line_width) || 1
120
+ end
121
+ end
122
+
123
+ # Draws a line from one point to another. Points may be specified as
124
+ # tuples or flattened argument list:
125
+ #
126
+ # pdf.line [100,100], [200,250]
127
+ # pdf.line(100,100,200,250)
128
+ #
129
+ def line(*points)
130
+ x0,y0,x1,y1 = points.flatten
131
+ move_to(x0, y0)
132
+ line_to(x1, y1)
133
+ end
134
+
135
+ # Draws a horizontal line from <tt>x1</tt> to <tt>x2</tt> at the
136
+ # current <tt>y</tt> position, or the position specified by the :at option.
137
+ #
138
+ # # draw a line from [25, 75] to [100, 75]
139
+ # horizontal_line 25, 100, :at => 75
140
+ #
141
+ def horizontal_line(x1,x2,options={})
142
+ if options[:at]
143
+ y1 = options[:at]
144
+ else
145
+ y1 = y - bounds.absolute_bottom
146
+ end
147
+
148
+ line(x1,y1,x2,y1)
149
+ end
150
+
151
+ # Draws a horizontal line from the left border to the right border of the
152
+ # bounding box at the current <tt>y</tt> position.
153
+ #
154
+ def horizontal_rule
155
+ horizontal_line(bounds.left, bounds.right)
156
+ end
157
+
158
+ # Draws a vertical line at the x cooordinate given by :at from y1 to y2.
159
+ #
160
+ # # draw a line from [25, 100] to [25, 300]
161
+ # vertical_line 100, 300, :at => 25
162
+ #
163
+ def vertical_line(y1,y2,params)
164
+ line(params[:at],y1,params[:at],y2)
165
+ end
166
+
167
+ # Draws a Bezier curve between two points, bounded by two additional
168
+ # points
169
+ #
170
+ # pdf.curve [50,100], [100,100], :bounds => [[90,90],[75,75]]
171
+ #
172
+ def curve(origin,dest, options={})
173
+ move_to(*origin)
174
+ curve_to(dest,options)
175
+ end
176
+
177
+ # This constant is used to approximate a symmetrical arc using a cubic
178
+ # Bezier curve.
179
+ #
180
+ KAPPA = 4.0 * ((Math.sqrt(2) - 1.0) / 3.0)
181
+
182
+ # Draws a circle of radius <tt>:radius</tt> with the centre-point at <tt>point</tt>
183
+ # as a complete subpath. The drawing point will be moved to the
184
+ # centre-point upon completion of the drawing the circle.
185
+ #
186
+ # pdf.circle_at [100,100], :radius => 25
187
+ #
188
+ def circle_at(point, options)
189
+ x,y = point
190
+ ellipse_at [x, y], options[:radius]
191
+ end
192
+
193
+ # Draws an ellipse of +x+ radius <tt>r1</tt> and +y+ radius <tt>r2</tt>
194
+ # with the centre-point at <tt>point</tt> as a complete subpath. The
195
+ # drawing point will be moved to the centre-point upon completion of the
196
+ # drawing the ellipse.
197
+ #
198
+ # # draws an ellipse with x-radius 25 and y-radius 50
199
+ # pdf.ellipse_at [100,100], 25, 50
200
+ #
201
+ def ellipse_at(point, r1, r2 = r1)
202
+ x, y = point
203
+ l1 = r1 * KAPPA
204
+ l2 = r2 * KAPPA
205
+
206
+ move_to(x + r1, y)
207
+
208
+ # Upper right hand corner
209
+ curve_to [x, y + r2],
210
+ :bounds => [[x + r1, y + l1], [x + l2, y + r2]]
211
+
212
+ # Upper left hand corner
213
+ curve_to [x - r1, y],
214
+ :bounds => [[x - l2, y + r2], [x - r1, y + l1]]
215
+
216
+ # Lower left hand corner
217
+ curve_to [x, y - r2],
218
+ :bounds => [[x - r1, y - l1], [x - l2, y - r2]]
219
+
220
+ # Lower right hand corner
221
+ curve_to [x + r1, y],
222
+ :bounds => [[x + l2, y - r2], [x + r1, y - l1]]
223
+
224
+ move_to(x, y)
225
+ end
226
+
227
+ # Draws a polygon from the specified points.
228
+ #
229
+ # # draws a snazzy triangle
230
+ # pdf.polygon [100,100], [100,200], [200,200]
231
+ #
232
+ def polygon(*points)
233
+ move_to points[0]
234
+ (points[1..-1] << points[0]).each do |point|
235
+ line_to(*point)
236
+ end
237
+ end
238
+
239
+ # Draws a rounded polygon from specified points using the radius to define bezier curves
240
+ #
241
+ # # draws a rounded filled in polygon
242
+ # pdf.fill_and_stroke_rounded_polygon(10, [100, 250], [200, 300], [300, 250],
243
+ # [300, 150], [200, 100], [100, 150])
244
+ def rounded_polygon(radius, *points)
245
+ move_to point_on_line(radius, points[1], points[0])
246
+ sides = points.size
247
+ points << points[0] << points[1]
248
+ (sides).times do |i|
249
+ rounded_vertex(radius, points[i], points[i + 1], points[i + 2])
250
+ end
251
+ end
252
+
253
+
254
+ # Creates a rounded vertex for a line segment used for building a rounded polygon
255
+ # requires a radius to define bezier curve and three points. The first two points define
256
+ # the line segment and the third point helps define the curve for the vertex.
257
+ def rounded_vertex(radius, *points)
258
+ x0,y0,x1,y1,x2,y2 = points.flatten
259
+ radial_point_1 = point_on_line(radius, points[0], points[1])
260
+ bezier_point_1 = point_on_line((radius - radius*KAPPA), points[0], points[1] )
261
+ radial_point_2 = point_on_line(radius, points[2], points[1])
262
+ bezier_point_2 = point_on_line((radius - radius*KAPPA), points[2], points[1])
263
+ line_to(radial_point_1)
264
+ curve_to(radial_point_2, :bounds => [bezier_point_1, bezier_point_2])
265
+ end
266
+
267
+ # Strokes and closes the current path. See Graphic::Color for color details
268
+ #
269
+ def stroke
270
+ yield if block_given?
271
+ add_content "S"
272
+ end
273
+
274
+ # Draws and strokes a rectangle represented by the current bounding box
275
+ #
276
+ def stroke_bounds
277
+ stroke_rectangle bounds.top_left, bounds.width, bounds.height
278
+ end
279
+
280
+ # Fills and closes the current path. See Graphic::Color for color details
281
+ #
282
+ def fill
283
+ yield if block_given?
284
+ add_content "f"
285
+ end
286
+
287
+ # Fills, strokes, and closes the current path. See Graphic::Color for color details
288
+ #
289
+ def fill_and_stroke
290
+ yield if block_given?
291
+ add_content "b"
292
+ end
293
+
294
+ private
295
+
296
+ def map_to_absolute(*point)
297
+ x,y = point.flatten
298
+ [@bounding_box.absolute_left + x, @bounding_box.absolute_bottom + y]
299
+ end
300
+
301
+ def map_to_absolute!(point)
302
+ point.replace(map_to_absolute(point))
303
+ end
304
+
305
+ def degree_to_rad(angle)
306
+ angle * Math::PI / 180
307
+ end
308
+
309
+ # Returns the coordinates for a point on a line that is a given distance away from the second
310
+ # point defining the line segement
311
+ def point_on_line(distance_from_end, *points)
312
+ x0,y0,x1,y1 = points.flatten
313
+ length = Math.sqrt((x1 - x0)**2 + (y1 - y0)**2)
314
+ p = (length - distance_from_end) / length
315
+ xr = x0 + p*(x1 - x0)
316
+ yr = y0 + p*(y1 - y0)
317
+ [xr, yr]
318
+ end
319
+
320
+ end
321
+ end
@@ -0,0 +1,46 @@
1
+ # encoding: ASCII-8BIT
2
+
3
+ # jpg.rb : Extracts the data from a JPG that is needed for embedding
4
+ #
5
+ # Copyright April 2008, James Healy. All Rights Reserved.
6
+ #
7
+ # This is free software. Please see the LICENSE and COPYING files for details.
8
+
9
+ require 'stringio'
10
+
11
+ module Prawn
12
+ module Images
13
+ # A convenience class that wraps the logic for extracting the parts
14
+ # of a JPG image that we need to embed them in a PDF
15
+ #
16
+ class JPG
17
+ attr_reader :width, :height, :bits, :channels
18
+ attr_accessor :scaled_width, :scaled_height
19
+
20
+ JPEG_SOF_BLOCKS = %W(\xc0 \xc1 \xc2 \xc3 \xc5 \xc6 \xc7 \xc9 \xca \xcb \xcd \xce \xcf)
21
+ JPEG_APP_BLOCKS = %W(\xe0 \xe1 \xe2 \xe3 \xe4 \xe5 \xe6 \xe7 \xe8 \xe9 \xea \xeb \xec \xed \xee \xef)
22
+
23
+ # Process a new JPG image
24
+ #
25
+ # <tt>:data</tt>:: A binary string of JPEG data
26
+ #
27
+ def initialize(data)
28
+ data = StringIO.new(data.dup)
29
+
30
+ c_marker = "\xff" # Section marker.
31
+ data.read(2) # Skip the first two bytes of JPEG identifier.
32
+ loop do
33
+ marker, code, length = data.read(4).unpack('aan')
34
+ raise "JPEG marker not found!" if marker != c_marker
35
+
36
+ if JPEG_SOF_BLOCKS.include?(code)
37
+ @bits, @height, @width, @channels = data.read(6).unpack("CnnC")
38
+ break
39
+ end
40
+
41
+ buffer = data.read(length - 2)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end