use of org.apache.pdfbox.pdmodel.graphics.state.RenderingMode in project pdfbox by apache.
the class PageDrawer method endTextClip.
/**
* End buffering the text clipping path, if any.
*/
private void endTextClip() {
PDGraphicsState state = getGraphicsState();
RenderingMode renderingMode = state.getTextState().getRenderingMode();
// apply the buffered clip as one area
if (renderingMode.isClip() && !textClippings.isEmpty()) {
// PDFBOX-4150: this is much faster than using textClippingArea.add(new Area(glyph))
// https://stackoverflow.com/questions/21519007/fast-union-of-shapes-in-java
GeneralPath path = new GeneralPath();
for (Shape shape : textClippings) {
path.append(shape, false);
}
state.intersectClippingPath(path);
textClippings = new ArrayList<>();
// PDFBOX-3681: lastClip needs to be reset, because after intersection it is still the same
// object, thus setClip() would believe that it is cached.
lastClip = null;
}
}
use of org.apache.pdfbox.pdmodel.graphics.state.RenderingMode in project pdfbox by apache.
the class PreflightContentStream method validateText.
/**
* Process the validation of a Text operand contains in a ContentStream This validation checks that :
* <UL>
* <li>The font isn't missing if the Rendering Mode isn't 3
* <li>The font metrics are consistent
* <li>All character used in the text are defined in the font program.
* </UL>
*
* @param string
* @throws IOException
*/
public void validateText(byte[] string) throws IOException {
// TextSize accessible through the TextState
PDTextState textState = getGraphicsState().getTextState();
final RenderingMode renderingMode = textState.getRenderingMode();
final PDFont font = textState.getFont();
if (font == null) {
// Unable to decode the Text without Font
registerError("Text operator can't be processed without a Font", ERROR_FONTS_UNKNOWN_FONT_REF);
return;
}
FontContainer<?> fontContainer = context.getFontContainer(font.getCOSObject());
if (renderingMode == RenderingMode.NEITHER && (fontContainer == null || !fontContainer.isEmbeddedFont())) {
// font not embedded and rendering mode is 3. Valid case and nothing to check
return;
} else if (fontContainer == null) {
// Font Must be embedded if the RenderingMode isn't 3
if (font.getName() == null) {
registerError("invalid font dictionary", ERROR_FONTS_UNKNOWN_FONT_REF);
} else {
registerError("font '" + font.getName() + "' is missing", ERROR_FONTS_UNKNOWN_FONT_REF);
}
return;
} else if (!fontContainer.isValid() && !fontContainer.errorsAleadyMerged()) {
context.addValidationErrors(fontContainer.getAllErrors());
fontContainer.setErrorsAlreadyMerged(true);
return;
}
if (!fontContainer.isValid() && fontContainer.errorsAleadyMerged()) {
// font already computed
return;
}
InputStream in = new ByteArrayInputStream(string);
while (in.available() > 0) {
try {
int code = font.readCode(in);
fontContainer.checkGlyphWidth(code);
} catch (IOException e) {
registerError("Encoding can't interpret the character code", ERROR_FONTS_ENCODING_ERROR, e);
return;
} catch (GlyphException e) {
if (renderingMode != RenderingMode.NEITHER) {
registerError(e.getMessage(), e.getErrorCode(), e);
return;
}
}
}
}
use of org.apache.pdfbox.pdmodel.graphics.state.RenderingMode in project pdfbox by apache.
the class PreflightStreamEngine method validateDefaultColorSpace.
/**
* In some cases, the colorspace isn't checked because defaults (/DeviceGray) is used. Thus we
* need to check all text output, stroke and fill for /DeviceGray.
*
* @param operator an operator.
* @throws ContentStreamException
*/
void validateDefaultColorSpace(Operator operator) throws ContentStreamException {
boolean v = false;
String op = operator.getName();
if ("Tj".equals(op) || "TJ".equals(op) || "'".equals(op) || "\"".equals(op)) {
RenderingMode rm = getGraphicsState().getTextState().getRenderingMode();
if (rm.isFill() && getGraphicsState().getNonStrokingColor().getColorSpace() instanceof PDDeviceGray) {
v = true;
}
if (rm.isStroke() && getGraphicsState().getStrokingColor().getColorSpace() instanceof PDDeviceGray) {
v = true;
}
}
// fills
if (("f".equals(op) || "F".equals(op) || "f*".equals(op) || "B".equals(op) || "B*".equals(op) || "b".equals(op) || "b*".equals(op)) && getGraphicsState().getNonStrokingColor().getColorSpace() instanceof PDDeviceGray) {
v = true;
}
// strokes
if (("B".equals(op) || "B*".equals(op) || "b".equals(op) || "b*".equals(op) || "s".equals(op) || "S".equals(op)) && getGraphicsState().getStrokingColor().getColorSpace() instanceof PDDeviceGray) {
v = true;
}
if (v && !validColorSpaceDestOutputProfile(PreflightStreamEngine.ColorSpaceType.ALL)) {
registerError("/DeviceGray default for operator \"" + op + "\" can't be used without Color Profile", ERROR_GRAPHIC_INVALID_COLOR_SPACE_MISSING);
}
}
use of org.apache.pdfbox.pdmodel.graphics.state.RenderingMode in project pdfbox by apache.
the class PrintTextColors method processTextPosition.
@Override
protected void processTextPosition(TextPosition text) {
super.processTextPosition(text);
PDColor strokingColor = getGraphicsState().getStrokingColor();
PDColor nonStrokingColor = getGraphicsState().getNonStrokingColor();
String unicode = text.getUnicode();
RenderingMode renderingMode = getGraphicsState().getTextState().getRenderingMode();
System.out.println("Unicode: " + unicode);
System.out.println("Rendering mode: " + renderingMode);
System.out.println("Stroking color: " + strokingColor);
System.out.println("Non-Stroking color: " + nonStrokingColor);
System.out.println("Non-Stroking color: " + nonStrokingColor);
System.out.println();
// See the PrintTextLocations for more attributes
}
use of org.apache.pdfbox.pdmodel.graphics.state.RenderingMode in project pdfbox by apache.
the class SetTextRenderingMode method process.
@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException {
if (arguments.size() < 1) {
throw new MissingOperandException(operator, arguments);
}
COSBase base0 = arguments.get(0);
if (!(base0 instanceof COSNumber)) {
return;
}
COSNumber mode = (COSNumber) base0;
int val = mode.intValue();
if (val < 0 || val >= RenderingMode.values().length) {
return;
}
RenderingMode renderingMode = RenderingMode.fromInt(val);
context.getGraphicsState().getTextState().setRenderingMode(renderingMode);
}
Aggregations