AffineTransform trans = ((WinGDIGraphics2D)g).getTransform();
x += (int)Math.round(trans.getTranslateX());
y += (int)Math.round(trans.getTranslateY());
WindowsFont wf = (WindowsFont)(g.getFont().getPeer());
long font = wf.getFontHandle();
long gi = ((WinGDIGraphics2D)g).getGraphicsInfo();
long hdc = ((WinGDIGraphics2D)g).getDC();
win32.SelectObject(hdc, font);
win32.SetTextColor(hdc, getGDIColor(g.getColor().getRGB()));
win32.SetBkMode(hdc, WindowsDefs.TRANSPARENT);
/*
* Work around for escape-subsequences. If e.g. we draw
* string "\n\uFFFF" - instead of default glyph for \uFFFF
* GDI draws small vertical rectangle. For this reason we draw
* chars one by one to avoid this situation.
*
* GDI draws all glyphs starting from 0. First 32 glyphs are taken from
* another font, hence we have to check if the glyph exists in chosen
* font and if success draw it, otherwise we draw default glyph,
* except esc-subsequence chars.
* */
char[] chars = new char[str.length()];
int j = 0;
for (int i=0; i < str.length(); i++){
char c = str.charAt(i);
Glyph gl = wf.getGlyph(c);
// We compare Advances because Width
// of a char can be 0 (e.g. "Space" char)
if (gl.getGlyphMetrics().getAdvance() != 0){
chars[j] = gl.getChar();
j++;
}
}
String out = new String(chars, 0, j);
win32.TextOutW(hdc, x, y - wf.getAscent(), out, j);
}