* @see #setClip
*/
public void drawString(String s, float x, float y) {
preparePainting();
Font fontState;
AffineTransform fontTransform = null;
if (ovFontState == null) {
java.awt.Font gFont = getFont();
fontTransform = gFont.getTransform();
String n = gFont.getFamily();
if (n.equals("sanserif")) {
n = "sans-serif";
}
float siz = gFont.getSize2D();
String style = gFont.isItalic() ? "italic" : "normal";
int weight = gFont.isBold() ? Font.WEIGHT_BOLD : Font.WEIGHT_NORMAL;
FontTriplet triplet = fontInfo.fontLookup(n, style, weight);
fontState = fontInfo.getFontInstance(triplet, (int)(siz * 1000 + 0.5));
} else {
fontState = fontInfo.getFontInstance(
ovFontState.getFontTriplet(), ovFontState.getFontSize());
ovFontState = null;
}
String name;
float size;
name = fontState.getFontName();
size = (float)fontState.getFontSize() / 1000f;
if ((!name.equals(this.currentFontName))
|| (size != this.currentFontSize)) {
this.currentFontName = name;
this.currentFontSize = size;
currentStream.write("/" + name + " " + size + " Tf\n");
}
currentStream.write("q\n");
Color c = getColor();
applyColor(c, true);
applyPaint(getPaint(), true);
int salpha = c.getAlpha();
if (salpha != 255) {
checkTransparencyAllowed();
Map vals = new java.util.HashMap();
vals.put(PDFGState.GSTATE_ALPHA_NONSTROKE, new Float(salpha / 255f));
PDFGState gstate = pdfDoc.getFactory().makeGState(
vals, graphicsState.getGState());
resourceContext.addGState(gstate);
currentStream.write("/" + gstate.getName() + " gs\n");
}
Map kerning = null;
boolean kerningAvailable = false;
kerning = fontState.getKerning();
if (kerning != null && !kerning.isEmpty()) {
kerningAvailable = true;
}
// This assumes that *all* CIDFonts use a /ToUnicode mapping
boolean useMultiByte = false;
org.apache.fop.fonts.Typeface f =
(org.apache.fop.fonts.Typeface)fontInfo.getFonts().get(name);
if (f instanceof LazyFont) {
if (((LazyFont) f).getRealFont() instanceof CIDFont) {
useMultiByte = true;
}
} else if (f instanceof CIDFont) {
useMultiByte = true;
}
// String startText = useMultiByte ? "<FEFF" : "(";
String startText = useMultiByte ? "<" : "(";
String endText = useMultiByte ? "> " : ") ";
AffineTransform trans = getTransform();
//trans.translate(x, y);
double[] vals = new double[6];
trans.getMatrix(vals);
concatMatrix(vals);
Shape imclip = getClip();
writeClip(imclip);
currentStream.write("BT\n");
AffineTransform localTransform = new AffineTransform();
localTransform.translate(x, y);
if (fontTransform != null) {
localTransform.concatenate(fontTransform);
}
localTransform.scale(1, -1);
double[] lt = new double[6];
localTransform.getMatrix(lt);
currentStream.write(PDFNumber.doubleOut(lt[0]) + " "
+ PDFNumber.doubleOut(lt[1]) + " " + PDFNumber.doubleOut(lt[2]) + " "
+ PDFNumber.doubleOut(lt[3]) + " " + PDFNumber.doubleOut(lt[4]) + " "
+ PDFNumber.doubleOut(lt[5]) + " Tm [" + startText);
int l = s.length();
for (int i = 0; i < l; i++) {
char ch = fontState.mapChar(s.charAt(i));
if (!useMultiByte) {
if (ch > 127) {
currentStream.write("\\");
currentStream.write(Integer.toOctalString((int)ch));
} else {
switch (ch) {
case '(':
case ')':
case '\\':
currentStream.write("\\");
break;
}
currentStream.write(ch);
}
} else {
currentStream.write(PDFText.toUnicodeHex(ch));
}
if (kerningAvailable && (i + 1) < l) {
addKerning(currentStream, (new Integer((int)ch)),
(new Integer((int)fontState.mapChar(s.charAt(i + 1)))),
kerning, startText, endText);
}
}
currentStream.write(endText);