Class: HexaPDF::Layout::InlineBox
- Inherits:
-
Object
- Object
- HexaPDF::Layout::InlineBox
- 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.
Complete box auto-sizing is not possible since the available space cannot be determined beforehand! This means the box must have at least its width set. The height may either also be set or determined during fitting.
Fitting of the wrapped box via #fit_wrapped_box needs to be done before accessing any other method that uses the wrapped box. For fitting, a frame is used that has the width of the wrapped box and its height, or if not set, a practically infinite height. In the latter case the height must be set during fitting.
Instance Attribute Summary collapse
-
#box ⇒ Object
readonly
The wrapped Box object.
-
#valign ⇒ Object
readonly
The vertical alignment of the box.
Class Method Summary collapse
-
.create(valign: :baseline, **args, &block) ⇒ Object
Creates an InlineBox that wraps a basic Box.
Instance Method Summary collapse
-
#draw(canvas, x, y) ⇒ Object
Draws the wrapped box.
-
#empty? ⇒ Boolean
Returns
true
if this inline box is just a placeholder without drawing operations. -
#fit_wrapped_box(context) ⇒ Object
Fits the wrapped box, using the given context (see Frame#context).
-
#height ⇒ Object
Returns the height of the wrapped box plus its top and bottom margins.
-
#initialize(box, valign: :baseline) ⇒ InlineBox
constructor
Creates a new InlineBox object wrapping
box
. -
#style ⇒ Object
Returns the style of the wrapped box.
-
#width ⇒ Object
Returns the width of the wrapped box plus its left and right margins.
-
#x_max ⇒ Object
The maximum x-coordinate which is equivalent to the width of the inline box.
-
#x_min ⇒ Object
The minimum x-coordinate which is always 0.
-
#y_max ⇒ Object
The maximum y-coordinate which is equivalent to the height of the inline box.
-
#y_min ⇒ Object
The minimum y-coordinate which is always 0.
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.
76 77 78 79 80 |
# File 'lib/hexapdf/layout/inline_box.rb', line 76 def initialize(box, valign: :baseline) raise HexaPDF::Error, "Width of box not set" if box.width == 0 @box = box @valign = valign end |
Instance Attribute Details
#box ⇒ Object (readonly)
The wrapped Box object.
70 71 72 |
# File 'lib/hexapdf/layout/inline_box.rb', line 70 def box @box end |
#valign ⇒ Object (readonly)
The vertical alignment of the box.
Can be any supported value except :text - see Line for all possible values.
67 68 69 |
# File 'lib/hexapdf/layout/inline_box.rb', line 67 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.
60 61 62 |
# File 'lib/hexapdf/layout/inline_box.rb', line 60 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.
104 105 106 107 |
# File 'lib/hexapdf/layout/inline_box.rb', line 104 def draw(canvas, x, y) canvas.translate(x - @fit_result.x + box.style.margin.left, y - @fit_result.y + box.style.margin.bottom) { @fit_result.draw(canvas) } end |
#empty? ⇒ Boolean
Returns true
if this inline box is just a placeholder without drawing operations.
88 89 90 |
# File 'lib/hexapdf/layout/inline_box.rb', line 88 def empty? box.empty? end |
#fit_wrapped_box(context) ⇒ Object
Fits the wrapped box, using the given context (see Frame#context).
130 131 132 133 134 135 136 137 138 |
# File 'lib/hexapdf/layout/inline_box.rb', line 130 def fit_wrapped_box(context) @fit_result = Frame.new(0, 0, box.width, box.height == 0 ? 100_000 : box.height, context: context).fit(box) if !@fit_result.success? raise HexaPDF::Error, "Box for inline use could not be fit" elsif box.height > 99_000 raise HexaPDF::Error, "Box for inline use has no valid height set after fitting" end end |
#height ⇒ Object
Returns the height of the wrapped box plus its top and bottom margins.
98 99 100 |
# File 'lib/hexapdf/layout/inline_box.rb', line 98 def height box.height + box.style.margin.top + box.style.margin.bottom end |
#style ⇒ Object
Returns the style of the wrapped box.
83 84 85 |
# File 'lib/hexapdf/layout/inline_box.rb', line 83 def style box.style end |
#width ⇒ Object
Returns the width of the wrapped box plus its left and right margins.
93 94 95 |
# File 'lib/hexapdf/layout/inline_box.rb', line 93 def width box.width + box.style.margin.left + box.style.margin.right end |
#x_max ⇒ Object
The maximum x-coordinate which is equivalent to the width of the inline box.
115 116 117 |
# File 'lib/hexapdf/layout/inline_box.rb', line 115 def x_max width end |
#x_min ⇒ Object
The minimum x-coordinate which is always 0.
110 111 112 |
# File 'lib/hexapdf/layout/inline_box.rb', line 110 def x_min 0 end |
#y_max ⇒ Object
The maximum y-coordinate which is equivalent to the height of the inline box.
125 126 127 |
# File 'lib/hexapdf/layout/inline_box.rb', line 125 def y_max height end |
#y_min ⇒ Object
The minimum y-coordinate which is always 0.
120 121 122 |
# File 'lib/hexapdf/layout/inline_box.rb', line 120 def y_min 0 end |