class HexaPDF::Layout::Line::HeightCalculator

Helper class for calculating the needed vertical dimensions of a line.

Public Class Methods

new(items = []) click to toggle source

Creates a new calculator with the given initial items.

# File lib/hexapdf/layout/line.rb, line 96
def initialize(items = [])
  reset
  items.each {|item| add(item) }
end

Public Instance Methods

<<(item)
Alias for: add
add(item) click to toggle source

Adds a new item to be considered when calculating the various dimensions.

# File lib/hexapdf/layout/line.rb, line 102
def add(item)
  case item.valign
  when :text
    @text_y_min = item.y_min if item.y_min < @text_y_min
    @text_y_max = item.y_max if item.y_max > @text_y_max
  when :baseline
    @max_base_height = item.height if @max_base_height < item.height
  when :top
    @max_top_height = item.height if @max_top_height < item.height
  when :text_top
    @max_text_top_height = item.height if @max_text_top_height < item.height
  when :bottom
    @max_bottom_height = item.height if @max_bottom_height < item.height
  when :text_bottom
    @max_text_bottom_height = item.height if @max_text_bottom_height < item.height
  else
    raise HexaPDF::Error, "Unknown inline box alignment #{item.valign}"
  end
  self
end
Also aliased as: <<
reset() click to toggle source

Resets the calculation.

# File lib/hexapdf/layout/line.rb, line 137
def reset
  @text_y_min = 0
  @text_y_max = 0
  @max_base_height = 0
  @max_top_height = 0
  @max_text_top_height = 0
  @max_bottom_height = 0
  @max_text_bottom_height = 0
end
result() click to toggle source

Returns the result of the calculations, the array [y_min, y_max, text_y_min, text_y_max].

See Line for their meaning.

# File lib/hexapdf/layout/line.rb, line 127
def result
  y_min = [@text_y_max - @max_text_top_height, @text_y_min].min
  y_max = [@text_y_min + @max_text_bottom_height, @max_base_height, @text_y_max].max
  y_min = [y_max - @max_top_height, y_min].min
  y_max = [y_min + @max_bottom_height, y_max].max

  [y_min, y_max, @text_y_min, @text_y_max]
end
simulate_height(item) click to toggle source

Returns the height of the line as if item was part of it but doesn't change the internal state.

# File lib/hexapdf/layout/line.rb, line 149
def simulate_height(item)
  text_y_min = @text_y_min
  text_y_max = @text_y_max
  max_base_height = @max_base_height
  max_top_height = @max_top_height
  max_text_top_height = @max_text_top_height
  max_bottom_height = @max_bottom_height
  max_text_bottom_height = @max_text_bottom_height
  y_min, y_max, = add(item).result
  [y_min, y_max, y_max - y_min]
ensure
  @text_y_min = text_y_min
  @text_y_max = text_y_max
  @max_base_height = max_base_height
  @max_top_height = max_top_height
  @max_text_top_height = max_text_top_height
  @max_bottom_height = max_bottom_height
  @max_text_bottom_height = max_text_bottom_height
end