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
@@ -0,0 +1,136 @@
1
+ # encoding: utf-8
2
+
3
+ # page_geometry.rb : Describes PDF page geometries
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
+ module Prawn
10
+ class Document
11
+
12
+ # Dimensions pulled from PDF::Writer, rubyforge.org/projects/ruby-pdf
13
+ #
14
+ # All of these dimensions are in PDF Points, see Prawn::Measurements for
15
+ # conversion utilities.
16
+ #
17
+ # Additionally, if the size you are after is not listed below, you can always
18
+ # specify your size by passing an array of width and height to Prawn::Document.new
19
+ # like:
20
+ #
21
+ # Prawn::Document.new(:page_size => [1000, 20000])
22
+ #
23
+ # The sizes below can be used by passing the appropriate string to :size:
24
+ #
25
+ # Prawn::Document.new(:page_size => '2A0')
26
+ #
27
+ # ===Inbuilt Sizes:
28
+ #
29
+ #
30
+ # 4A0:: => 4767.87 x 6740.79
31
+ # 2A0:: => 3370.39 x 4767.87
32
+ # A0:: => 2383.94 x 3370.39
33
+ # A1:: => 1683.78 x 2383.94
34
+ # A2:: => 1190.55 x 1683.78
35
+ # A3:: => 841.89 x 1190.55
36
+ # A4:: => 595.28 x 841.89
37
+ # A5:: => 419.53 x 595.28
38
+ # A6:: => 297.64 x 419.53
39
+ # A7:: => 209.76 x 297.64
40
+ # A8:: => 147.40 x 209.76
41
+ # A9:: => 104.88 x 147.40
42
+ # A10:: => 73.70 x 104.88
43
+ # B0:: => 2834.65 x 4008.19
44
+ # B1:: => 2004.09 x 2834.65
45
+ # B2:: => 1417.32 x 2004.09
46
+ # B3:: => 1000.63 x 1417.32
47
+ # B4:: => 708.66 x 1000.63
48
+ # B5:: => 498.90 x 708.66
49
+ # B6:: => 354.33 x 498.90
50
+ # B7:: => 249.45 x 354.33
51
+ # B8:: => 175.75 x 249.45
52
+ # B9:: => 124.72 x 175.75
53
+ # B10:: => 87.87 x 124.72
54
+ # C0:: => 2599.37 x 3676.54
55
+ # C1:: => 1836.85 x 2599.37
56
+ # C2:: => 1298.27 x 1836.85
57
+ # C3:: => 918.43 x 1298.27
58
+ # C4:: => 649.13 x 918.43
59
+ # C5:: => 459.21 x 649.13
60
+ # C6:: => 323.15 x 459.21
61
+ # C7:: => 229.61 x 323.15
62
+ # C8:: => 161.57 x 229.61
63
+ # C9:: => 113.39 x 161.57
64
+ # C10:: => 79.37 x 113.39
65
+ # RA0:: => 2437.80 x 3458.27
66
+ # RA1:: => 1729.13 x 2437.80
67
+ # RA2:: => 1218.90 x 1729.13
68
+ # RA3:: => 864.57 x 1218.90
69
+ # RA4:: => 609.45 x 864.57
70
+ # SRA0:: => 2551.18 x 3628.35
71
+ # SRA1:: => 1814.17 x 2551.18
72
+ # SRA2:: => 1275.59 x 1814.17
73
+ # SRA3:: => 907.09 x 1275.59
74
+ # SRA4:: => 637.80 x 907.09
75
+ # EXECUTIVE:: => 521.86 x 756.00
76
+ # FOLIO:: => 612.00 x 936.00
77
+ # LEGAL:: => 612.00 x 1008.00
78
+ # LETTER:: => 612.00 x 792.00
79
+ # TABLOID:: => 792.00 x 1224.00
80
+ #
81
+ module PageGeometry
82
+
83
+ SIZES = { "4A0" => [4767.87, 6740.79],
84
+ "2A0" => [3370.39, 4767.87],
85
+ "A0" => [2383.94, 3370.39],
86
+ "A1" => [1683.78, 2383.94],
87
+ "A2" => [1190.55, 1683.78],
88
+ "A3" => [841.89, 1190.55],
89
+ "A4" => [595.28, 841.89],
90
+ "A5" => [419.53, 595.28],
91
+ "A6" => [297.64, 419.53],
92
+ "A7" => [209.76, 297.64],
93
+ "A8" => [147.40, 209.76],
94
+ "A9" => [104.88, 147.40],
95
+ "A10" => [73.70, 104.88],
96
+ "B0" => [2834.65, 4008.19],
97
+ "B1" => [2004.09, 2834.65],
98
+ "B2" => [1417.32, 2004.09],
99
+ "B3" => [1000.63, 1417.32],
100
+ "B4" => [708.66, 1000.63],
101
+ "B5" => [498.90, 708.66],
102
+ "B6" => [354.33, 498.90],
103
+ "B7" => [249.45, 354.33],
104
+ "B8" => [175.75, 249.45],
105
+ "B9" => [124.72, 175.75],
106
+ "B10" => [87.87, 124.72],
107
+ "C0" => [2599.37, 3676.54],
108
+ "C1" => [1836.85, 2599.37],
109
+ "C2" => [1298.27, 1836.85],
110
+ "C3" => [918.43, 1298.27],
111
+ "C4" => [649.13, 918.43],
112
+ "C5" => [459.21, 649.13],
113
+ "C6" => [323.15, 459.21],
114
+ "C7" => [229.61, 323.15],
115
+ "C8" => [161.57, 229.61],
116
+ "C9" => [113.39, 161.57],
117
+ "C10" => [79.37, 113.39],
118
+ "RA0" => [2437.80, 3458.27],
119
+ "RA1" => [1729.13, 2437.80],
120
+ "RA2" => [1218.90, 1729.13],
121
+ "RA3" => [864.57, 1218.90],
122
+ "RA4" => [609.45, 864.57],
123
+ "SRA0" => [2551.18, 3628.35],
124
+ "SRA1" => [1814.17, 2551.18],
125
+ "SRA2" => [1275.59, 1814.17],
126
+ "SRA3" => [907.09, 1275.59],
127
+ "SRA4" => [637.80, 907.09],
128
+ "EXECUTIVE" => [521.86, 756.00],
129
+ "FOLIO" => [612.00, 936.00],
130
+ "LEGAL" => [612.00, 1008.00],
131
+ "LETTER" => [612.00, 792.00],
132
+ "TABLOID" => [792.00, 1224.00] }
133
+
134
+ end
135
+ end
136
+ end
@@ -10,9 +10,8 @@ require 'delegate'
10
10
 
11
11
  module Prawn
12
12
  class Document
13
- # @private
14
-
15
13
  module Snapshot
14
+
16
15
  RollbackTransaction = Class.new(StandardError)
17
16
 
18
17
  # Call this within a +transaction+ block to roll back the transaction and
@@ -28,7 +27,7 @@ module Prawn
28
27
  # +rollback+ is called or a RollbackTransaction exception is raised
29
28
  # inside the block, all actions taken inside the block will be rolled
30
29
  # back (with the exception of y-position, which you must restore
31
- # yourself).
30
+ # yourself).
32
31
  #
33
32
  # Returns true on success, or false if the transaction was rolled back.
34
33
  #
@@ -42,7 +41,7 @@ module Prawn
42
41
  end
43
42
 
44
43
  private
45
-
44
+
46
45
  # Takes a current snapshot of the document's state, sufficient to
47
46
  # reconstruct it after it was amended.
48
47
  #
@@ -51,10 +50,10 @@ module Prawn
51
50
  # monotonically as data is added to the document, so we share that
52
51
  # between the old and new copies.
53
52
  {:page_content => state.page.content.deep_copy,
54
- :current_page => state.page.dictionary.deep_copy([:Parent]),
53
+ :current_page => state.page.dictionary.deep_copy(share=[:Parent]),
55
54
  :bounds => bounds.deep_copy,
56
55
  :page_number => page_number,
57
- :page_kids => state.store.pages.data[:Kids].compact.map{|kid| kid.identifier},
56
+ :page_kids => state.store.pages.data[:Kids].map{|kid| kid.identifier},
58
57
  :dests => names? && names.data[:Dests].deep_copy}
59
58
  end
60
59
 
@@ -81,7 +80,7 @@ module Prawn
81
80
  self.bounds = BoundingBox.restore_deep_copy(shot[:bounds], self)
82
81
 
83
82
  if shot[:dests]
84
- names.data[:Dests] = shot[:dests]
83
+ names.data[:Dests] = shot[:dests]
85
84
  end
86
85
  end
87
86
 
@@ -8,10 +8,8 @@
8
8
 
9
9
  module Prawn
10
10
  class Document
11
- # @group Stable API
12
-
13
- # A span is a special purpose bounding box that allows a column of
14
- # elements to be positioned relative to the margin_box.
11
+ # A span is a special purpose bounding box that allows a column of
12
+ # elements to be positioned relative to the margin_box.
15
13
  #
16
14
  # Arguments:
17
15
  # +width+:: The width of the column in PDF points
@@ -19,23 +17,23 @@ module Prawn
19
17
  # Options:
20
18
  # <tt>:position</tt>:: One of :left, :center, :right or an x offset
21
19
  #
22
- # This method is typically used for flowing a column of text from one
20
+ # This method is typically used for flowing a column of text from one
23
21
  # page to the next.
24
22
  #
25
23
  # span(350, :position => :center) do
26
24
  # text "Here's some centered text in a 350 point column. " * 100
27
25
  # end
28
- #
26
+ #
29
27
  def span(width, options={})
30
28
  Prawn.verify_options [:position], options
31
- original_position = self.y
32
-
29
+ original_position = self.y
30
+
33
31
  # FIXME: Any way to move this upstream?
34
32
  left_boundary = case(options[:position] || :left)
35
33
  when :left
36
34
  margin_box.absolute_left
37
35
  when :center
38
- margin_box.absolute_left + margin_box.width / 2.0 - width / 2.0
36
+ margin_box.absolute_left + margin_box.width / 2.0 - width /2.0
39
37
  when :right
40
38
  margin_box.absolute_right - width
41
39
  when Numeric
@@ -43,15 +41,15 @@ module Prawn
43
41
  else
44
42
  raise ArgumentError, "Invalid option for :position"
45
43
  end
46
-
44
+
47
45
  # we need to bust out of whatever nested bounding boxes we're in.
48
46
  canvas do
49
- bounding_box([left_boundary,
47
+ bounding_box([left_boundary,
50
48
  margin_box.absolute_top], :width => width) do
51
49
  self.y = original_position
52
50
  yield
53
51
  end
54
- end
52
+ end
55
53
  end
56
54
  end
57
55
  end
@@ -5,7 +5,7 @@
5
5
  # This is free software. Please see the LICENSE and COPYING files for details.
6
6
  #
7
7
  module Prawn
8
- module Encoding # @private
8
+ module Encoding
9
9
  # Map between unicode and WinAnsiEnoding
10
10
  #
11
11
  class WinAnsi #:nodoc:
@@ -18,7 +18,7 @@ module Prawn
18
18
  .notdef .notdef .notdef .notdef
19
19
  .notdef .notdef .notdef .notdef
20
20
  .notdef .notdef .notdef .notdef
21
-
21
+
22
22
  space exclam quotedbl numbersign
23
23
  dollar percent ampersand quotesingle
24
24
  parenleft parenright asterisk plus
@@ -54,7 +54,7 @@ module Prawn
54
54
  quotedblright bullet endash emdash
55
55
  tilde trademark scaron guilsinglright
56
56
  oe .notdef zcaron ydieresis
57
-
57
+
58
58
  space exclamdown cent sterling
59
59
  currency yen brokenbar section
60
60
  dieresis copyright ordfeminine guillemotleft
@@ -81,10 +81,10 @@ module Prawn
81
81
  ocircumflex otilde odieresis divide
82
82
  oslash ugrave uacute ucircumflex
83
83
  udieresis yacute thorn ydieresis
84
- ]
85
-
84
+ ]
85
+
86
86
  def initialize
87
- @mapping_file = "#{Prawn::DATADIR}/encodings/win_ansi.txt"
87
+ @mapping_file = "#{Prawn::BASEDIR}/data/encodings/win_ansi.txt"
88
88
  load_mapping if self.class.mapping.empty?
89
89
  end
90
90
 
@@ -100,7 +100,7 @@ module Prawn
100
100
  # Replace anything else with an underscore
101
101
  self.class.mapping[codepoint] || 95
102
102
  end
103
-
103
+
104
104
  def self.mapping
105
105
  @mapping ||= {}
106
106
  end
@@ -108,9 +108,10 @@ module Prawn
108
108
  private
109
109
 
110
110
  def load_mapping
