lib/hexapdf/layout/box.rb in hexapdf-0.33.0 vs lib/hexapdf/layout/box.rb in hexapdf-0.34.0

- old
+ new

@@ -125,26 +125,40 @@ # Hash with custom properties. The keys should be strings and can be arbitrary. # # This can be used to store arbitrary information on boxes for later use. For example, a # generic style layer could use one or more custom properties for its work. + # + # The Box class itself uses the following properties: + # + # optional_content:: + # + # If this property is set, it needs to be an optional content group dictionary, a String + # defining an (optionally existing) optional content group dictionary, or an optional + # content membership dictionary. + # + # The whole content of the box, i.e. including padding, border, background..., is + # wrapped with the appropriate commands so that the optional content group or membership + # dictionary specifies whether the content is shown or not. + # + # See: HexaPDF::Type::OptionalContentProperties attr_reader :properties # :call-seq: - # Box.new(width: 0, height: 0, style: nil, properties: {}) {|canv, box| block} -> box + # Box.new(width: 0, height: 0, style: nil, properties: nil) {|canv, box| block} -> box # # Creates a new Box object with the given width and height that uses the provided block when # it is asked to draw itself on a canvas (see #draw). # # Since the final location of the box is not known beforehand, the drawing operations inside # the block should draw inside the rectangle (0, 0, content_width, content_height) - note that # the width and height of the box may not be known beforehand. - def initialize(width: 0, height: 0, style: nil, properties: {}, &block) + def initialize(width: 0, height: 0, style: nil, properties: nil, &block) @width = @initial_width = width @height = @initial_height = height @style = Style.create(style) - @properties = properties + @properties = properties || {} @draw_block = block @fit_successful = false @split_box = false end @@ -218,10 +232,14 @@ # The block specified when creating the box is invoked with the canvas and the box as # arguments. Subclasses can specify an on-demand drawing method by setting the +@draw_block+ # instance variable to +nil+ or a valid block. This is useful to avoid unnecessary set-up # operations when the block does nothing. def draw(canvas, x, y) + if (oc = properties['optional_content']) + canvas.optional_content(oc) + end + if style.background_color? && style.background_color canvas.save_graphics_state do canvas.opacity(fill_alpha: style.background_alpha). fill_color(style.background_color).rectangle(x, y, width, height).fill end @@ -231,9 +249,11 @@ style.border.draw(canvas, x, y, width, height) if style.border? draw_content(canvas, x + reserved_width_left, y + reserved_height_bottom) style.overlays.draw(canvas, x, y, self) if style.overlays? + + canvas.end_optional_content if oc end # Returns +true+ if no drawing operations are performed. def empty? !(@draw_block ||