hexapdf 0.17.1 → 0.17.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1024 -0
- data/LICENSE +29 -0
- data/README.md +129 -0
- data/Rakefile +109 -0
- data/agpl-3.0.txt +661 -0
- data/examples/001-hello_world.rb +16 -0
- data/examples/002-graphics.rb +275 -0
- data/examples/003-arcs.rb +50 -0
- data/examples/004-optimizing.rb +23 -0
- data/examples/005-merging.rb +27 -0
- data/examples/006-standard_pdf_fonts.rb +73 -0
- data/examples/007-truetype.rb +42 -0
- data/examples/008-show_char_bboxes.rb +55 -0
- data/examples/009-text_layouter_alignment.rb +47 -0
- data/examples/010-text_layouter_inline_boxes.rb +64 -0
- data/examples/011-text_layouter_line_wrapping.rb +57 -0
- data/examples/012-text_layouter_styling.rb +122 -0
- data/examples/013-text_layouter_shapes.rb +176 -0
- data/examples/014-text_in_polygon.rb +60 -0
- data/examples/015-boxes.rb +76 -0
- data/examples/016-frame_automatic_box_placement.rb +90 -0
- data/examples/017-frame_text_flow.rb +60 -0
- data/examples/018-composer.rb +44 -0
- data/examples/019-acro_form.rb +88 -0
- data/examples/emoji-smile.png +0 -0
- data/examples/emoji-wink.png +0 -0
- data/examples/machupicchu.jpg +0 -0
- data/lib/hexapdf/content/graphic_object/endpoint_arc.rb +66 -0
- data/lib/hexapdf/content/graphic_object/geom2d.rb +13 -0
- data/lib/hexapdf/version.rb +1 -1
- 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/images/ycck.jpg +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 +236 -0
- data/test/hexapdf/content/common.rb +39 -0
- data/test/hexapdf/content/graphic_object/test_arc.rb +102 -0
- data/test/hexapdf/content/graphic_object/test_endpoint_arc.rb +90 -0
- data/test/hexapdf/content/graphic_object/test_geom2d.rb +79 -0
- data/test/hexapdf/content/graphic_object/test_solid_arc.rb +86 -0
- data/test/hexapdf/content/test_canvas.rb +1279 -0
- data/test/hexapdf/content/test_color_space.rb +176 -0
- data/test/hexapdf/content/test_graphics_state.rb +151 -0
- data/test/hexapdf/content/test_operator.rb +619 -0
- data/test/hexapdf/content/test_parser.rb +99 -0
- data/test/hexapdf/content/test_processor.rb +163 -0
- data/test/hexapdf/content/test_transformation_matrix.rb +64 -0
- data/test/hexapdf/document/test_files.rb +72 -0
- data/test/hexapdf/document/test_fonts.rb +60 -0
- data/test/hexapdf/document/test_images.rb +72 -0
- data/test/hexapdf/document/test_pages.rb +130 -0
- data/test/hexapdf/encryption/common.rb +87 -0
- data/test/hexapdf/encryption/test_aes.rb +129 -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 +380 -0
- data/test/hexapdf/encryption/test_standard_security_handler.rb +322 -0
- data/test/hexapdf/filter/common.rb +53 -0
- data/test/hexapdf/filter/test_ascii85_decode.rb +59 -0
- data/test/hexapdf/filter/test_ascii_hex_decode.rb +38 -0
- data/test/hexapdf/filter/test_crypt.rb +21 -0
- data/test/hexapdf/filter/test_encryption.rb +24 -0
- data/test/hexapdf/filter/test_flate_decode.rb +44 -0
- data/test/hexapdf/filter/test_lzw_decode.rb +52 -0
- data/test/hexapdf/filter/test_predictor.rb +219 -0
- data/test/hexapdf/filter/test_run_length_decode.rb +32 -0
- data/test/hexapdf/font/cmap/test_parser.rb +102 -0
- data/test/hexapdf/font/cmap/test_writer.rb +66 -0
- data/test/hexapdf/font/encoding/test_base.rb +45 -0
- data/test/hexapdf/font/encoding/test_difference_encoding.rb +29 -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_cmap.rb +104 -0
- data/test/hexapdf/font/test_encoding.rb +27 -0
- data/test/hexapdf/font/test_invalid_glyph.rb +34 -0
- data/test/hexapdf/font/test_true_type_wrapper.rb +186 -0
- data/test/hexapdf/font/test_type1_wrapper.rb +107 -0
- data/test/hexapdf/font/true_type/common.rb +17 -0
- data/test/hexapdf/font/true_type/table/common.rb +27 -0
- data/test/hexapdf/font/true_type/table/test_cmap.rb +47 -0
- data/test/hexapdf/font/true_type/table/test_cmap_subtable.rb +141 -0
- data/test/hexapdf/font/true_type/table/test_directory.rb +30 -0
- data/test/hexapdf/font/true_type/table/test_glyf.rb +58 -0
- data/test/hexapdf/font/true_type/table/test_head.rb +56 -0
- data/test/hexapdf/font/true_type/table/test_hhea.rb +26 -0
- data/test/hexapdf/font/true_type/table/test_hmtx.rb +30 -0
- data/test/hexapdf/font/true_type/table/test_kern.rb +61 -0
- data/test/hexapdf/font/true_type/table/test_loca.rb +33 -0
- data/test/hexapdf/font/true_type/table/test_maxp.rb +50 -0
- data/test/hexapdf/font/true_type/table/test_name.rb +76 -0
- data/test/hexapdf/font/true_type/table/test_os2.rb +55 -0
- data/test/hexapdf/font/true_type/table/test_post.rb +78 -0
- data/test/hexapdf/font/true_type/test_builder.rb +42 -0
- data/test/hexapdf/font/true_type/test_font.rb +116 -0
- data/test/hexapdf/font/true_type/test_optimizer.rb +26 -0
- data/test/hexapdf/font/true_type/test_subsetter.rb +73 -0
- data/test/hexapdf/font/true_type/test_table.rb +48 -0
- data/test/hexapdf/font/type1/common.rb +6 -0
- data/test/hexapdf/font/type1/test_afm_parser.rb +65 -0
- data/test/hexapdf/font/type1/test_font.rb +104 -0
- data/test/hexapdf/font/type1/test_font_metrics.rb +22 -0
- data/test/hexapdf/font/type1/test_pfb_parser.rb +37 -0
- data/test/hexapdf/font_loader/test_from_configuration.rb +43 -0
- data/test/hexapdf/font_loader/test_from_file.rb +36 -0
- data/test/hexapdf/font_loader/test_standard14.rb +33 -0
- data/test/hexapdf/image_loader/test_jpeg.rb +93 -0
- data/test/hexapdf/image_loader/test_pdf.rb +47 -0
- data/test/hexapdf/image_loader/test_png.rb +259 -0
- data/test/hexapdf/layout/test_box.rb +154 -0
- data/test/hexapdf/layout/test_frame.rb +350 -0
- data/test/hexapdf/layout/test_image_box.rb +73 -0
- data/test/hexapdf/layout/test_inline_box.rb +71 -0
- data/test/hexapdf/layout/test_line.rb +206 -0
- data/test/hexapdf/layout/test_style.rb +790 -0
- data/test/hexapdf/layout/test_text_box.rb +140 -0
- data/test/hexapdf/layout/test_text_fragment.rb +375 -0
- data/test/hexapdf/layout/test_text_layouter.rb +758 -0
- data/test/hexapdf/layout/test_text_shaper.rb +62 -0
- data/test/hexapdf/layout/test_width_from_polygon.rb +109 -0
- data/test/hexapdf/task/test_dereference.rb +51 -0
- data/test/hexapdf/task/test_optimize.rb +162 -0
- data/test/hexapdf/test_composer.rb +258 -0
- data/test/hexapdf/test_configuration.rb +93 -0
- data/test/hexapdf/test_data_dir.rb +32 -0
- data/test/hexapdf/test_dictionary.rb +340 -0
- data/test/hexapdf/test_dictionary_fields.rb +269 -0
- data/test/hexapdf/test_document.rb +641 -0
- data/test/hexapdf/test_filter.rb +100 -0
- data/test/hexapdf/test_importer.rb +106 -0
- data/test/hexapdf/test_object.rb +258 -0
- data/test/hexapdf/test_parser.rb +645 -0
- data/test/hexapdf/test_pdf_array.rb +169 -0
- data/test/hexapdf/test_rectangle.rb +73 -0
- data/test/hexapdf/test_reference.rb +50 -0
- data/test/hexapdf/test_revision.rb +188 -0
- data/test/hexapdf/test_revisions.rb +196 -0
- data/test/hexapdf/test_serializer.rb +195 -0
- data/test/hexapdf/test_stream.rb +274 -0
- data/test/hexapdf/test_tokenizer.rb +80 -0
- data/test/hexapdf/test_type.rb +18 -0
- data/test/hexapdf/test_writer.rb +140 -0
- data/test/hexapdf/test_xref_section.rb +61 -0
- data/test/hexapdf/type/acro_form/test_appearance_generator.rb +795 -0
- data/test/hexapdf/type/acro_form/test_button_field.rb +308 -0
- data/test/hexapdf/type/acro_form/test_choice_field.rb +220 -0
- data/test/hexapdf/type/acro_form/test_field.rb +259 -0
- data/test/hexapdf/type/acro_form/test_form.rb +357 -0
- data/test/hexapdf/type/acro_form/test_signature_field.rb +38 -0
- data/test/hexapdf/type/acro_form/test_text_field.rb +201 -0
- data/test/hexapdf/type/acro_form/test_variable_text_field.rb +88 -0
- data/test/hexapdf/type/actions/test_launch.rb +24 -0
- data/test/hexapdf/type/actions/test_uri.rb +23 -0
- data/test/hexapdf/type/annotations/test_markup_annotation.rb +22 -0
- data/test/hexapdf/type/annotations/test_text.rb +34 -0
- data/test/hexapdf/type/annotations/test_widget.rb +225 -0
- data/test/hexapdf/type/test_annotation.rb +97 -0
- data/test/hexapdf/type/test_catalog.rb +48 -0
- data/test/hexapdf/type/test_cid_font.rb +61 -0
- data/test/hexapdf/type/test_file_specification.rb +141 -0
- data/test/hexapdf/type/test_font.rb +67 -0
- data/test/hexapdf/type/test_font_descriptor.rb +61 -0
- data/test/hexapdf/type/test_font_simple.rb +176 -0
- data/test/hexapdf/type/test_font_true_type.rb +31 -0
- data/test/hexapdf/type/test_font_type0.rb +120 -0
- data/test/hexapdf/type/test_font_type1.rb +142 -0
- data/test/hexapdf/type/test_font_type3.rb +26 -0
- data/test/hexapdf/type/test_form.rb +120 -0
- data/test/hexapdf/type/test_image.rb +261 -0
- data/test/hexapdf/type/test_info.rb +9 -0
- data/test/hexapdf/type/test_object_stream.rb +117 -0
- data/test/hexapdf/type/test_page.rb +598 -0
- data/test/hexapdf/type/test_page_tree_node.rb +315 -0
- data/test/hexapdf/type/test_resources.rb +209 -0
- data/test/hexapdf/type/test_trailer.rb +116 -0
- data/test/hexapdf/type/test_xref_stream.rb +143 -0
- data/test/hexapdf/utils/test_bit_field.rb +63 -0
- data/test/hexapdf/utils/test_bit_stream.rb +69 -0
- data/test/hexapdf/utils/test_graphics_helpers.rb +37 -0
- data/test/hexapdf/utils/test_lru_cache.rb +22 -0
- data/test/hexapdf/utils/test_object_hash.rb +120 -0
- data/test/hexapdf/utils/test_pdf_doc_encoding.rb +18 -0
- data/test/hexapdf/utils/test_sorted_tree_node.rb +239 -0
- data/test/test_helper.rb +58 -0
- metadata +263 -3
@@ -0,0 +1,269 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
require 'hexapdf/dictionary_fields'
|
5
|
+
require 'hexapdf/dictionary'
|
6
|
+
require 'hexapdf/stream'
|
7
|
+
require 'hexapdf/type'
|
8
|
+
|
9
|
+
describe HexaPDF::DictionaryFields do
|
10
|
+
include HexaPDF::DictionaryFields
|
11
|
+
|
12
|
+
describe "Field" do
|
13
|
+
before do
|
14
|
+
@field = self.class::Field.new([:Integer, self.class::PDFByteString], required: true,
|
15
|
+
default: 500, indirect: false, allowed_values: [500, 1],
|
16
|
+
version: '1.2')
|
17
|
+
HexaPDF::GlobalConfiguration['object.type_map'][:Integer] = Integer
|
18
|
+
end
|
19
|
+
|
20
|
+
after do
|
21
|
+
HexaPDF::GlobalConfiguration['object.type_map'].delete(:Integer)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "allows access to the basic field information" do
|
25
|
+
assert(@field.required?)
|
26
|
+
assert(@field.default?)
|
27
|
+
assert_equal(500, @field.default)
|
28
|
+
assert_equal(false, @field.indirect)
|
29
|
+
assert_equal('1.2', @field.version)
|
30
|
+
assert_equal([500, 1], @field.allowed_values)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "maps string types to constants" do
|
34
|
+
assert_equal([Integer, self.class::PDFByteString, Hash, String], @field.type)
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "convert" do
|
38
|
+
it "returns the converted object, using the first usable converter" do
|
39
|
+
doc = Minitest::Mock.new
|
40
|
+
doc.expect(:wrap, :data, [Hash, Hash])
|
41
|
+
@field.convert({}, doc)
|
42
|
+
doc.verify
|
43
|
+
|
44
|
+
assert(@field.convert('str', self).encoding == Encoding::BINARY)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "returns nil for unconvertable objects" do
|
48
|
+
assert_nil(@field.convert(5.5, self))
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
it "can check for a valid object" do
|
53
|
+
refute(@field.valid_object?(5.5))
|
54
|
+
assert(@field.valid_object?(5))
|
55
|
+
assert(@field.valid_object?(HexaPDF::Object.new(5)))
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "DictionaryConverter" do
|
60
|
+
before do
|
61
|
+
@field = self.class::Field.new(Class.new(HexaPDF::Dictionary))
|
62
|
+
@doc = Minitest::Mock.new
|
63
|
+
end
|
64
|
+
|
65
|
+
it "additionally adds Hash as allowed type" do
|
66
|
+
assert(@field.type.include?(Hash))
|
67
|
+
end
|
68
|
+
|
69
|
+
it "allows conversion from a hash" do
|
70
|
+
@doc.expect(:wrap, :data, [Hash, Hash])
|
71
|
+
@field.convert({Test: :value}, @doc)
|
72
|
+
@doc.verify
|
73
|
+
end
|
74
|
+
|
75
|
+
it "allows conversion from a Dictionary" do
|
76
|
+
@doc.expect(:wrap, :data, [HexaPDF::Dictionary, Hash])
|
77
|
+
@field.convert(HexaPDF::Dictionary.new({Test: :value}), @doc)
|
78
|
+
@doc.verify
|
79
|
+
end
|
80
|
+
|
81
|
+
it "allows conversion from an HexaPDF::Dictionary to a Stream if stream data is set" do
|
82
|
+
@field = self.class::Field.new(HexaPDF::Stream)
|
83
|
+
@doc.expect(:wrap, :data, [HexaPDF::Dictionary, Hash])
|
84
|
+
data = HexaPDF::PDFData.new({}, 0, 0, "")
|
85
|
+
@field.convert(HexaPDF::Dictionary.new(data), @doc)
|
86
|
+
@doc.verify
|
87
|
+
end
|
88
|
+
|
89
|
+
it "doesn't allow conversion to a Stream subclass from Hash or Dictionary" do
|
90
|
+
@field = self.class::Field.new(HexaPDF::Stream)
|
91
|
+
refute(@field.convert({}, @doc))
|
92
|
+
refute(@field.convert(HexaPDF::Dictionary.new({Test: :value}), @doc))
|
93
|
+
end
|
94
|
+
|
95
|
+
it "doesn't allow conversion from nil" do
|
96
|
+
refute(@field.convert(nil, @doc))
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe "ArrayConverter" do
|
101
|
+
before do
|
102
|
+
@field = self.class::Field.new(HexaPDF::PDFArray)
|
103
|
+
@doc = Minitest::Mock.new
|
104
|
+
end
|
105
|
+
|
106
|
+
it "additionally adds Array as allowed type" do
|
107
|
+
assert(@field.type.include?(Array))
|
108
|
+
end
|
109
|
+
|
110
|
+
it "allows conversion from an array" do
|
111
|
+
@doc.expect(:wrap, :data, [[1, 2], {type: HexaPDF::PDFArray}])
|
112
|
+
@field.convert([1, 2], @doc)
|
113
|
+
@doc.verify
|
114
|
+
end
|
115
|
+
|
116
|
+
it "doesn't allow conversion from nil" do
|
117
|
+
refute(@field.convert(nil, @doc))
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
describe "StringConverter" do
|
122
|
+
before do
|
123
|
+
@field = self.class::Field.new(String)
|
124
|
+
end
|
125
|
+
|
126
|
+
it "allows conversion to UTF-8 string from binary" do
|
127
|
+
refute(@field.convert("test", self))
|
128
|
+
|
129
|
+
str = @field.convert("\xfe\xff\x00t\x00e\x00s\x00t".b, self)
|
130
|
+
assert_equal('test', str)
|
131
|
+
assert_equal(Encoding::UTF_8, str.encoding)
|
132
|
+
str = @field.convert("Testing\x9c\x92".b, self)
|
133
|
+
assert_equal("Testing\u0153\u2122", str)
|
134
|
+
assert_equal(Encoding::UTF_8, str.encoding)
|
135
|
+
end
|
136
|
+
|
137
|
+
def configuration
|
138
|
+
{'document.on_invalid_string' => proc {|str| str }}
|
139
|
+
end
|
140
|
+
|
141
|
+
it "calls document.on_invalid_string if the provided string is invalid" do
|
142
|
+
str = "\xfe\xff\xD8\x00\x00s\x00t".b
|
143
|
+
assert_equal("\xD8\x00\x00s\x00t".force_encoding("UTF-16BE"), @field.convert(str, self))
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
describe "PDFByteStringConverter" do
|
148
|
+
before do
|
149
|
+
@field = self.class::Field.new(self.class::PDFByteString)
|
150
|
+
end
|
151
|
+
|
152
|
+
it "additionally adds String as allowed type if not already present" do
|
153
|
+
assert_equal([HexaPDF::Dictionary::PDFByteString, String], @field.type)
|
154
|
+
end
|
155
|
+
|
156
|
+
it "allows conversion to a binary string" do
|
157
|
+
refute(@field.convert('test'.b, self))
|
158
|
+
|
159
|
+
str = @field.convert("test", self)
|
160
|
+
assert_equal('test', str)
|
161
|
+
assert_equal(Encoding::BINARY, str.encoding)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
describe "DateConverter" do
|
166
|
+
before do
|
167
|
+
@field = self.class::Field.new(self.class::PDFDate)
|
168
|
+
end
|
169
|
+
|
170
|
+
it "additionally adds String/Time/Date/DateTime as allowed types" do
|
171
|
+
assert_equal([HexaPDF::Dictionary::PDFDate, String, Time, Date, DateTime], @field.type)
|
172
|
+
end
|
173
|
+
|
174
|
+
it "allows conversion to a Time object from a binary string" do
|
175
|
+
refute(@field.convert('test'.b, self))
|
176
|
+
|
177
|
+
[
|
178
|
+
["D:1998", [1998, 01, 01, 00, 00, 00, "-00:00"]],
|
179
|
+
["D:199812", [1998, 12, 01, 00, 00, 00, "-00:00"]],
|
180
|
+
["D:19981223", [1998, 12, 23, 00, 00, 00, "-00:00"]],
|
181
|
+
["D:1998122319", [1998, 12, 23, 19, 00, 00, "+00:00"]],
|
182
|
+
["D:199812231952", [1998, 12, 23, 19, 52, 00, "+00:00"]],
|
183
|
+
["D:19981223195210", [1998, 12, 23, 19, 52, 10, "+00:00"]],
|
184
|
+
["D:19981223195210-08'00'", [1998, 12, 23, 19, 52, 10, "-08:00"]],
|
185
|
+
["D:1998122319-08'00'", [1998, 12, 23, 19, 00, 00, "-08:00"]],
|
186
|
+
["D:19981223-08'00'", [1998, 12, 23, 00, 00, 00, "-08:00"]],
|
187
|
+
["D:199812-08'00'", [1998, 12, 01, 00, 00, 00, "-08:00"]],
|
188
|
+
["D:1998-08'00'", [1998, 01, 01, 00, 00, 00, "-08:00"]],
|
189
|
+
["D:19981223195210-08", [1998, 12, 23, 19, 52, 10, "-08:00"]], # non-standard, missing '
|
190
|
+
["D:19981223195210-08'00", [1998, 12, 23, 19, 52, 10, "-08:00"]], # non-standard, missing '
|
191
|
+
].each do |str, data|
|
192
|
+
obj = @field.convert(str, self)
|
193
|
+
assert_equal(Time.new(*data), obj, "date str used: #{str}")
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
describe "FileSpecificationConverter" do
|
199
|
+
before do
|
200
|
+
@field = self.class::Field.new(:Filespec)
|
201
|
+
end
|
202
|
+
|
203
|
+
it "additionally adds Hash and String as allowed types" do
|
204
|
+
assert(@field.type.include?(Hash))
|
205
|
+
assert(@field.type.include?(String))
|
206
|
+
end
|
207
|
+
|
208
|
+
it "allows conversion from a string" do
|
209
|
+
@doc = Minitest::Mock.new
|
210
|
+
@doc.expect(:wrap, :data, [{F: 'test'}, {type: HexaPDF::Type::FileSpecification}])
|
211
|
+
@field.convert('test', @doc)
|
212
|
+
@doc.verify
|
213
|
+
end
|
214
|
+
|
215
|
+
it "allows conversion from a hash/dictionary" do
|
216
|
+
@doc = Minitest::Mock.new
|
217
|
+
@doc.expect(:wrap, :data, [{F: 'test'}, {type: HexaPDF::Type::FileSpecification}])
|
218
|
+
@field.convert({F: 'test'}, @doc)
|
219
|
+
@doc.verify
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
describe "RectangleConverter" do
|
224
|
+
before do
|
225
|
+
@field = self.class::Field.new(HexaPDF::Rectangle)
|
226
|
+
end
|
227
|
+
|
228
|
+
it "additionally adds Array as allowed types" do
|
229
|
+
assert_equal([HexaPDF::Rectangle, Array], @field.type)
|
230
|
+
end
|
231
|
+
|
232
|
+
it "allows conversion to a Rectangle from an Array" do
|
233
|
+
doc = Minitest::Mock.new
|
234
|
+
doc.expect(:wrap, :data, [[0, 1, 2, 3], {type: HexaPDF::Rectangle}])
|
235
|
+
@field.convert([0, 1, 2, 3], doc)
|
236
|
+
doc.verify
|
237
|
+
end
|
238
|
+
|
239
|
+
it "allows conversion to a Rectangle from a HexaPDF::PDFArray" do
|
240
|
+
data = HexaPDF::PDFArray.new([0, 1, 2, 3])
|
241
|
+
doc = Minitest::Mock.new
|
242
|
+
doc.expect(:wrap, :data, [data, {type: HexaPDF::Rectangle}])
|
243
|
+
@field.convert(data, doc)
|
244
|
+
doc.verify
|
245
|
+
end
|
246
|
+
|
247
|
+
it "converts to a null value if an (invalid) empty array is given" do
|
248
|
+
doc = Minitest::Mock.new
|
249
|
+
doc.expect(:wrap, :data, [nil])
|
250
|
+
@field.convert([], doc)
|
251
|
+
doc.verify
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
describe "IntegerConverter" do
|
256
|
+
before do
|
257
|
+
@field = self.class::Field.new(Integer)
|
258
|
+
end
|
259
|
+
|
260
|
+
it "no additional field types allowed" do
|
261
|
+
assert_equal([Integer], @field.type)
|
262
|
+
end
|
263
|
+
|
264
|
+
it "allows conversion to an Integer from an equivalent Float value" do
|
265
|
+
refute_same(3, @field.convert(3.1, nil))
|
266
|
+
assert_same(3, @field.convert(3.0, nil))
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|