// Mismatch between actual painting with gc.drawString() and the gc.stringExtent() // info, for e.g., chars '\u0001' - \u0006'. The extent returned is larger than the // actual extent painted. // // This makes it impossible e.g., to match a mouse-click's x offset in the window // to the correct index inside the displayed text string. // // These characters seem to be filtered out somehow by StyledText's rendering // (they have a width of 0 pixels on the display)?! What is the full set of these // characters? import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.widgets.*; public class Main { public static void main (String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.open(); GC gc = new GC(shell); Font font = new Font(display, "Courier", 10, SWT.NORMAL); gc.setFont(font); String r = "*23456789012345678901234567890....+....01234567890"; String x1 = "0....+....01234567890"; String x2 = "....01234567890"; String x3 = "890"; int extr = gc.stringExtent(r).x; int ext1 = gc.stringExtent(x1).x; int ext2 = gc.stringExtent(x2).x; int ext3 = gc.stringExtent(x3).x; gc.drawString(r, 0, 0); for (int i = 0; i < 50; i++) { int x = gc.stringExtent(r.substring(0,i)).x; gc.drawLine(x, 15, x, 25); gc.drawString(String.valueOf(r.charAt(i)), x, 30); } gc.drawString(x1, 0, 60); for (int i = 0; i < 50; i++) { int x = gc.stringExtent(x1.substring(0,i)).x; gc.drawLine(x, 75, x, 85); gc.drawString(String.valueOf(x1.charAt(i)), x, 90); } gc.drawString(x2, 0, 120); for (int i = 0; i < 50; i++) { int x = gc.stringExtent(x2.substring(0,i)).x; gc.drawLine(x, 135, x, 145); gc.drawString(String.valueOf(x2.charAt(i)), x, 150); } gc.drawString(x3, 0, 180); for (int i = 0; i < 50; i++) { int x = gc.stringExtent(x3.substring(0,i)).x; gc.drawLine(x, 195, x, 205); gc.drawString(String.valueOf(x3.charAt(i)), x, 210); } gc.drawString("*** stringExtent()s: " + extr + ' ' + ext1 + ' ' + ext2 + ' ' + ext3, 0, 240); gc.dispose (); while (!shell.isDisposed ()) { if (!display.readAndDispatch ()) display.sleep (); } display.dispose (); } }