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.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/hexapdf/layout/style.rb', line 84

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
  when Integer, Float
    @type = :proportional
    @value = type
  else
    raise ArgumentError, "Invalid type #{type} for line spacing"
  end
end

Instance Attribute Details

#typeObject (readonly)

The type of line spacing - see LineSpacing



77
78
79
# File 'lib/hexapdf/layout/style.rb', line 77

def type
  @type
end

#valueObject (readonly)

The value (needed for some types) - see LineSpacing



80
81
82
# File 'lib/hexapdf/layout/style.rb', line 80

def value
  @value
end

Instance Method Details

#baseline_distance(line1, line2) ⇒ Object

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



110
111
112
113
114
115
116
# File 'lib/hexapdf/layout/style.rb', line 110

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 Line objects, i.e. the distance between the y_min of the first line and the y_max of the second line.



120
121
122
123
124
125
126
# File 'lib/hexapdf/layout/style.rb', line 120

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