pdfcontentbyte.java
来自「处理PDF」· Java 代码 · 共 1,856 行 · 第 1/5 页
JAVA
1,856 行
state.xTLM = x; state.yTLM = y; content.append(a).append(' ').append(b).append_i(' ') .append(c).append_i(' ').append(d).append_i(' ') .append(x).append_i(' ').append(y).append(" Tm").append_i(separator); } /** * Changes the text matrix. The first four parameters are {1,0,0,1}. * <P> * Remark: this operation also initializes the current point position.</P> * * @param x operand 3,1 in the matrix * @param y operand 3,2 in the matrix */ public void setTextMatrix(float x, float y) { setTextMatrix(1, 0, 0, 1, x, y); } /** * Moves to the start of the next line, offset from the start of the current line. * * @param x x-coordinate of the new current point * @param y y-coordinate of the new current point */ public void moveText(float x, float y) { state.xTLM += x; 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 * @param content the content */ static void escapeString(byte b[], ByteBuffer content) { content.append_i('('); for (int k = 0; k < b.length; ++k) { byte c = b[k]; switch (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 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(); } /** * Computes the width of the given string taking in account * the current values of "Character spacing", "Word Spacing" * and "Horizontal Scaling". * The additional spacing is not computed for the last character * of the string. * @param text the string to get width of * @param kerned the kerning option * @return the width */ public float getEffectiveStringWidth(String text, boolean kerned) { BaseFont bf = state.fontDetails.getBaseFont(); float w; if (kerned) w = bf.getWidthPointKerned(text, state.size); else w = bf.getWidthPoint(text, state.size); if (state.charSpace != 0.0f && text.length() > 1) { w += state.charSpace * (text.length() -1); } int ft = bf.getFontType(); if (state.wordSpace != 0.0f && (ft == BaseFont.FONT_TYPE_T1 || ft == BaseFont.FONT_TYPE_TT || ft == BaseFont.FONT_TYPE_T3)) { for (int i = 0; i < (text.length() -1); i++) { if (text.charAt(i) == ' ') w += state.wordSpace; } } if (state.scale != 100.0) w = (w * state.scale) / 100.0f; //System.out.println("String width = " + Float.toString(w)); return w; } /** * Shows text right, left or center aligned with rotation. * @param alignment 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 alignment, String text, float x, float y, float rotation) { showTextAligned(alignment, text, x, y, rotation, false); } private void showTextAligned(int alignment, String text, float x, float y, float rotation, boolean kerned) { if (state.fontDetails == null) throw new NullPointerException("Font and size must be set before writing any text"); if (rotation == 0) { switch (alignment) { case ALIGN_CENTER: x -= getEffectiveStringWidth(text, kerned) / 2; break; case ALIGN_RIGHT: x -= getEffectiveStringWidth(text, kerned); break; } setTextMatrix(x, y); if (kerned) showTextKerned(text); else 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 (alignment) { case ALIGN_CENTER: len = getEffectiveStringWidth(text, kerned) / 2; x -= len * cos; y -= len * sin; break; case ALIGN_RIGHT: len = getEffectiveStringWidth(text, kerned); x -= len * cos; y -= len * sin; break; } setTextMatrix(cos, sin, -sin, cos, x, y); if (kerned) showTextKerned(text); else showText(text); setTextMatrix(0f, 0f); } } /** * Shows text kerned right, left or center aligned with rotation. * @param alignment 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 alignment, String text, float x, float y, float rotation) { showTextAligned(alignment, text, x, y, rotation, true); } /** * 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.org), an excellent * PDF generator for Python (BSD license: http://www.reportlab.org/devfaq.html#1.3 ). * * @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.isEmpty()) 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 *
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?