hexapdf 0.21.1 → 0.24.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (253) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +137 -0
  3. data/LICENSE +1 -1
  4. data/Rakefile +1 -1
  5. data/examples/016-frame_automatic_box_placement.rb +7 -2
  6. data/examples/017-frame_text_flow.rb +10 -18
  7. data/examples/020-column_box.rb +20 -37
  8. data/examples/021-list_box.rb +26 -0
  9. data/lib/hexapdf/cli/batch.rb +1 -1
  10. data/lib/hexapdf/cli/command.rb +1 -1
  11. data/lib/hexapdf/cli/files.rb +1 -1
  12. data/lib/hexapdf/cli/fonts.rb +1 -1
  13. data/lib/hexapdf/cli/form.rb +31 -4
  14. data/lib/hexapdf/cli/image2pdf.rb +1 -1
  15. data/lib/hexapdf/cli/images.rb +1 -1
  16. data/lib/hexapdf/cli/info.rb +2 -2
  17. data/lib/hexapdf/cli/inspect.rb +19 -6
  18. data/lib/hexapdf/cli/merge.rb +1 -1
  19. data/lib/hexapdf/cli/modify.rb +24 -4
  20. data/lib/hexapdf/cli/optimize.rb +1 -1
  21. data/lib/hexapdf/cli/split.rb +1 -1
  22. data/lib/hexapdf/cli/watermark.rb +1 -1
  23. data/lib/hexapdf/cli.rb +1 -1
  24. data/lib/hexapdf/composer.rb +66 -125
  25. data/lib/hexapdf/configuration.rb +17 -1
  26. data/lib/hexapdf/content/canvas.rb +1 -1
  27. data/lib/hexapdf/content/color_space.rb +1 -1
  28. data/lib/hexapdf/content/graphic_object/arc.rb +1 -1
  29. data/lib/hexapdf/content/graphic_object/endpoint_arc.rb +1 -1
  30. data/lib/hexapdf/content/graphic_object/geom2d.rb +2 -1
  31. data/lib/hexapdf/content/graphic_object/solid_arc.rb +1 -1
  32. data/lib/hexapdf/content/graphic_object.rb +1 -1
  33. data/lib/hexapdf/content/graphics_state.rb +1 -1
  34. data/lib/hexapdf/content/operator.rb +1 -1
  35. data/lib/hexapdf/content/parser.rb +1 -1
  36. data/lib/hexapdf/content/processor.rb +1 -1
  37. data/lib/hexapdf/content/transformation_matrix.rb +1 -1
  38. data/lib/hexapdf/content.rb +1 -1
  39. data/lib/hexapdf/data_dir.rb +1 -1
  40. data/lib/hexapdf/dictionary.rb +1 -1
  41. data/lib/hexapdf/dictionary_fields.rb +2 -2
  42. data/lib/hexapdf/document/destinations.rb +396 -0
  43. data/lib/hexapdf/document/files.rb +1 -1
  44. data/lib/hexapdf/document/fonts.rb +1 -1
  45. data/lib/hexapdf/document/images.rb +1 -1
  46. data/lib/hexapdf/document/layout.rb +397 -0
  47. data/lib/hexapdf/document/pages.rb +17 -1
  48. data/lib/hexapdf/document/signatures.rb +5 -4
  49. data/lib/hexapdf/document.rb +46 -90
  50. data/lib/hexapdf/encryption/aes.rb +1 -1
  51. data/lib/hexapdf/encryption/arc4.rb +1 -1
  52. data/lib/hexapdf/encryption/fast_aes.rb +1 -1
  53. data/lib/hexapdf/encryption/fast_arc4.rb +30 -21
  54. data/lib/hexapdf/encryption/identity.rb +1 -1
  55. data/lib/hexapdf/encryption/ruby_aes.rb +1 -1
  56. data/lib/hexapdf/encryption/ruby_arc4.rb +1 -1
  57. data/lib/hexapdf/encryption/security_handler.rb +1 -1
  58. data/lib/hexapdf/encryption/standard_security_handler.rb +1 -1
  59. data/lib/hexapdf/encryption.rb +1 -1
  60. data/lib/hexapdf/error.rb +1 -1
  61. data/lib/hexapdf/filter/ascii85_decode.rb +1 -1
  62. data/lib/hexapdf/filter/ascii_hex_decode.rb +1 -1
  63. data/lib/hexapdf/filter/crypt.rb +1 -1
  64. data/lib/hexapdf/filter/encryption.rb +1 -1
  65. data/lib/hexapdf/filter/flate_decode.rb +1 -1
  66. data/lib/hexapdf/filter/lzw_decode.rb +1 -1
  67. data/lib/hexapdf/filter/pass_through.rb +1 -1
  68. data/lib/hexapdf/filter/predictor.rb +1 -1
  69. data/lib/hexapdf/filter/run_length_decode.rb +1 -1
  70. data/lib/hexapdf/filter.rb +1 -1
  71. data/lib/hexapdf/font/cmap/parser.rb +1 -1
  72. data/lib/hexapdf/font/cmap/writer.rb +1 -1
  73. data/lib/hexapdf/font/cmap.rb +1 -1
  74. data/lib/hexapdf/font/encoding/base.rb +1 -1
  75. data/lib/hexapdf/font/encoding/difference_encoding.rb +1 -1
  76. data/lib/hexapdf/font/encoding/glyph_list.rb +2 -2
  77. data/lib/hexapdf/font/encoding/mac_expert_encoding.rb +1 -1
  78. data/lib/hexapdf/font/encoding/mac_roman_encoding.rb +1 -1
  79. data/lib/hexapdf/font/encoding/standard_encoding.rb +1 -1
  80. data/lib/hexapdf/font/encoding/symbol_encoding.rb +1 -1
  81. data/lib/hexapdf/font/encoding/win_ansi_encoding.rb +1 -1
  82. data/lib/hexapdf/font/encoding/zapf_dingbats_encoding.rb +1 -1
  83. data/lib/hexapdf/font/encoding.rb +1 -1
  84. data/lib/hexapdf/font/invalid_glyph.rb +1 -1
  85. data/lib/hexapdf/font/true_type/builder.rb +1 -1
  86. data/lib/hexapdf/font/true_type/font.rb +1 -1
  87. data/lib/hexapdf/font/true_type/optimizer.rb +1 -1
  88. data/lib/hexapdf/font/true_type/subsetter.rb +1 -1
  89. data/lib/hexapdf/font/true_type/table/cmap.rb +1 -1
  90. data/lib/hexapdf/font/true_type/table/cmap_subtable.rb +1 -1
  91. data/lib/hexapdf/font/true_type/table/directory.rb +1 -1
  92. data/lib/hexapdf/font/true_type/table/glyf.rb +1 -1
  93. data/lib/hexapdf/font/true_type/table/head.rb +1 -1
  94. data/lib/hexapdf/font/true_type/table/hhea.rb +1 -1
  95. data/lib/hexapdf/font/true_type/table/hmtx.rb +1 -1
  96. data/lib/hexapdf/font/true_type/table/kern.rb +1 -1
  97. data/lib/hexapdf/font/true_type/table/loca.rb +1 -1
  98. data/lib/hexapdf/font/true_type/table/maxp.rb +1 -1
  99. data/lib/hexapdf/font/true_type/table/name.rb +1 -1
  100. data/lib/hexapdf/font/true_type/table/os2.rb +1 -1
  101. data/lib/hexapdf/font/true_type/table/post.rb +1 -1
  102. data/lib/hexapdf/font/true_type/table.rb +1 -1
  103. data/lib/hexapdf/font/true_type.rb +1 -1
  104. data/lib/hexapdf/font/true_type_wrapper.rb +1 -1
  105. data/lib/hexapdf/font/type1/afm_parser.rb +1 -1
  106. data/lib/hexapdf/font/type1/character_metrics.rb +1 -1
  107. data/lib/hexapdf/font/type1/font.rb +1 -1
  108. data/lib/hexapdf/font/type1/font_metrics.rb +1 -1
  109. data/lib/hexapdf/font/type1/pfb_parser.rb +1 -1
  110. data/lib/hexapdf/font/type1.rb +1 -1
  111. data/lib/hexapdf/font/type1_wrapper.rb +1 -1
  112. data/lib/hexapdf/font_loader/from_configuration.rb +1 -1
  113. data/lib/hexapdf/font_loader/from_file.rb +1 -1
  114. data/lib/hexapdf/font_loader/standard14.rb +1 -1
  115. data/lib/hexapdf/font_loader.rb +1 -1
  116. data/lib/hexapdf/image_loader/jpeg.rb +1 -1
  117. data/lib/hexapdf/image_loader/pdf.rb +1 -1
  118. data/lib/hexapdf/image_loader/png.rb +1 -1
  119. data/lib/hexapdf/image_loader.rb +1 -1
  120. data/lib/hexapdf/importer.rb +1 -1
  121. data/lib/hexapdf/layout/box.rb +121 -22
  122. data/lib/hexapdf/layout/box_fitter.rb +136 -0
  123. data/lib/hexapdf/layout/column_box.rb +168 -89
  124. data/lib/hexapdf/layout/frame.rb +155 -140
  125. data/lib/hexapdf/layout/image_box.rb +19 -4
  126. data/lib/hexapdf/layout/inline_box.rb +1 -1
  127. data/lib/hexapdf/layout/line.rb +1 -1
  128. data/lib/hexapdf/layout/list_box.rb +355 -0
  129. data/lib/hexapdf/layout/numeric_refinements.rb +1 -1
  130. data/lib/hexapdf/layout/style.rb +285 -8
  131. data/lib/hexapdf/layout/text_box.rb +30 -11
  132. data/lib/hexapdf/layout/text_fragment.rb +3 -2
  133. data/lib/hexapdf/layout/text_layouter.rb +23 -3
  134. data/lib/hexapdf/layout/text_shaper.rb +1 -1
  135. data/lib/hexapdf/layout/width_from_polygon.rb +12 -7
  136. data/lib/hexapdf/layout.rb +4 -1
  137. data/lib/hexapdf/name_tree_node.rb +1 -1
  138. data/lib/hexapdf/number_tree_node.rb +1 -1
  139. data/lib/hexapdf/object.rb +1 -1
  140. data/lib/hexapdf/parser.rb +1 -8
  141. data/lib/hexapdf/pdf_array.rb +1 -1
  142. data/lib/hexapdf/rectangle.rb +1 -1
  143. data/lib/hexapdf/reference.rb +1 -1
  144. data/lib/hexapdf/revision.rb +9 -2
  145. data/lib/hexapdf/revisions.rb +152 -51
  146. data/lib/hexapdf/serializer.rb +1 -1
  147. data/lib/hexapdf/stream.rb +1 -1
  148. data/lib/hexapdf/task/dereference.rb +1 -1
  149. data/lib/hexapdf/task/optimize.rb +22 -12
  150. data/lib/hexapdf/task.rb +1 -1
  151. data/lib/hexapdf/tokenizer.rb +1 -1
  152. data/lib/hexapdf/type/acro_form/appearance_generator.rb +1 -1
  153. data/lib/hexapdf/type/acro_form/button_field.rb +1 -1
  154. data/lib/hexapdf/type/acro_form/choice_field.rb +1 -1
  155. data/lib/hexapdf/type/acro_form/field.rb +1 -1
  156. data/lib/hexapdf/type/acro_form/form.rb +12 -6
  157. data/lib/hexapdf/type/acro_form/signature_field.rb +1 -1
  158. data/lib/hexapdf/type/acro_form/text_field.rb +9 -1
  159. data/lib/hexapdf/type/acro_form/variable_text_field.rb +1 -1
  160. data/lib/hexapdf/type/acro_form.rb +1 -1
  161. data/lib/hexapdf/type/action.rb +1 -1
  162. data/lib/hexapdf/type/actions/go_to.rb +1 -1
  163. data/lib/hexapdf/type/actions/go_to_r.rb +1 -1
  164. data/lib/hexapdf/type/actions/launch.rb +1 -1
  165. data/lib/hexapdf/type/actions/uri.rb +1 -1
  166. data/lib/hexapdf/type/actions.rb +1 -1
  167. data/lib/hexapdf/type/annotation.rb +1 -1
  168. data/lib/hexapdf/type/annotations/link.rb +1 -1
  169. data/lib/hexapdf/type/annotations/markup_annotation.rb +1 -1
  170. data/lib/hexapdf/type/annotations/text.rb +1 -1
  171. data/lib/hexapdf/type/annotations/widget.rb +1 -1
  172. data/lib/hexapdf/type/annotations.rb +1 -1
  173. data/lib/hexapdf/type/catalog.rb +10 -2
  174. data/lib/hexapdf/type/cid_font.rb +1 -1
  175. data/lib/hexapdf/type/embedded_file.rb +1 -1
  176. data/lib/hexapdf/type/file_specification.rb +1 -1
  177. data/lib/hexapdf/type/font.rb +1 -1
  178. data/lib/hexapdf/type/font_descriptor.rb +1 -1
  179. data/lib/hexapdf/type/font_simple.rb +1 -1
  180. data/lib/hexapdf/type/font_true_type.rb +1 -1
  181. data/lib/hexapdf/type/font_type0.rb +1 -1
  182. data/lib/hexapdf/type/font_type1.rb +1 -1
  183. data/lib/hexapdf/type/font_type3.rb +1 -1
  184. data/lib/hexapdf/type/form.rb +1 -1
  185. data/lib/hexapdf/type/graphics_state_parameter.rb +1 -1
  186. data/lib/hexapdf/type/icon_fit.rb +1 -1
  187. data/lib/hexapdf/type/image.rb +48 -4
  188. data/lib/hexapdf/type/info.rb +1 -1
  189. data/lib/hexapdf/type/names.rb +14 -1
  190. data/lib/hexapdf/type/object_stream.rb +1 -1
  191. data/lib/hexapdf/type/page.rb +1 -1
  192. data/lib/hexapdf/type/page_tree_node.rb +19 -2
  193. data/lib/hexapdf/type/resources.rb +1 -1
  194. data/lib/hexapdf/type/signature/adbe_pkcs7_detached.rb +1 -1
  195. data/lib/hexapdf/type/signature/adbe_x509_rsa_sha1.rb +1 -1
  196. data/lib/hexapdf/type/signature/handler.rb +1 -1
  197. data/lib/hexapdf/type/signature/verification_result.rb +1 -1
  198. data/lib/hexapdf/type/signature.rb +1 -1
  199. data/lib/hexapdf/type/trailer.rb +2 -2
  200. data/lib/hexapdf/type/viewer_preferences.rb +1 -1
  201. data/lib/hexapdf/type/xref_stream.rb +3 -2
  202. data/lib/hexapdf/type.rb +1 -1
  203. data/lib/hexapdf/utils/bit_field.rb +1 -1
  204. data/lib/hexapdf/utils/bit_stream.rb +1 -1
  205. data/lib/hexapdf/utils/graphics_helpers.rb +1 -1
  206. data/lib/hexapdf/utils/lru_cache.rb +1 -1
  207. data/lib/hexapdf/utils/math_helpers.rb +1 -1
  208. data/lib/hexapdf/utils/object_hash.rb +1 -1
  209. data/lib/hexapdf/utils/pdf_doc_encoding.rb +1 -1
  210. data/lib/hexapdf/utils/sorted_tree_node.rb +4 -2
  211. data/lib/hexapdf/version.rb +2 -2
  212. data/lib/hexapdf/writer.rb +23 -8
  213. data/lib/hexapdf/xref_section.rb +1 -1
  214. data/lib/hexapdf.rb +1 -1
  215. data/test/hexapdf/content/graphic_object/test_geom2d.rb +1 -1
  216. data/test/hexapdf/document/test_destinations.rb +338 -0
  217. data/test/hexapdf/document/test_images.rb +1 -1
  218. data/test/hexapdf/document/test_layout.rb +264 -0
  219. data/test/hexapdf/document/test_pages.rb +9 -0
  220. data/test/hexapdf/document/test_signatures.rb +10 -3
  221. data/test/hexapdf/encryption/test_security_handler.rb +3 -3
  222. data/test/hexapdf/font/encoding/test_glyph_list.rb +4 -0
  223. data/test/hexapdf/layout/test_box.rb +53 -3
  224. data/test/hexapdf/layout/test_box_fitter.rb +62 -0
  225. data/test/hexapdf/layout/test_column_box.rb +159 -0
  226. data/test/hexapdf/layout/test_frame.rb +114 -39
  227. data/test/hexapdf/layout/test_image_box.rb +1 -1
  228. data/test/hexapdf/layout/test_list_box.rb +249 -0
  229. data/test/hexapdf/layout/test_text_box.rb +33 -2
  230. data/test/hexapdf/layout/test_text_fragment.rb +1 -1
  231. data/test/hexapdf/layout/test_text_layouter.rb +49 -17
  232. data/test/hexapdf/layout/test_width_from_polygon.rb +13 -0
  233. data/test/hexapdf/task/test_optimize.rb +17 -4
  234. data/test/hexapdf/test_composer.rb +35 -1
  235. data/test/hexapdf/test_dictionary_fields.rb +10 -10
  236. data/test/hexapdf/test_document.rb +33 -136
  237. data/test/hexapdf/test_filter.rb +1 -1
  238. data/test/hexapdf/test_parser.rb +1 -3
  239. data/test/hexapdf/test_revision.rb +14 -0
  240. data/test/hexapdf/test_revisions.rb +137 -29
  241. data/test/hexapdf/test_serializer.rb +1 -5
  242. data/test/hexapdf/test_writer.rb +99 -15
  243. data/test/hexapdf/type/acro_form/test_form.rb +2 -1
  244. data/test/hexapdf/type/acro_form/test_text_field.rb +17 -0
  245. data/test/hexapdf/type/test_catalog.rb +8 -0
  246. data/test/hexapdf/type/test_image.rb +45 -9
  247. data/test/hexapdf/type/test_names.rb +20 -0
  248. data/test/hexapdf/type/test_page_tree_node.rb +21 -1
  249. data/test/hexapdf/type/test_trailer.rb +3 -3
  250. data/test/hexapdf/type/test_xref_stream.rb +2 -1
  251. data/test/hexapdf/utils/test_sorted_tree_node.rb +11 -1
  252. data/test/test_helper.rb +5 -1
  253. metadata +29 -3
