lib/hexapdf/document/metadata.rb in hexapdf-0.37.1 vs lib/hexapdf/document/metadata.rb in hexapdf-0.37.2

- old
+ new

@@ -2,11 +2,11 @@ # #-- # This file is part of HexaPDF. # # HexaPDF - A Versatile PDF Creation and Manipulation Library For Ruby -# Copyright (C) 2014-2023 Thomas Leitner +# Copyright (C) 2014-2024 Thomas Leitner # # HexaPDF is free software: you can redistribute it and/or modify it # under the terms of the GNU Affero General Public License version 3 as # published by the Free Software Foundation with the addition of the # following permission added to Section 15 as permitted in Section 7(a): @@ -108,21 +108,23 @@ # See: PDF2.0 s14.3, https://www.adobe.com/products/xmp.html class Metadata # Represents a localized XMP string, i.e. as string with an attached language. class LocalizedString < String + # The language identifier for the string in RFC3066 format. attr_accessor :language + end # Contains a mapping of predefined prefixes for XMP namespaces for metadata. PREDEFINED_NAMESPACES = { "rdf" => "http://www.w3.org/1999/02/22-rdf-syntax-ns#", "xmp" => "http://ns.adobe.com/xap/1.0/", "pdf" => "http://ns.adobe.com/pdf/1.3/", - "dc" => "http://purl.org/dc/elements/1.1/", - "x" => "adobe:ns:meta/", + "dc" => "http://purl.org/dc/elements/1.1/", + "x" => "adobe:ns:meta/", }.freeze # Contains a mapping of predefined XMP properties to their types, i.e. from namespace to # property and then type. PREDEFINED_PROPERTIES = { @@ -141,16 +143,15 @@ 'description' => 'LanguageArray', 'title' => 'LanguageArray', }.freeze, }.freeze - # Creates a new Metadata object for the given PDF document. def initialize(document) @document = document @namespaces = PREDEFINED_NAMESPACES.dup - @properties = PREDEFINED_PROPERTIES.transform_values {|value| value.dup} + @properties = PREDEFINED_PROPERTIES.transform_values(&:dup) @default_language = document.catalog[:Lang] || 'en' @metadata = Hash.new {|h, k| h[k] = {} } write_info_dict(true) write_metadata_stream(true) @document.register_listener(:complete_objects, &method(:write_metadata)) @@ -428,15 +429,15 @@ end # Creates an XMP packet with the given payload +data+. def xmp_packet(data) <<~XMP - <?xpacket begin="\u{FEFF}" id="#{SecureRandom.uuid.tr('-', '')}"?> - <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> - #{data} - </rdf:RDF> - <?xpacket end="r"?> + <?xpacket begin="\u{FEFF}" id="#{SecureRandom.uuid.tr('-', '')}"?> + <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> + #{data} + </rdf:RDF> + <?xpacket end="r"?> XMP end # Creates an 'rdf:Description' element for all metadata +values+ with the given +ns_prefix+. def xmp_description(ns_prefix, values) @@ -463,12 +464,12 @@ str << "><rdf:#{el_type}>\n#{value}\n</rdf:#{el_type}></#{ns_prefix}:#{name}>" end str end.join("\n") <<~XMP.strip - <rdf:Description rdf:about="" xmlns:#{ns_prefix}="#{xmp_escape(namespace(ns_prefix))}"> - #{values} - </rdf:Description> + <rdf:Description rdf:about="" xmlns:#{ns_prefix}="#{xmp_escape(namespace(ns_prefix))}"> + #{values} + </rdf:Description> XMP end # Escapes the given value so as to be usable as XMP simple value. def xmp_escape(value)