Skip to content

Commit

Permalink
Refactor computeUnzoomedNonCalcLengthDouble to only take a single Fon…
Browse files Browse the repository at this point in the history
…tCascade

https://bugs.webkit.org/show_bug.cgi?id=260650
rdar://114370210

Reviewed by Darin Adler.

This is in preparation of the implementation of the new root font units.

Since:
- the font cascade contains both the font metrics & font description
- we'll need the root font metrics for the new root font units
- we should only be using either (not both) the current font metrics or the current root font metrics depending on the unit

Instead of having 3 separate arguments to computeUnzoomedNonCalcLengthDouble for the metrics, description, root font description, only pass a single font cascade, which is either the root style font cascade or the current style font cascade depending on the unit.

* Source/WebCore/css/CSSPrimitiveValue.cpp:
(WebCore::CSSPrimitiveValue::computeUnzoomedNonCalcLengthDouble):
(WebCore::CSSPrimitiveValue::computeNonCalcLengthDouble):
* Source/WebCore/css/CSSPrimitiveValue.h:
(WebCore::CSSPrimitiveValue::isRootFontRelativeLength):
* Source/WebCore/style/StyleBuilderState.h:
* Source/WebCore/style/StyleResolveForFontRaw.cpp:
(WebCore::Style::resolveForFontRaw):

Canonical link: https://commits.webkit.org/267227@main
  • Loading branch information
nt1m committed Aug 24, 2023
1 parent 63319aa commit a37e927
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 33 deletions.
54 changes: 26 additions & 28 deletions Source/WebCore/css/CSSPrimitiveValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,30 +675,34 @@ static double lengthOfViewportPhysicalAxisForLogicalAxis(LogicalBoxAxis logicalA
return lengthOfViewportPhysicalAxisForLogicalAxis(logicalAxis, size, rootElement->renderStyle());
}

double CSSPrimitiveValue::computeUnzoomedNonCalcLengthDouble(CSSUnitType primitiveType, double value, CSSPropertyID propertyToCompute, const FontMetrics* fontMetrics, const FontCascadeDescription* fontDescription, const FontCascadeDescription* rootFontDescription, const RenderView* renderView)
double CSSPrimitiveValue::computeUnzoomedNonCalcLengthDouble(CSSUnitType primitiveType, double value, CSSPropertyID propertyToCompute, const FontCascade* fontCascadeForUnit, const RenderView* renderView)
{
// FIXME: Figure out where we don't initialize the root style in BuilderContext and check if this matters.
if (isRootFontRelativeLength(primitiveType) && !fontCascadeForUnit)
return value;

switch (primitiveType) {
case CSSUnitType::CSS_EMS:
case CSSUnitType::CSS_QUIRKY_EMS:
ASSERT(fontDescription);
return ((propertyToCompute == CSSPropertyFontSize) ? fontDescription->specifiedSize() : fontDescription->computedSize()) * value;
case CSSUnitType::CSS_EXS:
ASSERT(fontMetrics);
if (fontMetrics->hasXHeight())
return fontMetrics->xHeight() * value;
ASSERT(fontDescription);
return ((propertyToCompute == CSSPropertyFontSize) ? fontDescription->specifiedSize() : fontDescription->computedSize()) / 2.0 * value;
case CSSUnitType::CSS_REMS:
if (!rootFontDescription)
return value;
return ((propertyToCompute == CSSPropertyFontSize) ? rootFontDescription->specifiedSize() : rootFontDescription->computedSize()) * value;
case CSSUnitType::CSS_REMS: {
ASSERT(fontCascadeForUnit);
auto& fontDescription = fontCascadeForUnit->fontDescription();
return ((propertyToCompute == CSSPropertyFontSize) ? fontDescription.specifiedSize() : fontDescription.computedSize()) * value;
}
case CSSUnitType::CSS_EXS: {
ASSERT(fontCascadeForUnit);
auto& fontMetrics = fontCascadeForUnit->metricsOfPrimaryFont();
if (fontMetrics.hasXHeight())
return fontMetrics.xHeight() * value;
auto& fontDescription = fontCascadeForUnit->fontDescription();
return ((propertyToCompute == CSSPropertyFontSize) ? fontDescription.specifiedSize() : fontDescription.computedSize()) / 2.0 * value;
}
case CSSUnitType::CSS_CHS:
ASSERT(fontMetrics);
ASSERT(fontDescription);
return fontMetrics->zeroWidth().value_or(fontDescription->computedSize() / 2) * value;
ASSERT(fontCascadeForUnit);
return fontCascadeForUnit->metricsOfPrimaryFont().zeroWidth().value_or(fontCascadeForUnit->fontDescription().computedSize() / 2) * value;
case CSSUnitType::CSS_IC:
ASSERT(fontMetrics);
return fontMetrics->ideogramWidth() * value;
ASSERT(fontCascadeForUnit);
return fontCascadeForUnit->metricsOfPrimaryFont().ideogramWidth() * value;
case CSSUnitType::CSS_PX:
return value;
case CSSUnitType::CSS_CM:
Expand Down Expand Up @@ -807,23 +811,17 @@ double CSSPrimitiveValue::computeNonCalcLengthDouble(const CSSToLengthConversion
switch (primitiveType) {
case CSSUnitType::CSS_EMS:
case CSSUnitType::CSS_QUIRKY_EMS:
value = computeUnzoomedNonCalcLengthDouble(primitiveType, value, conversionData.propertyToCompute(), nullptr, &conversionData.fontCascadeForFontUnits().fontDescription());
break;

case CSSUnitType::CSS_EXS:
case CSSUnitType::CSS_CHS:
case CSSUnitType::CSS_IC:
// FIXME: We have a bug right now where the zoom will be applied twice to EX units.
// We really need to compute EX using fontMetrics for the original specifiedSize and not use
// our actual constructed rendering font.
value = computeUnzoomedNonCalcLengthDouble(primitiveType, value, conversionData.propertyToCompute(), &conversionData.fontCascadeForFontUnits().metricsOfPrimaryFont(), &conversionData.fontCascadeForFontUnits().fontDescription());
value = computeUnzoomedNonCalcLengthDouble(primitiveType, value, conversionData.propertyToCompute(), &conversionData.fontCascadeForFontUnits());
break;

case CSSUnitType::CSS_REMS:
value = computeUnzoomedNonCalcLengthDouble(primitiveType, value, conversionData.propertyToCompute(), nullptr, nullptr, conversionData.rootStyle() ? &conversionData.rootStyle()->fontDescription() : nullptr);
break;

case CSSUnitType::CSS_CHS:
case CSSUnitType::CSS_IC:
value = computeUnzoomedNonCalcLengthDouble(primitiveType, value, conversionData.propertyToCompute(), &conversionData.fontCascadeForFontUnits().metricsOfPrimaryFont(), &conversionData.fontCascadeForFontUnits().fontDescription());
value = computeUnzoomedNonCalcLengthDouble(primitiveType, value, conversionData.propertyToCompute(), conversionData.rootStyle() ? &conversionData.rootStyle()->fontCascade() : nullptr);
break;

case CSSUnitType::CSS_PX:
Expand Down
15 changes: 11 additions & 4 deletions Source/WebCore/css/CSSPrimitiveValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ namespace WebCore {
class CSSCalcValue;
class CSSToLengthConversionData;
class Color;
class FontCascadeDescription;
class FontMetrics;
class FontCascade;
class RenderStyle;
class RenderView;

Expand Down Expand Up @@ -76,7 +75,8 @@ class CSSPrimitiveValue final : public CSSValue {
bool isAngle() const { return unitCategory(primitiveType()) == CSSUnitCategory::Angle; }
bool isFontIndependentLength() const { return isFontIndependentLength(primitiveUnitType()); }
bool isFontRelativeLength() const { return isFontRelativeLength(primitiveUnitType()); }
bool isParentFontRelativeLength() const { return isPercentage() || (isFontRelativeLength() && primitiveType() != CSSUnitType::CSS_REMS && primitiveType() != CSSUnitType::CSS_RLHS); }
bool isParentFontRelativeLength() const { return isPercentage() || (isFontRelativeLength() && !isRootFontRelativeLength()); }
bool isRootFontRelativeLength() const { return isRootFontRelativeLength(primitiveUnitType()); }
bool isQuirkyEms() const { return primitiveType() == CSSUnitType::CSS_QUIRKY_EMS; }
bool isLength() const { return isLength(static_cast<CSSUnitType>(primitiveType())); }
bool isNumber() const { return primitiveType() == CSSUnitType::CSS_NUMBER; }
Expand Down Expand Up @@ -185,7 +185,7 @@ class CSSPrimitiveValue final : public CSSValue {
static std::optional<double> conversionToCanonicalUnitsScaleFactor(CSSUnitType);
static ASCIILiteral unitTypeString(CSSUnitType);

static double computeUnzoomedNonCalcLengthDouble(CSSUnitType, double value, CSSPropertyID, const FontMetrics* = nullptr, const FontCascadeDescription* = nullptr, const FontCascadeDescription* rootFontDescription = nullptr, const RenderView* = nullptr);
static double computeUnzoomedNonCalcLengthDouble(CSSUnitType, double value, CSSPropertyID, const FontCascade* = nullptr, const RenderView* = nullptr);
static double computeNonCalcLengthDouble(const CSSToLengthConversionData&, CSSUnitType, double value);
// True if computeNonCalcLengthDouble would produce identical results when resolved against both these styles.
static bool equalForLengthResolution(const RenderStyle&, const RenderStyle&);
Expand Down Expand Up @@ -227,6 +227,7 @@ class CSSPrimitiveValue final : public CSSValue {
NEVER_INLINE String formatIntegerValue(ASCIILiteral suffix) const;
static constexpr bool isFontIndependentLength(CSSUnitType);
static constexpr bool isFontRelativeLength(CSSUnitType);
static constexpr bool isRootFontRelativeLength(CSSUnitType);
static constexpr bool isViewportPercentageLength(CSSUnitType);

union {
Expand All @@ -252,6 +253,12 @@ constexpr bool CSSPrimitiveValue::isFontIndependentLength(CSSUnitType type)
|| type == CSSUnitType::CSS_PC;
}

constexpr bool CSSPrimitiveValue::isRootFontRelativeLength(CSSUnitType type)
{
return type == CSSUnitType::CSS_RLHS
|| type == CSSUnitType::CSS_REMS;
}

constexpr bool CSSPrimitiveValue::isFontRelativeLength(CSSUnitType type)
{
return type == CSSUnitType::CSS_EMS
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/style/StyleBuilderState.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
namespace WebCore {

class FilterOperations;
class FontCascadeDescription;
class RenderStyle;
class StyleImage;
class StyleResolver;
Expand Down
3 changes: 2 additions & 1 deletion Source/WebCore/style/StyleResolveForFontRaw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ std::optional<FontCascade> resolveForFontRaw(const FontRaw& fontRaw, FontCascade
// It's unclear in the specification if they're expected to work on OffscreenCanvas, given
// that it's off-screen and therefore doesn't strictly have an associated viewport.
// This needs clarification and possibly fixing.
return static_cast<float>(CSSPrimitiveValue::computeUnzoomedNonCalcLengthDouble(length.type, length.value, CSSPropertyFontSize, &fontCascade.metricsOfPrimaryFont(), &fontCascade.fontDescription(), &fontCascade.fontDescription(), is<Document>(context) ? downcast<Document>(context).renderView() : nullptr));
// FIXME: How should root font units work in OffscreenCanvas?
return static_cast<float>(CSSPrimitiveValue::computeUnzoomedNonCalcLengthDouble(length.type, length.value, CSSPropertyFontSize, &fontCascade, is<Document>(context) ? downcast<Document>(context).renderView() : nullptr));
}, [&] (const CSSPropertyParserHelpers::PercentRaw& percentage) {
return static_cast<float>((parentSize * percentage.value) / 100.0);
});
Expand Down

0 comments on commit a37e927

Please sign in to comment.