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,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'test_helper'
4
+ require 'hexapdf/font_loader'
5
+ require 'hexapdf/document'
6
+
7
+ describe HexaPDF::FontLoader::FromConfiguration do
8
+ before do
9
+ @doc = HexaPDF::Document.new
10
+ font_file = File.join(TEST_DATA_DIR, "fonts", "Ubuntu-Title.ttf")
11
+ @doc.config['font.map'] = {'font' => {none: font_file}}
12
+ @klass = HexaPDF::FontLoader::FromConfiguration
13
+ end
14
+
15
+ it "loads the configured font" do
16
+ wrapper = @klass.call(@doc, "font")
17
+ assert_equal("Ubuntu-Title", wrapper.wrapped_font.font_name)
18
+ end
19
+
20
+ it "fails if the font file cannot be read" do
21
+ @doc.config['font.map']['font'][:none] << "unknown"
22
+ assert_raises(HexaPDF::Error) { @klass.call(@doc, "font") }
23
+ end
24
+
25
+ it "returns nil for unknown fonts" do
26
+ assert_nil(@klass.call(@doc, "Unknown"))
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'test_helper'
4
+ require 'hexapdf/font_loader'
5
+ require 'hexapdf/document'
6
+
7
+ describe HexaPDF::FontLoader::Standard14 do
8
+ before do
9
+ @doc = HexaPDF::Document.new
10
+ end
11
+
12
+ it "loads the font if it is a standard PDF built-in font" do
13
+ wrapper = @doc.fonts.load("Times")
14
+ assert_equal("Times-Roman", wrapper.wrapped_font.font_name)
15
+ wrapper = @doc.fonts.load("Helvetica", variant: :bold)
16
+ assert_equal("Helvetica-Bold", wrapper.wrapped_font.font_name)
17
+ end
18
+
19
+ it "returns nil for unknown fonts" do
20
+ assert_nil(HexaPDF::FontLoader::Standard14.call(@doc, "Unknown"))
21
+ end
22
+ end
@@ -0,0 +1,83 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'test_helper'
4
+ require 'hexapdf/document'
5
+ require 'hexapdf/image_loader/jpeg'
6
+
7
+ describe HexaPDF::ImageLoader::JPEG do
8
+ before do
9
+ @images = Dir.glob(File.join(TEST_DATA_DIR, 'images', '*.jpg'))
10
+ @doc = HexaPDF::Document.new
11
+ @loader = HexaPDF::ImageLoader::JPEG
12
+ end
13
+
14
+ describe "handles?" do
15
+ it "works for jpeg files" do
16
+ @images.each do |image|
17
+ assert(@loader.handles?(image))
18
+ File.open(image, 'rb') {|file| assert(@loader.handles?(file))}
19
+ end
20
+ end
21
+ end
22
+
23
+ describe "load" do
24
+ it "can work with an IO stream instead of a file" do
25
+ jpeg = @images.grep(/rgb\.jpg/).first
26
+ File.open(jpeg, 'rb') do |file|
27
+ image = @loader.load(@doc, file)
28
+ assert_equal(File.binread(jpeg), image.stream)
29
+ end
30
+ end
31
+
32
+ it "works for a grayscale jpeg" do
33
+ jpeg = @images.grep(/gray\.jpg/).first
34
+ image = @loader.load(@doc, jpeg)
35
+ assert_equal(5, image[:Width])
36
+ assert_equal(5, image[:Height])
37
+ assert_equal(:DeviceGray, image[:ColorSpace])
38
+ assert_equal(File.binread(jpeg), image.stream)
39
+ end
40
+
41
+ it "works for a standard RGB jpeg" do
42
+ jpeg = @images.grep(/rgb\.jpg/).first
43
+ image = @loader.load(@doc, jpeg)
44
+ assert_equal(5, image[:Width])
45
+ assert_equal(5, image[:Height])
46
+ assert_equal(:DeviceRGB, image[:ColorSpace])
47
+ assert_equal(File.binread(jpeg), image.stream)
48
+ end
49
+
50
+ it "works for a jpeg image containing fill bytes" do
51
+ jpeg = @images.grep(/fillbytes\.jpg/).first
52
+ image = @loader.load(@doc, jpeg)
53
+ assert_equal(5, image[:Width])
54
+ assert_equal(5, image[:Height])
55
+ assert_equal(:DeviceRGB, image[:ColorSpace])
56
+ assert_equal(File.binread(jpeg), image.stream)
57
+ end
58
+
59
+ it "works for a CMYK jpeg" do
60
+ jpeg = @images.grep(/cmyk\.jpg/).first
61
+ image = @loader.load(@doc, jpeg)
62
+ assert_equal(5, image[:Width])
63
+ assert_equal(5, image[:Height])
64
+ assert_equal(:DeviceCMYK, image[:ColorSpace])
65
+ assert_equal([1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0], image[:Decode])
66
+ assert_equal(File.binread(jpeg), image.stream)
67
+ end
68
+
69
+ it "fails if the JPEG is corrupt" do
70
+ exp = assert_raises(HexaPDF::Error) do
71
+ @loader.load(@doc, StringIO.new("some non JPEG data"))
72
+ end
73
+ assert_match(/marker code/, exp.message)
74
+ end
75
+
76
+ it "fails if the JPEG contains components with more/less bits than 8" do
77
+ exp = assert_raises(HexaPDF::Error) do
78
+ @loader.load(@doc, StringIO.new("\xFF\xD8\xFF\xC0\x00\x06\x04\x00\x05\x00\x05\x03".b))
79
+ end
80
+ assert_match(/Unsupported.*bits.*4/, exp.message)
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,47 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'test_helper'
4
+ require 'hexapdf/document'
5
+ require 'hexapdf/image_loader/pdf'
6
+
7
+ describe HexaPDF::ImageLoader::PDF do
8
+ before do
9
+ @doc = HexaPDF::Document.new
10
+ @loader = HexaPDF::ImageLoader::PDF
11
+ @pdf = File.join(TEST_DATA_DIR, 'minimal.pdf')
12
+ end
13
+
14
+ describe "handles?" do
15
+ it "works for PDF files" do
16
+ assert(@loader.handles?(@pdf))
17
+ File.open(@pdf, 'rb') {|file| assert(@loader.handles?(file))}
18
+ end
19
+ end
20
+
21
+ describe "load" do
22
+ it "works for PDF files using a File object" do
23
+ File.open(@pdf, 'rb') do |file|
24
+ form = @loader.load(@doc, file)
25
+ assert_equal(:Form, form[:Subtype])
26
+ end
27
+ end
28
+
29
+ it "works for PDF files using a string object and use_stringio=true" do
30
+ @doc.config['image_loader.pdf.use_stringio'] = true
31
+ form = @loader.load(@doc, @pdf)
32
+ assert_equal(:Form, form[:Subtype])
33
+ end
34
+
35
+ it "works for PDF files using a string object and use_stringio=false" do
36
+ begin
37
+ @doc.config['image_loader.pdf.use_stringio'] = false
38
+ form = @loader.load(@doc, @pdf)
39
+ assert_equal(:Form, form[:Subtype])
40
+ ensure
41
+ ObjectSpace.each_object(File) do |file|
42
+ file.close if file.path == @pdf && !file.closed?
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,258 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'test_helper'
4
+ require 'hexapdf/document'
5
+ require 'hexapdf/image_loader/png'
6
+
7
+ describe HexaPDF::ImageLoader::PNG do
8
+ before do
9
+ @images = Dir.glob(File.join(TEST_DATA_DIR, 'images', '*.png'))
10
+ @doc = HexaPDF::Document.new
11
+ @loader = HexaPDF::ImageLoader::PNG
12
+ end
13
+
14
+ describe "handles?" do
15
+ it "works for png files" do
16
+ @images.each do |image|
17
+ assert(@loader.handles?(image))
18
+ File.open(image, 'rb') {|file| assert(@loader.handles?(file))}
19
+ end
20
+ end
21
+ end
22
+
23
+ def build_png(header_data, others = '')
24
+ @loader::MAGIC_FILE_MARKER + [13, "IHDR", *header_data].pack('NA4N2C5N') + others +
25
+ [0, "IEND", 0].pack('NA4N')
26
+ end
27
+
28
+ def assert_image(image, width, height, bpc, color_space, stream)
29
+ assert_equal(width, image[:Width])
30
+ assert_equal(height, image[:Height])
31
+ assert_equal(bpc, image[:BitsPerComponent])
32
+ assert_equal(color_space, image[:ColorSpace]) if color_space
33
+ data = stream.map {|row| [row.map {|i| i.to_s(2).rjust(bpc, '0')}.join("")].pack('B*')}.join("")
34
+ assert_equal(data, image.stream)
35
+ end
36
+
37
+ # Note: colors and image data for comparisons were extracted using GIMP and its color tools
38
+ describe "load" do
39
+ before do
40
+ @greyscale_1bit_data = [[1, 1, 0, 0, 0],
41
+ [1, 1, 0, 1, 0],
42
+ [1, 1, 1, 1, 1],
43
+ [0, 1, 0, 1, 0],
44
+ [0, 0, 0, 0, 0]]
45
+ end
46
+
47
+ it "can work with an IO stream instead of a file" do
48
+ png = @images.grep(/greyscale-1bit\.png/).first
49
+ File.open(png, 'rb') do |file|
50
+ image = @loader.load(@doc, file)
51
+ assert_image(image, 5, 5, 1, :DeviceGray, @greyscale_1bit_data)
52
+ end
53
+ end
54
+
55
+ it "works for a 1-bit greyscale png" do
56
+ png = @images.grep(/greyscale-1bit\.png/).first
57
+ image = @loader.load(@doc, png)
58
+ assert_image(image, 5, 5, 1, :DeviceGray, @greyscale_1bit_data)
59
+ end
60
+
61
+ it "works for a 2-bit greyscale png" do
62
+ png = @images.grep(/greyscale-2bit\.png/).first
63
+ image = @loader.load(@doc, png)
64
+ data = [[2, 2, 0, 0, 0],
65
+ [2, 3, 0, 3, 0],
66
+ [3, 3, 3, 3, 3],
67
+ [1, 3, 0, 3, 0],
68
+ [1, 1, 0, 0, 0]]
69
+ assert_image(image, 5, 5, 2, :DeviceGray, data)
70
+ end
71
+
72
+ it "works for a 4-bit greyscale png" do
73
+ png = @images.grep(/greyscale-4bit\.png/).first
74
+ image = @loader.load(@doc, png)
75
+ data = [[15, 12, 9, 6, 3]] * 5
76
+ assert_image(image, 5, 5, 4, :DeviceGray, data)
77
+ end
78
+
79
+ it "works for a 8-bit greyscale png" do
80
+ png = @images.grep(/greyscale-8bit\.png/).first
81
+ image = @loader.load(@doc, png)
82
+ data = [[254, 203, 153, 101, 50],
83
+ [253, 203, 152, 100, 50],
84
+ [254, 203, 151, 101, 50],
85
+ [253, 202, 151, 100, 50],
86
+ [253, 202, 151, 100, 49]]
87
+ assert_image(image, 5, 5, 8, :DeviceGray, data)
88
+ end
89
+
90
+ it "works for a 8-bit greyscale png with alpha" do
91
+ png = @images.grep(/greyscale-alpha-8bit\.png/).first
92
+ image = @loader.load(@doc, png)
93
+ data = [[254, 203, 153, 101, 50],
94
+ [253, 203, 152, 100, 50],
95
+ [254, 203, 151, 101, 50],
96
+ [255, 202, 151, 101, 50],
97
+ [255, 202, 151, 101, 50]]
98
+ assert_image(image, 5, 5, 8, :DeviceGray, data)
99
+
100
+ data = [[255, 255, 255, 255, 255]] * 3 +
101
+ [[129, 129, 129, 129, 129]] * 2
102
+ assert_image(image[:SMask], 5, 5, 8, :DeviceGray, data)
103
+ end
104
+
105
+ it "works for a 8-bit greyscale png with a single transparent color" do
106
+ png = @images.grep(/greyscale-trns-8bit\.png/).first
107
+ image = @loader.load(@doc, png)
108
+ data = [[254, 203, 153, 101, 50],
109
+ [253, 203, 152, 100, 50],
110
+ [254, 203, 151, 101, 50],
111
+ [253, 202, 151, 100, 50],
112
+ [253, 202, 151, 100, 49]]
113
+ assert_image(image, 5, 5, 8, :DeviceGray, data)
114
+ assert_equal([203, 203], image[:Mask])
115
+ end
116
+
117
+ it "works for a greyscale png with a gamma value of 1" do
118
+ png = @images.grep(/greyscale-with-gamma1\.0\.png/).first
119
+ image = @loader.load(@doc, png)
120
+ assert_equal(:DeviceGray, image[:ColorSpace])
121
+ end
122
+
123
+ it "works for a greyscale png with a gamma value of 1/1.5" do
124
+ png = @images.grep(/greyscale-with-gamma1\.5\.png/).first
125
+ image = @loader.load(@doc, png)
126
+ assert_equal([:CalGray, {WhitePoint: [1.0, 1.0, 1.0], Gamma: 1 / 1.5}], image[:ColorSpace])
127
+ end
128
+
129
+ it "works for an indexed 1-bit png" do
130
+ png = @images.grep(/indexed-1bit\.png/).first
131
+ image = @loader.load(@doc, png)
132
+ color_space = [:Indexed, :DeviceRGB, 1, "\xFF".b * 3 << "\x0".b * 3]
133
+ data = [[1, 0, 0, 0, 1]] * 5
134
+ assert_image(image, 5, 5, 1, color_space, data)
135
+ end
136
+
137
+ it "works for an indexed 2-bit png" do
138
+ png = @images.grep(/indexed-2bit\.png/).first
139
+ image = @loader.load(@doc, png)
140
+ colors = [44, 117, 63, 55, 118, 165, 97, 172, 110, 164, 189, 209].pack('C*')
141
+ data = [[1, 1, 3, 2, 0]] * 5
142
+ assert_image(image, 5, 5, 2, [:Indexed, :DeviceRGB, 3, colors], data)
143
+ end
144
+
145
+ it "works for an indexed 4-bit png" do
146
+ png = @images.grep(/indexed-4bit\.png/).first
147
+ image = @loader.load(@doc, png)
148
+ data = [[0, 6, 13, 11, 3],
149
+ [0, 7, 14, 11, 2],
150
+ [0, 7, 15, 10, 1],
151
+ [4, 8, 12, 9, 5],
152
+ [4, 8, 12, 9, 5]]
153
+ assert_image(image, 5, 5, 4, nil, data)
154
+ end
155
+
156
+ it "works for an indexed 8-bit png" do
157
+ png = @images.grep(/indexed-8bit\.png/).first
158
+ image = @loader.load(@doc, png)
159
+ data = [[0, 8, 21, 15, 1],
160
+ [0, 7, 20, 13, 3],
161
+ [0, 10, 17, 14, 5],
162
+ [2, 11, 18, 16, 6],
163
+ [2, 9, 19, 12, 4]]
164
+ assert_image(image, 5, 5, 8, nil, data)
165
+ end
166
+
167
+ it "works for an indexed 4-bit png with alpha values" do
168
+ png = @images.grep(/indexed-alpha-4bit\.png/).first
169
+ image = @loader.load(@doc, png)
170
+ data = [[5, 6, 7, 8, 9],
171
+ [5, 10, 11, 8, 9],
172
+ [5, 10, 11, 8, 9],
173
+ [0, 1, 2, 3, 4],
174
+ [0, 1, 2, 3, 4]]
175
+ assert_image(image, 5, 5, 4, nil, data)
176
+
177
+ data = [[255, 255, 255, 255, 255]] * 3 +
178
+ [[191, 191, 191, 191, 191]] * 2
179
+ assert_image(image[:SMask], 5, 5, 8, :DeviceGray, data)
180
+ end
181
+
182
+ it "works for an indexed 8-bit png with alpha values" do
183
+ png = @images.grep(/indexed-alpha-8bit\.png/).first
184
+ image = @loader.load(@doc, png)
185
+ data = [[5, 10, 11, 7, 6],
186
+ [5, 8, 9, 7, 6],
187
+ [5, 8, 9, 7, 6],
188
+ [0, 1, 4, 2, 3],
189
+ [0, 1, 4, 2, 3]]
190
+ assert_image(image, 5, 5, 8, nil, data)
191
+
192
+ data = [[255, 255, 255, 255, 255]] * 3 +
193
+ [[191, 191, 191, 191, 191]] * 2
194
+ assert_image(image[:SMask], 5, 5, 8, :DeviceGray, data)
195
+ end
196
+
197
+ it "works for a true color 8-bit png" do
198
+ png = @images.grep(/truecolour-8bit\.png/).first
199
+ image = @loader.load(@doc, png)
200
+ data = [[12, 92, 146, 80, 136, 175, 167, 193, 213, 97, 175, 101, 38, 113, 50],
201
+ [12, 92, 146, 81, 137, 176, 168, 194, 214, 97, 175, 101, 38, 113, 49],
202
+ [12, 92, 146, 81, 137, 176, 169, 195, 214, 96, 175, 101, 37, 113, 49],
203
+ [12, 92, 146, 81, 138, 176, 171, 195, 214, 96, 176, 101, 37, 113, 49],
204
+ [12, 92, 146, 82, 137, 177, 171, 196, 215, 97, 176, 102, 37, 113, 49]]
205
+ assert_image(image, 5, 5, 8, :DeviceRGB, data)
206
+ end
207
+
208
+ it "works for a true color 8-bit png with alpha" do
209
+ png = @images.grep(/truecolour-alpha-8bit\.png/).first
210
+ image = @loader.load(@doc, png)
211
+ data = [[12, 92, 146, 80, 136, 175, 167, 193, 213, 97, 175, 101, 38, 113, 50],
212
+ [12, 92, 146, 81, 137, 176, 168, 194, 214, 97, 175, 101, 38, 113, 49],
213
+ [12, 92, 146, 81, 137, 176, 169, 195, 214, 96, 175, 101, 37, 113, 49],
214
+ [12, 92, 146, 81, 137, 176, 169, 195, 214, 96, 175, 101, 37, 113, 49],
215
+ [12, 92, 146, 81, 137, 176, 169, 195, 214, 96, 175, 101, 37, 113, 49]]
216
+ assert_image(image, 5, 5, 8, :DeviceRGB, data)
217
+
218
+ data = [[255, 255, 255, 255, 255]] * 3 +
219
+ [[191, 191, 191, 191, 191]] * 2
220
+ assert_image(image[:SMask], 5, 5, 8, :DeviceGray, data)
221
+ end
222
+
223
+ it "works for true color 8-bit pngs with an sRGB chunk or gAMA/cHRM chunks" do
224
+ @images.grep(/truecolour-(?:srgb|gama-chrm)-8bit\.png/).each do |png|
225
+ image = @loader.load(@doc, png)
226
+ # For the literal numbers see http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
227
+ [0.9505, 1.0, 1.0891].zip(image[:ColorSpace][1][:WhitePoint]).each do |r, e|
228
+ assert_in_delta(r, e, 0.0001)
229
+ end
230
+ [0.4124, 0.2126, 0.0193, 0.3576, 0.7152, 0.1192, 0.1805, 0.0722, 0.9505].
231
+ zip(image[:ColorSpace][1][:Matrix]).each do |r, e|
232
+ assert_in_delta(r, e, 0.0001)
233
+ end
234
+ if png =~ /srgb/
235
+ assert_equal(:AbsoluteColorimetric, image[:Intent])
236
+ end
237
+ end
238
+ end
239
+
240
+ it "fails for unsupported PNG compression methods" do
241
+ data = build_png([0, 0, 8, 0, 1, 0, 0, 4])
242
+ exp = assert_raises(HexaPDF::Error) { @loader.load(@doc, StringIO.new(data)) }
243
+ assert_match(/compression method/, exp.message)
244
+ end
245
+
246
+ it "fails for unsupported PNG filter methods" do
247
+ data = build_png([0, 0, 8, 0, 0, 1, 0, 4])
248
+ exp = assert_raises(HexaPDF::Error) { @loader.load(@doc, StringIO.new(data)) }
249
+ assert_match(/filter method/, exp.message)
250
+ end
251
+
252
+ it "fails for unsupported PNG interlace methods" do
253
+ data = build_png([0, 0, 8, 0, 0, 0, 1, 4])
254
+ exp = assert_raises(HexaPDF::Error) { @loader.load(@doc, StringIO.new(data)) }
255
+ assert_match(/interlace method/, exp.message)
256
+ end
257
+ end
258
+ end
@@ -0,0 +1,46 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'test_helper'
4
+ require 'hexapdf/document'
5
+ require 'hexapdf/task/dereference'
6
+
7
+ describe HexaPDF::Task::Dereference do
8
+ before do
9
+ @doc = HexaPDF::Document.new(io: StringIO.new(MINIMAL_PDF))
10
+ end
11
+
12
+ it "dereferences all references to objects" do
13
+ obj = @doc.add(:test)
14
+ len = @doc.add(5)
15
+ str = @doc.add(@doc.wrap({Length: len}, stream: ''))
16
+ @doc.trailer[:Test] = str
17
+ checker = lambda do |val, done = {}|
18
+ case val
19
+ when Array then val.all? {|v| checker.call(v, done)}
20
+ when Hash then val.all? {|_, v| checker.call(v, done)}
21
+ when HexaPDF::Reference
22
+ false
23
+ when HexaPDF::Object
24
+ if done.key?(val)
25
+ true
26
+ else
27
+ done[val] = true if val.oid != 0
28
+ checker.call(val.value, done)
29
+ end
30
+ else
31
+ true
32
+ end
33
+ end
34
+ refute(checker.call(@doc.trailer))
35
+ assert_equal([obj, len], @doc.task(:dereference))
36
+ assert(checker.call(@doc.trailer))
37
+ assert_equal([obj, len], @doc.task(:dereference))
38
+ assert(checker.call(@doc.trailer))
39
+ end
40
+
41
+ it "dereferences only a single object" do
42
+ assert(@doc.object(5).value[:Font][:F1].kind_of?(HexaPDF::Reference))
43
+ assert_nil(@doc.task(:dereference, object: @doc.object(5)))
44
+ refute(@doc.object(5).value[:Font][:F1].kind_of?(HexaPDF::Reference))
45
+ end
46
+ end
@@ -0,0 +1,137 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'test_helper'
4
+ require 'hexapdf/document'
5
+ require 'hexapdf/task/optimize'
6
+
7
+ describe HexaPDF::Task::Optimize do
8
+ class TestType < HexaPDF::Dictionary
9
+ define_field :Optional, type: Symbol, default: :Optional
10
+ end
11
+
12
+ before do
13
+ @doc = HexaPDF::Document.new
14
+ @obj1 = @doc.add(@doc.wrap({Optional: :Optional}, type: TestType))
15
+ @doc.trailer[:Test] = @doc.wrap(@obj1)
16
+ @doc.revisions.add
17
+ @obj2 = @doc.add(Type: :UsedEntry)
18
+ @obj3 = @doc.add(Unused: @obj2)
19
+ @obj4 = @doc.add(Test: :Test)
20
+ @obj1[:Test] = @doc.wrap(@obj4, type: TestType)
21
+ end
22
+
23
+ def assert_objstms_generated
24
+ assert(@doc.revisions.all? {|rev| rev.any? {|obj| obj.type == :ObjStm}})
25
+ assert(@doc.revisions.all? {|rev| rev.any? {|obj| obj.type == :XRef}})
26
+ end
27
+
28
+ def assert_xrefstms_generated
29
+ assert(@doc.revisions.all? {|rev| rev.any? {|obj| obj.type == :XRef}})
30
+ end
31
+
32
+ def assert_no_objstms
33
+ assert(@doc.each(current: false).all? {|obj| obj.type != :ObjStm})
34
+ end
35
+
36
+ def assert_no_xrefstms
37
+ assert(@doc.each(current: false).all? {|obj| obj.type != :XRef})
38
+ end
39
+
40
+ def assert_default_deleted
41
+ refute(@obj1.value.key?(:Optional))
42
+ end
43
+
44
+ describe "compact" do
45
+ it "compacts the document" do
46
+ @doc.task(:optimize, compact: true)
47
+ assert_equal(1, @doc.revisions.size)
48
+ assert_equal(2, @doc.each(current: false).to_a.size)
49
+ refute_equal(@obj2, @doc.object(@obj2))
50
+ refute_equal(@obj3, @doc.object(@obj3))
51
+ assert_default_deleted
52
+ assert_equal(2, @obj4.oid)
53
+ assert_equal(@obj1[:Test], @obj4)
54
+ end
55
+
56
+ it "compacts and generates object streams" do
57
+ @doc.task(:optimize, compact: true, object_streams: :generate)
58
+ assert_objstms_generated
59
+ assert_default_deleted
60
+ end
61
+
62
+ it "compacts and deletes object streams" do
63
+ @doc.add(Type: :ObjStm)
64
+ @doc.task(:optimize, compact: true, object_streams: :delete)
65
+ assert_no_objstms
66
+ assert_default_deleted
67
+ end
68
+
69
+ it "compacts and generates xref streams" do
70
+ @doc.task(:optimize, compact: true, xref_streams: :generate)
71
+ assert_xrefstms_generated
72
+ assert_default_deleted
73
+ end
74
+
75
+ it "compacts and deletes xref streams" do
76
+ @doc.add({Type: :XRef}, revision: 0)
77
+ @doc.add({Type: :XRef}, revision: 1)
78
+ @doc.task(:optimize, compact: true, xref_streams: :delete)
79
+ assert_no_xrefstms
80
+ assert_default_deleted
81
+ end
82
+ end
83
+
84
+ describe "object_streams" do
85
+ it "generates object streams" do
86
+ objstm = @doc.add(Type: :ObjStm)
87
+ xref = @doc.add(Type: :XRef)
88
+ 210.times { @doc.add(5) }
89
+ @doc.task(:optimize, object_streams: :generate)
90
+ assert_objstms_generated
91
+ assert_default_deleted
92
+ assert_nil(@doc.object(objstm).value)
93
+ assert(3, @doc.revisions.current.find_all {|obj| obj.type == :ObjStm}.size)
94
+ assert([xref], @doc.revisions.current.find_all {|obj| obj.type == :XRef})
95
+ end
96
+
97
+ it "deletes object and xref streams" do
98
+ @doc.add(Type: :ObjStm)
99
+ @doc.task(:optimize, object_streams: :delete, xref_streams: :delete)
100
+ assert_no_objstms
101
+ assert_no_xrefstms
102
+ assert_default_deleted
103
+ end
104
+
105
+ it "deletes object and generates xref streams" do
106
+ @doc.add(Type: :ObjStm)
107
+ @doc.task(:optimize, object_streams: :delete, xref_streams: :generate)
108
+ assert_no_objstms
109
+ assert_xrefstms_generated
110
+ assert_default_deleted
111
+ end
112
+ end
113
+
114
+ describe "xref_streams" do
115
+ it "generates xref streams" do
116
+ @doc.task(:optimize, xref_streams: :generate)
117
+ assert_xrefstms_generated
118
+ assert_default_deleted
119
+ end
120
+
121
+ it "deletes xref streams" do
122
+ @doc.add(Type: :XRef)
123
+ @doc.task(:optimize, xref_streams: :delete)
124
+ assert_no_xrefstms
125
+ assert_default_deleted
126
+ end
127
+ end
128
+
129
+ describe "compress_pages" do
130
+ it "compresses pages streams" do
131
+ page = @doc.pages.add_page
132
+ page.contents = " 10 10 m q Q BI /Name 5 ID dataEI "
133
+ @doc.task(:optimize, compress_pages: true)
134
+ assert_equal("10 10 m\nq\nQ\nBI\n/Name 5 ID\ndataEI\n", page.contents)
135
+ end
136
+ end
137
+ end