@@ -0,0 +1,338 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'test_helper'
4
+ require 'hexapdf/document'
5
+
6
+ describe HexaPDF::Document::Destinations::Destination do
7
+ def destination(dest)
8
+ HexaPDF::Document::Destinations::Destination.new(dest)
9
+ end
10
+
11
+ it "can be asked whether the referenced page is in a remote document" do
12
+ assert(destination([5, :Fit]).remote?)
13
+ refute(destination([HexaPDF::Dictionary.new({}), :Fit]).remote?)
14
+ end
15
+
16
+ it "returns the page object" do
17
+ assert_equal(:page, destination([:page, :Fit]).page)
18
+ end
19
+
20
+ describe "type :xyz" do
21
+ before do
22
+ @dest = destination([:page, :XYZ, :left, :top, :zoom])
23
+ end
24
+
25
+ it "returns the type of the destination" do
26
+ assert_equal(:xyz, @dest.type)
27
+ end
28
+
29
+ it "returns the argument left" do
30
+ assert_equal(:left, @dest.left)
31
+ end
32
+
33
+ it "returns the argument top" do
34
+ assert_equal(:top, @dest.top)
35
+ end
36
+
37
+ it "returns the argument zoom" do
38
+ assert_equal(:zoom, @dest.zoom)
39
+ end
40
+
41
+ it "raises an error if the bottom and right properties are accessed" do
42
+ assert_raises(HexaPDF::Error) { @dest.bottom }
43
+ assert_raises(HexaPDF::Error) { @dest.right }
44
+ end
45
+ end
46
+
47
+ describe "type :fit_page" do
48
+ before do
49
+ @dest = destination([:page, :Fit])
50
+ end
51
+
52
+ it "returns the type of the destination" do
53
+ assert_equal(:fit_page, @dest.type)
54
+ end
55
+
56
+ it "raises an error if the top, left, bottom, right, zoom properties are accessed" do
57
+ assert_raises(HexaPDF::Error) { @dest.top }
58
+ assert_raises(HexaPDF::Error) { @dest.left }
59
+ assert_raises(HexaPDF::Error) { @dest.bottom }
60
+ assert_raises(HexaPDF::Error) { @dest.right }
61
+ assert_raises(HexaPDF::Error) { @dest.zoom }
62
+ end
63
+ end
64
+
65
+ describe "type :fit_page_horizontal" do
66
+ before do
67
+ @dest = destination([:page, :FitH, :top])
68
+ end
69
+
70
+ it "returns the type of the destination" do
71
+ assert_equal(:fit_page_horizontal, @dest.type)
72
+ end
73
+
74
+ it "returns the argument top" do
75
+ assert_equal(:top, @dest.top)
76
+ end
77
+
78
+ it "raises an error if the left, bottom, right, zoom properties are accessed" do
79
+ assert_raises(HexaPDF::Error) { @dest.left }
80
+ assert_raises(HexaPDF::Error) { @dest.bottom }
81
+ assert_raises(HexaPDF::Error) { @dest.right }
82
+ assert_raises(HexaPDF::Error) { @dest.zoom }
83
+ end
84
+ end
85
+
86
+ describe "type :fit_page_vertical" do
87
+ before do
88
+ @dest = destination([:page, :FitV, :left])
89
+ end
90
+
91
+ it "returns the type of the destination" do
92
+ assert_equal(:fit_page_vertical, @dest.type)
93
+ end
94
+
95
+ it "returns the argument left" do
96
+ assert_equal(:left, @dest.left)
97
+ end
98
+
99
+ it "raises an error if the top, bottom, right, zoom properties are accessed" do
100
+ assert_raises(HexaPDF::Error) { @dest.top }
101
+ assert_raises(HexaPDF::Error) { @dest.bottom }
102
+ assert_raises(HexaPDF::Error) { @dest.right }
103
+ assert_raises(HexaPDF::Error) { @dest.zoom }
104
+ end
105
+ end
106
+
107
+ describe "type :fit_rectangle" do
108
+ before do
109
+ @dest = destination([:page, :FitR, :left, :bottom, :right, :top])
110
+ end
111
+
112
+ it "returns the type of the destination" do
113
+ assert_equal(:fit_rectangle, @dest.type)
114
+ end
115
+
116
+ it "returns the argument left" do
117
+ assert_equal(:left, @dest.left)
118
+ end
119
+
120
+ it "returns the argument top" do
121
+ assert_equal(:top, @dest.top)
122
+ end
123
+
124
+ it "returns the argument right" do
125
+ assert_equal(:right, @dest.right)
126
+ end
127
+
128
+ it "returns the argument bottom" do
129
+ assert_equal(:bottom, @dest.bottom)
130
+ end
131
+
132
+ it "raises an error if the zoom property is accessed" do
133
+ assert_raises(HexaPDF::Error) { @dest.zoom }
134
+ end
135
+ end
136
+
137
+ describe "type :fit_bounding_box" do
138
+ before do
139
+ @dest = destination([:page, :FitB])
140
+ end
141
+
142
+ it "returns the type of the destination" do
143
+ assert_equal(:fit_bounding_box, @dest.type)
144
+ end
145
+
146
+ it "raises an error if the bottom and right properties are accessed" do
147
+ assert_raises(HexaPDF::Error) { @dest.left }
148
+ assert_raises(HexaPDF::Error) { @dest.bottom }
149
+ assert_raises(HexaPDF::Error) { @dest.right }
150
+ assert_raises(HexaPDF::Error) { @dest.top }
151
+ assert_raises(HexaPDF::Error) { @dest.zoom }
152
+ end
153
+ end
154
+
155
+ describe "type :fit_bounding_box_horizontal" do
156
+ before do
157
+ @dest = destination([:page, :FitBH, :top])
158
+ end
159
+
160
+ it "returns the type of the destination" do
161
+ assert_equal(:fit_bounding_box_horizontal, @dest.type)
162
+ end
163
+
164
+ it "returns the argument top" do
165
+ assert_equal(:top, @dest.top)
166
+ end
167
+
168
+ it "raises an error if the left, bottom, right, zoom properties are accessed" do
169
+ assert_raises(HexaPDF::Error) { @dest.left }
170
+ assert_raises(HexaPDF::Error) { @dest.bottom }
171
+ assert_raises(HexaPDF::Error) { @dest.right }
172
+ assert_raises(HexaPDF::Error) { @dest.zoom }
173
+ end
174
+ end
175
+
176
+ describe "type :fit_bounding_box_vertical::" do
177
+ before do
178
+ @dest = destination([:page, :FitBV, :left])
179
+ end
180
+
181
+ it "returns the type of the destination" do
182
+ assert_equal(:fit_bounding_box_vertical, @dest.type)
183
+ end
184
+
185
+ it "returns the argument left" do
186
+ assert_equal(:left, @dest.left)
187
+ end
188
+
189
+ it "raises an error if the left, bottom, right, zoom properties are accessed" do
190
+ assert_raises(HexaPDF::Error) { @dest.top }
191
+ assert_raises(HexaPDF::Error) { @dest.bottom }
192
+ assert_raises(HexaPDF::Error) { @dest.right }
193
+ assert_raises(HexaPDF::Error) { @dest.zoom }
194
+ end
195
+ end
196
+ end
197
+
198
+ describe HexaPDF::Document::Destinations do
199
+ before do
200
+ @doc = HexaPDF::Document.new
201
+ @page = @doc.pages.add
202
+ end
203
+
204
+ describe "create_xyz" do
205
+ it "creates the destination" do
206
+ dest = @doc.destinations.create_xyz(@page, left: 1, top: 2, zoom: 3)
207
+ assert_equal([@page, :XYZ, 1, 2, 3], dest)
208
+ end
209
+
210
+ it "creates the destination and registers it under the given name" do
211
+ dest = @doc.destinations.create_xyz(@page, name: 'xyz')
212
+ assert_equal([@page, :XYZ, nil, nil, nil], @doc.destinations[dest])
213
+ end
214
+ end
215
+
216
+ describe "create_fit_page" do
217
+ it "creates the destination" do
218
+ dest = @doc.destinations.create_fit_page(@page)
219
+ assert_equal([@page, :Fit], dest)
220
+ end
221
+
222
+ it "creates the destination and registers it under the given name" do
223
+ dest = @doc.destinations.create_fit_page(@page, name: 'xyz')
224
+ assert_equal([@page, :Fit], @doc.destinations[dest])
225
+ end
226
+ end
227
+
228
+ describe "create_fit_page_horizontal" do
229
+ it "creates the destination" do
230
+ dest = @doc.destinations.create_fit_page_horizontal(@page, top: 2)
231
+ assert_equal([@page, :FitH, 2], dest)
232
+ end
233
+
234
+ it "creates the destination and registers it under the given name" do
235
+ dest = @doc.destinations.create_fit_page_horizontal(@page, name: 'xyz')
236
+ assert_equal([@page, :FitH, nil], @doc.destinations[dest])
237
+ end
238
+ end
239
+
240
+ describe "create_fit_page_vertical" do
241
+ it "creates the destination" do
242
+ dest = @doc.destinations.create_fit_page_vertical(@page, left: 2)
243
+ assert_equal([@page, :FitV, 2], dest)
244
+ end
245
+
246
+ it "creates the destination and registers it under the given name" do
247
+ dest = @doc.destinations.create_fit_page_vertical(@page, name: 'xyz')
248
+ assert_equal([@page, :FitV, nil], @doc.destinations[dest])
249
+ end
250
+ end
251
+
252
+ describe "create_fit_rectangle" do
253
+ it "creates the destination" do
254
+ dest = @doc.destinations.create_fit_rectangle(@page, left: 1, bottom: 2, right: 3, top: 4)
255
+ assert_equal([@page, :FitR, 1, 2, 3, 4], dest)
256
+ end
257
+
258
+ it "creates the destination and registers it under the given name" do
259
+ dest = @doc.destinations.create_fit_rectangle(@page, name: 'xyz', left: 1, bottom: 2, right: 3, top: 4)
260
+ assert_equal([@page, :FitR, 1, 2, 3, 4], @doc.destinations[dest])
261
+ end
262
+ end
263
+
264
+ describe "create_fit_bounding_box" do
265
+ it "creates the destination" do
266
+ dest = @doc.destinations.create_fit_bounding_box(@page)
267
+ assert_equal([@page, :FitB], dest)
268
+ end
269
+
270
+ it "creates the destination and registers it under the given name" do
271
+ dest = @doc.destinations.create_fit_bounding_box(@page, name: 'xyz')
272
+ assert_equal([@page, :FitB], @doc.destinations[dest])
273
+ end
274
+ end
275
+
276
+ describe "create_fit_bounding_box_horizontal" do
277
+ it "creates the destination" do
278
+ dest = @doc.destinations.create_fit_bounding_box_horizontal(@page, top: 2)
279
+ assert_equal([@page, :FitBH, 2], dest)
280
+ end
281
+
282
+ it "creates the destination and registers it under the given name" do
283
+ dest = @doc.destinations.create_fit_bounding_box_horizontal(@page, name: 'xyz')
284
+ assert_equal([@page, :FitBH, nil], @doc.destinations[dest])
285
+ end
286
+ end
287
+
288
+ describe "create_fit_bounding_box_vertical" do
289
+ it "creates the destination" do
290
+ dest = @doc.destinations.create_fit_bounding_box_vertical(@page, left: 2)
291
+ assert_equal([@page, :FitBV, 2], dest)
292
+ end
293
+
294
+ it "creates the destination and registers it under the given name" do
295
+ dest = @doc.destinations.create_fit_bounding_box_vertical(@page, name: 'xyz')
296
+ assert_equal([@page, :FitBV, nil], @doc.destinations[dest])
297
+ end
298
+ end
299
+
300
+ it "adds a destination array to the destinations name tree and allows to retrieve it" do
301
+ @doc.destinations.add('abc', [:page, :Fit])
302
+ assert_equal([:page, :Fit], @doc.destinations['abc'])
303
+ end
304
+
305
+ it "deletes a named destination" do
306
+ @doc.destinations.add('abc', [:page, :Fit])
307
+ assert(@doc.destinations['abc'])
308
+ @doc.destinations.delete('abc')
309
+ refute(@doc.destinations['abc'])
310
+ end
311
+
312
+ describe "each" do
313
+ before do
314
+ 3.times {|i| @doc.destinations.add("abc#{i}", [:page, :Fit]) }
315
+ end
316
+
317
+ it "returns an enumerator if no block is given" do
318
+ enum = @doc.destinations.each
319
+ assert_equal('abc0', enum.next.first)
320
+ assert_equal('abc1', enum.next.first)
321
+ assert_equal('abc2', enum.next.first)
322
+ assert_raises(StopIteration) { enum.next }
323
+ end
324
+
325
+ it "iterates over all name-destination pairs in order" do
326
+ result = [
327
+ ['abc0', :fit_page],
328
+ ['abc1', :fit_page],
329
+ ['abc2', :fit_page],
330
+ ]
331
+ @doc.destinations.each do |name, dest|
332
+ exp_name, exp_type = result.shift
333
+ assert_equal(exp_name, name)
334
+ assert_equal(exp_type, dest.type)
335
+ end
336
+ end
337
+ end
338
+ end
@@ -31,7 +31,7 @@ describe HexaPDF::Document::Images do
31
31
  it "adds an image using an IO" do
