📄 pdfcontentbyte.java
字号:
/* * $Id: PdfContentByte.java,v 1.50 2002/11/19 08:33:36 blowagie Exp $ * $Name: $ * * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie * * The contents of this file are subject to the Mozilla Public License Version 1.1 * (the "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the License. * * The Original Code is 'iText, a free JAVA-PDF library'. * * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. * All Rights Reserved. * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. * * Contributor(s): all the names of the contributors are added in the source code * where applicable. * * Alternatively, the contents of this file may be used under the terms of the * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the * provisions of LGPL are applicable instead of those above. If you wish to * allow use of your version of this file only under the terms of the LGPL * License and not to allow others to use your version of this file under * the MPL, indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by the LGPL. * If you do not delete the provisions above, a recipient may use your version * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. * * This library is free software; you can redistribute it and/or modify it * under the terms of the MPL as stated above or under the terms of the GNU * Library General Public License as published by the Free Software Foundation; * either version 2 of the License, or any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more * details. * * If you didn't download this code from the following link, you should check if * you aren't using an obsolete version: * http://www.lowagie.com/iText/ */package com.lowagie.text.pdf;import java.awt.Color;import com.lowagie.text.Image;import com.lowagie.text.DocumentException;import com.lowagie.text.Rectangle;import com.lowagie.text.Image;import java.util.ArrayList;/** * <CODE>PdfContentByte</CODE> is an object containing the user positioned * text and graphic contents of a page. It knows how to apply the proper * font encoding. */public class PdfContentByte { /** * This class keeps the graphic state of the current page */ class GraphicState { /** This is the font in use */ FontDetails fontDetails; /** This is the color in use */ ColorDetails colorDetails; /** This is the font size in use */ float size; /** The x position of the text line matrix. */ protected float xTLM = 0; /** The y position of the text line matrix. */ protected float yTLM = 0; /** The current text leading. */ protected float leading = 0; } /** The alignement is center */ public static final int ALIGN_CENTER = 0; /** The alignement is left */ public static final int ALIGN_LEFT = 1; /** The alignement is right */ public static final int ALIGN_RIGHT = 2; // membervariables /** This is the actual content */ protected ByteBuffer content = new ByteBuffer(); /** This is the writer */ protected PdfWriter writer; /** This is the PdfDocument */ protected PdfDocument pdf; /** This is the GraphicState in use */ protected GraphicState state = new GraphicState(); /** The list were we save/restore the state */ protected ArrayList stateList = new ArrayList(); /** The separator between commands. */ protected int separator = '\n'; // constructors /** * Constructs a new <CODE>PdfContentByte</CODE>-object. * * @param wr the writer associated to this content */ public PdfContentByte(PdfWriter wr) { if (wr != null) { writer = wr; pdf = writer.getPdfDocument(); } } // methods to get the content of this object /** * Returns the <CODE>String</CODE> representation of this <CODE>PdfContentByte</CODE>-object. * * @return a <CODE>String</CODE> */ public String toString() { return content.toString(); } /** * Gets the internal buffer. * @return the internal buffer */ ByteBuffer getInternalBuffer() { return content; } /** Returns the PDF representation of this <CODE>PdfContentByte</CODE>-object. * * @param writer the <CODE>PdfWriter</CODE> * @return a <CODE>byte</CODE> array with the representation */ public byte[] toPdf(PdfWriter writer) { return content.toByteArray(); } // methods to add graphical content /** * Adds the content of another <CODE>PdfContent</CODE>-object to this object. * * @param other another <CODE>PdfByteContent</CODE>-object */ public void add(PdfContentByte other) { if (other.writer != null && writer != other.writer) throw new RuntimeException("Inconsistent writers. Are you mixing two documents?"); content.append(other.content); } /** * Gets the x position of the text line matrix. * * @return the x position of the text line matrix */ public float getXTLM() { return state.xTLM; } /** * Gets the y position of the text line matrix. * * @return the y position of the text line matrix */ public float getYTLM() { return state.yTLM; } /** * Gets the current text leading. * * @return the current text leading */ public float getLeading() { return state.leading; } /** * Changes the <VAR>Flatness</VAR>. * <P> * <VAR>Flatness</VAR> sets the maximum permitted distance in device pixels between the * mathematically correct path and an approximation constructed from straight line segments.<BR> * * @param flatness a value */ public void setFlatness(float flatness) { if (flatness >= 0 && flatness <= 100) { content.append(flatness).append(" i").append_i(separator); } } /** * Changes the <VAR>Line cap style</VAR>. * <P> * The <VAR>line cap style</VAR> specifies the shape to be used at the end of open subpaths * when they are stroked.<BR> * Allowed values are 0 (Butt end caps), 1 (Round end caps) and 2 (Projecting square end caps).<BR> * * @param style a value */ public void setLineCap(int style) { if (style >= 0 && style <= 2) { content.append(style).append(" J").append_i(separator); } } /** * Changes the value of the <VAR>line dash pattern</VAR>. * <P> * The line dash pattern controls the pattern of dashes and gaps used to stroke paths. * It is specified by an <I>array</I> and a <I>phase</I>. The array specifies the length * of the alternating dashes and gaps. The phase specifies the distance into the dash * pattern to start the dash.<BR> * * @param phase the value of the phase */ public void setLineDash(float phase) { content.append("[] ").append(phase).append(" d").append_i(separator); } /** * Changes the value of the <VAR>line dash pattern</VAR>. * <P> * The line dash pattern controls the pattern of dashes and gaps used to stroke paths. * It is specified by an <I>array</I> and a <I>phase</I>. The array specifies the length * of the alternating dashes and gaps. The phase specifies the distance into the dash * pattern to start the dash.<BR> * * @param phase the value of the phase * @param unitsOn the number of units that must be 'on' (equals the number of units that must be 'off'). */ public void setLineDash(float unitsOn, float phase) { content.append("[").append(unitsOn).append("] ").append(phase).append(" d").append_i(separator); } /** * Changes the value of the <VAR>line dash pattern</VAR>. * <P> * The line dash pattern controls the pattern of dashes and gaps used to stroke paths. * It is specified by an <I>array</I> and a <I>phase</I>. The array specifies the length * of the alternating dashes and gaps. The phase specifies the distance into the dash * pattern to start the dash.<BR> * * @param phase the value of the phase * @param unitsOn the number of units that must be 'on' * @param unitsOff the number of units that must be 'off' */ public void setLineDash(float unitsOn, float unitsOff, float phase) { content.append("[").append(unitsOn).append(' ').append(unitsOff).append("] ").append(phase).append(" d").append_i(separator); } /** * Changes the <VAR>Line join style</VAR>. * <P> * The <VAR>line join style</VAR> specifies the shape to be used at the corners of paths * that are stroked.<BR> * Allowed values are 0 (Miter joins), 1 (Round joins) and 2 (Bevel joins).<BR> * * @param style a value */ public void setLineJoin(int style) { if (style >= 0 && style <= 2) { content.append(style).append(" j").append_i(separator); } } /** * Changes the <VAR>line width</VAR>. * <P> * The line width specifies the thickness of the line used to stroke a path and is measured * in used space units.<BR> * * @param w a width */ public void setLineWidth(float w) { content.append(w).append(" w").append_i(separator); } /** * Changes the <VAR>Miter limit</VAR>. * <P> * When two line segments meet at a sharp angle and mitered joins have been specified as the * line join style, it is possible for the miter to extend far beyond the thickness of the line * stroking path. The miter limit imposes a maximum on the ratio of the miter length to the line * witdh. When the limit is exceeded, the join is converted from a miter to a bevel.<BR> * * @param miterLimit a miter limit */ public void setMiterLimit(float miterLimit) { if (miterLimit > 1) { content.append(miterLimit).append(" M").append_i(separator); } } /** * Modify the current clipping path by intersecting it with the current path, using the * nonzero winding number rule to determine which regions lie inside the clipping * path. */ public void clip() { content.append("W").append_i(separator); } /** * Modify the current clipping path by intersecting it with the current path, using the * even-odd rule to determine which regions lie inside the clipping path. */ public void eoClip() { content.append("W*").append_i(separator); } /** * Changes the currentgray tint for filling paths (device dependent colors!). * <P> * Sets the color space to <B>DeviceGray</B> (or the <B>DefaultGray</B> color space), * and sets the gray tint to use for filling paths.</P> * * @param gray a value between 0 (black) and 1 (white) */ public void setGrayFill(float gray) { content.append(gray).append(" g").append_i(separator); } /** * Changes the current gray tint for filling paths to black. */ public void resetGrayFill() { content.append("0 g").append_i(separator); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -