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.

See: PDF2.0 s7.9.2

Class Method Summary collapse

Class Method Details

.additional_typesObject

:nodoc:



250
251
# File 'lib/hexapdf/dictionary_fields.rb', line 250

def self.additional_types
end

.convert(str, _type, document) ⇒ Object

Converts the string into UTF-8 encoding, assuming it is a binary string (i.e. one not yet converted). Otherwise returns nil.



255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/hexapdf/dictionary_fields.rb', line 255

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 = str[2..-1].force_encoding(Encoding::UTF_16BE)
    if str.valid_encoding?
      str.encode!(Encoding::UTF_8)
    else
      document.config['document.on_invalid_string'].call(str)
    end
  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:



245
246
247
# File 'lib/hexapdf/dictionary_fields.rb', line 245

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