Class: Pdfrb::Filter::ASCII85Decode
- Inherits:
-
Object
- Object
- Pdfrb::Filter::ASCII85Decode
- Includes:
- Base
- Defined in:
- lib/pdfrb/filter/ascii_85_decode.rb
Overview
ASCII85Decode (s7.4.3). Adobe's base-85 encoding with EOD ~>.
The z shorthand expands to four zero bytes.
Class Method Summary collapse
Class Method Details
.decoder(bytes, _parms, _document) ⇒ Object
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 |
# File 'lib/pdfrb/filter/ascii_85_decode.rb', line 13 def decoder(bytes, _parms, _document) out = +"" group = [] bytes.each_byte do |b| ch = b.chr case ch when "z" raise bad_byte(b) unless group.empty? out << "\x00\x00\x00\x00".b when "~" # EOD marker. Flush any partial group. break when " " else next unless (33..117).cover?(b) group << b if group.length == 5 out << decode_group(group) group.clear end end end unless group.empty? n = group.length padded = group + [117] * (5 - n) decoded = decode_group(padded) out << decoded.byteslice(0, n - 1) end out.force_encoding(Encoding::BINARY) end |
.encoder(bytes, _parms, _document) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/pdfrb/filter/ascii_85_decode.rb', line 46 def encoder(bytes, _parms, _document) out = +"" bytes.bytes.each_slice(4) do |quad| padded = quad + [0] * (4 - quad.length) num = padded[0] << 24 | padded[1] << 16 | padded[2] << 8 | padded[3] chars = +"" 5.times { chars << (num % 85 + 33).chr; num /= 85 } chars.reverse! n = quad.length + 1 out << chars.byteslice(0, n) end out << "~>" out.force_encoding(Encoding::BINARY) end |