Uploaded image for project: 'Batik'
  1. Batik
  2. BATIK-1290

[PATCH] AbstractGraphicsNode.paint() sometimes does not dispose a Graphics2D child context

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 1.13, 1.12
    • None
    • GVT
    • None

    Description

      In some (rare?) cases, org.apache.batik.gvt.AbstractGraphicsNode::paint() calls g2d.create() twice, but only calls dispose() on one of the child Graphics2D.

      Because a clip is set, in line 456 the first child Graphics2D is created. Later in line 503 useOffscreen is true. This causes in line 535 the baseG2d (which was stored to be later be dispose()ed) is overridden the the child created in line 456. Then in line 539 a new child Graphics2D is created. And only this second child is disposed in line 552, the first child is never disposed.

      This is a problem if the used Graphics2D needs the dispose() calls to be "balanced" and correct. Which is the case when using it with my PdfBoxGraphics2D-Adapter (https://github.com/rototor/pdfbox-graphics2d). The child Graphics2D write a "q" (Save State) into the PDF stream and therefore need a matching "Q" (Restore State) - which is written when calling dispose(). Otherwise the PDF would be invalid.

      The attached patch fixes this by only creating a child Graphics2D if not already one exists.

      I also attached the SVG file that triggers the problem. It has transparency, so it is rendered to an offscreen image first but also has a clip set.

      For a "ready to use" example to reproduce this bug you can clone https://github.com/rototor/pdfbox-graphics2d, enable the testcase for this (https://github.com/rototor/pdfbox-graphics2d/blob/master/src/test/java/de/rototor/pdfbox/graphics2d/RenderSVGsTest.java#L40) and run mvn test. It will then fail because of this bug.

      This issue was originally reported in the openhtmltopdf project (https://github.com/danfickle/openhtmltopdf/issues/536)

      Attachments

        1. batik_gvt_fix_danlging_gfx.patch
          1 kB
          Emmeran Seehuber
        2. openhtml_536.svg
          0.6 kB
          Emmeran Seehuber

        Activity

          People

            Unassigned Unassigned
            rototor Emmeran Seehuber
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated: