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.

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

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.

Raises:



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

#boxObject (readonly)

The wrapped Box object.



70
71
72
# File 'lib/hexapdf/layout/inline_box.rb', line 70

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.



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.

Returns:

  • (Boolean)


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

#heightObject

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

#styleObject

Returns the style of the wrapped box.



83
84
85
# File 'lib/hexapdf/layout/inline_box.rb', line 83

def style
  box.style
end

#widthObject

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_maxObject

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_minObject

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_maxObject

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_minObject

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