📄 pdfcontentbyte.java
字号:
state.yTLM += y; content.append(x).append(' ').append(y).append(" Td").append_i(separator); } /** * Moves to the start of the next line, offset from the start of the current line. * <P> * As a side effect, this sets the leading parameter in the text state.</P> * * @param x offset of the new current point * @param y y-coordinate of the new current point */ public void moveTextWithLeading(float x, float y) { state.xTLM += x; state.yTLM += y; state.leading = -y; content.append(x).append(' ').append(y).append(" TD").append_i(separator); } /** * Moves to the start of the next line. */ public void newlineText() { state.yTLM -= state.leading; content.append("T*").append_i(separator); } /** * Gets the size of this content. * * @return the size of the content */ int size() { return content.size(); } /** * Escapes a <CODE>byte</CODE> array according to the PDF conventions. * * @param b the <CODE>byte</CODE> array to escape * @return an escaped <CODE>byte</CODE> array */ static byte[] escapeString(byte b[]) { ByteBuffer content = new ByteBuffer(); escapeString(b, content); return content.toByteArray(); } /** * Escapes a <CODE>byte</CODE> array according to the PDF conventions. * * @param b the <CODE>byte</CODE> array to escape */ static void escapeString(byte b[], ByteBuffer content) { content.append_i('('); for (int k = 0; k < b.length; ++k) { byte c = b[k]; switch ((int)c) { case '\r': content.append("\\r"); break; case '\n': content.append("\\n"); break; case '\t': content.append("\\t"); break; case '\b': content.append("\\b"); break; case '\f': content.append("\\f"); break; case '(': case ')': case '\\': content.append_i('\\').append_i(c); break; default: content.append_i(c); } } content.append(")"); } /** * Adds an outline to the document. * * @param outline the outline * @deprecated not needed anymore. The outlines are extracted * from the root outline */ public void addOutline(PdfOutline outline) { // for compatibility } /** * Adds a named outline to the document. * * @param outline the outline * @param name the name for the local destination */ public void addOutline(PdfOutline outline, String name) { checkWriter(); pdf.addOutline(outline, name); } /** * Gets the root outline. * * @return the root outline */ public PdfOutline getRootOutline() { checkWriter(); return pdf.getRootOutline(); } /** * Shows text right, left or center aligned with rotation. * @param alignement the alignment can be ALIGN_CENTER, ALIGN_RIGHT or ALIGN_LEFT * @param text the text to show * @param x the x pivot position * @param y the y pivot position * @param rotation the rotation to be applied in degrees counterclockwise */ public void showTextAligned(int alignement, String text, float x, float y, float rotation) { if (state.fontDetails == null) throw new NullPointerException("Font and size must be set before writing any text"); BaseFont bf = state.fontDetails.getBaseFont(); if (rotation == 0) { switch (alignement) { case ALIGN_CENTER: x -= bf.getWidthPoint(text, state.size) / 2; break; case ALIGN_RIGHT: x -= bf.getWidthPoint(text, state.size); break; } setTextMatrix(x, y); showText(text); } else { double alpha = rotation * Math.PI / 180.0; float cos = (float)Math.cos(alpha); float sin = (float)Math.sin(alpha); float len; switch (alignement) { case ALIGN_CENTER: len = bf.getWidthPoint(text, state.size) / 2; x -= len * cos; y -= len * sin; break; case ALIGN_RIGHT: len = bf.getWidthPoint(text, state.size); x -= len * cos; y -= len * sin; break; } setTextMatrix(cos, sin, -sin, cos, x, y); showText(text); setTextMatrix(0f, 0f); } } /** * Shows text kerned right, left or center aligned with rotation. * @param alignement the alignment can be ALIGN_CENTER, ALIGN_RIGHT or ALIGN_LEFT * @param text the text to show * @param x the x pivot position * @param y the y pivot position * @param rotation the rotation to be applied in degrees counterclockwise */ public void showTextAlignedKerned(int alignement, String text, float x, float y, float rotation) { if (state.fontDetails == null) throw new NullPointerException("Font and size must be set before writing any text"); BaseFont bf = state.fontDetails.getBaseFont(); if (rotation == 0) { switch (alignement) { case ALIGN_CENTER: x -= bf.getWidthPointKerned(text, state.size) / 2; break; case ALIGN_RIGHT: x -= bf.getWidthPointKerned(text, state.size); break; } setTextMatrix(x, y); showTextKerned(text); } else { double alpha = rotation * Math.PI / 180.0; float cos = (float)Math.cos(alpha); float sin = (float)Math.sin(alpha); float len; switch (alignement) { case ALIGN_CENTER: len = bf.getWidthPointKerned(text, state.size) / 2; x -= len * cos; y -= len * sin; break; case ALIGN_RIGHT: len = bf.getWidthPointKerned(text, state.size); x -= len * cos; y -= len * sin; break; } setTextMatrix(cos, sin, -sin, cos, x, y); showTextKerned(text); setTextMatrix(0f, 0f); } } /** * Concatenate a matrix to the current transformation matrix. * @param a an element of the transformation matrix * @param b an element of the transformation matrix * @param c an element of the transformation matrix * @param d an element of the transformation matrix * @param e an element of the transformation matrix * @param f an element of the transformation matrix **/ public void concatCTM(float a, float b, float c, float d, float e, float f) { content.append(a).append(' ').append(b).append(' ').append(c).append(' '); content.append(d).append(' ').append(e).append(' ').append(f).append(" cm").append_i(separator); } /** * Generates an array of bezier curves to draw an arc. * <P> * (x1, y1) and (x2, y2) are the corners of the enclosing rectangle. * Angles, measured in degrees, start with 0 to the right (the positive X * axis) and increase counter-clockwise. The arc extends from startAng * to startAng+extent. I.e. startAng=0 and extent=180 yields an openside-down * semi-circle. * <P> * The resulting coordinates are of the form float[]{x1,y1,x2,y2,x3,y3, x4,y4} * such that the curve goes from (x1, y1) to (x4, y4) with (x2, y2) and * (x3, y3) as their respective Bezier control points. * <P> * Note: this code was taken from ReportLab (www.reportlab.com), an excelent * PDF generator for Python. * * @param x1 a corner of the enclosing rectangle * @param y1 a corner of the enclosing rectangle * @param x2 a corner of the enclosing rectangle * @param y2 a corner of the enclosing rectangle * @param startAng starting angle in degrees * @param extent angle extent in degrees * @return a list of float[] with the bezier curves */ public static ArrayList bezierArc(float x1, float y1, float x2, float y2, float startAng, float extent) { float tmp; if (x1 > x2) { tmp = x1; x1 = x2; x2 = tmp; } if (y2 > y1) { tmp = y1; y1 = y2; y2 = tmp; } float fragAngle; int Nfrag; if (Math.abs(extent) <= 90f) { fragAngle = extent; Nfrag = 1; } else { Nfrag = (int)(Math.ceil(Math.abs(extent)/90f)); fragAngle = extent / Nfrag; } float x_cen = (x1+x2)/2f; float y_cen = (y1+y2)/2f; float rx = (x2-x1)/2f; float ry = (y2-y1)/2f; float halfAng = (float)(fragAngle * Math.PI / 360.); float kappa = (float)(Math.abs(4. / 3. * (1. - Math.cos(halfAng)) / Math.sin(halfAng))); ArrayList pointList = new ArrayList(); for (int i = 0; i < Nfrag; ++i) { float theta0 = (float)((startAng + i*fragAngle) * Math.PI / 180.); float theta1 = (float)((startAng + (i+1)*fragAngle) * Math.PI / 180.); float cos0 = (float)Math.cos(theta0); float cos1 = (float)Math.cos(theta1); float sin0 = (float)Math.sin(theta0); float sin1 = (float)Math.sin(theta1); if (fragAngle > 0f) { pointList.add(new float[]{x_cen + rx * cos0, y_cen - ry * sin0, x_cen + rx * (cos0 - kappa * sin0), y_cen - ry * (sin0 + kappa * cos0), x_cen + rx * (cos1 + kappa * sin1), y_cen - ry * (sin1 - kappa * cos1), x_cen + rx * cos1, y_cen - ry * sin1}); } else { pointList.add(new float[]{x_cen + rx * cos0, y_cen - ry * sin0, x_cen + rx * (cos0 + kappa * sin0), y_cen - ry * (sin0 - kappa * cos0), x_cen + rx * (cos1 - kappa * sin1), y_cen - ry * (sin1 + kappa * cos1), x_cen + rx * cos1, y_cen - ry * sin1}); } } return pointList; } /** * Draws a partial ellipse inscribed within the rectangle x1,y1,x2,y2, * starting at startAng degrees and covering extent degrees. Angles * start with 0 to the right (+x) and increase counter-clockwise. * * @param x1 a corner of the enclosing rectangle * @param y1 a corner of the enclosing rectangle * @param x2 a corner of the enclosing rectangle * @param y2 a corner of the enclosing rectangle * @param startAng starting angle in degrees * @param extent angle extent in degrees */ public void arc(float x1, float y1, float x2, float y2, float startAng, float extent) { ArrayList ar = bezierArc(x1, y1, x2, y2, startAng, extent); if (ar.size() == 0) return; float pt[] = (float [])ar.get(0); moveTo(pt[0], pt[1]); for (int k = 0; k < ar.size(); ++k) { pt = (float [])ar.get(k); curveTo(pt[2], pt[3], pt[4], pt[5], pt[6], pt[7]); } } /** * Draws an ellipse inscribed within the rectangle x1,y1,x2,y2. * * @param x1 a corner of the enclosing rectangle * @param y1 a corner of the enclosing rectangle * @param x2 a corner of the enclosing rectangle * @param y2 a corner of the enclosing rectangle */ public void ellipse(float x1, float y1, float x2, float y2) { arc(x1, y1, x2, y2, 0f, 360f); } /** * Create a new colored tiling pattern. * * @param width the width of the pattern * @param height the height of the pattern * @param xstep the desired horizontal spacing between pattern cells. * May be either positive or negative, but not zero. * @param ystep the desired vertical spacing between pattern cells. * May be either positive or negative, but not zero. * @return the <CODE>PdfPatternPainter</CODE> where the pattern will be created */ public PdfPatternPainter createPattern(float width, float height, float xstep, float ystep) { checkWriter(); if ( xstep == 0.0f || ystep == 0.0f ) throw new RuntimeException("XStep or YStep can not be ZERO."); PdfPatternPainter painter = new PdfPatternPainter(writer); painter.setWidth(width); painter.setHeight(height); painter.setXStep(xstep); painter.setYStep(ystep); writer.addSimplePattern(painter); return painter; } /** * Create a new colored tiling pattern. Variables xstep and ystep are set to the same values * of width and height.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -