Skip to content

Commit c6baf3d

Browse files
committed
manual fixes after amp did its thing
In teh previous commit I used amp [1] to start converting the RBI file into inline RBS style type annotations. It got me a fair way, but..... I spent enough money on the experiment and it was gonna cost a whole lot more to get it done. If I'd even get there. I flipped into manual fixup mode and got `srb tc` passing. [1] https://ampcode.com/manual
1 parent c0ff33c commit c6baf3d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+396
-153
lines changed

lib/pdf/reader.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,11 @@ class Reader
118118
#
119119
#: (String | Tempfile | IO | StringIO, ?Hash[untyped, untyped]) -> void
120120
def initialize(input, opts = {})
121-
@cache = PDF::Reader::ObjectCache.new
121+
@cache = PDF::Reader::ObjectCache.new #: PDF::Reader::ObjectCache
122122
opts.merge!(:cache => @cache)
123-
@objects = PDF::Reader::ObjectHash.new(input, opts)
123+
@objects = PDF::Reader::ObjectHash.new(input, opts) #: PDF::Reader::ObjectHash
124+
@page_count = nil #: Integer | nil
125+
@root = nil #: Hash[Symbol, untyped] | nil
124126
end
125127

126128
# Return a Hash with some basic information about the PDF file

lib/pdf/reader/advanced_text_run_filter.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def self.exclude(text_runs, filter_hash)
6060

6161
#: Array[PDF::Reader::TextRun]
6262
attr_reader :text_runs
63-
63+
6464
#: Hash[Symbol, untyped]
6565
attr_reader :filter_hash
6666

@@ -70,18 +70,21 @@ def initialize(text_runs, filter_hash)
7070
@filter_hash = filter_hash
7171
end
7272

73+
#: () -> Array[PDF::Reader::TextRun]
7374
def only
7475
return text_runs if filter_hash.empty?
7576
text_runs.select { |text_run| evaluate_filter(text_run) }
7677
end
7778

79+
#: () -> Array[PDF::Reader::TextRun]
7880
def exclude
7981
return text_runs if filter_hash.empty?
8082
text_runs.reject { |text_run| evaluate_filter(text_run) }
8183
end
8284

8385
private
8486

87+
#: (PDF::Reader::TextRun) -> bool
8588
def evaluate_filter(text_run)
8689
if filter_hash[:or]
8790
evaluate_or_filters(text_run, filter_hash[:or])
@@ -92,24 +95,28 @@ def evaluate_filter(text_run)
9295
end
9396
end
9497

98+
#: (PDF::Reader::TextRun, Array[Hash[Symbol, untyped]]) -> bool
9599
def evaluate_or_filters(text_run, conditions)
96100
conditions.any? do |condition|
97101
evaluate_filters(text_run, condition)
98102
end
99103
end
100104

105+
#: (PDF::Reader::TextRun, Array[Hash[Symbol, untyped]]) -> bool
101106
def evaluate_and_filters(text_run, conditions)
102107
conditions.all? do |condition|
103108
evaluate_filters(text_run, condition)
104109
end
105110
end
106111

112+
#: (PDF::Reader::TextRun, Hash[Symbol, untyped]) -> bool
107113
def evaluate_filters(text_run, filter_hash)
108114
filter_hash.all? do |attribute, conditions|
109115
evaluate_attribute_conditions(text_run, attribute, conditions)
110116
end
111117
end
112118

119+
#: (PDF::Reader::TextRun, Symbol, Hash[Symbol, untyped]) -> bool
113120
def evaluate_attribute_conditions(text_run, attribute, conditions)
114121
conditions.all? do |operator, value|
115122
unless VALID_OPERATORS.include?(operator)
@@ -120,6 +127,7 @@ def evaluate_attribute_conditions(text_run, attribute, conditions)
120127
end
121128
end
122129

130+
#: (untyped, Symbol, untyped) -> bool
123131
def apply_operator(attribute_value, operator, filter_value)
124132
case operator
125133
when :equal

lib/pdf/reader/aes_v3_security_handler.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class AesV3SecurityHandler
1515
#: (String) -> void
1616
def initialize(key)
1717
@encrypt_key = key
18-
@cipher = "AES-256-CBC"
18+
@cipher = "AES-256-CBC" #: String
1919
end
2020

2121
##7.6.2 General Encryption Algorithm

lib/pdf/reader/buffer.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ class Buffer
8080
#: ((StringIO | Tempfile | IO), ?Hash[Symbol, untyped]) -> void
8181
def initialize(io, opts = {})
8282
@io = io
83-
@tokens = []
84-
@in_content_stream = opts[:content_stream]
83+
@tokens = [] #: Array[String | PDF::Reader::Reference]
84+
@in_content_stream = opts[:content_stream] #: bool
8585

8686
@io.seek(opts[:seek]) if opts[:seek]
87-
@pos = @io.pos
87+
@pos = @io.pos #: Integer
8888
end
8989

9090
# return true if there are no more tokens left
@@ -202,6 +202,7 @@ def save_pos
202202

203203
# attempt to prime the buffer with the next few tokens.
204204
#
205+
#: () -> void
205206
def prepare_tokens
206207
10.times do
207208
case state
@@ -218,6 +219,7 @@ def prepare_tokens
218219
# tokenising behaves slightly differently based on the current context.
219220
# Determine the current context/state by examining the last token we found
220221
#
222+
#: () -> Symbol
221223
def state
222224
case @tokens.last
223225
when LEFT_PAREN then :literal_string
@@ -246,6 +248,7 @@ def state
246248
# indirect reference, so test for that case first and avoid the relatively
247249
# expensive regexp checks if possible.
248250
#
251+
#: () -> void
249252
def merge_indirect_reference
250253
return if @tokens.size < 3
251254
return if @tokens[2] != "R"
@@ -263,6 +266,7 @@ def merge_indirect_reference
263266
# If the EI follows white-space the space is dropped from the data
264267
# The EI must followed by white-space or end of buffer
265268
# This is to reduce the chance of accidentally matching an embedded EI
269+
#: () -> void
266270
def prepare_inline_token
267271
idstart = @io.pos
268272
prevchr = ''
@@ -309,6 +313,7 @@ def prepare_inline_token
309313
# if we're currently inside a hex string, read hex nibbles until
310314
# we find a closing >
311315
#
316+
#: () -> void
312317
def prepare_hex_token
313318
str = "".dup
314319

@@ -338,6 +343,7 @@ def prepare_hex_token
338343
# processing to fix things like escaped new lines, but that's someone else's
339344
# problem.
340345
#
346+
#: () -> void
341347
def prepare_literal_token
342348
str = "".dup
343349
count = 1
@@ -368,6 +374,7 @@ def prepare_literal_token
368374
# What each byte means is complex, check out section "3.1.1 Character Set" of the 1.7 spec
369375
# to read up on it.
370376
#
377+
#: () -> void
371378
def prepare_regular_token
372379
tok = "".dup
373380

@@ -445,6 +452,7 @@ def prepare_regular_token
445452
# peek at the next character in the io stream, leaving the stream position
446453
# untouched
447454
#
455+
#: () -> (Integer | nil)
448456
def peek_byte
449457
byte = @io.getbyte
450458
@io.seek(-1, IO::SEEK_CUR) if byte

lib/pdf/reader/cid_widths.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ class CidWidths
1818
# Graphics State Operators
1919
def_delegators :@widths, :[], :fetch
2020

21+
#: (Numeric, Array[Numeric]) -> void
2122
def initialize(default, array)
22-
@widths = parse_array(default, array.dup)
23+
@widths = parse_array(default, array.dup) #: Hash[Numeric, Numeric]
2324
end
2425

2526
private
2627

28+
#: (Numeric, Array[Numeric]) -> Hash[Numeric, Numeric]
2729
def parse_array(default, array)
2830
widths = Hash.new(default)
2931
params = []
@@ -43,6 +45,8 @@ def parse_array(default, array)
4345

4446
# this is the form 10 [234 63 234 346 47 234] where width of index 10 is
4547
# 234, index 11 is 63, etc
48+
#
49+
#: (Integer, Array[Numeric]) -> Hash[Numeric, Numeric]
4650
def parse_first_form(first, widths)
4751
widths.inject({}) { |accum, glyph_width|
4852
accum[first + accum.size] = glyph_width
@@ -51,6 +55,8 @@ def parse_first_form(first, widths)
5155
end
5256

5357
# this is the form 10 20 123 where all index between 10 and 20 have width 123
58+
#
59+
#: (Integer, Integer, Numeric) -> Hash[Numeric, Numeric]
5460
def parse_second_form(first, final, width)
5561
if first > final
5662
raise MalformedPDFError, "CidWidths: #{first} must be less than #{final}"

lib/pdf/reader/cmap.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class CMap # :nodoc:
4646
"def" => :noop
4747
} #: Hash[String, Symbol]
4848

49-
#: () -> Hash[Integer, Array[Integer]]
49+
#: Hash[Integer, Array[Integer]]
5050
attr_reader :map
5151

5252
#: (String) -> void
@@ -71,7 +71,7 @@ def decode(c)
7171

7272
private
7373

74-
#: (String, Symbol) -> void
74+
#: (String, ?Symbol) -> void
7575
def process_data(data, initial_mode = :none)
7676
parser = build_parser(data)
7777
mode = initial_mode

lib/pdf/reader/encoding.rb

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,23 @@ class Encoding # :nodoc:
4040

4141
#: (untyped) -> void
4242
def initialize(enc)
43-
@mapping = default_mapping # maps from character codes to Unicode codepoints
44-
@string_cache = {} # maps from character codes to UTF-8 strings.
43+
# maps from character codes to Unicode codepoints
44+
@mapping = default_mapping #: Hash[Integer, Integer]
4545

46-
@enc_name = if enc.kind_of?(Hash)
47-
enc[:Encoding] || enc[:BaseEncoding]
46+
# maps from character codes to UTF-8 strings.
47+
@string_cache = {} #: Hash[Integer, String]
48+
49+
@enc_name = :StandardEncoding #: Symbol
50+
if enc.kind_of?(Hash)
51+
@enc_name = enc[:Encoding] || enc[:BaseEncoding]
4852
elsif enc && enc.respond_to?(:to_sym)
49-
enc.to_sym
50-
else
51-
:StandardEncoding
53+
@enc_name = enc.to_sym
5254
end
5355

54-
@unpack = get_unpack(@enc_name)
55-
@map_file = get_mapping_file(@enc_name)
56+
@unpack = get_unpack(@enc_name) #: String
57+
@map_file = get_mapping_file(@enc_name) #: String | nil
58+
@differences = nil #: Hash[Integer, Integer] | nil
59+
@glyphlist = nil #: PDF::Reader::GlyphHash | nil
5660

5761
load_mapping(@map_file) if @map_file
5862

@@ -131,7 +135,12 @@ def int_to_name(glyph_code)
131135
elsif differences[glyph_code]
132136
[differences[glyph_code]]
133137
elsif @mapping[glyph_code]
134-
glyphlist.unicode_to_name(@mapping[glyph_code])
138+
val = @mapping[glyph_code]
139+
if val
140+
glyphlist.unicode_to_name(val)
141+
else
142+
[]
143+
end
135144
else
136145
[]
137146
end

