001/* 002 * $Id: LineSeparator.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 com.itextpdf.text.Element; 047import com.itextpdf.text.pdf.PdfContentByte; 048 049import com.itextpdf.text.BaseColor; 050 051/** 052 * Element that draws a solid line from left to right. 053 * Can be added directly to a document or column. 054 * Can also be used to create a separator chunk. 055 * @author Paulo Soares 056 * @since 2.1.2 057 */ 058public class LineSeparator extends VerticalPositionMark { 059 060 /** The thickness of the line. */ 061 protected float lineWidth = 1; 062 /** The width of the line as a percentage of the available page width. */ 063 protected float percentage = 100; 064 /** The color of the line. */ 065 protected BaseColor lineColor; 066 /** The alignment of the line. */ 067 protected int alignment = Element.ALIGN_CENTER; 068 069 /** 070 * Creates a new instance of the LineSeparator class. 071 * @param lineWidth the thickness of the line 072 * @param percentage the width of the line as a percentage of the available page width 073 * @param lineColor the color of the line 074 * @param align the alignment 075 * @param offset the offset of the line relative to the current baseline (negative = under the baseline) 076 */ 077 public LineSeparator(float lineWidth, float percentage, BaseColor lineColor, int align, float offset) { 078 this.lineWidth = lineWidth; 079 this.percentage = percentage; 080 this.lineColor = lineColor; 081 this.alignment = align; 082 this.offset = offset; 083 } 084 085 /** 086 * Creates a new instance of the LineSeparator class with 087 * default values: lineWidth 1 user unit, width 100%, centered with offset 0. 088 */ 089 public LineSeparator() { 090 } 091 092 /** 093 * @see com.itextpdf.text.pdf.draw.DrawInterface#draw(com.itextpdf.text.pdf.PdfContentByte, float, float, float, float, float) 094 */ 095 public void draw(PdfContentByte canvas, float llx, float lly, float urx, float ury, float y) { 096 canvas.saveState(); 097 drawLine(canvas, llx, urx, y); 098 canvas.restoreState(); 099 } 100 101 /** 102 * Draws a horizontal line. 103 * @param canvas the canvas to draw on 104 * @param leftX the left x coordinate 105 * @param rightX the right x coordindate 106 * @param y the y coordinate 107 */ 108 public void drawLine(PdfContentByte canvas, float leftX, float rightX, float y) { 109 float w; 110 if (getPercentage() < 0) 111 w = -getPercentage(); 112 else 113 w = (rightX - leftX) * getPercentage() / 100.0f; 114 float s; 115 switch (getAlignment()) { 116 case Element.ALIGN_LEFT: 117 s = 0; 118 break; 119 case Element.ALIGN_RIGHT: 120 s = rightX - leftX - w; 121 break; 122 default: 123 s = (rightX - leftX - w) / 2; 124 break; 125 } 126 canvas.setLineWidth(getLineWidth()); 127 if (getLineColor() != null) 128 canvas.setColorStroke(getLineColor()); 129 canvas.moveTo(s + leftX, y + offset); 130 canvas.lineTo(s + w + leftX, y + offset); 131 canvas.stroke(); 132 } 133 134 /** 135 * Getter for the line width. 136 * @return the thickness of the line that will be drawn. 137 */ 138 public float getLineWidth() { 139 return lineWidth; 140 } 141 142 /** 143 * Setter for the line width. 144 * @param lineWidth the thickness of the line that will be drawn. 145 */ 146 public void setLineWidth(float lineWidth) { 147 this.lineWidth = lineWidth; 148 } 149 150 /** 151 * Setter for the width as a percentage of the available width. 152 * @return a width percentage 153 */ 154 public float getPercentage() { 155 return percentage; 156 } 157 158 /** 159 * Setter for the width as a percentage of the available width. 160 * @param percentage a width percentage 161 */ 162 public void setPercentage(float percentage) { 163 this.percentage = percentage; 164 } 165 166 /** 167 * Getter for the color of the line that will be drawn. 168 * @return a color 169 */ 170 public BaseColor getLineColor() { 171 return lineColor; 172 } 173 174 /** 175 * Setter for the color of the line that will be drawn. 176 * @param color a color 177 */ 178 public void setLineColor(BaseColor color) { 179 this.lineColor = color; 180 } 181 182 /** 183 * Getter for the alignment of the line. 184 * @return an alignment value 185 */ 186 public int getAlignment() { 187 return alignment; 188 } 189 190 /** 191 * Setter for the alignment of the line. 192 * @param align an alignment value 193 */ 194 public void setAlignment(int align) { 195 this.alignment = align; 196 } 197}