Class: Pdfrb::Document::OutputIntents

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/document/output_intents.rb

Overview

Output Intents facade. An output intent declares the intended output device color profile (ICC) so that downstream processors can render colors consistently. Required for PDF/X.

/OutputIntents is an array on the Catalog. Each entry has:

/Type /OutputIntent
/S /GTS_PDFA1 (for PDF/A) or /GTS_PDFX (for PDF/X)
/OutputConditionIdentifier — registry name (e.g. "FOGRA39")
/OutputCondition 

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ OutputIntents



19
20
21
# File 'lib/pdfrb/document/output_intents.rb', line 19

def initialize(document)
  @document = document
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



17
18
19
# File 'lib/pdfrb/document/output_intents.rb', line 17

def document
  @document
end

Instance Method Details

#add(icc_stream_ref, identifier:, condition: nil, registry: nil, subtype: :GTS_PDFX) ⇒ Pdfrb::Model::Cos::Dictionary

Add an output intent referencing an ICC profile stream.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pdfrb/document/output_intents.rb', line 30

def add(icc_stream_ref, identifier:, condition: nil,
        registry: nil, subtype: :GTS_PDFX)
  intent = document.add(
    {
      Type: :OutputIntent,
      S: subtype,
      OutputConditionIdentifier: identifier,
      OutputCondition: condition,
      RegistryName: registry,
      DestOutputProfile: icc_stream_ref,
    }.compact,
    type: Pdfrb::Model::Cos::Dictionary
  )
  catalog = document.catalog
  intents = catalog.value[:OutputIntents]
  ref = intent.ref
  if intents.nil?
    catalog.value[:OutputIntents] = [ref]
  elsif intents.is_a?(::Array)
    intents << ref
  else
    catalog.value[:OutputIntents] = [intents, ref]
  end
  intent
end

#countObject



83
84
85
86
87
88
# File 'lib/pdfrb/document/output_intents.rb', line 83

def count
  intents = document.catalog.value[:OutputIntents]
  return 0 unless intents

  intents.is_a?(::Array) ? intents.length : 1
end

#each(&block) ⇒ Object

Enumerate output intents.



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pdfrb/document/output_intents.rb', line 71

def each(&block)
  intents = document.catalog.value[:OutputIntents]
  return enum_for(:each) unless block
  return self unless intents

  intents.each do |ref|
    obj = document.resolve(ref)
    yield obj if obj
  end
  self
end

#embed_icc(icc_bytes, identifier:, condition: nil, registry: nil, subtype: :GTS_PDFX) ⇒ Object

Convenience: embed an ICC profile and register as output intent.



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pdfrb/document/output_intents.rb', line 57

def embed_icc(icc_bytes, identifier:, condition: nil,
               registry: nil, subtype: :GTS_PDFX)
  profile = Pdfrb::Color::ICCProfile.new(icc_bytes)
  stream = document.add(
    { **profile.stream_dictionary_fields,
      Length: profile.raw_data.bytesize },
    type: Pdfrb::Model::Cos::Stream
  )
  stream.stream = profile.raw_data
  ref = stream.ref
  add(ref, identifier: identifier, condition: condition, registry: registry, subtype: subtype)
end