32
32
  File.open(__FILE__, 'rb') do |file|
33
33
  image = @doc.images.add(file)
34
- assert_equal(File.read(__FILE__), image.stream)
34
+ assert_equal(File.binread(__FILE__), image.stream)
35
35
  assert_equal(File.absolute_path(__FILE__), image.source_path)
36
36
  end
37
37
  end
@@ -0,0 +1,264 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'test_helper'
4
+ require 'hexapdf/document'
5
+
6
+ describe HexaPDF::Document::Layout::ChildrenCollector do
7
+ before do
8
+ @doc = HexaPDF::Document.new
9
+ @collector = HexaPDF::Document::Layout::ChildrenCollector.new(@doc.layout)
10
+ end
11
+
12
+ it "provides a convenient ::collect method which just returns the collected children" do
13
+ children = HexaPDF::Document::Layout::ChildrenCollector.collect(@doc.layout) do |collector|
14
+ collector.lorem_ipsum_box
15
+ collector.lorem_ipsum_box
16
+ end
17
+ assert_equal(2, children.size)
18
+ assert_kind_of(HexaPDF::Layout::TextBox, children[0])
19
+ assert_kind_of(HexaPDF::Layout::TextBox, children[1])
20
+ end
21
+
22
+ it "allows appending existing boxes" do
23
+ box = @doc.layout.lorem_ipsum_box
24
+ @collector << box
25
+ assert_equal([box], @collector.children)
26
+ end
27
+
28
+ it "allows appending an array of boxes created through another children collector" do
29
+ @collector.multiple do |collector|
30
+ collector.lorem_ipsum_box
31
+ collector.lorem_ipsum_box
32
+ end
33
+ assert_equal(1, @collector.children.size)
34
+ assert_equal(2, @collector.children[0].size)
35
+ end
36
+
37
+ it "allows appending boxes through method names of the Layout class" do
38
+ @collector.lorem_ipsum_box
39
+ assert_equal(1, @collector.children.size)
40
+ assert_kind_of(HexaPDF::Layout::TextBox, @collector.children[0])
41
+ end
42
+
43
+ it "allows appending boxes through method names without the _box suffix of the Layout class" do
44
+ @collector.lorem_ipsum
45
+ assert_equal(1, @collector.children.size)
46
+ assert_kind_of(HexaPDF::Layout::TextBox, @collector.children[0])
47
+ end
48
+
49
+ it "allows appending boxes through their registered names" do
50
+ @collector.column
51
+ assert_equal(1, @collector.children.size)
52
+ assert_kind_of(HexaPDF::Layout::ColumnBox, @collector.children[0])
53
+ end
54
+
55
+ it "can be asked which methods it supports" do
56
+ assert(@collector.respond_to?(:text_box))
57
+ assert(@collector.respond_to?(:text))
58
+ assert(@collector.respond_to?(:column))
59
+ refute(@collector.respond_to?(:unknown))
60
+ end
61
+
62
+ it "raises an error on an unknown method name" do
63
+ assert_raises(NameError) { @collector.unknown_box }
64
+ end
65
+ end
66
+
67
+ describe HexaPDF::Document::Layout do
68
+ before do
69
+ @doc = HexaPDF::Document.new
70
+ @layout = @doc.layout
71
+ end
72
+
73
+ describe "style" do
74
+ it "creates a new style if it does not exist based on the base argument" do
75
+ @layout.style(:base, font_size: 20)
76
+ assert_equal(20, @layout.style(:newstyle, subscript: true).font_size)
77
+ refute(@layout.style(:base).subscript)
78
+ assert_equal(10, @layout.style(:another_new, base: nil).font_size)
79
+ assert(@layout.style(:yet_another_new, base: :newstyle).subscript)
80
+ end
81
+
82
+ it "returns the named style" do
83
+ assert_kind_of(HexaPDF::Layout::Style, @layout.style(:base))
84
+ end
85
+
86
+ it "updates the style with the given properties" do
87
+ assert_equal(20, @layout.style(:base, font_size: 20).font_size)
88
+ end
89
+ end
90
+
91
+ describe "box" do
92
+ it "creates the request box" do
93
+ box = @layout.box(:column, columns: 3, gaps: 20, width: 15, height: 30, style: {font_size: 10})
94
+ assert_equal(15, box.width)
95
+ assert_equal(30, box.height)
96
+ assert_equal([-1, -1, -1], box.columns)
97
+ assert_equal([20], box.gaps)
98
+ assert_equal(10, box.style.font_size)
99
+ end
100
+
101
+ it "allows specifying the box's children via a provided block" do
102
+ box = @layout.box(:column) do |column|
103
+ column.lorem_ipsum
104
+ column.lorem_ipsum
105
+ end
106
+ assert_equal(2, box.children.size)
107
+ end
108
+
109
+ it "fails if the name is not registered" do
110
+ assert_raises(HexaPDF::Error) { @layout.box(:unknown) }
111
+ end
112
+ end
113
+
114
+ describe "text_box" do
115
+ it "creates a text box" do
116
+ box = @layout.text_box("Test", width: 10, height: 15)
117
+ assert_equal(10, box.width)
118
+ assert_equal(15, box.height)
119
+ assert_same(@doc.fonts.add("Times"), box.style.font)
120
+ items = box.instance_variable_get(:@items)
121
+ assert_equal(1, items.length)
122
+ assert_same(box.style, items.first.style)
123
+ end
124
+
125
+ it "allows setting of a custom style" do
126
+ style = HexaPDF::Layout::Style.new(font_size: 20, font: ['Times', {variant: :bold}])
127
+ box = @layout.text_box("Test", style: style)
128
+ assert_same(box.style, style)
129
+ assert_same(@doc.fonts.add("Times", variant: :bold), box.style.font)
130
+ assert_equal(20, box.style.font_size)
131
+
132
+ box = @layout.text_box("Test", style: {font_size: 20})
133
+ assert_equal(20, box.style.font_size)
134
+
135
+ @layout.style(:named, font_size: 20)
136
+ box = @layout.text_box("Test", style: :named)
137
+ assert_equal(20, box.style.font_size)
138
+ end
139
+
140
+ it "updates the used style with the provided options" do
141
+ box = @layout.text_box("Test", style: {subscript: true}, font_size: 20)
142
+ assert_equal(20, box.style.font_size)
143
+ end
144
+
145
+ it "allows using a box style different from the text style" do
146
+ style = HexaPDF::Layout::Style.new(font_size: 20)
147
+ box = @layout.text_box("Test", box_style: style)
148
+ refute_same(box.instance_variable_get(:@items).first.style, style)
149
+ assert_same(box.style, style)
150
+
151
+ @layout.style(:named, font_size: 20)
152
+ box = @layout.text_box("Test", box_style: :named)
153
+ assert_equal(20, box.style.font_size)
154
+ end
155
+ end
156
+
157
+ describe "formatted_text" do
158
+ it "creates a text box with the given text" do
159
+ box = @layout.formatted_text_box(["Test"], width: 10, height: 15)
160
+ assert_equal(10, box.width)
161
+ assert_equal(15, box.height)
162
+ assert_equal(1, box.instance_variable_get(:@items).length)
163
+ end
164
+
165
+ it "allows using a hash with :text key instead of a simple string" do
166
+ box = @layout.formatted_text_box([{text: "Test"}])
167
+ items = box.instance_variable_get(:@items)
168
+ assert_equal(4, items[0].items.length)
169
+ end
170
+
171
+ it "uses an empty string if the :text key for a hash is not specified" do
172
+ box = @layout.formatted_text_box([{font_size: "Test"}])
173
+ items = box.instance_variable_get(:@items)
174
+ assert_equal(0, items[0].items.length)
175
+ end
176
+
177
+ it "allows setting a custom base style for all parts" do
178
+ box = @layout.formatted_text_box(["Test", "other"], font_size: 20)
179
+ items = box.instance_variable_get(:@items)
180
+ assert_equal(20, box.style.font_size)
181
+ assert_equal(20, items[0].style.font_size)
182
+ assert_equal(20, items[1].style.font_size)
183
+ end
184
+
185
+ it "allows using custom style properties for a single part" do
186
+ box = @layout.formatted_text_box([{text: "Test", font_size: 20}, "test"], align: :center)
187
+ items = box.instance_variable_get(:@items)
188
+ assert_equal(10, box.style.font_size)
189
+
190
+ assert_equal(20, items[0].style.font_size)
191
+ assert_equal(:center, items[0].style.align)
192
+
193
+ assert_equal(10, items[1].style.font_size)
194
+ assert_equal(:center, items[1].style.align)
195
+ end
196
+
197
+ it "allows using a custom style as basis for a single part" do
198
+ box = @layout.formatted_text_box([{text: "Test", style: {font_size: 20}, subscript: true},
199
+ "test"], align: :center)
200
+ items = box.instance_variable_get(:@items)
201
+ assert_equal(10, box.style.font_size)
202
+
203
+ assert_equal(20, items[0].style.font_size)
204
+ assert_equal(:left, items[0].style.align)
205
+ assert(items[0].style.subscript)
206
+
207
+ assert_equal(10, items[1].style.font_size)
208
+ assert_equal(:center, items[1].style.align)
209
+ refute(items[1].style.subscript)
210
+ end
211
+
212
+ it "allows specifying a link to an URL via the :link key" do
213
+ box = @layout.formatted_text_box([{text: "Test", link: "URI"}, {link: "URI"}, "test"])
214
+ items = box.instance_variable_get(:@items)
215
+ assert_equal(3, items.length)
216
+ assert_equal(4, items[0].items.length, "text should be Test")
217
+ assert_equal(3, items[1].items.length, "text should be URI")
218
+ assert_equal([:link, {uri: 'URI'}], items[0].style.overlays.instance_variable_get(:@layers)[0])
219
+ refute(items[2].style.overlays?)
220
+ end
221
+ end
222
+
223
+ describe "image_box" do
224
+ it "creates an image box" do
225
+ image_path = File.join(TEST_DATA_DIR, 'images', 'gray.jpg')
226
+
227
+ box = @layout.image_box(image_path, width: 10, height: 15, style: {font_size: 20}, subscript: true)
228
+ assert_equal(10, box.width)
229
+ assert_equal(15, box.height)
230
+ assert_equal(20, box.style.font_size)
231
+ assert(box.style.subscript)
232
+ assert_same(@doc.images.add(image_path), box.image)
233
+ end
234
+
235
+ it "allows using a form XObject" do
236
+ form = @doc.add({Type: :XObject, Subtype: :Form, BBox: [0, 0, 10, 10]})
237
+ box = @layout.image_box(form, width: 10)
238
+ assert_equal(10, box.width)
239
+ assert_same(form, box.image)
240
+ end
241
+ end
242
+
243
+ describe "lorem_ipsum_box" do
244
+ it "creates a standard lorem ipsum box" do
245
+ box = @layout.lorem_ipsum_box(width: 10, height: 15, font_size: 15)
246
+ assert_equal(10, box.width)
247
+ assert_equal(15, box.height)
248
+ items = box.instance_variable_get(:@items)
249
+ assert_equal(HexaPDF::Document::Layout::LOREM_IPSUM.join(" ").size, items[0].items.length)
250
+ end
251
+
252
+ it "can use just some sentences from the lorem ipsum text" do
253
+ box = @layout.lorem_ipsum_box(sentences: 1)
254
+ items = box.instance_variable_get(:@items)
255
+ assert_equal(HexaPDF::Document::Layout::LOREM_IPSUM[0].size, items[0].items.length)
256
+ end
257
+
258
+ it "can use multiple of the selected sentences" do
259
+ box = @layout.lorem_ipsum_box(sentences: 2, count: 2)
260
+ items = box.instance_variable_get(:@items)
261
+ assert_equal(HexaPDF::Document::Layout::LOREM_IPSUM[0, 2].join(" ").size * 2 + 2, items[0].items.length)
262
+ end
263
+ end
264
+ end
@@ -72,6 +72,15 @@ describe HexaPDF::Document::Pages do
72
72
  end
