Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 42 additions & 22 deletions richtextfx/src/main/java/org/fxmisc/richtext/ParagraphText.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ public ObjectProperty<Paint> highlightTextFillProperty() {

private final Path caretShape = new Path();
private final Path selectionShape = new Path();
private final List<Path> backgroundShapes = new ArrayList<>();
private final List<Path> underlineShapes = new ArrayList<>();
private final List<Path> backgroundShapes;
private final List<Path> underlineShapes;
private final Val<Double> leftInset;
private final Val<Double> topInset;

// proxy for caretShape.visibleProperty() that implements unbind() correctly.
// This is necessary due to a bug in BooleanPropertyBase#unbind().
Expand All @@ -67,8 +69,8 @@ public ObjectProperty<Paint> highlightTextFillProperty() {

selection.addListener((obs, old, sel) -> requestLayout());

Val<Double> leftInset = Val.map(insetsProperty(), Insets::getLeft);
Val<Double> topInset = Val.map(insetsProperty(), Insets::getTop);
leftInset = Val.map(insetsProperty(), Insets::getLeft);
topInset = Val.map(insetsProperty(), Insets::getTop);

// selection highlight
selectionShape.setManaged(false);
Expand All @@ -95,30 +97,20 @@ public ObjectProperty<Paint> highlightTextFillProperty() {
// text.impl_selectionFillProperty().set(newFill);
// }
// });
int size = par.getSegments().size();
backgroundShapes = new ArrayList<>(size);
underlineShapes = new ArrayList<>(size);

// populate with text nodes
for(SEG segment: par.getSegments()) {
// create Segment
Node fxNode = nodeFactory.apply(segment);
getChildren().add(fxNode);

// add corresponding background node (empty)
Path backgroundShape = new Path();
backgroundShape.setManaged(false);
backgroundShape.setStrokeWidth(0);
backgroundShape.layoutXProperty().bind(leftInset);
backgroundShape.layoutYProperty().bind(topInset);
backgroundShapes.add(backgroundShape);
getChildren().add(0, backgroundShape);
// add placeholder to prevent IOOBE; only create shapes when needed
backgroundShapes.add(null);
underlineShapes.add(null);

// add corresponding underline node (empty)
Path underlineShape = new Path();
underlineShape.setManaged(false);
underlineShape.setStrokeWidth(0);
underlineShape.layoutXProperty().bind(leftInset);
underlineShape.layoutYProperty().bind(topInset);
underlineShapes.add(underlineShape);
getChildren().add(underlineShape);
}
}

Expand Down Expand Up @@ -221,6 +213,20 @@ private void updateBackgroundShapes() {
}
}

private Path getBackgroundShape(int index) {
Path backgroundShape = backgroundShapes.get(index);
if (backgroundShape == null) {
// add corresponding background node (empty)
backgroundShape = new Path();
backgroundShape.setManaged(false);
backgroundShape.setStrokeWidth(0);
backgroundShape.layoutXProperty().bind(leftInset);
backgroundShape.layoutYProperty().bind(topInset);
backgroundShapes.set(index, backgroundShape);
getChildren().add(0, backgroundShape);
}
return backgroundShape;
}

/**
* Updates the background shape for a text segment.
Expand All @@ -234,7 +240,7 @@ private void updateBackground(TextExt text, int start, int end, int index) {
// Set fill
Paint paint = text.backgroundColorProperty().get();
if (paint != null) {
Path backgroundShape = backgroundShapes.get(index);
Path backgroundShape = getBackgroundShape(index);
backgroundShape.setFill(paint);

// Set path elements
Expand All @@ -243,6 +249,20 @@ private void updateBackground(TextExt text, int start, int end, int index) {
}
}

private Path getUnderlineShape(int index) {
Path underlineShape = underlineShapes.get(index);
if (underlineShape == null) {
// add corresponding underline node (empty)
underlineShape = new Path();
underlineShape.setManaged(false);
underlineShape.setStrokeWidth(0);
underlineShape.layoutXProperty().bind(leftInset);
underlineShape.layoutYProperty().bind(topInset);
underlineShapes.set(index, underlineShape);
getChildren().add(underlineShape);
}
return underlineShape;
}

/**
* Updates the shape which renders the text underline.
Expand All @@ -257,7 +277,7 @@ private void updateUnderline(TextExt text, int start, int end, int index) {
Number underlineWidth = text.underlineWidthProperty().get();
if (underlineWidth != null && underlineWidth.doubleValue() > 0) {

Path underlineShape = underlineShapes.get(index);
Path underlineShape = getUnderlineShape(index);
underlineShape.setStrokeWidth(underlineWidth.doubleValue());

// get remaining CSS properties for the underline style
Expand Down