Class: Rpdfium::Annotation

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

Overview

Wrapper per FPDF_ANNOTATION. Le annotazioni includono link, highlight, commenti, widget di form. PDFium richiede di chiudere ogni handle con FPDFPage_CloseAnnot, gestito qui via finalizer.

Constant Summary collapse

SUBTYPES =
{
  Raw::FPDF_ANNOT_UNKNOWN => :unknown,
  Raw::FPDF_ANNOT_TEXT => :text,
  Raw::FPDF_ANNOT_LINK => :link,
  Raw::FPDF_ANNOT_FREETEXT => :free_text,
  Raw::FPDF_ANNOT_LINE => :line,
  Raw::FPDF_ANNOT_SQUARE => :square,
  Raw::FPDF_ANNOT_CIRCLE => :circle,
  Raw::FPDF_ANNOT_HIGHLIGHT => :highlight,
  Raw::FPDF_ANNOT_UNDERLINE => :underline,
  Raw::FPDF_ANNOT_SQUIGGLY => :squiggly,
  Raw::FPDF_ANNOT_STRIKEOUT => :strikeout,
  Raw::FPDF_ANNOT_STAMP => :stamp,
  Raw::FPDF_ANNOT_INK => :ink,
  Raw::FPDF_ANNOT_POPUP => :popup,
  Raw::FPDF_ANNOT_FILEATTACHMENT => :file_attachment,
  Raw::FPDF_ANNOT_WIDGET => :widget,
  Raw::FPDF_ANNOT_REDACT => :redact
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, index) ⇒ Annotation

Returns a new instance of Annotation.

Raises:



30
31
32
33
34
35
36
37
38
# File 'lib/rpdfium/annotation/annotation.rb', line 30

def initialize(page, index)
  @page   = page
  @index  = index
  handle  = Raw.FPDFPage_GetAnnot(page.handle, index)
  raise Error, "Could not load annotation #{index}" if handle.null?

  @state = { handle: handle, closed: false }
  ObjectSpace.define_finalizer(self, self.class.finalizer(@state))
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



28
29
30
# File 'lib/rpdfium/annotation/annotation.rb', line 28

def index
  @index
end

#pageObject (readonly)

Returns the value of attribute page.



28
29
30
# File 'lib/rpdfium/annotation/annotation.rb', line 28

def page
  @page
end

Class Method Details

.finalizer(state) ⇒ Object



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

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

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

Instance Method Details

#[](key) ⇒ Object

Valore di una chiave del dict di annotazione (UTF-16LE). Chiavi comuni: “Contents” (testo annotazione), “T” (autore), “M” (mod date), “NM” (uniq name).



70
71
72
# File 'lib/rpdfium/annotation/annotation.rb', line 70

def [](key)
  Raw.read_utf16_string(:FPDFAnnot_GetStringValue, @state[:handle], key.to_s)
end

#bboxObject



58
59
60
61
62
63
64
65
# File 'lib/rpdfium/annotation/annotation.rb', line 58

def bbox
  rect = Raw::FS_RECTF.new
  return nil if Raw.FPDFAnnot_GetRect(@state[:handle], rect) == 0

  h = @page.height
  { x0: rect[:left], x1: rect[:right],
    top: h - rect[:top], bottom: h - rect[:bottom] }
end

#closeObject



105
106
107
108
109
110
111
112
# File 'lib/rpdfium/annotation/annotation.rb', line 105

def close
  return if @state[:closed]

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

#handleObject



50
51
52
# File 'lib/rpdfium/annotation/annotation.rb', line 50

def handle
  @state[:handle]
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/rpdfium/annotation/annotation.rb', line 74

def has_key?(key)
  Raw.FPDFAnnot_HasKey(@state[:handle], key.to_s) == 1
end

Per link interni → indice pagina di destinazione, o nil.



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rpdfium/annotation/annotation.rb', line 92

def link_dest_page
  return nil unless subtype == :link

  link_handle = Raw.FPDFAnnot_GetLink(@state[:handle])
  return nil if link_handle.null?

  dest = Raw.FPDFLink_GetDest(@page.document.handle, link_handle)
  return nil if dest.null?

  idx = Raw.FPDFDest_GetDestPageIndex(@page.document.handle, dest)
  idx >= 0 ? idx : nil
end

Per annotazioni :link → URL di destinazione (se esterno) o nil.



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rpdfium/annotation/annotation.rb', line 79

def link_uri
  return nil unless subtype == :link

  link_handle = Raw.FPDFAnnot_GetLink(@state[:handle])
  return nil if link_handle.null?

  action = Raw.FPDFLink_GetAction(link_handle)
  return nil if action.null?

  Raw.read_utf16_string(:FPDFAction_GetURIPath, @page.document.handle, action)
end

#subtypeObject



54
55
56
# File 'lib/rpdfium/annotation/annotation.rb', line 54

def subtype
  SUBTYPES[Raw.FPDFAnnot_GetSubtype(@state[:handle])] || :unknown
end