Skip to content

Commit 87b874e

Browse files
committed
Refactored for updated API
1 parent 6b13bfa commit 87b874e

File tree

3 files changed

+14
-27
lines changed

3 files changed

+14
-27
lines changed

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

+11-8
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class TextFlowExt extends TextFlow {
2121
/*
2222
* Rename to getLayoutInfo() and delete once JavaFX
2323
* [PR1596](https://github.com/openjdk/jfx/pull/1596)
24-
* is integrated and released. Also delete
24+
* is integrated and released. Also delete
2525
* TextFlowLayout and TextFlowSpan.
2626
*/
2727
private TextFlowLayout textLayout()
@@ -37,17 +37,20 @@ int getLineCount() {
3737
}
3838

3939
int getLineStartPosition(int charIdx) {
40-
return textLayout().getTextLineStart( getLineOfCharacter(charIdx) );
40+
return textLayout().getTextLine( getLineOfCharacter(charIdx) ).getStart();
4141
}
4242

4343
int getLineEndPosition(int charIdx) {
44-
return textLayout().getTextLineEnd( getLineOfCharacter(charIdx) );
44+
int line = getLineOfCharacter( charIdx );
45+
int end = textLayout().getTextLine( line ).getEnd();
46+
if ( line < (getLineCount() - 1) ) end--; // trailing space
47+
return end;
4548
}
4649

4750
int getLineOfCharacter(int charIdx) {
4851
var layout = textLayout();
4952
return IntStream.range( 0, getLineCount() )
50-
.filter( l -> charIdx <= layout.getTextLineEnd( l ) )
53+
.filter( l -> charIdx < layout.getTextLine( l ).getEnd() )
5154
.findFirst().orElse( Math.max(0,getLineCount()-1) );
5255
}
5356

@@ -135,22 +138,22 @@ PathElement[] getUnderlineShape(int from, int to, double offset, double waveRadi
135138
}
136139

137140
CharacterHit hitLine(double x, int lineIndex) {
138-
Rectangle2D r = textLayout().getLineBounds( lineIndex );
141+
Rectangle2D r = textLayout().getTextLine( lineIndex ).getBounds();
139142
double y = r.getMinY() + r.getHeight() / 2.0;
140143
return hit( x, y, lineIndex );
141144
}
142145

143146
CharacterHit hit(double x, double y) {
144147
var layout = textLayout();
145148
int line = IntStream.range( 0, getLineCount() )
146-
.filter( l -> y < layout.getLineBounds( l ).getMaxY() )
149+
.filter( l -> y < layout.getTextLine( l ).getBounds().getMaxY() )
147150
.findFirst().orElse( Math.max(0,getLineCount()-1) );
148151
return hit( x, y, line );
149152
}
150153

151154
CharacterHit hit(double x, double y, int line) {
152155

153-
Rectangle2D lineBounds = textLayout().getLineBounds( line );
156+
Rectangle2D lineBounds = textLayout().getTextLine( line ).getBounds();
154157
HitInfo hit = hitTest(new Point2D(x, y));
155158
int charIdx = hit.getCharIndex();
156159
boolean leading = hit.isLeading();
@@ -162,7 +165,7 @@ CharacterHit hit(double x, double y, int line) {
162165
if ( ! leading && getLineCount() > 1) {
163166
// If this is a wrapped paragraph and hit character is at end of hit line, make sure that the
164167
// "character hit" stays at the end of the hit line (and not at the beginning of the next line).
165-
leading = (getLineOfCharacter(charIdx) + 1 < getLineCount() && charIdx + 1 >= textLayout().getTextLineEnd( line ));
168+
leading = (getLineOfCharacter(charIdx) + 1 < getLineCount() && charIdx + 1 >= textLayout().getTextLine( line ).getEnd());
166169
}
167170

168171
if(x < lineBounds.getMinX() || x > lineBounds.getMaxX()) {

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

+2-19
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import javafx.beans.Observable;
99
import javafx.geometry.Bounds;
1010
import javafx.geometry.Point2D;
11-
import javafx.geometry.Rectangle2D;
1211
import javafx.scene.Node;
1312
import javafx.scene.shape.LineTo;
1413
import javafx.scene.shape.MoveTo;
@@ -43,11 +42,11 @@ float getLineCenter( int lineNo ) {
4342

4443
@Deprecated
4544
int getLineLength( int lineNo ) {
46-
return getLineSpan( lineNo ).getLength();
45+
return getTextLine( lineNo ).getLength();
4746
}
4847

4948

50-
TextFlowSpan getLineSpan( int lineNo ) {
49+
TextFlowSpan getTextLine( int lineNo ) {
5150
return getTextLineCount() > 0 ? lineMetrics.get( lineNo ) : EMPTY_SPAN;
5251
}
5352

@@ -67,22 +66,6 @@ TwoLevelNavigator getTwoLevelNavigator() {
6766
}
6867

6968

70-
Rectangle2D getLineBounds( int line ) {
71-
return getLineSpan( line ).getBounds();
72-
}
73-
74-
int getTextLineStart( int line ) {
75-
return getLineSpan( line ).getStart();
76-
}
77-
78-
int getTextLineEnd( int line ) {
79-
TextFlowSpan span = getLineSpan( line );
80-
int end = span.getStart() + span.getLength();
81-
if ( line < (getTextLineCount() - 1) ) end--;
82-
return end;
83-
}
84-
85-
8669
/*
8770
* Iterate through the nodes in the TextFlow to determine the number of lines of text.
8871
* Also calculates the following metrics for each line along the way: line height,

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

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ float getCenterY() {
3232

3333
int getStart() { return start; }
3434
int getLength() { return length; }
35+
int getEnd() { return start + length; }
3536
double getHeight() { return height; }
3637
double getWidth() { return width; }
3738

0 commit comments

Comments
 (0)