Module: Prawn::Document::Internals

Included in:
Prawn::Document
Defined in:
lib/prawn/document/internals.rb

Overview

This module exposes a few low-level PDF features for those who want to extend Prawn’s core functionality. If you are not comfortable with low level PDF functionality as defined by Adobe’s specification, chances are you won’t need anything you find here.

Instance Method Summary collapse

Instance Method Details

#add_content(str) ⇒ Object

Appends a raw string to the current page content.

# Raw line drawing example:           
x1,y1,x2,y2 = 100,500,300,550
pdf.add_content("%.3f %.3f m" % [ x1, y1 ])  # move 
pdf.add_content("%.3f %.3f l" % [ x2, y2 ])  # draw path
pdf.add_content("S") # stroke


73
74
75
# File 'lib/prawn/document/internals.rb', line 73

def add_content(str)
  page_content << str << "\n"
end

#current_pageObject

Grabs the reference for the current page



61
62
63
# File 'lib/prawn/document/internals.rb', line 61

def current_page
  @store[@current_page]
end

#namesObject

The Name dictionary (PDF spec 3.6.3) for this document. It is lazily initialized, so that documents that do not need a name dictionary do not incur the additional overhead.



110
111
112
# File 'lib/prawn/document/internals.rb', line 110

def names
  @store.root.data[:Names] ||= ref!(:Type => :Names)
end

#page_contentObject

Grabs the reference for the current page content



55
56
57
# File 'lib/prawn/document/internals.rb', line 55

def page_content
  @store[@page_content]
end

#page_ext_gstatesObject



102
103
104
# File 'lib/prawn/document/internals.rb', line 102

def page_ext_gstates
  page_resources[:ExtGState] ||= {}
end

#page_fontsObject

The Font dictionary for the current page



92
93
94
# File 'lib/prawn/document/internals.rb', line 92

def page_fonts
  page_resources[:Font] ||= {}
end

#page_resourcesObject

The Resources dictionary for the current page



86
87
88
# File 'lib/prawn/document/internals.rb', line 86

def page_resources
  current_page.data[:Resources] ||= {}
end

#page_xobjectsObject

The XObject dictionary for the current page



98
99
100
# File 'lib/prawn/document/internals.rb', line 98

def page_xobjects
  page_resources[:XObject] ||= {}
end

#proc_set(*types) ⇒ Object

Add a new type to the current pages ProcSet



79
80
81
82
# File 'lib/prawn/document/internals.rb', line 79

def proc_set(*types)
  current_page.data[:ProcSet] ||= ref!([])
  current_page.data[:ProcSet].data |= types
end

#ref(data, &block) ⇒ Object

Creates a new Prawn::Reference and adds it to the Document’s object list. The data argument is anything that Prawn::PdfObject() can convert.

Returns the identifier which points to the reference in the ObjectStore

If a block is given, it will be invoked just before the object is written out to the PDF document stream. This allows you to do deferred processing on some references (such as fonts, which you might know all the details about until the last page of the document is finished).



29
30
31
# File 'lib/prawn/document/internals.rb', line 29

def ref(data, &block)
  ref!(data, &block).identifier
end

#ref!(data, &block) ⇒ Object

Like ref, but returns the actual reference instead of its identifier.

While you can use this to build up nested references within the object tree, it is recommended to persist only identifiers, and them provide helper methods to look up the actual references in the ObjectStore if needed. If you take this approach, Prawn::Document::Snapshot will probably work with your extension



41
42
43
# File 'lib/prawn/document/internals.rb', line 41

def ref!(data, &block)
  @store.ref(data, &block)
end