Skip to content

Commit

Permalink
PDFBOX-4627, PDFBOX-5054: ignore color operators in type3 charprocs w…
Browse files Browse the repository at this point in the history
…ith d1 or uncolored tiling patterns

git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1922853 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
THausherr committed Jan 3, 2025
1 parent 3b8fdb7 commit 8f806dd
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.apache.pdfbox.util.Matrix;
import org.apache.pdfbox.util.Vector;
import org.apache.pdfbox.contentstream.operator.Operator;
import org.apache.pdfbox.contentstream.operator.OperatorName;
import org.apache.pdfbox.contentstream.operator.OperatorProcessor;
import org.apache.pdfbox.pdmodel.graphics.blend.BlendMode;

Expand Down Expand Up @@ -91,7 +92,10 @@ public abstract class PDFStreamEngine

// default font, used if there isn't any font available
private PDFont defaultFont;


// false in certain cases, e.g. type3 charprocs with d1 or uncolored tiling patterns
private boolean shouldProcessColorOperators;

/**
* Creates a new PDFStreamEngine.
*/
Expand Down Expand Up @@ -524,18 +528,40 @@ private void processStreamOperators(PDContentStream contentStream) throws IOExce
List<COSBase> arguments = new ArrayList<>();
PDFStreamParser parser = new PDFStreamParser(contentStream);
Object token = parser.parseNextToken();
while (token != null)

boolean isFirstOperator = true;
boolean oldShouldProcessColorOperators = shouldProcessColorOperators;
shouldProcessColorOperators = true;
if (contentStream instanceof PDTilingPattern &&
((PDTilingPattern) contentStream).getPaintType() == PDTilingPattern.PAINT_UNCOLORED)
{
if (token instanceof Operator)
{
processOperator((Operator) token, arguments);
arguments.clear();
}
else
shouldProcessColorOperators = false;
}
try
{
while (token != null)
{
arguments.add((COSBase) token);
if (token instanceof Operator)
{
if (isFirstOperator && contentStream instanceof PDType3CharProc &&
OperatorName.TYPE3_D1.equals(((Operator) token).getName()))
{
shouldProcessColorOperators = false;
}
isFirstOperator = false;
processOperator((Operator) token, arguments);
arguments.clear();
}
else
{
arguments.add((COSBase) token);
}
token = parser.parseNextToken();
}
token = parser.parseNextToken();
}
finally
{
shouldProcessColorOperators = oldShouldProcessColorOperators;
}
}

Expand Down Expand Up @@ -1144,4 +1170,9 @@ public void decreaseLevel()
LOG.error("level is {}", level);
}
}

public boolean isShouldProcessColorOperators()
{
return shouldProcessColorOperators;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public void process(Operator operator, List<COSBase> arguments) throws IOExcepti
return;
}
PDFStreamEngine context = getContext();
if (!context.isShouldProcessColorOperators())
{
return;
}
PDColorSpace cs = context.getResources().getColorSpace((COSName) base);
context.getGraphicsState().setNonStrokingColorSpace(cs);
context.getGraphicsState().setNonStrokingColor(cs.getInitialColor());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public SetNonStrokingDeviceCMYKColor(PDFStreamEngine context)
public void process(Operator operator, List<COSBase> arguments) throws IOException
{
PDFStreamEngine context = getContext();
if (!context.isShouldProcessColorOperators())
{
return;
}
PDColorSpace cs = context.getResources().getColorSpace(COSName.DEVICECMYK);
context.getGraphicsState().setNonStrokingColorSpace(cs);
super.process(operator, arguments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public SetNonStrokingDeviceGrayColor(PDFStreamEngine context)
public void process(Operator operator, List<COSBase> arguments) throws IOException
{
PDFStreamEngine context = getContext();
if (!context.isShouldProcessColorOperators())
{
return;
}
PDColorSpace cs = context.getResources().getColorSpace(COSName.DEVICEGRAY);
context.getGraphicsState().setNonStrokingColorSpace(cs);
super.process(operator, arguments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public SetNonStrokingDeviceRGBColor(PDFStreamEngine context)
public void process(Operator operator, List<COSBase> arguments) throws IOException
{
PDFStreamEngine context = getContext();
if (!context.isShouldProcessColorOperators())
{
return;
}
PDColorSpace cs = context.getResources().getColorSpace(COSName.DEVICERGB);
context.getGraphicsState().setNonStrokingColorSpace(cs);
super.process(operator, arguments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public void process(Operator operator, List<COSBase> arguments) throws IOExcepti
return;
}
PDFStreamEngine context = getContext();
if (!context.isShouldProcessColorOperators())
{
return;
}
PDColorSpace cs = context.getResources().getColorSpace((COSName) base);
context.getGraphicsState().setStrokingColorSpace(cs);
context.getGraphicsState().setStrokingColor(cs.getInitialColor());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public SetStrokingDeviceCMYKColor(PDFStreamEngine context)
public void process(Operator operator, List<COSBase> arguments) throws IOException
{
PDFStreamEngine context = getContext();
if (!context.isShouldProcessColorOperators())
{
return;
}
PDColorSpace cs = context.getResources().getColorSpace(COSName.DEVICECMYK);
context.getGraphicsState().setStrokingColorSpace(cs);
super.process(operator, arguments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public SetStrokingDeviceGrayColor(PDFStreamEngine context)
public void process(Operator operator, List<COSBase> arguments) throws IOException
{
PDFStreamEngine context = getContext();
if (!context.isShouldProcessColorOperators())
{
return;
}
PDColorSpace cs = context.getResources().getColorSpace(COSName.DEVICEGRAY);
context.getGraphicsState().setStrokingColorSpace(cs);
super.process(operator, arguments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public SetStrokingDeviceRGBColor(PDFStreamEngine context)
public void process(Operator operator, List<COSBase> arguments) throws IOException
{
PDFStreamEngine context = getContext();
if (!context.isShouldProcessColorOperators())
{
return;
}
PDColorSpace cs = context.getResources().getColorSpace(COSName.DEVICERGB);
context.getGraphicsState().setStrokingColorSpace(cs);
super.process(operator, arguments);
Expand Down

0 comments on commit 8f806dd

Please sign in to comment.