hexapdf 0.17.0 → 0.18.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (269) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1065 -0
  3. data/LICENSE +29 -0
  4. data/README.md +129 -0
  5. data/Rakefile +109 -0
  6. data/agpl-3.0.txt +661 -0
  7. data/examples/001-hello_world.rb +16 -0
  8. data/examples/002-graphics.rb +275 -0
  9. data/examples/003-arcs.rb +50 -0
  10. data/examples/004-optimizing.rb +23 -0
  11. data/examples/005-merging.rb +27 -0
  12. data/examples/006-standard_pdf_fonts.rb +73 -0
  13. data/examples/007-truetype.rb +42 -0
  14. data/examples/008-show_char_bboxes.rb +55 -0
  15. data/examples/009-text_layouter_alignment.rb +47 -0
  16. data/examples/010-text_layouter_inline_boxes.rb +64 -0
  17. data/examples/011-text_layouter_line_wrapping.rb +57 -0
  18. data/examples/012-text_layouter_styling.rb +122 -0
  19. data/examples/013-text_layouter_shapes.rb +176 -0
  20. data/examples/014-text_in_polygon.rb +60 -0
  21. data/examples/015-boxes.rb +76 -0
  22. data/examples/016-frame_automatic_box_placement.rb +90 -0
  23. data/examples/017-frame_text_flow.rb +60 -0
  24. data/examples/018-composer.rb +44 -0
  25. data/examples/019-acro_form.rb +88 -0
  26. data/examples/emoji-smile.png +0 -0
  27. data/examples/emoji-wink.png +0 -0
  28. data/examples/machupicchu.jpg +0 -0
  29. data/lib/hexapdf/configuration.rb +9 -0
  30. data/lib/hexapdf/content/color_space.rb +19 -0
  31. data/lib/hexapdf/content/graphic_object/arc.rb +2 -2
  32. data/lib/hexapdf/content/graphic_object/endpoint_arc.rb +68 -2
  33. data/lib/hexapdf/content/graphic_object/geom2d.rb +13 -0
  34. data/lib/hexapdf/content/graphic_object/solid_arc.rb +111 -10
  35. data/lib/hexapdf/dictionary.rb +1 -1
  36. data/lib/hexapdf/dictionary_fields.rb +7 -2
  37. data/lib/hexapdf/encryption/security_handler.rb +3 -1
  38. data/lib/hexapdf/object.rb +18 -0
  39. data/lib/hexapdf/parser.rb +3 -0
  40. data/lib/hexapdf/serializer.rb +2 -0
  41. data/lib/hexapdf/type/acro_form/appearance_generator.rb +4 -4
  42. data/lib/hexapdf/type/acro_form/form.rb +39 -28
  43. data/lib/hexapdf/type/acro_form/variable_text_field.rb +56 -18
  44. data/lib/hexapdf/type/annotations/widget.rb +3 -15
  45. data/lib/hexapdf/version.rb +1 -1
  46. data/test/data/aes-test-vectors/CBCGFSbox-128-decrypt.data.gz +0 -0
  47. data/test/data/aes-test-vectors/CBCGFSbox-128-encrypt.data.gz +0 -0
  48. data/test/data/aes-test-vectors/CBCGFSbox-192-decrypt.data.gz +0 -0
  49. data/test/data/aes-test-vectors/CBCGFSbox-192-encrypt.data.gz +0 -0
  50. data/test/data/aes-test-vectors/CBCGFSbox-256-decrypt.data.gz +0 -0
  51. data/test/data/aes-test-vectors/CBCGFSbox-256-encrypt.data.gz +0 -0
  52. data/test/data/aes-test-vectors/CBCKeySbox-128-decrypt.data.gz +0 -0
  53. data/test/data/aes-test-vectors/CBCKeySbox-128-encrypt.data.gz +0 -0
  54. data/test/data/aes-test-vectors/CBCKeySbox-192-decrypt.data.gz +0 -0
  55. data/test/data/aes-test-vectors/CBCKeySbox-192-encrypt.data.gz +0 -0
  56. data/test/data/aes-test-vectors/CBCKeySbox-256-decrypt.data.gz +0 -0
  57. data/test/data/aes-test-vectors/CBCKeySbox-256-encrypt.data.gz +0 -0
  58. data/test/data/aes-test-vectors/CBCVarKey-128-decrypt.data.gz +0 -0
  59. data/test/data/aes-test-vectors/CBCVarKey-128-encrypt.data.gz +0 -0
  60. data/test/data/aes-test-vectors/CBCVarKey-192-decrypt.data.gz +0 -0
  61. data/test/data/aes-test-vectors/CBCVarKey-192-encrypt.data.gz +0 -0
  62. data/test/data/aes-test-vectors/CBCVarKey-256-decrypt.data.gz +0 -0
  63. data/test/data/aes-test-vectors/CBCVarKey-256-encrypt.data.gz +0 -0
  64. data/test/data/aes-test-vectors/CBCVarTxt-128-decrypt.data.gz +0 -0
  65. data/test/data/aes-test-vectors/CBCVarTxt-128-encrypt.data.gz +0 -0
  66. data/test/data/aes-test-vectors/CBCVarTxt-192-decrypt.data.gz +0 -0
  67. data/test/data/aes-test-vectors/CBCVarTxt-192-encrypt.data.gz +0 -0
  68. data/test/data/aes-test-vectors/CBCVarTxt-256-decrypt.data.gz +0 -0
  69. data/test/data/aes-test-vectors/CBCVarTxt-256-encrypt.data.gz +0 -0
  70. data/test/data/fonts/Ubuntu-Title.ttf +0 -0
  71. data/test/data/images/cmyk.jpg +0 -0
  72. data/test/data/images/fillbytes.jpg +0 -0
  73. data/test/data/images/gray.jpg +0 -0
  74. data/test/data/images/greyscale-1bit.png +0 -0
  75. data/test/data/images/greyscale-2bit.png +0 -0
  76. data/test/data/images/greyscale-4bit.png +0 -0
  77. data/test/data/images/greyscale-8bit.png +0 -0
  78. data/test/data/images/greyscale-alpha-8bit.png +0 -0
  79. data/test/data/images/greyscale-trns-8bit.png +0 -0
  80. data/test/data/images/greyscale-with-gamma1.0.png +0 -0
  81. data/test/data/images/greyscale-with-gamma1.5.png +0 -0
  82. data/test/data/images/indexed-1bit.png +0 -0
  83. data/test/data/images/indexed-2bit.png +0 -0
  84. data/test/data/images/indexed-4bit.png +0 -0
  85. data/test/data/images/indexed-8bit.png +0 -0
  86. data/test/data/images/indexed-alpha-4bit.png +0 -0
  87. data/test/data/images/indexed-alpha-8bit.png +0 -0
  88. data/test/data/images/rgb.jpg +0 -0
  89. data/test/data/images/truecolour-8bit.png +0 -0
  90. data/test/data/images/truecolour-alpha-8bit.png +0 -0
  91. data/test/data/images/truecolour-gama-chrm-8bit.png +0 -0
  92. data/test/data/images/truecolour-srgb-8bit.png +0 -0
  93. data/test/data/images/ycck.jpg +0 -0
  94. data/test/data/minimal.pdf +44 -0
  95. data/test/data/standard-security-handler/README +9 -0
  96. data/test/data/standard-security-handler/bothpwd-aes-128bit-V4.pdf +44 -0
  97. data/test/data/standard-security-handler/bothpwd-aes-256bit-V5.pdf +0 -0
  98. data/test/data/standard-security-handler/bothpwd-arc4-128bit-V2.pdf +43 -0
  99. data/test/data/standard-security-handler/bothpwd-arc4-128bit-V4.pdf +43 -0
  100. data/test/data/standard-security-handler/bothpwd-arc4-40bit-V1.pdf +0 -0
  101. data/test/data/standard-security-handler/nopwd-aes-128bit-V4.pdf +43 -0
  102. data/test/data/standard-security-handler/nopwd-aes-256bit-V5.pdf +0 -0
  103. data/test/data/standard-security-handler/nopwd-arc4-128bit-V2.pdf +43 -0
  104. data/test/data/standard-security-handler/nopwd-arc4-128bit-V4.pdf +43 -0
  105. data/test/data/standard-security-handler/nopwd-arc4-40bit-V1.pdf +43 -0
  106. data/test/data/standard-security-handler/ownerpwd-aes-128bit-V4.pdf +0 -0
  107. data/test/data/standard-security-handler/ownerpwd-aes-256bit-V5.pdf +43 -0
  108. data/test/data/standard-security-handler/ownerpwd-arc4-128bit-V2.pdf +43 -0
  109. data/test/data/standard-security-handler/ownerpwd-arc4-128bit-V4.pdf +43 -0
  110. data/test/data/standard-security-handler/ownerpwd-arc4-40bit-V1.pdf +43 -0
  111. data/test/data/standard-security-handler/userpwd-aes-128bit-V4.pdf +43 -0
  112. data/test/data/standard-security-handler/userpwd-aes-256bit-V5.pdf +43 -0
  113. data/test/data/standard-security-handler/userpwd-arc4-128bit-V2.pdf +0 -0
  114. data/test/data/standard-security-handler/userpwd-arc4-128bit-V4.pdf +0 -0
  115. data/test/data/standard-security-handler/userpwd-arc4-40bit-V1.pdf +43 -0
  116. data/test/hexapdf/common_tokenizer_tests.rb +236 -0
  117. data/test/hexapdf/content/common.rb +39 -0
  118. data/test/hexapdf/content/graphic_object/test_arc.rb +102 -0
  119. data/test/hexapdf/content/graphic_object/test_endpoint_arc.rb +98 -0
  120. data/test/hexapdf/content/graphic_object/test_geom2d.rb +79 -0
  121. data/test/hexapdf/content/graphic_object/test_solid_arc.rb +86 -0
  122. data/test/hexapdf/content/test_canvas.rb +1279 -0
  123. data/test/hexapdf/content/test_color_space.rb +202 -0
  124. data/test/hexapdf/content/test_graphics_state.rb +151 -0
  125. data/test/hexapdf/content/test_operator.rb +619 -0
  126. data/test/hexapdf/content/test_parser.rb +99 -0
  127. data/test/hexapdf/content/test_processor.rb +163 -0
  128. data/test/hexapdf/content/test_transformation_matrix.rb +64 -0
  129. data/test/hexapdf/document/test_files.rb +72 -0
  130. data/test/hexapdf/document/test_fonts.rb +60 -0
  131. data/test/hexapdf/document/test_images.rb +72 -0
  132. data/test/hexapdf/document/test_pages.rb +130 -0
  133. data/test/hexapdf/encryption/common.rb +87 -0
  134. data/test/hexapdf/encryption/test_aes.rb +129 -0
  135. data/test/hexapdf/encryption/test_arc4.rb +39 -0
  136. data/test/hexapdf/encryption/test_fast_aes.rb +17 -0
  137. data/test/hexapdf/encryption/test_fast_arc4.rb +12 -0
  138. data/test/hexapdf/encryption/test_identity.rb +21 -0
  139. data/test/hexapdf/encryption/test_ruby_aes.rb +23 -0
  140. data/test/hexapdf/encryption/test_ruby_arc4.rb +20 -0
  141. data/test/hexapdf/encryption/test_security_handler.rb +382 -0
  142. data/test/hexapdf/encryption/test_standard_security_handler.rb +322 -0
  143. data/test/hexapdf/filter/common.rb +53 -0
  144. data/test/hexapdf/filter/test_ascii85_decode.rb +59 -0
  145. data/test/hexapdf/filter/test_ascii_hex_decode.rb +38 -0
  146. data/test/hexapdf/filter/test_crypt.rb +21 -0
  147. data/test/hexapdf/filter/test_encryption.rb +24 -0
  148. data/test/hexapdf/filter/test_flate_decode.rb +44 -0
  149. data/test/hexapdf/filter/test_lzw_decode.rb +52 -0
  150. data/test/hexapdf/filter/test_predictor.rb +219 -0
  151. data/test/hexapdf/filter/test_run_length_decode.rb +32 -0
  152. data/test/hexapdf/font/cmap/test_parser.rb +102 -0
  153. data/test/hexapdf/font/cmap/test_writer.rb +66 -0
  154. data/test/hexapdf/font/encoding/test_base.rb +45 -0
  155. data/test/hexapdf/font/encoding/test_difference_encoding.rb +29 -0
  156. data/test/hexapdf/font/encoding/test_glyph_list.rb +59 -0
  157. data/test/hexapdf/font/encoding/test_zapf_dingbats_encoding.rb +16 -0
  158. data/test/hexapdf/font/test_cmap.rb +104 -0
  159. data/test/hexapdf/font/test_encoding.rb +27 -0
  160. data/test/hexapdf/font/test_invalid_glyph.rb +34 -0
  161. data/test/hexapdf/font/test_true_type_wrapper.rb +186 -0
  162. data/test/hexapdf/font/test_type1_wrapper.rb +107 -0
  163. data/test/hexapdf/font/true_type/common.rb +17 -0
  164. data/test/hexapdf/font/true_type/table/common.rb +27 -0
  165. data/test/hexapdf/font/true_type/table/test_cmap.rb +47 -0
  166. data/test/hexapdf/font/true_type/table/test_cmap_subtable.rb +141 -0
  167. data/test/hexapdf/font/true_type/table/test_directory.rb +30 -0
  168. data/test/hexapdf/font/true_type/table/test_glyf.rb +58 -0
  169. data/test/hexapdf/font/true_type/table/test_head.rb +56 -0
  170. data/test/hexapdf/font/true_type/table/test_hhea.rb +26 -0
  171. data/test/hexapdf/font/true_type/table/test_hmtx.rb +30 -0
  172. data/test/hexapdf/font/true_type/table/test_kern.rb +61 -0
  173. data/test/hexapdf/font/true_type/table/test_loca.rb +33 -0
  174. data/test/hexapdf/font/true_type/table/test_maxp.rb +50 -0
  175. data/test/hexapdf/font/true_type/table/test_name.rb +76 -0
  176. data/test/hexapdf/font/true_type/table/test_os2.rb +55 -0
  177. data/test/hexapdf/font/true_type/table/test_post.rb +78 -0
  178. data/test/hexapdf/font/true_type/test_builder.rb +42 -0
  179. data/test/hexapdf/font/true_type/test_font.rb +116 -0
  180. data/test/hexapdf/font/true_type/test_optimizer.rb +26 -0
  181. data/test/hexapdf/font/true_type/test_subsetter.rb +73 -0
  182. data/test/hexapdf/font/true_type/test_table.rb +48 -0
  183. data/test/hexapdf/font/type1/common.rb +6 -0
  184. data/test/hexapdf/font/type1/test_afm_parser.rb +65 -0
  185. data/test/hexapdf/font/type1/test_font.rb +104 -0
  186. data/test/hexapdf/font/type1/test_font_metrics.rb +22 -0
  187. data/test/hexapdf/font/type1/test_pfb_parser.rb +37 -0
  188. data/test/hexapdf/font_loader/test_from_configuration.rb +43 -0
  189. data/test/hexapdf/font_loader/test_from_file.rb +36 -0
  190. data/test/hexapdf/font_loader/test_standard14.rb +33 -0
  191. data/test/hexapdf/image_loader/test_jpeg.rb +93 -0
  192. data/test/hexapdf/image_loader/test_pdf.rb +47 -0
  193. data/test/hexapdf/image_loader/test_png.rb +259 -0
  194. data/test/hexapdf/layout/test_box.rb +154 -0
  195. data/test/hexapdf/layout/test_frame.rb +350 -0
  196. data/test/hexapdf/layout/test_image_box.rb +73 -0
  197. data/test/hexapdf/layout/test_inline_box.rb +71 -0
  198. data/test/hexapdf/layout/test_line.rb +206 -0
  199. data/test/hexapdf/layout/test_style.rb +790 -0
  200. data/test/hexapdf/layout/test_text_box.rb +140 -0
  201. data/test/hexapdf/layout/test_text_fragment.rb +375 -0
  202. data/test/hexapdf/layout/test_text_layouter.rb +758 -0
  203. data/test/hexapdf/layout/test_text_shaper.rb +62 -0
  204. data/test/hexapdf/layout/test_width_from_polygon.rb +109 -0
  205. data/test/hexapdf/task/test_dereference.rb +51 -0
  206. data/test/hexapdf/task/test_optimize.rb +162 -0
  207. data/test/hexapdf/test_composer.rb +258 -0
  208. data/test/hexapdf/test_configuration.rb +93 -0
  209. data/test/hexapdf/test_data_dir.rb +32 -0
  210. data/test/hexapdf/test_dictionary.rb +341 -0
  211. data/test/hexapdf/test_dictionary_fields.rb +269 -0
  212. data/test/hexapdf/test_document.rb +641 -0
  213. data/test/hexapdf/test_filter.rb +100 -0
  214. data/test/hexapdf/test_importer.rb +106 -0
  215. data/test/hexapdf/test_object.rb +286 -0
  216. data/test/hexapdf/test_parser.rb +656 -0
  217. data/test/hexapdf/test_pdf_array.rb +169 -0
  218. data/test/hexapdf/test_rectangle.rb +73 -0
  219. data/test/hexapdf/test_reference.rb +50 -0
  220. data/test/hexapdf/test_revision.rb +188 -0
  221. data/test/hexapdf/test_revisions.rb +196 -0
  222. data/test/hexapdf/test_serializer.rb +195 -0
  223. data/test/hexapdf/test_stream.rb +274 -0
  224. data/test/hexapdf/test_tokenizer.rb +80 -0
  225. data/test/hexapdf/test_type.rb +18 -0
  226. data/test/hexapdf/test_writer.rb +140 -0
  227. data/test/hexapdf/test_xref_section.rb +61 -0
  228. data/test/hexapdf/type/acro_form/test_appearance_generator.rb +802 -0
  229. data/test/hexapdf/type/acro_form/test_button_field.rb +308 -0
  230. data/test/hexapdf/type/acro_form/test_choice_field.rb +220 -0
  231. data/test/hexapdf/type/acro_form/test_field.rb +259 -0
  232. data/test/hexapdf/type/acro_form/test_form.rb +364 -0
  233. data/test/hexapdf/type/acro_form/test_signature_field.rb +38 -0
  234. data/test/hexapdf/type/acro_form/test_text_field.rb +201 -0
  235. data/test/hexapdf/type/acro_form/test_variable_text_field.rb +98 -0
  236. data/test/hexapdf/type/actions/test_launch.rb +24 -0
  237. data/test/hexapdf/type/actions/test_uri.rb +23 -0
  238. data/test/hexapdf/type/annotations/test_markup_annotation.rb +22 -0
  239. data/test/hexapdf/type/annotations/test_text.rb +34 -0
  240. data/test/hexapdf/type/annotations/test_widget.rb +225 -0
  241. data/test/hexapdf/type/test_annotation.rb +97 -0
  242. data/test/hexapdf/type/test_catalog.rb +48 -0
  243. data/test/hexapdf/type/test_cid_font.rb +61 -0
  244. data/test/hexapdf/type/test_file_specification.rb +141 -0
  245. data/test/hexapdf/type/test_font.rb +67 -0
  246. data/test/hexapdf/type/test_font_descriptor.rb +61 -0
  247. data/test/hexapdf/type/test_font_simple.rb +176 -0
  248. data/test/hexapdf/type/test_font_true_type.rb +31 -0
  249. data/test/hexapdf/type/test_font_type0.rb +120 -0
  250. data/test/hexapdf/type/test_font_type1.rb +142 -0
  251. data/test/hexapdf/type/test_font_type3.rb +26 -0
  252. data/test/hexapdf/type/test_form.rb +120 -0
  253. data/test/hexapdf/type/test_image.rb +261 -0
  254. data/test/hexapdf/type/test_info.rb +9 -0
  255. data/test/hexapdf/type/test_object_stream.rb +117 -0
  256. data/test/hexapdf/type/test_page.rb +598 -0
  257. data/test/hexapdf/type/test_page_tree_node.rb +315 -0
  258. data/test/hexapdf/type/test_resources.rb +209 -0
  259. data/test/hexapdf/type/test_trailer.rb +116 -0
  260. data/test/hexapdf/type/test_xref_stream.rb +143 -0
  261. data/test/hexapdf/utils/test_bit_field.rb +63 -0
  262. data/test/hexapdf/utils/test_bit_stream.rb +69 -0
  263. data/test/hexapdf/utils/test_graphics_helpers.rb +37 -0
  264. data/test/hexapdf/utils/test_lru_cache.rb +22 -0
  265. data/test/hexapdf/utils/test_object_hash.rb +120 -0
  266. data/test/hexapdf/utils/test_pdf_doc_encoding.rb +18 -0
  267. data/test/hexapdf/utils/test_sorted_tree_node.rb +239 -0
  268. data/test/test_helper.rb +58 -0
  269. metadata +263 -3
