using System; using System.Collections; using System.Text; using System.util; using iTextSharp.text.pdf; /* * $Id: Rectangle.cs,v 1.18 2008/05/13 11:25:12 psoares33 Exp $ * * * Copyright 1999, 2000, 2001, 2002 by 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/ */ namespace iTextSharp.text { /// /// A Rectangle is the representation of a geometric figure. /// /// /// /// /// public class Rectangle : Element, IElement { // static membervariables (concerning the presence of borders) /// This is the value that will be used as undefined. public const int UNDEFINED = -1; /// This represents one side of the border of the Rectangle. public const int TOP_BORDER = 1; /// This represents one side of the border of the Rectangle. public const int BOTTOM_BORDER = 2; /// This represents one side of the border of the Rectangle. public const int LEFT_BORDER = 4; /// This represents one side of the border of the Rectangle. public const int RIGHT_BORDER = 8; /// This represents a rectangle without borders. public const int NO_BORDER = 0; /// This represents a type of border. public const int BOX = TOP_BORDER + BOTTOM_BORDER + LEFT_BORDER + RIGHT_BORDER; // membervariables /// the lower left x-coordinate. protected float llx; /// the lower left y-coordinate. protected float lly; /// the upper right x-coordinate. protected float urx; /// the upper right y-coordinate. protected float ury; /// This represents the status of the 4 sides of the rectangle. protected int border = UNDEFINED; /// This is the width of the border around this rectangle. protected float borderWidth = UNDEFINED; /// This is the color of the border of this rectangle. protected Color borderColor = null; /** The color of the left border of this rectangle. */ protected Color borderColorLeft = null; /** The color of the right border of this rectangle. */ protected Color borderColorRight = null; /** The color of the top border of this rectangle. */ protected Color borderColorTop = null; /** The color of the bottom border of this rectangle. */ protected Color borderColorBottom = null; /** The width of the left border of this rectangle. */ protected float borderWidthLeft = UNDEFINED; /** The width of the right border of this rectangle. */ protected float borderWidthRight = UNDEFINED; /** The width of the top border of this rectangle. */ protected float borderWidthTop = UNDEFINED; /** The width of the bottom border of this rectangle. */ protected float borderWidthBottom = UNDEFINED; /** Whether variable width borders are used. */ protected bool useVariableBorders = false; /// This is the color of the background of this rectangle. protected Color backgroundColor = null; /// This is the rotation value of this rectangle. protected int rotation = 0; // constructors /// /// Constructs a Rectangle-object. /// /// lower left x /// lower left y /// upper right x /// upper right y public Rectangle(float llx, float lly, float urx, float ury) { this.llx = llx; this.lly = lly; this.urx = urx; this.ury = ury; } /// /// Constructs a Rectangle-object starting from the origin (0, 0). /// /// upper right x /// upper right y public Rectangle(float urx, float ury) : this(0, 0, urx, ury) {} /// /// Constructs a Rectangle-object. /// /// another Rectangle public Rectangle(Rectangle rect) : this(rect.llx, rect.lly, rect.urx, rect.ury) { CloneNonPositionParameters(rect); } /** * Copies all of the parameters from a Rectangle object * except the position. * * @param rect * Rectangle to copy from */ public virtual void CloneNonPositionParameters(Rectangle rect) { this.rotation = rect.rotation; this.border = rect.border; this.borderWidth = rect.borderWidth; this.borderColor = rect.borderColor; this.backgroundColor = rect.backgroundColor; this.borderColorLeft = rect.borderColorLeft; this.borderColorRight = rect.borderColorRight; this.borderColorTop = rect.borderColorTop; this.borderColorBottom = rect.borderColorBottom; this.borderWidthLeft = rect.borderWidthLeft; this.borderWidthRight = rect.borderWidthRight; this.borderWidthTop = rect.borderWidthTop; this.borderWidthBottom = rect.borderWidthBottom; this.useVariableBorders = rect.useVariableBorders; } /** * Copies all of the parameters from a Rectangle object * except the position. * * @param rect * Rectangle to copy from */ public virtual void SoftCloneNonPositionParameters(Rectangle rect) { if (rect.rotation != 0) this.rotation = rect.rotation; if (rect.border != UNDEFINED) this.border = rect.border; if (rect.borderWidth != UNDEFINED) this.borderWidth = rect.borderWidth; if (rect.borderColor != null) this.borderColor = rect.borderColor; if (rect.backgroundColor != null) this.backgroundColor = rect.backgroundColor; if (rect.borderColorLeft != null) this.borderColorLeft = rect.borderColorLeft; if (rect.borderColorRight != null) this.borderColorRight = rect.borderColorRight; if (rect.borderColorTop != null) this.borderColorTop = rect.borderColorTop; if (rect.borderColorBottom != null) this.borderColorBottom = rect.borderColorBottom; if (rect.borderWidthLeft != UNDEFINED) this.borderWidthLeft = rect.borderWidthLeft; if (rect.borderWidthRight != UNDEFINED) this.borderWidthRight = rect.borderWidthRight; if (rect.borderWidthTop != UNDEFINED) this.borderWidthTop = rect.borderWidthTop; if (rect.borderWidthBottom != UNDEFINED) this.borderWidthBottom = rect.borderWidthBottom; if (useVariableBorders) this.useVariableBorders = rect.useVariableBorders; } // implementation of the Element interface /// /// Processes the element by adding it (or the different parts) to an /// IElementListener. /// /// an IElementListener /// true if the element was processed successfully public virtual bool Process(IElementListener listener) { try { return listener.Add(this); } catch (DocumentException) { return false; } } /// /// Gets the type of the text element. /// /// a type public virtual int Type { get { return Element.RECTANGLE; } } /// /// Gets all the chunks in this element. /// /// an ArrayList public virtual ArrayList Chunks { get { return new ArrayList(); } } /** * @see com.lowagie.text.Element#isContent() * @since iText 2.0.8 */ public bool IsContent() { return true; } /** * @see com.lowagie.text.Element#isNestable() * @since iText 2.0.8 */ public virtual bool IsNestable() { return false; } // methods /** * Switches lowerleft with upperright */ public virtual void Normalize() { if (llx > urx) { float a = llx; llx = urx; urx = a; } if (lly > ury) { float a = lly; lly = ury; ury = a; } } /// /// Gets a Rectangle that is altered to fit on the page. /// /// the top position /// the bottom position /// a Rectangle public Rectangle GetRectangle(float top, float bottom) { Rectangle tmp = new Rectangle(this); if (this.Top > top) { tmp.Top = top; tmp.Border = border - (border & TOP_BORDER); } if (Bottom < bottom) { tmp.Bottom = bottom; tmp.Border = border - (border & BOTTOM_BORDER); } return tmp; } /// /// Swaps the values of urx and ury and of lly and llx in order to rotate the rectangle. /// /// a Rectangle public Rectangle Rotate() { Rectangle rect = new Rectangle(lly, llx, ury, urx); rect.rotation = rotation + 90; rect.rotation %= 360; return rect; } // methods to set the membervariables /// /// Get/set the upper right y-coordinate. /// /// a float public virtual float Top { get { return ury; } set { ury = value; } } /** * Enables the border on the specified side. * * @param side * the side to enable. One of LEFT, RIGHT, TOP, BOTTOM * */ public virtual void EnableBorderSide(int side) { if (border == UNDEFINED) { border = 0; } border |= side; } /** * Disables the border on the specified side. * * @param side * the side to disable. One of LEFT, RIGHT, TOP, BOTTOM * */ public virtual void DisableBorderSide(int side) { if (border == UNDEFINED) { border = 0; } border &= ~side; } /// /// Get/set the border /// /// a int public virtual int Border { get { return this.border; } set { border = value; } } /// /// Get/set the grayscale of the rectangle. /// /// a float public virtual float GrayFill { get { if (backgroundColor is GrayColor) return ((GrayColor)backgroundColor).Gray; else return 0; } set { backgroundColor = new GrayColor(value); } } // methods to get the membervariables /// /// Get/set the lower left x-coordinate. /// /// a float public virtual float Left { get { return llx; } set { llx = value; } } /// /// Get/set the upper right x-coordinate. /// /// a float public virtual float Right { get { return urx; } set { urx = value; } } /// /// Get/set the lower left y-coordinate. /// /// a float public virtual float Bottom { get { return lly; } set { lly = value; } } public virtual Color BorderColorBottom { get { if (borderColorBottom == null) return borderColor; return borderColorBottom; } set { borderColorBottom = value; } } public virtual Color BorderColorTop { get { if (borderColorTop == null) return borderColor; return borderColorTop; } set { borderColorTop = value; } } public virtual Color BorderColorLeft { get { if (borderColorLeft == null) return borderColor; return borderColorLeft; } set { borderColorLeft = value; } } public virtual Color BorderColorRight { get { if (borderColorRight == null) return borderColor; return borderColorRight; } set { borderColorRight = value; } } /// /// Returns the lower left x-coordinate, considering a given margin. /// /// a margin /// the lower left x-coordinate public virtual float GetLeft(float margin) { return llx + margin; } /// /// Returns the upper right x-coordinate, considering a given margin. /// /// a margin /// the upper right x-coordinate public virtual float GetRight(float margin) { return urx - margin; } /// /// Returns the upper right y-coordinate, considering a given margin. /// /// a margin /// the upper right y-coordinate public virtual float GetTop(float margin) { return ury - margin; } /// /// Returns the lower left y-coordinate, considering a given margin. /// /// a margin /// the lower left y-coordinate public virtual float GetBottom(float margin) { return lly + margin; } /// /// Returns the width of the rectangle. /// /// a width public virtual float Width { get { return urx - llx; } set { throw new InvalidOperationException("The width cannot be set."); } } /// /// Returns the height of the rectangle. /// /// a height public float Height { get { return ury - lly; } } /// /// Indicates if the table has borders. /// /// a bool public bool HasBorders() { return (border > 0) && ((borderWidth > 0) || (borderWidthLeft > 0) || (borderWidthRight > 0) || (borderWidthTop > 0) || (borderWidthBottom > 0)); } /// /// Indicates if the table has a some type of border. /// /// the type of border /// a bool public bool HasBorder(int type) { return border != UNDEFINED && (border & type) == type; } /// /// Get/set the borderwidth. /// /// a float public virtual float BorderWidth { get { return borderWidth; } set { borderWidth = value; } } /** * Gets the color of the border. * * @return a value */ /// /// Get/set the color of the border. /// /// a Color public virtual Color BorderColor { get { return borderColor; } set { borderColor = value; } } /** * Gets the backgroundcolor. * * @return a value */ /// /// Get/set the backgroundcolor. /// /// a Color public virtual Color BackgroundColor { get { return backgroundColor; } set { backgroundColor = value; } } /// /// Returns the rotation /// /// a int public int Rotation { get { return rotation; } } public virtual float BorderWidthLeft { get { return GetVariableBorderWidth(borderWidthLeft, LEFT_BORDER); } set { borderWidthLeft = value; UpdateBorderBasedOnWidth(value, LEFT_BORDER); } } public virtual float BorderWidthRight { get { return GetVariableBorderWidth(borderWidthRight, RIGHT_BORDER); } set { borderWidthRight = value; UpdateBorderBasedOnWidth(value, RIGHT_BORDER); } } public virtual float BorderWidthTop { get { return GetVariableBorderWidth(borderWidthTop, TOP_BORDER); } set { borderWidthTop = value; UpdateBorderBasedOnWidth(value, TOP_BORDER); } } public virtual float BorderWidthBottom { get { return GetVariableBorderWidth(borderWidthBottom, BOTTOM_BORDER); } set { borderWidthBottom = value; UpdateBorderBasedOnWidth(value, BOTTOM_BORDER); } } /** * Updates the border flag for a side based on the specified width. A width * of 0 will disable the border on that side. Any other width enables it. * * @param width * width of border * @param side * border side constant */ private void UpdateBorderBasedOnWidth(float width, int side) { useVariableBorders = true; if (width > 0) { EnableBorderSide(side); } else { DisableBorderSide(side); } } private float GetVariableBorderWidth(float variableWidthValue, int side) { if ((border & side) != 0) { return variableWidthValue != UNDEFINED ? variableWidthValue : borderWidth; } else { return 0; } } /** * Sets a parameter indicating if the rectangle has variable borders * * @param useVariableBorders * indication if the rectangle has variable borders */ public virtual bool UseVariableBorders{ get { return useVariableBorders; } set { useVariableBorders = value; } } public override String ToString() { StringBuilder buf = new StringBuilder("Rectangle: "); buf.Append(Width); buf.Append('x'); buf.Append(Height); buf.Append(" (rot: "); buf.Append(rotation); buf.Append(" degrees)"); return buf.ToString(); } } }