Search in sources :

Example 16 with PDColor

use of com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor in project PdfBox-Android by TomRoush.

the class PDFreeTextAppearanceHandler method generateNormalAppearance.

@Override
public void generateNormalAppearance() {
    PDAnnotationMarkup annotation = (PDAnnotationMarkup) getAnnotation();
    float[] pathsArray = new float[0];
    if (PDAnnotationMarkup.IT_FREE_TEXT_CALLOUT.equals(annotation.getIntent())) {
        pathsArray = annotation.getCallout();
        if (pathsArray == null || pathsArray.length != 4 && pathsArray.length != 6) {
            pathsArray = new float[0];
        }
    }
    AnnotationBorder ab = AnnotationBorder.getAnnotationBorder(annotation, annotation.getBorderStyle());
    PDAppearanceContentStream cs = null;
    try {
        cs = getNormalAppearanceAsContentStream(true);
        // The fill color is the /C entry, there is no /IC entry defined
        boolean hasBackground = cs.setNonStrokingColorOnDemand(annotation.getColor());
        setOpacity(cs, annotation.getConstantOpacity());
        // Adobe uses the last non stroking color from /DA as stroking color!
        // But if there is a color in /DS, then that one is used for text.
        PDColor strokingColor = extractNonStrokingColor(annotation);
        boolean hasStroke = cs.setStrokingColorOnDemand(strokingColor);
        PDColor textColor = strokingColor;
        String defaultStyleString = annotation.getDefaultStyleString();
        if (defaultStyleString != null) {
            Matcher m = COLOR_PATTERN.matcher(defaultStyleString);
            if (m.find()) {
                int color = Integer.parseInt(m.group(1), 16);
                float r = ((color >> 16) & 0xFF) / 255f;
                float g = ((color >> 8) & 0xFF) / 255f;
                float b = (color & 0xFF) / 255f;
                textColor = new PDColor(new float[] { r, g, b }, PDDeviceRGB.INSTANCE);
            }
        }
        if (ab.dashArray != null) {
            cs.setLineDashPattern(ab.dashArray, 0);
        }
        cs.setLineWidth(ab.width);
        // see CTAN-example-Annotations.pdf
        for (int i = 0; i < pathsArray.length / 2; ++i) {
            float x = pathsArray[i * 2];
            float y = pathsArray[i * 2 + 1];
            if (i == 0) {
                if (SHORT_STYLES.contains(annotation.getLineEndingStyle())) {
                    // modify coordinate to shorten the segment
                    // https://stackoverflow.com/questions/7740507/extend-a-line-segment-a-specific-distance
                    float x1 = pathsArray[2];
                    float y1 = pathsArray[3];
                    float len = (float) (Math.sqrt(Math.pow(x - x1, 2) + Math.pow(y - y1, 2)));
                    if (Float.compare(len, 0) != 0) {
                        x += (x1 - x) / len * ab.width;
                        y += (y1 - y) / len * ab.width;
                    }
                }
                cs.moveTo(x, y);
            } else {
                cs.lineTo(x, y);
            }
        }
        if (pathsArray.length > 0) {
            cs.stroke();
        }
        // paint the styles here and after line(s) draw, to avoid line crossing a filled shape
        if (PDAnnotationMarkup.IT_FREE_TEXT_CALLOUT.equals(annotation.getIntent()) && // check only needed to avoid q cm Q if LE_NONE
        !LE_NONE.equals(annotation.getLineEndingStyle()) && pathsArray.length >= 4) {
            float x2 = pathsArray[2];
            float y2 = pathsArray[3];
            float x1 = pathsArray[0];
            float y1 = pathsArray[1];
            cs.saveGraphicsState();
            if (ANGLED_STYLES.contains(annotation.getLineEndingStyle())) {
                // do a transform so that first "arm" is imagined flat,
                // like in line handler.
                // The alternative would be to apply the transform to the
                // LE shape coordinates directly, which would be more work
                // and produce code difficult to understand
                double angle = Math.atan2(y2 - y1, x2 - x1);
                cs.transform(Matrix.getRotateInstance(angle, x1, y1));
            } else {
                cs.transform(Matrix.getTranslateInstance(x1, y1));
            }
            drawStyle(annotation.getLineEndingStyle(), cs, 0, 0, ab.width, hasStroke, hasBackground, false);
            cs.restoreGraphicsState();
        }
        PDRectangle borderBox;
        PDBorderEffectDictionary borderEffect = annotation.getBorderEffect();
        if (borderEffect != null && borderEffect.getStyle().equals(PDBorderEffectDictionary.STYLE_CLOUDY)) {
            // Adobe draws the text with the original rectangle in mind.
            // but if there is an /RD, then writing area get smaller.
            // do this here because /RD is overwritten in a few lines
            borderBox = applyRectDifferences(getRectangle(), annotation.getRectDifferences());
            // TODO this segment was copied from square handler. Refactor?
            CloudyBorder cloudyBorder = new CloudyBorder(cs, borderEffect.getIntensity(), ab.width, getRectangle());
            cloudyBorder.createCloudyRectangle(annotation.getRectDifference());
            annotation.setRectangle(cloudyBorder.getRectangle());
            annotation.setRectDifference(cloudyBorder.getRectDifference());
            PDAppearanceStream appearanceStream = annotation.getNormalAppearanceStream();
            appearanceStream.setBBox(cloudyBorder.getBBox());
            appearanceStream.setMatrix(cloudyBorder.getMatrix());
        } else {
            // handle the border box
            // 
            // There are two options. The handling is not part of the PDF specification but
            // implementation specific to Adobe Reader
            // - if /RD is set the border box is the /Rect entry inset by the respective
            // border difference.
            // - if /RD is not set then we don't touch /RD etc because Adobe doesn't either.
            borderBox = applyRectDifferences(getRectangle(), annotation.getRectDifferences());
            annotation.getNormalAppearanceStream().setBBox(borderBox);
            // note that borderBox is not modified
            PDRectangle paddedRectangle = getPaddedRectangle(borderBox, ab.width / 2);
            cs.addRect(paddedRectangle.getLowerLeftX(), paddedRectangle.getLowerLeftY(), paddedRectangle.getWidth(), paddedRectangle.getHeight());
        }
        cs.drawShape(ab.width, hasStroke, hasBackground);
        // rotation is an undocumented feature, but Adobe uses it. Examples can be found
        // in pdf_commenting_new.pdf file, page 3.
        int rotation = annotation.getCOSObject().getInt(COSName.ROTATE, 0);
        cs.transform(Matrix.getRotateInstance(Math.toRadians(rotation), 0, 0));
        float xOffset;
        float yOffset;
        float width = rotation == 90 || rotation == 270 ? borderBox.getHeight() : borderBox.getWidth();
        // strategy to write formatted text is somewhat inspired by
        // AppearanceGeneratorHelper.insertGeneratedAppearance()
        PDFont font = PDType1Font.HELVETICA;
        float clipY;
        float clipWidth = width - ab.width * 4;
        float clipHeight = rotation == 90 || rotation == 270 ? borderBox.getWidth() - ab.width * 4 : borderBox.getHeight() - ab.width * 4;
        extractFontDetails(annotation);
        if (document != null && document.getDocumentCatalog().getAcroForm() != null) {
            // Try to get font from AcroForm default resources
            // Sample file: https://gitlab.freedesktop.org/poppler/poppler/issues/6
            PDResources defaultResources = document.getDocumentCatalog().getAcroForm().getDefaultResources();
            if (defaultResources != null) {
                PDFont defaultResourcesFont = defaultResources.getFont(fontName);
                if (defaultResourcesFont != null) {
                    font = defaultResourcesFont;
                }
            }
        }
        // value used by Adobe, no idea where it comes from, actual font bbox max y is 0.931
        // gathered by creating an annotation with width 0.
        float yDelta = 0.7896f;
        switch(rotation) {
            case 180:
                xOffset = -borderBox.getUpperRightX() + ab.width * 2;
                yOffset = -borderBox.getLowerLeftY() - ab.width * 2 - yDelta * fontSize;
                clipY = -borderBox.getUpperRightY() + ab.width * 2;
                break;
            case 90:
                xOffset = borderBox.getLowerLeftY() + ab.width * 2;
                yOffset = -borderBox.getLowerLeftX() - ab.width * 2 - yDelta * fontSize;
                clipY = -borderBox.getUpperRightX() + ab.width * 2;
                break;
            case 270:
                xOffset = -borderBox.getUpperRightY() + ab.width * 2;
                yOffset = borderBox.getUpperRightX() - ab.width * 2 - yDelta * fontSize;
                clipY = borderBox.getLowerLeftX() + ab.width * 2;
                break;
            case 0:
            default:
                xOffset = borderBox.getLowerLeftX() + ab.width * 2;
                yOffset = borderBox.getUpperRightY() - ab.width * 2 - yDelta * fontSize;
                clipY = borderBox.getLowerLeftY() + ab.width * 2;
                break;
        }
        // clip writing area
        cs.addRect(xOffset, clipY, clipWidth, clipHeight);
        cs.clip();
        cs.beginText();
        cs.setFont(font, fontSize);
        cs.setNonStrokingColor(textColor.getComponents());
        AppearanceStyle appearanceStyle = new AppearanceStyle();
        appearanceStyle.setFont(font);
        appearanceStyle.setFontSize(fontSize);
        PlainTextFormatter formatter = new PlainTextFormatter.Builder(cs).style(appearanceStyle).text(new PlainText(annotation.getContents())).width(width - ab.width * 4).wrapLines(true).initialOffset(xOffset, yOffset).build();
        try {
            formatter.format();
        } catch (IllegalArgumentException ex) {
            throw new IOException(ex);
        }
        cs.endText();
        if (pathsArray.length > 0) {
            PDRectangle rect = getRectangle();
            // Adjust rectangle
            // important to do this after the rectangle has been painted, because the
            // final rectangle will be bigger due to callout
            // CTAN-example-Annotations.pdf p1
            // TODO in a class structure this should be overridable
            float minX = Float.MAX_VALUE;
            float minY = Float.MAX_VALUE;
            float maxX = Float.MIN_VALUE;
            float maxY = Float.MIN_VALUE;
            for (int i = 0; i < pathsArray.length / 2; ++i) {
                float x = pathsArray[i * 2];
                float y = pathsArray[i * 2 + 1];
                minX = Math.min(minX, x);
                minY = Math.min(minY, y);
                maxX = Math.max(maxX, x);
                maxY = Math.max(maxY, y);
            }
            // arrow length is 9 * width at about 30° => 10 * width seems to be enough
            rect.setLowerLeftX(Math.min(minX - ab.width * 10, rect.getLowerLeftX()));
            rect.setLowerLeftY(Math.min(minY - ab.width * 10, rect.getLowerLeftY()));
            rect.setUpperRightX(Math.max(maxX + ab.width * 10, rect.getUpperRightX()));
            rect.setUpperRightY(Math.max(maxY + ab.width * 10, rect.getUpperRightY()));
            annotation.setRectangle(rect);
            // need to set the BBox too, because rectangle modification came later
            annotation.getNormalAppearanceStream().setBBox(getRectangle());
        // TODO when callout is used, /RD should be so that the result is the writable part
        }
    } catch (IOException ex) {
        Log.e("PdfBox-Android", ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(cs);
    }
}
Also used : PDFont(com.tom_roush.pdfbox.pdmodel.font.PDFont) Matcher(java.util.regex.Matcher) PDAppearanceStream(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream) PDBorderEffectDictionary(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDBorderEffectDictionary) PDAnnotationMarkup(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) IOException(java.io.IOException) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor) AppearanceStyle(com.tom_roush.pdfbox.pdmodel.interactive.annotation.layout.AppearanceStyle) PDAppearanceContentStream(com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream) PlainText(com.tom_roush.pdfbox.pdmodel.interactive.annotation.layout.PlainText) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) PlainTextFormatter(com.tom_roush.pdfbox.pdmodel.interactive.annotation.layout.PlainTextFormatter)

