Module: HexaPDF::Filter::ASCIIHexDecode

Defined in:
lib/hexapdf/filter/ascii_hex_decode.rb

Overview

This filter module implements the ASCII hex decode/encode filter which can encode arbitrary data into the two byte ASCII hex format that expands the original data by a factor of 1:2.

See: HexaPDF::Filter, PDF2.0 s7.4.2

Class Method Summary collapse

Class Method Details

.decoder(source, _ = nil) ⇒ Object

See HexaPDF::Filter



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hexapdf/filter/ascii_hex_decode.rb', line 51

def self.decoder(source, _ = nil)
  Fiber.new do
    rest = nil
    finished = false

    while !finished && source.alive? && (data = source.resume)
      data.tr!(HexaPDF::Tokenizer::WHITESPACE, '')
      finished = true if data.gsub!(/>.*?\z/m, '')
      if data.index(/[^A-Fa-f0-9]/)
        raise FilterError, "Invalid characters in ASCII hex stream"
      end

      data = rest << data if rest
      rest = (data.size.odd? ? data.slice!(-1, 1) : nil)

      Fiber.yield([data].pack('H*'))
    end
    [rest].pack('H*') if rest
  end
end

.encoder(source, _ = nil) ⇒ Object

See HexaPDF::Filter



73
74
75
76
77
78
79
80
# File 'lib/hexapdf/filter/ascii_hex_decode.rb', line 73

def self.encoder(source, _ = nil)
  Fiber.new do
    while source.alive? && (data = source.resume)
      Fiber.yield(data.unpack1('H*').force_encoding(Encoding::BINARY))
    end
    '>'.b
  end
end