111
- File.open(@mapping_file, "r:BINARY") do |f|
111
+ RUBY_VERSION >= "1.9" ? mode = "r:BINARY" : mode = "r"
112
+ File.open(@mapping_file, mode) do |f|
112
113
  f.each do |l|
113
- _, single_byte, unicode = *l.match(/([0-9A-Za-z]+);([0-9A-F]{4})/)
114
+ m, single_byte, unicode = *l.match(/([0-9A-Za-z]+);([0-9A-F]{4})/)
114
115
  self.class.mapping["0x#{unicode}".hex] = "0x#{single_byte}".hex if single_byte
115
116
  end
116
117
  end
data/lib/prawn/errors.rb CHANGED
@@ -8,18 +8,25 @@
8
8
  #
9
9
  module Prawn
10
10
  module Errors
11
- # Raised when a table is spanned in an impossible way.
11
+
12
+ # This error is raised when Prawn::PdfObject() encounters a Ruby object it
13
+ # cannot convert to PDF
12
14
  #
13
- InvalidTableSpan = Class.new(StandardError)
14
-
15
- # This error is raised when a method requiring a current page is called
15
+ FailedObjectConversion = Class.new(StandardError)
16
+
17
+ # This error is raised when Document#page_layout is set to anything
18
+ # other than :portrait or :landscape
19
+ #
20
+ InvalidPageLayout = Class.new(StandardError)
21
+
22
+ # This error is raised when a method requiring a current page is called
16
23
  # without being on a page.
17
24
  #
18
25
  NotOnPage = Class.new(StandardError)
19
-
20
- # This error is raised when Prawn cannot find a specified font
26
+
27
+ # This error is raised when Prawn cannot find a specified font
21
28
  #
22
- UnknownFont = Class.new(StandardError)
29
+ UnknownFont = Class.new(StandardError)
23
30
 
24
31
  # Raised when Prawn is asked to draw something into a too-small box
25
32
  #
@@ -28,18 +35,18 @@ module Prawn
28
35
  # Raised if group() is called with a block that is too big to be
29
36
  # rendered in the current context.
30
37
  #
31
- CannotGroup = Class.new(StandardError)
38
+ CannotGroup = Class.new(StandardError)
32
39
 
33
40
  # This error is raised when Prawn is being used on a M17N aware VM,
34
41
  # and the user attempts to add text that isn't compatible with UTF-8
35
42
  # to their document
36
43
  #
37
44
  IncompatibleStringEncoding = Class.new(StandardError)
38
-
45
+
39
46
  # This error is raised when Prawn encounters an unknown key in functions
40
47
  # that accept an options hash. This usually means there is a typo in your
41
48
  # code or that the option you are trying to use has a different name than
42
- # what you have specified.
49
+ # what you have specified.
43
50
  #
44
51
  UnknownOption = Class.new(StandardError)
45
52
 
@@ -59,22 +66,30 @@ module Prawn
59
66
  # This error is raised when an object is attempted to be
60
67
  # referenced by name, but no such name is associated with an object
61
68
  UndefinedObjectName = Class.new(StandardError)
62
-
69
+
63
70
  # This error is raised when a required option has not been set
64
71
  RequiredOption = Class.new(StandardError)
65
-
72
+
66
73
  # This error is raised when a requested outline item with a given title does not exist
67
74
  UnknownOutlineTitle = Class.new(StandardError)
68
75
 
69
76
  # This error is raised when a block is required, but not provided
70
77
  BlockRequired = Class.new(StandardError)
71
-
78
+
72
79
  # This error is rased when a graphics method is called with improper arguments
73
80
  InvalidGraphicsPath = Class.new(StandardError)
81
+
82
+ # This error is raised when Prawn fails to load a template file
83
+ #
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)
74
89
 
75
90
  # Raised when unrecognized content is provided for a table cell.
76
91
  #
77
92
  UnrecognizedTableContent = Class.new(StandardError)
78
-
93
+
79
94
  end
80
- end
95
+ end
data/lib/prawn/font.rb CHANGED
@@ -6,16 +6,13 @@
6
6
  #
7
7
  # This is free software. Please see the LICENSE and COPYING files for details.
8
8
  #
9
- require_relative "font/afm"
10
- require_relative "font/ttf"
11
- require_relative "font/dfont"
12
- require_relative "font_metric_cache"
9
+ require "prawn/font/afm"
10
+ require "prawn/font/ttf"
11
+ require "prawn/font/dfont"
13
12
 
14
13
  module Prawn