@@ -0,0 +1,88 @@
1
+ # # PDF Forms
2
+ #
3
+ # PDF files can be used for interactive forms, containing various types of form
4
+ # fields. HexaPDF supports the creation and processing of these forms.
5
+ #
6
+ # This example show-cases how to create the various form field types and their
7
+ # possible standard appearances.
8
+ #
9
+ # Usage:
10
+ # : `ruby acro_form.rb`
11
+ #
12
+
13
+ require 'hexapdf'
14
+
15
+ doc = HexaPDF::Document.new
16
+ page = doc.pages.add
17
+ canvas = page.canvas
18
+
19
+ canvas.font("Helvetica", size: 36)
20
+ canvas.text("Form Example", at: [50, 750])
21
+ form = doc.acro_form(create: true)
22
+
23
+ canvas.font_size(16)
24
+ canvas.text("Check boxes", at: [50, 650])
25
+ [:check, :circle, :cross, :diamond, :square, :star].each_with_index do |symbol, index|
26
+ cb = form.create_check_box("Checkbox #{index}")
27
+ widget = cb.create_widget(page, Rect: [200 + 50 * index, 640, 240 + 50 * index, 680])
28
+ widget.background_color(1 - 0.05 * index)
29
+ widget.marker_style(style: symbol, color: [0.166 * index, 0, 1 - 0.166 * index],
30
+ size: 7 * index)
31
+ cb.field_value = true
32
+ end
33
+
34
+ canvas.text("Radio buttons", at: [50, 550])
35
+ rb = form.create_radio_button("Radio")
36
+ [:check, :circle, :cross, :diamond, :square, :star].each_with_index do |symbol, index|
37
+ widget = rb.create_widget(page, value: :"button#{index}",
38
+ Rect: [200 + 50 * index, 540, 240 + 50 * index, 580])
39
+ widget.background_color(1 - 0.05 * index)
40
+ widget.marker_style(style: symbol, color: [0.166 * index, 0, 1 - 0.166 * index],
41
+ size: 7 * index)
42
+ end
43
+ rb.field_value = :button0
44
+
45
+ canvas.text("Text fields", at: [50, 450])
46
+
47
+ canvas.text("Single line", at: [70, 420])
48
+ tx = form.create_text_field("Single Line", font_size: 16)
49
+ widget = tx.create_widget(page, Rect: [200, 415, 500, 435])
50
+ tx.field_value = "A sample test string!"
51
+
52
+ canvas.text("Multiline", at: [70, 390])
53
+ tx = form.create_multiline_text_field("Multiline", font_size: 0, align: :right)
54
+ widget = tx.create_widget(page, Rect: [200, 325, 500, 405])
55
+ widget.border_style(color: 0, width: 1)
56
+ tx.field_value = "A sample test string! " * 30 + "\nNew line\n\nAnother line"
57
+
58
+ canvas.text("Password", at: [70, 300])
59
+ tx = form.create_password_field("Password", font_size: 16)
60
+ widget = tx.create_widget(page, Rect: [200, 295, 500, 315])
61
+
62
+ canvas.text("File select", at: [70, 270])
63
+ tx = form.create_file_select_field("File Select", font_size: 16)
64
+ widget = tx.create_widget(page, Rect: [200, 265, 500, 285])
65
+ tx.field_value = "path/to/file.pdf"
66
+
67
+ canvas.text("Comb", at: [70, 240])
68
+ tx = form.create_comb_text_field("Comb field", max_chars: 10, font_size: 16, align: :center)
69
+ widget = tx.create_widget(page, Rect: [200, 220, 500, 255])
70
+ widget.border_style(color: [30, 128, 0], width: 1)
71
+ tx.field_value = 'Hello'
72
+
73
+ canvas.text("Combo Box", at: [50, 170])
74
+ cb = form.create_combo_box("Combo Box", font_size: 12, editable: true,
75
+ option_items: ['Value 1', 'Another value', 'Choose me!'])
76
+ widget = cb.create_widget(page, Rect: [200, 150, 500, 185])
77
+ widget.border_style(width: 1)
78
+ cb.field_value = 'Another value'
79
+
80
+ canvas.text("List Box", at: [50, 120])
81
+ lb = form.create_list_box("List Box", font_size: 15, align: :center, multi_select: true,
82
+ option_items: 1.upto(7).map {|i| "Value #{i}" })
83
+ widget = lb.create_widget(page, Rect: [200, 50, 500, 135])
84
+ widget.border_style(width: 1)
85
+ lb.list_box_top_index = 1
86
+ lb.field_value = ['Value 6', 'Value 2']
87
+
88
+ doc.write('acro_form.pdf', optimize: true)
Binary file
Binary file
Binary file
@@ -203,6 +203,12 @@ module HexaPDF
203
203
  #
