Skip to content

Commit

Permalink
Adopt more smart pointers in FontCascade.cpp
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=268008

Reviewed by Brent Fulgham.

* Source/WebCore/platform/graphics/FontCascade.cpp:
(WebCore::FontCascade::width const):
(WebCore::FontCascade::widthForSimpleText const):
(WebCore::FontCascade::widthForSimpleTextWithFixedPitch const):
(WebCore::FontCascade::glyphDataForCharacter const):
(WebCore::FontCascade::isLoadingCustomFonts const):
(WebCore::FontCascade::emphasisMarkHeight const):
(WebCore::FontCascade::floatEmphasisMarkHeight const):
(WebCore::FontCascade::drawGlyphBuffer const):
(WebCore::GlyphToPathTranslator::GlyphToPathTranslator):
(WebCore::GlyphToPathTranslator::containsMorePaths):
(WebCore::GlyphToPathTranslator::path):
(WebCore::GlyphToPathTranslator::extents):
(WebCore::GlyphToPathTranslator::advance):
(WebCore::FontCascade::protectedFonts const):
* Source/WebCore/platform/graphics/FontCascade.h:
(WebCore::FontCascade::primaryFont const):
(WebCore::FontCascade::fallbackRangesAt const):
(WebCore::FontCascade::isFixedPitch const):
(WebCore::FontCascade::canTakeFixedPitchFastContentMeasuring const):
(WebCore::FontCascade::fontSelector const):

Canonical link: https://commits.webkit.org/273439@main
  • Loading branch information
cdumez committed Jan 24, 2024
1 parent 1d5e982 commit fb1c6b2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 32 deletions.
59 changes: 32 additions & 27 deletions Source/WebCore/platform/graphics/FontCascade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ float FontCascade::width(const TextRun& run, SingleThreadWeakHashSet<const Font>
}

bool hasWordSpacingOrLetterSpacing = wordSpacing() || letterSpacing();
float* cacheEntry = m_fonts->widthCache().add(run, std::numeric_limits<float>::quiet_NaN(), enableKerning() || requiresShaping(), hasWordSpacingOrLetterSpacing, glyphOverflow);
float* cacheEntry = protectedFonts()->widthCache().add(run, std::numeric_limits<float>::quiet_NaN(), enableKerning() || requiresShaping(), hasWordSpacingOrLetterSpacing, glyphOverflow);
if (cacheEntry && !std::isnan(*cacheEntry))
return *cacheEntry;

