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,172 @@
|
|
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/utils/math_helpers'
|
35
|
+
|
36
|
+
module HexaPDF
|
37
|
+
module Content
|
38
|
+
|
39
|
+
# A TransformationMatrix is a matrix used in PDF graphics operations to specify the
|
40
|
+
# relationship between different coordinate systems.
|
41
|
+
#
|
42
|
+
# All matrix operations modify the matrix in place. So if the original matrix should be
|
43
|
+
# preserved, duplicate it before the operation.
|
44
|
+
#
|
45
|
+
# It is important to note that the matrix transforms from the new coordinate system to the
|
46
|
+
# untransformed coordinate system. This means that after the transformation all coordinates
|
47
|
+
# are specified in the new, transformed coordinate system and to get the untransformed
|
48
|
+
# coordinates the matrix needs to be applied.
|
49
|
+
#
|
50
|
+
# Although all operations are done in 2D space the transformation matrix is a 3x3 matrix
|
51
|
+
# because homogeneous coordinates are used. This, however, also means that only six entries
|
52
|
+
# are actually used that are named like in the following graphic:
|
53
|
+
#
|
54
|
+
# a b 0
|
55
|
+
# c d 0
|
56
|
+
# e f 1
|
57
|
+
#
|
58
|
+
# Here is a simple transformation matrix to translate all coordinates by 5 units horizontally
|
59
|
+
# and 10 units vertically:
|
60
|
+
#
|
61
|
+
# 1 0 0
|
62
|
+
# 0 1 0
|
63
|
+
# 5 10 1
|
64
|
+
#
|
65
|
+
# Details and some examples can be found in the PDF reference.
|
66
|
+
#
|
67
|
+
# See: PDF1.7 s8.3
|
68
|
+
class TransformationMatrix
|
69
|
+
|
70
|
+
include HexaPDF::Utils::MathHelpers
|
71
|
+
|
72
|
+
# The value at the position (1,1) in the matrix.
|
73
|
+
attr_reader :a
|
74
|
+
|
75
|
+
# The value at the position (1,2) in the matrix.
|
76
|
+
attr_reader :b
|
77
|
+
|
78
|
+
# The value at the position (2,1) in the matrix.
|
79
|
+
attr_reader :c
|
80
|
+
|
81
|
+
# The value at the position (2,2) in the matrix.
|
82
|
+
attr_reader :d
|
83
|
+
|
84
|
+
# The value at the position (3,1) in the matrix.
|
85
|
+
attr_reader :e
|
86
|
+
|
87
|
+
# The value at the position (3,2) in the matrix.
|
88
|
+
attr_reader :f
|
89
|
+
|
90
|
+
# Initializes the transformation matrix with the given values.
|
91
|
+
def initialize(a = 1, b = 0, c = 0, d = 1, e = 0, f = 0)
|
92
|
+
@a = a
|
93
|
+
@b = b
|
94
|
+
@c = c
|
95
|
+
@d = d
|
96
|
+
@e = e
|
97
|
+
@f = f
|
98
|
+
end
|
99
|
+
|
100
|
+
# Returns the untransformed coordinates of the given point.
|
101
|
+
def evaluate(x, y)
|
102
|
+
[@a * x + @c * y + @e, @b * x + @d * y + @f]
|
103
|
+
end
|
104
|
+
|
105
|
+
# Translates this matrix by +x+ units horizontally and +y+ units vertically and returns it.
|
106
|
+
#
|
107
|
+
# This is equal to premultiply(1, 0, 0, 1, x, y).
|
108
|
+
def translate(x, y)
|
109
|
+
@e = x * @a + y * @c + @e
|
110
|
+
@f = x * @b + y * @d + @f
|
111
|
+
self
|
112
|
+
end
|
113
|
+
|
114
|
+
# Scales this matrix by +sx+ units horizontally and +y+ units vertically and returns it.
|
115
|
+
#
|
116
|
+
# This is equal to premultiply(sx, 0, 0, sy, 0, 0).
|
117
|
+
def scale(sx, sy)
|
118
|
+
@a = sx * @a
|
119
|
+
@b = sx * @b
|
120
|
+
@c = sy * @c
|
121
|
+
@d = sy * @d
|
122
|
+
self
|
123
|
+
end
|
124
|
+
|
125
|
+
# Rotates this matrix by an angle of +q+ degrees and returns it.
|
126
|
+
#
|
127
|
+
# This equal to premultiply(cos(rad(q)), sin(rad(q)), -sin(rad(q)), cos(rad(q)), x, y).
|
128
|
+
def rotate(q)
|
129
|
+
cq = Math.cos(deg_to_rad(q))
|
130
|
+
sq = Math.sin(deg_to_rad(q))
|
131
|
+
premultiply(cq, sq, -sq, cq, 0, 0)
|
132
|
+
end
|
133
|
+
|
134
|
+
# Skews this matrix by an angle of +a+ degrees for the x axis and by an angle of +b+ degrees
|
135
|
+
# for the y axis and returns it.
|
136
|
+
#
|
137
|
+
# This is equal to premultiply(1, tan(rad(a)), tan(rad(b)), 1, x, y).
|
138
|
+
def skew(a, b)
|
139
|
+
premultiply(1, Math.tan(deg_to_rad(a)), Math.tan(deg_to_rad(b)), 1, 0, 0)
|
140
|
+
end
|
141
|
+
|
142
|
+
# Transforms this matrix by premultiplying it with the given one (ie. given*this) and
|
143
|
+
# returns it.
|
144
|
+
def premultiply(a, b, c, d, e, f)
|
145
|
+
a1 = a * @a + b * @c
|
146
|
+
b1 = a * @b + b * @d
|
147
|
+
c1 = c * @a + d * @c
|
148
|
+
d1 = c * @b + d * @d
|
149
|
+
@e = e * @a + f * @c + @e
|
150
|
+
@f = e * @b + f * @d + @f
|
151
|
+
@a = a1
|
152
|
+
@b = b1
|
153
|
+
@c = c1
|
154
|
+
@d = d1
|
155
|
+
self
|
156
|
+
end
|
157
|
+
|
158
|
+
# Returns +true+ if the other object is a transformation matrix with the same values.
|
159
|
+
def ==(other)
|
160
|
+
(other.kind_of?(self.class) && @a == other.a && @b == other.b && @c == other.c &&
|
161
|
+
@d == other.d && @e == other.e && @f == other.f)
|
162
|
+
end
|
163
|
+
|
164
|
+
# Creates an array [a, b, c, d, e, f] from the transformation matrix.
|
165
|
+
def to_a
|
166
|
+
[@a, @b, @c, @d, @e, @f]
|
167
|
+
end
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|
172
|
+
end
|
@@ -0,0 +1,47 @@
|
|
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
|
+
module HexaPDF
|
35
|
+
|
36
|
+
# == Overview
|
37
|
+
#
|
38
|
+
# The Content module contains everything related to working with page content streams.
|
39
|
+
module Content
|
40
|
+
|
41
|
+
autoload(:Canvas, 'hexapdf/content/canvas')
|
42
|
+
autoload(:Parser, 'hexapdf/content/parser')
|
43
|
+
autoload(:Processor, 'hexapdf/content/processor')
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,51 @@
|
|
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
|
+
module HexaPDF
|
35
|
+
|
36
|
+
# Returns the data directory for HexaPDF.
|
37
|
+
def self.data_dir
|
38
|
+
unless defined?(@data_dir)
|
39
|
+
require 'rbconfig'
|
40
|
+
@data_dir = File.expand_path(File.join(__dir__, '..', '..', 'data', 'hexapdf'))
|
41
|
+
unless File.directory?(@data_dir)
|
42
|
+
@data_dir = File.expand_path(File.join(RbConfig::CONFIG["datadir"], "hexapdf"))
|
43
|
+
end
|
44
|
+
unless File.directory?(@data_dir)
|
45
|
+
raise "HexaPDF data directory not found! This is a bug, please report it!"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
@data_dir
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,303 @@
|
|
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/object'
|
36
|
+
require 'hexapdf/dictionary_fields'
|
37
|
+
require 'hexapdf/reference'
|
38
|
+
|
39
|
+
module HexaPDF
|
40
|
+
|
41
|
+
# Implementation of the PDF dictionary type.
|
42
|
+
#
|
43
|
+
# Subclasses should use the available class method ::define_field to create fields according to
|
44
|
+
# the PDF specification. This allows, among other things, automatic type checking and
|
45
|
+
# basic validation.
|
46
|
+
#
|
47
|
+
# Fields defined in superclasses are inherited by their subclasses. This avoids duplicating
|
48
|
+
# basic field information.
|
49
|
+
#
|
50
|
+
# See: PDF1.7 s7.3.7
|
51
|
+
class Dictionary < HexaPDF::Object
|
52
|
+
|
53
|
+
include DictionaryFields
|
54
|
+
|
55
|
+
# Defines an entry for the field +name+ and returns the initalized
|
56
|
+
# HexaPDF::DictionaryFields::Field object. A suitable converter module (see
|
57
|
+
# HexaPDF::DictionaryFields::Field#converter) is selected based on the type argument.
|
58
|
+
#
|
59
|
+
# Options:
|
60
|
+
#
|
61
|
+
# type:: The class (or an array of classes) that a value of this field must have. Here is a
|
62
|
+
# mapping from PDF object types to classes:
|
63
|
+
#
|
64
|
+
# Boolean:: \[TrueClass, FalseClass] (or use the Boolean constant)
|
65
|
+
# Integer:: Integer
|
66
|
+
# Real:: Float
|
67
|
+
# String:: String (for text strings), PDFByteString (for binary strings)
|
68
|
+
# Date:: PDFDate
|
69
|
+
# Name:: Symbol
|
70
|
+
# Array:: Array
|
71
|
+
# Dictionary:: Dictionary (or any subclass) or Hash
|
72
|
+
# Stream:: Stream (or any subclass)
|
73
|
+
# Null:: NilClass
|
74
|
+
#
|
75
|
+
# If an array of classes is provided, the value can be an instance of any of these
|
76
|
+
# classes.
|
77
|
+
#
|
78
|
+
# If a Symbol object instead of a class is provided, the class is looked up using the
|
79
|
+
# 'object.type_map' global configuration option when necessary to support lazy loading.
|
80
|
+
#
|
81
|
+
# required:: Specifies whether this field is required.
|
82
|
+
#
|
83
|
+
# default:: Specifies the default value for the field, if any.
|
84
|
+
#
|
85
|
+
# indirect:: Specifies whether the value (or the values in the array value) of this field has
|
86
|
+
# to be an indirect object (+true+), a direct object (+false+) or if it doesn't
|
87
|
+
# matter (unspecified or +nil+).
|
88
|
+
#
|
89
|
+
# version:: Specifies the minimum version of the PDF specification needed for this value.
|
90
|
+
def self.define_field(name, type:, required: false, default: nil, indirect: nil,
|
91
|
+
version: '1.2')
|
92
|
+
@fields ||= {}
|
93
|
+
@fields[name] = Field.new(type, required, default, indirect, version)
|
94
|
+
end
|
95
|
+
|
96
|
+
# Returns the field entry for the given field name.
|
97
|
+
#
|
98
|
+
# The ancestor classes are also searched for such a field entry if none is found for the
|
99
|
+
# current class.
|
100
|
+
def self.field(name)
|
101
|
+
if defined?(@fields) && @fields.key?(name)
|
102
|
+
@fields[name]
|
103
|
+
elsif superclass.respond_to?(:field)
|
104
|
+
superclass.field(name)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
# :call-seq:
|
109
|
+
# class.each_field {|name, data| block } -> class
|
110
|
+
# class.each_field -> Enumerator
|
111
|
+
#
|
112
|
+
# Calls the block once for each field defined either in this class or in one of the ancestor
|
113
|
+
# classes.
|
114
|
+
def self.each_field(&block) # :yields: name, data
|
115
|
+
return to_enum(__method__) unless block_given?
|
116
|
+
superclass.each_field(&block) if self != Dictionary && superclass != Dictionary
|
117
|
+
@fields.each(&block) if defined?(@fields)
|
118
|
+
end
|
119
|
+
|
120
|
+
|
121
|
+
# Returns the value for the given dictionary entry.
|
122
|
+
#
|
123
|
+
# This method should be used instead of direct access to the value because it provides
|
124
|
+
# numerous advantages:
|
125
|
+
#
|
126
|
+
# * References are automatically resolved.
|
127
|
+
#
|
128
|
+
# * Returns the native Ruby object for values with class HexaPDF::Object. However, all
|
129
|
+
# subclasses of HexaPDF::Object are returned as is (it makes no sense, for example, to return
|
130
|
+
# the hash that describes the Catalog instead of the Catalog object).
|
131
|
+
#
|
132
|
+
# * Automatically wraps hash values in specific subclasses of this class if field information is
|
133
|
+
# available (see ::define_field).
|
134
|
+
#
|
135
|
+
# * Returns the default value if one is specified and no value is available.
|
136
|
+
def [](name)
|
137
|
+
field = self.class.field(name)
|
138
|
+
data = if key?(name)
|
139
|
+
value[name]
|
140
|
+
elsif field && field.default?
|
141
|
+
value[name] = field.default
|
142
|
+
end
|
143
|
+
value[name] = data = document.deref(data) if data.kind_of?(HexaPDF::Reference)
|
144
|
+
data = data.value if data.class == HexaPDF::Object
|
145
|
+
self[name] = data = field.convert(data, document) if field && field.convert?(data)
|
146
|
+
data
|
147
|
+
end
|
148
|
+
|
149
|
+
# Stores the data under name in the dictionary. Name has to be a Symbol object.
|
150
|
+
#
|
151
|
+
# If the current value for this name has the class HexaPDF::Object (and only this, no
|
152
|
+
# subclasses) and the given value has not (including subclasses), the value is stored inside the
|
153
|
+
# HexaPDF::Object.
|
154
|
+
def []=(name, data)
|
155
|
+
unless name.kind_of?(Symbol)
|
156
|
+
raise ArgumentError, "Only Symbol (Name) keys are allowed to be used in PDF dictionaries"
|
157
|
+
end
|
158
|
+
|
159
|
+
if value[name].class == HexaPDF::Object && !data.kind_of?(HexaPDF::Object) &&
|
160
|
+
!data.kind_of?(HexaPDF::Reference)
|
161
|
+
value[name].value = data
|
162
|
+
else
|
163
|
+
value[name] = data
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
# Returns +true+ if the given key is present in the dictionary.
|
168
|
+
def key?(key)
|
169
|
+
value.key?(key)
|
170
|
+
end
|
171
|
+
|
172
|
+
# Deletes the name-value pair from the dictionary and returns the value. If such a pair does
|
173
|
+
# not exist, +nil+ is returned.
|
174
|
+
def delete(name)
|
175
|
+
value.delete(name) { nil }
|
176
|
+
end
|
177
|
+
|
178
|
+
# :call-seq:
|
179
|
+
# dict.each {|name, value| block} -> dict
|
180
|
+
# dict.each -> Enumerator
|
181
|
+
#
|
182
|
+
# Calls the given block once for every name-value entry that is stored in the dictionary.
|
183
|
+
#
|
184
|
+
# Note that the yielded value is already preprocessed like in #[].
|
185
|
+
def each
|
186
|
+
return to_enum(__method__) unless block_given?
|
187
|
+
value.each_key {|name| yield(name, self[name])}
|
188
|
+
self
|
189
|
+
end
|
190
|
+
|
191
|
+
# Returns the value of the /Type field or, if not set, the result of Object#type.
|
192
|
+
def type
|
193
|
+
self[:Type] || super
|
194
|
+
end
|
195
|
+
|
196
|
+
# Returns +true+ if the dictionary contains no entries.
|
197
|
+
def empty?
|
198
|
+
value.empty?
|
199
|
+
end
|
200
|
+
|
201
|
+
# Returns a dup of the underlying hash.
|
202
|
+
def to_hash
|
203
|
+
value.dup
|
204
|
+
end
|
205
|
+
alias :to_h :to_hash
|
206
|
+
|
207
|
+
|
208
|
+
private
|
209
|
+
|
210
|
+
# Ensures that the value is useful for a Dictionary and updates the object's value with
|
211
|
+
# information from the dictionary's field.
|
212
|
+
def after_data_change # :nodoc:
|
213
|
+
super
|
214
|
+
data.value ||= {}
|
215
|
+
unless value.kind_of?(Hash)
|
216
|
+
raise ArgumentError, "A PDF dictionary object needs a hash value, not a #{value.class}"
|
217
|
+
end
|
218
|
+
set_required_fields_with_defaults
|
219
|
+
end
|
220
|
+
|
221
|
+
# Sets all required fields that have no current value but a default value to their respective
|
222
|
+
# default value.
|
223
|
+
def set_required_fields_with_defaults
|
224
|
+
self.class.each_field do |name, field|
|
225
|
+
if !key?(name) && field.required? && field.default?
|
226
|
+
value[name] = field.default
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
# Iterates over all currently set fields and those that are required.
|
232
|
+
def each_set_key_or_required_field #:yields: name, field
|
233
|
+
value.each_key {|name| yield(name, self.class.field(name))}
|
234
|
+
self.class.each_field do |name, field|
|
235
|
+
yield(name, field) if field.required? && !value.key?(name)
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
# Performs validation tasks based on the currently set keys and defined fields.
|
240
|
+
def perform_validation(&block)
|
241
|
+
super
|
242
|
+
each_set_key_or_required_field do |name, field|
|
243
|
+
obj = key?(name) && self[name] || nil
|
244
|
+
|
245
|
+
# Validate direct PDF objects and those possibly nested within a hash
|
246
|
+
if obj.kind_of?(HexaPDF::Object) && !obj.indirect?
|
247
|
+
obj.validate(&block)
|
248
|
+
elsif obj.kind_of?(Hash)
|
249
|
+
validate_hash(obj, &block)
|
250
|
+
end
|
251
|
+
|
252
|
+
# The checks below need a valid field definition
|
253
|
+
next if field.nil?
|
254
|
+
|
255
|
+
# Check that required fields are set
|
256
|
+
if field.required? && obj.nil?
|
257
|
+
yield("Required field #{name} is not set", field.default?)
|
258
|
+
self[name] = obj = field.default
|
259
|
+
end
|
260
|
+
|
261
|
+
# Check if the document version is set high enough
|
262
|
+
if field.version > document.instance_variable_get(:@version)
|
263
|
+
yield("Field #{name} requires document version to be #{field.version}", true)
|
264
|
+
document.version = field.version
|
265
|
+
end
|
266
|
+
|
267
|
+
# The checks below assume that the field has a value
|
268
|
+
next if obj.nil?
|
269
|
+
|
270
|
+
# Check the type of the field
|
271
|
+
unless field.valid_object?(obj)
|
272
|
+
yield("Type of field #{name} is invalid: #{obj.class}", false)
|
273
|
+
end
|
274
|
+
|
275
|
+
# Check if field value needs to be (in)direct
|
276
|
+
unless field.indirect.nil?
|
277
|
+
obj = value[name] # we need the unwrapped object!
|
278
|
+
if field.indirect && (!obj.kind_of?(HexaPDF::Object) || !obj.indirect?)
|
279
|
+
yield("Field #{name} needs to be an indirect object", true)
|
280
|
+
value[name] = obj = document.add(obj)
|
281
|
+
elsif !field.indirect && obj.kind_of?(HexaPDF::Object) && obj.indirect?
|
282
|
+
yield("Field #{name} needs to be a direct object", true)
|
283
|
+
document.delete(obj)
|
284
|
+
value[name] = obj = obj.value
|
285
|
+
end
|
286
|
+
end
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
# Validates all nested values of the given hash.
|
291
|
+
def validate_hash(hash, &block)
|
292
|
+
hash.each_value do |obj|
|
293
|
+
if obj.kind_of?(HexaPDF::Object) && !obj.indirect?
|
294
|
+
obj.validate(&block)
|
295
|
+
elsif obj.kind_of?(Hash)
|
296
|
+
validate_hash(obj, &block)
|
297
|
+
end
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
end
|
302
|
+
|
303
|
+
end
|