class HexaPDF::Layout::Style::LineSpacing

Defines how the distance between the baselines of two adjacent text lines is determined:

:single

:proportional with value 1.

:double

:proportional with value 2.

:proportional

The y_min of the first line and the y_max of the second line are multiplied with the specified value, and the sum is used as baseline distance.

:fixed

The distance between the baselines is set to the specified value.

:leading

The distance between the baselines is set to the sum of the y_min of the first line, the y_max of the second line and the specified value.

Attributes

type[R]

The type of line spacing - see LineSpacing

value[R]

The value (needed for some types) - see LineSpacing

Public Class Methods

new(type:, value: 1) click to toggle source

Creates a new LineSpacing object for the given type which can be any valid line spacing type or a LineSpacing object.

# File lib/hexapdf/layout/style.rb, line 83
def initialize(type:, value: 1)
  case type
  when :single
    @type = :proportional
    @value = 1
  when :double
    @type = :proportional
    @value = 2
  when :fixed, :proportional, :leading
    unless value.kind_of?(Numeric)
      raise ArgumentError, "Need a valid number for #{type} line spacing"
    end
    @type = type
    @value = value
  when LineSpacing
    @type = type.type
    @value = type.value
  else
    raise ArgumentError, "Invalid type #{type} for line spacing"
  end
end

Public Instance Methods

baseline_distance(line1, line2) click to toggle source

Returns the distance between the baselines of the two given Line objects.

# File lib/hexapdf/layout/style.rb, line 106
def baseline_distance(line1, line2)
  case type
  when :proportional then (line1.y_min.abs + line2.y_max) * value
  when :fixed then value
  when :leading then line1.y_min.abs + line2.y_max + value
  end
end
gap(line1, line2) click to toggle source

Returns the gap between the two given Line objects, i.e. the distance between the y_min of the first line and the y_max of the second line.

# File lib/hexapdf/layout/style.rb, line 116
def gap(line1, line2)
  case type
  when :proportional then (line1.y_min.abs + line2.y_max) * (value - 1)
  when :fixed then value - line1.y_min.abs - line2.y_max
  when :leading then value
  end
end