Class: HexaPDF::Layout::LineFragment::HeightCalculator

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

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(items = []) ⇒ HeightCalculator

Creates a new calculator with the given initial items.



95
96
97
98
# File 'lib/hexapdf/layout/line_fragment.rb', line 95

def initialize(items = [])
  reset
  items.each {|item| add(item)}
end

Instance Method Details

#add(item) ⇒ Object Also known as: <<

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/hexapdf/layout/line_fragment.rb', line 101

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

#resetObject

Resets the calculation.



136
137
138
139
140
141
142
143
144
# File 'lib/hexapdf/layout/line_fragment.rb', line 136

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

#resultObject

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

See LineFragment for their meaning.



126
127
128
129
130
131
132
133
# File 'lib/hexapdf/layout/line_fragment.rb', line 126

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

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



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/hexapdf/layout/line_fragment.rb', line 148

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_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