File: validating_receiver.rb

package info (click to toggle)
ruby-pdf-reader 2.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,512 kB
  • sloc: ruby: 11,959; sh: 46; makefile: 11
file content (291 lines) | stat: -rw-r--r-- 8,318 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# coding: utf-8
# typed: strict
# frozen_string_literal: true

module PDF
  class Reader

    # Page#walk will execute the content stream of a page, calling methods on a receiver class
    # provided by the user. Each operator has a specific set of parameters it expects, and we
    # wrap the users receiver class in this one to verify the PDF uses valid parameters.
    #
    # Without these checks, users can't be confident about the number of parameters they'll receive
    # for an operator, or what the type of those parameters will be. Everyone ends up building their
    # own type safety guard clauses and it's tedious.
    #
    # Not all operators have type safety implemented yet, but we can expand the number over time.
    class ValidatingReceiver

      #: (untyped) -> void
      def initialize(wrapped)
        @wrapped = wrapped
      end

      #: (PDF::Reader::Page) -> void
      def page=(page)
        call_wrapped(:page=, page)
      end

      #####################################################
      # Graphics State Operators
      #####################################################
      #: (*untyped) -> void
      def save_graphics_state(*args)
        call_wrapped(:save_graphics_state)
      end

      #: (*untyped) -> void
      def restore_graphics_state(*args)
        call_wrapped(:restore_graphics_state)
      end

      #####################################################
      # Matrix Operators
      #####################################################

      #: (*untyped) -> void
      def concatenate_matrix(*args)
        a, b, c, d, e, f = *args
        call_wrapped(
          :concatenate_matrix,
          TypeCheck.cast_to_numeric!(a),
          TypeCheck.cast_to_numeric!(b),
          TypeCheck.cast_to_numeric!(c),
          TypeCheck.cast_to_numeric!(d),
          TypeCheck.cast_to_numeric!(e),
          TypeCheck.cast_to_numeric!(f),
        )
      end

      #####################################################
      # Text Object Operators
      #####################################################

      #: (*untyped) -> void
      def begin_text_object(*args)
        call_wrapped(:begin_text_object)
      end

      #: (*untyped) -> void
      def end_text_object(*args)
        call_wrapped(:end_text_object)
      end

      #####################################################
      # Text State Operators
      #####################################################
      #: (*untyped) -> void
      def set_character_spacing(*args)
        char_spacing, _ = *args
        call_wrapped(
          :set_character_spacing,
          TypeCheck.cast_to_numeric!(char_spacing)
        )
      end

      #: (*untyped) -> void
      def set_horizontal_text_scaling(*args)
        h_scaling, _ = *args
        call_wrapped(
          :set_horizontal_text_scaling,
          TypeCheck.cast_to_numeric!(h_scaling)
        )
      end

      #: (*untyped) -> void
      def set_text_font_and_size(*args)
        label, size, _ = *args
        call_wrapped(
          :set_text_font_and_size,
          TypeCheck.cast_to_symbol(label),
          TypeCheck.cast_to_numeric!(size)
        )
      end

      #: (*untyped) -> void
      def set_text_leading(*args)
        leading, _ = *args
        call_wrapped(
          :set_text_leading,
          TypeCheck.cast_to_numeric!(leading)
        )
      end

      #: (*untyped) -> void
      def set_text_rendering_mode(*args)
        mode, _ = *args
        call_wrapped(
          :set_text_rendering_mode,
          TypeCheck.cast_to_numeric!(mode)
        )
      end

      #: (*untyped) -> void
      def set_text_rise(*args)
        rise, _ = *args
        call_wrapped(
          :set_text_rise,
          TypeCheck.cast_to_numeric!(rise)
        )
      end

      #: (*untyped) -> void
      def set_word_spacing(*args)
        word_spacing, _ = *args
        call_wrapped(
          :set_word_spacing,
          TypeCheck.cast_to_numeric!(word_spacing)
        )
      end

      #####################################################
      # Text Positioning Operators
      #####################################################

      #: (*untyped) -> void
      def move_text_position(*args) # Td
        x, y, _ = *args
        call_wrapped(
          :move_text_position,
          TypeCheck.cast_to_numeric!(x),
          TypeCheck.cast_to_numeric!(y)
        )
      end

      #: (*untyped) -> void
      def move_text_position_and_set_leading(*args) # TD
        x, y, _ = *args
        call_wrapped(
          :move_text_position_and_set_leading,
          TypeCheck.cast_to_numeric!(x),
          TypeCheck.cast_to_numeric!(y)
        )
      end

      #: (*untyped) -> void
      def set_text_matrix_and_text_line_matrix(*args) # Tm
        a, b, c, d, e, f = *args
        call_wrapped(
          :set_text_matrix_and_text_line_matrix,
          TypeCheck.cast_to_numeric!(a),
          TypeCheck.cast_to_numeric!(b),
          TypeCheck.cast_to_numeric!(c),
          TypeCheck.cast_to_numeric!(d),
          TypeCheck.cast_to_numeric!(e),
          TypeCheck.cast_to_numeric!(f),
        )
      end

      #: (*untyped) -> void
      def move_to_start_of_next_line(*args) # T*
        call_wrapped(:move_to_start_of_next_line)
      end

      #####################################################
      # Text Showing Operators
      #####################################################
      #: (*untyped) -> void
      def show_text(*args) # Tj (AWAY)
        string, _ = *args
        call_wrapped(
          :show_text,
          TypeCheck.cast_to_string!(string)
        )
      end

      #: (*untyped) -> void
      def show_text_with_positioning(*args) # TJ [(A) 120 (WA) 20 (Y)]
        params, _ = *args
        unless params.is_a?(Array)
          raise MalformedPDFError, "TJ operator expects a single Array argument"
        end

        call_wrapped(
          :show_text_with_positioning,
          params
        )
      end

      #: (*untyped) -> void
      def move_to_next_line_and_show_text(*args) # '
        string, _ = *args
        call_wrapped(
          :move_to_next_line_and_show_text,
          TypeCheck.cast_to_string!(string)
        )
      end

      #: (*untyped) -> void
      def set_spacing_next_line_show_text(*args) # "
        aw, ac, string = *args
        call_wrapped(
          :set_spacing_next_line_show_text,
          TypeCheck.cast_to_numeric!(aw),
          TypeCheck.cast_to_numeric!(ac),
          TypeCheck.cast_to_string!(string)
        )
      end

      #####################################################
      # Form XObject Operators
      #####################################################

      #: (*untyped) -> void
      def invoke_xobject(*args)
        label, _ = *args

        call_wrapped(
          :invoke_xobject,
          TypeCheck.cast_to_symbol(label)
        )
      end

      #####################################################
      # Inline Image Operators
      #####################################################

      #: (*untyped) -> void
      def begin_inline_image(*args)
        call_wrapped(:begin_inline_image)
      end

      #: (*untyped) -> void
      def begin_inline_image_data(*args)
        # We can't use call_wrapped() here because sorbet won't allow splat args with a dynamic
        # number of elements
        @wrapped.begin_inline_image_data(*args) if @wrapped.respond_to?(:begin_inline_image_data)
      end

      #: (*untyped) -> void
      def end_inline_image(*args)
        data, _ = *args

        call_wrapped(
          :end_inline_image,
          TypeCheck.cast_to_string!(data)
        )
      end

      #####################################################
      # Final safety net for any operators that don't have type checking enabled yet
      #####################################################

      #: (untyped) -> bool
      def respond_to?(meth)
        @wrapped.respond_to?(meth)
      end

      #: (Symbol, *untyped) -> void
      def method_missing(methodname, *args)
        @wrapped.send(methodname, *args)
      end

      private

      #: (untyped, *untyped) -> void
      def call_wrapped(methodname, *args)
        @wrapped.send(methodname, *args) if @wrapped.respond_to?(methodname)
      end
    end
  end
end