Example 17 with PDColor

use of com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor in project PdfBox-Android by TomRoush.

the class PDHighlightAppearanceHandler method generateNormalAppearance.

@Override
public void generateNormalAppearance() {
    PDAnnotationTextMarkup annotation = (PDAnnotationTextMarkup) getAnnotation();
    PDRectangle rect = annotation.getRectangle();
    float[] pathsArray = annotation.getQuadPoints();
    if (pathsArray == null) {
        return;
    }
    AnnotationBorder ab = AnnotationBorder.getAnnotationBorder(annotation, annotation.getBorderStyle());
    PDColor color = annotation.getColor();
    if (color == null || color.getComponents().length == 0) {
        return;
    }
    // Adjust rectangle even if not empty, see PLPDF.com-MarkupAnnotations.pdf
    // TODO in a class structure this should be overridable
    // this is similar to polyline but different data type
    // TODO padding should consider the curves too; needs to know in advance where the curve is
    float minX = Float.MAX_VALUE;
    float minY = Float.MAX_VALUE;
    float maxX = Float.MIN_VALUE;
    float maxY = Float.MIN_VALUE;
    for (int i = 0; i < pathsArray.length / 2; ++i) {
        float x = pathsArray[i * 2];
        float y = pathsArray[i * 2 + 1];
        minX = Math.min(minX, x);
        minY = Math.min(minY, y);
        maxX = Math.max(maxX, x);
        maxY = Math.max(maxY, y);
    }
    // get the delta used for curves and use it for padding
    float maxDelta = 0;
    for (int i = 0; i < pathsArray.length / 8; ++i) {
        // one of the two is 0, depending whether the rectangle is
        // horizontal or vertical
        // if it is diagonal then... uh...
        float delta = Math.max((pathsArray[i + 0] - pathsArray[i + 4]) / 4, (pathsArray[i + 1] - pathsArray[i + 5]) / 4);
        maxDelta = Math.max(delta, maxDelta);
    }
    rect.setLowerLeftX(Math.min(minX - ab.width / 2 - maxDelta, rect.getLowerLeftX()));
    rect.setLowerLeftY(Math.min(minY - ab.width / 2 - maxDelta, rect.getLowerLeftY()));
    rect.setUpperRightX(Math.max(maxX + ab.width + maxDelta, rect.getUpperRightX()));
    rect.setUpperRightY(Math.max(maxY + ab.width + maxDelta, rect.getUpperRightY()));
    annotation.setRectangle(rect);
    PDAppearanceContentStream cs = null;
    try {
        cs = getNormalAppearanceAsContentStream();
        PDExtendedGraphicsState r0 = new PDExtendedGraphicsState();
        PDExtendedGraphicsState r1 = new PDExtendedGraphicsState();
        r0.setAlphaSourceFlag(false);
        r0.setStrokingAlphaConstant(annotation.getConstantOpacity());
        r0.setNonStrokingAlphaConstant(annotation.getConstantOpacity());
        r1.setAlphaSourceFlag(false);
        r1.setBlendMode(BlendMode.MULTIPLY);
        cs.setGraphicsStateParameters(r0);
        cs.setGraphicsStateParameters(r1);
        // TODO replace with document.getDocument().createCOSStream()
        // or call new PDFormXObject(document)
        PDFormXObject frm1 = new PDFormXObject(createCOSStream());
        PDFormXObject frm2 = new PDFormXObject(createCOSStream());
        frm1.setResources(new PDResources());
        PDFormContentStream mwfofrmCS = null;
        try {
            mwfofrmCS = new PDFormContentStream(frm1);
            mwfofrmCS.drawForm(frm2);
        } finally {
            IOUtils.closeQuietly(mwfofrmCS);
        }
        frm1.setBBox(annotation.getRectangle());
        COSDictionary groupDict = new COSDictionary();
        groupDict.setItem(COSName.S, COSName.TRANSPARENCY);
        // TODO PDFormXObject.setGroup() is missing
        frm1.getCOSObject().setItem(COSName.GROUP, groupDict);
        cs.drawForm(frm1);
        frm2.setBBox(annotation.getRectangle());
        PDFormContentStream frm2CS = null;
        try {
            frm2CS = new PDFormContentStream(frm2);
            frm2CS.setNonStrokingColor(color);
            int of = 0;
            while (of + 7 < pathsArray.length) {
                // quadpoints spec sequence is incorrect, correct one is (4,5 0,1 2,3 6,7)
                // https://stackoverflow.com/questions/9855814/pdf-spec-vs-acrobat-creation-quadpoints
                // for "curvy" highlighting, two Bézier control points are used that seem to have a
                // distance of about 1/4 of the height.
                // note that curves won't appear if outside of the rectangle
                float delta = 0;
                if (Float.compare(pathsArray[of + 0], pathsArray[of + 4]) == 0 && Float.compare(pathsArray[of + 1], pathsArray[of + 3]) == 0 && Float.compare(pathsArray[of + 2], pathsArray[of + 6]) == 0 && Float.compare(pathsArray[of + 5], pathsArray[of + 7]) == 0) {
                    // horizontal highlight
                    delta = (pathsArray[of + 1] - pathsArray[of + 5]) / 4;
                } else if (Float.compare(pathsArray[of + 1], pathsArray[of + 5]) == 0 && Float.compare(pathsArray[of + 0], pathsArray[of + 2]) == 0 && Float.compare(pathsArray[of + 3], pathsArray[of + 7]) == 0 && Float.compare(pathsArray[of + 4], pathsArray[of + 6]) == 0) {
                    // vertical highlight
                    delta = (pathsArray[of + 0] - pathsArray[of + 4]) / 4;
                }
                frm2CS.moveTo(pathsArray[of + 4], pathsArray[of + 5]);
                if (Float.compare(pathsArray[of + 0], pathsArray[of + 4]) == 0) {
                    // horizontal highlight
                    frm2CS.curveTo(pathsArray[of + 4] - delta, pathsArray[of + 5] + delta, pathsArray[of + 0] - delta, pathsArray[of + 1] - delta, pathsArray[of + 0], pathsArray[of + 1]);
                } else if (Float.compare(pathsArray[of + 5], pathsArray[of + 1]) == 0) {
                    // vertical highlight
                    frm2CS.curveTo(pathsArray[of + 4] + delta, pathsArray[of + 5] + delta, pathsArray[of + 0] - delta, pathsArray[of + 1] + delta, pathsArray[of + 0], pathsArray[of + 1]);
                } else {
                    frm2CS.lineTo(pathsArray[of + 0], pathsArray[of + 1]);
                }
                frm2CS.lineTo(pathsArray[of + 2], pathsArray[of + 3]);
                if (Float.compare(pathsArray[of + 2], pathsArray[of + 6]) == 0) {
                    // horizontal highlight
                    frm2CS.curveTo(pathsArray[of + 2] + delta, pathsArray[of + 3] - delta, pathsArray[of + 6] + delta, pathsArray[of + 7] + delta, pathsArray[of + 6], pathsArray[of + 7]);
                } else if (Float.compare(pathsArray[of + 3], pathsArray[of + 7]) == 0) {
                    // vertical highlight
                    frm2CS.curveTo(pathsArray[of + 2] - delta, pathsArray[of + 3] - delta, pathsArray[of + 6] + delta, pathsArray[of + 7] - delta, pathsArray[of + 6], pathsArray[of + 7]);
                } else {
                    frm2CS.lineTo(pathsArray[of + 6], pathsArray[of + 7]);
                }
                frm2CS.fill();
                of += 8;
            }
        } finally {
            IOUtils.closeQuietly(frm2CS);
        }
    } catch (IOException ex) {
        Log.e("PdfBox-Android", ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(cs);
    }
}
Also used : PDFormContentStream(com.tom_roush.pdfbox.pdmodel.PDFormContentStream) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) PDExtendedGraphicsState(com.tom_roush.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState) PDAnnotationTextMarkup(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) IOException(java.io.IOException) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor) PDAppearanceContentStream(com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream) PDFormXObject(com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Example 18 with PDColor

use of com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor in project PdfBox-Android by TomRoush.

the class PDLineAppearanceHandler method generateNormalAppearance.

@Override
public void generateNormalAppearance() {
    PDAnnotationLine annotation = (PDAnnotationLine) getAnnotation();
    PDRectangle rect = annotation.getRectangle();
    float[] pathsArray = annotation.getLine();
    if (pathsArray == null) {
        return;
    }
    AnnotationBorder ab = AnnotationBorder.getAnnotationBorder(annotation, annotation.getBorderStyle());
    PDColor color = annotation.getColor();
    if (color == null || color.getComponents().length == 0) {
        return;
    }
    float ll = annotation.getLeaderLineLength();
    float lle = annotation.getLeaderLineExtensionLength();
    float llo = annotation.getLeaderLineOffsetLength();
    // Adjust rectangle even if not empty, see PLPDF.com-MarkupAnnotations.pdf
    float minX = Float.MAX_VALUE;
    float minY = Float.MAX_VALUE;
    float maxX = Float.MIN_VALUE;
    float maxY = Float.MIN_VALUE;
    for (int i = 0; i < pathsArray.length / 2; ++i) {
        float x = pathsArray[i * 2];
        float y = pathsArray[i * 2 + 1];
        minX = Math.min(minX, x);
        minY = Math.min(minY, y);
        maxX = Math.max(maxX, x);
        maxY = Math.max(maxY, y);
    }
    // Leader lines
    if (ll < 0) {
        // /LLO and /LLE go in the same direction as /LL
        llo = -llo;
        lle = -lle;
    }
    // observed with diagonal line of AnnotationSample.Standard.pdf
    // for line endings, very small widths must be treated as size 1.
    // However the border of the line ending shapes is not drawn.
    float lineEndingSize = (ab.width < 1e-5) ? 1 : ab.width;
    // add/subtract with, font height, and arrows
    // arrow length is 9 * width at about 30° => 10 * width seems to be enough
    // but need to consider /LL, /LLE and /LLO too
    // TODO find better way to calculate padding
    rect.setLowerLeftX(Math.min(minX - Math.max(lineEndingSize * 10, Math.abs(llo + ll + lle)), rect.getLowerLeftX()));
    rect.setLowerLeftY(Math.min(minY - Math.max(lineEndingSize * 10, Math.abs(llo + ll + lle)), rect.getLowerLeftY()));
    rect.setUpperRightX(Math.max(maxX + Math.max(lineEndingSize * 10, Math.abs(llo + ll + lle)), rect.getUpperRightX()));
    rect.setUpperRightY(Math.max(maxY + Math.max(lineEndingSize * 10, Math.abs(llo + ll + lle)), rect.getUpperRightY()));
    annotation.setRectangle(rect);
    PDAppearanceContentStream cs = null;
    try {
        cs = getNormalAppearanceAsContentStream();
        setOpacity(cs, annotation.getConstantOpacity());
        // Tested with Adobe Reader:
        // text is written first (TODO)
        // width 0 is used by Adobe as such (but results in a visible line in rendering)
        // empty color array results in an invisible line ("n" operator) but the rest is visible
        // empty content is like no caption
        boolean hasStroke = cs.setStrokingColorOnDemand(color);
        if (ab.dashArray != null) {
            cs.setLineDashPattern(ab.dashArray, 0);
        }
        cs.setLineWidth(ab.width);
        float x1 = pathsArray[0];
        float y1 = pathsArray[1];
        float x2 = pathsArray[2];
        float y2 = pathsArray[3];
        // if there are leader lines, then the /L coordinates represent
        // the endpoints of the leader lines rather than the endpoints of the line itself.
        // so for us, llo + ll is the vertical offset for the line.
        float y = llo + ll;
        String contents = annotation.getContents();
        if (contents == null) {
            contents = "";
        }
        cs.saveGraphicsState();
        double angle = Math.atan2(y2 - y1, x2 - x1);
        cs.transform(Matrix.getRotateInstance(angle, x1, y1));
        float lineLength = (float) Math.sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
        // Leader lines
        cs.moveTo(0, llo);
        cs.lineTo(0, llo + ll + lle);
        cs.moveTo(lineLength, llo);
        cs.lineTo(lineLength, llo + ll + lle);
        if (annotation.getCaption() && !contents.isEmpty()) {
            // Note that Adobe places the text as a caption even if /CP is not set
            // when the text is so long that it would cross arrows, but we ignore this for now
            // and stick to the specification.
            PDType1Font font = PDType1Font.HELVETICA;
            // TODO: support newlines!!!!!
            // see https://www.pdfill.com/example/pdf_commenting_new.pdf
            float contentLength = 0;
            try {
                contentLength = font.getStringWidth(annotation.getContents()) / 1000 * FONT_SIZE;
            // TODO How to decide the size of the font?
            // 9 seems to be standard, but if the text doesn't fit, a scaling is done
            // see AnnotationSample.Standard.pdf, diagonal line
            } catch (IllegalArgumentException ex) {
                // Adobe Reader displays placeholders instead
                Log.e("PdfBox-Android", "line text '" + annotation.getContents() + "' can't be shown", ex);
            }
            float xOffset = (lineLength - contentLength) / 2;
            float yOffset;
            String captionPositioning = annotation.getCaptionPositioning();
            // that's the easiest way to calculate the positions for the line before and after inline caption
            if (SHORT_STYLES.contains(annotation.getStartPointEndingStyle())) {
                cs.moveTo(lineEndingSize, y);
            } else {
                cs.moveTo(0, y);
            }
            if ("Top".equals(captionPositioning)) {
                // this arbitrary number is from Adobe
                yOffset = 1.908f;
            } else {
                // Inline
                // this arbitrary number is from Adobe
                yOffset = -2.6f;
                cs.lineTo(xOffset - lineEndingSize, y);
                cs.moveTo(lineLength - xOffset + lineEndingSize, y);
            }
            if (SHORT_STYLES.contains(annotation.getEndPointEndingStyle())) {
                cs.lineTo(lineLength - lineEndingSize, y);
            } else {
                cs.lineTo(lineLength, y);
            }
            cs.drawShape(lineEndingSize, hasStroke, false);
            // /CO entry (caption offset)
            float captionHorizontalOffset = annotation.getCaptionHorizontalOffset();
            float captionVerticalOffset = annotation.getCaptionVerticalOffset();
            // check contentLength so we don't show if there was trouble before
            if (contentLength > 0) {
                cs.beginText();
                cs.setFont(font, FONT_SIZE);
                cs.newLineAtOffset(xOffset + captionHorizontalOffset, y + yOffset + captionVerticalOffset);
                cs.showText(annotation.getContents());
                cs.endText();
            }
            if (Float.compare(captionVerticalOffset, 0) != 0) {
                // Adobe paints vertical bar to the caption
                cs.moveTo(0 + lineLength / 2, y);
                cs.lineTo(0 + lineLength / 2, y + captionVerticalOffset);
                cs.drawShape(lineEndingSize, hasStroke, false);
            }
        } else {
            if (SHORT_STYLES.contains(annotation.getStartPointEndingStyle())) {
                cs.moveTo(lineEndingSize, y);
            } else {
                cs.moveTo(0, y);
            }
            if (SHORT_STYLES.contains(annotation.getEndPointEndingStyle())) {
                cs.lineTo(lineLength - lineEndingSize, y);
            } else {
                cs.lineTo(lineLength, y);
            }
            cs.drawShape(lineEndingSize, hasStroke, false);
        }
        cs.restoreGraphicsState();
        // paint the styles here and not before showing the text, or the text would appear
        // with the interior color
        boolean hasBackground = cs.setNonStrokingColorOnDemand(annotation.getInteriorColor());
        // is not drawn.
        if (ab.width < 1e-5) {
            hasStroke = false;
        }
        // check for LE_NONE only needed to avoid q cm Q for that case
        if (!LE_NONE.equals(annotation.getStartPointEndingStyle())) {
            cs.saveGraphicsState();
            if (ANGLED_STYLES.contains(annotation.getStartPointEndingStyle())) {
                cs.transform(Matrix.getRotateInstance(angle, x1, y1));
                drawStyle(annotation.getStartPointEndingStyle(), cs, 0, y, lineEndingSize, hasStroke, hasBackground, false);
            } else {
                // Support of non-angled styles is more difficult than in the other handlers
                // because the lines do not always go from (x1,y1) to (x2,y2) due to the leader lines
                // when the "y" value above is not 0.
                // We use the angle we already know and the distance y to translate to the new coordinate.
                float xx1 = x1 - (float) (y * Math.sin(angle));
                float yy1 = y1 + (float) (y * Math.cos(angle));
                drawStyle(annotation.getStartPointEndingStyle(), cs, xx1, yy1, lineEndingSize, hasStroke, hasBackground, false);
            }
            cs.restoreGraphicsState();
        }
        // check for LE_NONE only needed to avoid q cm Q for that case
        if (!LE_NONE.equals(annotation.getEndPointEndingStyle())) {
            // save / restore not needed because it's the last one
            if (ANGLED_STYLES.contains(annotation.getEndPointEndingStyle())) {
                cs.transform(Matrix.getRotateInstance(angle, x2, y2));
                drawStyle(annotation.getEndPointEndingStyle(), cs, 0, y, lineEndingSize, hasStroke, hasBackground, true);
            } else {
                // Support of non-angled styles is more difficult than in the other handlers
                // because the lines do not always go from (x1,y1) to (x2,y2) due to the leader lines
                // when the "y" value above is not 0.
                // We use the angle we already know and the distance y to translate to the new coordinate.
                float xx2 = x2 - (float) (y * Math.sin(angle));
                float yy2 = y2 + (float) (y * Math.cos(angle));
                drawStyle(annotation.getEndPointEndingStyle(), cs, xx2, yy2, lineEndingSize, hasStroke, hasBackground, true);
            }
        }
    } catch (IOException ex) {
        Log.e("PdfBox-Android", ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(cs);
    }
}
Also used : IOException(java.io.IOException) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor) PDType1Font(com.tom_roush.pdfbox.pdmodel.font.PDType1Font) PDAnnotationLine(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationLine) PDAppearanceContentStream(com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Example 19 with PDColor

use of com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor in project PdfBox-Android by TomRoush.

the class PDLinkAppearanceHandler method generateNormalAppearance.

@Override
public void generateNormalAppearance() {
    PDAnnotationLink annotation = (PDAnnotationLink) getAnnotation();
    if (annotation.getRectangle() == null) {
        // 660402-p1-AnnotationEmptyRect.pdf has /Rect entry with 0 elements
        return;
    }
    // Adobe doesn't generate an appearance for a link annotation
    float lineWidth = getLineWidth();
    PDAppearanceContentStream contentStream = null;
    try {
        contentStream = getNormalAppearanceAsContentStream();
        PDColor color = annotation.getColor();
        if (color == null) {
            // spec is unclear, but black is what Adobe does
            color = new PDColor(new float[] { 0 }, PDDeviceGray.INSTANCE);
        }
        boolean hasStroke = contentStream.setStrokingColorOnDemand(color);
        contentStream.setBorderLine(lineWidth, annotation.getBorderStyle(), annotation.getBorder());
        // Acrobat applies a padding to each side of the bbox so the line is completely within
        // the bbox.
        PDRectangle borderEdge = getPaddedRectangle(getRectangle(), lineWidth / 2);
        float[] pathsArray = annotation.getQuadPoints();
        if (pathsArray != null) {
            // QuadPoints shall be ignored if any coordinate in the array lies outside
            // the region specified by Rect.
            PDRectangle rect = annotation.getRectangle();
            for (int i = 0; i < pathsArray.length / 2; ++i) {
                if (!rect.contains(pathsArray[i * 2], pathsArray[i * 2 + 1])) {
                    Log.w("PdfBox-Android", "At least one /QuadPoints entry (" + pathsArray[i * 2] + ";" + pathsArray[i * 2 + 1] + ") is outside of rectangle, " + rect + ", /QuadPoints are ignored and /Rect is used instead");
                    pathsArray = null;
                    break;
                }
            }
        }
        if (pathsArray == null) {
            // Convert rectangle coordinates as if it was a /QuadPoints entry
            pathsArray = new float[8];
            pathsArray[0] = borderEdge.getLowerLeftX();
            pathsArray[1] = borderEdge.getLowerLeftY();
            pathsArray[2] = borderEdge.getUpperRightX();
            pathsArray[3] = borderEdge.getLowerLeftY();
            pathsArray[4] = borderEdge.getUpperRightX();
            pathsArray[5] = borderEdge.getUpperRightY();
            pathsArray[6] = borderEdge.getLowerLeftX();
            pathsArray[7] = borderEdge.getUpperRightY();
        }
        int of = 0;
        while (of + 7 < pathsArray.length) {
            if (annotation.getBorderStyle() != null && annotation.getBorderStyle().getStyle().equals(PDBorderStyleDictionary.STYLE_UNDERLINE)) {
                contentStream.moveTo(pathsArray[of], pathsArray[of + 1]);
                contentStream.lineTo(pathsArray[of + 2], pathsArray[of + 3]);
            } else {
                contentStream.moveTo(pathsArray[of], pathsArray[of + 1]);
                contentStream.lineTo(pathsArray[of + 2], pathsArray[of + 3]);
                contentStream.lineTo(pathsArray[of + 4], pathsArray[of + 5]);
                contentStream.lineTo(pathsArray[of + 6], pathsArray[of + 7]);
                contentStream.closePath();
            }
            of += 8;
        }
        contentStream.drawShape(lineWidth, hasStroke, false);
    } catch (IOException e) {
        Log.e("PdfBox-Android", e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(contentStream);
    }
}
Also used : PDAppearanceContentStream(com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) IOException(java.io.IOException) PDAnnotationLink(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)

Example 20 with PDColor

use of com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor in project PdfBox-Android by TomRoush.

the class PDPolylineAppearanceHandler method generateNormalAppearance.

@Override
public void generateNormalAppearance() {
    PDAnnotationMarkup annotation = (PDAnnotationMarkup) getAnnotation();
    PDRectangle rect = annotation.getRectangle();
    float[] pathsArray = annotation.getVertices();
    if (pathsArray == null || pathsArray.length < 4) {
        return;
    }
    AnnotationBorder ab = AnnotationBorder.getAnnotationBorder(annotation, annotation.getBorderStyle());
    PDColor color = annotation.getColor();
    if (color == null || color.getComponents().length == 0 || Float.compare(ab.width, 0) == 0) {
        return;
    }
    // Adjust rectangle even if not empty
    // CTAN-example-Annotations.pdf and pdf_commenting_new.pdf p11
    // TODO in a class structure this should be overridable
    float minX = Float.MAX_VALUE;
    float minY = Float.MAX_VALUE;
    float maxX = Float.MIN_VALUE;
    float maxY = Float.MIN_VALUE;
    for (int i = 0; i < pathsArray.length / 2; ++i) {
        float x = pathsArray[i * 2];
        float y = pathsArray[i * 2 + 1];
        minX = Math.min(minX, x);
        minY = Math.min(minY, y);
        maxX = Math.max(maxX, x);
        maxY = Math.max(maxY, y);
    }
    // arrow length is 9 * width at about 30° => 10 * width seems to be enough
    rect.setLowerLeftX(Math.min(minX - ab.width * 10, rect.getLowerLeftX()));
    rect.setLowerLeftY(Math.min(minY - ab.width * 10, rect.getLowerLeftY()));
    rect.setUpperRightX(Math.max(maxX + ab.width * 10, rect.getUpperRightX()));
    rect.setUpperRightY(Math.max(maxY + ab.width * 10, rect.getUpperRightY()));
    annotation.setRectangle(rect);
    PDAppearanceContentStream cs = null;
    try {
        cs = getNormalAppearanceAsContentStream();
        boolean hasBackground = cs.setNonStrokingColorOnDemand(annotation.getInteriorColor());
        setOpacity(cs, annotation.getConstantOpacity());
        boolean hasStroke = cs.setStrokingColorOnDemand(color);
        if (ab.dashArray != null) {
            cs.setLineDashPattern(ab.dashArray, 0);
        }
        cs.setLineWidth(ab.width);
        for (int i = 0; i < pathsArray.length / 2; ++i) {
            float x = pathsArray[i * 2];
            float y = pathsArray[i * 2 + 1];
            if (i == 0) {
                if (SHORT_STYLES.contains(annotation.getStartPointEndingStyle())) {
                    // modify coordinate to shorten the segment
                    // https://stackoverflow.com/questions/7740507/extend-a-line-segment-a-specific-distance
                    float x1 = pathsArray[2];
                    float y1 = pathsArray[3];
                    float len = (float) (Math.sqrt(Math.pow(x - x1, 2) + Math.pow(y - y1, 2)));
                    if (Float.compare(len, 0) != 0) {
                        x += (x1 - x) / len * ab.width;
                        y += (y1 - y) / len * ab.width;
                    }
                }
                cs.moveTo(x, y);
            } else {
                if (i == pathsArray.length / 2 - 1 && SHORT_STYLES.contains(annotation.getEndPointEndingStyle())) {
                    // modify coordinate to shorten the segment
                    // https://stackoverflow.com/questions/7740507/extend-a-line-segment-a-specific-distance
                    float x0 = pathsArray[pathsArray.length - 4];
                    float y0 = pathsArray[pathsArray.length - 3];
                    float len = (float) (Math.sqrt(Math.pow(x0 - x, 2) + Math.pow(y0 - y, 2)));
                    if (Float.compare(len, 0) != 0) {
                        x -= (x - x0) / len * ab.width;
                        y -= (y - y0) / len * ab.width;
                    }
                }
                cs.lineTo(x, y);
            }
        }
        cs.stroke();
        // paint the styles here and after polyline draw, to avoid line crossing a filled shape
        if (!LE_NONE.equals(annotation.getStartPointEndingStyle())) {
            // check only needed to avoid q cm Q if LE_NONE
            float x2 = pathsArray[2];
            float y2 = pathsArray[3];
            float x1 = pathsArray[0];
            float y1 = pathsArray[1];
            cs.saveGraphicsState();
            if (ANGLED_STYLES.contains(annotation.getStartPointEndingStyle())) {
                double angle = Math.atan2(y2 - y1, x2 - x1);
                cs.transform(Matrix.getRotateInstance(angle, x1, y1));
            } else {
                cs.transform(Matrix.getTranslateInstance(x1, y1));
            }
            drawStyle(annotation.getStartPointEndingStyle(), cs, 0, 0, ab.width, hasStroke, hasBackground, false);
            cs.restoreGraphicsState();
        }
        if (!LE_NONE.equals(annotation.getEndPointEndingStyle())) {
            // check only needed to avoid q cm Q if LE_NONE
            float x1 = pathsArray[pathsArray.length - 4];
            float y1 = pathsArray[pathsArray.length - 3];
            float x2 = pathsArray[pathsArray.length - 2];
            float y2 = pathsArray[pathsArray.length - 1];
            // save / restore not needed because it's the last one
            if (ANGLED_STYLES.contains(annotation.getEndPointEndingStyle())) {
                double angle = Math.atan2(y2 - y1, x2 - x1);
                cs.transform(Matrix.getRotateInstance(angle, x2, y2));
            } else {
                cs.transform(Matrix.getTranslateInstance(x2, y2));
            }
            drawStyle(annotation.getEndPointEndingStyle(), cs, 0, 0, ab.width, hasStroke, hasBackground, true);
        }
    } catch (IOException ex) {
        Log.e("PdfBox-Android", ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(cs);
    }
}
Also used : PDAppearanceContentStream(com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream) PDAnnotationMarkup(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) IOException(java.io.IOException) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)

Aggregations

PDColor (com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)22 IOException (java.io.IOException)12 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)11 PDAppearanceContentStream (com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream)10 COSArray (com.tom_roush.pdfbox.cos.COSArray)5 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)4 PDAnnotationTextMarkup (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup)4 PDAnnotationMarkup (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup)3 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)2 PDFormContentStream (com.tom_roush.pdfbox.pdmodel.PDFormContentStream)2 PDColorSpace (com.tom_roush.pdfbox.pdmodel.graphics.color.PDColorSpace)2 PDFormXObject (com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject)2 PDAppearanceCharacteristicsDictionary (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceCharacteristicsDictionary)2 PDBorderStyleDictionary (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary)2 Matrix (com.tom_roush.pdfbox.util.Matrix)2 RectF (android.graphics.RectF)1 MissingOperandException (com.tom_roush.pdfbox.contentstream.operator.MissingOperandException)1 Operator (com.tom_roush.pdfbox.contentstream.operator.Operator)1 COSBase (com.tom_roush.pdfbox.cos.COSBase)1 COSFloat (com.tom_roush.pdfbox.cos.COSFloat)1