204
204
  # In nearly all cases this option should not be changed from its default setting!
205
205
  #
206
+ # document.on_invalid_string::
207
+ # A callable object that takes the invalid UTF-16BE encoded string and returns a valid UTF-8
208
+ # encoded string.
209
+ #
210
+ # The default is to remove all invalid characters.
211
+ #
206
212
  # encryption.aes::
207
213
  # The class that should be used for AES encryption. If the value is a String, it should
208
214
  # contain the name of a constant to such a class.
@@ -380,6 +386,9 @@ module HexaPDF
380
386
  end,
381
387
  'acro_form.text_field.default_width' => 100,
382
388
  'document.auto_decrypt' => true,
389
+ 'document.on_invalid_string' => proc do |str|
390
+ str.encode(Encoding::UTF_8, invalid: :replace, replace: '')
391
+ end,
383
392
  'encryption.aes' => 'HexaPDF::Encryption::FastAES',
384
393
  'encryption.arc4' => 'HexaPDF::Encryption::FastARC4',
385
394
  'encryption.filter_map' => {
@@ -35,7 +35,9 @@
35
35
  #++
36
36
 
37
37
  require 'hexapdf/error'
38
+ require 'hexapdf/content'
38
39
  require 'hexapdf/configuration'
40
+ require 'hexapdf/serializer'
39
41
 
40
42
  module HexaPDF
41
43
  module Content
@@ -304,6 +306,23 @@ module HexaPDF
304
306
  GlobalConfiguration.constantize('color_space.map', for_components(spec)).new.color(*spec)
305
307
  end
306
308
 
309
+ # Serializes the given device color into the form expected by PDF content streams.
310
+ #
311
+ # The +type+ argument can either be :stroke to serialize as stroke color operator or :fill as
312
+ # fill color operator.
313
+ def self.serialize_device_color(color, type: :fill)
314
+ operator = case color.color_space.family
315
+ when :DeviceRGB then :rg
316
+ when :DeviceGray then :g
317
+ when :DeviceCMYK then :k
318
+ else
319
+ raise ArgumentError, "Device color object expected, got #{color.class}"
320
+ end
321
+ operator = operator.upcase if type == :stroke
322
+ Content::Operator::DEFAULT_OPERATORS[operator].
323
+ serialize(HexaPDF::Serializer.new, *color.components)
324
+ end
325
+
307
326
  # Returns a device color object for the given components array without applying value
308
327
  # normalization.
309
328
  def self.prenormalized_device_color(components)
@@ -51,8 +51,8 @@ module HexaPDF
51
51
  # Examples:
52
52
  #
53
53
  # #>pdf-center
54
- # arc = canvas.graphic_object(:arc, a: 100, b: 50).stroke
55
- # arc.draw(canvas).stroke # or: canvas.draw(arc).stroke
54
+ # arc = canvas.graphic_object(:arc, a: 100, b: 50, end_angle: 150)
55
+ # canvas.draw(arc).stroke
56
56
  #
57
57
  # See: ELL - https://spaceroots.org/documents/ellipse/elliptical-arc.pdf
58
58
  class Arc
@@ -43,6 +43,15 @@ module HexaPDF
43
43
  # This class describes an elliptical arc in endpoint parameterization. It allows one to
44
44
  # generate an arc from the current point to a given point, similar to Content::Canvas#line_to.
45
45
  #
46
+ # This graphic object is registered under the :endpoint_arc key for use with the
47
+ # HexaPDF::Content::Canvas class.
48
+ #
49
+ # Examples:
50
+ #
51
+ # #>pdf-center
52
+ # arc = canvas.graphic_object(:endpoint_arc, x: 50, y: 20, a: 30, b: 10)
53
+ # canvas.move_to(0, 0).draw(arc).stroke
54
+ #
46
55
  # See: GraphicObject::Arc, ARC - https://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
47
56
  class EndpointArc
48
57
 
@@ -58,29 +67,86 @@ module HexaPDF
58
67
  end
59
68
 
60
69
  # x-coordinate of endpoint
70
+ #
71
+ # Examples:
72
+ #
73
+ # #>pdf-center
74
+ # arc = canvas.graphic_object(:endpoint_arc, x: 50, y: 20, a: 30, b: 20)
75
+ # canvas.move_to(0, 0).draw(arc).stroke
76
+ # canvas.stroke_color("red").move_to(0, 0).draw(arc, x: -50).stroke
61
77
  attr_reader :x
62
78
 
63
79
  # y-coordinate of endpoint
80
+ #
81
+ # Examples:
82
+ #
83
+ # #>pdf-center
84
+ # arc = canvas.graphic_object(:endpoint_arc, x: 50, y: 20, a: 30, b: 20)
85
+ # canvas.move_to(0, 0).draw(arc).stroke
86
+ # canvas.stroke_color("red").move_to(0, 0).draw(arc, y: -20).stroke
64
87
  attr_reader :y
65
88
 
66
89
  # Length of semi-major axis
90
+ #
91
+ # Examples:
92
+ #
93
+ # #>pdf-center
94
+ # arc = canvas.graphic_object(:endpoint_arc, x: 50, y: 20, a: 30, b: 20)
95
+ # canvas.move_to(0, 0).draw(arc).stroke
96
+ # canvas.stroke_color("red").move_to(0, 0).draw(arc, a: 40).stroke
67
97
  attr_reader :a
68
98
 
69
99
  # Length of semi-minor axis
100
+ #
101
+ # Examples:
102
+ #
103
+ # #>pdf-center
104
+ # arc = canvas.graphic_object(:endpoint_arc, x: 50, y: 20, a: 30, b: 20)
105
+ # canvas.move_to(0, 0).draw(arc).stroke
106
+ # canvas.stroke_color("red").move_to(0, 0).draw(arc, b: 50).stroke
70
107
  attr_reader :b
71
108
 
72
109
  # Inclination in degrees of semi-major axis in respect to x-axis
110
+ #
111
+ # Examples:
112
+ #
113
+ # #>pdf-center
114
+ # arc = canvas.graphic_object(:endpoint_arc, x: 50, y: 20, a: 30, b: 20)
115
+ # canvas.move_to(0, 0).draw(arc).stroke
116
+ # canvas.stroke_color("red").move_to(0, 0).draw(arc, inclination: 45).stroke
73
117
  attr_reader :inclination
74
118
 
75
119
  # Large arc choice - if +true+ use the large arc (i.e. the one spanning more than 180
76
120
  # degrees), else the small arc
121
+ #
122
+ # Examples:
123
+ #
124
+ # #>pdf-center
125
+ # arc = canvas.graphic_object(:endpoint_arc, x: 50, y: 20, a: 30, b: 20)
126
+ # canvas.move_to(0, 0).draw(arc).stroke
127
+ # canvas.stroke_color("red").move_to(0, 0).draw(arc, large_arc: false, clockwise: true).stroke
77
128
  attr_reader :large_arc
78
129
 
79
130
  # Direction of arc - if +true+ in clockwise direction, else in counterclockwise direction
131
+ #
132
+ # This is needed, for example, when filling paths using the nonzero winding number rule to achieve
133
+ # different effects.
134
+ #
135
+ # Examples:
136
+ #
137
+ # #>pdf-center
138
+ # arc = canvas.graphic_object(:endpoint_arc, x: 50, y: 20, a: 30, b: 20)
139
+ # canvas.move_to(0, 0).draw(arc).stroke
140
+ # canvas.stroke_color("red").move_to(0, 0).draw(arc, clockwise: true).stroke
80
141
  attr_reader :clockwise
81
142
 
82
143
  # Creates an endpoint arc with default values x=0, y=0, a=0, b=0, inclination=0,
83
144
  # large_arc=true, clockwise=false (a line to the origin).
145
+ #
146
+ # Examples:
147
+ #
148
+ # #>pdf-center
149
+ # canvas.move_to(30, 30).draw(:endpoint_arc).stroke
84
150
  def initialize
85
151
  @x = @y = 0
86
152
  @a = @b = 0
@@ -185,10 +251,10 @@ module HexaPDF
185
251
  cy = sin_theta * cxp + cos_theta * cyp + (y1 + y2) / 2.0
186
252
 
187
253
  # F.6.5.5
188
- start_angle = compute_angle_to_x_axis((x1p - cxp) / rx, (y1p - cyp) / ry)
254
+ start_angle = compute_angle_to_x_axis((x1p - cxp), (y1p - cyp)) % 360
189
255
 
190
256
  # F.6.5.6 (modified bc we just need the end angle)
191
- end_angle = compute_angle_to_x_axis((-x1p - cxp) / rx, (-y1p - cyp) / ry)
257
+ end_angle = compute_angle_to_x_axis((-x1p - cxp), (-y1p - cyp)) % 360
192
258
 
193
259
  {cx: cx, cy: cy, a: rx, b: ry, start_angle: start_angle, end_angle: end_angle,
194
260
  inclination: @inclination, clockwise: @clockwise}
@@ -57,9 +57,22 @@ module HexaPDF
57
57
  attr_accessor :object
58
58
 
59
59
  # The radius to use when drawing Geom2D::Point objects; defaults to 1
60
+ #
61
+ # Examples:
62
+ #
63
+ # #>pdf-center
64
+ # canvas.draw(:geom2d, object: ::Geom2D::Point(0, 0))
65
+ # canvas.draw(:geom2d, object: ::Geom2D::Point(50, 0), point_radius: 5)
60
66
  attr_accessor :point_radius
61
67
 
62
68
  # Specifies whether only paths should be drawn or if they should be stroked/filled too
69
+ # (default).
70
+ #
71
+ # Examples:
72
+ #
73
+ # #>pdf-center
74
+ # canvas.draw(:geom2d, object: ::Geom2D::Segment([0, 0], [0, 50]))
75
+ # canvas.draw(:geom2d, object: ::Geom2D::Segment([0, 0], [50, 0]), path_only: true)
63
76
  attr_accessor :path_only
64
77
 
65
78
  # Creates a Geom2D drawing support object.
@@ -41,20 +41,45 @@ module HexaPDF
41
41
  # This graphic object represents a solid elliptical arc, i.e. an arc that has an inner and
42
42
  # an outer set of a/b values.
43
43
  #
44
+ # This graphic object is registered under the :solid_arc key for use with the
45
+ # HexaPDF::Content::Canvas class.
46
+ #
44
47
  # Thus it can be used to create
45
48
  #
46
- # * an (elliptical) disk (when the inner a/b are zero and the difference between start and
49
+ # * an (*elliptical*) *disk* (when the inner a/b are zero and the difference between start and
47
50
  # end angles is greater than or equal to 360),
48
51
  #
49
- # * an (elliptical) sector (when the inner a/b are zero and the difference between start
52
+ # #>pdf-center
53
+ # canvas.fill_color("red").
54
+ # draw(:solid_arc, outer_a: 80, outer_b: 50, end_angle: 360).
55
+ # fill_stroke
56
+ #
57
+ # * an (*elliptical*) *sector* (when the inner a/b are zero and the difference between start
50
58
  # and end angles is less than 360),
51
59
  #
52
- # * an (elliptical) annulus (when the inner a/b are nonzero and the difference between
60
+ # #>pdf-center
61
+ # canvas.fill_color("red").
62
+ # draw(:solid_arc, outer_a: 80, outer_b: 50, start_angle: 20, end_angle: 230).
63
+ # fill_stroke
64
+ #
65
+ # * an (*elliptical*) *annulus* (when the inner a/b are nonzero and the difference between
53
66
  # start and end angles is greater than or equal to 360), and
54
67
  #
55
- # * an (elliptical) annular sector (when the inner a/b are nonzero and the difference
68
+ # #>pdf-center
69
+ # canvas.fill_color("red").
70
+ # draw(:solid_arc, outer_a: 80, outer_b: 50, inner_a: 70, inner_b: 30,
71
+ # end_angle: 360).
72
+ # fill_stroke
73
+ #
74
+ # * an (*elliptical*) *annular sector* (when the inner a/b are nonzero and the difference
56
75
  # between start and end angles is less than 360)
57
76
  #
77
+ # #>pdf-center
78
+ # canvas.fill_color("red").
79
+ # draw(:solid_arc, outer_a: 80, outer_b: 50, inner_a: 70, inner_b: 30,
80
+ # start_angle: 20, end_angle: 230).
81
+ # fill_stroke
82
+ #
58
83
  # See: Arc
59
84
  class SolidArc
60
85
 
@@ -66,30 +91,106 @@ module HexaPDF
66
91
  end
67
92
 
68
93
  # x-coordinate of center point
94
+ #
95
+ # Examples:
96
+ #
97
+ # #>pdf-center
98
+ # solid_arc = canvas.graphic_object(:solid_arc, outer_a: 30, outer_b: 20,
99
+ # inner_a: 20, inner_b: 10)
100
+ # canvas.draw(solid_arc).stroke
101
+ # canvas.stroke_color("red").draw(solid_arc, cx: 50).stroke
69
102
  attr_reader :cx
70
103
 
71
104
  # y-coordinate of center point
105
+ #
106
+ # Examples:
107
+ #
108
+ # #>pdf-center
109
+ # solid_arc = canvas.graphic_object(:solid_arc, outer_a: 30, outer_b: 20,
110
+ # inner_a: 20, inner_b: 10)
111
+ # canvas.draw(solid_arc).stroke
112
+ # canvas.stroke_color("red").draw(solid_arc, cy: 50).stroke
72
113
  attr_reader :cy
73
114
 
74
- # Length of inner semi-major axis
115
+ # Length of inner semi-major axis which (without altering the #inclination) is parallel to
116
+ # the x-axis
117
+ #
118
+ # Examples:
119
+ #
120
+ # #>pdf-center
121
+ # solid_arc = canvas.graphic_object(:solid_arc, outer_a: 30, outer_b: 20,
122
+ # inner_a: 20, inner_b: 10)
123
+ # canvas.draw(solid_arc).stroke
124
+ # canvas.stroke_color("red").draw(solid_arc, inner_a: 5).stroke
75
125
  attr_reader :inner_a
76
126
 
77
- # Length of inner semi-minor axis
127
+ # Length of inner semi-minor axis which (without altering the #inclination) is parallel to the
128
+ # y-axis
129
+ #
130
+ # Examples:
131
+ #
132
+ # #>pdf-center
133
+ # solid_arc = canvas.graphic_object(:solid_arc, outer_a: 30, outer_b: 20,
134
+ # inner_a: 20, inner_b: 10)
135
+ # canvas.draw(solid_arc).stroke
136
+ # canvas.stroke_color("red").draw(solid_arc, inner_b: 20).stroke
78
137
  attr_reader :inner_b
79
138
 
80
- # Length of outer semi-major axis
139
+ # Length of outer semi-major axis which (without altering the #inclination) is parallel to
140
+ # the x-axis
141
+ #
142
+ # Examples:
143
+ #
144
+ # #>pdf-center
145
+ # solid_arc = canvas.graphic_object(:solid_arc, outer_a: 30, outer_b: 20,
146
+ # inner_a: 20, inner_b: 10)
147
+ # canvas.draw(solid_arc).stroke
148
+ # canvas.stroke_color("red").draw(solid_arc, outer_a: 45).stroke
81
149
  attr_reader :outer_a
82
150
 
83
- # Length of outer semi-minor axis
151
+ # Length of outer semi-minor axis which (without altering the #inclination) is parallel to the
152
+ # y-axis
153
+ #
154
+ # Examples:
155
+ #
156
+ # #>pdf-center
157
+ # solid_arc = canvas.graphic_object(:solid_arc, outer_a: 30, outer_b: 20,
158
+ # inner_a: 20, inner_b: 10)
159
+ # canvas.draw(solid_arc).stroke
160
+ # canvas.stroke_color("red").draw(solid_arc, outer_b: 40).stroke
84
161
  attr_reader :outer_b
85
162
 
86
- # Start angle in degrees
163
+ # Start angle of the solid arc in degrees
164
+ #
165
+ # Examples:
166
+ #
167
+ # #>pdf-center
168
+ # solid_arc = canvas.graphic_object(:solid_arc, outer_a: 30, outer_b: 20,
169
+ # inner_a: 20, inner_b: 10)
170
+ # canvas.draw(solid_arc).stroke
171
+ # canvas.stroke_color("red").draw(solid_arc, start_angle: 60).stroke
87
172
  attr_reader :start_angle
88
173
 
89
- # End angle in degrees
174
+ # End angle of the solid arc in degrees
175
+ #
176
+ # Examples:
177
+ #
178
+ # #>pdf-center
179
+ # solid_arc = canvas.graphic_object(:solid_arc, outer_a: 30, outer_b: 20,
180
+ # inner_a: 20, inner_b: 10)
181
+ # canvas.draw(solid_arc).stroke
182
+ # canvas.stroke_color("red").draw(solid_arc, end_angle: 120).stroke
90
183
  attr_reader :end_angle
91
184
 
92
185
  # Inclination in degrees of semi-major axis in respect to x-axis
186
+ #
187
+ # Examples:
188
+ #
189
+ # #>pdf-center
190
+ # solid_arc = canvas.graphic_object(:solid_arc, outer_a: 30, outer_b: 20,
191
+ # inner_a: 20, inner_b: 10)
192
+ # canvas.draw(solid_arc).stroke
193
+ # canvas.stroke_color("red").draw(solid_arc, inclination: 40).stroke
93
194
  attr_reader :inclination
94
195
 
95
196
  # Creates a solid arc with default values (a unit disk at the origin).
@@ -318,8 +318,8 @@ module HexaPDF
318
318
  value[name] = document.add(obj)
319
319
  elsif !field.indirect && obj.kind_of?(HexaPDF::Object) && obj.indirect?
320
320
  yield("Field #{name} needs to be a direct object", true)
321
- document.delete(obj)
322
321
  value[name] = obj.value
322
+ document.delete(obj)
323
323
  end
324
324
  end
325
325
  end
@@ -233,11 +233,16 @@ module HexaPDF
233
233
 
234
234
  # Converts the string into UTF-8 encoding, assuming it is a binary string. Otherwise +nil+ is
235
235
  # returned.
236
- def self.convert(str, _type, _document)
236
+ def self.convert(str, _type, document)
237
237
  return unless str.kind_of?(String) && str.encoding == Encoding::BINARY
238
238
 
239
239
  if str.getbyte(0) == 254 && str.getbyte(1) == 255
240
- str[2..-1].force_encoding(Encoding::UTF_16BE).encode(Encoding::UTF_8)
240
+ str = str[2..-1].force_encoding(Encoding::UTF_16BE)
241
+ if str.valid_encoding?
242
+ str.encode!(Encoding::UTF_8)
243
+ else
244
+ document.configuration['document.on_invalid_string'].call(str)
245
+ end
241
246
  else
242
247
  Utils::PDFDocEncoding.convert_to_utf8(str)
243
248
  end
@@ -213,7 +213,9 @@ module HexaPDF
213
213
  end
214
214
 
215
215
  handler = handler.new(document)
216
- document.trailer[:Encrypt] = handler.set_up_decryption(dict, **options)
216
+ dict = document.trailer[:Encrypt] = handler.set_up_decryption(dict, **options)
217
+ HexaPDF::Object.make_direct(dict.value)
218
+ document.revisions.current.update(dict)
217
219
  document.revisions.each do |r|
218
220
  loader = r.loader
219
221
  r.loader = lambda do |xref_entry|
@@ -141,6 +141,24 @@ module HexaPDF
141
141
  end
142
142
  end
143
143
 
144
+ # Makes sure that the object itself as well as all nested values are direct objects.
145
+ #
146
+ # If an indirect object is found, it is turned into a direct object and the indirect object is
147
+ # deleted from the document.
148
+ def self.make_direct(object)
149
+ if object.kind_of?(HexaPDF::Object) && object.indirect?
150
+ object_to_delete = object
151
+ object = object.value
152
+ object_to_delete.document.delete(object_to_delete)
153
+ end
154
+ if object.kind_of?(Hash)
155
+ object.transform_values! {|val| make_direct(val) }
156
+ elsif object.kind_of?(Array)
157
+ object.map! {|val| make_direct(val) }
158
+ end
159
+ object
160
+ end
161
+
144
162
  # The wrapped HexaPDF::PDFData value.
145
163
  #
146
164
  # This attribute is not part of the public API!
@@ -404,6 +404,7 @@ module HexaPDF
404
404
  def reconstruct_revision
405
405
  return if @in_reconstruct_revision
406
406
  @in_reconstruct_revision = true
407
+ @header_offset = 0
407
408
 
408
409
  raise unless @document.config['parser.try_xref_reconstruction']
409
410
  msg = "#{$!} - trying cross-reference table reconstruction"
@@ -428,8 +429,10 @@ module HexaPDF
428
429
  elsif gen.kind_of?(Integer) && tok.kind_of?(Tokenizer::Token) && tok == 'obj'
429
430
  xref.add_in_use_entry(token, gen, pos)
430
431
  if linearized.nil?
432
+ pos = @tokenizer.pos
431
433
  obj = @tokenizer.next_object rescue nil
432
434
  linearized = obj.kind_of?(Hash) && obj.key?(:Linearized)
435
+ @tokenizer.pos = pos
433
436
  end
434
437
  @tokenizer.scan_until(/(?:\n|\r\n?)endobj\b/)
435
438
  end
@@ -35,6 +35,8 @@
35
35
  #++
36
36
 
37
37
  require 'time'
38
+ require 'hexapdf/object'
39
+ require 'hexapdf/stream'
38
40
  require 'hexapdf/tokenizer'
39
41
  require 'hexapdf/filter'
40
42
  require 'hexapdf/utils/lru_cache'
@@ -227,8 +227,8 @@ module HexaPDF
227
227
  # Note: Rich text fields are currently not supported!
228
228
  def create_text_appearances
229
229
  default_resources = @document.acro_form.default_resources
230
- font, font_size = retrieve_font_information(default_resources)
231
- style = HexaPDF::Layout::Style.new(font: font)
230
+ font, font_size, font_color = retrieve_font_information(default_resources)
231
+ style = HexaPDF::Layout::Style.new(font: font, fill_color: font_color)
232
232
  border_style = @widget.border_style
233
233
  padding = [1, border_style.width].max
234
234
 
@@ -482,7 +482,7 @@ module HexaPDF
482
482
 
483
483
  # Returns the font wrapper and font size to be used for a variable text field.
484
484
  def retrieve_font_information(resources)
485
- font_name, font_size = @field.parse_default_appearance_string
485
+ font_name, font_size, font_color = @field.parse_default_appearance_string
486
486
  font_object = resources.font(font_name) rescue nil
487
487
  font = font_object&.font_wrapper
488
488
  unless font
@@ -498,7 +498,7 @@ module HexaPDF
498
498
  raise(HexaPDF::Error, "Font #{font_name} of the AcroForm's default resources not usable")
499
499
  end
500
500
  end
501
- [font, font_size]
501
+ [font, font_size, font_color]
502
502
  end
503
503
 
504
504
  # Calculates the font size for text fields based on the font and font size of the default