ContentTextItem.java
5.43 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.fontengine.font.Font
* com.adobe.fontengine.font.FontLoadingException
* com.adobe.fontengine.font.InvalidFontException
* com.adobe.fontengine.font.InvalidGlyphException
* com.adobe.fontengine.font.UnsupportedFontException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFRuntimeException
* com.adobe.internal.pdftoolkit.core.types.ASMatrix
*/
package com.adobe.internal.pdftoolkit.graphicsDOM;
import com.adobe.fontengine.font.Font;
import com.adobe.fontengine.font.FontLoadingException;
import com.adobe.fontengine.font.InvalidFontException;
import com.adobe.fontengine.font.InvalidGlyphException;
import com.adobe.fontengine.font.UnsupportedFontException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFRuntimeException;
import com.adobe.internal.pdftoolkit.core.types.ASMatrix;
import com.adobe.internal.pdftoolkit.graphicsDOM.AbstractContentItem;
import com.adobe.internal.pdftoolkit.graphicsDOM.ContentType;
import com.adobe.internal.pdftoolkit.graphicsDOM.DocumentContext;
import com.adobe.internal.pdftoolkit.graphicsDOM.Glyph;
import com.adobe.internal.pdftoolkit.graphicsDOM.GraphicsState;
import com.adobe.internal.pdftoolkit.graphicsDOM.PrimitiveContentItem;
import com.adobe.internal.pdftoolkit.graphicsDOM.TextState;
import com.adobe.internal.pdftoolkit.graphicsDOM.Type3Glyph;
import com.adobe.internal.pdftoolkit.graphicsDOM.utils.GraphicsUtils;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public abstract class ContentTextItem<T extends TextState, G extends GraphicsState>
extends AbstractContentItem<G>
implements PrimitiveContentItem<G> {
private List<Glyph> text;
private T textState;
private Rectangle2D boundingBox = null;
public ContentTextItem(G graphicsState, T textState, int id) {
super(graphicsState, id);
this.setTState(textState);
}
public T getTState() {
return this.textState;
}
public void setTState(T tState) {
this.textState = tState;
}
public List<Glyph> getText() {
return this.text;
}
public void setText(List<Glyph> text) {
this.text = text;
}
@Override
public void writeToDisplayArea(DocumentContext context) {
AbstractContentItem.super.writeToDisplayArea(context);
if (this.getTState() != null) {
this.getTState().writeToDisplayArea(context);
}
}
public boolean isFontType3() {
return this.text != null && !this.text.isEmpty() && this.text.get(0) instanceof Type3Glyph;
}
@Override
public ContentType getType() {
return ContentType.Text;
}
@Override
public Rectangle2D getBoundingBox(DocumentContext context) {
if (this.boundingBox != null) {
return this.boundingBox;
}
Iterator<Glyph> itr = this.text.iterator();
Glyph glyph = null;
GeneralPath outline = null;
Rectangle2D currentGlyphBoundingBox = null;
Rectangle2D completeBoundingBox = null;
while (itr.hasNext()) {
try {
glyph = itr.next();
if (this.isFontType3()) {
currentGlyphBoundingBox = ((Type3Glyph)glyph).getBoundingBox(context);
} else {
outline = glyph.getGlyphOutline();
if (outline == null) {
outline = context.getCachedGlyphOutline(glyph.getGID(), this.getTState().getFontName());
if (outline == null) {
outline = GraphicsUtils.getGlyphsOulines(glyph.getGID(), this.getTState().getFontMap().get(this.getTState().getFontName()));
context.cacheGlyphOutline(outline, glyph.getGID(), this.getTState().getFontName());
}
outline = GraphicsUtils.getTransformedGlyphsOutlines(outline, glyph.getGID(), new ASMatrix(this.getTState().getMatrix()), this.getTState().getFontSize(), this.getTState().getTextRise(), glyph, null);
glyph.setGlyphOutline(outline);
}
currentGlyphBoundingBox = outline.getBounds2D();
}
if (currentGlyphBoundingBox == null || currentGlyphBoundingBox.getWidth() == 0.0 || currentGlyphBoundingBox.getHeight() == 0.0) continue;
completeBoundingBox = completeBoundingBox == null ? currentGlyphBoundingBox : completeBoundingBox.createUnion(currentGlyphBoundingBox);
continue;
}
catch (InvalidGlyphException e) {
continue;
}
catch (FontLoadingException e) {
throw new PDFRuntimeException("Exception while calculating bounding box of text.", (Throwable)e);
}
catch (InvalidFontException e) {
throw new PDFRuntimeException("Exception while calculating bounding box of text.", (Throwable)e);
}
catch (UnsupportedFontException e) {
throw new PDFRuntimeException("Exception while calculating bounding box of text.", (Throwable)e);
}
}
this.boundingBox = completeBoundingBox != null ? completeBoundingBox : currentGlyphBoundingBox;
return this.boundingBox;
}
}