use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle 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);
}
}
use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle 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);
}
}
use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.
the class PDSquareAppearanceHandler method generateNormalAppearance.
@Override
public void generateNormalAppearance() {
float lineWidth = getLineWidth();
PDAnnotationSquareCircle annotation = (PDAnnotationSquareCircle) getAnnotation();
PDAppearanceContentStream contentStream = null;
try {
contentStream = getNormalAppearanceAsContentStream();
boolean hasStroke = contentStream.setStrokingColorOnDemand(getColor());
boolean hasBackground = contentStream.setNonStrokingColorOnDemand(annotation.getInteriorColor());
setOpacity(contentStream, annotation.getConstantOpacity());
contentStream.setBorderLine(lineWidth, annotation.getBorderStyle(), annotation.getBorder());
PDBorderEffectDictionary borderEffect = annotation.getBorderEffect();
if (borderEffect != null && borderEffect.getStyle().equals(PDBorderEffectDictionary.STYLE_CLOUDY)) {
CloudyBorder cloudyBorder = new CloudyBorder(contentStream, borderEffect.getIntensity(), lineWidth, 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 {
PDRectangle borderBox = handleBorderBox(annotation, lineWidth);
contentStream.addRect(borderBox.getLowerLeftX(), borderBox.getLowerLeftY(), borderBox.getWidth(), borderBox.getHeight());
}
contentStream.drawShape(lineWidth, hasStroke, hasBackground);
} catch (IOException e) {
Log.e("PdfBox-Android", e.getMessage(), e);
} finally {
IOUtils.closeQuietly(contentStream);
}
}
use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.
the class PDTextAppearanceHandler method drawHelp.
private void drawHelp(PDAnnotationText annotation, final PDAppearanceContentStream contentStream) throws IOException {
PDRectangle bbox = adjustRectAndBBox(annotation, 20, 20);
float min = Math.min(bbox.getWidth(), bbox.getHeight());
contentStream.setMiterLimit(4);
contentStream.setLineJoinStyle(1);
contentStream.setLineCapStyle(0);
// value from Adobe
contentStream.setLineWidth(0.59f);
// Adobe first fills a white circle with CA ca 0.6, so do we
contentStream.saveGraphicsState();
contentStream.setLineWidth(1);
PDExtendedGraphicsState gs = new PDExtendedGraphicsState();
gs.setAlphaSourceFlag(false);
gs.setStrokingAlphaConstant(0.6f);
gs.setNonStrokingAlphaConstant(0.6f);
gs.setBlendMode(BlendMode.NORMAL);
contentStream.setGraphicsStateParameters(gs);
contentStream.setNonStrokingColor(1f);
drawCircle2(contentStream, min / 2, min / 2, min / 2 - 1);
contentStream.fill();
contentStream.restoreGraphicsState();
contentStream.saveGraphicsState();
// rescale so that "?" fits into circle and move "?" to circle center
// values gathered by trial and error
contentStream.transform(Matrix.getScaleInstance(0.001f * min / 2.25f, 0.001f * min / 2.25f));
contentStream.transform(Matrix.getTranslateInstance(500, 375));
// we get the shape of an Helvetica bold "?" and use that one.
// Adobe uses a different font (which one?), or created the shape from scratch.
Path path = PDType1Font.HELVETICA_BOLD.getPath("question");
addPath(contentStream, path);
contentStream.restoreGraphicsState();
// draw the outer circle counterclockwise to fill area between circle and "?"
drawCircle2(contentStream, min / 2, min / 2, min / 2 - 1);
contentStream.fillAndStroke();
}
use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.
the class PDTextAppearanceHandler method adjustRectAndBBox.
private PDRectangle adjustRectAndBBox(PDAnnotationText annotation, float width, float height) {
// For /Note (other types have different values):
// Adobe takes the left upper bound as anchor, and adjusts the rectangle to 18 x 20.
// Observed with files 007071.pdf, 038785.pdf, 038787.pdf,
// but not with 047745.pdf p133 and 084374.pdf p48, both have the NoZoom flag.
// there the BBox is also set to fixed values, but the rectangle is left untouched.
// When no flags are there, Adobe sets /F 24 = NoZoom NoRotate.
PDRectangle rect = getRectangle();
PDRectangle bbox;
if (!annotation.isNoZoom()) {
rect.setUpperRightX(rect.getLowerLeftX() + width);
rect.setLowerLeftY(rect.getUpperRightY() - height);
annotation.setRectangle(rect);
}
if (!annotation.getCOSObject().containsKey(COSName.F)) {
// We set these flags because Adobe does so, but PDFBox doesn't support them when rendering.
annotation.setNoRotate(true);
annotation.setNoZoom(true);
}
bbox = new PDRectangle(width, height);
annotation.getNormalAppearanceStream().setBBox(bbox);
return bbox;
}
Aggregations