static int[] convertSimpleWidthsArray(PdfArray widthsArray, int first, int missingWidth) { int[] res = new int[256]; for (int i = 0; i < res.length; i++) { res[i] = missingWidth; } if (widthsArray == null) { Logger logger = LoggerFactory.getLogger(FontUtil.class); logger.warn(LogMessageConstant.FONT_DICTIONARY_WITH_NO_WIDTHS); return res; } for (int i = 0; i < widthsArray.size() && first + i < 256; i++) { PdfNumber number = widthsArray.getAsNumber(i); res[first + i] = number != null ? number.intValue() : missingWidth; } return res; }
/** * Converts a {@link com.itextpdf.kernel.pdf.PdfArray} to an array of longs * * @param pdfArray PdfArray to be converted * @return long[] containing the PdfArray values * @deprecated Will be removed in 7.2. Use {@link PdfArray#toLongArray()} instead */ @Deprecated public static long[] asLongArray(PdfArray pdfArray) { long[] rslt = new long[pdfArray.size()]; for (int k = 0; k < rslt.length; ++k) { rslt[k] = pdfArray.getAsNumber(k).longValue(); } return rslt; }
/** * Converts a {@link com.itextpdf.kernel.pdf.PdfArray} to an array of longs * * @param pdfArray PdfArray to be converted * @return long[] containing the PdfArray values * @deprecated Will be removed in 7.2. Use {@link PdfArray#toLongArray()} instead */ @Deprecated public static long[] asLongArray(PdfArray pdfArray) { long[] rslt = new long[pdfArray.size()]; for (int k = 0; k < rslt.length; ++k) { rslt[k] = pdfArray.getAsNumber(k).longValue(); } return rslt; }
/** * Returns the first four elements of this array as a PdfArray. The first four values need to be * PdfNumbers, if not a PdfException will be thrown. * * @return Rectangle of the first four values * @throws com.itextpdf.kernel.PdfException if one of the first values isn't a PdfNumber */ public Rectangle toRectangle() { try { float x1 = getAsNumber(0).floatValue(); float y1 = getAsNumber(1).floatValue(); float x2 = getAsNumber(2).floatValue(); float y2 = getAsNumber(3).floatValue(); float llx, lly, urx, ury; //Find the lower-left and upper-right of these 4 points llx = Math.min(x1,x2); lly = Math.min(y1,y2); urx = Math.max(x1,x2); ury = Math.max(y1,y2); return new Rectangle(llx, lly, urx - llx, ury - lly); } catch (Exception e) { throw new PdfException(PdfException.CannotConvertPdfArrayToRectanle, e, this); } }
/** * Adds I{@code PdfFormXObject} to the specified position with specified width preserving aspect ratio. * * @param form * @param x * @param y * @param width * @return current canvas. */ private PdfCanvas addForm(PdfFormXObject form, float x, float y, float width) { PdfArray bbox = form.getPdfObject().getAsArray(PdfName.BBox); if (bbox == null) throw new PdfException(PdfException.PdfFormXobjectHasInvalidBbox); float formWidth = Math.abs(bbox.getAsNumber(2).floatValue() - bbox.getAsNumber(0).floatValue()); float formHeight = Math.abs(bbox.getAsNumber(3).floatValue() - bbox.getAsNumber(1).floatValue()); return addForm(form, width, 0, 0, width / formWidth * formHeight, x, y); }
/** * The interior color which is used to fill areas specific for different types of annotation. For {@link PdfLineAnnotation} * and polyline annotation ({@link PdfPolyGeomAnnotation} - the annotation's line endings, for {@link PdfSquareAnnotation} * and {@link PdfCircleAnnotation} - the annotation's rectangle or ellipse, for {@link PdfRedactAnnotation} - the redacted * region after the affected content has been removed. * @return {@link Color} of either {@link DeviceGray}, {@link DeviceRgb} or {@link DeviceCmyk} type which defines * interior color of the annotation, or null if interior color is not specified. */ public static Color parseInteriorColor(PdfArray color) { if (color == null) { return null; } switch (color.size()) { case 1: return new DeviceGray(color.getAsNumber(0).floatValue()); case 3: return new DeviceRgb(color.getAsNumber(0).floatValue(), color.getAsNumber(1).floatValue(), color.getAsNumber(2).floatValue()); case 4: return new DeviceCmyk(color.getAsNumber(0).floatValue(), color.getAsNumber(1).floatValue(), color.getAsNumber(2).floatValue(), color.getAsNumber(3).floatValue()); default: return null; } } }
/** * Adds {@code PdfFormXObject} to the specified position with specified height preserving aspect ratio. * * @param form * @param x * @param y * @param height * @param dummy * @return current canvas */ private PdfCanvas addForm(PdfFormXObject form, float x, float y, float height, boolean dummy) { PdfArray bbox = form.getPdfObject().getAsArray(PdfName.BBox); if (bbox == null) throw new PdfException(PdfException.PdfFormXobjectHasInvalidBbox); float formWidth = Math.abs(bbox.getAsNumber(2).floatValue() - bbox.getAsNumber(0).floatValue()); float formHeight = Math.abs(bbox.getAsNumber(3).floatValue() - bbox.getAsNumber(1).floatValue()); return addForm(form, height / formHeight * formWidth, 0, 0, height, x, y); }
/** * Concatenates the 2x3 affine transformation matrix to the current matrix * in the content stream managed by this Canvas. * If an array not containing the 6 values of the matrix is passed, * The current canvas is returned unchanged. * * @param array affine transformation stored as a PdfArray with 6 values * @return current canvas */ public PdfCanvas concatMatrix(PdfArray array) { if (array.size() != 6) { //Throw exception or warning here return this; } for (int i = 0; i < array.size(); i++) { if (!array.get(i).isNumber()) { return this; } } return concatMatrix(array.getAsNumber(0).doubleValue(), array.getAsNumber(1).doubleValue(), array.getAsNumber(2).doubleValue(), array.getAsNumber(3).doubleValue(), array.getAsNumber(4).doubleValue(), array.getAsNumber(5).doubleValue()); }
/** * Gets width based on XObject's BBox. * * @return float value. */ @Override public float getWidth() { return getBBox() == null ? 0 : getBBox().getAsNumber(2).floatValue() - getBBox().getAsNumber(0).floatValue(); }
/** * Gets height based on XObject's BBox. * * @return float value. */ @Override public float getHeight() { return getBBox() == null ? 0 : getBBox().getAsNumber(3).floatValue() - getBBox().getAsNumber(1).floatValue(); }
Rectangle calculateViewPort(SvgDrawContext context){ float portX = 0f; float portY = 0f; float portWidth = 0f; float portHeight = 0f; PdfStream contentStream = context.getCurrentCanvas().getContentStream(); if ( ! contentStream.containsKey(PdfName.BBox) ) { throw new SvgProcessingException(SvgLogMessageConstant.ROOT_SVG_NO_BBOX); } PdfArray bboxArray = contentStream.getAsArray(PdfName.BBox); portX = bboxArray.getAsNumber(0).floatValue(); portY = bboxArray.getAsNumber(1).floatValue(); portWidth = bboxArray.getAsNumber(2).floatValue() - portX; portHeight = bboxArray.getAsNumber(3).floatValue() - portY; return new Rectangle(portX, portY, portWidth, portHeight); }
/** * Returns this array as an array of floats. Will throw a PdfException when it encounters an issue. * * @return this array as an array of floats * @throws com.itextpdf.kernel.PdfException if one of the values isn't a number */ public float[] toFloatArray() { try { float[] rslt = new float[size()]; for (int k = 0; k < rslt.length; ++ k) { rslt[k] = getAsNumber(k).floatValue(); } return rslt; } catch (Exception e) { throw new PdfException(PdfException.CannotConvertPdfArrayToFloatArray, e, this); } }
/** * Returns this array as an array of longs. Will throw a PdfException when it encounters an issue. * * @return this array as an array of longs * @throws com.itextpdf.kernel.PdfException if one of the values isn't a number */ public long[] toLongArray() { try { long[] rslt = new long[size()]; for (int k = 0; k < rslt.length; ++ k) { rslt[k] = getAsNumber(k).longValue(); } return rslt; } catch (Exception e) { throw new PdfException(PdfException.CannotConvertPdfArrayToLongArray, e, this); } }
/** * Returns this array as an array of ints. Will throw a PdfException when it encounters an issue. * * @return this array as an array of ints * @throws com.itextpdf.kernel.PdfException if one of the values isn't a number */ public int[] toIntArray() { try { int[] rslt = new int[size()]; for (int k = 0; k < rslt.length; ++ k) { rslt[k] = getAsNumber(k).intValue(); } return rslt; } catch (Exception e) { throw new PdfException(PdfException.CannotConvertPdfArrayToIntArray, e, this); } }
/** * Returns this array as an array of doubles. Will throw a PdfException when it encounters an issue. * * @return this array as an array of doubles * @throws com.itextpdf.kernel.PdfException if one of the values isn't a number */ public double[] toDoubleArray() { try { double[] rslt = new double[size()]; for (int k = 0; k < rslt.length; ++ k) { rslt[k] = getAsNumber(k).doubleValue(); } return rslt; } catch (Exception e) { throw new PdfException(PdfException.CannotConvertPdfArrayToDoubleArray, e, this); } }
/** * PDF 2.0. Gets halftone origin * * @return an array of two values specifying X and Y values of the halftone origin in the current coordinate system, * respectively, or <code>null</code> if halftone origin is not specified */ public float[] getHalftoneOrigin() { PdfArray hto = getPdfObject().getAsArray(PdfName.HTO); if (hto != null && hto.size() == 2 && hto.get(0).isNumber() && hto.get(1).isNumber()) { return new float[]{hto.getAsNumber(0).floatValue(), hto.getAsNumber(1).floatValue()}; } else { return null; } }
private PdfNumber flattenNumTree(PdfDictionary dictionary, PdfNumber leftOver, LinkedList<PdfObject> items /*Map<PdfNumber, PdfObject> items*/) { PdfArray nums = dictionary.getAsArray(PdfName.Nums); if (nums != null) { for (int k = 0; k < nums.size(); k++) { PdfNumber number; if (leftOver == null) number = nums.getAsNumber(k++); else { number = leftOver; leftOver = null; } if (k < nums.size()) { items.addLast(number); items.addLast(nums.get(k, false)); } else { return number; } } } else if ((nums = dictionary.getAsArray(PdfName.Kids)) != null) { for (int k = 0; k < nums.size(); k++) { PdfDictionary kid = nums.getAsDictionary(k); leftOver = flattenNumTree(kid, leftOver, items); } } return null; }
private PdfNumber iterateItems(PdfDictionary dictionary, PdfNumber leftOver) { PdfArray nums = dictionary.getAsArray(PdfName.Nums); if (nums != null) { for (int k = 0; k < nums.size(); k++) { PdfNumber number; if (leftOver == null) number = nums.getAsNumber(k++); else { number = leftOver; leftOver = null; } if (k < nums.size()) { items.put(number.intValue(), nums.get(k)); } else { return number; } } } else if ((nums = dictionary.getAsArray(PdfName.Kids)) != null) { for (int k = 0; k < nums.size(); k++) { PdfDictionary kid = nums.getAsDictionary(k); leftOver = iterateItems(kid, leftOver); } } return null; } }
private Color getColor(PdfDictionary appearanceCharacteristics, PdfName property) { PdfArray colorData = appearanceCharacteristics.getAsArray(property); if (colorData != null) { float[] backgroundFloat = new float[colorData.size()]; for (int i = 0; i < colorData.size(); i++) backgroundFloat[i] = colorData.getAsNumber(i).floatValue(); switch (colorData.size()) { case 0: return null; case 1: return new DeviceGray(backgroundFloat[0]); case 3: return new DeviceRgb(backgroundFloat[0], backgroundFloat[1], backgroundFloat[2]); case 4: return new DeviceCmyk(backgroundFloat[0], backgroundFloat[1], backgroundFloat[2], backgroundFloat[3]); } } return null; } }
/** * {@inheritDoc} */ public void invoke(PdfCanvasProcessor processor, PdfLiteral operator, List<PdfObject> operands) { PdfName dictionaryName = (PdfName) operands.get(0); PdfDictionary extGState = processor.getResources().getResource(PdfName.ExtGState); if (extGState == null) throw new PdfException(PdfException.ResourcesDoNotContainExtgstateEntryUnableToProcessOperator1).setMessageParams(operator); PdfDictionary gsDic = extGState.getAsDictionary(dictionaryName); if (gsDic == null) { gsDic = extGState.getAsStream(dictionaryName); if (gsDic == null) throw new PdfException(PdfException._1IsAnUnknownGraphicsStateDictionary).setMessageParams(dictionaryName); } // at this point, all we care about is the FONT entry in the GS dictionary TODO merge the whole gs dictionary PdfArray fontParameter = gsDic.getAsArray(PdfName.Font); if (fontParameter != null) { PdfFont font = processor.getFont(fontParameter.getAsDictionary(0)); float size = fontParameter.getAsNumber(1).floatValue(); processor.getGraphicsState().setFont(font); processor.getGraphicsState().setFontSize(size); } } }