Skip to content

PDFGraphics2D loses color in writeSetClip #34

Open
@benediktmu

Description

@benediktmu

PDFGraphics2D has two bugs which share a root cause: 1. When two draw calls are in different clips but share the same color (identical rgba values), the second one is drawn black. 2. Related but slightly different: Colors set before the clip don't affect the clipped objects.

These effects happen because in writeSetClip, a new state is opened which is unaffected by the previously set color. This issue gets more pronounced by the fact that AbstractVectorGraphicsIO optimizes writePaint calls away if the previous current color is the same as the one for the new call - then even setting a color after setting a clip can fail.

The fix is simple: writeSetClip needs to take over the current color to the new state, like it already does for transform and stroke:

    protected void writeSetClip(Shape s) throws IOException {
        // clear old clip
        try {
            AffineTransform at = getTransform();
            Stroke stroke = getStroke();
            Color color = getColor(); // fix

            writeGraphicsRestore();
            writeGraphicsSave();

            writeStroke(stroke);
            writeTransform(at);
            writePaint(color); // fix
        } catch (IOException e) {
            handleException(e);
        }

        // write clip
        writeClip(s);
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions