📄 pdfcontentbyte.java
字号:
public void beginText()
{
state.xTLM = 0;
state.yTLM = 0;
content.append("BT\n");
}
/**
* Ends the writing of text and makes the current font invalid.
*/
public void endText()
{
content.append("ET\n");
}
/**
* Saves the graphic state. <CODE>saveState</CODE> and
* <CODE>restoreState</CODE> must be balanced.
*/
public void saveState()
{
content.append("q\n");
stateList.add(state);
}
/**
* Restores the graphic state. <CODE>saveState</CODE> and
* <CODE>restoreState</CODE> must be balanced.
*/
public void restoreState()
{
content.append("Q\n");
int idx = stateList.size() - 1;
if (idx < 0)
throw new RuntimeException("Unbalanced save/restore state operators.");
state = (GraphicState)stateList.get(idx);
stateList.remove(idx);
}
/**
* Sets the character spacing parameter.
*
* @param charSpace a parameter
*/
public void setCharacterSpacing(float charSpace)
{
content.append(charSpace).append(" Tc\n");
}
/**
* Sets the word spacing parameter.
*
* @param wordSpace a parameter
*/
public void setWordSpacing(float wordSpace)
{
content.append(wordSpace).append(" Tw\n");
}
/**
* Sets the horizontal scaling parameter.
*
* @param scale a parameter
*/
public void setHorizontalScaling(float scale)
{
content.append(scale).append(" Tz\n");
}
/**
* Sets the text leading parameter.
* <P>
* The leading parameter is measured in text space units. It specifies the vertical distance
* between the baselines of adjacent lines of text.</P>
*
* @param leading the new leading
*/
public void setLeading(float leading)
{
state.leading = leading;
content.append(leading).append(" TL\n");
}
/**
* Set the font and the size for the subsequent text writing.
*
* @param bf the font
* @param size the font size in points
*/
public void setFontAndSize(BaseFont bf, float size)
{
checkWriter();
state.size = size;
state.fontDetails = writer.add(bf);
content.append(state.fontDetails.getFontName().toPdf(null)).append(' ').append(size).append(" Tf\n");
}
/**
* Sets the text rendering parameter.
*
* @param rendering a parameter
*/
public void setTextRenderingMode(int rendering)
{
content.append(rendering).append(" Tr\n");
}
/**
* Sets the text rise parameter.
* <P>
* This allows to write text in subscript or superscript mode.</P>
*
* @param rise a parameter
*/
public void setTextRise(float rise)
{
content.append(rise).append(" Ts\n");
}
/**
* A helper to insert into the content stream the <CODE>text</CODE>
* converted to bytes according to the font's encoding.
*
* @param text the text to write
*/
private void showText2(String text)
{
if (state.fontDetails == null)
throw new NullPointerException("Font and size must be set before writing any text");
byte b[] = state.fontDetails.convertToBytes(text);
content.append(escapeString(b));
}
/**
* Shows the <CODE>text</CODE>.
*
* @param text the text to write
*/
public void showText(String text)
{
showText2(text);
content.append("Tj\n");
}
/**
* Shows the <CODE>text</CODE>.
*
* @param text the text to write
*/
public void showText(PdfPrintable text)
{
showText2(text.toString());
content.append("Tj\n");
}
/**
* Moves to the next line and shows <CODE>text</CODE>.
*
* @param text the text to write
*/
public void newlineShowText(String text)
{
state.yTLM -= state.leading;
showText2(text);
content.append("'\n");
}
/**
* Moves to the next line and shows <CODE>text</CODE>.
*
* @param text the text to write
*/
public void newlineShowText(PdfPrintable text)
{
state.yTLM -= state.leading;
showText2(text.toString());
content.append("'\n");
}
/**
* 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("\"\n");
}
/**
* 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\n");
}
/**
* 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\n");
}
/**
* 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\n");
}
/**
* Moves to the start of the next line.
*/
public void newlineText()
{
state.yTLM -= state.leading;
content.append("T*\n");
}
/**
* 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();
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 '(':
case ')':
case '\\':
content.append_i('\\').append_i(c);
break;
default:
content.append_i(c);
}
}
content.append(")");
return content.toByteArray();
}
/**
* Adds an outline to the document.
*
* @param outline the outline
*/
public void addOutline(PdfOutline outline)
{
checkWriter();
pdf.addOutline(outline);
}
/**
* 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);
}
}
/**
* 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\n");
}
/**
* 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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -