Commit graph

62 commits

Author SHA1 Message Date
MacDue
71baa8c31a LibWeb: Add CSSPixels::nearest_value_for(FloatingPoint)
This is intended to annotate conversions from unknown floating-point
values to CSSPixels, and make it more obvious the fp value will be
rounded to the nearest fixed-point value.
2023-08-26 23:53:45 +02:00
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
Sam Atkins
6bee81cfb6 LibWeb: Make serializing basic CSS types infallible 2023-08-22 17:51:48 +01:00
Sam Atkins
1feacd4b52 LibWeb: Use doubles for CSS dimension types
Avoid unintentionally converting between float and double multiple times
by just using double everywhere. Also, remove the unused `int` versions
of their constructors.
2023-08-20 14:25:18 +01:00
Andreas Kling
df8a96ee00 LibWeb: Add a way to resolve calc() values without a layout node
Instead of a layout node, you can pass a new Length::ResolutionContext
struct which contains everything needed to resolve calc() lengths.
2023-06-02 20:03:28 +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
41d518669e LibWeb: Remove weird CSS::Length::resolved() API
This API is no longer used by anyone so let's remove it.
2023-05-06 21:14:38 +02:00
Sam Atkins
28ceeec435 LibWeb: Split Length::relative_length_to_px() by type
Length units are either relative to the font, or to the viewport, but
never both. So we can save some work by not gathering font metrics for
a viewport unit, and not retrieving the viewport for a font unit.

Currently this is only helpful when the `to_px(Layout::Node)` method is
called, but since that is 208 places according to CLion, (plus 33
indirect uses via `Length::resolved()`) it still seems worthwhile. :^)
2023-04-29 16:23:50 +02:00
Sam Atkins
d6e5e61ed4 LibWeb: Add even more viewport-based Length units
`*vi` and `*vb` vary on which direction they check depending on whether
the writing mode is horizontal or vertical, so they will need some
modification once we support that.
2023-04-29 16:23:50 +02:00
Sam Atkins
091a1ff527 LibWeb: Add *lots* of viewport-based Length units
`sfoo` `lfoo` and `dfoo` are, for our purposes, identical to `foo`,
because we don't have dynamic GUI elements that cover the page content.
2023-04-29 16:23:50 +02:00
Sam Atkins
0dd585ba7b LibWeb: Add ic and ric Length units
Using the rough heuristic instead of the actual spec measurement. It's
allowed by the spec, but not ideal:

> In the cases where it is impossible or impractical to determine the
  ideographic advance measure, it must be assumed to be 1em.
2023-04-29 16:23:50 +02:00
Sam Atkins
03ed37eb14 LibWeb: Add cap and rcap Length units
As noted, the ascent of the font is not the best heuristic for this, but
it is one that's listed as OK to use by the spec:

> In the cases where it is impossible or impractical to determine the
  cap-height, the font’s ascent must be used.
2023-04-29 16:23:50 +02:00
Sam Atkins
a8e0fa403a LibWeb: Add rex and rch Length units
These are the same as `ex` and `ch`, but using the root element's
metrics. We now have this information available, so let's use it. :^)
2023-04-29 16:23:50 +02:00
Sam Atkins
0679b4e0b9 LibWeb: Wrap font metrics into a struct
Rather than passing an increasingly-unwieldy number of font parameters
individually to every function that resolves lengths, let's wrap them
up.

This is frustratingly close to being `Gfx::FontPixelMetrics`, but bitmap
fonts cause issues: We choose the closest font to what the CSS
requests, but that might have a wildly different size than what the
page expects, so we have to fudge the numbers.

No behaviour changes.
2023-04-29 16:23:50 +02:00
Sam Atkins
4a191875a9 LibWeb: Categorize relative length units 2023-04-29 16:23:50 +02:00
Sam Atkins
2c5f72b4f0 LibWeb: Remove outdated comment
I missed this when removing calc() from Length. Oops!
2023-04-29 16:23:50 +02:00
Sam Atkins
6ea84a7c87 LibWeb: Sort and group CSS Length units as they are in the spec
They previously weren't sorted at all. Alphabetical would be nice, but
then things like `em` and `rem` would be separated. So, let's copy the
spec's order. That way it's easier to keep track of which units we have
or haven't implemented. (Since there are so many...)
2023-04-29 16:23:50 +02:00
Sam Atkins
5f2f780662 LibWeb: Expose type and raw values of basic CSS types
This makes it possible to do arithmetic on them without having to
resolve to their canonical unit, which often requires context
information that is not available until the last minute. For example, a
Length cannot be resolved to px without knowing the font size, parent
element's size, etc.

