Kieran France Kieran France is a programmer for IDRSolutions in charge of there internal test suite. In his spare time he enjoys tinkering with gadgets and code.

Why Change To Path2D Bounds in Java 19

1 min read

Java 19 vs 18

We have recently completed testing our products on Java 19. As of its release BuildVu, FormVu, JDeli, and JPedal will officially support Java 19.

Whilst testing we did find one change in this version that we thought it worth documenting. In the class Path2D (both Path2D.Float and Path2D.Double) the getBounds() methods return value is changed subtly.

What’s different in Java 19?

Before Java19 Path2D.getBounds would return a Rectangle2D as either a Rectangle2D.Float or a Rectangle.Double. This was based on which Path2D variant you are using.

As of Java19 the Float/Double variants of getBounds() call the same static method to generate the Rectangle2D of the bounds and always returns Rectangle2D.Double.

Why is this interesting?

We have an optimisation to ignore clips in BuildVu output if the shape and clip are rectangular and the shape is completely contained within a clip. This is because in this case the clip has no effect in that case. However different object types would return clip or shape bounds as a Rectangle2D.Float or Rectangle2D.Double.

Due to the use of both Floats and Doubles being handled there is a niche case that may occur. Slight value changes due to the way float and double values are handled can take place. In these cases identical shape and clip values can be shifted resulting in them not being identical. When this happens our code thinks the shape crosses the clip bounds and therefore needs to be clipped.

How do we benefit from this?

In some uses the clip that is applied means we lose a sliver of content and a small gap between content appears which most commonly appears as a fine white line.

Now in Java19 both bounds are returned as Double, the values remain consistent and any shift in the value happens for both the shape and the clip. We can now correctly detect the clip isn’t needed so we don’t lose the sliver and the gap is gone.

For example the coloured bands below have clips identical to the bands and should be ignored. The bands should all be touching with no gap. In Java 18 and lower the clips are applied (image on the left), in Java 19 they are ignored to the sliver isn’t lost (image on the right).

Image showing 3 touching coloured lines which have a clip producing a gap between them
Java 18 the clip is applied
Image showing 3 touching coloured lines which have not clip gap between
Java 19 the clip is not applied

 

 

This is a niche case but it has improved the appearance of several files in our baseline.



View PDF files in a web application →

Parse PDF files as HTML →

Display PDF Forms in a web browser →

View PDF Documents in Java Applications →

Rasterize PDF Documents to image →

Read/Write images (including HEIC, JPG and WEBP) →

Convert Image files to PDF Documents →

Kieran France Kieran France is a programmer for IDRSolutions in charge of there internal test suite. In his spare time he enjoys tinkering with gadgets and code.

Leave a Reply

Your email address will not be published. Required fields are marked *

IDRsolutions Ltd 2022. All rights reserved.