Class: ZIMG::PNG::Chunk::ITXT

Inherits:
TextChunk show all
Defined in:
lib/zimg/png/text_chunks.rb

Constant Summary

Constants inherited from TextChunk

TextChunk::INTEGER_CLASS

Constants inherited from ZIMG::PNG::Chunk

KNOWN_TYPES, VALID_SIZE_RANGE

Instance Attribute Summary collapse

Attributes inherited from TextChunk

#keyword, #text

Attributes inherited from ZIMG::PNG::Chunk

#crc, #data, #idx, #offset, #size, #type

Instance Method Summary collapse

Methods inherited from TextChunk

#inspect

Methods inherited from ZIMG::PNG::Chunk

#check, #crc_ok?, #export, #export_data, #fix_crc!, from_stream, #inspect, #valid?

Methods included from DeepCopyable

#deep_copy

Methods inherited from Chunk

#cli_color

Constructor Details

#initialize(*args) ⇒ ITXT

Returns a new instance of ITXT.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/zimg/png/text_chunks.rb', line 58

def initialize *args
  super
  # The text, unlike the other strings, is not null-terminated; its length is implied by the chunk length.
  # http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html#C.iTXt
  @keyword, @cflag, @cmethod, @language, @translated_keyword, @text = data.unpack("Z*CCZ*Z*a*")
  @text = Zlib::Inflate.inflate(@text) if @cflag == 1 && @cmethod == 0
  if @text
    begin
      @text.force_encoding("utf-8")
    rescue StandardError
      nil
    end
  end
  return unless @translated_keyword

  begin
    @translated_keyword.force_encoding("utf-8")
  rescue StandardError
    nil
  end
end

Instance Attribute Details

#cflagObject

compression flag & method



56
57
58
# File 'lib/zimg/png/text_chunks.rb', line 56

def cflag
  @cflag
end

#cmethodObject

compression flag & method



56
57
58
# File 'lib/zimg/png/text_chunks.rb', line 56

def cmethod
  @cmethod
end

#languageObject

compression flag & method



56
57
58
# File 'lib/zimg/png/text_chunks.rb', line 56

def language
  @language
end

#translated_keywordObject

compression flag & method



56
57
58
# File 'lib/zimg/png/text_chunks.rb', line 56

def translated_keyword
  @translated_keyword
end

Instance Method Details

#to_hashObject



80
81
82
83
84
85
# File 'lib/zimg/png/text_chunks.rb', line 80

def to_hash
  super.tap do |h|
    h[:language] = @language if @language || !@language.empty?
    h[:translated_keyword] = @translated_keyword if @translated_keyword || !@translated_keyword.empty?
  end
end