73
73
  end
74
74
 
75
+ describe "move" do
76
+ it "moves the page to the given index" do
77
+ first = @doc.pages.add
78
+ second = @doc.pages.add
79
+ @doc.pages.move(first, -1)
80
+ assert_equal([second, first], @doc.pages.each.to_a)
81
+ end
82
+ end
83
+
75
84
  describe "delete" do
76
85
  it "deletes a given page" do
77
86
  page1 = @doc.pages.add
@@ -138,7 +138,7 @@ describe HexaPDF::Document::Signatures do
138
138
  describe "add" do
139
139
  before do
140
140
  @doc = HexaPDF::Document.new(io: StringIO.new(MINIMAL_PDF))
141
- @io = StringIO.new
141
+ @io = StringIO.new(''.b)
142
142
  end
143
143
 
144
144
  it "uses the provided signature dictionary" do
@@ -164,7 +164,7 @@ describe HexaPDF::Document::Signatures do
164
164
  sig = @doc.signatures.first
165
165
  assert_equal(:'Adobe.PPKLite', sig[:Filter])
166
166
  assert_equal(:'adbe.pkcs7.detached', sig[:SubFilter])
167
- assert_equal([0, 968, 3590, 2425], sig[:ByteRange].value)
167
+ assert_equal([0, 968, 3590, 2517], sig[:ByteRange].value)
168
168
  assert_equal(:sig, sig[:key])
