JDK-8317120 : RFE: TextFlow.rangeShape() ignores lineSpacing
  • Type: Enhancement
  • Component: javafx
  • Sub-Component: graphics
  • Affected Version: jfx21
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2023-09-27
  • Updated: 2023-10-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Cloners :  
Relates :  
Description
Problem:

When TextFlow has non-zero lineSpacing property set, the shape returned by TextFlow.rangeShape() does not include the line spacing (notice the yellow highlight in the screenshot).  This presents an issue for implementing a rich text area where the selection shape is obtained by calling rangeShape().

Compare this to what MS Word does where selection highlight does include line spacing (word.png).

This might be considered a bug or an enhancement, depending on whether we want to do.  The main reason for this ticket is to initiate a discussion.

We might have at least three options:

- provide an alternative method that accepts may be a boolean option to control whether lineSpacing should be included
- modify the existing method to include lineSpacing (raised a backward compatibility alert)
- let the application code derive a new shape that includes lineSpacing


SCCE:

```
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Background;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Path;
import javafx.scene.shape.PathElement;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;

public class TextFlow_RangeShape extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        String text = "TextFlow{lineSpacing=20}\nline two\nline three\nline four";
        TextFlow t = new TextFlow(new Text(text));
        t.setLineSpacing(20);
        
        Path path = new Path();
        path.setManaged(false);
        path.setFill(Color.rgb(255, 255, 128, 0.5));
        path.setStroke(Color.rgb(128, 128, 0));
        
        Label label = new Label("Next Component Starts Here");
        label.setOpacity(1.0);
        label.setBackground(Background.fill(Color.DARKGRAY));
        
        VBox vb = new VBox();
        vb.getChildren().addAll(path, t, label);
        VBox.setVgrow(label, Priority.ALWAYS);

        Scene scene = new Scene(vb);
        stage.setScene(scene);
        stage.setWidth(400);
        stage.setHeight(200);
        stage.setTitle("TextFlow.rangeShape() ignores lineSpacing");
        stage.show();
        
        Platform.runLater(() -> {
            PathElement[] pe = t.rangeShape(0, text.length());
            path.getElements().setAll(pe);
        });
    }
}
```
Comments
Thank you for the analysis, Phil. #3 is likely not that hard, but it relies on undocumented return value of rangeShape(). Looks like it's a sequence of MoveTo/LineTo path elements that represent (possibly disjoint) rectangular shapes, so it's fairly trivial to implement. #4 something tells me this will be the most likely outcome ;-)
29-09-2023

I'm undecided on this >We might have at least three options: #1 - provide an alternative method that accepts may be a boolean option to control whether lineSpacing should be included Possible, most compatible. #2 - modify the existing method to include lineSpacing (raised a backward compatibility alert) Not sure how many apps will actually depend on this, but the real question for me on this, is whether this is what the answer really ought to be ? ie was it an out and out bug to exclude it ? What actual problem arises other than a view on what highlighting ought to look like ? If you copy and paste the selected text, the line spacing isn't part of what you paste, except perhaps in the context of rich text. #3 - let the application code derive a new shape that includes lineSpacing Is this hard to do ? And #4 .. do nothing.
29-09-2023