Module: HexaPDF::DictionaryFields::StringConverter

Defined in:
lib/hexapdf/dictionary_fields.rb

Overview

Converter module for string fields to automatically convert a string into UTF-8 encoding.

Class Method Summary collapse

Class Method Details

.additional_typesObject

:nodoc:



207
208
# File 'lib/hexapdf/dictionary_fields.rb', line 207

def self.additional_types
end

.convert(str, _type, _document) ⇒ Object

Converts the string into UTF-8 encoding, assuming it is a binary string. Otherwise nil is returned.



212
213
214
215
216
217
218
219
220
# File 'lib/hexapdf/dictionary_fields.rb', line 212

def self.convert(str, _type, _document)
  return unless str.kind_of?(String) && str.encoding == Encoding::BINARY

  if str.getbyte(0) == 254 && str.getbyte(1) == 255
    str[2..-1].force_encoding(Encoding::UTF_16BE).encode(Encoding::UTF_8)
  else
    Utils::PDFDocEncoding.convert_to_utf8(str)
  end
end

.usable_for?(type) ⇒ Boolean

This converter is usable if the type is the String class.

Returns:



202
203
204
# File 'lib/hexapdf/dictionary_fields.rb', line 202

def self.usable_for?(type)
  type == String
end