tags:

views:

21

answers:

1

If i write a text on SWT say "this text" in a particular font/style , i want the individual coords of 't' 'h' 'i' 's' and so on. Is there any way to obtain the same.

A: 
Text text = new Text(parent, SWT.NONE);
text.setText("This");

GC gc = new GC(text);
int currentPixelsX = text.getBounds().x;
int currentPixelsY = text.getBounds().y;
for (int i = 0; i < text.getText().length(); i++) {
 System.out.println("X co-ordinate of " + text.getText().charAt(i) + ":" + currentPixelsX);
 System.out.println("Y co-ordinate of " + text.getText().charAt(i) + ":" + currentPixelsY);

 currentPixelsX = currentPixelsX + gc.getCharWidth(text.getText().charAt(i));
}

This gives the the pixel co-ordinates with respect to the parent of the text box.

Rusty
The solution you are suggesting does not accomodate for char spacing/word spacing adjustments of GDI.
Jagan