169
169
  assert_equal(:sig_field, @doc.acro_form.each_field.first[:key])
170
170
  assert(sig.key?(:Contents))
@@ -205,7 +205,14 @@ describe HexaPDF::Document::Signatures do
205
205
  it "handles different xref section types correctly when determing the offsets" do
206
206
  @doc.delete(7)
207
207
  sig = @doc.signatures.add(@io, @handler, write_options: {update_fields: false})
208
- assert_equal([0, 968, 3590, 2412], sig[:ByteRange].value)
208
+ assert_equal([0, 968, 3590, 2491], sig[:ByteRange].value)
209
+ end
210
+
211
+ it "works if the signature object is the last object of the xref section" do
212
+ field = @doc.acro_form(create: true).create_signature_field('Signature2')
213
+ field.create_widget(@doc.pages[0], Rect: [0, 0, 0, 0])
214
+ sig = @doc.signatures.add(@io, @handler, signature: field, write_options: {update_fields: false})
215
+ assert_equal([0, 3063, 5685, 400], sig[:ByteRange].value)
209
216
  end
210
217
 
211
218
  it "allows writing to a file in addition to writing to an IO" do
@@ -302,9 +302,9 @@ describe HexaPDF::Encryption::SecurityHandler do
302
302
  @handler = TestHandler.new(@document)
