Search in sources :

Example 1 with RenderingMode

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;
    }
}
Also used : RenderingMode(org.apache.pdfbox.pdmodel.graphics.state.RenderingMode) Shape(java.awt.Shape) GeneralPath(java.awt.geom.GeneralPath) PDGraphicsState(org.apache.pdfbox.pdmodel.graphics.state.PDGraphicsState)

Example 2 with RenderingMode

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;
            }
        }
    }
}
Also used : PDFont(org.apache.pdfbox.pdmodel.font.PDFont) RenderingMode(org.apache.pdfbox.pdmodel.graphics.state.RenderingMode) ByteArrayInputStream(java.io.ByteArrayInputStream) GlyphException(org.apache.pdfbox.preflight.font.util.GlyphException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PDTextState(org.apache.pdfbox.pdmodel.graphics.state.PDTextState) IOException(java.io.IOException)

Example 3 with RenderingMode

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);
    }
}
Also used : PDDeviceGray(org.apache.pdfbox.pdmodel.graphics.color.PDDeviceGray) SetTextRenderingMode(org.apache.pdfbox.contentstream.operator.text.SetTextRenderingMode) RenderingMode(org.apache.pdfbox.pdmodel.graphics.state.RenderingMode) COSString(org.apache.pdfbox.cos.COSString)

Example 4 with RenderingMode

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
}
Also used : RenderingMode(org.apache.pdfbox.pdmodel.graphics.state.RenderingMode) PDColor(org.apache.pdfbox.pdmodel.graphics.color.PDColor)

Example 5 with RenderingMode

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);
}
Also used : RenderingMode(org.apache.pdfbox.pdmodel.graphics.state.RenderingMode) MissingOperandException(org.apache.pdfbox.contentstream.operator.MissingOperandException) COSNumber(org.apache.pdfbox.cos.COSNumber) COSBase(org.apache.pdfbox.cos.COSBase)

Aggregations

RenderingMode (org.apache.pdfbox.pdmodel.graphics.state.RenderingMode)6 Shape (java.awt.Shape)2 PDGraphicsState (org.apache.pdfbox.pdmodel.graphics.state.PDGraphicsState)2 GeneralPath (java.awt.geom.GeneralPath)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MissingOperandException (org.apache.pdfbox.contentstream.operator.MissingOperandException)1 SetTextRenderingMode (org.apache.pdfbox.contentstream.operator.text.SetTextRenderingMode)1 COSBase (org.apache.pdfbox.cos.COSBase)1 COSNumber (org.apache.pdfbox.cos.COSNumber)1 COSString (org.apache.pdfbox.cos.COSString)1 PDFont (org.apache.pdfbox.pdmodel.font.PDFont)1 PDColor (org.apache.pdfbox.pdmodel.graphics.color.PDColor)1 PDDeviceGray (org.apache.pdfbox.pdmodel.graphics.color.PDDeviceGray)1 PDTextState (org.apache.pdfbox.pdmodel.graphics.state.PDTextState)1 GlyphException (org.apache.pdfbox.preflight.font.util.GlyphException)1