Class: Arrolio::Flowables::ImageFlowable

Inherits:
Arrolio::Flowable show all
Defined in:
lib/arrolio/flowables/image_flowable.rb

Overview

Renders a PNG / JPEG / SVG via Pdfrb's image registry. Scales to display_width while preserving aspect ratio; if display_width is nil, scales to fit the available width.

Image registration is deferred to the render pass: the PlacedBox carries the source path, and the renderer resolves it (including SVG-to-PNG rasterization if needed) when it walks the page tree.

Instance Attribute Summary collapse

Attributes inherited from Arrolio::Flowable

#style

Instance Method Summary collapse

Methods inherited from Arrolio::Flowable

#do_split, #keep_together?, #keep_with_next?, #page_break_after?, #page_break_before?, #space_after, #space_before, #split, #splittable?

Constructor Details

#initialize(src, natural_width:, natural_height:, display_width: nil, alt: nil, style: Style::Definition.new) ⇒ ImageFlowable

Returns a new instance of ImageFlowable.



15
16
17
18
19
20
21
22
23
24
# File 'lib/arrolio/flowables/image_flowable.rb', line 15

def initialize(src, natural_width:, natural_height:,
               display_width: nil, alt: nil,
               style: Style::Definition.new)
  super(style: style)
  @src = src.to_s
  @natural_width = natural_width.to_f
  @natural_height = natural_height.to_f
  @display_width = display_width
  @alt = alt
end

Instance Attribute Details

#altObject (readonly)

Returns the value of attribute alt.



13
14
15
# File 'lib/arrolio/flowables/image_flowable.rb', line 13

def alt
  @alt
end

#display_widthObject (readonly)

Returns the value of attribute display_width.



13
14
15
# File 'lib/arrolio/flowables/image_flowable.rb', line 13

def display_width
  @display_width
end

#natural_heightObject (readonly)

Returns the value of attribute natural_height.



13
14
15
# File 'lib/arrolio/flowables/image_flowable.rb', line 13

def natural_height
  @natural_height
end

#natural_widthObject (readonly)

Returns the value of attribute natural_width.



13
14
15
# File 'lib/arrolio/flowables/image_flowable.rb', line 13

def natural_width
  @natural_width
end

#srcObject (readonly)

Returns the value of attribute src.



13
14
15
# File 'lib/arrolio/flowables/image_flowable.rb', line 13

def src
  @src
end

Instance Method Details

#emit(x, y, width, _context = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/arrolio/flowables/image_flowable.rb', line 33

def emit(x, y, width, _context = nil)
  return [[], 0.0] if @natural_width.zero?

  actual_w = actual_width(width)
  actual_h = actual_w * (@natural_height / @natural_width)
  box = Output::PlacedBox.image(
    x: x, y: y - actual_h,
    width: actual_w, height: actual_h,
    image_ref: @src
  )
  [[box], actual_h]
end

#height(width, _context = nil) ⇒ Object



26
27
28
29
30
31
# File 'lib/arrolio/flowables/image_flowable.rb', line 26

def height(width, _context = nil)
  return 0.0 if @natural_width.zero?

  actual_w = actual_width(width)
  actual_w * (@natural_height / @natural_width)
end