Class: HexaPDF::Layout::PageStyle
- Inherits:
-
Object
- Object
- HexaPDF::Layout::PageStyle
- Defined in:
- lib/hexapdf/layout/page_style.rb
Overview
A PageStyle defines the initial look of a page and the placement of one or more frames.
Instance Attribute Summary collapse
-
#frame ⇒ Object
The HexaPDF::Layout::Frame object that defines the area on the page where content should be placed.
-
#next_style ⇒ Object
Defines the name of the page style that should be used for the next page.
-
#orientation ⇒ Object
The page orientation, either
:portraitor:landscape. -
#page_size ⇒ Object
The page size.
-
#template ⇒ Object
A callable object that defines the initial content of a page created with #create_page.
Instance Method Summary collapse
-
#create_frame(page, margin = 36) ⇒ Object
Creates a frame based on the given page’s box and margin.
-
#create_page(document) ⇒ Object
Creates a new page in the given document with this page style and returns it.
-
#initialize(page_size: :A4, orientation: :portrait, next_style: nil, &block) ⇒ PageStyle
constructor
Creates a new page style instance for the given page size, orientation and next style values.
Constructor Details
#initialize(page_size: :A4, orientation: :portrait, next_style: nil, &block) ⇒ PageStyle
Creates a new page style instance for the given page size, orientation and next style values. If a block is given, it is used as template for defining the initial content.
Example:
PageStyle.new(page_size: :Letter) do |canvas, style|
style.frame = style.create_frame(canvas.context, 72)
style.next_style = :other
canvas.fill_color("fd0") { canvas.circle(100, 100, 50).fill }
end
108 109 110 111 112 113 114 |
# File 'lib/hexapdf/layout/page_style.rb', line 108 def initialize(page_size: :A4, orientation: :portrait, next_style: nil, &block) @page_size = page_size @orientation = orientation @template = block @frame = nil @next_style = next_style end |
Instance Attribute Details
#frame ⇒ Object
The HexaPDF::Layout::Frame object that defines the area on the page where content should be placed.
This can either be set beforehand or during execution of the #template.
If no frame has been set, a frame covering the page except for a default margin on all sides is set during #create_page.
91 92 93 |
# File 'lib/hexapdf/layout/page_style.rb', line 91 def frame @frame end |
#next_style ⇒ Object
Defines the name of the page style that should be used for the next page.
If this attribute is nil (the default), it means that this style should be used again.
96 97 98 |
# File 'lib/hexapdf/layout/page_style.rb', line 96 def next_style @next_style end |
#orientation ⇒ Object
The page orientation, either :portrait or :landscape.
Only used if #page_size is one of the predefined page sizes and not an array.
61 62 63 |
# File 'lib/hexapdf/layout/page_style.rb', line 61 def orientation @orientation end |
#page_size ⇒ Object
The page size.
Can be any valid predefined page size (see HexaPDF::Type::Page::PAPER_SIZE) or an array
- llx, lly, urx, ury
-
specifying a custom page size.
Example:
style.page_size = :A4
style.page_size = [0, 0, 200, 200]
56 57 58 |
# File 'lib/hexapdf/layout/page_style.rb', line 56 def page_size @page_size end |
#template ⇒ Object
A callable object that defines the initial content of a page created with #create_page.
The callable object is given a canvas and the page style as arguments. It needs to draw the initial content of the page. Note that the graphics state of the canvas is not saved before executing the template code and restored afterwards. If this is needed, the object needs to do it itself.
Furthermore it should set the #frame and #next_style attributes appropriately, if not done beforehand. The #create_frame method can be used for easily creating a rectangular frame.
Example:
page_style.template = lambda do |canvas, style
box = canvas.context.box
canvas.fill_color("fd0") do
canvas.rectangle(0, 0, box.width, box.height).fill
end
style.frame = style.create_frame(canvas.context, 72)
end
82 83 84 |
# File 'lib/hexapdf/layout/page_style.rb', line 82 def template @template end |
Instance Method Details
#create_frame(page, margin = 36) ⇒ Object
Creates a frame based on the given page’s box and margin.
The margin can be any value allowed by HexaPDF::Layout::Style::Quad#set.
Note: This is a helper method for use inside the #template callable.
132 133 134 135 136 137 138 139 140 |
# File 'lib/hexapdf/layout/page_style.rb', line 132 def create_frame(page, margin = 36) box = page.box margin = Layout::Style::Quad.new(margin) Layout::Frame.new(box.left + margin.left, box.bottom + margin.bottom, box.width - margin.left - margin.right, box.height - margin.bottom - margin.top, context: page) end |
#create_page(document) ⇒ Object
Creates a new page in the given document with this page style and returns it.
If #frame has not been set beforehand or during execution of the #template, a default frame covering the whole page except a margin of 36 is created.
120 121 122 123 124 125 |
# File 'lib/hexapdf/layout/page_style.rb', line 120 def create_page(document) page = document.pages.create(media_box: page_size, orientation: orientation) template&.call(page.canvas, self) self.frame ||= create_frame(page, 36) page end |