Class: HexaPDF::Layout::TableBox::Cell

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

Overview

Represents a single cell of the table.

A cell is a container box that fits and draws its children with a BoxFitter. Its dimensions (width and height) are not determined by its children but by the table layout algorithm. Furthermore, its style can be used for drawing e.g. a cell border.

Cell borders work similar to the separated borders model of CSS, i.e. each cell has its own borders that do not overlap.

Constant Summary

Constants included from Utils

Utils::EPSILON

Instance Attribute Summary collapse

Attributes inherited from Box

#fit_result, #height, #properties, #style, #width

Instance Method Summary collapse

Methods inherited from Box

#content_height, #content_width, create, #draw, #fit, #split, #split_box?, #supports_position_flow?

Constructor Details

#initialize(row:, column:, children: nil, min_height: nil, row_span: nil, col_span: nil, **kwargs) ⇒ Cell

Creates a new Cell instance.



203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/hexapdf/layout/table_box.rb', line 203

def initialize(row:, column:, children: nil, min_height: nil, row_span: nil, col_span: nil, **kwargs)
  super(**kwargs, width: 0, height: 0)
  @children = children
  @row = row
  @column = column
  @row_span = row_span || 1
  @col_span = col_span || 1
  @min_height = min_height
  style.border.width.set(1) unless style.border?
  style.border.draw_on_bounds = true
  style.padding.set(5) unless style.padding?
end

Instance Attribute Details

#childrenObject

The boxes to layout inside this cell.

This may either be nil (if the cell has no content), a single Box instance or an array of Box instances.



200
201
202
# File 'lib/hexapdf/layout/table_box.rb', line 200

def children
  @children
end

#col_spanObject (readonly)

The number of columns this cell spans.



194
195
196
# File 'lib/hexapdf/layout/table_box.rb', line 194

def col_span
  @col_span
end

#columnObject (readonly)

The 0-based column number of the cell.



188
189
190
# File 'lib/hexapdf/layout/table_box.rb', line 188

def column
  @column
end

#leftObject

The x-coordinate of the cell’s top-left corner.

The coordinate is relative to the table’s content rectangle, with positive x-axis going to the right and positive y-axis going to the bottom.

This value is set by the parent Cells object during fitting and may therefore only be relied on afterwards.



167
168
169
# File 'lib/hexapdf/layout/table_box.rb', line 167

def left
  @left
end

#preferred_heightObject (readonly)

The preferred height of the cell, determined during #fit.



182
183
184
# File 'lib/hexapdf/layout/table_box.rb', line 182

def preferred_height
  @preferred_height
end

#preferred_widthObject (readonly)

The preferred width of the cell, determined during #fit.



179
180
181
# File 'lib/hexapdf/layout/table_box.rb', line 179

def preferred_width
  @preferred_width
end

#rowObject (readonly)

The 0-based row number of the cell.



185
186
187
# File 'lib/hexapdf/layout/table_box.rb', line 185

def row
  @row
end

#row_spanObject (readonly)

The number of rows this cell spans.



191
192
193
# File 'lib/hexapdf/layout/table_box.rb', line 191

def row_span
  @row_span
end

#topObject

The y-coordinate of the cell’s top-left corner.

The coordinate is relative to the table’s content rectangle, with positive x-axis going to the right and positive y-axis going to the bottom.

This value is set by the parent Cells object during fitting and may therefore only be relied on afterwards.



176
177
178
# File 'lib/hexapdf/layout/table_box.rb', line 176

def top
  @top
end

Instance Method Details

#empty?Boolean

Returns true if the cell has no content.

Returns:

  • (Boolean)


217
218
219
# File 'lib/hexapdf/layout/table_box.rb', line 217

def empty?
  super && (!@fit_results || @fit_results.empty?)
end

#inspectObject

:nodoc:



232
233
234
# File 'lib/hexapdf/layout/table_box.rb', line 232

def inspect
  "<Cell (#{row},#{column}) #{row_span}x#{col_span} #{Array(children).map(&:class)}>"
end

#update_height(height) ⇒ Object

Updates the height of the box to the given value.

The height has to be greater than or equal to the fitted height.



224
225
226
227
228
229
# File 'lib/hexapdf/layout/table_box.rb', line 224

def update_height(height)
  if height < @height
    raise HexaPDF::Error, "Given height needs to be at least as big as fitted height"
  end
  @height = height
end