use of com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream 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);
}
}
use of com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream 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.PDAppearanceContentStream 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.PDAppearanceContentStream 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.PDAppearanceContentStream in project PdfBox-Android by TomRoush.
the class PDUnderlineAppearanceHandler 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;
}
if (Float.compare(ab.width, 0) == 0) {
// value found in adobe reader
ab.width = 1.5f;
}
// 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
// all coordinates (unlike painting) are used because I'm lazy
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);
}
rect.setLowerLeftX(Math.min(minX - ab.width / 2, rect.getLowerLeftX()));
rect.setLowerLeftY(Math.min(minY - ab.width / 2, rect.getLowerLeftY()));
rect.setUpperRightX(Math.max(maxX + ab.width / 2, rect.getUpperRightX()));
rect.setUpperRightY(Math.max(maxY + ab.width / 2, rect.getUpperRightY()));
annotation.setRectangle(rect);
PDAppearanceContentStream cs = null;
try {
cs = getNormalAppearanceAsContentStream();
setOpacity(cs, annotation.getConstantOpacity());
cs.setStrokingColor(color);
if (ab.dashArray != null) {
cs.setLineDashPattern(ab.dashArray, 0);
}
cs.setLineWidth(ab.width);
// https://stackoverflow.com/questions/9855814/pdf-spec-vs-acrobat-creation-quadpoints
for (int i = 0; i < pathsArray.length / 8; ++i) {
// Adobe doesn't use the lower coordinate for the line, it uses lower + delta / 7.
// do the math for diagonal annotations with this weird old trick:
// https://stackoverflow.com/questions/7740507/extend-a-line-segment-a-specific-distance
float len0 = (float) (Math.sqrt(Math.pow(pathsArray[i * 8] - pathsArray[i * 8 + 4], 2) + Math.pow(pathsArray[i * 8 + 1] - pathsArray[i * 8 + 5], 2)));
float x0 = pathsArray[i * 8 + 4];
float y0 = pathsArray[i * 8 + 5];
if (Float.compare(len0, 0) != 0) {
// only if both coordinates are not identical to avoid divide by zero
x0 += (pathsArray[i * 8] - pathsArray[i * 8 + 4]) / len0 * len0 / 7;
y0 += (pathsArray[i * 8 + 1] - pathsArray[i * 8 + 5]) / len0 * (len0 / 7);
}
float len1 = (float) (Math.sqrt(Math.pow(pathsArray[i * 8 + 2] - pathsArray[i * 8 + 6], 2) + Math.pow(pathsArray[i * 8 + 3] - pathsArray[i * 8 + 7], 2)));
float x1 = pathsArray[i * 8 + 6];
float y1 = pathsArray[i * 8 + 7];
if (Float.compare(len1, 0) != 0) {
// only if both coordinates are not identical to avoid divide by zero
x1 += (pathsArray[i * 8 + 2] - pathsArray[i * 8 + 6]) / len1 * len1 / 7;
y1 += (pathsArray[i * 8 + 3] - pathsArray[i * 8 + 7]) / len1 * len1 / 7;
}
cs.moveTo(x0, y0);
cs.lineTo(x1, y1);
}
cs.stroke();
} catch (IOException ex) {
Log.e("PdfBox-Android", ex.getMessage(), ex);
} finally {
IOUtils.closeQuietly(cs);
}
}
Aggregations