class HexaPDF::Layout::Style::Layers

Represents layers that can be drawn under or over a box.

There are two ways to specify layers via add:

The object resolved in this way needs to respond to call(canvas, box) where canvas is the HexaPDF::Content::Canvas object on which it should be drawn and box is a box-like object (e.g. Box or TextFragment). The coordinate system is translated so that the origin is at the bottom left corner of the box during the drawing operations.

Public Class Methods

new(layers = []) click to toggle source

Creates a new Layers object popuplated with the given layers.

# File lib/hexapdf/layout/style.rb, line 381
def initialize(layers = [])
  @layers = layers
end

Public Instance Methods

add {|canvas, box| block} click to toggle source
add(name, **options)

Adds a new layer object.

The layer object can either be specified as a block or by reference to a configured layer object in 'style.layers_map'. In this case name is used as the reference and the options are passed to layer object if it needs initialization.

# File lib/hexapdf/layout/style.rb, line 400
def add(name = nil, **options, &block)
  if block_given?
    @layers << block
  elsif name
    @layers << [name, options]
  else
    raise ArgumentError, "Layer object name or block missing"
  end
end
draw(canvas, x, y, box) click to toggle source

Draws all layer objects onto the canvas at the position [x, y] for the given box.

# File lib/hexapdf/layout/style.rb, line 411
def draw(canvas, x, y, box)
  return if none?

  canvas.translate(x, y) do
    each(canvas.context.document.config) do |layer|
      canvas.save_graphics_state { layer.call(canvas, box) }
    end
  end
end
each(config) { |layer| ... } click to toggle source

Yields all layer objects. Objects that have been specified via a reference are first resolved using the provided configuration object.

# File lib/hexapdf/layout/style.rb, line 423
def each(config) #:yield: layer
  @layers.each do |obj, options|
    obj = config.constantize('style.layers_map', obj) unless obj.respond_to?(:call)
    obj = obj.new(**options) unless obj.respond_to?(:call)
    yield(obj)
  end
end
initialize_copy(other) click to toggle source

Duplicates the array holding the layers.

Calls superclass method
# File lib/hexapdf/layout/style.rb, line 386
def initialize_copy(other)
  super
  @layers = @layers.dup
end
none?() click to toggle source

Returns true if there are no layers defined.

# File lib/hexapdf/layout/style.rb, line 432
def none?
  @layers.empty?
end