Only Length currently requires such context, but treating all these
types the same means that code that manipulates them does not need to
know or care if a new unit gets added that does require contextual
information.
2023-04-13 09:53:47 +02:00
Sam Atkins
16e3a86393 LibWeb: Make absolutized_length() helper a Length method
There were a mix of users between those who want to know if the Length
changed, and those that just want an absolute Length. So, we now have
two methods: Length::absolutize() returns an empty Optional if nothing
changed, and Length::absolutized() always returns a value.
2023-03-30 21:29:50 +02:00
Sam Atkins
53a4a31af2 LibWeb: Remove CalculatedStyleValue from Length 2023-03-30 21:29:50 +02:00
Simon Wanner
554c4af90f LibWeb: Add support for the lh and rlh length units
Resolving these units is somewhat tricky because of their interaction
with both font-size and line-height, but this implementation seems to
work as tested in http://wpt.live/css/css-values/lh-unit-001.html and
http://wpt.live/css/css-values/lh-unit-002.html
2023-03-18 20:14:52 +01:00
Sam Atkins
9e735cc02e LibWeb: Remove unused includes for DeprecatedString
Missed these before, oops.
2023-02-15 12:48:26 -05:00
martinfalisse
ce0f41b9fb LibWeb+WebContent: Use new String class in CSS::StyleValue
Converts uses of DeprecatedString to String in StyleValue, and patches
surrounding files that depend on these functions.
2023-01-09 11:09:31 +01:00
Sam Atkins
4084c66ad2 LibWeb: Use CSS Pixels for viewport rects 2023-01-05 17:42:31 +01:00
Sam Atkins
8cc0bdf777 LibWeb: Resolve Lengths to CSSPixels 2023-01-05 17:42:31 +01:00
Sam Atkins
7d40e3eb0d LibWeb: Replace all px Length creation with Length::make_px(CSSPixels) 2023-01-05 17:42:31 +01:00
Sam Atkins
56422e37e0 LibWeb: Allow creating Lengths from CSSPixels 2022-12-10 12:03:19 +00:00
Linus Groh
57dc179b1f Everywhere: Rename to_{string => deprecated_string}() where applicable
This will make it easier to support both string types at the same time
while we convert code, and tracking down remaining uses.

One big exception is Value::to_string() in LibJS, where the name is
dictated by the ToString AO.
2022-12-06 08:54:33 +01:00
Linus Groh
6e19ab2bbc AK+Everywhere: Rename String to DeprecatedString
We have a new, improved string type coming up in AK (OOM aware, no null
state), and while it's going to use UTF-8, the name UTF8String is a
mouthful - so let's free up the String name by renaming the existing
class.
Making the old one have an annoying name will hopefully also help with
quick adoption :^)
2022-12-06 08:54:33 +01:00
Daniel Bertalan
4296425bd8 Everywhere: Remove redundant inequality comparison operators
C++20 can automatically synthesize `operator!=` from `operator==`, so
there is no point in writing such functions by hand if all they do is
call through to `operator==`.

This fixes a compile error with compilers that implement P2468 (Clang
16 currently). This paper restores the C++17 behavior that if both
`T::operator==(U)` and `T::operator!=(U)` exist, `U == T` won't be
rewritten in reverse to call `T::operator==(U)`. Removing `!=` operators
makes the rewriting possible again.
See https://reviews.llvm.org/D134529#3853062
2022-11-06 10:25:08 -07:00
Ben Wiederhake
8deced39a8 LibWeb: Resolve cyclic declaration/definitions involving Length
This remained undetected for a long time as HeaderCheck is disabled by
default. This commit makes the following file compile again:

    // file: compile_me.cpp
    #include <LibWeb/CSS/GridTrackSize.h>
    // That's it, this was enough to cause a compilation error.
2022-09-15 14:45:38 +01:00
Sam Atkins
7b4004d682 LibWeb: Ensure PercentageOr<T>::resolved() returns a concrete T
Which is to say, a T where `is_calculated()` is false.

As is becoming a repeating theme with CSS types, we have two states for
a FooPercentage that is a `calc()` expression: Either the FooPercentage
holds the CalculatedStyleValue directly, or it holds a Foo which itself
holds the CalculatedStyleValue. The first case was already handled to
return Foo, and with this patch, the second is too. :^)
2022-07-27 17:03:55 +02:00
Idan Horowitz
086969277e Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
Andreas Kling
344374588b LibGfx: Rename FontMetrics => FontPixelMetrics
Let's make it clear in the type name that this contains pixel metrics.
Also rename Font::metrics() => Font::pixel_metrics().
2022-03-30 00:57:15 +02:00
Sam Atkins
f76a541819 LibWeb: Move length-unit-from-string code into Length
This means the units are defined in a single place instead of two.