303
303
 
304
304
  assert_equal("Something",
305
- @handler.decrypt(@document.revisions[0].trailer[:Encrypt])[:Key])
305
+ @handler.decrypt(@document.revisions.all[0].trailer[:Encrypt])[:Key])
306
306
  assert_equal("Otherthing",
307
- @handler.decrypt(@document.revisions[1].trailer[:Encrypt])[:Key])
307
+ @handler.decrypt(@document.revisions.all[1].trailer[:Encrypt])[:Key])
308
308
  end
309
309
 
310
310
  it "defers handling encryption to a Crypt filter is specified" do
@@ -378,7 +378,7 @@ describe HexaPDF::Encryption::SecurityHandler do
378
378
 
379
379
  it "works correctly with different decryption and encryption handlers" do
380
380
  test_file = File.join(TEST_DATA_DIR, 'standard-security-handler', 'nopwd-arc4-40bit-V1.pdf')
381
- doc = HexaPDF::Document.new(io: StringIO.new(File.read(test_file)))
381
+ doc = HexaPDF::Document.new(io: StringIO.new(File.binread(test_file)))
382
382
  doc.encrypt(algorithm: :aes, password: 'test')
383
383
  out = StringIO.new(''.b)
384
384
  doc.write(out, update_fields: false)
@@ -21,6 +21,10 @@ describe HexaPDF::Font::Encoding::GlyphList do
21
21
  assert_equal("\u05da\u05b8", @list.name_to_unicode(:finalkafqamats))
22
22
  end
23
23
 
24
+ it "parses the whole file" do
25
+ assert_equal("ズ", @list.name_to_unicode(:zukatakana))
26
+ end
27
+
24
28
  it "maps special uniXXXX names to unicode values" do
25
29
  assert_equal("A", @list.name_to_unicode(:uni0041))
26
30
  assert_equal("\u1234", @list.name_to_unicode(:uni1234))