📄 pdfcontentbyte.java
字号:
* @param red the intensity of red. A value between 0 and 1
* @param green the intensity of green. A value between 0 and 1
* @param blue the intensity of blue. A value between 0 and 1
*/
public final void setRGBColorStrokeF(float red, float green, float blue) {
HelperRGB(red, green, blue);
content.append(" RG\n");
}
/**
* Changes the current color for stroking paths to black.
*
*/
public final void resetRGBColorStroke() {
content.append("0 0 0 RG\n");
}
/**
* Helper to validate and write the CMYK color components
* @param cyan the intensity of cyan. A value between 0 and 1
* @param magenta the intensity of magenta. A value between 0 and 1
* @param yellow the intensity of yellow. A value between 0 and 1
* @param black the intensity of black. A value between 0 and 1
*/
private void HelperCMYK(float cyan, float magenta, float yellow, float black) {
if (cyan < 0)
cyan = 0.0f;
else if (cyan > 1.0f)
cyan = 1.0f;
if (magenta < 0)
magenta = 0.0f;
else if (magenta > 1.0f)
magenta = 1.0f;
if (yellow < 0)
yellow = 0.0f;
else if (yellow > 1.0f)
yellow = 1.0f;
if (black < 0)
black = 0.0f;
else if (black > 1.0f)
black = 1.0f;
content.append(cyan).append(' ').append(magenta).append(' ').append(yellow).append(' ').append(black);
}
/**
* Changes the current color for filling paths (device dependent colors!).
* <P>
* Sets the color space to <B>DeviceCMYK</B> (or the <B>DefaultCMYK</B> color space),
* and sets the color to use for filling paths.</P>
* <P>
* Following the PDF manual, each operand must be a number between 0 (no ink) and
* 1 (maximum ink).</P>
*
* @param cyan the intensity of cyan. A value between 0 and 1
* @param magenta the intensity of magenta. A value between 0 and 1
* @param yellow the intensity of yellow. A value between 0 and 1
* @param black the intensity of black. A value between 0 and 1
*/
public final void setCMYKColorFillF(float cyan, float magenta, float yellow, float black) {
HelperCMYK(cyan, magenta, yellow, black);
content.append(" k\n");
}
/**
* Changes the current color for filling paths to black.
*
*/
public final void resetCMYKColorFill() {
content.append("0 0 0 1 k\n");
}
/**
* Changes the current color for stroking paths (device dependent colors!).
* <P>
* Sets the color space to <B>DeviceCMYK</B> (or the <B>DefaultCMYK</B> color space),
* and sets the color to use for stroking paths.</P>
* <P>
* Following the PDF manual, each operand must be a number between 0 (miniumum intensity) and
* 1 (maximum intensity).
*
* @param cyan the intensity of cyan. A value between 0 and 1
* @param magenta the intensity of magenta. A value between 0 and 1
* @param yellow the intensity of yellow. A value between 0 and 1
* @param black the intensity of black. A value between 0 and 1
*/
public final void setCMYKColorStrokeF(float cyan, float magenta, float yellow, float black) {
HelperCMYK(cyan, magenta, yellow, black);
content.append(" K\n");
}
/**
* Changes the current color for stroking paths to black.
*
*/
public final void resetCMYKColorStroke() {
content.append("0 0 0 1 K\n");
}
/**
* Move the current point <I>(x, y)</I>, omitting any connecting line segment.
*
* @param x new x-coordinate
* @param y new y-coordinate
*/
public final void moveTo(float x, float y) {
content.append(x).append(' ').append(y).append(" m\n");
}
/**
* Appends a straight line segment from the current point <I>(x, y)</I>. The new current
* point is <I>(x, y)</I>.
*
* @param x new x-coordinate
* @param y new y-coordinate
*/
public final void lineTo(float x, float y) {
content.append(x).append(' ').append(y).append(" l\n");
}
/**
* Appends a B陑ier curve to the path, starting from the current point.
*
* @param x1 x-coordinate of the first control point
* @param y1 y-coordinate of the first control point
* @param x2 x-coordinate of the second control point
* @param y2 y-coordinate of the second control point
* @param x3 x-coordinaat of the ending point (= new current point)
* @param y3 y-coordinaat of the ending point (= new current point)
*/
public final void curveTo(float x1, float y1, float x2, float y2, float x3, float y3) {
content.append(x1).append(' ').append(y1).append(' ').append(x2).append(' ').append(y2).append(' ').append(x3).append(' ').append(y3).append(" c\n");
}
/**
* Appends a B陑ier curve to the path, starting from the current point.
*
* @param x2 x-coordinate of the second control point
* @param y2 y-coordinate of the second control point
* @param x3 x-coordinaat of the ending point (= new current point)
* @param y3 y-coordinaat of the ending point (= new current point)
*/
public final void curveTo(float x2, float y2, float x3, float y3) {
content.append(x2).append(' ').append(y2).append(' ').append(x3).append(' ').append(y3).append(" v\n");
}
/**
* Appends a B陑ier curve to the path, starting from the current point.
*
* @param x1 x-coordinate of the first control point
* @param y1 y-coordinate of the first control point
* @param x3 x-coordinaat of the ending point (= new current point)
* @param y3 y-coordinaat of the ending point (= new current point)
*/
public final void curveFromTo(float x1, float y1, float x3, float y3) {
content.append(x1).append(' ').append(y1).append(' ').append(x3).append(' ').append(y3).append(" y\n");
}
/** Draws a circle. The endpoint will (x+r, y).
*
* @param x x center of circle
* @param y y center of circle
* @param r radius of circle
*/
public final void circle(float x, float y, float r)
{
float b = 0.5523f;
moveTo(x + r, y);
curveTo(x + r, y + r * b, x + r * b, y + r, x, y + r);
curveTo(x - r * b, y + r, x - r, y + r * b, x - r, y);
curveTo(x - r, y - r * b, x - r * b, y - r, x, y - r);
curveTo(x + r * b, y - r, x + r, y - r * b, x + r, y);
}
/**
* Adds a rectangle to the current path.
*
* @param x x-coordinate of the starting point
* @param y y-coordinate of the starting point
* @param w width
* @param h height
*/
public final void rectangle(float x, float y, float w, float h) {
content.append(x).append(' ').append(y).append(' ').append(w).append(' ').append(h).append(" re\n");
}
/**
* Adds a border (complete or partially) to the current path..
*
* @param rectangle a <CODE>Rectangle</CODE>
*/
public final void rectangle(Rectangle rectangle) {
// the coordinates of the border are retrieved
float x1 = rectangle.left();
float y1 = rectangle.top();
float x2 = rectangle.right();
float y2 = rectangle.bottom();
// the backgroundcolor is set
Color background = rectangle.backgroundColor();
if (background != null) {
setRGBColorStroke(background.getRed(), background.getGreen(), background.getBlue());
setRGBColorFill(background.getRed(), background.getGreen(), background.getBlue());
rectangle(x1, y1, x2 - x1, y2 - y1);
closePathFillStroke();
resetRGBColorFill();
resetRGBColorStroke();
}
else if (rectangle.grayFill() > 0.0) {
setGrayStroke((float)rectangle.grayFill());
setGrayFill((float)rectangle.grayFill());
rectangle(x1, y1, x2 - x1, y2 - y1);
closePathFillStroke();
resetGrayFill();
resetGrayStroke();
}
// if the element hasn't got any borders, nothing is added
if (! rectangle.hasBorders()) {
return;
}
// the width is set to the width of the element
if (rectangle.borderWidth() != Rectangle.UNDEFINED) {
setLineWidth((float)rectangle.borderWidth());
}
// the color is set to the color of the element
Color color = rectangle.borderColor();
if (color != null) {
setRGBColorStroke(color.getRed(), color.getGreen(), color.getBlue());
}
// if the box is a rectangle, it is added as a rectangle
if (rectangle.hasBorder(Rectangle.BOX)) {
rectangle(x1, y1, x2 - x1, y2 - y1);
}
// if the border isn't a rectangle, the different sides are added apart
else {
if (rectangle.hasBorder(Rectangle.RIGHT)) {
moveTo(x2, y1);
lineTo(x2, y2);
}
if (rectangle.hasBorder(Rectangle.LEFT)) {
moveTo(x1, y1);
lineTo(x1, y2);
}
if (rectangle.hasBorder(Rectangle.BOTTOM)) {
moveTo(x1, y2);
lineTo(x2, y2);
}
if (rectangle.hasBorder(Rectangle.TOP)) {
moveTo(x1, y1);
lineTo(x2, y1);
}
}
stroke();
if (color != null) {
resetRGBColorStroke();
}
}
/**
* Closes the current subpath by appending a straight line segment from the current point
* to the starting point of the subpath.
*/
public final void closePath() {
content.append("h\n");
}
/**
* Ends the path without filling or stroking it.
*/
public final void newPath() {
content.append("n\n");
}
/**
* Strokes the path.
*/
public final void stroke() {
content.append("S\n");
}
/**
* Closes the path and strokes it.
*/
public final void closePathStroke() {
content.append("s\n");
}
/**
* Fills the path, using the non-zero winding number rule to determine the region to fill.
*/
public final void fill() {
content.append("f\n");
}
/**
* Fills the path, using the even-odd rule to determine the region to fill.
*/
public final void eoFill() {
content.append("f*\n");
}
/**
* Fills the path using the non-zero winding number rule to determine the region to fill and strokes it.
*/
public final void fillStroke() {
content.append("B\n");
}
/**
* Closes the path, fills it using the non-zero winding number rule to determine the region to fill and strokes it.
*/
public final void closePathFillStroke() {
content.append("b\n");
}
/**
* Fills the path, using the even-odd rule to determine the region to fill and strokes it.
*/
public final void eoFillStroke() {
content.append("B*\n");
}
/**
* Closes the path, fills it using the even-odd rule to determine the region to fill and strokes it.
*/
public final void closePathEoFillStroke() {
content.append("b*\n");
}
/**
* Adds an <CODE>Image</CODE> to the page. The <CODE>Image</CODE> must have
* absolute positioning.
* @param image the <CODE>Image</CODE> object
* @throws DocumentException if the <CODE>Image</CODE> does not have absolute positioning
*/
final public void addImage(Image image) throws DocumentException
{
if (!image.hasAbsolutePosition())
throw new DocumentException("The image must have absolute positioning.");
float matrix[] = image.matrix();
matrix[Image.CX] = image.absoluteX() - matrix[Image.CX];
matrix[Image.CY] = image.absoluteY() - matrix[Image.CY];
addImage(image, matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
}
/**
* Adds an <CODE>Image</CODE> to the page. The positioning of the <CODE>Image</CODE>
* is done with the transformation matrix. To position an <CODE>image</CODE> at (x,y)
* use addImage(image, image_width, 0, 0, image_height, x, y).
* @param image the <CODE>Image</CODE> object
* @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
* @throws DocumentException on error
*/
public void addImage(Image image, float a, float b, float c, float d, float e, float f) throws DocumentException
{
checkWriter();
try {
if (image.isImgTemplate()) {
PdfTemplate template = image.templateData();
float w = template.getWidth();
float h = template.getHeight();
addTemplate(template, a / w, b / w, c / h, d / h, e, f);
}
else {
PdfName name = pdf.addDirectImage(image);
content.append("q ");
content.append(a).append(' ');
content.append(b).append(' ');
content.append(c).append(' ');
content.append(d).append(' ');
content.append(e).append(' ');
content.append(f).append(" cm ");
content.append(name.toString()).append(" Do Q\n");
}
}
catch (Exception ee) {
throw new DocumentException(ee.getMessage());
}
}
/**
* Makes this <CODE>PdfContentByte</CODE> empty.
*/
public void reset()
{
content.reset();
stateList.clear();
state = new GraphicState();
}
/**
* Starts the writing of text.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -