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.



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

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

Instance Attribute Details

#boxObject

The box that was fitted into the frame.



111
112
113
# File 'lib/hexapdf/layout/box.rb', line 111

def box
  @box
end

#frameObject

The frame into which the box was fitted.



114
115
116
# File 'lib/hexapdf/layout/box.rb', line 114

def frame
  @frame
end

#maskObject

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



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

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.



133
134
135
# File 'lib/hexapdf/layout/box.rb', line 133

def status
  @status
end

#xObject

The horizontal position where the box will be drawn.



117
118
119
# File 'lib/hexapdf/layout/box.rb', line 117

def x
  @x
end

#yObject

The vertical position where the box will be drawn.



120
121
122
# File 'lib/hexapdf/layout/box.rb', line 120

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.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/hexapdf/layout/box.rb', line 181

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)


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

def failure?
  @status == :failure
end

#overflow!Object

Sets the result status to overflow.



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

def overflow!
  @status = :overflow
end

#overflow?Boolean

Returns true if only parts of the box were fitted.

Returns:

  • (Boolean)


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

def overflow?
  @status == :overflow
end

#reset(frame) ⇒ Object

Resets the result object.



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

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

#success!Object

Sets the result status to success.



150
151
152
# File 'lib/hexapdf/layout/box.rb', line 150

def success!
  @status = :success
end

#success?Boolean

Returns true if fitting was successful.

Returns:

  • (Boolean)


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

def success?
  @status == :success
end