Class: Rpdfium::Form::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/rpdfium/form/form.rb

Overview

FPDF_FORMHANDLE is required to read widget annotations. In read-only mode it is enough to initialize it with a minimal FORMFILLINFO (version=2, callbacks NULL). PDFium invokes the callbacks only during user interaction or JavaScript, which we do not use.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Environment

Returns a new instance of Environment.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rpdfium/form/form.rb', line 12

def initialize(document)
  @document = document
  @info = Raw::FPDF_FORMFILLINFO.new
  @info[:version] = 2
  # All pointers remain NULL (the FFI::Struct default).
  handle = Raw.FPDFDOC_InitFormFillEnvironment(document.handle, @info)
  if handle.null?
    raise FormError,
          "FPDFDOC_InitFormFillEnvironment failed (form_type=#{document.form_type})"
  end
  @state = { handle: handle, closed: false }
  ObjectSpace.define_finalizer(self, self.class.finalizer(@state))
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



10
11
12
# File 'lib/rpdfium/form/form.rb', line 10

def document
  @document
end

Class Method Details

.finalizer(state) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/rpdfium/form/form.rb', line 26

def self.finalizer(state)
  proc do
    next if state[:closed]
    next if state[:handle].null?

    Raw.FPDFDOC_ExitFormFillEnvironment(state[:handle])
    state[:closed] = true
  end
end

Instance Method Details

#closeObject



40
41
42
43
44
45
46
47
48
# File 'lib/rpdfium/form/form.rb', line 40

def close
  return if @state[:closed]

  Raw.FPDFDOC_ExitFormFillEnvironment(@state[:handle]) unless @state[:handle].null?
  @state[:handle] = FFI::Pointer::NULL
  @info = nil
  @state[:closed] = true
  ObjectSpace.undefine_finalizer(self)
end

#handleObject



36
37
38
# File 'lib/rpdfium/form/form.rb', line 36

def handle
  @state[:handle]
end