Class: HexaPDF::Layout::Style::Layers

Inherits:
Object
  • Object
show all
Defined in:
lib/hexapdf/layout/style.rb

Overview

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

There are two ways to specify layers via #add:

  • Directly by providing a callable object.

  • By reference to a callable object or class in the 'style.layers_map' configuration option. The reference name is looked up in the configuration option using HexaPDF::Configuration#constantize. If the resulting object is a callable object, it is used; otherwise it is assumed that it is a class and an object is instantiated, passing in any options given on #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.

Instance Method Summary collapse

Constructor Details

#initialize(layers = []) ⇒ Layers

Creates a new Layers object popuplated with the given layers.



385
386
387
# File 'lib/hexapdf/layout/style.rb', line 385

def initialize(layers = [])
  @layers = layers
end

Instance Method Details

#add(name = nil, **options, &block) ⇒ Object

:call-seq:

layers.add {|canvas, box| block}
layers.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.



404
405
406
407
408
409
410
411
412
# File 'lib/hexapdf/layout/style.rb', line 404

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) ⇒ Object

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



415
416
417
418
419
420
421
422
423
# File 'lib/hexapdf/layout/style.rb', line 415

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) ⇒ Object

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



427
428
429
430
431
432
433
# File 'lib/hexapdf/layout/style.rb', line 427

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) ⇒ Object

Duplicates the array holding the layers.



390
391
392
393
# File 'lib/hexapdf/layout/style.rb', line 390

def initialize_copy(other)
  super
  @layers = @layers.dup
end

#none?Boolean

Returns true if there are no layers defined.

Returns:

  • (Boolean)


436
437
438
# File 'lib/hexapdf/layout/style.rb', line 436

def none?
  @layers.empty?
end