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,139 @@
|
|
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 'weakref'
|
36
|
+
|
37
|
+
module HexaPDF
|
38
|
+
|
39
|
+
# The Importer class manages the process of copying objects from one Document to another.
|
40
|
+
#
|
41
|
+
# It may seem unnecessary using an importer containing state for the task. However, by retaining
|
42
|
+
# some information about the already copied objects we can make sure that already imported
|
43
|
+
# objects don't get imported again.
|
44
|
+
#
|
45
|
+
# Two types of indirect objects are *never* imported from one document to another: the catalog
|
46
|
+
# and page tree nodes. If the catalog was imported, the whole source document would be imported.
|
47
|
+
# And if one page tree node would imported, the whole page tree would be imported.
|
48
|
+
#
|
49
|
+
# See: Document#import
|
50
|
+
class Importer
|
51
|
+
|
52
|
+
class NullableWeakRef < WeakRef #:nodoc:
|
53
|
+
def __getobj__ #:nodoc:
|
54
|
+
super rescue nil
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Returns the Importer object for copying objects from the +source+ to the +destination+
|
59
|
+
# document.
|
60
|
+
def self.for(source:, destination:)
|
61
|
+
@map ||= {}
|
62
|
+
@map.keep_if {|_, v| v.source.weakref_alive? && v.destination.weakref_alive?}
|
63
|
+
source = NullableWeakRef.new(source)
|
64
|
+
destination = NullableWeakRef.new(destination)
|
65
|
+
@map[[source.hash, destination.hash]] ||= new(source: source, destination: destination)
|
66
|
+
end
|
67
|
+
|
68
|
+
private_class_method :new
|
69
|
+
|
70
|
+
attr_reader :source, :destination #:nodoc:
|
71
|
+
|
72
|
+
# Initializes a new importer that can import objects from the +source+ document to the
|
73
|
+
# +destination+ document.
|
74
|
+
def initialize(source:, destination:)
|
75
|
+
@source = source
|
76
|
+
@destination = destination
|
77
|
+
@mapper = {}
|
78
|
+
end
|
79
|
+
|
80
|
+
# Imports the given +object+ from the source to the destination object and returns the
|
81
|
+
# imported object.
|
82
|
+
#
|
83
|
+
# Note: Indirect objects are automatically added to the destination document but direct or
|
84
|
+
# simple objects are not.
|
85
|
+
#
|
86
|
+
# An error is raised if the object doesn't belong to the +source+ document.
|
87
|
+
def import(object)
|
88
|
+
mapped_object = @mapper[object.data] if object.kind_of?(HexaPDF::Object)
|
89
|
+
if object.kind_of?(HexaPDF::Object) && object.document? && @source != object.document
|
90
|
+
raise HexaPDF::Error, "Import error: Incorrect document object for importer"
|
91
|
+
elsif mapped_object && mapped_object == @destination.object(mapped_object)
|
92
|
+
mapped_object
|
93
|
+
else
|
94
|
+
duplicate(object)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
|
100
|
+
# Recursively duplicates the object.
|
101
|
+
#
|
102
|
+
# PDF objects are automatically added to the destination document if they are indirect objects
|
103
|
+
# in the source document.
|
104
|
+
def duplicate(object)
|
105
|
+
case object
|
106
|
+
when Hash
|
107
|
+
object.each_with_object({}) do |(k, v), obj|
|
108
|
+
obj[k] = duplicate(v)
|
109
|
+
end
|
110
|
+
when Array
|
111
|
+
object.map {|v| duplicate(v)}
|
112
|
+
when HexaPDF::Reference
|
113
|
+
import(@source.object(object))
|
114
|
+
when HexaPDF::Object
|
115
|
+
if object.type == :Catalog || object.type == :Pages
|
116
|
+
@mapper[object.data] = nil
|
117
|
+
else
|
118
|
+
obj = @mapper[object.data] = object.dup
|
119
|
+
obj.document = @destination
|
120
|
+
obj.instance_variable_set(:@data, obj.data.dup)
|
121
|
+
obj.data.oid = 0
|
122
|
+
obj.data.gen = 0
|
123
|
+
@destination.add(obj) if object.indirect?
|
124
|
+
|
125
|
+
obj.data.stream = obj.data.stream.dup if obj.data.stream.kind_of?(String)
|
126
|
+
obj.data.value = duplicate(obj.data.value)
|
127
|
+
obj.data.value.update(duplicate(object.copy_inherited_values)) if object.type == :Page
|
128
|
+
obj
|
129
|
+
end
|
130
|
+
when String
|
131
|
+
object.dup
|
132
|
+
else
|
133
|
+
object
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
@@ -0,0 +1,78 @@
|
|
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/dictionary'
|
35
|
+
require 'hexapdf/utils/sorted_tree_node'
|
36
|
+
|
37
|
+
module HexaPDF
|
38
|
+
|
39
|
+
# Implementation of PDF name trees.
|
40
|
+
#
|
41
|
+
# Name trees are used in a similar fashion as dictionaries, however, the key in a name tree is
|
42
|
+
# always a string instead of a symbol. Another difference is that the keys in a name tree are
|
43
|
+
# always sorted to allow fast lookup of a specific key.
|
44
|
+
#
|
45
|
+
# A name tree consists of one or more NameTreeNodes. If there is only one node, it contains all
|
46
|
+
# stored associations in the /Names entry. Otherwise the root node needs to have a /Kids entry
|
47
|
+
# that points to one or more intermediate or leaf nodes. An intermediate node contains a /Kids
|
48
|
+
# entry whereas a leaf node contains a /Names entry.
|
49
|
+
#
|
50
|
+
# Since this is a complex structure that must follow several restrictions, it is not advised to
|
51
|
+
# build a name tree manually. Instead, use the provided convenience methods (see
|
52
|
+
# HexaPDF::Utils::SortedTreeNode) to add or retrieve entries. They ensure that the name tree stays
|
53
|
+
# valid.
|
54
|
+
#
|
55
|
+
# See: PDF1.7 s7.9.6
|
56
|
+
class NameTreeNode < Dictionary
|
57
|
+
|
58
|
+
include Utils::SortedTreeNode
|
59
|
+
|
60
|
+
define_field :Kids, type: Array
|
61
|
+
define_field :Names, type: Array
|
62
|
+
define_field :Limits, type: Array
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
# Defines the dictionary entry name that contains the leaf node entries.
|
67
|
+
def leaf_node_container_name
|
68
|
+
:Names
|
69
|
+
end
|
70
|
+
|
71
|
+
# Defines the class that is used for the keys in the name tree (String).
|
72
|
+
def key_type
|
73
|
+
String
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
@@ -0,0 +1,67 @@
|
|
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/dictionary'
|
35
|
+
require 'hexapdf/utils/sorted_tree_node'
|
36
|
+
|
37
|
+
module HexaPDF
|
38
|
+
|
39
|
+
# Implementation of PDF number trees.
|
40
|
+
#
|
41
|
+
# Number trees are similar to name trees but use integers as keys instead of strings. See
|
42
|
+
# HexaPDF::NameTreeNode for a more detailed explanation.
|
43
|
+
#
|
44
|
+
# See: PDF1.7 s7.9.7, HexaPDF::NameTreeNode
|
45
|
+
class NumberTreeNode < Dictionary
|
46
|
+
|
47
|
+
include Utils::SortedTreeNode
|
48
|
+
|
49
|
+
define_field :Kids, type: Array
|
50
|
+
define_field :Nums, type: Array
|
51
|
+
define_field :Limits, type: Array
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
# Defines the dictionary entry name that contains the leaf node entries.
|
56
|
+
def leaf_node_container_name
|
57
|
+
:Nums
|
58
|
+
end
|
59
|
+
|
60
|
+
# Defines the class that is used for the keys in the number tree (Integer).
|
61
|
+
def key_type
|
62
|
+
Integer
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
@@ -0,0 +1,363 @@
|
|
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/reference'
|
36
|
+
|
37
|
+
module HexaPDF
|
38
|
+
|
39
|
+
# Internal value object for storing object number, generation number, object value and a
|
40
|
+
# possible stream together. Such objects are not used directly but wrapped by Object or one of
|
41
|
+
# its subclasses.
|
42
|
+
class PDFData
|
43
|
+
|
44
|
+
#:nodoc:
|
45
|
+
attr_reader :oid, :gen
|
46
|
+
|
47
|
+
#:nodoc:
|
48
|
+
attr_accessor :stream, :value
|
49
|
+
|
50
|
+
def initialize(value, oid = nil, gen = nil, stream = nil) #:nodoc:
|
51
|
+
self.value = value
|
52
|
+
self.oid = oid
|
53
|
+
self.gen = gen
|
54
|
+
self.stream = stream
|
55
|
+
end
|
56
|
+
|
57
|
+
def oid=(oid) #:nodoc:
|
58
|
+
@oid = Integer(oid || 0)
|
59
|
+
end
|
60
|
+
|
61
|
+
def gen=(gen) #:nodoc
|
62
|
+
@gen = Integer(gen || 0)
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
# Objects of the PDF object system.
|
69
|
+
#
|
70
|
+
# == Overview
|
71
|
+
#
|
72
|
+
# A PDF object is like a normal object but with an additional *object identifier* consisting of
|
73
|
+
# an object number and a generation number. If the object number is zero, then the PDF object
|
74
|
+
# represents a direct object. Otherwise the object identifier uniquely identifies this object as
|
75
|
+
# an indirect object and can be used for referencing it (from possibly multiple places).
|
76
|
+
#
|
77
|
+
# Furthermore a PDF object may have an associated stream. However, this stream is only
|
78
|
+
# accessible if the subclass Stream is used.
|
79
|
+
#
|
80
|
+
# A PDF object *should* be connected to a PDF document, otherwise some methods may not work.
|
81
|
+
#
|
82
|
+
# Most PDF objects in a PDF document are represented by subclasses of this class that provide
|
83
|
+
# additional functionality.
|
84
|
+
#
|
85
|
+
# The methods #hash and #eql? are implemented so that objects of this class can be used as hash
|
86
|
+
# keys. Furthermore the implementation is compatible to the one of Reference, i.e. the hash of a
|
87
|
+
# PDF Object is the same as the hash of its corresponding Reference object.
|
88
|
+
#
|
89
|
+
# == Allowed PDF Object Values
|
90
|
+
#
|
91
|
+
# The PDF specification knows of the following object types:
|
92
|
+
#
|
93
|
+
# * Boolean (mapped to +true+ and +false+),
|
94
|
+
# * Integer (mapped to Integer object)
|
95
|
+
# * Real (mapped to Float objects)
|
96
|
+
# * String (mapped to String objects with UTF-8 or binary encoding)
|
97
|
+
# * Names (mapped to Symbol objects)
|
98
|
+
# * Array (mapped to Array objects)
|
99
|
+
# * Dictionary (mapped to Hash objects)
|
100
|
+
# * Stream (mapped to the Stream class which is a Dictionary with the associated stream data)
|
101
|
+
# * Null (mapped to +nil+)
|
102
|
+
# * Indirect Object (mapped to this class)
|
103
|
+
#
|
104
|
+
# So working with PDF objects in HexaPDF is rather straightforward since the common Ruby objects
|
105
|
+
# can be used for most things, i.e. wrapping an plain Ruby object into an object of this class is
|
106
|
+
# not necessary (except if it should become an indirect object).
|
107
|
+
#
|
108
|
+
# There are also some additional data structures built from these primitive ones. For example,
|
109
|
+
# Time objects are represented as specially formatted string objects and conversion from and to
|
110
|
+
# the string representation is handled automatically.
|
111
|
+
#
|
112
|
+
# *Important*: Users of HexaPDF may use other plain Ruby objects but then there is no guarantee
|
113
|
+
# that everything will work correctly, especially when using other collection types than arrays
|
114
|
+
# and hashes.
|
115
|
+
#
|
116
|
+
# See: HexaPDF::Dictionary, HexaPDF::Stream, HexaPDF::Reference, HexaPDF::Document
|
117
|
+
#
|
118
|
+
# See: PDF1.7 s7.3.10, s7.3.8
|
119
|
+
class Object
|
120
|
+
|
121
|
+
include Comparable
|
122
|
+
|
123
|
+
# A list of classes whose objects cannot be duplicated.
|
124
|
+
NOT_DUPLICATABLE_CLASSES = [NilClass, FalseClass, TrueClass, Symbol, Integer, Float]
|
125
|
+
|
126
|
+
# :call-seq:
|
127
|
+
# HexaPDF::Object.deep_copy(object) -> copy
|
128
|
+
#
|
129
|
+
# Creates a deep copy of the given object which retains the references to indirect objects.
|
130
|
+
def self.deep_copy(object)
|
131
|
+
case object
|
132
|
+
when Hash
|
133
|
+
object.each_with_object({}) {|(key, val), memo| memo[key] = deep_copy(val)}
|
134
|
+
when Array
|
135
|
+
object.map {|o| deep_copy(o)}
|
136
|
+
when HexaPDF::Object
|
137
|
+
(object.indirect? ? object : deep_copy(object.value))
|
138
|
+
when HexaPDF::Reference
|
139
|
+
object
|
140
|
+
when *NOT_DUPLICATABLE_CLASSES
|
141
|
+
object
|
142
|
+
else
|
143
|
+
object.dup
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
|
148
|
+
# The wrapped HexaPDF::PDFData value.
|
149
|
+
#
|
150
|
+
# This attribute is not part of the public API!
|
151
|
+
attr_reader :data
|
152
|
+
|
153
|
+
# Sets the associated PDF document.
|
154
|
+
attr_writer :document
|
155
|
+
|
156
|
+
# Sets whether the object has to be an indirect object once it is written.
|
157
|
+
attr_writer :must_be_indirect
|
158
|
+
|
159
|
+
# Creates a new PDF object wrapping the value.
|
160
|
+
#
|
161
|
+
# The +value+ can either be a PDFData object in which case it is used directly. If it is a PDF
|
162
|
+
# Object, then its data is used. Otherwise the +value+ object is used as is. In all cases, the
|
163
|
+
# oid, gen and stream values may be overridden by the corresponding keyword arguments.
|
164
|
+
def initialize(value, document: nil, oid: nil, gen: nil, stream: nil)
|
165
|
+
@data = case value
|
166
|
+
when PDFData then value
|
167
|
+
when Object then value.data
|
168
|
+
else PDFData.new(value)
|
169
|
+
end
|
170
|
+
@data.oid = oid if oid
|
171
|
+
@data.gen = gen if gen
|
172
|
+
@data.stream = stream if stream
|
173
|
+
self.document = document
|
174
|
+
self.must_be_indirect = false
|
175
|
+
after_data_change
|
176
|
+
end
|
177
|
+
|
178
|
+
# Returns the object number of the PDF object.
|
179
|
+
def oid
|
180
|
+
data.oid
|
181
|
+
end
|
182
|
+
|
183
|
+
# Sets the object number of the PDF object.
|
184
|
+
def oid=(oid)
|
185
|
+
data.oid = oid
|
186
|
+
end
|
187
|
+
|
188
|
+
# Returns the generation number of the PDF object.
|
189
|
+
def gen
|
190
|
+
data.gen
|
191
|
+
end
|
192
|
+
|
193
|
+
# Sets the generation number of the PDF object.
|
194
|
+
def gen=(gen)
|
195
|
+
data.gen = gen
|
196
|
+
end
|
197
|
+
|
198
|
+
# Returns the object value.
|
199
|
+
def value
|
200
|
+
data.value
|
201
|
+
end
|
202
|
+
|
203
|
+
# Sets the object value. Unlike in #initialize the value is used as is!
|
204
|
+
def value=(val)
|
205
|
+
data.value = val
|
206
|
+
after_data_change
|
207
|
+
end
|
208
|
+
|
209
|
+
# Returns the associated PDF document.
|
210
|
+
#
|
211
|
+
# If no document is associated, an error is raised.
|
212
|
+
def document
|
213
|
+
@document || raise(HexaPDF::Error, "No document associated with this object (#{inspect})")
|
214
|
+
end
|
215
|
+
|
216
|
+
# Returns +true+ if a PDF document is associated.
|
217
|
+
def document?
|
218
|
+
!@document.nil?
|
219
|
+
end
|
220
|
+
|
221
|
+
# Returns +true+ if the object is an indirect object (i.e. has an object number unequal to
|
222
|
+
# zero).
|
223
|
+
def indirect?
|
224
|
+
oid != 0
|
225
|
+
end
|
226
|
+
|
227
|
+
# Returns +true+ if the object must be an indirect object once it is written.
|
228
|
+
def must_be_indirect?
|
229
|
+
@must_be_indirect
|
230
|
+
end
|
231
|
+
|
232
|
+
# Returns the type (symbol) of the object.
|
233
|
+
#
|
234
|
+
# Since the type system is implemented in such a way as to allow exchanging implementations of
|
235
|
+
# specific types, the class of an object can't be reliably used for determining the actual
|
236
|
+
# type.
|
237
|
+
#
|
238
|
+
# However, the Type and Subtype fields can easily be used for this. Subclasses for PDF objects
|
239
|
+
# that don't have such fields may use a unique name that has to begin with XX (see PDF1.7 sE.2)
|
240
|
+
# and therefore doesn't clash with names defined by the PDF specification.
|
241
|
+
#
|
242
|
+
# For basic objects this always returns +:Unknown+.
|
243
|
+
def type
|
244
|
+
:Unknown
|
245
|
+
end
|
246
|
+
|
247
|
+
# Returns +true+ if the object represents the PDF null object.
|
248
|
+
def null?
|
249
|
+
value.nil?
|
250
|
+
end
|
251
|
+
|
252
|
+
# :call-seq:
|
253
|
+
# obj.validate(auto_correct: true) -> true or false
|
254
|
+
# obj.validate(auto_correct: true) {|msg, correctable| block } -> true or false
|
255
|
+
#
|
256
|
+
# Validates the object and, optionally, corrects problems when the option +auto_correct+ is set.
|
257
|
+
# The validation routine itself has to be implemented in the #perform_validation method - see
|
258
|
+
# its documentation for more information.
|
259
|
+
#
|
260
|
+
# If a block is given, it is called on validation problems with a problem description and
|
261
|
+
# whether the problem is correctable.
|
262
|
+
#
|
263
|
+
# Returns +true+ if the object is deemed valid and +false+ otherwise.
|
264
|
+
#
|
265
|
+
# *Note*: Even if the return value is +true+ there may be problems since HexaPDF doesn't
|
266
|
+
# currently implement the full PDF spec. However, if the return value is +false+, there is
|
267
|
+
# certainly a problem!
|
268
|
+
def validate(auto_correct: true, &block)
|
269
|
+
catch do |catch_tag|
|
270
|
+
perform_validation do |msg, correctable|
|
271
|
+
block.call(msg, correctable) if block
|
272
|
+
throw(catch_tag, false) unless auto_correct && correctable
|
273
|
+
end
|
274
|
+
true
|
275
|
+
end
|
276
|
+
end
|
277
|
+
|
278
|
+
# Makes a deep copy of the source PDF object and resets the object identifier.
|
279
|
+
def deep_copy
|
280
|
+
obj = dup
|
281
|
+
obj.instance_variable_set(:@data, @data.dup)
|
282
|
+
obj.data.oid = 0
|
283
|
+
obj.data.gen = 0
|
284
|
+
obj.data.stream = @data.stream.dup if @data.stream.kind_of?(String)
|
285
|
+
obj.data.value = self.class.deep_copy(@data.value)
|
286
|
+
obj
|
287
|
+
end
|
288
|
+
|
289
|
+
# Compares this object to another object.
|
290
|
+
#
|
291
|
+
# If the other object does not respond to +oid+ or +gen+, +nil+ is returned. Otherwise objects
|
292
|
+
# are ordered first by object number and then by generation number.
|
293
|
+
def <=>(other)
|
294
|
+
return nil unless other.respond_to?(:oid) && other.respond_to?(:gen)
|
295
|
+
(oid == other.oid ? gen <=> other.gen : oid <=> other.oid)
|
296
|
+
end
|
297
|
+
|
298
|
+
# Returns +true+ if the other object is an Object and wraps the same #data structure.
|
299
|
+
def ==(other)
|
300
|
+
other.kind_of?(Object) && data == other.data
|
301
|
+
end
|
302
|
+
|
303
|
+
# Returns +true+ if the other object references the same PDF object as this object.
|
304
|
+
def eql?(other)
|
305
|
+
other.respond_to?(:oid) && oid == other.oid && other.respond_to?(:gen) && gen == other.gen
|
306
|
+
end
|
307
|
+
|
308
|
+
# Computes the hash value based on the object and generation numbers.
|
309
|
+
def hash
|
310
|
+
oid.hash ^ gen.hash
|
311
|
+
end
|
312
|
+
|
313
|
+
def inspect #:nodoc:
|
314
|
+
"#<#{self.class.name} [#{oid}, #{gen}] value=#{value.inspect}>"
|
315
|
+
end
|
316
|
+
|
317
|
+
private
|
318
|
+
|
319
|
+
# This method is called whenever the value or the stream of the wrapped PDFData structure is
|
320
|
+
# changed.
|
321
|
+
#
|
322
|
+
# A subclass implementing this method has to call +super+! Otherwise things might not work
|
323
|
+
# properly.
|
324
|
+
def after_data_change
|
325
|
+
end
|
326
|
+
|
327
|
+
# Returns the configuration object of the PDF document.
|
328
|
+
def config
|
329
|
+
document.config
|
330
|
+
end
|
331
|
+
|
332
|
+
# Validates the basic object properties.
|
333
|
+
#
|
334
|
+
# == Implementation Hint for Subclasses
|
335
|
+
#
|
336
|
+
# A subclass needs to call the super method so that the validation routines of the superclasses
|
337
|
+
# are also performed!
|
338
|
+
#
|
339
|
+
# When the validation routine finds that the object is invalid, it has to yield a problem
|
340
|
+
# description and whether the problem can be corrected. After yielding, the problem has to be
|
341
|
+
# corrected which poses no problem because the #validate method makes sure that the yield only
|
342
|
+
# returns if the problem is actually correctable and if it should be corrected.
|
343
|
+
#
|
344
|
+
# Here is a sample validation routine for stream objects:
|
345
|
+
#
|
346
|
+
# def perform_validation
|
347
|
+
# super
|
348
|
+
# unless value.kind_of?(Hash)
|
349
|
+
# yield("A stream object needs a Hash as value")
|
350
|
+
# self.value = {}
|
351
|
+
# end
|
352
|
+
# end
|
353
|
+
def perform_validation
|
354
|
+
# Validate that the object is indirect if #must_be_indirect? is +true+.
|
355
|
+
if must_be_indirect? && !indirect?
|
356
|
+
yield("Object must be an indirect object", true)
|
357
|
+
document.add(self)
|
358
|
+
end
|
359
|
+
end
|
360
|
+
|
361
|
+
end
|
362
|
+
|
363
|
+
end
|