hexapdf 1.1.1 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +79 -0
- data/README.md +1 -1
- data/lib/hexapdf/cli/command.rb +63 -63
- data/lib/hexapdf/cli/inspect.rb +14 -5
- data/lib/hexapdf/cli/modify.rb +0 -1
- data/lib/hexapdf/cli/optimize.rb +5 -5
- data/lib/hexapdf/composer.rb +14 -0
- data/lib/hexapdf/configuration.rb +26 -0
- data/lib/hexapdf/content/graphics_state.rb +1 -1
- data/lib/hexapdf/digital_signature/signing/signed_data_creator.rb +1 -1
- data/lib/hexapdf/document/annotations.rb +173 -0
- data/lib/hexapdf/document/layout.rb +45 -6
- data/lib/hexapdf/document.rb +28 -7
- data/lib/hexapdf/error.rb +11 -3
- data/lib/hexapdf/font/true_type/subsetter.rb +15 -2
- data/lib/hexapdf/font/true_type_wrapper.rb +1 -0
- data/lib/hexapdf/font/type1_wrapper.rb +1 -0
- data/lib/hexapdf/layout/style.rb +101 -7
- data/lib/hexapdf/object.rb +2 -2
- data/lib/hexapdf/pdf_array.rb +25 -3
- data/lib/hexapdf/tokenizer.rb +4 -1
- data/lib/hexapdf/type/acro_form/appearance_generator.rb +57 -8
- data/lib/hexapdf/type/acro_form/field.rb +1 -0
- data/lib/hexapdf/type/acro_form/form.rb +7 -6
- data/lib/hexapdf/type/acro_form/java_script_actions.rb +9 -2
- data/lib/hexapdf/type/acro_form/text_field.rb +9 -2
- data/lib/hexapdf/type/annotation.rb +71 -1
- data/lib/hexapdf/type/annotations/appearance_generator.rb +348 -0
- data/lib/hexapdf/type/annotations/border_effect.rb +99 -0
- data/lib/hexapdf/type/annotations/border_styling.rb +160 -0
- data/lib/hexapdf/type/annotations/circle.rb +65 -0
- data/lib/hexapdf/type/annotations/interior_color.rb +84 -0
- data/lib/hexapdf/type/annotations/line.rb +490 -0
- data/lib/hexapdf/type/annotations/square.rb +65 -0
- data/lib/hexapdf/type/annotations/square_circle.rb +77 -0
- data/lib/hexapdf/type/annotations/widget.rb +52 -116
- data/lib/hexapdf/type/annotations.rb +8 -0
- data/lib/hexapdf/type/form.rb +2 -2
- data/lib/hexapdf/version.rb +1 -1
- data/lib/hexapdf/writer.rb +0 -1
- data/lib/hexapdf/xref_section.rb +7 -4
- data/test/hexapdf/content/test_graphics_state.rb +2 -3
- data/test/hexapdf/content/test_operator.rb +4 -5
- data/test/hexapdf/digital_signature/test_cms_handler.rb +7 -8
- data/test/hexapdf/digital_signature/test_handler.rb +2 -3
- data/test/hexapdf/digital_signature/test_pkcs1_handler.rb +1 -2
- data/test/hexapdf/document/test_annotations.rb +55 -0
- data/test/hexapdf/document/test_layout.rb +24 -2
- data/test/hexapdf/font/test_true_type_wrapper.rb +7 -0
- data/test/hexapdf/font/test_type1_wrapper.rb +7 -0
- data/test/hexapdf/font/true_type/test_subsetter.rb +10 -0
- data/test/hexapdf/layout/test_style.rb +27 -2
- data/test/hexapdf/task/test_optimize.rb +1 -1
- data/test/hexapdf/test_composer.rb +7 -0
- data/test/hexapdf/test_document.rb +11 -3
- data/test/hexapdf/test_object.rb +1 -1
- data/test/hexapdf/test_pdf_array.rb +36 -3
- data/test/hexapdf/test_stream.rb +1 -2
- data/test/hexapdf/test_xref_section.rb +1 -1
- data/test/hexapdf/type/acro_form/test_appearance_generator.rb +78 -3
- data/test/hexapdf/type/acro_form/test_button_field.rb +7 -6
- data/test/hexapdf/type/acro_form/test_field.rb +5 -0
- data/test/hexapdf/type/acro_form/test_form.rb +17 -1
- data/test/hexapdf/type/acro_form/test_java_script_actions.rb +21 -0
- data/test/hexapdf/type/acro_form/test_text_field.rb +7 -1
- data/test/hexapdf/type/annotations/test_appearance_generator.rb +482 -0
- data/test/hexapdf/type/annotations/test_border_effect.rb +59 -0
- data/test/hexapdf/type/annotations/test_border_styling.rb +114 -0
- data/test/hexapdf/type/annotations/test_interior_color.rb +37 -0
- data/test/hexapdf/type/annotations/test_line.rb +169 -0
- data/test/hexapdf/type/annotations/test_widget.rb +35 -81
- data/test/hexapdf/type/test_annotation.rb +55 -0
- data/test/hexapdf/type/test_form.rb +6 -0
- metadata +17 -2
@@ -0,0 +1,482 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
require 'hexapdf/document'
|
5
|
+
|
6
|
+
describe HexaPDF::Type::Annotations::AppearanceGenerator do
|
7
|
+
before do
|
8
|
+
@doc = HexaPDF::Document.new
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "create" do
|
12
|
+
it "fails for unsupported annotation types" do
|
13
|
+
annot = @doc.add({Type: :Annot, Subtype: :Unknown})
|
14
|
+
error = assert_raises(HexaPDF::Error) do
|
15
|
+
HexaPDF::Type::Annotations::AppearanceGenerator.new(annot).create_appearance
|
16
|
+
end
|
17
|
+
assert_match(/Unknown.*not yet supported/, error.message)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "line" do
|
22
|
+
before do
|
23
|
+
@line = @doc.add({Type: :Annot, Subtype: :Line, L: [100, 100, 200, 100], C: [0]})
|
24
|
+
@generator = HexaPDF::Type::Annotations::AppearanceGenerator.new(@line)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "sets the print flag and unsets the hidden flag" do
|
28
|
+
@line.flag(:hidden)
|
29
|
+
@generator.create_appearance
|
30
|
+
assert(@line.flagged?(:print))
|
31
|
+
refute(@line.flagged?(:hidden))
|
32
|
+
end
|
33
|
+
|
34
|
+
it "creates a simple line" do
|
35
|
+
@generator.create_appearance
|
36
|
+
assert_equal([96, 96, 204, 104], @line[:Rect])
|
37
|
+
assert_equal([96, 96, 204, 104], @line.appearance[:BBox])
|
38
|
+
assert_operators(@line.appearance.stream,
|
39
|
+
[[:concatenate_matrix, [1.0, 0.0, -0.0, 1.0, 100, 100]],
|
40
|
+
[:move_to, [0, 0]],
|
41
|
+
[:line_to, [100.0, 0]],
|
42
|
+
[:stroke_path]])
|
43
|
+
end
|
44
|
+
|
45
|
+
it "creates a rotated line" do
|
46
|
+
@line.line(100, 100, 50, 150)
|
47
|
+
@generator.create_appearance
|
48
|
+
assert_equal([46, 96, 104, 154], @line[:Rect])
|
49
|
+
assert_operators(@line.appearance.stream,
|
50
|
+
[[:concatenate_matrix, [-0.707107, 0.707107, -0.707107, -0.707107, 100, 100]],
|
51
|
+
[:move_to, [0, 0]],
|
52
|
+
[:line_to, [70.710678, 0]],
|
53
|
+
[:stroke_path]])
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "stroke color" do
|
57
|
+
it "uses the specified border color for stroking operations" do
|
58
|
+
@line.border_style(color: "red")
|
59
|
+
@generator.create_appearance
|
60
|
+
assert_operators(@line.appearance.stream,
|
61
|
+
[:set_device_rgb_stroking_color, [1, 0, 0]], range: 0)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "works with a transparent border" do
|
65
|
+
@line.border_style(color: :transparent, width: 1)
|
66
|
+
@generator.create_appearance
|
67
|
+
assert_operators(@line.appearance.stream, [:end_path], range: 3)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
it "uses the specified interior color for non-stroking operations" do
|
72
|
+
@line.interior_color("red")
|
73
|
+
@generator.create_appearance
|
74
|
+
assert_operators(@line.appearance.stream,
|
75
|
+
[:set_device_rgb_non_stroking_color, [1, 0, 0]], range: 0)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "sets the specified border line width" do
|
79
|
+
@line.border_style(width: 2)
|
80
|
+
@generator.create_appearance
|
81
|
+
assert_operators(@line.appearance.stream,
|
82
|
+
[:set_line_width, [2]], range: 0)
|
83
|
+
end
|
84
|
+
|
85
|
+
it "sets the specified line dash pattern if it is an array" do
|
86
|
+
@line.border_style(style: [5, 2])
|
87
|
+
@generator.create_appearance
|
88
|
+
assert_operators(@line.appearance.stream,
|
89
|
+
[:set_line_dash_pattern, [[5, 2], 0]], range: 0)
|
90
|
+
end
|
91
|
+
|
92
|
+
describe "leader lines" do
|
93
|
+
it "works for positive leader line length values" do
|
94
|
+
@line.leader_line_length(10)
|
95
|
+
@generator.create_appearance
|
96
|
+
assert_operators(@line.appearance.stream,
|
97
|
+
[[:concatenate_matrix, [1.0, 0.0, -0.0, 1.0, 100, 100]],
|
98
|
+
[:move_to, [0, 0]],
|
99
|
+
[:line_to, [0, 10]],
|
100
|
+
[:move_to, [100, 0]],
|
101
|
+
[:line_to, [100, 10]],
|
102
|
+
[:move_to, [0, 10]],
|
103
|
+
[:line_to, [100.0, 10]],
|
104
|
+
[:stroke_path]])
|
105
|
+
end
|
106
|
+
|
107
|
+
it "works for negative leader line length values" do
|
108
|
+
@line.leader_line_length(-10)
|
109
|
+
@generator.create_appearance
|
110
|
+
assert_operators(@line.appearance.stream,
|
111
|
+
[[:concatenate_matrix, [1.0, 0.0, -0.0, 1.0, 100, 100]],
|
112
|
+
[:move_to, [0, 0]],
|
113
|
+
[:line_to, [0, -10]],
|
114
|
+
[:move_to, [100, 0]],
|
115
|
+
[:line_to, [100, -10]],
|
116
|
+
[:move_to, [0, -10]],
|
117
|
+
[:line_to, [100.0, -10]],
|
118
|
+
[:stroke_path]])
|
119
|
+
end
|
120
|
+
|
121
|
+
it "works when using an offset and a positive leader line length" do
|
122
|
+
@line.leader_line_length(10)
|
123
|
+
@line.leader_line_offset(5)
|
124
|
+
@generator.create_appearance
|
125
|
+
assert_operators(@line.appearance.stream,
|
126
|
+
[[:concatenate_matrix, [1.0, 0.0, -0.0, 1.0, 100, 100]],
|
127
|
+
[:move_to, [0, 5]],
|
128
|
+
[:line_to, [0, 15]],
|
129
|
+
[:move_to, [100, 5]],
|
130
|
+
[:line_to, [100, 15]],
|
131
|
+
[:move_to, [0, 15]],
|
132
|
+
[:line_to, [100.0, 15]],
|
133
|
+
[:stroke_path]])
|
134
|
+
end
|
135
|
+
|
136
|
+
it "works when using an offset and a negative leader line length" do
|
137
|
+
@line.leader_line_length(-10)
|
138
|
+
@line.leader_line_offset(5)
|
139
|
+
@generator.create_appearance
|
140
|
+
assert_operators(@line.appearance.stream,
|
141
|
+
[[:concatenate_matrix, [1.0, 0.0, -0.0, 1.0, 100, 100]],
|
142
|
+
[:move_to, [0, -5]],
|
143
|
+
[:line_to, [0, -15]],
|
144
|
+
[:move_to, [100, -5]],
|
145
|
+
[:line_to, [100, -15]],
|
146
|
+
[:move_to, [0, -15]],
|
147
|
+
[:line_to, [100.0, -15]],
|
148
|
+
[:stroke_path]])
|
149
|
+
end
|
150
|
+
|
151
|
+
it "works when using leader line extensions" do
|
152
|
+
@line.leader_line_length(10)
|
153
|
+
@line.leader_line_extension_length(5)
|
154
|
+
@generator.create_appearance
|
155
|
+
assert_operators(@line.appearance.stream,
|
156
|
+
[[:concatenate_matrix, [1.0, 0.0, -0.0, 1.0, 100, 100]],
|
157
|
+
[:move_to, [0, 0]],
|
158
|
+
[:line_to, [0, 15]],
|
159
|
+
[:move_to, [100, 0]],
|
160
|
+
[:line_to, [100, 15]],
|
161
|
+
[:move_to, [0, 10]],
|
162
|
+
[:line_to, [100.0, 10]],
|
163
|
+
[:stroke_path]])
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
describe "line ending styles" do
|
168
|
+
before do
|
169
|
+
@line.border_style(width: 2)
|
170
|
+
@line.interior_color("red")
|
171
|
+
end
|
172
|
+
|
173
|
+
it "works correctly for a transparent border" do
|
174
|
+
@line.line_ending_style(start_style: :square, end_style: :square)
|
175
|
+
@line.border_style(color: :transparent)
|
176
|
+
@generator.create_appearance
|
177
|
+
assert_operators(@line.appearance.stream,
|
178
|
+
[[:append_rectangle, [-3, -3, 6, 6]],
|
179
|
+
[:fill_path_non_zero],
|
180
|
+
[:append_rectangle, [97, -3, 6, 6]],
|
181
|
+
[:fill_path_non_zero]], range: 5..-1)
|
182
|
+
end
|
183
|
+
|
184
|
+
it "works for a square" do
|
185
|
+
@line.line_ending_style(start_style: :square, end_style: :square)
|
186
|
+
@generator.create_appearance
|
187
|
+
assert_operators(@line.appearance.stream,
|
188
|
+
[[:append_rectangle, [-6, -6, 12, 12]],
|
189
|
+
[:fill_and_stroke_path_non_zero],
|
190
|
+
[:append_rectangle, [94, -6, 12, 12]],
|
191
|
+
[:fill_and_stroke_path_non_zero]], range: 6..-1)
|
192
|
+
end
|
193
|
+
|
194
|
+
it "works for a circle" do
|
195
|
+
@line.line_ending_style(start_style: :circle, end_style: :circle)
|
196
|
+
@generator.create_appearance
|
197
|
+
assert_operators(@line.appearance.stream,
|
198
|
+
[[:move_to, [6.0, 0.0]],
|
199
|
+
[:curve_to, [6.0, 2.140933, 4.854102, 4.125686, 3.0, 5.196152]],
|
200
|
+
[:curve_to, [1.145898, 6.266619, -1.145898, 6.266619, -3.0, 5.196152]],
|
201
|
+
[:curve_to, [-4.854102, 4.125686, -6.0, 2.140933, -6.0, 0.0]],
|
202
|
+
[:curve_to, [-6.0, -2.140933, -4.854102, -4.125686, -3.0, -5.196152]],
|
203
|
+
[:curve_to, [-1.145898, -6.266619, 1.145898, -6.266619, 3.0, -5.196152]],
|
204
|
+
[:curve_to, [4.854102, -4.125686, 6.0, -2.140933, 6.0, -0.0]],
|
205
|
+
[:close_subpath],
|
206
|
+
[:fill_and_stroke_path_non_zero],
|
207
|
+
[:move_to, [106.0, 0.0]],
|
208
|
+
[:curve_to, [106.0, 2.140933, 104.854102, 4.125686, 103.0, 5.196152]],
|
209
|
+
[:curve_to, [101.145898, 6.266619, 98.854102, 6.266619, 97.0, 5.196152]],
|
210
|
+
[:curve_to, [95.145898, 4.125686, 94.0, 2.140933, 94.0, 0.0]],
|
211
|
+
[:curve_to, [94.0, -2.140933, 95.145898, -4.125686, 97.0, -5.196152]],
|
212
|
+
[:curve_to, [98.854102, -6.266619, 101.145898, -6.266619, 103.0, -5.196152]],
|
213
|
+
[:curve_to, [104.854102, -4.125686, 106.0, -2.140933, 106.0, -0.0]],
|
214
|
+
[:close_subpath],
|
215
|
+
[:fill_and_stroke_path_non_zero]], range: 6..-1)
|
216
|
+
end
|
217
|
+
|
218
|
+
it "works for a diamond" do
|
219
|
+
@line.line_ending_style(start_style: :diamond, end_style: :diamond)
|
220
|
+
@generator.create_appearance
|
221
|
+
assert_operators(@line.appearance.stream,
|
222
|
+
[[:move_to, [6, 0]],
|
223
|
+
[:line_to, [0, 6]],
|
224
|
+
[:line_to, [-6, 0]],
|
225
|
+
[:line_to, [0, -6]],
|
226
|
+
[:close_subpath],
|
227
|
+
[:fill_and_stroke_path_non_zero],
|
228
|
+
[:move_to, [106.0, 0]],
|
229
|
+
[:line_to, [100.0, 6]],
|
230
|
+
[:line_to, [94.0, 0]],
|
231
|
+
[:line_to, [100.0, -6]],
|
232
|
+
[:close_subpath],
|
233
|
+
[:fill_and_stroke_path_non_zero]], range: 6..-1)
|
234
|
+
end
|
235
|
+
|
236
|
+
it "works for open and closed as well as reversed open and closed arrows" do
|
237
|
+
dx = 15.588457
|
238
|
+
[:open_arrow, :closed_arrow, :ropen_arrow, :rclosed_arrow].each do |style|
|
239
|
+
@line.line_ending_style(start_style: style, end_style: style)
|
240
|
+
@generator.create_appearance
|
241
|
+
used_dx = (style == :ropen_arrow || style == :rclosed_arrow ? -dx : dx)
|
242
|
+
ops = [[:move_to, [used_dx, 9.0]],
|
243
|
+
[:line_to, [0, 0]],
|
244
|
+
[:line_to, [used_dx, -9.0]],
|
245
|
+
[:move_to, [100 - used_dx, -9.0]],
|
246
|
+
[:line_to, [100.0, 0]],
|
247
|
+
[:line_to, [100 - used_dx, 9.0]]]
|
248
|
+
if style == :closed_arrow || style == :rclosed_arrow
|
249
|
+
ops.insert(3, [:close_subpath], [:fill_and_stroke_path_non_zero])
|
250
|
+
ops.insert(-1, [:close_subpath], [:fill_and_stroke_path_non_zero])
|
251
|
+
else
|
252
|
+
ops.insert(3, [:stroke_path])
|
253
|
+
ops.insert(-1, [:stroke_path])
|
254
|
+
end
|
255
|
+
assert_operators(@line.appearance.stream, ops, range: 6..-1)
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
it "works for butt" do
|
260
|
+
@line.line_ending_style(start_style: :butt, end_style: :butt)
|
261
|
+
@generator.create_appearance
|
262
|
+
assert_operators(@line.appearance.stream,
|
263
|
+
[[:move_to, [0, 6]],
|
264
|
+
[:line_to, [0, -6]],
|
265
|
+
[:stroke_path],
|
266
|
+
[:move_to, [100.0, 6]],
|
267
|
+
[:line_to, [100.0, -6]],
|
268
|
+
[:stroke_path]], range: 6..-1)
|
269
|
+
end
|
270
|
+
|
271
|
+
it "works for slash" do
|
272
|
+
@line.line_ending_style(start_style: :slash, end_style: :slash)
|
273
|
+
@generator.create_appearance
|
274
|
+
assert_operators(@line.appearance.stream,
|
275
|
+
[[:move_to, [3, 5.196152]],
|
276
|
+
[:line_to, [-3, -5.196152]],
|
277
|
+
[:stroke_path],
|
278
|
+
[:move_to, [103.0, 5.196152]],
|
279
|
+
[:line_to, [97.0, -5.196152]],
|
280
|
+
[:stroke_path]], range: 6..-1)
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
describe "caption" do
|
285
|
+
before do
|
286
|
+
@line.captioned(true)
|
287
|
+
@line.contents("Test")
|
288
|
+
end
|
289
|
+
|
290
|
+
it "adjusts the annotation's /Rect entry" do
|
291
|
+
@line.contents("This is some eeeeextra long text")
|
292
|
+
@generator.create_appearance
|
293
|
+
assert_equal([80.2225, 91.83749999999999, 219.7775, 108.1625], @line[:Rect])
|
294
|
+
end
|
295
|
+
|
296
|
+
it "puts the caption inline" do
|
297
|
+
@generator.create_appearance
|
298
|
+
assert_operators(@line.appearance.stream,
|
299
|
+
[[:move_to, [0, 0]],
|
300
|
+
[:line_to, [40.2475, 0]],
|
301
|
+
[:move_to, [59.7525, 0]],
|
302
|
+
[:line_to, [100, 0]],
|
303
|
+
[:stroke_path],
|
304
|
+
[:save_graphics_state],
|
305
|
+
[:set_font_and_size, [:F1, 9]],
|
306
|
+
[:begin_text],
|
307
|
+
[:move_text, [41.2475, -2.2995]],
|
308
|
+
[:show_text, ["Test"]],
|
309
|
+
[:end_text]], range: 1..-2)
|
310
|
+
end
|
311
|
+
|
312
|
+
it "puts the caption inline with an offset" do
|
313
|
+
@line.caption_offset(20, 5)
|
314
|
+
@generator.create_appearance
|
315
|
+
assert_operators(@line.appearance.stream,
|
316
|
+
[[:move_to, [0, 0]],
|
317
|
+
[:line_to, [60.2475, 0]],
|
318
|
+
[:move_to, [79.7525, 0]],
|
319
|
+
[:line_to, [100, 0]],
|
320
|
+
[:stroke_path],
|
321
|
+
[:save_graphics_state],
|
322
|
+
[:set_font_and_size, [:F1, 9]],
|
323
|
+
[:begin_text],
|
324
|
+
[:move_text, [61.2475, 2.7005]],
|
325
|
+
[:show_text, ["Test"]],
|
326
|
+
[:end_text]], range: 1..-2)
|
327
|
+
end
|
328
|
+
|
329
|
+
it "handles too long inline captions" do
|
330
|
+
@line.contents('This inline text is so long that no line is shown')
|
331
|
+
@generator.create_appearance
|
332
|
+
assert_operators(@line.appearance.stream,
|
333
|
+
[[:move_to, [0, 0]],
|
334
|
+
[:line_to, [0, 0]],
|
335
|
+
[:move_to, [100, 0]],
|
336
|
+
[:line_to, [100, 0]],
|
337
|
+
[:stroke_path],
|
338
|
+
[:save_graphics_state],
|
339
|
+
[:set_font_and_size, [:F1, 9]],
|
340
|
+
[:begin_text],
|
341
|
+
[:move_text, [-41.0395, -2.2995]],
|
342
|
+
[:show_text, ["This inline text is so long that no line is shown"]],
|
343
|
+
[:end_text]], range: 1..-2)
|
344
|
+
end
|
345
|
+
|
346
|
+
it "puts the caption on top of the line" do
|
347
|
+
@line.caption_position(:top)
|
348
|
+
@generator.create_appearance
|
349
|
+
assert_operators(@line.appearance.stream,
|
350
|
+
[[:move_to, [0, 0]],
|
351
|
+
[:line_to, [100, 0]],
|
352
|
+
[:stroke_path],
|
353
|
+
[:save_graphics_state],
|
354
|
+
[:set_font_and_size, [:F1, 9]],
|
355
|
+
[:begin_text],
|
356
|
+
[:move_text, [41.2475, 3.863]],
|
357
|
+
[:show_text, ["Test"]],
|
358
|
+
[:end_text]], range: 1..-2)
|
359
|
+
end
|
360
|
+
|
361
|
+
it "puts the caption on top of the line" do
|
362
|
+
@line.caption_position(:top)
|
363
|
+
@line.caption_offset(-20, -5)
|
364
|
+
@generator.create_appearance
|
365
|
+
assert_operators(@line.appearance.stream,
|
366
|
+
[[:move_to, [0, 0]],
|
367
|
+
[:line_to, [100, 0]],
|
368
|
+
[:stroke_path],
|
369
|
+
[:save_graphics_state],
|
370
|
+
[:set_font_and_size, [:F1, 9]],
|
371
|
+
[:begin_text],
|
372
|
+
[:move_text, [21.2475, -1.137]],
|
373
|
+
[:show_text, ["Test"]],
|
374
|
+
[:end_text]], range: 1..-2)
|
375
|
+
end
|
376
|
+
|
377
|
+
it "handles text with line breaks" do
|
378
|
+
@line.contents("This inline text\ris long")
|
379
|
+
@generator.create_appearance
|
380
|
+
assert_operators(@line.appearance.stream,
|
381
|
+
[[:move_to, [0, 0]],
|
382
|
+
[:line_to, [20.2405, 0]],
|
383
|
+
[:move_to, [79.7595, 0]],
|
384
|
+
[:line_to, [100, 0]],
|
385
|
+
[:stroke_path],
|
386
|
+
[:save_graphics_state],
|
387
|
+
[:set_leading, [10.40625]],
|
388
|
+
[:set_font_and_size, [:F1, 9]],
|
389
|
+
[:begin_text],
|
390
|
+
[:move_text, [21.2405, 2.903625]],
|
391
|
+
[:show_text, ["This inline text"]],
|
392
|
+
[:move_text_next_line],
|
393
|
+
[:show_text, ["is long"]],
|
394
|
+
[:end_text]], range: 1..-2)
|
395
|
+
end
|
396
|
+
end
|
397
|
+
end
|
398
|
+
|
399
|
+
describe "square/circle" do
|
400
|
+
before do
|
401
|
+
@square = @doc.add({Type: :Annot, Subtype: :Square, Rect: [100, 100, 200, 150], C: [0],
|
402
|
+
BS: {W: 2}})
|
403
|
+
@generator = HexaPDF::Type::Annotations::AppearanceGenerator.new(@square)
|
404
|
+
end
|
405
|
+
|
406
|
+
it "sets the print flag and unsets the hidden flag" do
|
407
|
+
@square.flag(:hidden)
|
408
|
+
@generator.create_appearance
|
409
|
+
assert(@square.flagged?(:print))
|
410
|
+
refute(@square.flagged?(:hidden))
|
411
|
+
end
|
412
|
+
|
413
|
+
it "creates the /RD entry if it doesn't exist and adjusts the /Rect" do
|
414
|
+
@generator.create_appearance
|
415
|
+
assert_equal([99, 99, 201, 151], @square[:Rect])
|
416
|
+
assert_equal([0, 0, 102, 52], @square.appearance[:BBox])
|
417
|
+
assert_operators(@square.appearance.stream,
|
418
|
+
[[:set_line_width, [2]],
|
419
|
+
[:append_rectangle, [1, 1, 100, 50]],
|
420
|
+
[:stroke_path]])
|
421
|
+
end
|
422
|
+
|
423
|
+
it "uses an existing /RD entry" do
|
424
|
+
@square[:RD] = [2, 4, 6, 8]
|
425
|
+
@generator.create_appearance
|
426
|
+
assert_equal([100, 100, 200, 150], @square[:Rect])
|
427
|
+
assert_equal([0, 0, 100, 50], @square.appearance[:BBox])
|
428
|
+
assert_operators(@square.appearance.stream,
|
429
|
+
[[:set_line_width, [2]],
|
430
|
+
[:append_rectangle, [3, 9, 90, 36]],
|
431
|
+
[:stroke_path]])
|
432
|
+
end
|
433
|
+
|
434
|
+
it "can apply just a fill color without a stroke color" do
|
435
|
+
@square.delete(:C)
|
436
|
+
@square.interior_color(255, 0, 0)
|
437
|
+
@generator.create_appearance
|
438
|
+
assert_operators(@square.appearance.stream,
|
439
|
+
[[:set_device_rgb_non_stroking_color, [1, 0, 0]],
|
440
|
+
[:set_line_width, [2]],
|
441
|
+
[:append_rectangle, [1, 1, 100, 50]],
|
442
|
+
[:fill_path_non_zero]])
|
443
|
+
end
|
444
|
+
|
445
|
+
it "applies all set styling options" do
|
446
|
+
@square.border_style(color: [255, 0, 0], width: 10, style: [2, 1])
|
447
|
+
@square.interior_color(0, 255, 0)
|
448
|
+
@square.opacity(fill_alpha: 0.5, stroke_alpha: 0.5)
|
449
|
+
@generator.create_appearance
|
450
|
+
assert_operators(@square.appearance.stream,
|
451
|
+
[[:set_graphics_state_parameters, [:GS1]],
|
452
|
+
[:set_device_rgb_stroking_color, [1, 0, 0]],
|
453
|
+
[:set_device_rgb_non_stroking_color, [0, 1, 0]],
|
454
|
+
[:set_line_width, [10]],
|
455
|
+
[:set_line_dash_pattern, [[2, 1], 0]],
|
456
|
+
[:append_rectangle, [5, 5, 100, 50]],
|
457
|
+
[:fill_and_stroke_path_non_zero]])
|
458
|
+
end
|
459
|
+
|
460
|
+
it "doesn't draw anything if neither stroke nor fill color is set" do
|
461
|
+
@square.delete(:C)
|
462
|
+
@generator.create_appearance
|
463
|
+
assert_operators(@square.appearance.stream, [])
|
464
|
+
end
|
465
|
+
|
466
|
+
it "draws an ellipse" do
|
467
|
+
@square[:Subtype] = :Circle
|
468
|
+
@generator.create_appearance
|
469
|
+
assert_operators(@square.appearance.stream,
|
470
|
+
[[:set_line_width, [2]],
|
471
|
+
[:move_to, [101.0, 26.0]],
|
472
|
+
[:curve_to, [101.0, 34.920552, 91.45085, 43.190359, 76.0, 47.650635]],
|
473
|
+
[:curve_to, [60.54915, 52.110911, 41.45085, 52.110911, 26.0, 47.650635]],
|
474
|
+
[:curve_to, [10.54915, 43.190359, 1.0, 34.920552, 1.0, 26.0]],
|
475
|
+
[:curve_to, [1.0, 17.079448, 10.54915, 8.809641, 26.0, 4.349365]],
|
476
|
+
[:curve_to, [41.45085, -0.110911, 60.54915, -0.110911, 76.0, 4.349365]],
|
477
|
+
[:curve_to, [91.45085, 8.809641, 101.0, 17.079448, 101.0, 26.0]],
|
478
|
+
[:close_subpath],
|
479
|
+
[:stroke_path]])
|
480
|
+
end
|
481
|
+
end
|
482
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
require 'hexapdf/document'
|
5
|
+
require 'hexapdf/type/annotations/border_effect'
|
6
|
+
|
7
|
+
describe HexaPDF::Type::Annotations::BorderEffect do
|
8
|
+
class TestAnnot < HexaPDF::Type::Annotation
|
9
|
+
define_field :BE, type: :XXBorderEffect
|
10
|
+
include HexaPDF::Type::Annotations::BorderEffect
|
11
|
+
end
|
12
|
+
|
13
|
+
before do
|
14
|
+
@doc = HexaPDF::Document.new
|
15
|
+
@annot = @doc.wrap({Type: :Annot}, type: TestAnnot)
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "border_effect" do
|
19
|
+
it "returns :none if no border effect is set" do
|
20
|
+
assert_equal(:none, @annot.border_effect)
|
21
|
+
@annot[:BE] = {}
|
22
|
+
assert_equal(:none, @annot.border_effect)
|
23
|
+
@annot[:BE] = {S: :S}
|
24
|
+
assert_equal(:none, @annot.border_effect)
|
25
|
+
@annot[:BE] = {S: :K}
|
26
|
+
assert_equal(:none, @annot.border_effect)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "returns cloud(y|ier|iest) if /S is /C and depending on /I" do
|
30
|
+
@annot[:BE] = {S: :C}
|
31
|
+
assert_equal(:cloudy, @annot.border_effect)
|
32
|
+
@annot[:BE][:I] = 0
|
33
|
+
assert_equal(:cloudy, @annot.border_effect)
|
34
|
+
@annot[:BE][:I] = 1
|
35
|
+
assert_equal(:cloudier, @annot.border_effect)
|
36
|
+
@annot[:BE][:I] = 2
|
37
|
+
assert_equal(:cloudiest, @annot.border_effect)
|
38
|
+
@annot[:BE][:I] = 3
|
39
|
+
assert_equal(:cloudy, @annot.border_effect)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "sets the /BE entry appropriately" do
|
43
|
+
@annot.border_effect(:none)
|
44
|
+
refute(@annot.key?(:BE))
|
45
|
+
@annot.border_effect(nil)
|
46
|
+
refute(@annot.key?(:BE))
|
47
|
+
@annot.border_effect(:cloudy)
|
48
|
+
assert_equal({S: :C, I: 0}, @annot[:BE])
|
49
|
+
@annot.border_effect(:cloudier)
|
50
|
+
assert_equal({S: :C, I: 1}, @annot[:BE])
|
51
|
+
@annot.border_effect(:cloudiest)
|
52
|
+
assert_equal({S: :C, I: 2}, @annot[:BE])
|
53
|
+
end
|
54
|
+
|
55
|
+
it "raises an error if the given type is unknown" do
|
56
|
+
assert_raises(ArgumentError) { @annot.border_effect(:unknown) }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
require 'hexapdf/document'
|
5
|
+
require 'hexapdf/type/annotations/border_styling'
|
6
|
+
|
7
|
+
describe HexaPDF::Type::Annotations::BorderStyling do
|
8
|
+
class TestAnnot < HexaPDF::Type::Annotation
|
9
|
+
define_field :BS, type: :Border
|
10
|
+
define_field :MK, type: :XXAppearanceCharacteristics
|
11
|
+
include HexaPDF::Type::Annotations::BorderStyling
|
12
|
+
end
|
13
|
+
|
14
|
+
before do
|
15
|
+
@doc = HexaPDF::Document.new
|
16
|
+
@annot = @doc.wrap({Type: :Annot}, type: TestAnnot)
|
17
|
+
@color = HexaPDF::Content::ColorSpace.prenormalized_device_color([1, 0, 1])
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "border_style" do
|
21
|
+
describe "getter" do
|
22
|
+
it "no /Border, /BS or /C|/MK set" do
|
23
|
+
@annot.delete(:MK)
|
24
|
+
assert_equal([1, nil, :solid, 0, 0], @annot.border_style.to_a)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "no /Border, /BS but with /MK empty" do
|
28
|
+
@annot[:Subtype] = :Widget
|
29
|
+
@annot[:MK] = {}
|
30
|
+
assert_equal([1, nil, :solid, 0, 0], @annot.border_style.to_a)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "uses the color from /C" do
|
34
|
+
@annot[:C] = [1, 0, 1]
|
35
|
+
assert_equal([1, @color, :solid, 0, 0], @annot.border_style.to_a)
|
36
|
+
@annot[:C] = []
|
37
|
+
assert_equal([1, nil, :solid, 0, 0], @annot.border_style.to_a)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "uses the color from /MK" do
|
41
|
+
@annot[:Subtype] = :Widget
|
42
|
+
@annot[:MK] = {BC: [1, 0, 1]}
|
43
|
+
assert_equal([1, @color, :solid, 0, 0], @annot.border_style.to_a)
|
44
|
+
@annot[:MK][:BC] = []
|
45
|
+
assert_equal([1, nil, :solid, 0, 0], @annot.border_style.to_a)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "uses the data from /Border" do
|
49
|
+
@annot[:Border] = [1, 2, 3, [1, 2]]
|
50
|
+
assert_equal([3, nil, [1, 2], 1, 2], @annot.border_style.to_a)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "uses the data from /BS, overriding /Border values" do
|
54
|
+
@annot[:Border] = [1, 2, 3, [1, 2]]
|
55
|
+
@annot[:BS] = {W: 5, S: :D, D: [5, 6]}
|
56
|
+
assert_equal([5, nil, [5, 6], 0, 0], @annot.border_style.to_a)
|
57
|
+
|
58
|
+
[[:S, :solid], [:D, [5, 6]], [:B, :beveled], [:I, :inset],
|
59
|
+
[:U, :underlined], [:Unknown, :solid]].each do |val, result|
|
60
|
+
@annot[:BS] = {S: val, D: [5, 6]}
|
61
|
+
assert_equal([1, nil, result, 0, 0], @annot.border_style.to_a)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "setter" do
|
67
|
+
it "returns self" do
|
68
|
+
assert_equal(@annot, @annot.border_style(width: 1))
|
69
|
+
end
|
70
|
+
|
71
|
+
it "sets the color" do
|
72
|
+
@annot.border_style(color: [1.0, 51, 1.0])
|
73
|
+
assert_equal([1, 0.2, 1], @annot[:C])
|
74
|
+
|
75
|
+
@annot.border_style(color: :transparent)
|
76
|
+
assert_equal([], @annot[:C])
|
77
|
+
end
|
78
|
+
|
79
|
+
it "sets the color on a widget using /MK" do
|
80
|
+
@annot[:Subtype] = :Widget
|
81
|
+
@annot.border_style(color: [1.0, 51, 1.0])
|
82
|
+
assert_equal([1, 0.2, 1], @annot[:MK][:BC])
|
83
|
+
|
84
|
+
@annot.border_style(color: :transparent)
|
85
|
+
assert_equal([], @annot[:MK][:BC])
|
86
|
+
end
|
87
|
+
|
88
|
+
it "sets the width" do
|
89
|
+
@annot.border_style(width: 2)
|
90
|
+
assert_equal(2, @annot[:BS][:W])
|
91
|
+
end
|
92
|
+
|
93
|
+
it "sets the style" do
|
94
|
+
[[:solid, :S], [[5, 6], :D], [:beveled, :B], [:inset, :I], [:underlined, :U]].each do |val, r|
|
95
|
+
@annot.border_style(style: val)
|
96
|
+
assert_equal(r, @annot[:BS][:S])
|
97
|
+
assert_equal(val, @annot[:BS][:D]) if r == :D
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
it "overrides all priorly set values" do
|
102
|
+
@annot.border_style(width: 3, style: :inset, color: [1])
|
103
|
+
@annot.border_style(width: 5)
|
104
|
+
border_style = @annot.border_style
|
105
|
+
assert_equal(:solid, border_style.style)
|
106
|
+
assert_equal([0], border_style.color.components)
|
107
|
+
end
|
108
|
+
|
109
|
+
it "raises an error for an unknown style" do
|
110
|
+
assert_raises(ArgumentError) { @annot.border_style(style: :unknown) }
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
require 'hexapdf/document'
|
5
|
+
require 'hexapdf/type/annotations/interior_color'
|
6
|
+
|
7
|
+
describe HexaPDF::Type::Annotations::InteriorColor do
|
8
|
+
class TestAnnot < HexaPDF::Type::Annotation
|
9
|
+
define_field :IC, type: HexaPDF::PDFArray
|
10
|
+
include HexaPDF::Type::Annotations::InteriorColor
|
11
|
+
end
|
12
|
+
|
13
|
+
before do
|
14
|
+
@doc = HexaPDF::Document.new
|
15
|
+
@annot = @doc.wrap({Type: :Annot}, type: TestAnnot)
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "interior_color" do
|
19
|
+
it "returns the interior color" do
|
20
|
+
assert_nil(@annot.interior_color)
|
21
|
+
@annot[:IC] = []
|
22
|
+
assert_nil(@annot.interior_color)
|
23
|
+
@annot[:IC] = [0.5]
|
24
|
+
assert_equal(HexaPDF::Content::ColorSpace.device_color_from_specification(0.5),
|
25
|
+
@annot.interior_color)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "sets the interior color" do
|
29
|
+
@annot.interior_color(255)
|
30
|
+
assert_equal([1.0], @annot[:IC])
|
31
|
+
@annot.interior_color(255, 255, 0)
|
32
|
+
assert_equal([1.0, 1.0, 0], @annot[:IC])
|
33
|
+
@annot.interior_color(:transparent)
|
34
|
+
assert_equal([], @annot[:IC])
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|