PDFRectangle.java
9.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.internal.pdftoolkit.core.cos.CosArray
* com.adobe.internal.pdftoolkit.core.cos.CosObject
* com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException
* com.adobe.internal.pdftoolkit.core.types.ASMatrix
* com.adobe.internal.pdftoolkit.core.types.ASRectangle
*/
package com.adobe.internal.pdftoolkit.pdf.graphics;
import com.adobe.internal.pdftoolkit.core.cos.CosArray;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.core.types.ASMatrix;
import com.adobe.internal.pdftoolkit.core.types.ASRectangle;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosObject;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import java.awt.geom.Rectangle2D;
public class PDFRectangle
extends PDFCosObject {
private PDFRectangle(CosObject cosObject) throws PDFInvalidDocumentException {
super(cosObject);
}
public static PDFRectangle getInstance(CosObject cosObject) throws PDFInvalidDocumentException {
if (PDFCosObject.checkNullCosObject(cosObject) == null) {
return null;
}
PDFRectangle pdfObject = PDFCosObject.getCachedInstance(cosObject, PDFRectangle.class);
if (pdfObject == null) {
pdfObject = new PDFRectangle(cosObject);
}
return pdfObject;
}
public static PDFRectangle newInstance(PDFDocument pdfDocument, double llx, double lly, double urx, double ury) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosArray cosObject = PDFCosObject.newCosArray(pdfDocument);
PDFRectangle pdfRectangle = new PDFRectangle((CosObject)cosObject);
cosObject.addDouble(llx);
cosObject.addDouble(lly);
cosObject.addDouble(urx);
cosObject.addDouble(ury);
return pdfRectangle;
}
public static PDFRectangle newInstance(PDFDocument pdfDocument, ASRectangle asRectangle) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return PDFRectangle.newInstance(pdfDocument, asRectangle.left(), asRectangle.bottom(), asRectangle.right(), asRectangle.top());
}
public ASRectangle getRectangle() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return new ASRectangle(this.left(), this.bottom(), this.right(), this.top());
}
public double[] getValues() throws PDFCosParseException, PDFIOException, PDFSecurityException {
CosArray cosArray = this.getCosArray();
double[] vals = new double[]{cosArray.getDouble(0), cosArray.getDouble(1), cosArray.getDouble(2), cosArray.getDouble(3)};
return vals;
}
public PDFRectangle normalized(PDFDocument pdfDocument) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
double[] newVals = new double[]{Math.min(this.left(), this.right()), Math.min(this.bottom(), this.top()), Math.max(this.left(), this.right()), Math.max(this.bottom(), this.top())};
return PDFRectangle.newInstance(pdfDocument, newVals[0], newVals[1], newVals[2], newVals[3]);
}
public double llx() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getCosArray().getDouble(0);
}
public double lly() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getCosArray().getDouble(1);
}
public double urx() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getCosArray().getDouble(2);
}
public double ury() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getCosArray().getDouble(3);
}
public double left() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.llx();
}
public double bottom() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.lly();
}
public double right() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.urx();
}
public double top() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.ury();
}
public double width() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return Math.abs(this.urx() - this.llx());
}
public double height() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return Math.abs(this.ury() - this.lly());
}
public boolean contains(PDFRectangle rect) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
boolean isRectContained = false;
Rectangle2D.Double currentRectangle = new Rectangle2D.Double(this.llx(), this.lly(), this.urx() - this.llx(), this.ury() - this.lly());
isRectContained = currentRectangle.contains(rect.llx(), rect.lly(), rect.urx() - rect.llx(), rect.ury() - rect.lly());
return isRectContained;
}
public boolean intersects(PDFRectangle rect) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
Rectangle2D.Double rectToCheck;
boolean doesIntersect = false;
Rectangle2D.Double currentRectangle = new Rectangle2D.Double(this.llx(), this.lly(), this.urx() - this.llx(), this.ury() - this.lly());
if (currentRectangle.intersects(rectToCheck = new Rectangle2D.Double(rect.llx(), rect.lly(), Math.abs(rect.urx() - rect.llx()), Math.abs(rect.ury() - rect.lly())))) {
doesIntersect = true;
}
return doesIntersect;
}
public PDFRectangle createUnion(PDFRectangle rect) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFRectangle newRect = null;
Rectangle2D.Double currentRectangle = new Rectangle2D.Double(this.llx(), this.lly(), this.urx() - this.llx(), this.ury() - this.lly());
Rectangle2D.Double givenRectangle = new Rectangle2D.Double(rect.llx(), rect.lly(), rect.urx() - rect.llx(), rect.ury() - rect.lly());
Rectangle2D.Double unionRect = new Rectangle2D.Double();
unionRect.setRect(currentRectangle.createUnion(givenRectangle));
newRect = PDFRectangle.newInstance(this.getPDFDocument(), unionRect.x, unionRect.y, unionRect.x + unionRect.width, unionRect.y + unionRect.height);
return newRect;
}
public PDFRectangle createIntersection(PDFRectangle rect) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFRectangle newRect = null;
Rectangle2D.Double currentRectangle = new Rectangle2D.Double(this.llx(), this.lly(), this.urx() - this.llx(), this.ury() - this.lly());
Rectangle2D.Double givenRectangle = new Rectangle2D.Double(rect.llx(), rect.lly(), rect.urx() - rect.llx(), rect.ury() - rect.lly());
Rectangle2D.Double intersectRect = new Rectangle2D.Double();
intersectRect.setRect(currentRectangle.createIntersection(givenRectangle));
newRect = PDFRectangle.newInstance(this.getPDFDocument(), intersectRect.x, intersectRect.y, intersectRect.x + intersectRect.width, intersectRect.y + intersectRect.height);
return newRect;
}
public boolean hasNonZeroDimensions() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.llx() == 0.0 && this.lly() == 0.0 && this.urx() == 0.0 && this.ury() == 0.0) {
return false;
}
if (this.height() == 0.0 || this.width() == 0.0) {
return false;
}
return true;
}
public boolean hasNonZeroDimensions(double epsilon) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (Math.abs(this.llx()) < epsilon && Math.abs(this.lly()) < epsilon && Math.abs(this.urx()) < epsilon && Math.abs(this.ury()) < epsilon) {
return false;
}
if (this.height() < epsilon || this.width() < epsilon) {
return false;
}
return true;
}
public PDFRectangle rotate(int rotationAngle) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
ASRectangle asRect = this.getRectangle();
asRect = asRect.transform(PDFRectangle.getTransformationMatrix(rotationAngle));
return PDFRectangle.newInstance(this.getPDFDocument(), asRect);
}
public static PDFRectangle rotate(PDFRectangle rect, int rotationAngle) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
ASRectangle asRect = rect.getRectangle();
asRect = asRect.transform(PDFRectangle.getTransformationMatrix(rotationAngle));
return PDFRectangle.newInstance(rect.getPDFDocument(), asRect);
}
private static ASMatrix getTransformationMatrix(int rotationAngle) {
return ASMatrix.createIdentityMatrix().rotate(Math.toRadians(rotationAngle));
}
}