Class: ChunkyPNG::Chunk::InternationalText
- Defined in:
- lib/chunky_png/chunk.rb
Overview
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.
Instance Attribute Summary collapse
-
#compressed ⇒ Object
Returns the value of attribute compressed.
-
#compression ⇒ Object
Returns the value of attribute compression.
-
#keyword ⇒ Object
Returns the value of attribute keyword.
-
#language_tag ⇒ Object
Returns the value of attribute language_tag.
-
#text ⇒ Object
Returns the value of attribute text.
-
#translated_keyword ⇒ Object
Returns the value of attribute translated_keyword.
Attributes inherited from Base
Class Method Summary collapse
-
.read(type, content) ⇒ ChunkyPNG::Chunk::InternationalText
Reads the iTXt chunk.
Instance Method Summary collapse
-
#content ⇒ String
Assembles the content to write to the stream for this chunk.
-
#initialize(keyword, text, language_tag = "", translated_keyword = "", compressed = ChunkyPNG::UNCOMPRESSED_CONTENT, compression = ChunkyPNG::COMPRESSION_DEFAULT) ⇒ InternationalText
constructor
A new instance of InternationalText.
Methods inherited from Base
Constructor Details
#initialize(keyword, text, language_tag = "", translated_keyword = "", compressed = ChunkyPNG::UNCOMPRESSED_CONTENT, compression = ChunkyPNG::COMPRESSION_DEFAULT) ⇒ InternationalText
Returns a new instance of InternationalText.
410 411 412 413 414 415 416 417 418 |
# File 'lib/chunky_png/chunk.rb', line 410 def initialize(keyword, text, language_tag = "", translated_keyword = "", compressed = ChunkyPNG::UNCOMPRESSED_CONTENT, compression = ChunkyPNG::COMPRESSION_DEFAULT) super("iTXt") @keyword = keyword @text = text @language_tag = language_tag @translated_keyword = translated_keyword @compressed = compressed @compression = compression end |
Instance Attribute Details
#compressed ⇒ Object
Returns the value of attribute compressed.
408 409 410 |
# File 'lib/chunky_png/chunk.rb', line 408 def compressed @compressed end |
#compression ⇒ Object
Returns the value of attribute compression.
408 409 410 |
# File 'lib/chunky_png/chunk.rb', line 408 def compression @compression end |
#keyword ⇒ Object
Returns the value of attribute keyword.
408 409 410 |
# File 'lib/chunky_png/chunk.rb', line 408 def keyword @keyword end |
#language_tag ⇒ Object
Returns the value of attribute language_tag.
408 409 410 |
# File 'lib/chunky_png/chunk.rb', line 408 def language_tag @language_tag end |
#text ⇒ Object
Returns the value of attribute text.
408 409 410 |
# File 'lib/chunky_png/chunk.rb', line 408 def text @text end |
#translated_keyword ⇒ Object
Returns the value of attribute translated_keyword.
408 409 410 |
# File 'lib/chunky_png/chunk.rb', line 408 def translated_keyword @translated_keyword end |
Class Method Details
.read(type, content) ⇒ ChunkyPNG::Chunk::InternationalText
Reads the iTXt chunk.
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
# File 'lib/chunky_png/chunk.rb', line 427 def self.read(type, content) keyword, compressed, compression, language_tag, translated_keyword, text = content.unpack("Z*CCZ*Z*a*") raise ChunkyPNG::NotSupported, "Compression flag #{compressed.inspect} not supported!" unless compressed == ChunkyPNG::UNCOMPRESSED_CONTENT || compressed == ChunkyPNG::COMPRESSED_CONTENT raise ChunkyPNG::NotSupported, "Compression method #{compression.inspect} not supported!" unless compression == ChunkyPNG::COMPRESSION_DEFAULT text = Zlib::Inflate.inflate(text) if compressed == ChunkyPNG::COMPRESSED_CONTENT text.force_encoding("utf-8") raise ChunkyPNG::InvalidUTF8, "Invalid unicode encountered in iTXt chunk" unless text.valid_encoding? translated_keyword.force_encoding("utf-8") raise ChunkyPNG::InvalidUTF8, "Invalid unicode encountered in iTXt chunk" unless translated_keyword.valid_encoding? new(keyword, text, language_tag, translated_keyword, compressed, compression) end |
Instance Method Details
#content ⇒ String
Assembles the content to write to the stream for this chunk.
445 446 447 448 449 450 |
# File 'lib/chunky_png/chunk.rb', line 445 def content text_field = text.encode("utf-8") text_field = compressed == ChunkyPNG::COMPRESSED_CONTENT ? Zlib::Deflate.deflate(text_field) : text_field [keyword, compressed, compression, language_tag, translated_keyword.encode("utf-8"), text_field].pack("Z*CCZ*Z*a*") end |