Class: HexaPDF::Layout::ContainerBox

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

Overview

This is a simple container box for laying out a number of boxes together. It is registered under the :container name.

The box does not support the value :flow for the style property position, so the child boxes are laid out in the current region only. Since the boxes should be laid out together, if any box doesn’t fit, the whole container doesn’t fit. Splitting the container is also not possible for the same reason.

By default the child boxes are laid out from top to bottom by default. By appropriately setting the style properties ‘mask_mode’, ‘align’ and ‘valign’, it is possible to lay out the children bottom to top, left to right, or right to left:

  • The standard top-to-bottom layout:

    #>pdf-composer100
    composer.container do |container|
      container.box(:base, height: 20, style: {background_color: "hp-blue-dark"})
      container.box(:base, height: 20, style: {background_color: "hp-blue"})
      container.box(:base, height: 20, style: {background_color: "hp-blue-light"})
    end
    
  • The bottom-to-top layout (using valign = :bottom to fill up from the bottom and mask_mode = :fill_horizontal to only remove the area to the left and right of the box):

    #>pdf-composer100
    composer.container do |container|
      container.box(:base, height: 20, style: {background_color: "hp-blue-dark",
                                               mask_mode: :fill_horizontal, valign: :bottom})
      container.box(:base, height: 20, style: {background_color: "hp-blue",
                                               mask_mode: :fill_horizontal, valign: :bottom})
      container.box(:base, height: 20, style: {background_color: "hp-blue-light",
                                               mask_mode: :fill_horizontal, valign: :bottom})
    end
    
  • The left-to-right layout (using mask_mode = :fill_vertical to fill the area to the top and bottom of the box):

    #>pdf-composer100
    composer.container do |container|
      container.box(:base, width: 20, style: {background_color: "hp-blue-dark",
                                              mask_mode: :fill_vertical})
      container.box(:base, width: 20, style: {background_color: "hp-blue",
                                              mask_mode: :fill_vertical})
      container.box(:base, width: 20, style: {background_color: "hp-blue-light",
                                              mask_mode: :fill_vertical})
    end
    
  • The right-to-left layout (using align = :right to fill up from the right and mask_mode = :fill_vertical to fill the area to the top and bottom of the box):

    #>pdf-composer100
    composer.container do |container|
      container.box(:base, width: 20, style: {background_color: "hp-blue-dark",
                                              mask_mode: :fill_vertical, align: :right})
      container.box(:base, width: 20, style: {background_color: "hp-blue",
                                              mask_mode: :fill_vertical, align: :right})
      container.box(:base, width: 20, style: {background_color: "hp-blue-light",
                                              mask_mode: :fill_vertical, align: :right})
    end
    

Constant Summary

Constants included from Utils

Utils::EPSILON

Instance Attribute Summary collapse

Attributes inherited from Box

#fit_result, #height, #properties, #style, #width

Instance Method Summary collapse

Methods inherited from Box

#content_height, #content_width, create, #draw, #fit, #split, #split_box?, #supports_position_flow?

Constructor Details

#initialize(children: [], **kwargs) ⇒ ContainerBox

Creates a new container box, optionally accepting an array of child boxes.

Example:

#>pdf-composer100
composer.text("A paragraph here")
composer.container(height: 40, style: {border: {width: 1}, padding: 5,
                                       align: :center}) do |container|
  container.text("Some", mask_mode: :fill_vertical)
  container.text("text", mask_mode: :fill_vertical, valign: :center)
  container.text("here", mask_mode: :fill_vertical, valign: :bottom)
end
composer.text("Another paragraph")


120
121
122
123
# File 'lib/hexapdf/layout/container_box.rb', line 120

def initialize(children: [], **kwargs)
  super(**kwargs)
  @children = children
end

Instance Attribute Details

#childrenObject (readonly)

The child boxes of this ContainerBox. They need to be finalized before #fit is called.



105
106
107
# File 'lib/hexapdf/layout/container_box.rb', line 105

def children
  @children
end

Instance Method Details

#empty?Boolean

Returns true if no box was fitted into the container.

Returns:

  • (Boolean)


126
127
128
# File 'lib/hexapdf/layout/container_box.rb', line 126

def empty?
  super && (!@box_fitter || @box_fitter.fit_results.empty?)
end