hexapdf 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (346) hide show
  1. checksums.yaml +7 -0
  2. data/CONTRIBUTERS +3 -0
  3. data/LICENSE +26 -0
  4. data/README.md +88 -0
  5. data/Rakefile +121 -0
  6. data/VERSION +1 -0
  7. data/agpl-3.0.txt +661 -0
  8. data/bin/hexapdf +6 -0
  9. data/data/hexapdf/afm/Courier-Bold.afm +342 -0
  10. data/data/hexapdf/afm/Courier-BoldOblique.afm +342 -0
  11. data/data/hexapdf/afm/Courier-Oblique.afm +342 -0
  12. data/data/hexapdf/afm/Courier.afm +342 -0
  13. data/data/hexapdf/afm/Helvetica-Bold.afm +2827 -0
  14. data/data/hexapdf/afm/Helvetica-BoldOblique.afm +2827 -0
  15. data/data/hexapdf/afm/Helvetica-Oblique.afm +3051 -0
  16. data/data/hexapdf/afm/Helvetica.afm +3051 -0
  17. data/data/hexapdf/afm/MustRead.html +1 -0
  18. data/data/hexapdf/afm/Symbol.afm +213 -0
  19. data/data/hexapdf/afm/Times-Bold.afm +2588 -0
  20. data/data/hexapdf/afm/Times-BoldItalic.afm +2384 -0
  21. data/data/hexapdf/afm/Times-Italic.afm +2667 -0
  22. data/data/hexapdf/afm/Times-Roman.afm +2419 -0
  23. data/data/hexapdf/afm/ZapfDingbats.afm +225 -0
  24. data/data/hexapdf/encoding/glyphlist.txt +4305 -0
  25. data/data/hexapdf/encoding/zapfdingbats.txt +225 -0
  26. data/examples/arc.rb +50 -0
  27. data/examples/graphics.rb +274 -0
  28. data/examples/hello_world.rb +16 -0
  29. data/examples/machupicchu.jpg +0 -0
  30. data/examples/merging.rb +24 -0
  31. data/examples/optimizing.rb +20 -0
  32. data/examples/show_char_bboxes.rb +55 -0
  33. data/examples/standard_pdf_fonts.rb +72 -0
  34. data/examples/truetype.rb +45 -0
  35. data/lib/hexapdf/cli/extract.rb +128 -0
  36. data/lib/hexapdf/cli/info.rb +121 -0
  37. data/lib/hexapdf/cli/inspect.rb +157 -0
  38. data/lib/hexapdf/cli/modify.rb +218 -0
  39. data/lib/hexapdf/cli.rb +121 -0
  40. data/lib/hexapdf/configuration.rb +392 -0
  41. data/lib/hexapdf/content/canvas.rb +1974 -0
  42. data/lib/hexapdf/content/color_space.rb +364 -0
  43. data/lib/hexapdf/content/graphic_object/arc.rb +267 -0
  44. data/lib/hexapdf/content/graphic_object/endpoint_arc.rb +208 -0
  45. data/lib/hexapdf/content/graphic_object/solid_arc.rb +173 -0
  46. data/lib/hexapdf/content/graphic_object.rb +81 -0
  47. data/lib/hexapdf/content/graphics_state.rb +579 -0
  48. data/lib/hexapdf/content/operator.rb +1072 -0
  49. data/lib/hexapdf/content/parser.rb +204 -0
  50. data/lib/hexapdf/content/processor.rb +451 -0
  51. data/lib/hexapdf/content/transformation_matrix.rb +172 -0
  52. data/lib/hexapdf/content.rb +47 -0
  53. data/lib/hexapdf/data_dir.rb +51 -0
  54. data/lib/hexapdf/dictionary.rb +303 -0
  55. data/lib/hexapdf/dictionary_fields.rb +382 -0
  56. data/lib/hexapdf/document.rb +589 -0
  57. data/lib/hexapdf/document_utils.rb +209 -0
  58. data/lib/hexapdf/encryption/aes.rb +206 -0
  59. data/lib/hexapdf/encryption/arc4.rb +93 -0
  60. data/lib/hexapdf/encryption/fast_aes.rb +79 -0
  61. data/lib/hexapdf/encryption/fast_arc4.rb +67 -0
  62. data/lib/hexapdf/encryption/identity.rb +63 -0
  63. data/lib/hexapdf/encryption/ruby_aes.rb +447 -0
  64. data/lib/hexapdf/encryption/ruby_arc4.rb +96 -0
  65. data/lib/hexapdf/encryption/security_handler.rb +494 -0
  66. data/lib/hexapdf/encryption/standard_security_handler.rb +616 -0
  67. data/lib/hexapdf/encryption.rb +94 -0
  68. data/lib/hexapdf/error.rb +73 -0
  69. data/lib/hexapdf/filter/ascii85_decode.rb +160 -0
  70. data/lib/hexapdf/filter/ascii_hex_decode.rb +87 -0
  71. data/lib/hexapdf/filter/dct_decode.rb +57 -0
  72. data/lib/hexapdf/filter/encryption.rb +59 -0
  73. data/lib/hexapdf/filter/flate_decode.rb +93 -0
  74. data/lib/hexapdf/filter/jpx_decode.rb +56 -0
  75. data/lib/hexapdf/filter/lzw_decode.rb +191 -0
  76. data/lib/hexapdf/filter/predictor.rb +266 -0
  77. data/lib/hexapdf/filter/run_length_decode.rb +108 -0
  78. data/lib/hexapdf/filter.rb +176 -0
  79. data/lib/hexapdf/font/cmap/parser.rb +146 -0
  80. data/lib/hexapdf/font/cmap/writer.rb +176 -0
  81. data/lib/hexapdf/font/cmap.rb +90 -0
  82. data/lib/hexapdf/font/encoding/base.rb +77 -0
  83. data/lib/hexapdf/font/encoding/difference_encoding.rb +64 -0
  84. data/lib/hexapdf/font/encoding/glyph_list.rb +150 -0
  85. data/lib/hexapdf/font/encoding/mac_expert_encoding.rb +221 -0
  86. data/lib/hexapdf/font/encoding/mac_roman_encoding.rb +265 -0
  87. data/lib/hexapdf/font/encoding/standard_encoding.rb +205 -0
  88. data/lib/hexapdf/font/encoding/symbol_encoding.rb +244 -0
  89. data/lib/hexapdf/font/encoding/win_ansi_encoding.rb +280 -0
  90. data/lib/hexapdf/font/encoding/zapf_dingbats_encoding.rb +250 -0
  91. data/lib/hexapdf/font/encoding.rb +68 -0
  92. data/lib/hexapdf/font/true_type/font.rb +179 -0
  93. data/lib/hexapdf/font/true_type/table/cmap.rb +103 -0
  94. data/lib/hexapdf/font/true_type/table/cmap_subtable.rb +384 -0
  95. data/lib/hexapdf/font/true_type/table/directory.rb +92 -0
  96. data/lib/hexapdf/font/true_type/table/glyf.rb +166 -0
  97. data/lib/hexapdf/font/true_type/table/head.rb +143 -0
  98. data/lib/hexapdf/font/true_type/table/hhea.rb +109 -0
  99. data/lib/hexapdf/font/true_type/table/hmtx.rb +79 -0
  100. data/lib/hexapdf/font/true_type/table/loca.rb +79 -0
  101. data/lib/hexapdf/font/true_type/table/maxp.rb +112 -0
  102. data/lib/hexapdf/font/true_type/table/name.rb +218 -0
  103. data/lib/hexapdf/font/true_type/table/os2.rb +200 -0
  104. data/lib/hexapdf/font/true_type/table/post.rb +230 -0
  105. data/lib/hexapdf/font/true_type/table.rb +155 -0
  106. data/lib/hexapdf/font/true_type.rb +48 -0
  107. data/lib/hexapdf/font/true_type_wrapper.rb +240 -0
  108. data/lib/hexapdf/font/type1/afm_parser.rb +230 -0
  109. data/lib/hexapdf/font/type1/character_metrics.rb +67 -0
  110. data/lib/hexapdf/font/type1/font.rb +123 -0
  111. data/lib/hexapdf/font/type1/font_metrics.rb +117 -0
  112. data/lib/hexapdf/font/type1/pfb_parser.rb +71 -0
  113. data/lib/hexapdf/font/type1.rb +52 -0
  114. data/lib/hexapdf/font/type1_wrapper.rb +193 -0
  115. data/lib/hexapdf/font_loader/from_configuration.rb +70 -0
  116. data/lib/hexapdf/font_loader/standard14.rb +98 -0
  117. data/lib/hexapdf/font_loader.rb +85 -0
  118. data/lib/hexapdf/font_utils.rb +89 -0
  119. data/lib/hexapdf/image_loader/jpeg.rb +166 -0
  120. data/lib/hexapdf/image_loader/pdf.rb +89 -0
  121. data/lib/hexapdf/image_loader/png.rb +410 -0
  122. data/lib/hexapdf/image_loader.rb +68 -0
  123. data/lib/hexapdf/importer.rb +139 -0
  124. data/lib/hexapdf/name_tree_node.rb +78 -0
  125. data/lib/hexapdf/number_tree_node.rb +67 -0
  126. data/lib/hexapdf/object.rb +363 -0
  127. data/lib/hexapdf/parser.rb +349 -0
  128. data/lib/hexapdf/rectangle.rb +99 -0
  129. data/lib/hexapdf/reference.rb +98 -0
  130. data/lib/hexapdf/revision.rb +206 -0
  131. data/lib/hexapdf/revisions.rb +194 -0
  132. data/lib/hexapdf/serializer.rb +326 -0
  133. data/lib/hexapdf/stream.rb +279 -0
  134. data/lib/hexapdf/task/dereference.rb +109 -0
  135. data/lib/hexapdf/task/optimize.rb +230 -0
  136. data/lib/hexapdf/task.rb +68 -0
  137. data/lib/hexapdf/tokenizer.rb +406 -0
  138. data/lib/hexapdf/type/catalog.rb +107 -0
  139. data/lib/hexapdf/type/embedded_file.rb +87 -0
  140. data/lib/hexapdf/type/file_specification.rb +232 -0
  141. data/lib/hexapdf/type/font.rb +81 -0
  142. data/lib/hexapdf/type/font_descriptor.rb +109 -0
  143. data/lib/hexapdf/type/font_simple.rb +190 -0
  144. data/lib/hexapdf/type/font_true_type.rb +47 -0
  145. data/lib/hexapdf/type/font_type1.rb +162 -0
  146. data/lib/hexapdf/type/form.rb +103 -0
  147. data/lib/hexapdf/type/graphics_state_parameter.rb +79 -0
  148. data/lib/hexapdf/type/image.rb +73 -0
  149. data/lib/hexapdf/type/info.rb +70 -0
  150. data/lib/hexapdf/type/names.rb +69 -0
  151. data/lib/hexapdf/type/object_stream.rb +224 -0
  152. data/lib/hexapdf/type/page.rb +355 -0
  153. data/lib/hexapdf/type/page_tree_node.rb +269 -0
  154. data/lib/hexapdf/type/resources.rb +212 -0
  155. data/lib/hexapdf/type/trailer.rb +128 -0
  156. data/lib/hexapdf/type/viewer_preferences.rb +73 -0
  157. data/lib/hexapdf/type/xref_stream.rb +204 -0
  158. data/lib/hexapdf/type.rb +67 -0
  159. data/lib/hexapdf/utils/bit_field.rb +87 -0
  160. data/lib/hexapdf/utils/bit_stream.rb +148 -0
  161. data/lib/hexapdf/utils/lru_cache.rb +65 -0
  162. data/lib/hexapdf/utils/math_helpers.rb +55 -0
  163. data/lib/hexapdf/utils/object_hash.rb +130 -0
  164. data/lib/hexapdf/utils/pdf_doc_encoding.rb +93 -0
  165. data/lib/hexapdf/utils/sorted_tree_node.rb +339 -0
  166. data/lib/hexapdf/version.rb +39 -0
  167. data/lib/hexapdf/writer.rb +199 -0
  168. data/lib/hexapdf/xref_section.rb +152 -0
  169. data/lib/hexapdf.rb +34 -0
  170. data/man/man1/hexapdf.1 +249 -0
  171. data/test/data/aes-test-vectors/CBCGFSbox-128-decrypt.data.gz +0 -0
  172. data/test/data/aes-test-vectors/CBCGFSbox-128-encrypt.data.gz +0 -0
  173. data/test/data/aes-test-vectors/CBCGFSbox-192-decrypt.data.gz +0 -0
  174. data/test/data/aes-test-vectors/CBCGFSbox-192-encrypt.data.gz +0 -0
  175. data/test/data/aes-test-vectors/CBCGFSbox-256-decrypt.data.gz +0 -0
  176. data/test/data/aes-test-vectors/CBCGFSbox-256-encrypt.data.gz +0 -0
  177. data/test/data/aes-test-vectors/CBCKeySbox-128-decrypt.data.gz +0 -0
  178. data/test/data/aes-test-vectors/CBCKeySbox-128-encrypt.data.gz +0 -0
  179. data/test/data/aes-test-vectors/CBCKeySbox-192-decrypt.data.gz +0 -0
  180. data/test/data/aes-test-vectors/CBCKeySbox-192-encrypt.data.gz +0 -0
  181. data/test/data/aes-test-vectors/CBCKeySbox-256-decrypt.data.gz +0 -0
  182. data/test/data/aes-test-vectors/CBCKeySbox-256-encrypt.data.gz +0 -0
  183. data/test/data/aes-test-vectors/CBCVarKey-128-decrypt.data.gz +0 -0
  184. data/test/data/aes-test-vectors/CBCVarKey-128-encrypt.data.gz +0 -0
  185. data/test/data/aes-test-vectors/CBCVarKey-192-decrypt.data.gz +0 -0
  186. data/test/data/aes-test-vectors/CBCVarKey-192-encrypt.data.gz +0 -0
  187. data/test/data/aes-test-vectors/CBCVarKey-256-decrypt.data.gz +0 -0
  188. data/test/data/aes-test-vectors/CBCVarKey-256-encrypt.data.gz +0 -0
  189. data/test/data/aes-test-vectors/CBCVarTxt-128-decrypt.data.gz +0 -0
  190. data/test/data/aes-test-vectors/CBCVarTxt-128-encrypt.data.gz +0 -0
  191. data/test/data/aes-test-vectors/CBCVarTxt-192-decrypt.data.gz +0 -0
  192. data/test/data/aes-test-vectors/CBCVarTxt-192-encrypt.data.gz +0 -0
  193. data/test/data/aes-test-vectors/CBCVarTxt-256-decrypt.data.gz +0 -0
  194. data/test/data/aes-test-vectors/CBCVarTxt-256-encrypt.data.gz +0 -0
  195. data/test/data/fonts/Ubuntu-Title.ttf +0 -0
  196. data/test/data/images/cmyk.jpg +0 -0
  197. data/test/data/images/fillbytes.jpg +0 -0
  198. data/test/data/images/gray.jpg +0 -0
  199. data/test/data/images/greyscale-1bit.png +0 -0
  200. data/test/data/images/greyscale-2bit.png +0 -0
  201. data/test/data/images/greyscale-4bit.png +0 -0
  202. data/test/data/images/greyscale-8bit.png +0 -0
  203. data/test/data/images/greyscale-alpha-8bit.png +0 -0
  204. data/test/data/images/greyscale-trns-8bit.png +0 -0
  205. data/test/data/images/greyscale-with-gamma1.0.png +0 -0
  206. data/test/data/images/greyscale-with-gamma1.5.png +0 -0
  207. data/test/data/images/indexed-1bit.png +0 -0
  208. data/test/data/images/indexed-2bit.png +0 -0
  209. data/test/data/images/indexed-4bit.png +0 -0
  210. data/test/data/images/indexed-8bit.png +0 -0
  211. data/test/data/images/indexed-alpha-4bit.png +0 -0
  212. data/test/data/images/indexed-alpha-8bit.png +0 -0
  213. data/test/data/images/rgb.jpg +0 -0
  214. data/test/data/images/truecolour-8bit.png +0 -0
  215. data/test/data/images/truecolour-alpha-8bit.png +0 -0
  216. data/test/data/images/truecolour-gama-chrm-8bit.png +0 -0
  217. data/test/data/images/truecolour-srgb-8bit.png +0 -0
  218. data/test/data/minimal.pdf +44 -0
  219. data/test/data/standard-security-handler/README +9 -0
  220. data/test/data/standard-security-handler/bothpwd-aes-128bit-V4.pdf +44 -0
  221. data/test/data/standard-security-handler/bothpwd-aes-256bit-V5.pdf +0 -0
  222. data/test/data/standard-security-handler/bothpwd-arc4-128bit-V2.pdf +43 -0
  223. data/test/data/standard-security-handler/bothpwd-arc4-128bit-V4.pdf +43 -0
  224. data/test/data/standard-security-handler/bothpwd-arc4-40bit-V1.pdf +0 -0
  225. data/test/data/standard-security-handler/nopwd-aes-128bit-V4.pdf +43 -0
  226. data/test/data/standard-security-handler/nopwd-aes-256bit-V5.pdf +0 -0
  227. data/test/data/standard-security-handler/nopwd-arc4-128bit-V2.pdf +43 -0
  228. data/test/data/standard-security-handler/nopwd-arc4-128bit-V4.pdf +43 -0
  229. data/test/data/standard-security-handler/nopwd-arc4-40bit-V1.pdf +43 -0
  230. data/test/data/standard-security-handler/ownerpwd-aes-128bit-V4.pdf +0 -0
  231. data/test/data/standard-security-handler/ownerpwd-aes-256bit-V5.pdf +43 -0
  232. data/test/data/standard-security-handler/ownerpwd-arc4-128bit-V2.pdf +43 -0
  233. data/test/data/standard-security-handler/ownerpwd-arc4-128bit-V4.pdf +43 -0
  234. data/test/data/standard-security-handler/ownerpwd-arc4-40bit-V1.pdf +43 -0
  235. data/test/data/standard-security-handler/userpwd-aes-128bit-V4.pdf +43 -0
  236. data/test/data/standard-security-handler/userpwd-aes-256bit-V5.pdf +43 -0
  237. data/test/data/standard-security-handler/userpwd-arc4-128bit-V2.pdf +0 -0
  238. data/test/data/standard-security-handler/userpwd-arc4-128bit-V4.pdf +0 -0
  239. data/test/data/standard-security-handler/userpwd-arc4-40bit-V1.pdf +43 -0
  240. data/test/hexapdf/common_tokenizer_tests.rb +204 -0
  241. data/test/hexapdf/content/common.rb +31 -0
  242. data/test/hexapdf/content/graphic_object/test_arc.rb +93 -0
  243. data/test/hexapdf/content/graphic_object/test_endpoint_arc.rb +91 -0
  244. data/test/hexapdf/content/graphic_object/test_solid_arc.rb +86 -0
  245. data/test/hexapdf/content/test_canvas.rb +1113 -0
  246. data/test/hexapdf/content/test_color_space.rb +97 -0
  247. data/test/hexapdf/content/test_graphics_state.rb +138 -0
  248. data/test/hexapdf/content/test_operator.rb +619 -0
  249. data/test/hexapdf/content/test_parser.rb +66 -0
  250. data/test/hexapdf/content/test_processor.rb +156 -0
  251. data/test/hexapdf/content/test_transformation_matrix.rb +64 -0
  252. data/test/hexapdf/encryption/common.rb +87 -0
  253. data/test/hexapdf/encryption/test_aes.rb +121 -0
  254. data/test/hexapdf/encryption/test_arc4.rb +39 -0
  255. data/test/hexapdf/encryption/test_fast_aes.rb +17 -0
  256. data/test/hexapdf/encryption/test_fast_arc4.rb +12 -0
  257. data/test/hexapdf/encryption/test_identity.rb +21 -0
  258. data/test/hexapdf/encryption/test_ruby_aes.rb +23 -0
  259. data/test/hexapdf/encryption/test_ruby_arc4.rb +20 -0
  260. data/test/hexapdf/encryption/test_security_handler.rb +356 -0
  261. data/test/hexapdf/encryption/test_standard_security_handler.rb +274 -0
  262. data/test/hexapdf/filter/common.rb +53 -0
  263. data/test/hexapdf/filter/test_ascii85_decode.rb +60 -0
  264. data/test/hexapdf/filter/test_ascii_hex_decode.rb +33 -0
  265. data/test/hexapdf/filter/test_encryption.rb +24 -0
  266. data/test/hexapdf/filter/test_flate_decode.rb +35 -0
  267. data/test/hexapdf/filter/test_lzw_decode.rb +52 -0
  268. data/test/hexapdf/filter/test_predictor.rb +183 -0
  269. data/test/hexapdf/filter/test_run_length_decode.rb +32 -0
  270. data/test/hexapdf/font/cmap/test_parser.rb +67 -0
  271. data/test/hexapdf/font/cmap/test_writer.rb +58 -0
  272. data/test/hexapdf/font/encoding/test_base.rb +35 -0
  273. data/test/hexapdf/font/encoding/test_difference_encoding.rb +21 -0
  274. data/test/hexapdf/font/encoding/test_glyph_list.rb +59 -0
  275. data/test/hexapdf/font/encoding/test_zapf_dingbats_encoding.rb +16 -0
  276. data/test/hexapdf/font/test_encoding.rb +27 -0
  277. data/test/hexapdf/font/test_true_type_wrapper.rb +110 -0
  278. data/test/hexapdf/font/test_type1_wrapper.rb +66 -0
  279. data/test/hexapdf/font/true_type/common.rb +19 -0
  280. data/test/hexapdf/font/true_type/table/test_cmap.rb +59 -0
  281. data/test/hexapdf/font/true_type/table/test_cmap_subtable.rb +133 -0
  282. data/test/hexapdf/font/true_type/table/test_directory.rb +35 -0
  283. data/test/hexapdf/font/true_type/table/test_glyf.rb +58 -0
  284. data/test/hexapdf/font/true_type/table/test_head.rb +76 -0
  285. data/test/hexapdf/font/true_type/table/test_hhea.rb +40 -0
  286. data/test/hexapdf/font/true_type/table/test_hmtx.rb +38 -0
  287. data/test/hexapdf/font/true_type/table/test_loca.rb +43 -0
  288. data/test/hexapdf/font/true_type/table/test_maxp.rb +62 -0
  289. data/test/hexapdf/font/true_type/table/test_name.rb +95 -0
  290. data/test/hexapdf/font/true_type/table/test_os2.rb +65 -0
  291. data/test/hexapdf/font/true_type/table/test_post.rb +89 -0
  292. data/test/hexapdf/font/true_type/test_font.rb +120 -0
  293. data/test/hexapdf/font/true_type/test_table.rb +41 -0
  294. data/test/hexapdf/font/type1/test_afm_parser.rb +51 -0
  295. data/test/hexapdf/font/type1/test_font.rb +68 -0
  296. data/test/hexapdf/font/type1/test_pfb_parser.rb +37 -0
  297. data/test/hexapdf/font_loader/test_from_configuration.rb +28 -0
  298. data/test/hexapdf/font_loader/test_standard14.rb +22 -0
  299. data/test/hexapdf/image_loader/test_jpeg.rb +83 -0
  300. data/test/hexapdf/image_loader/test_pdf.rb +47 -0
  301. data/test/hexapdf/image_loader/test_png.rb +258 -0
  302. data/test/hexapdf/task/test_dereference.rb +46 -0
  303. data/test/hexapdf/task/test_optimize.rb +137 -0
  304. data/test/hexapdf/test_configuration.rb +82 -0
  305. data/test/hexapdf/test_data_dir.rb +32 -0
  306. data/test/hexapdf/test_dictionary.rb +284 -0
  307. data/test/hexapdf/test_dictionary_fields.rb +185 -0
  308. data/test/hexapdf/test_document.rb +574 -0
  309. data/test/hexapdf/test_document_utils.rb +144 -0
  310. data/test/hexapdf/test_filter.rb +96 -0
  311. data/test/hexapdf/test_font_utils.rb +47 -0
  312. data/test/hexapdf/test_importer.rb +78 -0
  313. data/test/hexapdf/test_object.rb +177 -0
  314. data/test/hexapdf/test_parser.rb +394 -0
  315. data/test/hexapdf/test_rectangle.rb +36 -0
  316. data/test/hexapdf/test_reference.rb +41 -0
  317. data/test/hexapdf/test_revision.rb +139 -0
  318. data/test/hexapdf/test_revisions.rb +93 -0
  319. data/test/hexapdf/test_serializer.rb +169 -0
  320. data/test/hexapdf/test_stream.rb +262 -0
  321. data/test/hexapdf/test_tokenizer.rb +30 -0
  322. data/test/hexapdf/test_writer.rb +120 -0
  323. data/test/hexapdf/test_xref_section.rb +35 -0
  324. data/test/hexapdf/type/test_catalog.rb +30 -0
  325. data/test/hexapdf/type/test_embedded_file.rb +16 -0
  326. data/test/hexapdf/type/test_file_specification.rb +148 -0
  327. data/test/hexapdf/type/test_font.rb +35 -0
  328. data/test/hexapdf/type/test_font_descriptor.rb +51 -0
  329. data/test/hexapdf/type/test_font_simple.rb +190 -0
  330. data/test/hexapdf/type/test_font_type1.rb +128 -0
  331. data/test/hexapdf/type/test_form.rb +60 -0
  332. data/test/hexapdf/type/test_info.rb +14 -0
  333. data/test/hexapdf/type/test_names.rb +9 -0
  334. data/test/hexapdf/type/test_object_stream.rb +84 -0
  335. data/test/hexapdf/type/test_page.rb +260 -0
  336. data/test/hexapdf/type/test_page_tree_node.rb +255 -0
  337. data/test/hexapdf/type/test_resources.rb +167 -0
  338. data/test/hexapdf/type/test_trailer.rb +109 -0
  339. data/test/hexapdf/type/test_xref_stream.rb +131 -0
  340. data/test/hexapdf/utils/test_bit_field.rb +47 -0
  341. data/test/hexapdf/utils/test_lru_cache.rb +22 -0
  342. data/test/hexapdf/utils/test_object_hash.rb +115 -0
  343. data/test/hexapdf/utils/test_pdf_doc_encoding.rb +18 -0
  344. data/test/hexapdf/utils/test_sorted_tree_node.rb +232 -0
  345. data/test/test_helper.rb +56 -0
  346. metadata +427 -0
