<Swing Dev> RFR: 8231286: HTML font size too large with high-DPI scaling and W3C_UNIT_LENGTHS
Matthias Perktold
github.com+7334069+mperktold at openjdk.java.net
Wed Dec 9 14:54:36 UTC 2020
On Wed, 9 Dec 2020 03:34:14 GMT, Sergey Bylokhov <serb at openjdk.org> wrote:
>> Issue is when using a JEditorPane to render HTML views with W3C_UNIT_LENGTHS enabled, font-sizes set using CSS are much larger than the same font size outside the HTML.
>> It's because CSS LengthUnit uses screen resolution to calculate units so for hidpi screens, the html font size is bigger.
>> Fix is to calculate the units based on the CSS absolute length mentioned in https://drafts.csswg.org/css-values-3/#absolute-lengths so hidpi scaling is not applied twice in CSS and again by Java.
>
> src/java.desktop/share/classes/javax/swing/text/html/CSS.java line 2867:
>
>> 2865:
>> 2866: // mapping according to the CSS2 spec
>> 2867: // https://drafts.csswg.org/css-values-3/#absolute-lengths
>
> Are you sure that you can get the exact values of these absolute lengths without using a DPI of the screen? I mean that these values can be measured with a ruler on the screen and they must correspond to "cm" is one centimeter, and "in" is one "inch" etc.
>
> BTW how the browsers handle that?
I opened this issue and suggested this fix because it is in line with how `px` were handled already, and fixes convertions between the various units.
> Are you sure that you can get the exact values of these absolute lengths without using a DPI of the screen?
It depends on how Java handles hi-DPI awareness in general, which I am not aware of.
Looking at the sources again, I've found `java.awt.GraphicsConfiguration#getDefaultTransform()` and `java.awt.GraphicsConfiguration#getNormalizingTransform()`, both of which are apparently related to hi-DPI awareness.
I think `getDefaultTransform` is usually used by the Runtime, and `getNormalizingTransform` is what we are looking for, acording to its JavaDoc:
> Returns an AffineTransform that can be concatenated with the default AffineTransform of a GraphicsConfiguration so that 72 units in user space equals 1 inch in device space. [...]
I was quite happy with the results without considering `getNormalizingTransform`, but maybe I was just lucky that it corresponds to the identity transformation for my setup.
> BTW how the browsers handle that?
Browsers have a `devicePixelRatio` indicating how many native device pixels form one CSS pixel (i.e. `px`).
On hi-DPI device, this value is greater than 1, e.g. 2, otherwise it is 1. It depends on the pixel density as well as on the usual viewing distance of the device.
The convertion between the various units is always a constant factor. Units such as `cm` or `in` are never precise, and thus not widely used.
-------------
PR: https://git.openjdk.java.net/jdk/pull/1628
More information about the swing-dev
mailing list