Skip to content

Commit ecd2861

Browse files
authored
Balanced linespacing above and below text for better highlighting look. (#872)
1 parent 5a11be7 commit ecd2861

3 files changed

Lines changed: 24 additions & 11 deletions

File tree

richtextfx/src/main/java/org/fxmisc/richtext/GenericStyledArea.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,8 @@ public void setLineHighlighterOn( boolean show )
11871187
{
11881188
int p = getCurrentParagraph();
11891189
int start = getCurrentLineStartInParargraph();
1190-
int end = getCurrentLineEndInParargraph() + 1; // +1 for empty lines
1190+
int end = getCurrentLineEndInParargraph();
1191+
if (end == 0) end++;// +1 for empty lines
11911192
lineHighlighter.selectRange( p, start, p, end );
11921193
}
11931194
};

richtextfx/src/main/java/org/fxmisc/richtext/ParagraphBox.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,6 @@ public final ObservableMap<Selection<PS, SEG, S>, SelectionPath> selectionsPrope
9595
this.text = new ParagraphText<>(par, nodeFactory);
9696
applyParagraphStyle.accept(this.text, par.getParagraphStyle());
9797

98-
// Apply line-spacing (in a paragraph) to between paragraphs as well. Can be overriden with -fx-padding CSS.
99-
text.lineSpacingProperty().addListener( (ob,ov,nv) -> setPadding( new Insets( 0, 0, nv.doubleValue(), 0 ) ) );
100-
10198
// start at -1 so that the first time it is displayed, the caret at pos 0 is not
10299
// accidentally removed from its parent and moved to this node's ParagraphText
103100
// before this node gets updated to its real index and therefore removes
@@ -240,21 +237,20 @@ protected double computePrefWidth(double ignoredHeight) {
240237
protected double computePrefHeight(double width) {
241238
Insets insets = getInsets();
242239
double overhead = getGraphicPrefWidth() + insets.getLeft() + insets.getRight();
243-
return text.prefHeight(width - overhead) + insets.getTop() + insets.getBottom();
240+
return text.prefHeight(width - overhead) + insets.getTop() + insets.getBottom() + text.getLineSpacing();
244241
}
245242

246243
@Override
247-
protected
248-
void layoutChildren() {
244+
protected void layoutChildren() {
249245
Insets ins = getInsets();
250246
double w = getWidth() - ins.getLeft() - ins.getRight();
251247
double h = getHeight() - ins.getTop() - ins.getBottom();
252248
double graphicWidth = getGraphicPrefWidth();
249+
double half = text.getLineSpacing() / 2.0;
253250

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

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

260256
double getGraphicPrefWidth() {

richtextfx/src/main/java/org/fxmisc/richtext/ParagraphText.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ private PathElement[] getRangeShapeSafely(int start, int end) {
361361
} else {
362362
shape = getRangeShape(start, paragraph.length());
363363
// Since this might be a wrapped multi-line paragraph,
364-
// there may be multiple groups of (1 MoveTo, 3 LineTo objects) for each line:
364+
// there may be multiple groups of (1 MoveTo, 4 LineTo objects) for each line:
365365
// MoveTo(topLeft), LineTo(topRight), LineTo(bottomRight), LineTo(bottomLeft)
366366

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

381+
if ( getLineSpacing() > 0 ) {
382+
double half = getLineSpacing() / 2.0;
383+
for ( int g = 0; g < shape.length; g += 5 ) {
384+
MoveTo tl = (MoveTo) shape[g];
385+
tl.setY( tl.getY()-half );
386+
LineTo tr = (LineTo) shape[g+1];
387+
tr.setY( tl.getY() );
388+
LineTo br = (LineTo) shape[g+2];
389+
br.setY( br.getY()+half );
390+
LineTo bl = (LineTo) shape[g+3];
391+
bl.setY( br.getY() );
392+
LineTo t2 = (LineTo) shape[g+4];
393+
t2.setY( tl.getY() );
394+
}
395+
}
396+
381397
if (getLineCount() > 1) {
382398
// adjust right corners of wrapped lines
383399
boolean wrappedAtEndPos = (end > 0 && getLineOfCharacter(end) > getLineOfCharacter(end - 1));

0 commit comments

Comments
 (0)