module HexaPDF::Filter::ASCIIHexDecode

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, PDF1.7 s7.4.2

Public Class Methods

decoder(source, _ = nil) click to toggle source

See HexaPDF::Filter

# File lib/hexapdf/filter/ascii_hex_decode.rb, line 50
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) click to toggle source

See HexaPDF::Filter

# File lib/hexapdf/filter/ascii_hex_decode.rb, line 72
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