📄 pdfcontentbyte.java
字号:
content.append("'").append_i(separator);
}
/**
* Moves to the next line and shows text string, using the given values of the character and word spacing parameters.
*
* @param wordSpacing a parameter
* @param charSpacing a parameter
* @param text the text to write
*/
public void newlineShowText(float wordSpacing, float charSpacing, String text) {
state.yTLM -= state.leading;
content.append(wordSpacing).append(' ').append(charSpacing);
showText2(text);
content.append("\"").append_i(separator);
// The " operator sets charSpace and wordSpace into graphics state
// (cfr PDF reference v1.6, table 5.6)
state.charSpace = charSpacing;
state.wordSpace = wordSpacing;
}
/**
* Changes the text matrix.
* <P>
* Remark: this operation also initializes the current point position.</P>
*
* @param a operand 1,1 in the matrix
* @param b operand 1,2 in the matrix
* @param c operand 2,1 in the matrix
* @param d operand 2,2 in the matrix
* @param x operand 3,1 in the matrix
* @param y operand 3,2 in the matrix
*/
public void setTextMatrix(float a, float b, float c, float d, float x, float y) {
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 ((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();
}
/**
* 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 excelent
* 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_ce
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -