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


65
66
67
# File 'lib/prawn/document/internals.rb', line 65

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

#current_pageObject

Grabs the reference for the current page



53
54
55
# File 'lib/prawn/document/internals.rb', line 53

def current_page
  @active_stamp_dictionary || @store[@current_page]
end

#go_to_page(k) ⇒ Object

:nodoc:



99
100
101
102
103
# File 'lib/prawn/document/internals.rb', line 99

def go_to_page(k) # :nodoc:
  jump_to = @store.pages.data[:Kids][k]
  @current_page = jump_to.identifier
  @page_content = jump_to.data[:Contents].identifier
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.



95
96
97
# File 'lib/prawn/document/internals.rb', line 95

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

#page_contentObject

Grabs the reference for the current page content



47
48
49
# File 'lib/prawn/document/internals.rb', line 47

def page_content
  @active_stamp_stream || @store[@page_content]
end

#page_ext_gstatesObject



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

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

#page_fontsObject

The Font dictionary for the current page



77
78
79
# File 'lib/prawn/document/internals.rb', line 77

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

#page_resourcesObject

The Resources dictionary for the current page



71
72
73
# File 'lib/prawn/document/internals.rb', line 71

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

#page_xobjectsObject

The XObject dictionary for the current page



83
84
85
# File 'lib/prawn/document/internals.rb', line 83

def page_xobjects
  page_resources[:XObject] ||= {}
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