lib/pdf/reader/error.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,36 @@ class PDF::Reader
3131
# are valid
3232
class Error # :nodoc:
3333
################################################################################
34+
#: (untyped, untyped, ?untyped) -> untyped
3435
def self.str_assert(lvalue, rvalue, chars=nil)
3536
raise MalformedPDFError, "PDF malformed, expected string but found #{lvalue.class} instead" if chars and !lvalue.kind_of?(String)
3637
lvalue = lvalue[0,chars] if chars
3738
raise MalformedPDFError, "PDF malformed, expected '#{rvalue}' but found '#{lvalue}' instead" if lvalue != rvalue
3839
end
3940
################################################################################
41+
#: (untyped, untyped, ?untyped) -> untyped
4042
def self.str_assert_not(lvalue, rvalue, chars=nil)
4143
raise MalformedPDFError, "PDF malformed, expected string but found #{lvalue.class} instead" if chars and !lvalue.kind_of?(String)
4244
lvalue = lvalue[0,chars] if chars
4345
raise MalformedPDFError, "PDF malformed, expected '#{rvalue}' but found '#{lvalue}' instead" if lvalue == rvalue
4446
end
4547
################################################################################
48+
#: (untyped, untyped) -> untyped
4649
def self.assert_equal(lvalue, rvalue)
4750
raise MalformedPDFError, "PDF malformed, expected '#{rvalue}' but found '#{lvalue}' instead" if lvalue != rvalue
4851
end
4952
################################################################################
53+
#: (Object, String, Module) -> void
5054
def self.validate_type(object, name, klass)
5155
raise ArgumentError, "#{name} (#{object}) must be a #{klass}" unless object.is_a?(klass)
5256
end
5357
################################################################################
58+
#: (Object, String, Module) -> void
5459
def self.validate_type_as_malformed(object, name, klass)
5560
raise MalformedPDFError, "#{name} (#{object}) must be a #{klass}" unless object.is_a?(klass)
5661
end
5762
################################################################################
63+
#: (Object, String) -> void
5864
def self.validate_not_nil(object, name)
5965
raise ArgumentError, "#{object} must not be nil" if object.nil?
6066
end

lib/pdf/reader/filter/depredict.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def filter(data)
3636
private
3737

3838
################################################################################
39+
#: (untyped) -> String
3940
def tiff_depredict(data)
4041
data = data.unpack("C*")
4142
unfiltered = ''
@@ -62,6 +63,7 @@ def tiff_depredict(data)
6263
unfiltered
6364
end
6465
################################################################################
66+
#: (untyped) -> String
6567
def png_depredict(data)
6668
return data if @options[:Predictor].to_i < 10
6769

lib/pdf/reader/font.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,34 @@ class PDF::Reader
3636
class Font
3737
#: Symbol?
3838
attr_accessor :subtype
39-
39+
4040
#: PDF::Reader::Encoding
4141
attr_accessor :encoding
42-
42+
4343
#: Array[PDF::Reader::Font]
4444
attr_accessor :descendantfonts
45-
45+
4646
#: PDF::Reader::CMap
4747
attr_accessor :tounicode
4848

4949
#: Array[Integer]
5050
attr_reader :widths
51-
51+
5252
#: Integer?
5353
attr_reader :first_char
54-
54+
5555
#: Integer?
5656
attr_reader :last_char
57-
57+
5858
#: Symbol?
5959
attr_reader :basefont
60-
60+
6161
#: PDF::Reader::FontDescriptor?
6262
attr_reader :font_descriptor
63-
63+
6464
#: Array[Numeric]
6565
attr_reader :cid_widths
66-
66+
6767
#: Numeric
6868
attr_reader :cid_default_width
6969

@@ -228,7 +228,7 @@ def extract_descriptor(obj)
228228
if obj[:FontDescriptor]
229229
# create a font descriptor object if we can, in other words, unless this is
230230
# a CID Font
231-
fd = @ohash.deref_hash(obj[:FontDescriptor])
231+
fd = @ohash.deref_hash(obj[:FontDescriptor]) || {}
232232
@font_descriptor = PDF::Reader::FontDescriptor.new(@ohash, fd)
233233
else
234234
@font_descriptor = nil
@@ -240,9 +240,9 @@ def extract_descendants(obj)
240240
# A one-element array specifying the CIDFont dictionary that is the
241241
# descendant of this Type 0 font.
242242
if obj[:DescendantFonts]
243-
descendants = @ohash.deref_array(obj[:DescendantFonts])
243+
descendants = @ohash.deref_array(obj[:DescendantFonts]) || []
244244
@descendantfonts = descendants.map { |desc|
245-
PDF::Reader::Font.new(@ohash, @ohash.deref_hash(desc))
245+
PDF::Reader::Font.new(@ohash, @ohash.deref_hash(desc) || {})
246246
}
247247
else
248248
@descendantfonts = []

0 commit comments

Comments
 (0)