Class: Pdfrb::Document::FormXObject
- Inherits:
-
Object
- Object
- Pdfrb::Document::FormXObject
- Defined in:
- lib/pdfrb/document/form_xobject.rb
Overview
Form XObject facade. Creates reusable form templates that can be drawn on multiple pages via the /Do operator. A Form XObject is a stream with /Type (omitted), /Subtype /Form, /BBox, and optional /Matrix + /Resources.
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
Instance Method Summary collapse
-
#canvas ⇒ Object
Returns a Canvas for drawing into this Form XObject.
-
#draw_on(target_canvas, at:, scale: 1.0) ⇒ Object
Draw this Form XObject on a canvas at the given position.
-
#finalize! ⇒ Object
Finalize: copy the canvas content into the Form XObject stream and register it in the document's resource names.
-
#initialize(document, name: nil, bbox: nil, matrix: nil) ⇒ FormXObject
constructor
A new instance of FormXObject.
-
#register_on_page(page) ⇒ Object
Register this Form XObject in a page's /Resources /XObject dict so it can be referenced via the /Do operator.
Constructor Details
#initialize(document, name: nil, bbox: nil, matrix: nil) ⇒ FormXObject
Returns a new instance of FormXObject.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/pdfrb/document/form_xobject.rb', line 12 def initialize(document, name: nil, bbox: nil, matrix: nil) @document = document @bbox = bbox || [0, 0, 612, 792] @matrix = matrix dict = { Subtype: :Form, BBox: @bbox } dict[:Matrix] = @matrix if @matrix @stream = document.add(dict, type: Pdfrb::Model::Cos::Stream) @name = (name || "Form#{@stream.oid}").to_sym @canvas = nil end |
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
10 11 12 |
# File 'lib/pdfrb/document/form_xobject.rb', line 10 def document @document end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/pdfrb/document/form_xobject.rb', line 10 def name @name end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
10 11 12 |
# File 'lib/pdfrb/document/form_xobject.rb', line 10 def stream @stream end |
Instance Method Details
#canvas ⇒ Object
Returns a Canvas for drawing into this Form XObject. The Canvas writes to the Form XObject's content stream.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/pdfrb/document/form_xobject.rb', line 26 def canvas return @canvas if @canvas contents_stream = @document.add({}, type: Pdfrb::Model::Cos::Stream) @stream.value[:Resources] = {} @canvas = Pdfrb::Content::Canvas.new(contents_stream, document: @document) @stream.stream = +"".b # placeholder; filled by ensure_stream_payload @contents_stream = contents_stream @canvas end |
#draw_on(target_canvas, at:, scale: 1.0) ⇒ Object
Draw this Form XObject on a canvas at the given position.
59 60 61 62 63 64 65 |
# File 'lib/pdfrb/document/form_xobject.rb', line 59 def draw_on(target_canvas, at:, scale: 1.0) target_canvas.emit_op(Pdfrb::Content::Operator::SaveGraphicsState) target_canvas.translate(at[0], at[1]) target_canvas.scale(scale) if (scale - 1.0).abs > 1e-9 target_canvas.append(" /#{@name} Do\n") target_canvas.emit_op(Pdfrb::Content::Operator::RestoreGraphicsState) end |
#finalize! ⇒ Object
Finalize: copy the canvas content into the Form XObject stream and register it in the document's resource names. Called automatically before write.
40 41 42 43 44 45 |
# File 'lib/pdfrb/document/form_xobject.rb', line 40 def finalize! return unless @contents_stream @stream.stream = @contents_stream.stream @stream.value[:Resources] = @contents_stream.value[:Resources] || {} end |
#register_on_page(page) ⇒ Object
Register this Form XObject in a page's /Resources /XObject dict so it can be referenced via the /Do operator.
49 50 51 52 53 54 55 56 |
# File 'lib/pdfrb/document/form_xobject.rb', line 49 def register_on_page(page) resources = page.value[:Resources] resources = {} unless resources.is_a?(::Hash) xobjects = resources[:XObject] || {} xobjects[@name] = Pdfrb::Model::Reference.new(@stream.oid, @stream.gen) resources[:XObject] = xobjects page.value[:Resources] = resources end |