15
14
 
16
15
  class Document
17
- # @group Stable API
18
-
19
16
  # Without arguments, this returns the currently selected font. Otherwise,
20
17
  # it sets the current font. When a block is used, the font is applied
21
18
  # transactionally and is rolled back when the block exits.
@@ -41,7 +38,7 @@ module Prawn
41
38
  #
42
39
  # If a ttf font is specified, the glyphs necessary to render your document
43
40
  # will be embedded in the rendered PDF. This should be your preferred option
44
- # in most cases. It will increase the size of the resulting file, but also
41
+ # in most cases. It will increase the size of the resulting file, but also
45
42
  # make it more portable.
46
43
  #
47
44
  # The options parameter is an optional hash providing size and style. To use
@@ -52,9 +49,9 @@ module Prawn
52
49
  return((defined?(@font) && @font) || font("Helvetica")) if name.nil?
53
50
 
54
51
  if state.pages.empty? && !state.page.in_stamp_stream?
55
- raise Prawn::Errors::NotOnPage
52
+ raise Prawn::Errors::NotOnPage
56
53
  end
57
-
54
+
58
55
  new_font = find_font(name.to_s, options)
59
56
 
60
57
  if block_given?
@@ -69,10 +66,7 @@ module Prawn
69
66
  @font
70
67
  end
71
68
 
72
- # @method font_size(points=nil)
73
- #
74
69
  # When called with no argument, returns the current font size.
75
- #
76
70
  # When called with a single argument but no block, sets the current font
77
71
  # size. When a block is used, the font size is applied transactionally and
78
72
  # is rolled back when the block exits. You may still change the font size
@@ -103,44 +97,59 @@ module Prawn
103
97
  @font_size = size_before_yield
104
98
  end
105
99
 
106
- # Sets the font size
107
- def font_size=(size)
108
- font_size(size)
100
+ # Sets the font directly, given an actual Font object
101
+ # and size.
102
+ #
103
+ def set_font(font, size=nil) # :nodoc:
104
+ @font = font
105
+ @font_size = size if size
109
106
  end
110
107
 
111
- # Returns the width of the given string using the given font. If :size is not
112
- # specified as one of the options, the string is measured using the current
113
- # font size. You can also pass :kerning as an option to indicate whether
114
- # kerning should be used when measuring the width (defaults to +false+).
115
- #
116
- # Note that the string _must_ be encoded properly for the font being used.
117
- # For AFM fonts, this is WinAnsi. For TTF, make sure the font is encoded as
118
- # UTF-8. You can use the Font#normalize_encoding method to make sure strings
119
- # are in an encoding appropriate for the current font.
120
- #--
121
- # For the record, this method used to be a method of Font (and still delegates
122
- # to width computations on Font). However, having the primary interface for
123
- # calculating string widths exist on Font made it tricky to write extensions
124
- # for Prawn in which widths are computed differently (e.g., taking formatting
125
- # tags into account, or the like).
108
+ # Saves the current font, and then yields. When the block
109
+ # finishes, the original font is restored.
126
110
  #
127
- # By putting width_of here, on Document itself, extensions may easily override
128
- # it and redefine the width calculation behavior.
129
- #++
130
- def width_of(string, options={})
131
- if p = options[:inline_format]
132
- p = [] unless p.is_a?(Array)
111
+ def save_font
112
+ @font ||= find_font("Helvetica")
113
+ original_font = @font
114
+ original_size = @font_size
133
115
 
134
- # Build up an Arranger with the entire string on one line, finalize it,
135
- # and find its width.
136
- arranger = Prawn::Text::Formatted::Arranger.new(self, options)
137
- arranger.consumed = self.text_formatter.format(string, *p)
138
- arranger.finalize_line
116
+ yield
117
+ ensure
118
+ set_font(original_font, original_size) if original_font
119
+ end
139
120
 