@@ -0,0 +1,96 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'test_helper'
4
+ require 'hexapdf/filter'
5
+ require 'stringio'
6
+ require 'tempfile'
7
+
8
+ describe HexaPDF::Filter do
9
+ before do
10
+ @obj = HexaPDF::Filter
11
+ @str = ''
12
+ 40.times { @str << [rand(2**32)].pack('N') }
13
+ end
14
+
15
+ describe "source_from_string" do
16
+ it "doesn't modify the given string" do
17
+ str = @str.dup
18
+ @obj.source_from_string(@str).resume.slice!(0, 10)
19
+ assert_equal(str, @str)
20
+ end
21
+
22
+ it "returns the whole string" do
23
+ assert_equal(@str, collector(@obj.source_from_string(@str)))
24
+ end
25
+ end
26
+
27
+ describe "source_from_io" do
28
+ before do
29
+ @io = StringIO.new(@str.dup)
30
+ end
31
+
32
+ it "converts an IO into a source via #source_from_io" do
33
+ assert_equal(@str, collector(@obj.source_from_io(@io)))
34
+
35
+ assert_equal(@str, collector(@obj.source_from_io(@io, pos: -10)))
36
+ assert_equal(@str[10..-1], collector(@obj.source_from_io(@io, pos: 10)))
37
+ assert_equal("", collector(@obj.source_from_io(@io, pos: 200)))
38
+
39
+ assert_equal("", collector(@obj.source_from_io(@io, length: 0)))
40
+ assert_equal(@str[0...100], collector(@obj.source_from_io(@io, length: 100)))
41
+ assert_equal(@str, collector(@obj.source_from_io(@io, length: -15)))
42
+ assert_equal(100, @obj.source_from_io(@io, length: 100).length)
43
+
44
+ assert_equal(@str, collector(@obj.source_from_io(@io, chunk_size: -15)))
45
+ assert_equal(@str, collector(@obj.source_from_io(@io, chunk_size: 0)))
46
+ assert_equal(@str, collector(@obj.source_from_io(@io, chunk_size: 100)))
47
+ assert_equal(@str, collector(@obj.source_from_io(@io, chunk_size: 200)))
48
+
49
+ assert_equal(@str[0...20], collector(@obj.source_from_io(@io, length: 20, chunk_size: 100)))
50
+ assert_equal(@str[20...40], collector(@obj.source_from_io(@io, pos: 20, length: 20, chunk_size: 100)))
51
+ assert_equal(@str[20...40], collector(@obj.source_from_io(@io, pos: 20, length: 20, chunk_size: 5)))
52
+ end
53
+
54
+ it "fails if not all requested bytes could be read" do
55
+ assert_raises(HexaPDF::FilterError) do
56
+ collector(@obj.source_from_io(@io, length: 200))
57
+ end
58
+ end
59
+ end
60
+
61
+ describe "source_from_file" do
62
+ before do
63
+ @file = Tempfile.new('hexapdf-filter')
64
+ @file.write(@str)
65
+ @file.close
66
+ end
67
+
68
+ after do
69
+ @file.unlink
70
+ end
71
+
72
+ it "converts the file into a source fiber" do
73
+ assert_equal(@str, collector(@obj.source_from_file(@file.path)))
74
+ assert_equal(@file.size, @obj.source_from_file(@file.path).length)
75
+
76
+ assert_equal(@str[100..-1], collector(@obj.source_from_file(@file.path, pos: 100)))
77
+ assert_equal(@str[100..-1].length, @obj.source_from_file(@file.path, pos: 100).length)
78
+
79
+ assert_equal(@str[50...100], collector(@obj.source_from_file(@file.path, pos: 50, length: 50)))
80
+ assert_equal(50, @obj.source_from_file(@file.path, length: 50).length)
81
+ end
82
+
83
+ it "fails if more bytes are requested than stored in the file" do
84
+ assert_raises(HexaPDF::FilterError) do
85
+ collector(@obj.source_from_file(@file.path, length: 200))
86
+ end
87
+ end
88
+ end
89
+
90
+ it "collects the binary string from a source via #string_from_source" do
91
+ source = @obj.source_from_io(StringIO.new(@str), chunk_size: 50)
92
+ result = @obj.string_from_source(source)
93
+ assert_equal(@str, result)
94
+ assert_equal(Encoding::BINARY, result.encoding)
95
+ end
96
+ end
@@ -0,0 +1,47 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'test_helper'
4
+ require 'hexapdf/font_utils'
5
+ require 'hexapdf/document'
6
+
7
+ describe HexaPDF::FontUtils do
8
+ before do
9
+ @doc = HexaPDF::Document.new
10
+ @doc.config['font_loader'] = []
11
+ end
12
+
13
+ describe "load" do
14
+ before do
15
+ @doc.config['font_loader'] << lambda do |doc, name, **options|
16
+ assert_equal(@doc, doc)
17
+ if name == :TestFont
18
+ x = Object.new
19
+ x.define_singleton_method(:name) do
20
+ options[:variant] == :bold ? :BoldFont : :NormalFont
21
+ end
22
+ x
23
+ else
24
+ nil
25
+ end
26
+ end
27
+ end
28
+
29
+ it "loads the specified font" do
30
+ assert_equal(:NormalFont, @doc.fonts.load(:TestFont).name)
31
+ assert_equal(:BoldFont, @doc.fonts.load(:TestFont, variant: :bold).name)
32
+ end
33
+
34
+ it "caches loaded fonts" do
35
+ assert_same(@doc.fonts.load(:TestFont), @doc.fonts.load(:TestFont))
36
+ end
37
+
38
+ it "fails if the requested font is not found" do
39
+ assert_raises(HexaPDF::Error) { @doc.fonts.load("Unknown") }
40
+ end
41
+
42
+ it "raises an error if a font loader cannot be correctly retrieved" do
43
+ @doc.config['font_loader'][0] = 'UnknownFontLoader'
44
+ assert_raises(HexaPDF::Error) { @doc.fonts.load(:Other) }
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,78 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'test_helper'
4
+ require 'hexapdf/importer'
5
+ require 'hexapdf/document'
6
+
7
+ describe HexaPDF::Importer do
8
+ before do
9
+ @source = HexaPDF::Document.new
10
+ obj = @source.add("test")
11
+ @obj = @source.add(hash: {key: "value"}, array: ["one", "two"],
12
+ ref: HexaPDF::Reference.new(obj.oid, obj.gen),
13
+ others: [:symbol, 5, 5.5, nil, true, false])
14
+ @source.pages.add_page
15
+ @source.pages[:Rotate] = 90
16
+ @dest = HexaPDF::Document.new
17
+ @importer = HexaPDF::Importer.for(source: @source, destination: @dest)
18
+ end
19
+
20
+ describe "::for" do
21
+ it "caches the importer" do
22
+ assert_same(@importer, HexaPDF::Importer.for(source: @source, destination: @dest))
23
+ end
24
+ end
25
+
26
+ describe "import" do
27
+ it "imports an object only once" do
28
+ obj = @importer.import(@obj)
29
+ assert_same(obj, @importer.import(@obj))
30
+ assert_equal(2, @dest.each.to_a.size)
31
+ end
32
+
33
+ it "re-imports an object that was imported but then deleted" do
34
+ obj = @importer.import(@obj)
35
+ @dest.delete(obj)
36
+ refute_same(obj, @importer.import(@obj))
37
+ end
38
+
39
+ it "can import a direct object" do
40
+ obj = @importer.import(key: @obj)
41
+ assert(@dest.object?(obj[:key]))
42
+ end
43
+
44
+ it "copies the data of the imported objects" do
45
+ data = {key: @obj, str: "str"}
46
+ obj = @importer.import(data)
47
+ obj[:str].upcase!
48
+ obj[:key][:hash][:key].upcase!
49
+ obj[:key][:hash][:data] = :value
50
+ obj[:key][:array].unshift
51
+ obj[:key][:array][0].upcase!
52
+
53
+ assert_equal("str", data[:str])
54
+ assert_equal("value", @obj[:hash][:key])
55
+ assert_equal(["one", "two"], @obj[:array])
56
+ end
57
+
58
+ it "does not import objects of type Catalog or Pages" do
59
+ @obj[:catalog] = @source.catalog
60
+ @obj[:pages] = @source.pages
61
+ obj = @importer.import(@obj)
62
+
63
+ assert_nil(obj[:catalog])
64
+ assert_nil(obj[:pages])
65
+ end
66
+
67
+ it "imports Page objects correctly by copying the inherited values" do
68
+ page = @importer.import(@source.pages.page(0))
69
+ assert_equal(90, page[:Rotate])
70
+ end
71
+
72
+ it "raise an error if the given object doesn't belong to the source document" do
73
+ other_doc = HexaPDF::Document.new
74
+ other_obj = other_doc.add("test")
75
+ assert_raises(HexaPDF::Error) { @importer.import(other_obj) }
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,177 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'test_helper'
4
+ require 'hexapdf/object'
5
+ require 'hexapdf/reference'
6
+
7
+ describe HexaPDF::Object do
8
+ describe "class.deep_copy" do
9
+ it "handles not-duplicatable classes" do
10
+ assert_equal(5, HexaPDF::Object.deep_copy(5))
11
+ assert_equal(5.5, HexaPDF::Object.deep_copy(5.5))
12
+ assert_equal(nil, HexaPDF::Object.deep_copy(nil))
13
+ assert_equal(true, HexaPDF::Object.deep_copy(true))
14
+ assert_equal(false, HexaPDF::Object.deep_copy(false))
15
+ assert_equal(:Name, HexaPDF::Object.deep_copy(:Name))
16
+ end
17
+
18
+ it "handles general, duplicatable classes" do
19
+ x = "test"
20
+ assert_equal("test", HexaPDF::Object.deep_copy(x))
21
+ refute_same(x, HexaPDF::Object.deep_copy(x))
22
+ end
23
+
24
+ it "handles arrays" do
25
+ x = [5, 6, [1, 2, 3]]
26
+ y = HexaPDF::Object.deep_copy(x)
27
+ x[2][0] = 4
28
+ assert_equal([5, 6, [1, 2, 3]], y)
29
+ end
30
+
31
+ it "handles hashes" do
32
+ x = {a: 5, b: 6, c: {a: 1, b: 2}}
33
+ y = HexaPDF::Object.deep_copy(x)
34
+ x[:c][:a] = 4
35
+ assert_equal({a: 5, b: 6, c: {a: 1, b: 2}}, y)
36
+ end
37
+
38
+ it "handles PDF references" do
39
+ x = HexaPDF::Reference.new(1, 2)
40
+ assert_same(x, HexaPDF::Object.deep_copy(x))
41
+ end
42
+
43
+ it "handles PDF objects" do
44
+ x = HexaPDF::Object.new("test")
45
+ assert_equal("test", HexaPDF::Object.deep_copy(x))
46
+ x.oid = 1
47
+ assert_same(x, HexaPDF::Object.deep_copy(x))
48
+ end
49
+ end
50
+
51
+ describe "initialize" do
52
+ it "uses a simple value as is" do
53
+ obj = HexaPDF::Object.new(5)
54
+ assert_equal(5, obj.value)
55
+ end
56
+
57
+ it "reuses the data object of a HexaPDF::Object" do
58
+ obj = HexaPDF::Object.new(5)
59
+ assert_same(obj.data, HexaPDF::Object.new(obj).data)
60
+ end
61
+
62
+ it "uses a provided PDFData structure" do
63
+ obj = HexaPDF::PDFData.new(5)
64
+ assert_equal(obj, HexaPDF::Object.new(obj).data)
65
+ end
66
+ end
67
+
68
+ describe "getters and setters" do
69
+ before do
70
+ @obj = HexaPDF::Object.new(5)
71
+ end
72
+
73
+ it "can get/set oid" do
74
+ @obj.oid = 7
75
+ assert_equal(7, @obj.oid)
76
+ end
77
+
78
+ it "can get/set gen" do
79
+ @obj.gen = 7
80
+ assert_equal(7, @obj.gen)
81
+ end
82
+
83
+ it "can get/set the value" do
84
+ @obj.value = :test
85
+ assert_equal(:test, @obj.value)
86
+ end
87
+ end
88
+
89
+ describe "null?" do
90
+ it "works for nil values" do
91
+ assert(HexaPDF::Object.new(nil).null?)
92
+ end
93
+ end
94
+
95
+ describe "validate" do
96
+ it "invokes perform_validation correctly via #validate" do
97
+ obj = HexaPDF::Object.new(5)
98
+ invoked = {}
99
+ obj.define_singleton_method(:perform_validation) do |&block|
100
+ invoked[:method] = true
101
+ block.call("error", true)
102
+ end
103
+ assert(obj.validate {|*a| invoked[:block] = a})
104
+ assert_equal([:method, :block], invoked.keys)
105
+ assert_equal(["error", true], invoked[:block])
106
+
107
+ refute(obj.validate(auto_correct: false))
108
+ end
109
+
110
+ it "stops validating on an uncorrectable problem" do
111
+ obj = HexaPDF::Object.new(5)
112
+ invoked = {}
113
+ obj.define_singleton_method(:perform_validation) do |&block|
114
+ invoked[:before] = true
115
+ block.call("error", false)
116
+ invoked[:after] = true
117
+ end
118
+ refute(obj.validate {|*a| invoked[:block] = a})
119
+ refute(invoked.key?(:after))
120
+ end
121
+ end
122
+
123
+ it "can represent itself during inspection" do
124
+ obj = HexaPDF::Object.new(5, oid: 5)
125
+ assert_match(/\[5, 0\].*value=5/, obj.inspect)
126
+ end
127
+
128
+ it "can be compared to another object" do
129
+ obj = HexaPDF::Object.new(5, oid: 5)
130
+
131
+ assert_equal(obj, HexaPDF::Object.new(obj))
132
+ refute_equal(obj, HexaPDF::Object.new(5, oid: 5))
133
+ refute_equal(obj, HexaPDF::Object.new(6, oid: 5))
134
+ refute_equal(obj, HexaPDF::Object.new(5, oid: 1))
135
+ refute_equal(obj, HexaPDF::Object.new(5, oid: 5, gen: 1))
136
+ end
137
+
138
+ it "works correctly as hash key, is interchangable in this regard with Reference objects" do
139
+ hash = {}
140
+ hash[HexaPDF::Reference.new(1)] = :one
141
+ hash[HexaPDF::Object.new(:val, oid: 2)] = :two
142
+ assert_equal(:one, hash[HexaPDF::Reference.new(1, 0)])
143
+ assert_equal(:one, hash[HexaPDF::Object.new(:data, oid: 1)])
144
+ assert_equal(:two, hash[HexaPDF::Reference.new(2)])
145
+ assert_equal(:two, hash[HexaPDF::Object.new(:data, oid: 2)])
146
+ end
147
+
148
+ it "can be sorted together with Reference objects" do
149
+ a = HexaPDF::Object.new(:data, oid: 1)
150
+ b = HexaPDF::Object.new(:data, oid: 1, gen: 1)
151
+ c = HexaPDF::Reference.new(5, 7)
152
+ assert_equal([a, b, c], [b, c, a].sort)
153
+ end
154
+
155
+ describe "deep_copy" do
156
+ it "creates an independent object" do
157
+ obj = HexaPDF::Object.new(a: "mystring", b: HexaPDF::Reference.new(1, 0), c: 5)
158
+ copy = obj.deep_copy
159
+ refute_equal(copy, obj)
160
+ assert_equal(copy.value, obj.value)
161
+ refute_same(copy.value[:a], obj.value[:a])
162
+ end
163
+ end
164
+
165
+ it "validates that the object is indirect if it must be indirect" do
166
+ doc = Object.new
167
+ def doc.add(obj) obj.oid = 1 end
168
+ obj = HexaPDF::Object.new(6, document: doc)
169
+
170
+ obj.validate
171
+ assert_equal(0, obj.oid)
172
+
173
+ obj.must_be_indirect = true
174
+ obj.validate
175
+ assert_equal(1, obj.oid)
176
+ end
177
+ end