Expand Down Expand Up @@ -341,19 +341,19 @@ float FontCascade::widthForSimpleText(StringView text, TextDirection textDirecti
if (text.isNull() || text.isEmpty())
return 0;
ASSERT(codePath(TextRun(text)) != CodePath::Complex);
float* cacheEntry = m_fonts->widthCache().add(text, std::numeric_limits<float>::quiet_NaN());
float* cacheEntry = protectedFonts()->widthCache().add(text, std::numeric_limits<float>::quiet_NaN());
if (cacheEntry && !std::isnan(*cacheEntry))
return *cacheEntry;

GlyphBuffer glyphBuffer;
auto& font = primaryFont();
ASSERT(!font.syntheticBoldOffset()); // This function should only be called when RenderText::computeCanUseSimplifiedTextMeasuring() returns true, and that function requires no synthetic bold.
Ref font = primaryFont();
ASSERT(!font->syntheticBoldOffset()); // This function should only be called when RenderText::computeCanUseSimplifiedTextMeasuring() returns true, and that function requires no synthetic bold.
if (text.is8Bit())
addGlyphsFromText(glyphBuffer, font, text.characters8(), text.length());
else
addGlyphsFromText(glyphBuffer, font, text.characters16(), text.length());

auto initialAdvance = font.applyTransforms(glyphBuffer, 0, 0, enableKerning(), requiresShaping(), fontDescription().computedLocale(), text, textDirection);
auto initialAdvance = font->applyTransforms(glyphBuffer, 0, 0, enableKerning(), requiresShaping(), fontDescription().computedLocale(), text, textDirection);
auto width = 0.f;
for (size_t i = 0; i < glyphBuffer.size(); ++i)
width += WebCore::width(glyphBuffer.advanceAt(i));
Expand All @@ -373,7 +373,7 @@ float FontCascade::widthForSimpleTextWithFixedPitch(StringView text, bool whites
if (whitespaceIsCollapsed)
return text.length() * monospaceCharacterWidth;

float* cacheEntry = m_fonts->widthCache().add(text, std::numeric_limits<float>::quiet_NaN());
float* cacheEntry = protectedFonts()->widthCache().add(text, std::numeric_limits<float>::quiet_NaN());
if (cacheEntry && !std::isnan(*cacheEntry))
return *cacheEntry;

Expand Down Expand Up @@ -428,7 +428,7 @@ GlyphData FontCascade::glyphDataForCharacter(char32_t c, bool mirror, FontVarian

auto emojiPolicy = resolveEmojiPolicy(m_fontDescription.variantEmoji(), c);

return m_fonts->glyphDataForCharacter(c, m_fontDescription, variant, emojiPolicy);
return protectedFonts()->glyphDataForCharacter(c, m_fontDescription, variant, emojiPolicy);
}

// For font families where any of the fonts don't have a valid entry in the OS/2 table
Expand Down Expand Up @@ -1225,7 +1225,8 @@ bool FontCascade::canReceiveTextEmphasis(char32_t c)

bool FontCascade::isLoadingCustomFonts() const
{
return m_fonts && m_fonts->isLoadingCustomFonts();
RefPtr fonts = m_fonts;
return fonts && fonts->isLoadingCustomFonts();
}

enum class GlyphUnderlineType : uint8_t {
Expand Down Expand Up @@ -1343,14 +1344,14 @@ const Font* FontCascade::fontForEmphasisMark(const AtomString& mark) const

int FontCascade::emphasisMarkHeight(const AtomString& mark) const
{
if (auto* font = fontForEmphasisMark(mark))
if (RefPtr font = fontForEmphasisMark(mark))
return font->fontMetrics().height();
return { };
}

float FontCascade::floatEmphasisMarkHeight(const AtomString& mark) const
{
if (auto* font = fontForEmphasisMark(mark))
if (RefPtr font = fontForEmphasisMark(mark))
return font->fontMetrics().floatHeight();
return { };
}
Expand Down Expand Up @@ -1437,21 +1438,21 @@ inline bool shouldDrawIfLoading(const Font& font, FontCascade::CustomFontNotRead
void FontCascade::drawGlyphBuffer(GraphicsContext& context, const GlyphBuffer& glyphBuffer, FloatPoint& point, CustomFontNotReadyAction customFontNotReadyAction) const
{
ASSERT(glyphBuffer.isFlattened());
const Font* fontData = &glyphBuffer.fontAt(0);
RefPtr fontData = &glyphBuffer.fontAt(0);
FloatPoint startPoint = point;
float nextX = startPoint.x() + WebCore::width(glyphBuffer.advanceAt(0));
float nextY = startPoint.y() + height(glyphBuffer.advanceAt(0));
unsigned lastFrom = 0;
unsigned nextGlyph = 1;
while (nextGlyph < glyphBuffer.size()) {
const Font& nextFontData = glyphBuffer.fontAt(nextGlyph);
RefPtr nextFontData = &glyphBuffer.fontAt(nextGlyph);

if (&nextFontData != fontData) {
if (nextFontData != fontData) {
if (shouldDrawIfLoading(*fontData, customFontNotReadyAction))
context.drawGlyphs(*fontData, glyphBuffer.glyphs(lastFrom), glyphBuffer.advances(lastFrom), nextGlyph - lastFrom, startPoint, m_fontDescription.usedFontSmoothing());

lastFrom = nextGlyph;
fontData = &nextFontData;
fontData = WTFMove(nextFontData);
startPoint.setX(nextX);
startPoint.setY(nextY);
}
Expand Down Expand Up @@ -1717,42 +1718,41 @@ static void findPathIntersections(GlyphIterationState& state, const PathElement&
class GlyphToPathTranslator {
public:
GlyphToPathTranslator(const TextRun& textRun, const GlyphBuffer& glyphBuffer, const FloatPoint& textOrigin)
: m_index(0)
, m_textRun(textRun)
: m_textRun(textRun)
, m_glyphBuffer(glyphBuffer)
, m_fontData(&glyphBuffer.fontAt(m_index))
, m_fontData(glyphBuffer.fontAt(m_index))
, m_translation(AffineTransform::makeTranslation(toFloatSize(textOrigin)))
{
#if USE(CG)
m_translation.flipY();
#endif
}

bool containsMorePaths() { return m_index != m_glyphBuffer.size(); }
bool containsMorePaths() { return m_index != m_glyphBuffer->size(); }
Path path();
std::pair<float, float> extents();
GlyphUnderlineType underlineType();
void advance();

private:
unsigned m_index;
const TextRun& m_textRun;
const GlyphBuffer& m_glyphBuffer;
const Font* m_fontData;
unsigned m_index { 0 };
CheckedRef<const TextRun> m_textRun;
CheckedRef<const GlyphBuffer> m_glyphBuffer;
Ref<const Font> m_fontData;
AffineTransform m_translation;
};

Path GlyphToPathTranslator::path()
{
Path path = m_fontData->pathForGlyph(m_glyphBuffer.glyphAt(m_index));
Path path = Ref { m_fontData }->pathForGlyph(m_glyphBuffer->glyphAt(m_index));
path.transform(m_translation);
return path;
}

std::pair<float, float> GlyphToPathTranslator::extents()
{
auto beginning = m_translation.mapPoint(FloatPoint(0, 0));
auto advance = m_glyphBuffer.advanceAt(m_index);
auto advance = m_glyphBuffer->advanceAt(m_index);
auto end = m_translation.mapSize(size(advance));
return std::make_pair(beginning.x(), beginning.x() + end.width());
}
Expand All @@ -1764,11 +1764,16 @@ auto GlyphToPathTranslator::underlineType() -> GlyphUnderlineType

void GlyphToPathTranslator::advance()
{
GlyphBufferAdvance advance = m_glyphBuffer.advanceAt(m_index);
GlyphBufferAdvance advance = m_glyphBuffer->advanceAt(m_index);
m_translation.translate(size(advance));
++m_index;
if (m_index < m_glyphBuffer.size())
m_fontData = &m_glyphBuffer.fontAt(m_index);
if (m_index < m_glyphBuffer->size())
m_fontData = m_glyphBuffer->fontAt(m_index);
}

RefPtr<FontCascadeFonts> FontCascade::protectedFonts() const
{
return m_fonts;
}

DashArray FontCascade::dashesForIntersectionsWithRect(const TextRun& run, const FloatPoint& textOrigin, const FloatRect& lineExtents) const
Expand Down
12 changes: 7 additions & 5 deletions Source/WebCore/platform/graphics/FontCascade.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ class FontCascade : public CanMakeWeakPtr<FontCascade>, public CanMakeCheckedPtr

bool useBackslashAsYenSymbol() const { return m_useBackslashAsYenSymbol; }
FontCascadeFonts* fonts() const { return m_fonts.get(); }
WEBCORE_EXPORT RefPtr<FontCascadeFonts> protectedFonts() const;
bool isLoadingCustomFonts() const;

static ResolvedEmojiPolicy resolveEmojiPolicy(FontVariantEmoji, char32_t);
Expand Down Expand Up @@ -356,30 +357,31 @@ class FontCascade : public CanMakeWeakPtr<FontCascade>, public CanMakeCheckedPtr
inline const Font& FontCascade::primaryFont() const
{
ASSERT(m_fonts);
return m_fonts->primaryFont(m_fontDescription);
return protectedFonts()->primaryFont(m_fontDescription);
}

inline const FontRanges& FontCascade::fallbackRangesAt(unsigned index) const
{
ASSERT(m_fonts);
return m_fonts->realizeFallbackRangesAt(m_fontDescription, index);
return protectedFonts()->realizeFallbackRangesAt(m_fontDescription, index);
}

inline bool FontCascade::isFixedPitch() const
{
ASSERT(m_fonts);
return m_fonts->isFixedPitch(m_fontDescription);
return protectedFonts()->isFixedPitch(m_fontDescription);
}

inline bool FontCascade::canTakeFixedPitchFastContentMeasuring() const
{
ASSERT(m_fonts);
return m_fonts->canTakeFixedPitchFastContentMeasuring(m_fontDescription);
return protectedFonts()->canTakeFixedPitchFastContentMeasuring(m_fontDescription);
}

inline FontSelector* FontCascade::fontSelector() const
{
return m_fonts ? m_fonts->fontSelector() : nullptr;
RefPtr fonts = m_fonts;
return fonts ? fonts->fontSelector() : nullptr;
}

inline float FontCascade::tabWidth(const Font& font, const TabSize& tabSize, float position, Font::SyntheticBoldInclusion syntheticBoldInclusion) const
Expand Down

0 comments on commit fb1c6b2

Please sign in to comment.