Class: Pdfrb::Document::Layers
- Inherits:
-
Object
- Object
- Pdfrb::Document::Layers
- Defined in:
- lib/pdfrb/document/layers.rb
Overview
Facade for Optional Content Groups (layers). OCGs let authors mark content as belonging to a named layer that can be toggled on/off by the viewer (ISO 32000-2 §8.11).
Each layer is a /Type /OCG dictionary with a /Name. The Catalog gets an /OCProperties entry:
/OCGs — array of all OCG indirect refs.
/D — default viewing config:
/BaseState /ON, /OFF [list of off-by-default OCGs], etc.
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#groups ⇒ Object
readonly
Returns the value of attribute groups.
Instance Method Summary collapse
-
#add(name, default_on: true) ⇒ Object
Add a named layer.
-
#initialize(document) ⇒ Layers
constructor
A new instance of Layers.
-
#sync! ⇒ Object
Write /OCProperties to the Catalog.
Constructor Details
#initialize(document) ⇒ Layers
Returns a new instance of Layers.
17 18 19 20 |
# File 'lib/pdfrb/document/layers.rb', line 17 def initialize(document) @document = document @groups = [] end |
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
15 16 17 |
# File 'lib/pdfrb/document/layers.rb', line 15 def document @document end |
#groups ⇒ Object (readonly)
Returns the value of attribute groups.
15 16 17 |
# File 'lib/pdfrb/document/layers.rb', line 15 def groups @groups end |
Instance Method Details
#add(name, default_on: true) ⇒ Object
Add a named layer. @return [Pdfrb::Model::Cos::Dictionary] the OCG.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/pdfrb/document/layers.rb', line 23 def add(name, default_on: true) ensure_oc_properties ocg = document.add( { Type: :OCG, Name: name }, type: Pdfrb::Model::Cos::Dictionary ) ref = Pdfrb::Model::Reference.new(ocg.oid, ocg.gen) @groups << { ref: ref, ocg: ocg, default_on: default_on } ocg end |
#sync! ⇒ Object
Write /OCProperties to the Catalog. Called automatically by #add but can be called again to refresh after external modifications.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/pdfrb/document/layers.rb', line 36 def sync! return if @groups.empty? ocgs_array = @groups.map { |g| g[:ref] } off_array = @groups.reject { |g| g[:default_on] }.map { |g| g[:ref] } default_config = { BaseState: :ON } default_config[:OFF] = off_array unless off_array.empty? catalog = document.catalog catalog.value[:OCProperties] = { OCGs: ocgs_array, D: default_config, } end |