Skip to content

Commit

Permalink
Balanced linespacing above and below text for better highlighting loo…
Browse files Browse the repository at this point in the history
…k. (#872)
  • Loading branch information
Jugen committed Nov 22, 2019
1 parent 5a11be7 commit ecd2861
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,8 @@ public void setLineHighlighterOn( boolean show )
{
int p = getCurrentParagraph();
int start = getCurrentLineStartInParargraph();
int end = getCurrentLineEndInParargraph() + 1; // +1 for empty lines
int end = getCurrentLineEndInParargraph();
if (end == 0) end++;// +1 for empty lines
lineHighlighter.selectRange( p, start, p, end );
}
};
Expand Down
14 changes: 5 additions & 9 deletions richtextfx/src/main/java/org/fxmisc/richtext/ParagraphBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,6 @@ public final ObservableMap<Selection<PS, SEG, S>, SelectionPath> selectionsPrope
this.text = new ParagraphText<>(par, nodeFactory);
applyParagraphStyle.accept(this.text, par.getParagraphStyle());

// Apply line-spacing (in a paragraph) to between paragraphs as well. Can be overriden with -fx-padding CSS.
text.lineSpacingProperty().addListener( (ob,ov,nv) -> setPadding( new Insets( 0, 0, nv.doubleValue(), 0 ) ) );

// start at -1 so that the first time it is displayed, the caret at pos 0 is not
// accidentally removed from its parent and moved to this node's ParagraphText
// before this node gets updated to its real index and therefore removes
Expand Down Expand Up @@ -240,21 +237,20 @@ protected double computePrefWidth(double ignoredHeight) {
protected double computePrefHeight(double width) {
Insets insets = getInsets();
double overhead = getGraphicPrefWidth() + insets.getLeft() + insets.getRight();
return text.prefHeight(width - overhead) + insets.getTop() + insets.getBottom();
return text.prefHeight(width - overhead) + insets.getTop() + insets.getBottom() + text.getLineSpacing();
}

@Override
protected
void layoutChildren() {
protected void layoutChildren() {
Insets ins = getInsets();
double w = getWidth() - ins.getLeft() - ins.getRight();
double h = getHeight() - ins.getTop() - ins.getBottom();
double graphicWidth = getGraphicPrefWidth();
double half = text.getLineSpacing() / 2.0;

text.resizeRelocate(graphicWidth + ins.getLeft(), ins.getTop(), w - graphicWidth, h);
text.resizeRelocate(graphicWidth + ins.getLeft(), ins.getTop() + half, w - graphicWidth, h - half);

graphic.ifPresent(g -> g.resizeRelocate(graphicOffset.get() + ins.getLeft(), ins.getTop(), graphicWidth, h + ins.getBottom()));
// h + ins.getBottom() so that there aren't gaps when -fx-line-spacing or -fx-padding is active.
graphic.ifPresent(g -> g.resizeRelocate(graphicOffset.get() + ins.getLeft(), ins.getTop(), graphicWidth, h));
}

double getGraphicPrefWidth() {
Expand Down
18 changes: 17 additions & 1 deletion richtextfx/src/main/java/org/fxmisc/richtext/ParagraphText.java
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ private PathElement[] getRangeShapeSafely(int start, int end) {
} else {
shape = getRangeShape(start, paragraph.length());
// Since this might be a wrapped multi-line paragraph,
// there may be multiple groups of (1 MoveTo, 3 LineTo objects) for each line:
// there may be multiple groups of (1 MoveTo, 4 LineTo objects) for each line:
// MoveTo(topLeft), LineTo(topRight), LineTo(bottomRight), LineTo(bottomLeft)

// We only need to adjust the top right and bottom right corners to extend to the
Expand All @@ -378,6 +378,22 @@ private PathElement[] getRangeShapeSafely(int start, int end) {
}
}

if ( getLineSpacing() > 0 ) {
double half = getLineSpacing() / 2.0;
for ( int g = 0; g < shape.length; g += 5 ) {
MoveTo tl = (MoveTo) shape[g];
tl.setY( tl.getY()-half );
LineTo tr = (LineTo) shape[g+1];
tr.setY( tl.getY() );
LineTo br = (LineTo) shape[g+2];
br.setY( br.getY()+half );
LineTo bl = (LineTo) shape[g+3];
bl.setY( br.getY() );
LineTo t2 = (LineTo) shape[g+4];
t2.setY( tl.getY() );
}
}

if (getLineCount() > 1) {
// adjust right corners of wrapped lines
boolean wrappedAtEndPos = (end > 0 && getLineOfCharacter(end) > getLineOfCharacter(end - 1));
Expand Down

0 comments on commit ecd2861

Please sign in to comment.