public static void drawString(Graphics g, String s, int x, int y, double fontSize) throws IOException {
final GlyphTable glyphTable = ttf.getGlyphTable();
final CMapTable cmapTable = ttf.getCMapTable();
final HorizontalHeaderTable hheadTable = ttf.getHorizontalHeaderTable();
final HorizontalMetricsTable hmTable = ttf.getHorizontalMetricsTable();
if (!(cmapTable.getNrEncodingTables() > 0)) {
throw new RuntimeException("No Encoding is found!");
}
final CMapTable.EncodingTable encTable = cmapTable.getEncodingTable(0);
if (encTable.getTableFormat() == null) {
throw new RuntimeException("The table is NUll!!");
}
final int maxAdvance = hheadTable.getMaxAdvance();
final double ascent = hheadTable.getAscent();
final int descent = -hheadTable.getDescent();
final AffineTransform tx = new AffineTransform();
double scale = fontSize / ascent;
tx.translate(x, y + fontSize);
System.out.println("Scale=" + scale);
tx.scale(scale, -scale);
tx.translate(0, ascent);
final Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.GREEN);
g2.fill(new Rectangle2D.Double(x, y - ascent * scale, maxAdvance * scale, ascent * scale));
g2.setColor(Color.YELLOW);
g2.fill(new Rectangle2D.Double(x, y, maxAdvance * scale, descent * scale));
g2.setColor(Color.RED);
final GeneralPath gp = new GeneralPath();
for (int i = 0; i < s.length(); i++) {
// get the index for the needed glyph
final int index = encTable.getTableFormat().getGlyphIndex(s.charAt(i));
final TTFGlyph glyph = (TTFGlyph) glyphTable.getGlyph(index);
final GeneralPath shape = glyph.getShape();
gp.append(shape.getPathIterator(tx), false);
tx.translate(hmTable.getAdvanceWidth(index), 0);
}
g2.draw(gp);
}