Search in sources :

Example 1 with TextLine

use of com.sun.javafx.scene.text.TextLine 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;
}
Also used : TextLine(com.sun.javafx.scene.text.TextLine) RectBounds(com.sun.javafx.geom.RectBounds) Bounds(javafx.geometry.Bounds) Text(javafx.scene.text.Text) InvocationTargetException(java.lang.reflect.InvocationTargetException) CacheHint(javafx.scene.CacheHint) Paint(javafx.scene.paint.Paint) TextLayout(com.sun.javafx.scene.text.TextLayout) BoundingBox(javafx.geometry.BoundingBox) RectBounds(com.sun.javafx.geom.RectBounds)

Aggregations

RectBounds (com.sun.javafx.geom.RectBounds)1 TextLayout (com.sun.javafx.scene.text.TextLayout)1 TextLine (com.sun.javafx.scene.text.TextLine)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 BoundingBox (javafx.geometry.BoundingBox)1 Bounds (javafx.geometry.Bounds)1 CacheHint (javafx.scene.CacheHint)1 Paint (javafx.scene.paint.Paint)1 Text (javafx.scene.text.Text)1