Also removed the verify that we didn't produce a bogus % dimension token
in the Tokenizer, since this has never happened and the parser is not a
tokenizer test suite. :^)
2022-02-24 08:04:25 +01:00
Sam Atkins
a07fed4e53 LibWeb: Account for Calculated in Length methods
We were ignoring this in a couple of places.
2022-02-24 08:04:25 +01:00
Andreas Kling
c61747fb2a LibWeb: Respect font-size specified by CSS in "em" length calculations
"5em" means 5*font-size, but by forcing "em" to mean the presentation
size of the bitmap font actually used, we broke a bunch of layouts that
depended on a correct interpretation of "em".

This means that "em" units will no longer be relative to the exact
size of the bitmap font in use, but I think that's a compromise we'll
have to make, since accurate layouts are more important.

This yields a visual progression on both ACID2 and ACID3. :^)
2022-02-21 18:35:12 +01:00
Andreas Kling
bfe69e9d0d LibWeb: Cache and reuse some very common StyleValue objects
Length values: auto, 0px, 1px
Color values: black, white, transparent
2022-02-19 14:45:59 +01:00
Sam Atkins
356d8bcfe8 LibWeb: Remove Length::Type::Undefined! :^) 2022-02-18 19:04:37 +01:00
Sam Atkins
6df3f9920d LibWeb: Calculate calc() lengths in to_px()
to_px() has the layout node available, so let's use it.
2022-02-18 19:04:37 +01:00
Sam Atkins
67066c5140 LibWeb: Remove fallback value from Length::resolved()
Nobody makes undefined Lengths now, (although actually removing
Undefined will come in a later commit) so we can remove this parameter,
and `resolved_or_auto()`/`resolved_or_zero()`.
2022-02-18 19:04:37 +01:00
Sam Atkins
ce0a516e59 LibWeb: Replace Length::set_calculated_style() with ::make_calculated()
There's no need to modify the Length's calculated-value after creating
it, so let's make it immutable. :^)
2022-02-04 13:52:02 +01:00
Sam Atkins
db04b5687d LibWeb: Move calc()-resolution code from Length to CalculatedStyleValue
The code is unchanged, just moved.
2022-02-04 13:52:02 +01:00
Sam Atkins
b34950a825 LibWeb: Add Formatters for Length, Percentage and LengthPercentage 2022-01-20 00:04:10 +01:00
Sam Atkins
bfcbab0dcf LibWeb: Remove reference_for_percent parameter from Length::resolved()
Despite looking like it was still needed, it was only used for passing
to other calls to Length::resolved() recursively. This makes the
various `foo.resolved().resolved()` calls a lot less awkward.
(Though, still quite awkward.)

I think we'd need to separate calculated lengths out to properly tidy
these calls up, but one yak at a time. :^)
2022-01-20 00:04:10 +01:00
Sam Atkins
cff44831a8 LibWeb: Remove Length::Type::Percentage :^)
All `<percentage>`s in the CSS grammar are now represented by the
`Percentage` class, and `<length-percentage>` by `LengthPercentage`.
2022-01-20 00:04:10 +01:00
Sam Atkins
01b57fa8b7 LibWeb: Add CSS::Percentage, PercentageOr and LengthPercentage types
Length and Percentage are different types, and sometimes only one or the
other is allowed in a given CSS property. This is a first step towards
separating them.
2022-01-20 00:04:10 +01:00
Andreas Kling
d413d0557d LibWeb: Remove unused CSS::Length::relative_length_to_px(Layout::Node) 2021-10-28 18:15:15 +02:00
Ben Wiederhake
21ba485fd3 LibWeb: Resolve cyclic dependency: Length and CalculatedStyleValue
Previously: Length (and all nearly all of its inline method
definitions) depended on the definition of class CalculatedStyleValue.
Meanwhile, CalculatedStyleValue (and nearly all of its namespaced
structs) depended on the definition of class Length.

Thus, a compilation unit that (for example) only contains
    #include <Userland/Libraries/LibWeb/CSS/Length.h>
would fail to compile.

This patch resolves this issue by pushing the inline definition of
various Web::CSS::Length methods into a different file.
2021-10-06 23:52:40 +01:00
Sam Atkins
f354fd72f1 LibWeb: Split Length::absolute_length_to_px() out from to_px()
In cases where we know the Length is absolute, we know we don't need to
pass in a Layout::Node or FontMetrics etc, and yet we were required to
before. Splitting it means jumping through less hoops that we don't have
to. :^)
2021-10-05 18:51:39 +02:00