RFR: 8342530: Specifying "@Nx" scaling level in ImageStorage should only load that specific level
John Hendrikx
jhendrikx at openjdk.org
Thu May 15 20:21:58 UTC 2025
On Thu, 15 May 2025 20:10:53 GMT, John Hendrikx <jhendrikx at openjdk.org> wrote:
>> This follow-up change finishes the earlier changes to `ImageStorage.loadAll()` and adds support for loading specific scale requested in the input.
>>
>> `loadAll()` will now first check if the input path ends with a scaling level specified, and if that is the case it will attempt creating a Stream. If requested resource does not exist it will throw an Exception, skipping the rest of the load process. If the resource does _not_ have a scaled name in its path, it will continue loading as normal - looking for all scale levels, trying to load the main resource and falling back to trying to load "@1x" variant.
>>
>> Added tests to check the new `ImageTools.hasScaledName()` method + new behavior.
>
> modules/javafx.graphics/src/main/java/com/sun/javafx/iio/common/ImageTools.java line 166:
>
>> 164: return true;
>> 165: }
>> 166:
>
> 1. Is looking for a slash going to be compatible on all platforms? Where is the path string coming from?
> 2. Catching `NumberFormatException` to "check" if something is a number is bad form
> 3. It will allow `@0x` and `@-1x` etc...
> 4. Consider using a regular expression, it is much more concise and intended for this kind of matching
>
> Here's a regular expression for this:
>
> Pattern SCALED_PATTERN = Pattern.compile(".*@[1-9][0-9]?x(\.[^\.]+)?");
>
> The above will match any path that ends with `@` followed by a number from 1 to 99, followed by an `x`, optionally followed by an extension that does not contain a dot. No need to check for slashes.
If you want you can even return the scale with a slightly altered pattern:
private static final Pattern SCALED_PATTERN = Pattern.compile(".*@([1-9][0-9]?)x(?:\.[^\.]+)?");
Then do:
Matcher matcher = SCALED_PATTERN.matcher(path);
if (matcher.matches()) {
return Integer.parseInt(matcher.group(1)); // can't throw NumberFormatException, number is validated by pattern
}
return 0; // there was no scale
-------------
PR Review Comment: https://git.openjdk.org/jfx/pull/1809#discussion_r2091898708
More information about the openjfx-dev
mailing list