Class: Pdfrb::Document::Images

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

Overview

Image facade. add dispatches to ImageLoader.load which auto-detects format from the bytes (JPEG, PNG, or PDF page).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Images

Returns a new instance of Images.



12
13
14
15
16
# File 'lib/pdfrb/document/images.rb', line 12

def initialize(document)
  @document = document
  @registry = {}        # resource name -> image Object
  @next_id = 1
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



10
11
12
# File 'lib/pdfrb/document/images.rb', line 10

def document
  @document
end

#registryObject (readonly)

Returns the value of attribute registry.



10
11
12
# File 'lib/pdfrb/document/images.rb', line 10

def registry
  @registry
end

Instance Method Details

#[](name) ⇒ Object



29
30
31
# File 'lib/pdfrb/document/images.rb', line 29

def [](name)
  registry[name]
end

#add(io, **opts) ⇒ Object

Build an Image XObject from io (path, IO, or bytes) and register it in the document's /Resources/XObject. Returns the resource name (e.g. :Im1).



21
22
23
24
25
26
27
# File 'lib/pdfrb/document/images.rb', line 21

def add(io, **opts)
  image = Pdfrb::ImageLoader.load(document, io, **opts)
  name = next_resource_name
  attach_to_resources(name, image)
  registry[name] = image
  name
end

#each(&block) ⇒ Object



33
34
35
36
37
38
# File 'lib/pdfrb/document/images.rb', line 33

def each(&block)
  return enum_for(:each) unless block_given?

  registry.each(&block)
  self
end