class ChunkyPNG::Chunk::InternationalText

The InternationalText (iTXt) chunk contains keyword/value metadata about the PNG stream, translated to a given locale.

The metadata in this chunk can be encoded using UTF-8 characters. Moreover, it is possible to define the language of the metadata, and give a translation of the keyword name. Finally, it supports bot compressed and uncompressed values.

@see www.w3.org/TR/PNG/#11iTXt @see ChunkyPNG::Chunk::Text @see ChunkyPNG::Chunk::CompressedText

Attributes

compressed[RW]
compression[RW]
keyword[RW]
language_tag[RW]
text[RW]
translated_keyword[RW]

Public Class Methods

new(keyword, text, language_tag = "", translated_keyword = "", compressed = ChunkyPNG::UNCOMPRESSED_CONTENT, compression = ChunkyPNG::COMPRESSION_DEFAULT) click to toggle source
Calls superclass method ChunkyPNG::Chunk::Base::new
    # File lib/chunky_png/chunk.rb
410 def initialize(keyword, text, language_tag = "", translated_keyword = "", compressed = ChunkyPNG::UNCOMPRESSED_CONTENT, compression = ChunkyPNG::COMPRESSION_DEFAULT)
411   super("iTXt")
412   @keyword = keyword
413   @text = text
414   @language_tag = language_tag
415   @translated_keyword = translated_keyword
416   @compressed = compressed
417   @compression = compression
418 end
read(type, content) click to toggle source

Reads the iTXt chunk. @param type [String] The four character chunk type indicator (= “iTXt”). @param content [String] The content read from the chunk. @return [ChunkyPNG::Chunk::InternationalText] The new End chunk instance. @raise [ChunkyPNG::InvalidUTF8] If the chunk contains data that is not UTF8-encoded text. @raise [ChunkyPNG::NotSupported] If the chunk refers to an unsupported compression method.

Currently uncompressed data and deflate are supported.
    # File lib/chunky_png/chunk.rb
427 def self.read(type, content)
428   keyword, compressed, compression, language_tag, translated_keyword, text = content.unpack("Z*CCZ*Z*a*")
429   raise ChunkyPNG::NotSupported, "Compression flag #{compressed.inspect} not supported!" unless compressed == ChunkyPNG::UNCOMPRESSED_CONTENT || compressed == ChunkyPNG::COMPRESSED_CONTENT
430   raise ChunkyPNG::NotSupported, "Compression method #{compression.inspect} not supported!" unless compression == ChunkyPNG::COMPRESSION_DEFAULT
431 
432   text = Zlib::Inflate.inflate(text) if compressed == ChunkyPNG::COMPRESSED_CONTENT
433 
434   text.force_encoding("utf-8")
435   raise ChunkyPNG::InvalidUTF8, "Invalid unicode encountered in iTXt chunk" unless text.valid_encoding?
436 
437   translated_keyword.force_encoding("utf-8")
438   raise ChunkyPNG::InvalidUTF8, "Invalid unicode encountered in iTXt chunk" unless translated_keyword.valid_encoding?
439 
440   new(keyword, text, language_tag, translated_keyword, compressed, compression)
441 end

Public Instance Methods

content() click to toggle source

Assembles the content to write to the stream for this chunk. @return [String] The binary content that should be written to the datastream.

    # File lib/chunky_png/chunk.rb
445 def content
446   text_field = text.encode("utf-8")
447   text_field = compressed == ChunkyPNG::COMPRESSED_CONTENT ? Zlib::Deflate.deflate(text_field) : text_field
448 
449   [keyword, compressed, compression, language_tag, translated_keyword.encode("utf-8"), text_field].pack("Z*CCZ*Z*a*")
450 end