Class: HexaPDF::Layout::InlineBox

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

Overview

An InlineBox wraps a regular Box so that it can be used as an item for a Line. This enables inline graphics.

When an inline box gets placed on a line, the method #fit_wrapped_box is called to fit the wrapped box. This allows the wrapped box to correctly set its width and height which are needed by the TextLayouter algorithm.

Note: It is mandatory that the wrapped box sets its width and height without relying on the dimensions of the frame’s current region.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(box, valign: :baseline) ⇒ InlineBox

Creates a new InlineBox object wrapping box.

The valign argument can be used to specify the vertical alignment of the box relative to other items in the Line.



74
75
76
77
# File 'lib/hexapdf/layout/inline_box.rb', line 74

def initialize(box, valign: :baseline)
  @box = box
  @valign = valign
end

Instance Attribute Details

#boxObject (readonly)

The wrapped Box object.



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

def box
  @box
end

#valignObject (readonly)

The vertical alignment of the box.

Can be any supported value except :text - see Line for all possible values.



65
66
67
# File 'lib/hexapdf/layout/inline_box.rb', line 65

def valign
  @valign
end

Class Method Details

.create(valign: :baseline, **args, &block) ⇒ Object

Creates an InlineBox that wraps a basic Box. All arguments (except valign) and the block are passed to Box::create.

See ::new for the valign argument.



58
59
60
# File 'lib/hexapdf/layout/inline_box.rb', line 58

def self.create(valign: :baseline, **args, &block)
  new(Box.create(**args, &block), valign: valign)
end

Instance Method Details

#draw(canvas, x, y) ⇒ Object

Draws the wrapped box. If the box has margins specified, the x and y offsets are correctly adjusted.



101
102
103
# File 'lib/hexapdf/layout/inline_box.rb', line 101

def draw(canvas, x, y)
  @fit_result.draw(canvas, dx: x, dy: y)
end

#empty?Boolean

Returns true if this inline box is just a placeholder without drawing operations.

Returns:

  • (Boolean)


85
86
87
# File 'lib/hexapdf/layout/inline_box.rb', line 85

def empty?
  box.empty?
end

#fit_wrapped_box(frame = nil) ⇒ Object

Fits the wrapped box.

If the frame argument is nil, a custom frame is created. Otherwise the given frame is used for creating an appropriate child frame for the fitting operation.

After this operation the caller is responsible for checking the actual width and height of the inline box and whether it really fits.



132
133
134
135
136
137
138
139
# File 'lib/hexapdf/layout/inline_box.rb', line 132

def fit_wrapped_box(frame = nil)
  @fit_result = box.fit(100_000, 100_000, frame || Frame.new(0, 0, 100_000, 100_000))
  margin = box.style.margin if box.style.margin?
  @fit_result.x = margin&.left.to_i
  @fit_result.y = margin&.bottom.to_i
  @fit_result.mask = Geom2D::Rectangle(0, 0, @fit_result.x + box.width + margin&.right.to_i,
                                       @fit_result.y + box.height + margin&.top.to_i)
end

#heightObject

Returns the height of the wrapped box plus its top and bottom margins.



95
96
97
# File 'lib/hexapdf/layout/inline_box.rb', line 95

def height
  box.height + box.style.margin.top + box.style.margin.bottom
end

#styleObject

Returns the style of the wrapped box.



80
81
82
# File 'lib/hexapdf/layout/inline_box.rb', line 80

def style
  box.style
end

#widthObject

Returns the width of the wrapped box plus its left and right margins.



90
91
92
# File 'lib/hexapdf/layout/inline_box.rb', line 90

def width
  box.width + box.style.margin.left + box.style.margin.right
end

#x_maxObject

The maximum x-coordinate which is equivalent to the width of the inline box.



111
112
113
# File 'lib/hexapdf/layout/inline_box.rb', line 111

def x_max
  width
end

#x_minObject

The minimum x-coordinate which is always 0.



106
107
108
# File 'lib/hexapdf/layout/inline_box.rb', line 106

def x_min
  0
end

#y_maxObject

The maximum y-coordinate which is equivalent to the height of the inline box.



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

def y_max
  height
end

#y_minObject

The minimum y-coordinate which is always 0.



116
117
118
# File 'lib/hexapdf/layout/inline_box.rb', line 116

def y_min
  0
end