RFR: 8341670: [Text, TextFlow] Public API for Text Layout Info [v18]
Michael Strauß
mstrauss at openjdk.org
Wed May 21 23:52:00 UTC 2025
On Mon, 10 Mar 2025 16:08:31 GMT, Andy Goryachev <angorya at openjdk.org> wrote:
>> modules/javafx.graphics/src/main/java/com/sun/javafx/scene/text/TextLayout.java line 256:
>>
>>> 254: * @return the caret geometry
>>> 255: */
>>> 256: public float[] getCaretGeometry(int offset, boolean leading);
>>
>> Does this method throw any exceptions? If so, please specify.
>
> This method does not throw any exceptions, so none are specified.
Instead of returning `float[]` here, this would be a good candidate to return a discriminated union instead, something like:
sealed interface CaretGeometry {
record Single(float x, float y, float height) implements CaretGeometry {}
record Split(float x1, float x2, float y, float height) implements CaretGeometry {}
}
This has two advantages:
1. It's more expressive, as you don't need to look up what the array values represent.
2. Instead of disciminating between the two cases by inspecting the array length as you do in `PrismLayoutInfo::caretInfoAt`:
```java
Rectangle2D[] parts;
if (c.length == 3) {
..
parts = ...
} else {
...
parts = ...
}
```
...you can use an exhaustive switch expression instead:
```
Rectangle2D[] parts = switch (c) {
case Single s -> new Rectangle2D[] {
new Rectangle2D(s.x() + dx, s.y() + dy, 0, s.height())
};
case Split s -> new Rectangle2D[] {
new Rectangle2D(...),
new Rectangle2D(...)
};
};
```
This is easier to read, and you'll get the compiler checks for free.
-------------
PR Review Comment: https://git.openjdk.org/jfx/pull/1596#discussion_r2101366202
More information about the openjfx-dev
mailing list