Class: HexaPDF::Layout::Line::HeightCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/hexapdf/layout/line.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.



97
98
99
100
# File 'lib/hexapdf/layout/line.rb', line 97

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.



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

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.



138
139
140
141
142
143
144
145
146
# File 'lib/hexapdf/layout/line.rb', line 138

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 Line for their meaning.



128
129
130
131
132
133
134
135
# File 'lib/hexapdf/layout/line.rb', line 128

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.



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

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