Skip to content

Commit

Permalink
Decoupling from-font resolution from FontCascadeFonts::primaryFont
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=273339
rdar://127132471

Reviewed by Brent Fulgham.

font-size-adjust from-font resolution was fixed by [1]. It now works correctly and
it uses the primary font for resolution. However, this patch changed FontCascadeFonts::primaryFont
to receive a mutable (non-const) FontCascadeDescription object, such that it could resolve the
from-font reference after the primary font is resolved.

This means that now FontCascadeFonts::primaryFont does two different things:
a) resolving and caching a font as the primary font
b) resolving from-font according to the primary font if necessary

The (a) part of primaryFont doesn't require a mutable FontCascadeDescription.
We can update FontCascadeDescription to have a method for resolving font-size-adjust
from-font at the point where we own a mutable FontCascadeDescription instead.

This allows us to compute the primaryFont itself from places where we only have access
to a const FontCascadeDescription. This is for example, required for solving
https://bugs.webkit.org/show_bug.cgi?id=273233, where we need to access to primaryFont
from FontCascadeFonts::glyphDataForVariant

This patch should have no side effects, as it is a preparation
for solving 273233.

[1] https://commits.webkit.org/269041@main

* Source/WebCore/css/ComputedStyleExtractor.cpp:
(WebCore::fontSizeAdjustFromStyle):
* Source/WebCore/platform/graphics/FontCascade.h:
(WebCore::FontCascade::primaryFont const):
* Source/WebCore/platform/graphics/FontCascadeDescription.cpp:
(WebCore::FontCascadeDescription::resolveFontSizeAdjustFromFontIfNeeded):
* Source/WebCore/platform/graphics/FontCascadeDescription.h:
* Source/WebCore/platform/graphics/FontCascadeFonts.h:
(WebCore::FontCascadeFonts::primaryFont):
* Source/WebCore/platform/graphics/FontSizeAdjust.h:
(WebCore::FontSizeAdjust::shouldResolveFromFont const):

Canonical link: https://commits.webkit.org/278219@main
  • Loading branch information
vitorroriz committed May 1, 2024
1 parent 9c98cfb commit 44d3464
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Source/WebCore/css/ComputedStyleExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ static Ref<CSSValue> fontSizeAdjustFromStyle(const RenderStyle& style)
return CSSPrimitiveValue::create(CSSValueNone);

auto metric = fontSizeAdjust.metric;
auto value = fontSizeAdjust.isFromFont() ? fontSizeAdjust.resolve(style.computedFontSize(), style.metricsOfPrimaryFont()) : fontSizeAdjust.value.asOptional();
auto value = fontSizeAdjust.shouldResolveFromFont() ? fontSizeAdjust.resolve(style.computedFontSize(), style.metricsOfPrimaryFont()) : fontSizeAdjust.value.asOptional();
if (metric == FontSizeAdjust::Metric::ExHeight)
return CSSPrimitiveValue::create(*value);

Expand Down
4 changes: 3 additions & 1 deletion Source/WebCore/platform/graphics/FontCascade.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,9 @@ class FontCascade final : public CanMakeWeakPtr<FontCascade>, public CanMakeChec
inline const Font& FontCascade::primaryFont() const
{
ASSERT(m_fonts);
return protectedFonts()->primaryFont(m_fontDescription);
auto& font = protectedFonts()->primaryFont(m_fontDescription);
m_fontDescription.resolveFontSizeAdjustFromFontIfNeeded(font);
return font;
}

inline const FontRanges& FontCascade::fallbackRangesAt(unsigned index) const
Expand Down
10 changes: 10 additions & 0 deletions Source/WebCore/platform/graphics/FontCascadeDescription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,14 @@ FontSmoothingMode FontCascadeDescription::usedFontSmoothing() const
return fontSmoothingMode;
}

void FontCascadeDescription::resolveFontSizeAdjustFromFontIfNeeded(const Font& font)
{
const auto& fontSizeAdjust = this->fontSizeAdjust();
if (!fontSizeAdjust.shouldResolveFromFont())
return;

auto aspectValue = fontSizeAdjust.resolve(computedSize(), font.fontMetrics());
setFontSizeAdjust({ fontSizeAdjust.metric, FontSizeAdjust::ValueType::FromFont, aspectValue });
}

} // namespace WebCore
4 changes: 4 additions & 0 deletions Source/WebCore/platform/graphics/FontCascadeDescription.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ typedef FontFamilySpecificationNull FontFamilyPlatformSpecification;

typedef std::variant<AtomString, FontFamilyPlatformSpecification> FontFamilySpecification;

class Font;

class FontCascadeDescription : public FontDescription {
public:
WEBCORE_EXPORT FontCascadeDescription();
Expand Down Expand Up @@ -118,6 +120,8 @@ class FontCascadeDescription : public FontDescription {
}
#endif

WEBCORE_EXPORT void resolveFontSizeAdjustFromFontIfNeeded(const Font&);

// Initial values for font properties.
static std::optional<FontSelectionValue> initialItalic() { return std::nullopt; }
static FontStyleAxis initialFontStyleAxis() { return FontStyleAxis::slnt; }
Expand Down
12 changes: 3 additions & 9 deletions Source/WebCore/platform/graphics/FontCascadeFonts.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class FontCascadeFonts : public RefCounted<FontCascadeFonts> {
WidthCache& widthCache() { return m_widthCache; }
const WidthCache& widthCache() const { return m_widthCache; }

const Font& primaryFont(FontCascadeDescription&);
const Font& primaryFont(const FontCascadeDescription&);
WEBCORE_EXPORT const FontRanges& realizeFallbackRangesAt(const FontCascadeDescription&, unsigned fallbackIndex);

void pruneSystemFallbacks();
Expand Down Expand Up @@ -144,7 +144,7 @@ inline bool FontCascadeFonts::canTakeFixedPitchFastContentMeasuring(const FontCa
return m_canTakeFixedPitchFastContentMeasuring == TriState::True;
}

inline const Font& FontCascadeFonts::primaryFont(FontCascadeDescription& description)
inline const Font& FontCascadeFonts::primaryFont(const FontCascadeDescription& description)
{
ASSERT(m_thread ? m_thread->ptr() == &Thread::current() : isMainThread());
if (!m_cachedPrimaryFont) {
Expand All @@ -164,14 +164,8 @@ inline const Font& FontCascadeFonts::primaryFont(FontCascadeDescription& descrip
}
}
}

ASSERT(m_cachedPrimaryFont);
auto fontSizeAdjust = description.fontSizeAdjust();
if (fontSizeAdjust.isFromFont()) {
auto aspectValue = fontSizeAdjust.resolve(description.computedSize(), m_cachedPrimaryFont->fontMetrics());
description.setFontSizeAdjust({ fontSizeAdjust.metric, FontSizeAdjust::ValueType::FromFont, aspectValue });
}
}
ASSERT(m_cachedPrimaryFont);
return *m_cachedPrimaryFont;
}

Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/platform/graphics/FontSizeAdjust.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ struct FontSizeAdjust {

bool isNone() const { return !value && type != ValueType::FromFont; }
bool isFromFont() const { return type == ValueType::FromFont; }
bool shouldResolveFromFont() const { return isFromFont() && !value; }

Metric metric { Metric::ExHeight };
ValueType type { ValueType::Number };
Expand Down

0 comments on commit 44d3464

Please sign in to comment.