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
|
/*
* Copyright 1999-2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* $Id$ */
package org.apache.fop.render.ps;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.io.IOException;
import org.apache.fop.fonts.Font;
import org.apache.fop.fonts.FontInfo;
import org.apache.fop.fonts.FontSetup;
import org.apache.fop.fonts.FontTriplet;
import org.apache.xmlgraphics.java2d.ps.PSGraphics2D;
import org.apache.xmlgraphics.java2d.ps.TextHandler;
import org.apache.xmlgraphics.ps.PSGenerator;
/**
* Specialized TextHandler implementation that the PSGraphics2D class delegates to to paint text
* using PostScript text operations.
*/
public class NativeTextHandler implements TextHandler {
private PSGraphics2D g2d;
/** FontInfo containing all available fonts */
protected FontInfo fontInfo;
/** Currently valid Font */
protected Font font;
/** Overriding FontState */
protected Font overrideFont = null;
/** the current (internal) font name */
protected String currentFontName;
/** the current font size in millipoints */
protected int currentFontSize;
/**
* Main constructor.
* @param g2d the PSGraphics2D instance this instances is used by
* @param fontInfo the FontInfo object with all available fonts
*/
public NativeTextHandler(PSGraphics2D g2d, FontInfo fontInfo) {
this.g2d = g2d;
if (fontInfo != null) {
this.fontInfo = fontInfo;
} else {
setupFontInfo();
}
}
private void setupFontInfo() {
//Sets up a FontInfo with default fonts
fontInfo = new FontInfo();
FontSetup.setup(fontInfo, null, null);
}
/**
* Return the font information associated with this object
* @return the FontInfo object
*/
public FontInfo getFontInfo() {
return fontInfo;
}
private PSGenerator getPSGenerator() {
return this.g2d.getPSGenerator();
}
/** @see org.apache.xmlgraphics.java2d.ps.TextHandler#writeSetup() */
public void writeSetup() throws IOException {
if (fontInfo != null) {
PSFontUtils.writeFontDict(getPSGenerator(), fontInfo);
}
}
/** @see org.apache.xmlgraphics.java2d.ps.TextHandler#writePageSetup() */
public void writePageSetup() throws IOException {
if (fontInfo != null) {
getPSGenerator().writeln("FOPFonts begin");
}
}
/**
* Draw a string to the PostScript document. The text is painted using
* text operations.
* @see org.apache.xmlgraphics.java2d.ps.TextHandler#drawString(java.lang.String, float, float)
*/
public void drawString(String s, float x, float y) throws IOException {
g2d.preparePainting();
if (this.overrideFont == null) {
java.awt.Font awtFont = g2d.getFont();
this.font = createFont(awtFont);
} else {
this.font = this.overrideFont;
this.overrideFont = null;
}
//Color and Font state
g2d.establishColor(g2d.getColor());
establishCurrentFont();
PSGenerator gen = getPSGenerator();
gen.saveGraphicsState();
//Clip
Shape imclip = g2d.getClip();
g2d.writeClip(imclip);
//Prepare correct transformation
AffineTransform trans = g2d.getTransform();
gen.concatMatrix(trans);
gen.writeln(gen.formatDouble(x) + " "
+ gen.formatDouble(y) + " moveto ");
gen.writeln("1 -1 scale");
StringBuffer sb = new StringBuffer("(");
escapeText(s, sb);
sb.append(") t ");
gen.writeln(sb.toString());
gen.restoreGraphicsState();
}
private void escapeText(final String text, StringBuffer target) {
final int l = text.length();
for (int i = 0; i < l; i++) {
final char ch = text.charAt(i);
final char mch = this.font.mapChar(ch);
PSGenerator.escapeChar(mch, target);
}
}
private Font createFont(java.awt.Font f) {
String fontFamily = f.getFamily();
if (fontFamily.equals("sanserif")) {
fontFamily = "sans-serif";
}
int fontSize = 1000 * f.getSize();
String style = f.isItalic() ? "italic" : "normal";
int weight = f.isBold() ? Font.BOLD : Font.NORMAL;
FontTriplet triplet = fontInfo.findAdjustWeight(fontFamily, style, weight);
if (triplet == null) {
triplet = fontInfo.findAdjustWeight("sans-serif", style, weight);
}
return fontInfo.getFontInstance(triplet, fontSize);
}
private void establishCurrentFont() throws IOException {
if ((currentFontName != this.font.getFontName())
|| (currentFontSize != this.font.getFontSize())) {
PSGenerator gen = getPSGenerator();
gen.writeln(this.font.getFontName() + " "
+ gen.formatDouble(font.getFontSize() / 1000f) + " F");
currentFontName = this.font.getFontName();
currentFontSize = this.font.getFontSize();
}
}
/**
* Sets the overriding font.
* @param override Overriding Font to set
*/
public void setOverrideFont(Font override) {
this.overrideFont = override;
}
}
|