use of com.tom_roush.pdfbox.pdfparser.PDFStreamParser in project PdfBox-Android by TomRoush.
the class PDFreeTextAppearanceHandler method extractNonStrokingColor.
// get the last non stroking color from the /DA entry
private PDColor extractNonStrokingColor(PDAnnotationMarkup annotation) {
// It could also work with a regular expression, but that should be written so that
// "/LucidaConsole 13.94766 Tf .392 .585 .93 rg" does not produce "2 .585 .93 rg" as result
// Another alternative might be to create a PDDocument and a PDPage with /DA content as /Content,
// process the whole thing and then get the non stroking color.
PDColor strokingColor = new PDColor(new float[] { 0 }, PDDeviceGray.INSTANCE);
String defaultAppearance = annotation.getDefaultAppearance();
if (defaultAppearance == null) {
return strokingColor;
}
try {
// not sure if charset is correct, but we only need numbers and simple characters
PDFStreamParser parser = new PDFStreamParser(defaultAppearance.getBytes(Charsets.US_ASCII));
COSArray arguments = new COSArray();
COSArray colors = null;
Operator graphicOp = null;
for (Object token = parser.parseNextToken(); token != null; token = parser.parseNextToken()) {
if (token instanceof COSObject) {
arguments.add(((COSObject) token).getObject());
} else if (token instanceof Operator) {
Operator op = (Operator) token;
String name = op.getName();
if (OperatorName.NON_STROKING_GRAY.equals(name) || OperatorName.NON_STROKING_RGB.equals(name) || OperatorName.NON_STROKING_CMYK.equals(name)) {
graphicOp = op;
colors = arguments;
}
arguments = new COSArray();
} else {
arguments.add((COSBase) token);
}
}
if (graphicOp != null) {
String graphicOpName = graphicOp.getName();
if (OperatorName.NON_STROKING_GRAY.equals(graphicOpName)) {
strokingColor = new PDColor(colors, PDDeviceGray.INSTANCE);
} else if (OperatorName.NON_STROKING_RGB.equals(graphicOpName)) {
strokingColor = new PDColor(colors, PDDeviceRGB.INSTANCE);
} else if (OperatorName.NON_STROKING_CMYK.equals(graphicOpName)) {
// strokingColor = new PDColor(colors, PDDeviceCMYK.INSTANCE); TODO: PdfBox-Android
}
}
} catch (IOException ex) {
Log.w("PdfBox-Android", "Problem parsing /DA, will use default black", ex);
}
return strokingColor;
}
use of com.tom_roush.pdfbox.pdfparser.PDFStreamParser in project PdfBox-Android by TomRoush.
the class TestPDPageContentStream method testSetRGBandGColors.
public void testSetRGBandGColors() throws IOException {
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, true);
// pass a non-stroking color in RGB and Gray color space
contentStream.setNonStrokingColor(0.1f, 0.2f, 0.3f);
contentStream.setNonStrokingColor(1, 2, 3);
contentStream.setNonStrokingColor(0.8f);
contentStream.setNonStrokingColor(8);
contentStream.close();
// now read the PDF stream and verify that the values are correct
PDFStreamParser parser = new PDFStreamParser(page);
parser.parse();
List<Object> pageTokens = parser.getTokens();
assertEquals(0.1f, ((COSNumber) pageTokens.get(0)).floatValue());
assertEquals(0.2f, ((COSNumber) pageTokens.get(1)).floatValue());
assertEquals(0.3f, ((COSNumber) pageTokens.get(2)).floatValue());
assertEquals(OperatorName.NON_STROKING_RGB, ((Operator) pageTokens.get(3)).getName());
assertEquals(1 / 255f, ((COSNumber) pageTokens.get(4)).floatValue(), 0.00001d);
assertEquals(2 / 255f, ((COSNumber) pageTokens.get(5)).floatValue(), 0.00001d);
assertEquals(3 / 255f, ((COSNumber) pageTokens.get(6)).floatValue(), 0.00001d);
assertEquals(OperatorName.NON_STROKING_RGB, ((Operator) pageTokens.get(7)).getName());
assertEquals(0.8f, ((COSNumber) pageTokens.get(8)).floatValue());
assertEquals(OperatorName.NON_STROKING_GRAY, ((Operator) pageTokens.get(9)).getName());
assertEquals(8 / 255f, ((COSNumber) pageTokens.get(10)).floatValue(), 0.00001d);
assertEquals(OperatorName.NON_STROKING_GRAY, ((Operator) pageTokens.get(11)).getName());
// same as above but for PDPageContentStream#setStrokingColor
page = new PDPage();
doc.addPage(page);
contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, false);
// pass a non-stroking color in RGB and Gray color space
contentStream.setStrokingColor(0.5f, 0.6f, 0.7f);
contentStream.setStrokingColor(5, 6, 7);
contentStream.setStrokingColor(0.8f);
contentStream.setStrokingColor(8);
contentStream.close();
// now read the PDF stream and verify that the values are correct
parser = new PDFStreamParser(page);
parser.parse();
pageTokens = parser.getTokens();
assertEquals(0.5f, ((COSNumber) pageTokens.get(0)).floatValue());
assertEquals(0.6f, ((COSNumber) pageTokens.get(1)).floatValue());
assertEquals(0.7f, ((COSNumber) pageTokens.get(2)).floatValue());
assertEquals(OperatorName.STROKING_COLOR_RGB, ((Operator) pageTokens.get(3)).getName());
assertEquals(5 / 255f, ((COSNumber) pageTokens.get(4)).floatValue(), 0.00001d);
assertEquals(6 / 255f, ((COSNumber) pageTokens.get(5)).floatValue(), 0.00001d);
assertEquals(7 / 255f, ((COSNumber) pageTokens.get(6)).floatValue(), 0.00001d);
assertEquals(OperatorName.STROKING_COLOR_RGB, ((Operator) pageTokens.get(7)).getName());
assertEquals(0.8f, ((COSNumber) pageTokens.get(8)).floatValue());
assertEquals(OperatorName.STROKING_COLOR_GRAY, ((Operator) pageTokens.get(9)).getName());
assertEquals(8 / 255f, ((COSNumber) pageTokens.get(10)).floatValue(), 0.00001d);
assertEquals(OperatorName.STROKING_COLOR_GRAY, ((Operator) pageTokens.get(11)).getName());
doc.close();
}
use of com.tom_roush.pdfbox.pdfparser.PDFStreamParser in project PdfBox-Android by TomRoush.
the class PDType3CharProc method getWidth.
/**
* Get the width from a type3 charproc stream.
*
* @return the glyph width.
* @throws IOException if the stream could not be read, or did not have d0 or d1 as first
* operator, or if their first argument was not a number.
*/
public float getWidth() throws IOException {
List<COSBase> arguments = new ArrayList<COSBase>();
PDFStreamParser parser = new PDFStreamParser(this);
Object token = parser.parseNextToken();
while (token != null) {
if (token instanceof COSObject) {
arguments.add(((COSObject) token).getObject());
} else if (token instanceof Operator) {
return parseWidth((Operator) token, arguments);
} else {
arguments.add((COSBase) token);
}
token = parser.parseNextToken();
}
throw new IOException("Unexpected end of stream");
}
use of com.tom_roush.pdfbox.pdfparser.PDFStreamParser in project PdfBox-Android by TomRoush.
the class PDType3CharProc method getGlyphBBox.
/**
* Calculate the bounding box of this glyph. This will work only if the first operator in the
* stream is d1.
*
* @return the bounding box of this glyph, or null if the first operator is not d1.
* @throws IOException If an io error occurs while parsing the stream.
*/
public PDRectangle getGlyphBBox() throws IOException {
List<COSBase> arguments = new ArrayList<COSBase>();
PDFStreamParser parser = new PDFStreamParser(this);
Object token = parser.parseNextToken();
while (token != null) {
if (token instanceof COSObject) {
arguments.add(((COSObject) token).getObject());
} else if (token instanceof Operator) {
if (((Operator) token).getName().equals("d1") && arguments.size() == 6) {
for (int i = 0; i < 6; ++i) {
if (!(arguments.get(i) instanceof COSNumber)) {
return null;
}
}
return new PDRectangle(((COSNumber) arguments.get(2)).floatValue(), ((COSNumber) arguments.get(3)).floatValue(), ((COSNumber) arguments.get(4)).floatValue() - ((COSNumber) arguments.get(2)).floatValue(), ((COSNumber) arguments.get(5)).floatValue() - ((COSNumber) arguments.get(3)).floatValue());
} else {
return null;
}
} else {
arguments.add((COSBase) token);
}
token = parser.parseNextToken();
}
return null;
}
use of com.tom_roush.pdfbox.pdfparser.PDFStreamParser in project PdfBox-Android by TomRoush.
the class AppearanceGeneratorHelper method tokenize.
/**
* Parses an appearance stream into tokens.
*/
private List<Object> tokenize(PDAppearanceStream appearanceStream) throws IOException {
PDFStreamParser parser = new PDFStreamParser(appearanceStream);
parser.parse();
return parser.getTokens();
}
Aggregations