001/*
002 * $Id: VerticalPositionMark.java 4784 2011-03-15 08:33:00Z blowagie $
003 *
004 * This file is part of the iText (R) project.
005 * Copyright (c) 1998-2011 1T3XT BVBA
006 * Authors: Bruno Lowagie, Paulo Soares, et al.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Affero General Public License version 3
010 * as published by the Free Software Foundation with the addition of the
011 * following permission added to Section 15 as permitted in Section 7(a):
012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
014 *
015 * This program is distributed in the hope that it will be useful, but
016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
017 * or FITNESS FOR A PARTICULAR PURPOSE.
018 * See the GNU Affero General Public License for more details.
019 * You should have received a copy of the GNU Affero General Public License
020 * along with this program; if not, see http://www.gnu.org/licenses or write to
021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
022 * Boston, MA, 02110-1301 USA, or download the license from the following URL:
023 * http://itextpdf.com/terms-of-use/
024 *
025 * The interactive user interfaces in modified source and object code versions
026 * of this program must display Appropriate Legal Notices, as required under
027 * Section 5 of the GNU Affero General Public License.
028 *
029 * In accordance with Section 7(b) of the GNU Affero General Public License,
030 * a covered work must retain the producer line in every PDF that is created
031 * or manipulated using iText.
032 *
033 * You can be released from the requirements of the license by purchasing
034 * a commercial license. Buying such a license is mandatory as soon as you
035 * develop commercial activities involving the iText software without
036 * disclosing the source code of your own applications.
037 * These activities include: offering paid services to customers as an ASP,
038 * serving PDFs on the fly in a web application, shipping iText with a closed
039 * source product.
040 *
041 * For more information, please contact iText Software Corp. at this
042 * address: sales@itextpdf.com
043 */
044package com.itextpdf.text.pdf.draw;
045
046import java.util.List;
047import java.util.ArrayList;
048
049import com.itextpdf.text.Chunk;
050import com.itextpdf.text.DocumentException;
051import com.itextpdf.text.Element;
052import com.itextpdf.text.ElementListener;
053import com.itextpdf.text.pdf.PdfContentByte;
054
055/**
056 * Helper class implementing the DrawInterface. Can be used to add
057 * horizontal or vertical separators. Won't draw anything unless
058 * you implement the draw method.
059 * @since       2.1.2
060 */
061
062public class VerticalPositionMark implements DrawInterface, Element {
063
064    /** Another implementation of the DrawInterface; its draw method will overrule LineSeparator.draw(). */
065    protected DrawInterface drawInterface = null;
066
067    /** The offset for the line. */
068    protected float offset = 0;
069
070        /**
071         * Creates a vertical position mark that won't draw anything unless
072         * you define a DrawInterface.
073         */
074        public VerticalPositionMark() {
075        }
076
077        /**
078         * Creates a vertical position mark that won't draw anything unless
079         * you define a DrawInterface.
080         * @param       drawInterface   the drawInterface for this vertical position mark.
081         * @param       offset                  the offset for this vertical position mark.
082         */
083        public VerticalPositionMark(final DrawInterface drawInterface, final float offset) {
084                this.drawInterface = drawInterface;
085                this.offset = offset;
086        }
087
088        /**
089         * @see com.itextpdf.text.pdf.draw.DrawInterface#draw(com.itextpdf.text.pdf.PdfContentByte, float, float, float, float, float)
090         */
091        public void draw(final PdfContentByte canvas, final float llx, final float lly, final float urx, final float ury, final float y) {
092                if (drawInterface != null) {
093                        drawInterface.draw(canvas, llx, lly, urx, ury, y + offset);
094                }
095        }
096
097    /**
098     * @see com.itextpdf.text.Element#process(com.itextpdf.text.ElementListener)
099     */
100    public boolean process(final ElementListener listener) {
101                try {
102                        return listener.add(this);
103                } catch (DocumentException e) {
104                        return false;
105                }
106    }
107
108    /**
109     * @see com.itextpdf.text.Element#type()
110     */
111    public int type() {
112        return Element.YMARK;
113    }
114
115    /**
116     * @see com.itextpdf.text.Element#isContent()
117     */
118    public boolean isContent() {
119        return true;
120    }
121
122    /**
123     * @see com.itextpdf.text.Element#isNestable()
124     */
125    public boolean isNestable() {
126        return false;
127    }
128
129    /**
130     * @see com.itextpdf.text.Element#getChunks()
131     */
132    public List<Chunk> getChunks() {
133        List<Chunk> list = new ArrayList<Chunk>();
134        list.add(new Chunk(this, true));
135        return list;
136    }
137
138    /**
139     * Getter for the interface with the overruling draw() method.
140     * @return  a DrawInterface implementation
141     */
142    public DrawInterface getDrawInterface() {
143        return drawInterface;
144    }
145
146    /**
147     * Setter for the interface with the overruling draw() method.
148     * @param drawInterface a DrawInterface implementation
149     */
150    public void setDrawInterface(final DrawInterface drawInterface) {
151        this.drawInterface = drawInterface;
152    }
153
154    /**
155     * Getter for the offset relative to the baseline of the current line.
156     * @return  an offset
157     */
158    public float getOffset() {
159        return offset;
160    }
161
162    /**
163     * Setter for the offset. The offset is relative to the current
164     * Y position. If you want to underline something, you have to
165     * choose a negative offset.
166     * @param offset    an offset
167     */
168    public void setOffset(final float offset) {
169        this.offset = offset;
170    }
171}