int[] result = new int[2];
result[0] = start.getHit().getCharIndex();
result[1] = finish.getHit().getCharIndex();
// this next bit is to make sure that ligatures are selected properly
TextSpanLayout startLayout = start.getLayout();
TextSpanLayout finishLayout = finish.getLayout();
int startGlyphIndex = startLayout.getGlyphIndex(result[0]);
int finishGlyphIndex = finishLayout.getGlyphIndex(result[1]);
int startCharCount = startLayout.getCharacterCount(startGlyphIndex, startGlyphIndex);
int finishCharCount = finishLayout.getCharacterCount(finishGlyphIndex, finishGlyphIndex);
if (startCharCount > 1) {
if (result[0] > result[1] && startLayout.isLeftToRight()) {
result[0] += startCharCount-1;
} else if (result[1] > result[0] && !startLayout.isLeftToRight()) {
result[0] -= startCharCount-1;
}
}
if (finishCharCount > 1) {
if (result[1] > result[0] && finishLayout.isLeftToRight()) {
result[1] += finishCharCount-1;
} else if (result[0] > result[1] && !finishLayout.isLeftToRight()) {
result[1] -= finishCharCount-1;
}
}
return result;