JDK-8317122 : RFE: TextFlow.preferredHeight ignores lineSpacing
  • Type: Enhancement
  • Component: javafx
  • Sub-Component: graphics
  • Affected Version: jfx21
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2023-09-27
  • Updated: 2023-09-29
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, preferred height calculation does not include the line spacing amount for the last line (see the screenshot and notice zero space between "line four" and "Next Component...").

While this looks like a bug in the context of rich text area where multiple TextFlows are stacked vertically, the workaround is to simply add the lineSpacing amount to the height of each paragraph which is fairly trivial.  Another argument might be to treat the lineSpacing similarly to how whitespace is treated when wrapping text (we don't want to have leading or trailing whitespace there, see JDK-8314215 for example).

The main reason for this ticket is to initiate a discussion.




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
Clearly it looks wrong for stacked textflow, but equally clearly (to me) it would be wrong for a single TextFlow. So no universally correct default. Another property is possible but (1) is it an important enough case (2) remember lineSpacing starts with the Text node so it might need to be there too.
29-09-2023