Class: HexaPDF::Layout::ImageBox
- Defined in:
- lib/hexapdf/layout/image_box.rb
Overview
An Image box object is used for displaying an image.
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.
-
If both have been set, both are used as is.
-
If neither has been set, the image is scaled to fit the available space.
Also see: HexaPDF::Content::Canvas#image
Instance Attribute Summary collapse
-
#image ⇒ Object
readonly
The image that is shown in the box.
Attributes inherited from Box
Instance Method Summary collapse
-
#fit(available_width, available_height, _) ⇒ Object
Fits the image into the available space.
-
#initialize(image, **kwargs) ⇒ ImageBox
constructor
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).
Methods inherited from Box
#content_height, #content_width, create, #draw, #empty?, #split
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).
58 59 60 61 |
# File 'lib/hexapdf/layout/image_box.rb', line 58 def initialize(image, **kwargs) super(**kwargs, &:unused_draw_block) @image = image end |
Instance Attribute Details
#image ⇒ Object (readonly)
The image that is shown in the box.
54 55 56 |
# File 'lib/hexapdf/layout/image_box.rb', line 54 def image @image end |
Instance Method Details
#fit(available_width, available_height, _) ⇒ Object
Fits the image into the available space.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/hexapdf/layout/image_box.rb', line 64 def fit(available_width, available_height, _) 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 @width <= available_width && @height <= available_height end |