Skip to content

ArrayIndexOutOfBoundsException in PdfBoxGraphics2DPaintApplier$CreateAlphaShadingMask.patchFunction #60

Open
@ganomi

Description

@ganomi

Hi,

after upgrading my dependencies of openhtmltopdf, my stack is now using Java 17 with pdfbox-graphics2d 3.0.2, pdfbox 3.0.2 and batik 1.17.

With the test code below i can provoke the following error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
at de.rototor.pdfbox.graphics2d.PdfBoxGraphics2DPaintApplier$CreateAlphaShadingMask.patchFunction(PdfBoxGraphics2DPaintApplier.java:1318)
at de.rototor.pdfbox.graphics2d.PdfBoxGraphics2DPaintApplier$CreateAlphaShadingMask.patchFunction(PdfBoxGraphics2DPaintApplier.java:1297)
at de.rototor.pdfbox.graphics2d.PdfBoxGraphics2DPaintApplier$CreateAlphaShadingMask.createMaskShading(PdfBoxGraphics2DPaintApplier.java:1278)
at de.rototor.pdfbox.graphics2d.PdfBoxGraphics2DPaintApplier$CreateAlphaShadingMask.applyMasking(PdfBoxGraphics2DPaintApplier.java:1196)
at de.rototor.pdfbox.graphics2d.PdfBoxGraphics2DPaintApplier.buildLinearGradientShading(PdfBoxGraphics2DPaintApplier.java:552)
at de.rototor.pdfbox.graphics2d.PdfBoxGraphics2DPaintApplier.applyPaint(PdfBoxGraphics2DPaintApplier.java:277)
at de.rototor.pdfbox.graphics2d.PdfBoxGraphics2DPaintApplier.applyPaint(PdfBoxGraphics2DPaintApplier.java:211)
at de.rototor.pdfbox.graphics2d.PdfBoxGraphics2D.applyPaint(PdfBoxGraphics2D.java:1065)
at de.rototor.pdfbox.graphics2d.PdfBoxGraphics2D.applyPaint(PdfBoxGraphics2D.java:1031)
at de.rototor.pdfbox.graphics2d.PdfBoxGraphics2D.fill(PdfBoxGraphics2D.java:931)
at org.apache.batik.gvt.FillShapePainter.paint(FillShapePainter.java:83)

I have tried to create an example without openhtmltopdf and i have manually minified the example svg.

import de.rototor.pdfbox.graphics2d.PdfBoxGraphics2D;
import java.io.IOException;
import java.io.StringReader;
import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.bridge.DocumentLoader;
import org.apache.batik.bridge.GVTBuilder;
import org.apache.batik.bridge.UserAgent;
import org.apache.batik.bridge.UserAgentAdapter;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.util.XMLResourceDescriptor;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.w3c.dom.Document;

public class TestClass {

    public static void main (String[] args) throws IOException {
        // create the document
        String parser = XMLResourceDescriptor.getXMLParserClassName();
        SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
        Document document = f.createDocument("http://test", new StringReader("""
            <svg width="64" height="64" viewBox="0 0 64 64" xml:space="preserve" xmlns="http://www.w3.org/2000/svg">
              <switch>
                <foreignObject requiredExtensions="http://ns.adobe.com/AdobeIllustrator/10.0/" x="0" y="0" width="1" height="1"/>
                <g>
                  <linearGradient id="a" gradientUnits="userSpaceOnUse" x1="47.7" y1="2.3" x2="47.7" y2="63.3">
                    <stop offset="0" stop-color="#DEE0DD"/>
                    <stop offset=".2" stop-color="#333F47"/>
                    <stop offset=".5" stop-color="#27343C"/>
                    <stop offset="1" stop-color="#434D54"/>
                  </linearGradient>
                  <path fill="url(#a)" d="M42 2.3c10.5 4 20.4 15 20.4 28.9C62.4 48 49 61.7 32 61.7v1.6c17 0 31.3-14 31.3-31.3 0-13.8-8.8-25.4-21.3-29.7"/>
                  <linearGradient id="b" gradientUnits="userSpaceOnUse" x1="57.3" y1="8.1" x2="43.8" y2="27.9">
                    <stop offset=".7" stop-color="#27343C"/>
                    <stop offset=".7" style="stop-color:#2b373f;stop-opacity:1"/>
                    <stop offset=".8" style="stop-color:#646d73;stop-opacity:0"/>
                  </linearGradient>
                  <path fill="url(#b)" d="M58.8 20.2C51.8 4.1 36 3.2 35.1 3.1H35c12.1 2.2 19.8 10.1 22.5 18.4v.1c1.2 3.2 1.8 6.6 1.9 10.3.1 3.5-.7 7.4-2.2 11-.1.5-.2 1.1-.3 1.1h1.6c4.8-9 2.7-18.1.3-23.8"/>
                </g>
              </switch>
            </svg>
            """));

        // create the GVT
        UserAgent userAgent = new UserAgentAdapter();
        DocumentLoader loader = new DocumentLoader(userAgent);
        BridgeContext bctx = new BridgeContext(userAgent, loader);
        bctx.setDynamicState(BridgeContext.STATIC);
        GVTBuilder builder = new GVTBuilder();
        final GraphicsNode gvtRoot = builder.build(bctx, document);

        PDDocument newDocument = new PDDocument();
        PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(newDocument, 400, 400);
        gvtRoot.paint(pdfBoxGraphics2D);
    }
}

For a working example: The code will run without error if you remove the following line from the svg:
<stop offset=".8" style="stop-color:#646d73;stop-opacity:0"/>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions