Class: HexaPDF::Layout::Style::LineSpacing

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

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, value: 1) ⇒ LineSpacing

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/hexapdf/layout/style.rb', line 73

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

Instance Attribute Details

#typeObject (readonly)

The type of line spacing - see LineSpacing



66
67
68
# File 'lib/hexapdf/layout/style.rb', line 66

def type
  @type
end

#valueObject (readonly)

The value (needed for some types) - see LineSpacing



69
70
71
# File 'lib/hexapdf/layout/style.rb', line 69

def value
  @value
end

Instance Method Details

#baseline_distance(line1, line2) ⇒ Object

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



96
97
98
99
100
101
102
# File 'lib/hexapdf/layout/style.rb', line 96

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) ⇒ Object

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



106
107
108
109
110
111
112
# File 'lib/hexapdf/layout/style.rb', line 106

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