140
- arranger.line_width
141
- else
142
- width_of_string(string, options)
143
- end
121
+ # Looks up the given font using the given criteria. Once a font has been
122
+ # found by that matches the criteria, it will be cached to subsequent lookups
123
+ # for that font will return the same object.
124
+ #--
125
+ # Challenges involved: the name alone is not sufficient to uniquely identify
126
+ # a font (think dfont suitcases that can hold multiple different fonts in a
127
+ # single file). Thus, the :name key is included in the cache key.
128
+ #
129
+ # It is further complicated, however, since fonts in some formats (like the
130
+ # dfont suitcases) can be identified either by numeric index, OR by their
131
+ # name within the suitcase, and both should hash to the same font object
132
+ # (to avoid the font being embedded multiple times). This is not yet implemented,
133
+ # which means if someone selects a font both by name, and by index, the
134
+ # font will be embedded twice. Since we do font subsetting, this double
135
+ # embedding won't be catastrophic, just annoying.
136
+ # ++
137
+ def find_font(name, options={}) #:nodoc:
138
+ if font_families.key?(name)
139
+ family, name = name, font_families[name][options[:style] || :normal]
140
+ if name.is_a?(Hash)
141
+ options = options.merge(name)
142
+ name = options[:file]
143
+ end
144
+ end
145
+ key = "#{name}:#{options[:font] || 0}"
146
+ font_registry[key] ||= Font.load(self, name, options.merge(:family => family))
147
+ end
148
+
149
+ # Hash of Font objects keyed by names
150
+ #
151
+ def font_registry #:nodoc:
152
+ @font_registry ||= {}
144
153
  end
145
154
 
146
155
  # Hash that maps font family names to their styled individual font names.
@@ -164,12 +173,13 @@ module Prawn
164
173
  # wish to support.
165
174
  #
166
175
  # By default the styles :bold, :italic, :bold_italic, and :normal are
167
- # defined for fonts "Courier", "Times-Roman" and "Helvetica". When
168
- # defining your own font families, you can map any or all of these
169
- # styles to whatever font files you'd like.
176
+ # defined for fonts "Courier", "Times-Roman" and "Helvetica".
177
+ #
178
+ # You probably want to provide those four styles, but are free to define
179
+ # custom ones, like :thin, and use them in font calls.
170
180
  #
171
181
  def font_families
