hexapdf 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CONTRIBUTERS +3 -0
- data/LICENSE +26 -0
- data/README.md +88 -0
- data/Rakefile +121 -0
- data/VERSION +1 -0
- data/agpl-3.0.txt +661 -0
- data/bin/hexapdf +6 -0
- data/data/hexapdf/afm/Courier-Bold.afm +342 -0
- data/data/hexapdf/afm/Courier-BoldOblique.afm +342 -0
- data/data/hexapdf/afm/Courier-Oblique.afm +342 -0
- data/data/hexapdf/afm/Courier.afm +342 -0
- data/data/hexapdf/afm/Helvetica-Bold.afm +2827 -0
- data/data/hexapdf/afm/Helvetica-BoldOblique.afm +2827 -0
- data/data/hexapdf/afm/Helvetica-Oblique.afm +3051 -0
- data/data/hexapdf/afm/Helvetica.afm +3051 -0
- data/data/hexapdf/afm/MustRead.html +1 -0
- data/data/hexapdf/afm/Symbol.afm +213 -0
- data/data/hexapdf/afm/Times-Bold.afm +2588 -0
- data/data/hexapdf/afm/Times-BoldItalic.afm +2384 -0
- data/data/hexapdf/afm/Times-Italic.afm +2667 -0
- data/data/hexapdf/afm/Times-Roman.afm +2419 -0
- data/data/hexapdf/afm/ZapfDingbats.afm +225 -0
- data/data/hexapdf/encoding/glyphlist.txt +4305 -0
- data/data/hexapdf/encoding/zapfdingbats.txt +225 -0
- data/examples/arc.rb +50 -0
- data/examples/graphics.rb +274 -0
- data/examples/hello_world.rb +16 -0
- data/examples/machupicchu.jpg +0 -0
- data/examples/merging.rb +24 -0
- data/examples/optimizing.rb +20 -0
- data/examples/show_char_bboxes.rb +55 -0
- data/examples/standard_pdf_fonts.rb +72 -0
- data/examples/truetype.rb +45 -0
- data/lib/hexapdf/cli/extract.rb +128 -0
- data/lib/hexapdf/cli/info.rb +121 -0
- data/lib/hexapdf/cli/inspect.rb +157 -0
- data/lib/hexapdf/cli/modify.rb +218 -0
- data/lib/hexapdf/cli.rb +121 -0
- data/lib/hexapdf/configuration.rb +392 -0
- data/lib/hexapdf/content/canvas.rb +1974 -0
- data/lib/hexapdf/content/color_space.rb +364 -0
- data/lib/hexapdf/content/graphic_object/arc.rb +267 -0
- data/lib/hexapdf/content/graphic_object/endpoint_arc.rb +208 -0
- data/lib/hexapdf/content/graphic_object/solid_arc.rb +173 -0
- data/lib/hexapdf/content/graphic_object.rb +81 -0
- data/lib/hexapdf/content/graphics_state.rb +579 -0
- data/lib/hexapdf/content/operator.rb +1072 -0
- data/lib/hexapdf/content/parser.rb +204 -0
- data/lib/hexapdf/content/processor.rb +451 -0
- data/lib/hexapdf/content/transformation_matrix.rb +172 -0
- data/lib/hexapdf/content.rb +47 -0
- data/lib/hexapdf/data_dir.rb +51 -0
- data/lib/hexapdf/dictionary.rb +303 -0
- data/lib/hexapdf/dictionary_fields.rb +382 -0
- data/lib/hexapdf/document.rb +589 -0
- data/lib/hexapdf/document_utils.rb +209 -0
- data/lib/hexapdf/encryption/aes.rb +206 -0
- data/lib/hexapdf/encryption/arc4.rb +93 -0
- data/lib/hexapdf/encryption/fast_aes.rb +79 -0
- data/lib/hexapdf/encryption/fast_arc4.rb +67 -0
- data/lib/hexapdf/encryption/identity.rb +63 -0
- data/lib/hexapdf/encryption/ruby_aes.rb +447 -0
- data/lib/hexapdf/encryption/ruby_arc4.rb +96 -0
- data/lib/hexapdf/encryption/security_handler.rb +494 -0
- data/lib/hexapdf/encryption/standard_security_handler.rb +616 -0
- data/lib/hexapdf/encryption.rb +94 -0
- data/lib/hexapdf/error.rb +73 -0
- data/lib/hexapdf/filter/ascii85_decode.rb +160 -0
- data/lib/hexapdf/filter/ascii_hex_decode.rb +87 -0
- data/lib/hexapdf/filter/dct_decode.rb +57 -0
- data/lib/hexapdf/filter/encryption.rb +59 -0
- data/lib/hexapdf/filter/flate_decode.rb +93 -0
- data/lib/hexapdf/filter/jpx_decode.rb +56 -0
- data/lib/hexapdf/filter/lzw_decode.rb +191 -0
- data/lib/hexapdf/filter/predictor.rb +266 -0
- data/lib/hexapdf/filter/run_length_decode.rb +108 -0
- data/lib/hexapdf/filter.rb +176 -0
- data/lib/hexapdf/font/cmap/parser.rb +146 -0
- data/lib/hexapdf/font/cmap/writer.rb +176 -0
- data/lib/hexapdf/font/cmap.rb +90 -0
- data/lib/hexapdf/font/encoding/base.rb +77 -0
- data/lib/hexapdf/font/encoding/difference_encoding.rb +64 -0
- data/lib/hexapdf/font/encoding/glyph_list.rb +150 -0
- data/lib/hexapdf/font/encoding/mac_expert_encoding.rb +221 -0
- data/lib/hexapdf/font/encoding/mac_roman_encoding.rb +265 -0
- data/lib/hexapdf/font/encoding/standard_encoding.rb +205 -0
- data/lib/hexapdf/font/encoding/symbol_encoding.rb +244 -0
- data/lib/hexapdf/font/encoding/win_ansi_encoding.rb +280 -0
- data/lib/hexapdf/font/encoding/zapf_dingbats_encoding.rb +250 -0
- data/lib/hexapdf/font/encoding.rb +68 -0
- data/lib/hexapdf/font/true_type/font.rb +179 -0
- data/lib/hexapdf/font/true_type/table/cmap.rb +103 -0
- data/lib/hexapdf/font/true_type/table/cmap_subtable.rb +384 -0
- data/lib/hexapdf/font/true_type/table/directory.rb +92 -0
- data/lib/hexapdf/font/true_type/table/glyf.rb +166 -0
- data/lib/hexapdf/font/true_type/table/head.rb +143 -0
- data/lib/hexapdf/font/true_type/table/hhea.rb +109 -0
- data/lib/hexapdf/font/true_type/table/hmtx.rb +79 -0
- data/lib/hexapdf/font/true_type/table/loca.rb +79 -0
- data/lib/hexapdf/font/true_type/table/maxp.rb +112 -0
- data/lib/hexapdf/font/true_type/table/name.rb +218 -0
- data/lib/hexapdf/font/true_type/table/os2.rb +200 -0
- data/lib/hexapdf/font/true_type/table/post.rb +230 -0
- data/lib/hexapdf/font/true_type/table.rb +155 -0
- data/lib/hexapdf/font/true_type.rb +48 -0
- data/lib/hexapdf/font/true_type_wrapper.rb +240 -0
- data/lib/hexapdf/font/type1/afm_parser.rb +230 -0
- data/lib/hexapdf/font/type1/character_metrics.rb +67 -0
- data/lib/hexapdf/font/type1/font.rb +123 -0
- data/lib/hexapdf/font/type1/font_metrics.rb +117 -0
- data/lib/hexapdf/font/type1/pfb_parser.rb +71 -0
- data/lib/hexapdf/font/type1.rb +52 -0
- data/lib/hexapdf/font/type1_wrapper.rb +193 -0
- data/lib/hexapdf/font_loader/from_configuration.rb +70 -0
- data/lib/hexapdf/font_loader/standard14.rb +98 -0
- data/lib/hexapdf/font_loader.rb +85 -0
- data/lib/hexapdf/font_utils.rb +89 -0
- data/lib/hexapdf/image_loader/jpeg.rb +166 -0
- data/lib/hexapdf/image_loader/pdf.rb +89 -0
- data/lib/hexapdf/image_loader/png.rb +410 -0
- data/lib/hexapdf/image_loader.rb +68 -0
- data/lib/hexapdf/importer.rb +139 -0
- data/lib/hexapdf/name_tree_node.rb +78 -0
- data/lib/hexapdf/number_tree_node.rb +67 -0
- data/lib/hexapdf/object.rb +363 -0
- data/lib/hexapdf/parser.rb +349 -0
- data/lib/hexapdf/rectangle.rb +99 -0
- data/lib/hexapdf/reference.rb +98 -0
- data/lib/hexapdf/revision.rb +206 -0
- data/lib/hexapdf/revisions.rb +194 -0
- data/lib/hexapdf/serializer.rb +326 -0
- data/lib/hexapdf/stream.rb +279 -0
- data/lib/hexapdf/task/dereference.rb +109 -0
- data/lib/hexapdf/task/optimize.rb +230 -0
- data/lib/hexapdf/task.rb +68 -0
- data/lib/hexapdf/tokenizer.rb +406 -0
- data/lib/hexapdf/type/catalog.rb +107 -0
- data/lib/hexapdf/type/embedded_file.rb +87 -0
- data/lib/hexapdf/type/file_specification.rb +232 -0
- data/lib/hexapdf/type/font.rb +81 -0
- data/lib/hexapdf/type/font_descriptor.rb +109 -0
- data/lib/hexapdf/type/font_simple.rb +190 -0
- data/lib/hexapdf/type/font_true_type.rb +47 -0
- data/lib/hexapdf/type/font_type1.rb +162 -0
- data/lib/hexapdf/type/form.rb +103 -0
- data/lib/hexapdf/type/graphics_state_parameter.rb +79 -0
- data/lib/hexapdf/type/image.rb +73 -0
- data/lib/hexapdf/type/info.rb +70 -0
- data/lib/hexapdf/type/names.rb +69 -0
- data/lib/hexapdf/type/object_stream.rb +224 -0
- data/lib/hexapdf/type/page.rb +355 -0
- data/lib/hexapdf/type/page_tree_node.rb +269 -0
- data/lib/hexapdf/type/resources.rb +212 -0
- data/lib/hexapdf/type/trailer.rb +128 -0
- data/lib/hexapdf/type/viewer_preferences.rb +73 -0
- data/lib/hexapdf/type/xref_stream.rb +204 -0
- data/lib/hexapdf/type.rb +67 -0
- data/lib/hexapdf/utils/bit_field.rb +87 -0
- data/lib/hexapdf/utils/bit_stream.rb +148 -0
- data/lib/hexapdf/utils/lru_cache.rb +65 -0
- data/lib/hexapdf/utils/math_helpers.rb +55 -0
- data/lib/hexapdf/utils/object_hash.rb +130 -0
- data/lib/hexapdf/utils/pdf_doc_encoding.rb +93 -0
- data/lib/hexapdf/utils/sorted_tree_node.rb +339 -0
- data/lib/hexapdf/version.rb +39 -0
- data/lib/hexapdf/writer.rb +199 -0
- data/lib/hexapdf/xref_section.rb +152 -0
- data/lib/hexapdf.rb +34 -0
- data/man/man1/hexapdf.1 +249 -0
- data/test/data/aes-test-vectors/CBCGFSbox-128-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCGFSbox-128-encrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCGFSbox-192-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCGFSbox-192-encrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCGFSbox-256-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCGFSbox-256-encrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCKeySbox-128-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCKeySbox-128-encrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCKeySbox-192-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCKeySbox-192-encrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCKeySbox-256-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCKeySbox-256-encrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarKey-128-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarKey-128-encrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarKey-192-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarKey-192-encrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarKey-256-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarKey-256-encrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarTxt-128-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarTxt-128-encrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarTxt-192-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarTxt-192-encrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarTxt-256-decrypt.data.gz +0 -0
- data/test/data/aes-test-vectors/CBCVarTxt-256-encrypt.data.gz +0 -0
- data/test/data/fonts/Ubuntu-Title.ttf +0 -0
- data/test/data/images/cmyk.jpg +0 -0
- data/test/data/images/fillbytes.jpg +0 -0
- data/test/data/images/gray.jpg +0 -0
- data/test/data/images/greyscale-1bit.png +0 -0
- data/test/data/images/greyscale-2bit.png +0 -0
- data/test/data/images/greyscale-4bit.png +0 -0
- data/test/data/images/greyscale-8bit.png +0 -0
- data/test/data/images/greyscale-alpha-8bit.png +0 -0
- data/test/data/images/greyscale-trns-8bit.png +0 -0
- data/test/data/images/greyscale-with-gamma1.0.png +0 -0
- data/test/data/images/greyscale-with-gamma1.5.png +0 -0
- data/test/data/images/indexed-1bit.png +0 -0
- data/test/data/images/indexed-2bit.png +0 -0
- data/test/data/images/indexed-4bit.png +0 -0
- data/test/data/images/indexed-8bit.png +0 -0
- data/test/data/images/indexed-alpha-4bit.png +0 -0
- data/test/data/images/indexed-alpha-8bit.png +0 -0
- data/test/data/images/rgb.jpg +0 -0
- data/test/data/images/truecolour-8bit.png +0 -0
- data/test/data/images/truecolour-alpha-8bit.png +0 -0
- data/test/data/images/truecolour-gama-chrm-8bit.png +0 -0
- data/test/data/images/truecolour-srgb-8bit.png +0 -0
- data/test/data/minimal.pdf +44 -0
- data/test/data/standard-security-handler/README +9 -0
- data/test/data/standard-security-handler/bothpwd-aes-128bit-V4.pdf +44 -0
- data/test/data/standard-security-handler/bothpwd-aes-256bit-V5.pdf +0 -0
- data/test/data/standard-security-handler/bothpwd-arc4-128bit-V2.pdf +43 -0
- data/test/data/standard-security-handler/bothpwd-arc4-128bit-V4.pdf +43 -0
- data/test/data/standard-security-handler/bothpwd-arc4-40bit-V1.pdf +0 -0
- data/test/data/standard-security-handler/nopwd-aes-128bit-V4.pdf +43 -0
- data/test/data/standard-security-handler/nopwd-aes-256bit-V5.pdf +0 -0
- data/test/data/standard-security-handler/nopwd-arc4-128bit-V2.pdf +43 -0
- data/test/data/standard-security-handler/nopwd-arc4-128bit-V4.pdf +43 -0
- data/test/data/standard-security-handler/nopwd-arc4-40bit-V1.pdf +43 -0
- data/test/data/standard-security-handler/ownerpwd-aes-128bit-V4.pdf +0 -0
- data/test/data/standard-security-handler/ownerpwd-aes-256bit-V5.pdf +43 -0
- data/test/data/standard-security-handler/ownerpwd-arc4-128bit-V2.pdf +43 -0
- data/test/data/standard-security-handler/ownerpwd-arc4-128bit-V4.pdf +43 -0
- data/test/data/standard-security-handler/ownerpwd-arc4-40bit-V1.pdf +43 -0
- data/test/data/standard-security-handler/userpwd-aes-128bit-V4.pdf +43 -0
- data/test/data/standard-security-handler/userpwd-aes-256bit-V5.pdf +43 -0
- data/test/data/standard-security-handler/userpwd-arc4-128bit-V2.pdf +0 -0
- data/test/data/standard-security-handler/userpwd-arc4-128bit-V4.pdf +0 -0
- data/test/data/standard-security-handler/userpwd-arc4-40bit-V1.pdf +43 -0
- data/test/hexapdf/common_tokenizer_tests.rb +204 -0
- data/test/hexapdf/content/common.rb +31 -0
- data/test/hexapdf/content/graphic_object/test_arc.rb +93 -0
- data/test/hexapdf/content/graphic_object/test_endpoint_arc.rb +91 -0
- data/test/hexapdf/content/graphic_object/test_solid_arc.rb +86 -0
- data/test/hexapdf/content/test_canvas.rb +1113 -0
- data/test/hexapdf/content/test_color_space.rb +97 -0
- data/test/hexapdf/content/test_graphics_state.rb +138 -0
- data/test/hexapdf/content/test_operator.rb +619 -0
- data/test/hexapdf/content/test_parser.rb +66 -0
- data/test/hexapdf/content/test_processor.rb +156 -0
- data/test/hexapdf/content/test_transformation_matrix.rb +64 -0
- data/test/hexapdf/encryption/common.rb +87 -0
- data/test/hexapdf/encryption/test_aes.rb +121 -0
- data/test/hexapdf/encryption/test_arc4.rb +39 -0
- data/test/hexapdf/encryption/test_fast_aes.rb +17 -0
- data/test/hexapdf/encryption/test_fast_arc4.rb +12 -0
- data/test/hexapdf/encryption/test_identity.rb +21 -0
- data/test/hexapdf/encryption/test_ruby_aes.rb +23 -0
- data/test/hexapdf/encryption/test_ruby_arc4.rb +20 -0
- data/test/hexapdf/encryption/test_security_handler.rb +356 -0
- data/test/hexapdf/encryption/test_standard_security_handler.rb +274 -0
- data/test/hexapdf/filter/common.rb +53 -0
- data/test/hexapdf/filter/test_ascii85_decode.rb +60 -0
- data/test/hexapdf/filter/test_ascii_hex_decode.rb +33 -0
- data/test/hexapdf/filter/test_encryption.rb +24 -0
- data/test/hexapdf/filter/test_flate_decode.rb +35 -0
- data/test/hexapdf/filter/test_lzw_decode.rb +52 -0
- data/test/hexapdf/filter/test_predictor.rb +183 -0
- data/test/hexapdf/filter/test_run_length_decode.rb +32 -0
- data/test/hexapdf/font/cmap/test_parser.rb +67 -0
- data/test/hexapdf/font/cmap/test_writer.rb +58 -0
- data/test/hexapdf/font/encoding/test_base.rb +35 -0
- data/test/hexapdf/font/encoding/test_difference_encoding.rb +21 -0
- data/test/hexapdf/font/encoding/test_glyph_list.rb +59 -0
- data/test/hexapdf/font/encoding/test_zapf_dingbats_encoding.rb +16 -0
- data/test/hexapdf/font/test_encoding.rb +27 -0
- data/test/hexapdf/font/test_true_type_wrapper.rb +110 -0
- data/test/hexapdf/font/test_type1_wrapper.rb +66 -0
- data/test/hexapdf/font/true_type/common.rb +19 -0
- data/test/hexapdf/font/true_type/table/test_cmap.rb +59 -0
- data/test/hexapdf/font/true_type/table/test_cmap_subtable.rb +133 -0
- data/test/hexapdf/font/true_type/table/test_directory.rb +35 -0
- data/test/hexapdf/font/true_type/table/test_glyf.rb +58 -0
- data/test/hexapdf/font/true_type/table/test_head.rb +76 -0
- data/test/hexapdf/font/true_type/table/test_hhea.rb +40 -0
- data/test/hexapdf/font/true_type/table/test_hmtx.rb +38 -0
- data/test/hexapdf/font/true_type/table/test_loca.rb +43 -0
- data/test/hexapdf/font/true_type/table/test_maxp.rb +62 -0
- data/test/hexapdf/font/true_type/table/test_name.rb +95 -0
- data/test/hexapdf/font/true_type/table/test_os2.rb +65 -0
- data/test/hexapdf/font/true_type/table/test_post.rb +89 -0
- data/test/hexapdf/font/true_type/test_font.rb +120 -0
- data/test/hexapdf/font/true_type/test_table.rb +41 -0
- data/test/hexapdf/font/type1/test_afm_parser.rb +51 -0
- data/test/hexapdf/font/type1/test_font.rb +68 -0
- data/test/hexapdf/font/type1/test_pfb_parser.rb +37 -0
- data/test/hexapdf/font_loader/test_from_configuration.rb +28 -0
- data/test/hexapdf/font_loader/test_standard14.rb +22 -0
- data/test/hexapdf/image_loader/test_jpeg.rb +83 -0
- data/test/hexapdf/image_loader/test_pdf.rb +47 -0
- data/test/hexapdf/image_loader/test_png.rb +258 -0
- data/test/hexapdf/task/test_dereference.rb +46 -0
- data/test/hexapdf/task/test_optimize.rb +137 -0
- data/test/hexapdf/test_configuration.rb +82 -0
- data/test/hexapdf/test_data_dir.rb +32 -0
- data/test/hexapdf/test_dictionary.rb +284 -0
- data/test/hexapdf/test_dictionary_fields.rb +185 -0
- data/test/hexapdf/test_document.rb +574 -0
- data/test/hexapdf/test_document_utils.rb +144 -0
- data/test/hexapdf/test_filter.rb +96 -0
- data/test/hexapdf/test_font_utils.rb +47 -0
- data/test/hexapdf/test_importer.rb +78 -0
- data/test/hexapdf/test_object.rb +177 -0
- data/test/hexapdf/test_parser.rb +394 -0
- data/test/hexapdf/test_rectangle.rb +36 -0
- data/test/hexapdf/test_reference.rb +41 -0
- data/test/hexapdf/test_revision.rb +139 -0
- data/test/hexapdf/test_revisions.rb +93 -0
- data/test/hexapdf/test_serializer.rb +169 -0
- data/test/hexapdf/test_stream.rb +262 -0
- data/test/hexapdf/test_tokenizer.rb +30 -0
- data/test/hexapdf/test_writer.rb +120 -0
- data/test/hexapdf/test_xref_section.rb +35 -0
- data/test/hexapdf/type/test_catalog.rb +30 -0
- data/test/hexapdf/type/test_embedded_file.rb +16 -0
- data/test/hexapdf/type/test_file_specification.rb +148 -0
- data/test/hexapdf/type/test_font.rb +35 -0
- data/test/hexapdf/type/test_font_descriptor.rb +51 -0
- data/test/hexapdf/type/test_font_simple.rb +190 -0
- data/test/hexapdf/type/test_font_type1.rb +128 -0
- data/test/hexapdf/type/test_form.rb +60 -0
- data/test/hexapdf/type/test_info.rb +14 -0
- data/test/hexapdf/type/test_names.rb +9 -0
- data/test/hexapdf/type/test_object_stream.rb +84 -0
- data/test/hexapdf/type/test_page.rb +260 -0
- data/test/hexapdf/type/test_page_tree_node.rb +255 -0
- data/test/hexapdf/type/test_resources.rb +167 -0
- data/test/hexapdf/type/test_trailer.rb +109 -0
- data/test/hexapdf/type/test_xref_stream.rb +131 -0
- data/test/hexapdf/utils/test_bit_field.rb +47 -0
- data/test/hexapdf/utils/test_lru_cache.rb +22 -0
- data/test/hexapdf/utils/test_object_hash.rb +115 -0
- data/test/hexapdf/utils/test_pdf_doc_encoding.rb +18 -0
- data/test/hexapdf/utils/test_sorted_tree_node.rb +232 -0
- data/test/test_helper.rb +56 -0
- metadata +427 -0
@@ -0,0 +1,326 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
#
|
3
|
+
#--
|
4
|
+
# This file is part of HexaPDF.
|
5
|
+
#
|
6
|
+
# HexaPDF - A Versatile PDF Creation and Manipulation Library For Ruby
|
7
|
+
# Copyright (C) 2016 Thomas Leitner
|
8
|
+
#
|
9
|
+
# HexaPDF is free software: you can redistribute it and/or modify it
|
10
|
+
# under the terms of the GNU Affero General Public License version 3 as
|
11
|
+
# published by the Free Software Foundation with the addition of the
|
12
|
+
# following permission added to Section 15 as permitted in Section 7(a):
|
13
|
+
# FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
|
14
|
+
# THOMAS LEITNER, THOMAS LEITNER DISCLAIMS THE WARRANTY OF NON
|
15
|
+
# INFRINGEMENT OF THIRD PARTY RIGHTS.
|
16
|
+
#
|
17
|
+
# HexaPDF is distributed in the hope that it will be useful, but WITHOUT
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
20
|
+
# License for more details.
|
21
|
+
#
|
22
|
+
# You should have received a copy of the GNU Affero General Public License
|
23
|
+
# along with HexaPDF. If not, see <http://www.gnu.org/licenses/>.
|
24
|
+
#
|
25
|
+
# The interactive user interfaces in modified source and object code
|
26
|
+
# versions of HexaPDF must display Appropriate Legal Notices, as required
|
27
|
+
# under Section 5 of the GNU Affero General Public License version 3.
|
28
|
+
#
|
29
|
+
# In accordance with Section 7(b) of the GNU Affero General Public
|
30
|
+
# License, a covered work must retain the producer line in every PDF that
|
31
|
+
# is created or manipulated using HexaPDF.
|
32
|
+
#++
|
33
|
+
|
34
|
+
require 'time'
|
35
|
+
require 'hexapdf/tokenizer'
|
36
|
+
require 'hexapdf/filter'
|
37
|
+
require 'hexapdf/utils/lru_cache'
|
38
|
+
|
39
|
+
module HexaPDF
|
40
|
+
|
41
|
+
# Knows how to serialize Ruby objects for a PDF file.
|
42
|
+
#
|
43
|
+
# For normal serialization purposes, the #serialize or #serialize_to_io methods should be used.
|
44
|
+
# However, if the type of the object to be serialized is known, a specialized serialization
|
45
|
+
# method like #serialize_float can be used.
|
46
|
+
#
|
47
|
+
# Additionally, an object for encrypting strings and streams while serializing can be set via the
|
48
|
+
# #encrypter= method. The assigned object has to respond to #encrypt_string(str, ind_obj) (where
|
49
|
+
# the string is part of the indirect object; returns the encrypted string) and
|
50
|
+
# #encrypt_stream(stream) (returns a fiber that represents the encrypted stream).
|
51
|
+
#
|
52
|
+
#
|
53
|
+
# == How This Class Works
|
54
|
+
#
|
55
|
+
# The main public interface consists of the #serialize and #serialize_to_io methods which accept
|
56
|
+
# an object and return its serialized form. During serialization of this object it is accessible
|
57
|
+
# by individual serialization methods via the @object instance variable (useful if the object is a
|
58
|
+
# composed object).
|
59
|
+
#
|
60
|
+
# Internally, the #__serialize method is used for invoking the correct serialization method
|
61
|
+
# based on the class of a given object. It is also used for serializing individual parts of a
|
62
|
+
# composed object.
|
63
|
+
#
|
64
|
+
# Therefore the serializer contains one serialization method for each class it needs to
|
65
|
+
# serialize. The naming scheme of these methods is based on the class name: The full class name
|
66
|
+
# is converted to lowercase, the namespace separator '::' is replaced with a single underscore
|
67
|
+
# and the string "serialize_" is then prepended.
|
68
|
+
#
|
69
|
+
# Examples:
|
70
|
+
#
|
71
|
+
# NilClass => serialize_nilclass
|
72
|
+
# TrueClass => serialize_trueclass
|
73
|
+
# HexaPDF::Object => serialize_hexapdf_object
|
74
|
+
#
|
75
|
+
# If no serialization method for a specific class is found, the ancestors classes are tried.
|
76
|
+
#
|
77
|
+
# See: PDF1.7 s7.3
|
78
|
+
class Serializer
|
79
|
+
|
80
|
+
# The encrypter to use for encrypting strings and streams. If +nil+, strings and streams are not
|
81
|
+
# encrypted.
|
82
|
+
#
|
83
|
+
# Default: +nil+
|
84
|
+
attr_accessor :encrypter
|
85
|
+
|
86
|
+
# Creates a new Serializer object.
|
87
|
+
def initialize
|
88
|
+
@dispatcher = Hash.new do |h, klass|
|
89
|
+
method = nil
|
90
|
+
klass.ancestors.each do |ancestor_klass|
|
91
|
+
method = "serialize_#{ancestor_klass.name.downcase.gsub(/::/, '_')}"
|
92
|
+
(h[klass] = method; break) if respond_to?(method, true)
|
93
|
+
end
|
94
|
+
method
|
95
|
+
end
|
96
|
+
@encrypter = false
|
97
|
+
@io = nil
|
98
|
+
@object = nil
|
99
|
+
end
|
100
|
+
|
101
|
+
# Returns the serialized form of the given object.
|
102
|
+
#
|
103
|
+
# For developers: While the object is serialized, methods can use the instance variable
|
104
|
+
# @object to obtain information about or use the object in case it is a composed object.
|
105
|
+
def serialize(obj)
|
106
|
+
@object = obj
|
107
|
+
__serialize(obj)
|
108
|
+
ensure
|
109
|
+
@object = nil
|
110
|
+
end
|
111
|
+
|
112
|
+
# Serializes the given object and writes it to the IO.
|
113
|
+
#
|
114
|
+
# Also see: #serialize
|
115
|
+
def serialize_to_io(obj, io)
|
116
|
+
@io = io
|
117
|
+
@io << serialize(obj).freeze
|
118
|
+
ensure
|
119
|
+
@io = nil
|
120
|
+
end
|
121
|
+
|
122
|
+
# Serializes the +nil+ value.
|
123
|
+
#
|
124
|
+
# See: PDF1.7 s7.3.9
|
125
|
+
def serialize_nilclass(_obj)
|
126
|
+
"null"
|
127
|
+
end
|
128
|
+
|
129
|
+
# Serializes the +true+ value.
|
130
|
+
#
|
131
|
+
# See: PDF1.7 s7.3.2
|
132
|
+
def serialize_trueclass(_obj)
|
133
|
+
"true"
|
134
|
+
end
|
135
|
+
|
136
|
+
# Serializes the +false+ value.
|
137
|
+
#
|
138
|
+
# See: PDF1.7 s7.3.2
|
139
|
+
def serialize_falseclass(_obj)
|
140
|
+
"false"
|
141
|
+
end
|
142
|
+
|
143
|
+
# Serializes a Numeric object (either Integer or Float).
|
144
|
+
#
|
145
|
+
# This method should be used for cases where it is known that the object is either an Integer
|
146
|
+
# or a Float.
|
147
|
+
#
|
148
|
+
# See: PDF1.7 s7.3.3
|
149
|
+
def serialize_numeric(obj)
|
150
|
+
obj.kind_of?(Integer) ? obj.to_s : serialize_float(obj)
|
151
|
+
end
|
152
|
+
|
153
|
+
# Serializes an Integer object.
|
154
|
+
#
|
155
|
+
# See: PDF1.7 s7.3.3
|
156
|
+
def serialize_integer(obj)
|
157
|
+
obj.to_s
|
158
|
+
end
|
159
|
+
|
160
|
+
# Serializes a Float object.
|
161
|
+
#
|
162
|
+
# See: PDF1.7 s7.3.3
|
163
|
+
def serialize_float(obj)
|
164
|
+
obj.abs < 0.0001 && obj != 0 ? sprintf("%.6f".freeze, obj) : obj.round(6).to_s
|
165
|
+
end
|
166
|
+
|
167
|
+
# The regexp matches all characters that need to be escaped and the substs hash contains the
|
168
|
+
# mapping from these characters to their escaped form.
|
169
|
+
#
|
170
|
+
# See PDF1.7 s7.3.5
|
171
|
+
NAME_SUBSTS = {} # :nodoc:
|
172
|
+
[0..32, 127..255, Tokenizer::DELIMITER.bytes, Tokenizer::WHITESPACE.bytes, [35]].each do |a|
|
173
|
+
a.each {|c| NAME_SUBSTS[c.chr] = "##{c.to_s(16).rjust(2, "0")}"}
|
174
|
+
end
|
175
|
+
NAME_REGEXP = /[^!-~&&[^##{Regexp.escape(Tokenizer::DELIMITER)}#{Regexp.escape(Tokenizer::WHITESPACE)}]]/ # :nodoc:
|
176
|
+
NAME_CACHE = Utils::LRUCache.new(1000) # :nodoc:
|
177
|
+
|
178
|
+
# Serializes a Symbol object (i.e. a PDF name object).
|
179
|
+
#
|
180
|
+
# See: PDF1.7 s7.3.5
|
181
|
+
def serialize_symbol(obj)
|
182
|
+
NAME_CACHE[obj] ||=
|
183
|
+
begin
|
184
|
+
str = obj.to_s.force_encoding(Encoding::BINARY)
|
185
|
+
str.gsub!(NAME_REGEXP) {|m| NAME_SUBSTS[m]}
|
186
|
+
"/#{str}"
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
BYTE_IS_DELIMITER = {40 => true, 47 => true, 60 => true, 91 => true, # :nodoc:
|
191
|
+
41 => true, 62 => true, 93 => true}
|
192
|
+
|
193
|
+
# Serializes an Array object.
|
194
|
+
#
|
195
|
+
# See: PDF1.7 s7.3.6
|
196
|
+
def serialize_array(obj)
|
197
|
+
str = "["
|
198
|
+
index = 0
|
199
|
+
while index < obj.size
|
200
|
+
tmp = __serialize(obj[index])
|
201
|
+
str << " ".freeze unless BYTE_IS_DELIMITER[tmp.getbyte(0)] ||
|
202
|
+
BYTE_IS_DELIMITER[str.getbyte(-1)]
|
203
|
+
str << tmp
|
204
|
+
index += 1
|
205
|
+
end
|
206
|
+
str << "]".freeze
|
207
|
+
end
|
208
|
+
|
209
|
+
# Serializes a Hash object (i.e. a PDF dictionary object).
|
210
|
+
#
|
211
|
+
# See: PDF1.7 s7.3.7
|
212
|
+
def serialize_hash(obj)
|
213
|
+
str = "<<"
|
214
|
+
obj.each do |k, v|
|
215
|
+
next if v.nil? || (v.respond_to?(:null?) && v.null?)
|
216
|
+
str << __serialize(k)
|
217
|
+
tmp = __serialize(v)
|
218
|
+
str << " ".freeze unless BYTE_IS_DELIMITER[tmp.getbyte(0)] ||
|
219
|
+
BYTE_IS_DELIMITER[str.getbyte(-1)]
|
220
|
+
str << tmp
|
221
|
+
end
|
222
|
+
str << ">>".freeze
|
223
|
+
end
|
224
|
+
|
225
|
+
STRING_ESCAPE_MAP = {"(" => "\\(", ")" => "\\)", "\\" => "\\\\", "\r" => "\\r"} # :nodoc:
|
226
|
+
|
227
|
+
# Serializes a String object.
|
228
|
+
#
|
229
|
+
# See: PDF1.7 s7.3.4
|
230
|
+
def serialize_string(obj)
|
231
|
+
if @encrypter && @object.kind_of?(HexaPDF::Object) && @object.indirect?
|
232
|
+
obj = encrypter.encrypt_string(obj, @object)
|
233
|
+
elsif obj.encoding != Encoding::BINARY && obj =~ /[^ -~\t\r\n]/
|
234
|
+
obj = "\xFE\xFF".b << obj.encode(Encoding::UTF_16BE).force_encoding(Encoding::BINARY)
|
235
|
+
elsif obj.encoding != Encoding::BINARY
|
236
|
+
obj = obj.b
|
237
|
+
end
|
238
|
+
"(" << obj.gsub(/[\(\)\\\r]/n) {|m| STRING_ESCAPE_MAP[m]} << ")".freeze
|
239
|
+
end
|
240
|
+
|
241
|
+
# The ISO PDF specification differs in respect to the supported date format. When converting
|
242
|
+
# to a date string, a format suitable for both is output.
|
243
|
+
#
|
244
|
+
# See: PDF1.7 s7.9.4, ADB1.7 3.8.3
|
245
|
+
def serialize_time(obj)
|
246
|
+
zone = obj.strftime("%z'")
|
247
|
+
if zone == "+0000'"
|
248
|
+
zone = ''
|
249
|
+
else
|
250
|
+
zone[3, 0] = "'"
|
251
|
+
end
|
252
|
+
serialize_string(obj.strftime("D:%Y%m%d%H%M%S#{zone}"))
|
253
|
+
end
|
254
|
+
|
255
|
+
# See: #serialize_time
|
256
|
+
def serialize_date(obj)
|
257
|
+
serialize_time(obj.to_time)
|
258
|
+
end
|
259
|
+
|
260
|
+
# See: #serialize_time
|
261
|
+
def serialize_datetime(obj)
|
262
|
+
serialize_time(obj.to_time)
|
263
|
+
end
|
264
|
+
|
265
|
+
private
|
266
|
+
|
267
|
+
# Uses #serialize_hexapdf_reference if it is an indirect object, otherwise just serializes
|
268
|
+
# the objects value.
|
269
|
+
def serialize_hexapdf_object(obj)
|
270
|
+
if obj.indirect? && obj != @object
|
271
|
+
serialize_hexapdf_reference(obj)
|
272
|
+
else
|
273
|
+
__serialize(obj.value)
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
# See: PDF1.7 s7.3.10
|
278
|
+
def serialize_hexapdf_reference(obj)
|
279
|
+
"#{obj.oid} #{obj.gen} R".freeze
|
280
|
+
end
|
281
|
+
|
282
|
+
# Serializes the streams dictionary and its stream.
|
283
|
+
#
|
284
|
+
# See: PDF1.7 s7.3.8
|
285
|
+
def serialize_hexapdf_stream(obj)
|
286
|
+
if !obj.indirect?
|
287
|
+
raise HexaPDF::Error, "Can't serialize PDF stream without object identifier"
|
288
|
+
elsif obj != @object
|
289
|
+
return serialize_hexapdf_reference(obj)
|
290
|
+
end
|
291
|
+
|
292
|
+
fiber = if @encrypter
|
293
|
+
encrypter.encrypt_stream(obj)
|
294
|
+
else
|
295
|
+
obj.stream_encoder
|
296
|
+
end
|
297
|
+
|
298
|
+
if @io && fiber.respond_to?(:length) && fiber.length >= 0
|
299
|
+
obj.value[:Length] = fiber.length
|
300
|
+
@io << __serialize(obj.value)
|
301
|
+
@io << "stream\n".freeze
|
302
|
+
while fiber.alive? && (data = fiber.resume)
|
303
|
+
@io << data.freeze
|
304
|
+
end
|
305
|
+
@io << "\nendstream".freeze
|
306
|
+
|
307
|
+
nil
|
308
|
+
else
|
309
|
+
data = Filter.string_from_source(fiber)
|
310
|
+
obj.value[:Length] = data.size
|
311
|
+
|
312
|
+
str = __serialize(obj.value)
|
313
|
+
str << "stream\n".freeze
|
314
|
+
str << data
|
315
|
+
str << "\nendstream".freeze
|
316
|
+
end
|
317
|
+
end
|
318
|
+
|
319
|
+
# Invokes the correct serialization method for the object.
|
320
|
+
def __serialize(obj)
|
321
|
+
send(@dispatcher[obj.class], obj)
|
322
|
+
end
|
323
|
+
|
324
|
+
end
|
325
|
+
|
326
|
+
end
|
@@ -0,0 +1,279 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
#
|
3
|
+
#--
|
4
|
+
# This file is part of HexaPDF.
|
5
|
+
#
|
6
|
+
# HexaPDF - A Versatile PDF Creation and Manipulation Library For Ruby
|
7
|
+
# Copyright (C) 2016 Thomas Leitner
|
8
|
+
#
|
9
|
+
# HexaPDF is free software: you can redistribute it and/or modify it
|
10
|
+
# under the terms of the GNU Affero General Public License version 3 as
|
11
|
+
# published by the Free Software Foundation with the addition of the
|
12
|
+
# following permission added to Section 15 as permitted in Section 7(a):
|
13
|
+
# FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
|
14
|
+
# THOMAS LEITNER, THOMAS LEITNER DISCLAIMS THE WARRANTY OF NON
|
15
|
+
# INFRINGEMENT OF THIRD PARTY RIGHTS.
|
16
|
+
#
|
17
|
+
# HexaPDF is distributed in the hope that it will be useful, but WITHOUT
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
20
|
+
# License for more details.
|
21
|
+
#
|
22
|
+
# You should have received a copy of the GNU Affero General Public License
|
23
|
+
# along with HexaPDF. If not, see <http://www.gnu.org/licenses/>.
|
24
|
+
#
|
25
|
+
# The interactive user interfaces in modified source and object code
|
26
|
+
# versions of HexaPDF must display Appropriate Legal Notices, as required
|
27
|
+
# under Section 5 of the GNU Affero General Public License version 3.
|
28
|
+
#
|
29
|
+
# In accordance with Section 7(b) of the GNU Affero General Public
|
30
|
+
# License, a covered work must retain the producer line in every PDF that
|
31
|
+
# is created or manipulated using HexaPDF.
|
32
|
+
#++
|
33
|
+
|
34
|
+
require 'hexapdf/error'
|
35
|
+
require 'hexapdf/dictionary'
|
36
|
+
require 'hexapdf/filter'
|
37
|
+
|
38
|
+
module HexaPDF
|
39
|
+
|
40
|
+
# Container for stream data that is more complex than a string.
|
41
|
+
#
|
42
|
+
# This helper class wraps all information necessary to read stream data by using a Fiber object
|
43
|
+
# (see HexaPDF::Filter). The underlying data either comes from an IO object, a file represented by
|
44
|
+
# its file name or a Fiber defined via a Proc object.
|
45
|
+
#
|
46
|
+
# Additionally, the #filter and #decode_parms can be set to indicate that the data returned from
|
47
|
+
# the Fiber needs to be post-processed. The +filter+ and +decode_parms+ are automatically
|
48
|
+
# normalized to arrays on assignment to ease further processing.
|
49
|
+
class StreamData
|
50
|
+
|
51
|
+
# The filter(s) that need to be applied for getting the decoded stream data.
|
52
|
+
attr_reader :filter
|
53
|
+
|
54
|
+
# The decoding parameters associated with the +filter+(s).
|
55
|
+
attr_reader :decode_parms
|
56
|
+
|
57
|
+
# :call-seq:
|
58
|
+
# StreamData.new(io) -> stream_data
|
59
|
+
# StreamData.new(str) -> stream_data
|
60
|
+
# StreamData.new(proc) -> stream_data
|
61
|
+
# StreamData.new { block } -> stream_data
|
62
|
+
#
|
63
|
+
# Creates a new StreamData object for the given +source+ and with the given options.
|
64
|
+
#
|
65
|
+
# The +source+ can be:
|
66
|
+
#
|
67
|
+
# * An IO stream which is read starting from a specific +offset+ for a specific +length+
|
68
|
+
#
|
69
|
+
# * A string which is interpreted as a file name and read starting from a specific +offset+
|
70
|
+
# * and for a specific +length+
|
71
|
+
#
|
72
|
+
# * A Proc object (that is converted to a Fiber when needed) in which case the +offset+ and
|
73
|
+
# value is ignored. The Proc object can also be passed by using a block.
|
74
|
+
def initialize(source = nil, offset: nil, length: nil, filter: nil, decode_parms: nil, &block)
|
75
|
+
if source.nil? && !block_given?
|
76
|
+
raise ArgumentError, "Either a source object or a block must be given"
|
77
|
+
end
|
78
|
+
@source = source || block
|
79
|
+
@offset = offset
|
80
|
+
@length = length
|
81
|
+
@filter = [filter].flatten.compact
|
82
|
+
@decode_parms = [decode_parms].flatten
|
83
|
+
freeze
|
84
|
+
end
|
85
|
+
|
86
|
+
# Returns a Fiber for getting at the data of the stream represented by this object.
|
87
|
+
def fiber(chunk_size = 0)
|
88
|
+
if @source.kind_of?(Proc)
|
89
|
+
FiberWithLength.new(@length, &@source)
|
90
|
+
elsif @source.kind_of?(String)
|
91
|
+
HexaPDF::Filter.source_from_file(@source, pos: @offset || 0, length: @length || -1,
|
92
|
+
chunk_size: chunk_size)
|
93
|
+
else
|
94
|
+
HexaPDF::Filter.source_from_io(@source, pos: @offset || 0, length: @length || -1,
|
95
|
+
chunk_size: chunk_size)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
# Implements Stream objects of the PDF object system.
|
102
|
+
#
|
103
|
+
# == Stream Objects
|
104
|
+
#
|
105
|
+
# A stream may also be associated with a PDF object but only if the value is a PDF dictionary.
|
106
|
+
# This associated dictionary further describes the stream, like its length or how it is encoded.
|
107
|
+
#
|
108
|
+
# Such a stream object in PDF contains string data but of possibly unlimited length. Therefore
|
109
|
+
# it is used for large amounts of data like images, page descriptions or embedded files.
|
110
|
+
#
|
111
|
+
# The basic Object class cannot hold stream data, only this subclass contains the necessary
|
112
|
+
# methods to conveniently work with the stream data!
|
113
|
+
#
|
114
|
+
# See: PDF1.7 s7.3.8, Dictionary
|
115
|
+
class Stream < Dictionary
|
116
|
+
|
117
|
+
define_field :Length, type: Integer # not required, will be auto-filled when writing
|
118
|
+
define_field :Filter, type: [Symbol, Array]
|
119
|
+
define_field :DecodeParms, type: [Dictionary, Hash, Array]
|
120
|
+
define_field :F, type: :FileSpec, version: '1.2'
|
121
|
+
define_field :FFilter, type: [Symbol, Array], version: '1.2'
|
122
|
+
define_field :FDecodeParms, type: [Dictionary, Hash, Array], version: '1.2'
|
123
|
+
define_field :DL, type: Integer
|
124
|
+
|
125
|
+
# Stream objects must always be indirect.
|
126
|
+
def must_be_indirect?
|
127
|
+
true
|
128
|
+
end
|
129
|
+
|
130
|
+
# Assigns a new stream data object.
|
131
|
+
#
|
132
|
+
# The +stream+ argument can be a HexaPDF::StreamData object, a String object or +nil+.
|
133
|
+
#
|
134
|
+
# If +stream+ is +nil+, an empty binary string is used instead.
|
135
|
+
def stream=(stream)
|
136
|
+
data.stream = stream
|
137
|
+
after_data_change
|
138
|
+
end
|
139
|
+
|
140
|
+
# Returns the (possibly decoded) stream data as string.
|
141
|
+
#
|
142
|
+
# Note that modifications done to the returned string are not reflected in the Stream object
|
143
|
+
# itself. The modified string must explicitly be assigned via #stream= to take effect.
|
144
|
+
def stream
|
145
|
+
if data.stream.kind_of?(String)
|
146
|
+
data.stream.dup
|
147
|
+
else
|
148
|
+
HexaPDF::Filter.string_from_source(stream_decoder)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
# Returns the raw stream object.
|
153
|
+
#
|
154
|
+
# The returned value can be of many different types (see #stream=). For working with the
|
155
|
+
# decoded stream contents use #stream.
|
156
|
+
def raw_stream
|
157
|
+
data.stream
|
158
|
+
end
|
159
|
+
|
160
|
+
# Returns the Fiber representing the unprocessed content of the stream.
|
161
|
+
def stream_source
|
162
|
+
if data.stream.kind_of?(String)
|
163
|
+
HexaPDF::Filter.source_from_string(data.stream)
|
164
|
+
else
|
165
|
+
data.stream.fiber(config['io.chunk_size'.freeze])
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
# Returns the decoder Fiber for the stream data.
|
170
|
+
#
|
171
|
+
# See the Filter module for more information on how to work with the fiber.
|
172
|
+
def stream_decoder
|
173
|
+
source = stream_source
|
174
|
+
|
175
|
+
if data.stream.kind_of?(StreamData)
|
176
|
+
data.stream.filter.zip(data.stream.decode_parms) do |filter, decode_parms|
|
177
|
+
source = filter_for_name(filter).decoder(source, decode_parms)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
source
|
182
|
+
end
|
183
|
+
|
184
|
+
# Returns the encoder Fiber for the stream data.
|
185
|
+
#
|
186
|
+
# The two arguments can be used to add additional filters for *only* this returned encoder
|
187
|
+
# Fiber. They should normally *not* be used and are here for use by the encryption facilities.
|
188
|
+
#
|
189
|
+
# See the Filter module for more information on how to work with the fiber.
|
190
|
+
def stream_encoder(additional_filter = nil, additional_decode_parms = nil)
|
191
|
+
encoder_data = [additional_filter, document.unwrap(self[:Filter])].flatten.
|
192
|
+
zip([additional_decode_parms, document.unwrap(self[:DecodeParms])].flatten).
|
193
|
+
delete_if {|f, _| f.nil?}
|
194
|
+
source = stream_source
|
195
|
+
|
196
|
+
if data.stream.kind_of?(StreamData)
|
197
|
+
decoder_data = data.stream.filter.zip(data.stream.decode_parms)
|
198
|
+
|
199
|
+
while !decoder_data.empty? && !encoder_data.empty? && decoder_data.last == encoder_data.last
|
200
|
+
decoder_data.pop
|
201
|
+
encoder_data.pop
|
202
|
+
end
|
203
|
+
|
204
|
+
decoder_data.each do |filter, decode_parms|
|
205
|
+
source = filter_for_name(filter).decoder(source, decode_parms)
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
encoder_data.reverse!.each do |filter, decode_parms|
|
210
|
+
source = filter_for_name(filter).encoder(source, decode_parms)
|
211
|
+
end
|
212
|
+
|
213
|
+
source
|
214
|
+
end
|
215
|
+
|
216
|
+
# Sets the filters that should be used for encoding the stream.
|
217
|
+
#
|
218
|
+
# The arguments +filter+ as well as +decode_parms+ can either be a single items or arrays.
|
219
|
+
#
|
220
|
+
# The filters have to be specified in the *decoding order*! For example, if the filters would
|
221
|
+
# be [:A85, :Fl], the stream would first be encoded with the Flate and then with the ASCII85
|
222
|
+
# filter.
|
223
|
+
def set_filter(filter, decode_parms = nil)
|
224
|
+
if filter.nil? || (filter.kind_of?(Array) && filter.empty?)
|
225
|
+
delete(:Filter)
|
226
|
+
else
|
227
|
+
self[:Filter] = filter
|
228
|
+
end
|
229
|
+
if decode_parms.nil? || (decode_parms.kind_of?(Array) && decode_parms.empty?) ||
|
230
|
+
!key?(:Filter)
|
231
|
+
delete(:DecodeParms)
|
232
|
+
else
|
233
|
+
self[:DecodeParms] = decode_parms
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
private
|
238
|
+
|
239
|
+
# Makes sure that the stream data is either a String or a HexaPDF::StreamData object.
|
240
|
+
def after_data_change
|
241
|
+
super
|
242
|
+
data.stream ||= ''.b
|
243
|
+
unless data.stream.kind_of?(StreamData) || data.stream.kind_of?(String)
|
244
|
+
raise ArgumentError, "Object of class #{data.stream.class} cannot be used as stream value"
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
# Returns the filter object that corresponds to the given filter name.
|
249
|
+
#
|
250
|
+
# See: HexaPDF::Filter
|
251
|
+
def filter_for_name(filter_name)
|
252
|
+
GlobalConfiguration.constantize('filter.map', filter_name) do
|
253
|
+
raise HexaPDF::Error, "Unknown stream filter '#{filter_name}' encountered"
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
# :nodoc:
|
258
|
+
# A mapping from short name to long name for filters.
|
259
|
+
FILTER_MAP = {AHx: :ASCIIHexDecode, A85: :ASCII85Decode, LZW: :LZWDecode,
|
260
|
+
Fl: :FlateDecode, RL: :RunLengthDecode, CCF: :CCITTFaxDecode, DCT: :DCTDecode}
|
261
|
+
|
262
|
+
# Validates the /Filter entry so that it contains only long-name filter names.
|
263
|
+
def perform_validation
|
264
|
+
super
|
265
|
+
if value[:Filter].kind_of?(Symbol) && FILTER_MAP.key?(value[:Filter])
|
266
|
+
yield("A stream's /Filter entry may only use long-form filter names", true)
|
267
|
+
value[:Filter] = FILTER_MAP[value[:Filter]]
|
268
|
+
elsif value[:Filter].kind_of?(Array)
|
269
|
+
value[:Filter].map! do |filter|
|
270
|
+
next filter unless FILTER_MAP.key?(filter)
|
271
|
+
yield("A stream's /Filter entry may only use long-form filter names", true)
|
272
|
+
FILTER_MAP[filter]
|
273
|
+
end
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
end
|
278
|
+
|
279
|
+
end
|