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:



231
232
# File 'lib/hexapdf/dictionary_fields.rb', line 231

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.



236
237
238
239
240
241
242
243
244
# File 'lib/hexapdf/dictionary_fields.rb', line 236

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:



226
227
228
# File 'lib/hexapdf/dictionary_fields.rb', line 226

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