Commit graph

19 commits

Author SHA1 Message Date
MacDue
360c0eb509 LibWeb: Remove implicit conversion from float and double to CSSPixels
In general it is not safe to convert any arbitrary floating-point value
to CSSPixels. CSSPixels has a resolution of 0.015625, which for small
values (e.g. scale factors between 0 and 1), can produce bad results
if converted to CSSPixels then scaled back up. In the worst case values
can underflow to zero and produce incorrect results.
2023-08-26 23:53:45 +02:00
Hendiadyoin1
b342b4dfb8 LibWeb: Fix a rounding issue on CSSPixels multiplication
Co-Authored-By: ronak69 <ronak69@danwin1210.de>
2023-08-18 17:58:44 +02:00
Aliaksandr Kalenik
e9a718ff88 LibWeb/Layout: Replace INFINITY with CSSPixels::min() or max() 2023-08-17 20:03:13 +02:00
Hendiadyoin1
ce188c9a9c LibWeb: Allow direct rouding of CSSPixelRects to CSSPixelRects
Preciously we were casting to float, round and cast back, which actually
might loose precision and was quite ugly.
2023-08-10 12:24:21 +02:00
MacDue
5f0d3c083f LibWeb: Allow (explicitly) converting CSSPixels to float and int
...and remove some unnecessary cast chains.
2023-08-08 14:58:26 +02:00
Hendiadyoin1
36dcb0ada7 LibWeb: Use int-hash for CSSPixels 2023-08-05 20:03:09 +02:00
Hendiadyoin1
6c341ca985 LibWeb: Implement rounding for PixelUnits 2023-08-05 20:03:09 +02:00
Hendiadyoin1
cc74083f27 LibWeb: Inline and constexpr most PixelUnits methods 2023-08-05 20:03:09 +02:00
Hendiadyoin1
1e5ba09d76 LibWeb: Saturate PixelUnits on creation when needed 2023-08-05 20:03:09 +02:00
Hendiadyoin1
f9fc0505fb LibWeb: Add propper rounding to PixelUnits::operator*
Also moves related constants into the class to make them accessible for
tests.
2023-08-05 20:03:09 +02:00
Hendiadyoin1
618f889486 LibWeb: Add and use convinient from_raw helper in PixelUnits 2023-08-05 20:03:09 +02:00
Aliaksandr Kalenik
2bd7d1426b LibWeb: Remove unused CSSPixels::epsilon() 2023-08-04 19:00:02 +02:00
Aliaksandr Kalenik
bec07d4af7 LibWeb: Use fixed-point saturated arithmetics for CSSPixels
Using fixed-point saturated arithmetics for CSSPixels allows to avoid
accumulating floating-point errors.

This implementation is not complete yet: currently saturated
arithmetics implemented only for addition. But it is enough to not
regress any of layout tests we have :)

See https://github.com/SerenityOS/serenity/issues/18566
2023-07-25 11:52:02 +02:00
Aliaksandr Kalenik
147c3b3d97 LibWeb+WebContent: Forbid access to underlying type of CSSPixels
Although DistinctNumeric, which is supposed to abstract the underlying
type, was used to represent CSSPixels, we have a whole bunch of places
in the layout code that assume CSSPixels::value() returns a
floating-point type. This assumption makes it difficult to replace the
underlying type in CSSPixels with a non-floating type.

To make it easier to transition CSSPixels to fixed-point math, one step
we can take is to prevent access to the underlying type using value()
and instead use explicit conversions with the to_float(), to_double(),
and to_int() methods.
2023-06-13 06:08:27 +02:00
Andreas Kling
655d9d1462 LibWeb: Make CSSPixels and Length use 64-bit (double) floating point
This fixes a plethora of rounding problems on many websites.
In the future, we may want to replace this with fixed-point arithmetic
(bug #18566) for performance (and consistency with other engines),
but in the meantime this makes the web look a bit better. :^)

There's a lot more things that could be converted to doubles, which
would reduce the amount of casting necessary in this patch.
We can do that incrementally, however.
2023-05-24 14:40:35 +02:00
Andreas Kling
134717ec8f LibWeb: VERIFY that nobody tries to hash a non-finite CSSPixels value
Non-finite CSSPixels quantities should never make their way into hash
tables. If this ever happens, let's catch it closer to the source
instead of letting things cascade into confusion.
2023-05-06 18:41:34 +02:00
Timothy Flynn
dbc04bbf1b LibWeb: Use type-correct hashing and formatting functions for pixels
1. Don't use double_hash. This is not for doubles, as its name implies.

2. Specialize traits and formatters using the underlying DistinctNumeric
   type of Web::DevicePixels and Web::CSSPixels.
2023-01-21 10:36:14 +01:00
Sam Atkins
a3298017d6 LibWeb: Only allow DevicePixels operators to work with integers
Allowing floats here was causing accidental truncations.

Co-authored-by: MacDue <macdue@dueutil.tech>
2022-12-14 16:47:57 +00:00
Sam Atkins
bd272e638c LibWeb: Introduce CSSPixels and DevicePixels classes
These are an attempt to separate the internal "pixel" used by CSS from
the actual "pixel" that exists on the display. Because of things like
2x display scaling, the ratio between these can vary, so having
distinct types will help prevent errors when converting from one unit
to the other.

`CSSPixels` refers to the `px` unit used on the web, which depending on
the device may or may not map to 1 pixel on the physical display. It's
a wrapper around `float`, and will be used by LibWeb for size and
position values up until we go to paint them to the screen.

`DevicePixels` on the other hand is a 1-to-1 pixel on the physical
display. It's a wrapper around `int`.
2022-12-08 12:46:03 +00:00