GraphicsUtils.java
10.7 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.fontengine.font.Font
* com.adobe.fontengine.font.FontData
* com.adobe.fontengine.font.FontImpl
* com.adobe.fontengine.font.FontLoadingException
* com.adobe.fontengine.font.InvalidFontException
* com.adobe.fontengine.font.OutlineConsumer
* com.adobe.fontengine.font.UnsupportedFontException
* com.adobe.internal.pdftoolkit.core.types.ASMatrix
* com.adobe.internal.pdftoolkit.core.types.ASName
*/
package com.adobe.internal.pdftoolkit.graphicsDOM.utils;
import com.adobe.fontengine.font.Font;
import com.adobe.fontengine.font.FontData;
import com.adobe.fontengine.font.FontImpl;
import com.adobe.fontengine.font.FontLoadingException;
import com.adobe.fontengine.font.InvalidFontException;
import com.adobe.fontengine.font.OutlineConsumer;
import com.adobe.fontengine.font.UnsupportedFontException;
import com.adobe.internal.pdftoolkit.core.types.ASMatrix;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.graphicsDOM.ContentItem;
import com.adobe.internal.pdftoolkit.graphicsDOM.Glyph;
import com.adobe.internal.pdftoolkit.graphicsDOM.GraphicsState;
import com.adobe.internal.pdftoolkit.graphicsDOM.TextState;
import com.adobe.internal.pdftoolkit.graphicsDOM.TilingPattern;
import com.adobe.internal.pdftoolkit.graphicsDOM.utils.GlyphAbsoluteOutlineGenerator;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Arrays;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class GraphicsUtils {
public static final String DeviceGray = ASName.k_DeviceGray.asString();
public static final String DeviceRGB = ASName.k_DeviceRGB.asString();
public static final String DeviceCMYK = ASName.k_DeviceCMYK.asString();
private GraphicsUtils() {
}
public static int readNthSample(byte[] data, int bpv, int n) {
if (bpv == 8) {
return data[n];
}
int bitPosToStartFrom = n * bpv;
int bytePosInData = bitPosToStartFrom / 8;
int leftBitsToIgnoreFromCurrentByte = bitPosToStartFrom - bytePosInData * 8;
int bitsToReadFromCurrentByte = bpv < 8 - leftBitsToIgnoreFromCurrentByte ? bpv : 8 - leftBitsToIgnoreFromCurrentByte;
int returnValue = data[bytePosInData] & 255 >> leftBitsToIgnoreFromCurrentByte;
returnValue >>= 8 - leftBitsToIgnoreFromCurrentByte - bitsToReadFromCurrentByte;
for (int moreBitsToRead = bpv - bitsToReadFromCurrentByte; moreBitsToRead != 0; moreBitsToRead -= bitsToReadFromCurrentByte) {
bitsToReadFromCurrentByte = moreBitsToRead > 8 ? 8 : moreBitsToRead;
int left = returnValue << bitsToReadFromCurrentByte;
int right = data[++bytePosInData] >> 8 - bitsToReadFromCurrentByte;
returnValue = left | right;
}
return returnValue;
}
public static double scaleToRange(double inputRangeMin, double inputRangeMax, double inputValueToBeScaledToRange, double outputRangeMin, double outputRangeMax) {
return outputRangeMin + (inputValueToBeScaledToRange - inputRangeMin) * (outputRangeMin - outputRangeMax) / (inputRangeMin - inputRangeMax);
}
public static int rint(double a) {
if (a - (double)((int)a) <= 0.5) {
return (int)a;
}
return (int)a + 1;
}
public static boolean equalPaths(GeneralPath path1, GeneralPath path2) {
if (path1 == null && path2 == null) {
return true;
}
if (path1 == null || path2 == null) {
return false;
}
if (path1.getWindingRule() != path2.getWindingRule()) {
return false;
}
PathIterator pathItr1 = path1.getPathIterator(null);
PathIterator pathItr2 = path2.getPathIterator(null);
double[] coords1 = new double[6];
double[] coords2 = new double[6];
while (!pathItr1.isDone() && !pathItr2.isDone()) {
int segType2;
int segType1 = pathItr1.currentSegment(coords1);
if (segType1 != (segType2 = pathItr2.currentSegment(coords2)) || !Arrays.equals(coords1, coords2)) {
return false;
}
pathItr1.next();
pathItr2.next();
}
return pathItr1.isDone() && pathItr2.isDone();
}
public static int toARGB(double[] rgb, double alpha) {
int a = GraphicsUtils.rint(alpha * 255.0);
int red = GraphicsUtils.rint(rgb[0] * 255.0);
int green = GraphicsUtils.rint(rgb[1] * 255.0);
int blue = GraphicsUtils.rint(rgb[2] * 255.0);
return a << 24 | red << 16 | green << 8 | blue;
}
public static double[] getClipBounds(TilingPattern<? extends GraphicsState, ? extends TextState, ? extends ContentItem<? extends GraphicsState>> tilingPattern, Rectangle2D virtualRect, Rectangle2D patternVirtualRect, int[] posBounds) {
double[] clipBounds = new double[]{virtualRect.getX(), virtualRect.getY(), virtualRect.getX() + virtualRect.getWidth(), virtualRect.getY() + virtualRect.getHeight()};
double clipBoundsXmin = clipBounds[0] - patternVirtualRect.getWidth() - tilingPattern.getPhase()[0];
double clipBoundsYmin = clipBounds[1] - patternVirtualRect.getHeight() - tilingPattern.getPhase()[1];
double clipBoundsXMax = Math.ceil(clipBounds[2] + patternVirtualRect.getWidth()) - tilingPattern.getPhase()[0];
double clipBoundsYMax = Math.ceil(clipBounds[3] + patternVirtualRect.getHeight()) - tilingPattern.getPhase()[1];
GraphicsUtils.getExactPatternCellPositionNearby(clipBoundsXmin, clipBoundsYmin, tilingPattern, posBounds);
GraphicsUtils.getExactPatternCellPositionNearby(clipBoundsXMax, clipBoundsYmin, tilingPattern, posBounds);
GraphicsUtils.getExactPatternCellPositionNearby(clipBoundsXMax, clipBoundsYMax, tilingPattern, posBounds);
GraphicsUtils.getExactPatternCellPositionNearby(clipBoundsXmin, clipBoundsYMax, tilingPattern, posBounds);
return new double[]{clipBoundsXmin, clipBoundsYmin, clipBoundsXMax, clipBoundsYMax};
}
private static void getExactPatternCellPositionNearby(double X, double Y, TilingPattern<? extends GraphicsState, ? extends TextState, ? extends ContentItem<? extends GraphicsState>> tilingPattern, int[] posBounds) {
double[] xStep = tilingPattern.getXStep();
double[] yStep = tilingPattern.getYStep();
double posX = (X * yStep[1] - Y * yStep[0]) / (xStep[0] * yStep[1] - xStep[1] * yStep[0]);
double posY = (Y * xStep[0] - X * xStep[1]) / (xStep[0] * yStep[1] - xStep[1] * yStep[0]);
if ((int)posX < posBounds[0]) {
posBounds[0] = (int)posX;
}
if ((int)posY < posBounds[1]) {
posBounds[1] = (int)posY;
}
if ((int)posX > posBounds[2]) {
posBounds[2] = (int)posX + 1;
}
if ((int)posY > posBounds[3]) {
posBounds[3] = (int)posY + 1;
}
}
public static double[] placeCell(int posX, int posY, TilingPattern<? extends GraphicsState, ? extends TextState, ? extends ContentItem<? extends GraphicsState>> tilingPattern) {
double[] xStep = tilingPattern.getXStep();
double[] yStep = tilingPattern.getYStep();
return new double[]{(double)posY * yStep[0] + (double)posX * xStep[0], (double)posY * yStep[1] + (double)posX * xStep[1]};
}
public static String generateKey(int objNumber, ASName objName, double[] rgbColor) {
if (objNumber != 0) {
StringBuilder key = new StringBuilder(String.valueOf(objNumber));
if (rgbColor != null) {
return key.append(Arrays.hashCode(rgbColor)).toString();
}
return key.toString();
}
if (objName == ASName.k_DeviceGray) {
return DeviceGray;
}
if (objName == ASName.k_DeviceRGB) {
return DeviceRGB;
}
if (objName == ASName.k_DeviceCMYK) {
return DeviceCMYK;
}
return objName != null ? objName.asString() : null;
}
public static AffineTransform pdfToRasterTransform(double[] transformation, double scalingFactorW, double scalingFactorH, int width, int height, double pageHeight, boolean isTilingPattern, Graphics2D g2) {
AffineTransform img2user = new AffineTransform();
img2user.setTransform(transformation[0], transformation[1], transformation[2], transformation[3], 0.0, 0.0);
img2user.scale(scalingFactorW / (double)width, (- scalingFactorH) / (double)height);
Point2D.Double point = new Point2D.Double(0.0, height);
img2user.transform(point, point);
if (isTilingPattern) {
AffineTransform xform = new AffineTransform();
xform.translate(0.0, point.x - pageHeight - point.y);
g2.transform(xform);
}
img2user.setTransform(transformation[0], - transformation[1], - transformation[2], transformation[3], scalingFactorW * transformation[4], pageHeight - scalingFactorH * transformation[5]);
img2user.scale(scalingFactorW / (double)width, (- scalingFactorH) / (double)height);
img2user.setTransform(img2user.getScaleX(), img2user.getShearY(), img2user.getShearX(), img2user.getScaleY(), img2user.getTranslateX() - point.x, img2user.getTranslateY() + point.y);
img2user.scale(1.0, -1.0);
return img2user;
}
public static GeneralPath getGlyphsOulines(int gid, Font font) throws InvalidFontException, UnsupportedFontException, FontLoadingException {
FontData fontData = ((FontImpl)font).getFontData();
GeneralPath glyphOutline = null;
GlyphAbsoluteOutlineGenerator glyphOutlineGenerator = new GlyphAbsoluteOutlineGenerator(null);
fontData.getGlyphOutline(gid, (OutlineConsumer)glyphOutlineGenerator);
glyphOutline = glyphOutlineGenerator.getOutline();
return glyphOutline;
}
public static GeneralPath getTransformedGlyphsOutlines(GeneralPath glyphOutline, int gid, ASMatrix textMatrix, double fontSize, double textRise, Glyph glyph, Font font) throws InvalidFontException, UnsupportedFontException, FontLoadingException {
if (glyphOutline == null) {
glyphOutline = GraphicsUtils.getGlyphsOulines(gid, font);
}
ASMatrix transformationMatrix = new ASMatrix(textMatrix.geta() * fontSize, textMatrix.getb() * fontSize, textMatrix.getc() * fontSize, textMatrix.getd() * fontSize, glyph.getXPos(), glyph.getYPos() + textRise);
glyphOutline = (GeneralPath)glyphOutline.clone();
glyphOutline.transform(new AffineTransform(transformationMatrix.getValues()));
glyph.setGlyphOutline(glyphOutline);
return glyphOutline;
}
}