1 31 package org.pdfbox.pdmodel.common; 32 33 import org.pdfbox.cos.COSArray; 34 import org.pdfbox.cos.COSBase; 35 import org.pdfbox.cos.COSFloat; 36 import org.pdfbox.cos.COSNumber; 37 38 import org.fontbox.util.BoundingBox; 39 40 import java.awt.Dimension ; 41 42 48 public class PDRectangle implements COSObjectable 49 { 50 private COSArray rectArray; 51 52 57 public PDRectangle() 58 { 59 rectArray = new COSArray(); 60 rectArray.add( new COSFloat( 0.0f ) ); 61 rectArray.add( new COSFloat( 0.0f ) ); 62 rectArray.add( new COSFloat( 0.0f ) ); 63 rectArray.add( new COSFloat( 0.0f ) ); 64 } 65 66 72 public PDRectangle( float width, float height ) 73 { 74 rectArray = new COSArray(); 75 rectArray.add( new COSFloat( 0.0f ) ); 76 rectArray.add( new COSFloat( 0.0f ) ); 77 rectArray.add( new COSFloat( width ) ); 78 rectArray.add( new COSFloat( height ) ); 79 } 80 81 86 public PDRectangle( BoundingBox box ) 87 { 88 rectArray = new COSArray(); 89 rectArray.add( new COSFloat( box.getLowerLeftX() ) ); 90 rectArray.add( new COSFloat( box.getLowerLeftY() ) ); 91 rectArray.add( new COSFloat( box.getUpperRightX() ) ); 92 rectArray.add( new COSFloat( box.getUpperRightY() ) ); 93 } 94 95 100 public PDRectangle( COSArray array ) 101 { 102 rectArray = array; 103 } 104 105 111 public boolean contains( float x, float y ) 112 { 113 float llx = getLowerLeftX(); 114 float urx = getUpperRightX(); 115 float lly = getLowerLeftY(); 116 float ury = getUpperRightY(); 117 return x >= llx && x <= urx && 118 y >= lly && y <= ury; 119 } 120 121 130 public PDRectangle createRetranslatedRectangle() 131 { 132 PDRectangle retval = new PDRectangle(); 133 retval.setUpperRightX( getWidth() ); 134 retval.setUpperRightY( getHeight() ); 135 return retval; 136 } 137 138 143 public COSArray getCOSArray() 144 { 145 return rectArray; 146 } 147 148 153 public float getLowerLeftX() 154 { 155 return ((COSNumber)rectArray.get(0)).floatValue(); 156 } 157 158 163 public void setLowerLeftX(float value) 164 { 165 rectArray.set(0, new COSFloat( value ) ); 166 } 167 168 173 public float getLowerLeftY() 174 { 175 return ((COSNumber)rectArray.get(1)).floatValue(); 176 } 177 178 183 public void setLowerLeftY(float value) 184 { 185 rectArray.set(1, new COSFloat( value ) ); 186 } 187 188 193 public float getUpperRightX() 194 { 195 return ((COSNumber)rectArray.get(2)).floatValue(); 196 } 197 198 203 public void setUpperRightX(float value) 204 { 205 rectArray.set(2, new COSFloat( value ) ); 206 } 207 208 213 public float getUpperRightY() 214 { 215 return ((COSNumber)rectArray.get(3)).floatValue(); 216 } 217 218 223 public void setUpperRightY(float value) 224 { 225 rectArray.set(3, new COSFloat( value ) ); 226 } 227 228 234 public float getWidth() 235 { 236 return getUpperRightX() - getLowerLeftX(); 237 } 238 239 245 public float getHeight() 246 { 247 return getUpperRightY() - getLowerLeftY(); 248 } 249 250 255 public Dimension createDimension() 256 { 257 return new Dimension ( (int)getWidth(), (int)getHeight() ); 258 } 259 260 266 public void move(float horizontalAmount, float verticalAmount) 267 { 268 setUpperRightX(getUpperRightX() + horizontalAmount); 269 setLowerLeftX(getLowerLeftX() + horizontalAmount); 270 setUpperRightY(getUpperRightY() + verticalAmount); 271 setLowerLeftY(getLowerLeftY() + verticalAmount); 272 } 273 274 279 public COSBase getCOSObject() 280 { 281 return rectArray; 282 } 283 284 285 290 public String toString() 291 { 292 return "[" + getLowerLeftX() + "," + getLowerLeftY() + "," + 293 getUpperRightX() + "," + getUpperRightY() +"]"; 294 } 295 } | Popular Tags |