use of com.sun.javafx.geom.RectBounds in project JFoenix by jfoenixadmin.
the class JFXHighlighter method getMatchingBounds.
private ArrayList<Bounds> getMatchingBounds(String query, Text text) {
// find local text bounds in parent
Bounds textBounds = parent.sceneToLocal(text.localToScene(text.getBoundsInLocal()));
ArrayList<Bounds> rectBounds = new ArrayList<>();
TextLayout textLayout = null;
try {
textLayout = (TextLayout) textLayoutMethod.invoke(text);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
int queryLength = query.length();
TextLine[] lines = textLayout.getLines();
// handle matches in all lines
for (int i = 0; i < lines.length; i++) {
TextLine line = lines[i];
String lineText = text.getText().substring(line.getStart(), line.getStart() + line.getLength());
final String lineTextLow = lineText.toLowerCase();
final String queryLow = query.toLowerCase();
int beginIndex = lineTextLow.indexOf(queryLow);
if (beginIndex == -1) {
continue;
}
RectBounds lineBounds = (line.getBounds());
// compute Y layout
double height = Math.round(lineBounds.getMaxY()) - Math.round(lineBounds.getMinY());
double startY = height * i;
// handle multiple matches in one line
while (beginIndex != -1) {
// compute X layout
Text temp = new Text(lineText.substring(beginIndex, beginIndex + queryLength));
temp.setFont(text.getFont());
temp.applyCss();
double width = temp.getLayoutBounds().getWidth();
temp.setText(lineText.substring(0, beginIndex + queryLength));
temp.applyCss();
double maxX = temp.getLayoutBounds().getMaxX();
double startX = maxX - width;
rectBounds.add(new BoundingBox(textBounds.getMinX() + startX, textBounds.getMinY() + startY, width, temp.getLayoutBounds().getHeight()));
beginIndex = lineTextLow.indexOf(queryLow, beginIndex + queryLength);
}
}
return rectBounds;
}
Aggregations