lib/iptcr/iptc.rb in iptcr-0.1.0 vs lib/iptcr/iptc.rb in iptcr-0.1.1

- old
+ new

@@ -1,11 +1,9 @@ require "iptcr" require "iptcr/records" module IPTCR - class Malformed < RuntimeError; end - # # IPTC # # ## Notes # # IPTC metadata is a series of records. Records contain datasets. Each @@ -22,11 +20,12 @@ attr_reader :fields # IPTC IIM specifics the default encoding is ISO646/4873, which is roughly ASCII DEFAULT_ENCODING = Encoding::ASCII - def initialize(io, length:) + def initialize(io, length: nil, default_encoding: nil) + @encoding = default_encoding @fields = [] read_fields(io, length: length) end def encoding @@ -93,12 +92,14 @@ # and fall back to ASCII. if field.dataset? and field.dataset_name == "CodedCharacterSet" case field.value when "\x1b%G" @encoding = Encoding::UTF_8 + when "\x1b\xe2\x80\xa2A" + @encoding = Encoding::ISO_8859_1 # Latin 1 else - EXIFR.logger.warn { "IPTC: Unknown codec character set: #{field.value.inspect}" } + warn "IPTC: Unknown encoding: #{field.value.inspect}, using default #{encoding}" end end unless io.tell == finish io.seek(finish) @@ -174,13 +175,13 @@ def value case dataset_type when "string" # Record number 1 is always the default encoding if record_number == 1 - @data.force_encoding(DEFAULT_ENCODING) + @data.force_encoding(DEFAULT_ENCODING) unless @data.nil? # Records 2-6 and 8 respect tagged encoding elsif (2..6).include?(record_number) || record_number == 8 - @data.force_encoding(encoding) + @data.force_encoding(encoding) unless @data.nil? # Other behaviour is undefined else @data end when "digits"