172
- @font_families ||= {}.merge!(
182
+ @font_families ||= Hash.new.merge!(
173
183
  { "Courier" => { :bold => "Courier-Bold",
174
184
  :italic => "Courier-Oblique",
175
185
  :bold_italic => "Courier-BoldOblique",
@@ -187,79 +197,45 @@ module Prawn
187
197
  })
188
198
  end
189
199
 
190
- # @group Experimental API
191
-
192
- # Sets the font directly, given an actual Font object
193
- # and size.
194
- #
195
- def set_font(font, size=nil) # :nodoc:
196
- @font = font
197
- @font_size = size if size
198
- end
199
-
200
- # Saves the current font, and then yields. When the block
201
- # finishes, the original font is restored.
200
+ # Returns the width of the given string using the given font. If :size is not
201
+ # specified as one of the options, the string is measured using the current
202
+ # font size. You can also pass :kerning as an option to indicate whether
203
+ # kerning should be used when measuring the width (defaults to +false+).
202
204
  #
203
- def save_font
204
- @font ||= find_font("Helvetica")
205
- original_font = @font
206
- original_size = @font_size
207
-
208
- yield
209
- ensure
210
- set_font(original_font, original_size) if original_font
211
- end
212
-
213
- # Looks up the given font using the given criteria. Once a font has been
214
- # found by that matches the criteria, it will be cached to subsequent lookups
215
- # for that font will return the same object.
205
+ # Note that the string _must_ be encoded properly for the font being used.
206
+ # For AFM fonts, this is WinAnsi. For TTF, make sure the font is encoded as
207
+ # UTF-8. You can use the Font#normalize_encoding method to make sure strings
208
+ # are in an encoding appropriate for the current font.
216
209
  #--
217
- # Challenges involved: the name alone is not sufficient to uniquely identify
218
- # a font (think dfont suitcases that can hold multiple different fonts in a
219
- # single file). Thus, the :name key is included in the cache key.
220
- #
221
- # It is further complicated, however, since fonts in some formats (like the
222
- # dfont suitcases) can be identified either by numeric index, OR by their
223
- # name within the suitcase, and both should hash to the same font object
224
- # (to avoid the font being embedded multiple times). This is not yet implemented,
225
- # which means if someone selects a font both by name, and by index, the
226
- # font will be embedded twice. Since we do font subsetting, this double
227
- # embedding won't be catastrophic, just annoying.
228
- # ++
229
- #
230
- # @private
231
- def find_font(name, options={}) #:nodoc:
232
- if font_families.key?(name)
233
- family, name = name, font_families[name][options[:style] || :normal]
234
- if name.is_a?(::Hash)
235
- options = options.merge(name)
236
- name = options[:file]
237
- end
238
- end
239
- key = "#{name}:#{options[:font] || 0}"
240
- font_registry[key] ||= Font.load(self, name, options.merge(:family => family))
241
- end
242
-
243
- # Hash of Font objects keyed by names
210
+ # For the record, this method used to be a method of Font (and still delegates
211
+ # to width computations on Font). However, having the primary interface for
212
+ # calculating string widths exist on Font made it tricky to write extensions
213
+ # for Prawn in which widths are computed differently (e.g., taking formatting
214
+ # tags into account, or the like).
244
215
  #
245
- def font_registry #:nodoc:
246
- @font_registry ||= {}
247
- end
248
-
249
- private
250
-
251
- def width_of_inline_formatted_string(string, options={})
252
- # Build up an Arranger with the entire string on one line, finalize it,
253
- # and find its width.
254
- arranger = Prawn::Text::Formatted::Arranger.new(self, options)
255
- arranger.consumed = Text::Formatted::Parser.format(string)
256
- arranger.finalize_line
257
-
258
- arranger.line_width
259
- end
216
+ # By putting width_of here, on Document itself, extensions may easily override
217
+ # it and redefine the width calculation behavior.
218
+ #++
219
+ def width_of(string, options={})
220
+ if options[:inline_format]
221
+ # Build up an Arranger with the entire string on one line, finalize it,
222
+ # and find its width.
223
+ arranger = Core::Text::Formatted::Arranger.new(self, options)
224
+ arranger.consumed = Text::Formatted::Parser.to_array(string)
225
+ arranger.finalize_line
260
226
 
261
- def width_of_string(string, options={})
262
- font_metric_cache.width_of( string, options )
227
+ arranger.line_width
228
+ else
229
+ f = if options[:style]
230
+ # override style with :style => :bold
231
+ find_font(@font ? @font.name : 'Helvetica',
232
+ :style => options[:style])
233
+ else
234
+ font
235
+ end
236
+ f.compute_width_of(string, options) +
237
+ (character_spacing * font.character_count(string))
238
+ end
263
239
  end
264
240
  end
265
241
 
@@ -281,7 +257,7 @@ module Prawn
281
257
  # will be passed through to Font::AFM.new()
282
258
  #
283
259
  def self.load(document,name,options={})
284
- case name.to_s
260
+ case name
285
261
  when /\.ttf$/i then TTF.new(document, name, options)
286
262
  when /\.dfont$/i then DFont.new(document, name, options)
287
263
  when /\.afm$/i then AFM.new(document, name, options)
@@ -301,7 +277,7 @@ module Prawn
301
277
  @references = {}
302
278
  end
303
279
 
304
- # The size of the font ascender in PDF points
280
+ # The size of the font ascender in PDF points
305
281
  #
306
282
  def ascender
307
283
  @ascender / 1000.0 * size
@@ -319,6 +295,14 @@ module Prawn
319
295
  @line_gap / 1000.0 * size
320
296
  end
321
297
 
298
+ def identifier_for(subset)
299
+ "#{@identifier}.#{subset}".to_sym
300
+ end
301
+
302
+ def inspect
303
+ "#{self.class.name}< #{name}: #{size} >"
304
+ end
305
+
322
306
  # Normalizes the encoding of the string to an encoding supported by the
323
307
  # font. The string is expected to be UTF-8 going in. It will be re-encoded
324
308
  # and the new string will be returned. For an in-place (destructive)
@@ -364,22 +348,6 @@ module Prawn
364
348
  "#{self.class.name}< #{name}: #{size} >"
365
349
  end
366
350
 
367
- # Return a hash (as in Object#hash) for the font based on the output of
368
- # #inspect. This is required since font objects are used as keys in hashes
369
- # that cache certain values (See
370
- # Prawn::Table::Text#styled_with_of_single_character)
371
- #
372
- def hash #:nodoc:
373
- [ self.class, self.name, self.family, size ].hash
374
- end
375
-
376
- # Compliments the #hash implementation above
377
- #
378
- def eql?( other ) #:nodoc:
379
- self.class == other.class && self.name == other.name &&
380
- self.family == other.family && size == other.send(:size)
381
- end
382
-
383
351
  private
384
352
 
385
353
  # generate a font identifier that hasn't been used on the curretn page yet