Class: HexaPDF::Layout::Box::FitResult

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

Overview

Stores the result of fitting a box in a frame.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(box, frame: nil) ⇒ FitResult

Initializes the result object for the given box and, optionally, frame.



147
148
149
150
# File 'lib/hexapdf/layout/box.rb', line 147

def initialize(box, frame: nil)
  @box = box
  reset(frame)
end

Instance Attribute Details

#boxObject

The box that was fitted into the frame.



122
123
124
# File 'lib/hexapdf/layout/box.rb', line 122

def box
  @box
end

#frameObject

The frame into which the box was fitted.



125
126
127
# File 'lib/hexapdf/layout/box.rb', line 125

def frame
  @frame
end

#maskObject

The rectangle (a Geom2D::Rectangle object) that will be removed from the frame when drawing the box.



135
136
137
# File 'lib/hexapdf/layout/box.rb', line 135

def mask
  @mask
end

#statusObject (readonly)

The status result of fitting the box in the frame.

Allowed values are:

:failure

(default) Indicates fitting the box has failed.

:success

Indicates that the box was completely fitted.

:overflow

Indicates that only a part of the box was fitted.



144
145
146
# File 'lib/hexapdf/layout/box.rb', line 144

def status
  @status
end

#xObject

The horizontal position where the box will be drawn.



128
129
130
# File 'lib/hexapdf/layout/box.rb', line 128

def x
  @x
end

#yObject

The vertical position where the box will be drawn.



131
132
133
# File 'lib/hexapdf/layout/box.rb', line 131

def y
  @y
end

Instance Method Details

#draw(canvas, dx: 0, dy: 0) ⇒ Object

Draws the #box onto the canvas at (#x + dx, #y + dy).

The relative offset (dx, dy) is useful when rendering results that were accumulated and then need to be moved because the container holding them changes its position.

The configuration option “debug” can be used to add visual debug output with respect to box placement.



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/hexapdf/layout/box.rb', line 192

def draw(canvas, dx: 0, dy: 0)
  return if box.height == 0 || box.width == 0
  doc = canvas.context.document
  if doc.config['debug']
    name = (frame.parent_boxes + [box]).map do |box|
      box.class.to_s.sub(/.*::/, '')
    end.join('-') << "##{box.object_id}"
    name = "#{name} (#{(x + dx).to_i},#{(y + dy).to_i}-#{mask.width.to_i}x#{mask.height.to_i})"
    ocg = doc.optional_content.ocg(name)
    canvas.optional_content(ocg) do
      canvas.translate(dx, dy) do
        canvas.fill_color("green").stroke_color("darkgreen").
          opacity(fill_alpha: 0.1, stroke_alpha: 0.2).
          draw(:geom2d, object: mask, path_only: true).fill_stroke
      end
    end
    page = "Page #{canvas.context.index + 1}" rescue "XObject"
    doc.optional_content.default_configuration.add_ocg_to_ui(ocg, path: ['Debug', page])
  end
  box.draw(canvas, x + dx, y + dy)
end

#failure?Boolean

Returns true if fitting was a failure.

Returns:

  • (Boolean)


181
182
183
# File 'lib/hexapdf/layout/box.rb', line 181

def failure?
  @status == :failure
end

#overflow!Object

Sets the result status to overflow.



171
172
173
# File 'lib/hexapdf/layout/box.rb', line 171

def overflow!
  @status = :overflow
end

#overflow?Boolean

Returns true if only parts of the box were fitted.

Returns:

  • (Boolean)


176
177
178
# File 'lib/hexapdf/layout/box.rb', line 176

def overflow?
  @status == :overflow
end

#reset(frame) ⇒ Object

Resets the result object.



153
154
155
156
157
158
# File 'lib/hexapdf/layout/box.rb', line 153

def reset(frame)
  @frame = frame
  @x = @y = @mask = nil
  @status = :failure
  self
end

#success!Object

Sets the result status to success.



161
162
163
# File 'lib/hexapdf/layout/box.rb', line 161

def success!
  @status = :success
end

#success?Boolean

Returns true if fitting was successful.

Returns:

  • (Boolean)


166
167
168
# File 'lib/hexapdf/layout/box.rb', line 166

def success?
  @status == :success
end