use of com.tom_roush.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState in project PdfBox-Android by TomRoush.
the class PDAbstractAppearanceHandler method setOpacity.
void setOpacity(PDAppearanceContentStream contentStream, float opacity) throws IOException {
if (opacity < 1) {
PDExtendedGraphicsState gs = new PDExtendedGraphicsState();
gs.setStrokingAlphaConstant(opacity);
gs.setNonStrokingAlphaConstant(opacity);
contentStream.setGraphicsStateParameters(gs);
}
}
use of com.tom_roush.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState 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);
}
}
use of com.tom_roush.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState 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.graphics.state.PDExtendedGraphicsState in project PdfBox-Android by TomRoush.
the class PDTextAppearanceHandler method drawParagraph.
private void drawParagraph(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 / 3, 0.001f * min / 3));
contentStream.transform(Matrix.getTranslateInstance(850, 900));
// we get the shape of an Helvetica "?" and use that one.
// Adobe uses a different font (which one?), or created the shape from scratch.
Path path = PDType1Font.HELVETICA.getPath("paragraph");
addPath(contentStream, path);
contentStream.restoreGraphicsState();
contentStream.fillAndStroke();
drawCircle(contentStream, min / 2, min / 2, min / 2 - 1);
contentStream.stroke();
}
use of com.tom_roush.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState in project PdfBox-Android by TomRoush.
the class PDTextAppearanceHandler method drawComment.
private void drawComment(PDAnnotationText annotation, final PDAppearanceContentStream contentStream) throws IOException {
adjustRectAndBBox(annotation, 18, 18);
contentStream.setMiterLimit(4);
contentStream.setLineJoinStyle(1);
contentStream.setLineCapStyle(0);
contentStream.setLineWidth(200);
// Adobe first fills a white rectangle 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);
contentStream.addRect(0.3f, 0.3f, 18 - 0.6f, 18 - 0.6f);
contentStream.fill();
contentStream.restoreGraphicsState();
contentStream.transform(Matrix.getScaleInstance(0.003f, 0.003f));
contentStream.transform(Matrix.getTranslateInstance(500, -300));
// outer shape was gathered from Font Awesome by "printing" comment.svg
// into a PDF and looking at the content stream
contentStream.moveTo(2549, 5269);
contentStream.curveTo(1307, 5269, 300, 4451, 300, 3441);
contentStream.curveTo(300, 3023, 474, 2640, 764, 2331);
contentStream.curveTo(633, 1985, 361, 1691, 357, 1688);
contentStream.curveTo(299, 1626, 283, 1537, 316, 1459);
contentStream.curveTo(350, 1382, 426, 1332, 510, 1332);
contentStream.curveTo(1051, 1332, 1477, 1558, 1733, 1739);
contentStream.curveTo(1987, 1659, 2261, 1613, 2549, 1613);
contentStream.curveTo(3792, 1613, 4799, 2431, 4799, 3441);
contentStream.curveTo(4799, 4451, 3792, 5269, 2549, 5269);
contentStream.closePath();
// can't use addRect: if we did that, we wouldn't get the donut effect
contentStream.moveTo(0.3f / 0.003f - 500, 0.3f / 0.003f + 300);
contentStream.lineTo(0.3f / 0.003f - 500, 0.3f / 0.003f + 300 + 17.4f / 0.003f);
contentStream.lineTo(0.3f / 0.003f - 500 + 17.4f / 0.003f, 0.3f / 0.003f + 300 + 17.4f / 0.003f);
contentStream.lineTo(0.3f / 0.003f - 500 + 17.4f / 0.003f, 0.3f / 0.003f + 300);
contentStream.closeAndFillAndStroke();
}
Aggregations