Class: HexaPDF::Layout::ImageBox

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

Overview

An Image box object is used for displaying an image.

It can either be used directly or through the HexaPDF::Composer#image method.

How an image is displayed inside an image box, depends on whether the width and/or height of the box has been set:

  • If one of them has been set, the other is adjusted to retain the image ratio.

    #>pdf-composer100
    composer.image(machu_picchu, width: 40)
    composer.image(machu_picchu, height: 40)
    
  • If both have been set, both are used as is.

    #>pdf-composer100
    composer.image(machu_picchu, width: 100, height: 30)
    
  • If neither has been set, the image is scaled to fit the current region.

    #>pdf-composer100
    composer.image(machu_picchu)
    

Also see: HexaPDF::Content::Canvas#image

Constant Summary

Constants included from Utils

Utils::EPSILON

Instance Attribute Summary collapse

Attributes inherited from Box

#height, #properties, #style, #width

Instance Method Summary collapse

Methods inherited from Box

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

Constructor Details

#initialize(image:, **kwargs) ⇒ ImageBox

Creates a new Image box object for the given image argument which needs to be an image object (e.g. returned by HexaPDF::Document::Images#add).



72
73
74
75
# File 'lib/hexapdf/layout/image_box.rb', line 72

def initialize(image:, **kwargs)
  super(**kwargs)
  @image = image
end

Instance Attribute Details

#imageObject (readonly)

The image that is shown in the box.



68
69
70
# File 'lib/hexapdf/layout/image_box.rb', line 68

def image
  @image
end

Instance Method Details

#empty?Boolean

Returns false since the image is always drawn if it fits.

Returns:

  • (Boolean)


78
79
80
# File 'lib/hexapdf/layout/image_box.rb', line 78

def empty?
  false
end

#fit(available_width, available_height, _frame) ⇒ Object

Fits the image into the current region of the frame, taking the initially set width and height into account (see the class description for details).



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/hexapdf/layout/image_box.rb', line 84

def fit(available_width, available_height, _frame)
  image_width = @image.width.to_f
  image_height = @image.height.to_f
  image_ratio = image_width / image_height

  if @initial_width > 0 && @initial_height > 0
    @width = @initial_width
    @height = @initial_height
  elsif @initial_width > 0
    @width = @initial_width
    @height = (@width - reserved_width) / image_ratio + reserved_height
  elsif @initial_height > 0
    @height = @initial_height
    @width = (@height - reserved_height) * image_ratio + reserved_width
  else
    rw = reserved_width
    rh = reserved_height
    ratio = [(available_width - rw) / image_width, (available_height - rh) / image_height].min
    @width = image_width * ratio + rw
    @height = image_height * ratio + rh
  end

  @fit_successful = float_compare(@width, available_width) <= 0 &&
    float_compare(@height, available_height) <= 0
end