lib/hexapdf/layout/box.rb in hexapdf-0.45.0 vs lib/hexapdf/layout/box.rb in hexapdf-0.46.0

- old
+ new

@@ -67,10 +67,14 @@ # # #supports_position_flow?:: # If the subclass supports the value :flow of the 'position' style property, this method # needs to be overridden to return +true+. # + # Additionally, if a box object uses flow positioning, #fit_result.x should be set to the + # correct value since Frame#fit can't determine this and uses Frame#left in the absence of a + # set value. + # # #empty?:: # This method should return +true+ if the subclass won't draw anything when #draw is called. # # #fit_content:: # This method determines whether the box fits into the available region and should set the @@ -87,32 +91,17 @@ # # #draw_content:: # This method draws the box specific content and is called from #draw which already handles # things like drawing the border and background. So #draw should usually not be overridden. # - # This base class provides various private helper methods for use in the above methods: + # This base class also provides various protected helper methods for use in the above methods: # - # +reserved_width+, +reserved_height+:: - # Returns the width respectively the height of the reserved space inside the box that is - # used for the border and padding. - # - # +reserved_width_left+, +reserved_width_right+, +reserved_height_top+, - # +reserved_height_bottom+:: - # Returns the reserved space inside the box at the specified edge (left, right, top, - # bottom). - # - # +update_content_width+, +update_content_height+:: - # Takes a block that should return the content width respectively height and sets the box's - # width respectively height accordingly. - # - # +create_split_box+:: - # Creates a new box based on this one and resets the internal data back to their original - # values. - # - # The keyword argument +split_box_value+ (defaults to +true+) is used to set the - # +@split_box+ variable to make the new box aware that it is a split box. This can be set to - # any other truthy value to convey more meaning. + # * #reserved_width, #reserved_height + # * #reserved_width_left, #reserved_width_right, #reserved_height_top, + # #reserved_height_bottom + # * #update_content_width, #update_content_height + # * #create_split_box class Box include HexaPDF::Utils # Stores the result of fitting a box in a frame. @@ -350,11 +339,13 @@ frame.y - frame.bottom else available_height end return @fit_result if !position_flow && (float_compare(@width, available_width) > 0 || - float_compare(@height, available_height) > 0) + float_compare(@height, available_height) > 0 || + @width - reserved_width < 0 || + @height - reserved_height < 0) fit_content(available_width, available_height, frame) @fit_x = frame.x + reserved_width_left @fit_y = frame.y - @height + reserved_height_bottom @@ -430,11 +421,11 @@ (style.underlays? && !style.underlays.none?) || (style.border? && !style.border.none?) || (style.overlays? && !style.overlays.none?)) end - private + protected # Returns the width that is reserved by the padding and border style properties. def reserved_width reserved_width_left + reserved_width_right end @@ -478,31 +469,36 @@ result += style.padding.bottom if style.padding? result += style.border.width.bottom if style.border? result end + # :call-seq: + # update_content_width { block } + # # Updates the width of the box using the content width returned by the block. def update_content_width return if @initial_width > 0 @width = yield + reserved_width end + # :call-seq: + # update_content_height { block } + # # Updates the height of the box using the content height returned by the block. def update_content_height return if @initial_height > 0 @height = yield + reserved_height end # Fits the content of the box and returns whether fitting was successful. # - # This is just a stub implementation that sets the #fit_result status to success if the - # content rectangle is not degenerate. Subclasses should override it to provide the box - # specific behaviour. + # This is just a stub implementation that sets the #fit_result status to success. Subclasses + # should override it to provide the box specific behaviour. # # See #fit for details. def fit_content(_available_width, _available_height, _frame) - fit_result.success! if content_width > 0 && content_height > 0 + fit_result.success! end # Splits the content of the box. # # This is just a stub implementation, returning [nil, self] since we can't know how to split @@ -523,10 +519,11 @@ if @draw_block canvas.translate(x, y) { @draw_block.call(canvas, self) } end end - # Creates a new box based on this one and resets the data back to their original values. + # Creates a new box based on this one and resets the internal data back to their original + # values. # # The variable +@split_box+ is set to +split_box_value+ (defaults to +true+) to make the new # box aware that it is a split box. If needed, subclasses can set the variable to other truthy # values to convey more meaning. #