tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit fb4b653594efa6646a5f62030a4129e387b77347
parent 08b764a4129fa4fb78ac7fe5bbe8c1106ac872ce
Author: Jonathan Kew <jkew@mozilla.com>
Date:   Sat, 18 Oct 2025 07:26:08 +0000

Bug 1994197 - patch 2 - Convert more text measurement to use integer nscoord values. r=layout-reviewers,TYLin

Differential Revision: https://phabricator.services.mozilla.com/D268560

Diffstat:
Mdom/canvas/CanvasRenderingContext2D.cpp | 17+++++++++--------
Mgfx/src/nsFontMetrics.cpp | 4++--
Mgfx/thebes/gfxFont.h | 8+++-----
Mgfx/thebes/gfxTextRun.h | 2+-
Mlayout/generic/nsTextFrame.cpp | 35+++++++++++++++++------------------
Mlayout/generic/nsTextFrame.h | 6+++---
Mlayout/mathml/nsMathMLChar.cpp | 2+-
7 files changed, 36 insertions(+), 38 deletions(-)

diff --git a/dom/canvas/CanvasRenderingContext2D.cpp b/dom/canvas/CanvasRenderingContext2D.cpp @@ -4665,7 +4665,7 @@ struct MOZ_STACK_CLASS CanvasBidiProcessor final } aSpacing++; } - return mProcessor.mLetterSpacing != 0.0 || mProcessor.mWordSpacing != 0.0; + return mProcessor.mLetterSpacing != 0 || mProcessor.mWordSpacing != 0; } mozilla::StyleHyphens GetHyphensOption() const { @@ -4677,9 +4677,9 @@ struct MOZ_STACK_CLASS CanvasBidiProcessor final gfxTextRun::HyphenType* aBreakBefore) const { MOZ_ASSERT_UNREACHABLE("no hyphenation in canvas2d text!"); } - gfxFloat GetHyphenWidth() const { + nscoord GetHyphenWidth() const { MOZ_ASSERT_UNREACHABLE("no hyphenation in canvas2d text!"); - return 0.0; + return 0; } already_AddRefed<DrawTarget> GetDrawTarget() const { MOZ_ASSERT_UNREACHABLE("no hyphenation in canvas2d text!"); @@ -4740,7 +4740,7 @@ struct MOZ_STACK_CLASS CanvasBidiProcessor final mBoundingBox = mBoundingBox.Union(textRunMetrics.mBoundingBox); } - return NSToCoordRound(textRunMetrics.mAdvanceWidth); + return textRunMetrics.mAdvanceWidth; } already_AddRefed<gfxPattern> GetGradientFor(Style aStyle) { @@ -4936,8 +4936,8 @@ struct MOZ_STACK_CLASS CanvasBidiProcessor final mozilla::gfx::PaletteCache& mPaletteCache; // spacing adjustments to be applied - gfx::Float mLetterSpacing = 0.0f; - gfx::Float mWordSpacing = 0.0f; + nscoord mLetterSpacing = 0; + nscoord mWordSpacing = 0; // to record any unsupported characters found in the text, // and notify front-end if it is interested @@ -5104,8 +5104,9 @@ UniquePtr<TextMetrics> CanvasRenderingContext2D::DrawOrMeasureText( if (state.letterSpacing != 0.0 || state.wordSpacing != 0.0) { processor.mLetterSpacing = - state.letterSpacing * processor.mAppUnitsPerDevPixel; - processor.mWordSpacing = state.wordSpacing * processor.mAppUnitsPerDevPixel; + NSToCoordRound(state.letterSpacing * processor.mAppUnitsPerDevPixel); + processor.mWordSpacing = + NSToCoordRound(state.wordSpacing * processor.mAppUnitsPerDevPixel); processor.mTextRunFlags |= gfx::ShapedTextFlags::TEXT_ENABLE_SPACING; if (state.letterSpacing != 0.0) { processor.mTextRunFlags |= diff --git a/gfx/src/nsFontMetrics.cpp b/gfx/src/nsFontMetrics.cpp @@ -89,7 +89,7 @@ class StubPropertyProvider final : public gfxTextRun::PropertyProvider { "This shouldn't be called because we never call BreakAndMeasureText"); return mozilla::StyleHyphens::None; } - gfxFloat GetHyphenWidth() const override { + nscoord GetHyphenWidth() const override { NS_ERROR("This shouldn't be called because we never enable hyphens"); return 0; } @@ -405,7 +405,7 @@ static nsBoundingMetrics GetTextBoundingMetrics( m.rightBearing = NSToCoordCeil(theMetrics.mBoundingBox.XMost()); m.ascent = NSToCoordCeil(-theMetrics.mBoundingBox.Y()); m.descent = NSToCoordCeil(theMetrics.mBoundingBox.YMost()); - m.width = NSToCoordRound(theMetrics.mAdvanceWidth); + m.width = theMetrics.mAdvanceWidth; } return m; } diff --git a/gfx/thebes/gfxFont.h b/gfx/thebes/gfxFont.h @@ -1662,19 +1662,17 @@ class gfxFont { * Metrics for a particular string */ struct RunMetrics { - RunMetrics() { mAdvanceWidth = mAscent = mDescent = 0.0; } - void CombineWith(const RunMetrics& aOther, bool aOtherIsOnLeft); // can be negative (partly due to negative spacing). // Advance widths should be additive: the advance width of the // (offset1, length1) plus the advance width of (offset1 + length1, // length2) should be the advance width of (offset1, length1 + length2) - gfxFloat mAdvanceWidth; + nscoord mAdvanceWidth = 0; // For zero-width substrings, these must be zero! - gfxFloat mAscent; // always non-negative - gfxFloat mDescent; // always non-negative + gfxFloat mAscent = 0.0; // always non-negative + gfxFloat mDescent = 0.0; // always non-negative // Bounding box that is guaranteed to include everything drawn. // If a tight boundingBox was requested when these metrics were diff --git a/gfx/thebes/gfxTextRun.h b/gfx/thebes/gfxTextRun.h @@ -230,7 +230,7 @@ class gfxTextRun : public gfxShapedText { // Returns the extra width that will be consumed by a hyphen. This should // be constant for a given textrun. - virtual gfxFloat GetHyphenWidth() const = 0; + virtual nscoord GetHyphenWidth() const = 0; // Return orientation flags to be used when creating a hyphen textrun. virtual mozilla::gfx::ShapedTextFlags GetShapedTextFlags() const = 0; diff --git a/layout/generic/nsTextFrame.cpp b/layout/generic/nsTextFrame.cpp @@ -272,11 +272,11 @@ TextAutospace::BoundarySet TextAutospace::InitBoundarySet( } // namespace mozilla struct TabWidth { - TabWidth(uint32_t aOffset, uint32_t aWidth) - : mOffset(aOffset), mWidth(float(aWidth)) {} + TabWidth(uint32_t aOffset, nscoord aWidth) + : mOffset(aOffset), mWidth(aWidth) {} uint32_t mOffset; // DOM offset relative to the current frame's offset. - float mWidth; // extra space to be added at this position (in app units) + nscoord mWidth; // extra space to be added at this position (in app units) }; struct nsTextFrame::TabWidthStore { @@ -3464,7 +3464,7 @@ nsTextFrame::PropertyProvider::PropertyProvider( mLength(aLength), mWordSpacing(WordSpacing(aFrame, *aTextStyle)), mLetterSpacing(LetterSpacing(aFrame, *aTextStyle)), - mMinTabAdvance(-1.0), + mMinTabAdvance(-1), mHyphenWidth(-1), mOffsetFromBlockOriginForTabs(aOffsetFromBlockOriginForTabs), mJustificationArrayStart(0), @@ -3494,7 +3494,7 @@ nsTextFrame::PropertyProvider::PropertyProvider( mLength(aFrame->GetContentLength()), mWordSpacing(WordSpacing(aFrame, *mTextStyle)), mLetterSpacing(LetterSpacing(aFrame, *mTextStyle)), - mMinTabAdvance(-1.0), + mMinTabAdvance(-1), mHyphenWidth(-1), mOffsetFromBlockOriginForTabs(0), mJustificationArrayStart(0), @@ -3514,7 +3514,7 @@ already_AddRefed<DrawTarget> nsTextFrame::PropertyProvider::GetDrawTarget() } gfxFloat nsTextFrame::PropertyProvider::MinTabAdvance() const { - if (mMinTabAdvance < 0.0) { + if (mMinTabAdvance < 0) { mMinTabAdvance = GetMinTabAdvanceAppUnits(mTextRun); } return mMinTabAdvance; @@ -3863,7 +3863,7 @@ static gfxFloat ComputeTabWidthAppUnits(const nsIFrame* aFrame) { MOZ_ASSERT(tabSize.IsNumber()); gfxFloat spaces = tabSize.number._0; - MOZ_ASSERT(spaces >= 0); + MOZ_ASSERT(spaces >= 0.0); const nsIFrame* cb = aFrame->GetContainingBlock(0, aFrame->StyleDisplay()); const auto* styleText = cb->StyleText(); @@ -4266,12 +4266,13 @@ bool nsTextFrame::PropertyProvider::GetSpacingInternal(Range aRange, } // aX and the result are in whole appunits. -static gfxFloat AdvanceToNextTab(gfxFloat aX, gfxFloat aTabWidth, - gfxFloat aMinAdvance) { +static nscoord AdvanceToNextTab(nscoord aX, gfxFloat aTabWidth, + gfxFloat aMinAdvance) { // Advance aX to the next multiple of aTabWidth. We must advance // by at least aMinAdvance. gfxFloat nextPos = aX + aMinAdvance; - return aTabWidth > 0.0 ? ceil(nextPos / aTabWidth) * aTabWidth : nextPos; + return NSToCoordRound(aTabWidth > 0 ? ceil(nextPos / aTabWidth) * aTabWidth + : nextPos); } void nsTextFrame::PropertyProvider::CalcTabWidths(Range aRange, @@ -4332,11 +4333,10 @@ void nsTextFrame::PropertyProvider::CalcTabWidths(Range aRange, mTabWidths = new TabWidthStore(mFrame->GetContentOffset()); mFrame->SetProperty(TabWidthProperty(), mTabWidths); } - double nextTab = AdvanceToNextTab(mOffsetFromBlockOriginForTabs, - aTabWidth, MinTabAdvance()); + nscoord nextTab = AdvanceToNextTab(mOffsetFromBlockOriginForTabs, + aTabWidth, MinTabAdvance()); mTabWidths->mWidths.AppendElement( - TabWidth(i - startOffset, - NSToIntRound(nextTab - mOffsetFromBlockOriginForTabs))); + TabWidth(i - startOffset, nextTab - mOffsetFromBlockOriginForTabs)); mOffsetFromBlockOriginForTabs = nextTab; } @@ -4356,7 +4356,7 @@ void nsTextFrame::PropertyProvider::CalcTabWidths(Range aRange, } } -gfxFloat nsTextFrame::PropertyProvider::GetHyphenWidth() const { +nscoord nsTextFrame::PropertyProvider::GetHyphenWidth() const { if (mHyphenWidth < 0) { const auto& hyphenateChar = mTextStyle->mHyphenateCharacter; if (hyphenateChar.IsAuto()) { @@ -9547,7 +9547,7 @@ void nsTextFrame::AddInlineMinISizeForFlow(gfxContext* aRenderingContext, aData->ForceBreak(); } else if (i < flowEndInTextRun && hyphenating && gfxTextRun::IsOptionalHyphenBreak(hyphBuffer[i - start])) { - aData->OptionallyBreak(NSToCoordRound(provider.GetHyphenWidth())); + aData->OptionallyBreak(provider.GetHyphenWidth()); } else { aData->OptionallyBreak(); } @@ -10697,8 +10697,7 @@ void nsTextFrame::ReflowText(nsLineLayout& aLineLayout, nscoord aAvailableWidth, // Disallow negative widths WritingMode wm = GetWritingMode(); LogicalSize finalSize(wm); - finalSize.ISize(wm) = - NSToCoordCeilClamped(std::max(gfxFloat(0.0), textMetrics.mAdvanceWidth)); + finalSize.ISize(wm) = std::max(0, textMetrics.mAdvanceWidth); nscoord fontBaseline; // Note(dshin): Baseline should tecnhically be halfway through the em box for diff --git a/layout/generic/nsTextFrame.h b/layout/generic/nsTextFrame.h @@ -161,7 +161,7 @@ class nsTextFrame : public nsIFrame { void InitializeForMeasure(); bool GetSpacing(Range aRange, Spacing* aSpacing) const final; - gfxFloat GetHyphenWidth() const final; + nscoord GetHyphenWidth() const final; void GetHyphenationBreaks(Range aRange, HyphenType* aBreakBefore) const final; mozilla::StyleHyphens GetHyphensOption() const final { @@ -270,8 +270,8 @@ class nsTextFrame : public nsIFrame { // min advance for <tab> char mutable gfxFloat mMinTabAdvance; - mutable gfxFloat mHyphenWidth; - mutable gfxFloat mOffsetFromBlockOriginForTabs; + mutable nscoord mHyphenWidth; + mutable nscoord mOffsetFromBlockOriginForTabs; // The values in mJustificationSpacings corresponds to unskipped // characters start from mJustificationArrayStart. diff --git a/layout/mathml/nsMathMLChar.cpp b/layout/mathml/nsMathMLChar.cpp @@ -889,7 +889,7 @@ static nsBoundingMetrics MeasureTextRun(DrawTarget* aDrawTarget, bm.rightBearing = NSToCoordCeil(metrics.mBoundingBox.XMost()); bm.ascent = NSToCoordCeil(-metrics.mBoundingBox.Y()); bm.descent = NSToCoordCeil(metrics.mBoundingBox.YMost()); - bm.width = NSToCoordRound(metrics.mAdvanceWidth); + bm.width = metrics.mAdvanceWidth; return bm; }