// scale the rect but round to smallest containing rect nsRect& nsRect::ScaleRoundOut(float aScale) { nscoord right = NSToCoordCeil(float(XMost()) * aScale); nscoord bottom = NSToCoordCeil(float(YMost()) * aScale); x = NSToCoordFloor(float(x) * aScale); y = NSToCoordFloor(float(y) * aScale); width = (right - x); height = (bottom - y); return *this; }
//------------------------------------------------------------------------------ void nsPageFrame::PaintPageContent(nsIRenderingContext& aRenderingContext, const nsRect& aDirtyRect, nsPoint aPt) { nsIFrame* pageContentFrame = mFrames.FirstChild(); nsRect rect = aDirtyRect; float scale = PresContext()->GetPageScale(); aRenderingContext.PushState(); nsPoint framePos = aPt + pageContentFrame->GetOffsetTo(this); aRenderingContext.Translate(framePos.x, framePos.y); // aPt translates to coords relative to this, then margins translate to // pageContentFrame's coords rect -= framePos; aRenderingContext.Scale(scale, scale); rect.ScaleRoundOut(1.0f / scale); // Make sure we don't draw where we aren't supposed to draw, especially // when printing selection nsRect clipRect(nsPoint(0, 0), pageContentFrame->GetSize()); // Note: this computation matches how we compute maxSize.height // in nsPageFrame::Reflow nscoord expectedPageContentHeight = NSToCoordCeil((GetSize().height - mPD->mReflowMargin.TopBottom()) / scale); if (clipRect.height > expectedPageContentHeight) { // We're doing print-selection, with one long page-content frame. // Clip to the appropriate page-content slice for the current page. NS_ASSERTION(mPageNum > 0, "page num should be positive"); // Note: The pageContentFrame's y-position has been set such that a zero // y-value matches the top edge of the current page. So, to clip to the // current page's content (in coordinates *relative* to the page content // frame), we just negate its y-position and add the top margin. clipRect.y = NSToCoordCeil((-pageContentFrame->GetRect().y + mPD->mReflowMargin.top) / scale); clipRect.height = expectedPageContentHeight; NS_ASSERTION(clipRect.y < pageContentFrame->GetSize().height, "Should be clipping to region inside the page content bounds"); } aRenderingContext.SetClipRect(clipRect, nsClipCombine_kIntersect); nsRect backgroundRect = nsRect(nsPoint(0, 0), pageContentFrame->GetSize()); nsCSSRendering::PaintBackground(PresContext(), aRenderingContext, this, rect, backgroundRect, nsCSSRendering::PAINTBG_SYNC_DECODE_IMAGES); nsLayoutUtils::PaintFrame(&aRenderingContext, pageContentFrame, nsRegion(rect), NS_RGBA(0,0,0,0), nsLayoutUtils::PAINT_SYNC_DECODE_IMAGES); aRenderingContext.PopState(); }
static void GetTextRunBoundingMetrics(gfxTextRun *aTextRun, PRUint32 aStart, PRUint32 aLength, nsThebesRenderingContext *aContext, nsBoundingMetrics &aBoundingMetrics) { StubPropertyProvider provider; gfxTextRun::Metrics theMetrics = aTextRun->MeasureText(aStart, aLength, PR_TRUE, aContext->ThebesContext(), &provider); aBoundingMetrics.leftBearing = NSToCoordFloor(theMetrics.mBoundingBox.X()); aBoundingMetrics.rightBearing = NSToCoordCeil(theMetrics.mBoundingBox.XMost()); aBoundingMetrics.width = NSToCoordRound(theMetrics.mAdvanceWidth); aBoundingMetrics.ascent = NSToCoordCeil(- theMetrics.mBoundingBox.Y()); aBoundingMetrics.descent = NSToCoordCeil(theMetrics.mBoundingBox.YMost()); }
nscoord nsMathMLmfracFrame::CalcLineThickness(nsPresContext* aPresContext, nsStyleContext* aStyleContext, nsString& aThicknessAttribute, nscoord onePixel, nscoord aDefaultRuleThickness) { nscoord defaultThickness = aDefaultRuleThickness; nscoord lineThickness = aDefaultRuleThickness; nscoord minimumThickness = onePixel; // linethickness // // "Specifies the thickness of the horizontal 'fraction bar', or 'rule'. The // default value is 'medium', 'thin' is thinner, but visible, 'thick' is // thicker; the exact thickness of these is left up to the rendering agent." // // values: length | "thin" | "medium" | "thick" // default: medium // if (!aThicknessAttribute.IsEmpty()) { if (aThicknessAttribute.EqualsLiteral("thin")) { lineThickness = NSToCoordFloor(defaultThickness * THIN_FRACTION_LINE); minimumThickness = onePixel * THIN_FRACTION_LINE_MINIMUM_PIXELS; // should visually decrease by at least one pixel, if default is not a pixel if (defaultThickness > onePixel && lineThickness > defaultThickness - onePixel) lineThickness = defaultThickness - onePixel; } else if (aThicknessAttribute.EqualsLiteral("medium")) { // medium is default } else if (aThicknessAttribute.EqualsLiteral("thick")) { lineThickness = NSToCoordCeil(defaultThickness * THICK_FRACTION_LINE); minimumThickness = onePixel * THICK_FRACTION_LINE_MINIMUM_PIXELS; // should visually increase by at least one pixel if (lineThickness < defaultThickness + onePixel) lineThickness = defaultThickness + onePixel; } else { // length value lineThickness = defaultThickness; ParseNumericValue(aThicknessAttribute, &lineThickness, nsMathMLElement::PARSE_ALLOW_UNITLESS, aPresContext, aStyleContext); } } // use minimum if the lineThickness is a non-zero value less than minimun if (lineThickness && lineThickness < minimumThickness) lineThickness = minimumThickness; return lineThickness; }
nscoord nsMathMLmfracFrame::CalcLineThickness(nsPresContext* aPresContext, nsStyleContext* aStyleContext, nsString& aThicknessAttribute, nscoord onePixel, nscoord aDefaultRuleThickness) { nscoord defaultThickness = aDefaultRuleThickness; nscoord lineThickness = aDefaultRuleThickness; nscoord minimumThickness = onePixel; if (!aThicknessAttribute.IsEmpty()) { if (aThicknessAttribute.EqualsLiteral("thin")) { lineThickness = NSToCoordFloor(defaultThickness * THIN_FRACTION_LINE); minimumThickness = onePixel * THIN_FRACTION_LINE_MINIMUM_PIXELS; // should visually decrease by at least one pixel, if default is not a pixel if (defaultThickness > onePixel && lineThickness > defaultThickness - onePixel) lineThickness = defaultThickness - onePixel; } else if (aThicknessAttribute.EqualsLiteral("medium")) { lineThickness = NSToCoordRound(defaultThickness * MEDIUM_FRACTION_LINE); minimumThickness = onePixel * MEDIUM_FRACTION_LINE_MINIMUM_PIXELS; // should visually increase by at least one pixel if (lineThickness < defaultThickness + onePixel) lineThickness = defaultThickness + onePixel; } else if (aThicknessAttribute.EqualsLiteral("thick")) { lineThickness = NSToCoordCeil(defaultThickness * THICK_FRACTION_LINE); minimumThickness = onePixel * THICK_FRACTION_LINE_MINIMUM_PIXELS; // should visually increase by at least two pixels if (lineThickness < defaultThickness + 2*onePixel) lineThickness = defaultThickness + 2*onePixel; } else { // see if it is a plain number, or a percentage, or a h/v-unit like 1ex, 2px, 1em nsCSSValue cssValue; if (ParseNumericValue(aThicknessAttribute, cssValue)) { nsCSSUnit unit = cssValue.GetUnit(); if (eCSSUnit_Number == unit) lineThickness = nscoord(float(defaultThickness) * cssValue.GetFloatValue()); else if (eCSSUnit_Percent == unit) lineThickness = nscoord(float(defaultThickness) * cssValue.GetPercentValue()); else if (eCSSUnit_Null != unit) lineThickness = CalcLength(aPresContext, aStyleContext, cssValue); } } } // use minimum if the lineThickness is a non-zero value less than minimun if (lineThickness && lineThickness < minimumThickness) lineThickness = minimumThickness; return lineThickness; }
nsBoundingMetrics nsFontMetrics::GetBoundingMetrics(const PRUnichar *aString, PRUint32 aLength, nsRenderingContext *aContext) { if (aLength == 0) return nsBoundingMetrics(); StubPropertyProvider provider; AutoTextRun textRun(this, aContext, aString, aLength); gfxTextRun::Metrics theMetrics = textRun->MeasureText(0, aLength, gfxFont::TIGHT_HINTED_OUTLINE_EXTENTS, aContext->ThebesContext(), &provider); nsBoundingMetrics m; m.leftBearing = NSToCoordFloor( theMetrics.mBoundingBox.X()); m.rightBearing = NSToCoordCeil( theMetrics.mBoundingBox.XMost()); m.ascent = NSToCoordCeil( -theMetrics.mBoundingBox.Y()); m.descent = NSToCoordCeil( theMetrics.mBoundingBox.YMost()); m.width = NSToCoordRound( theMetrics.mAdvanceWidth); return m; }
static nsBoundingMetrics GetTextBoundingMetrics(nsFontMetrics* aMetrics, const char16_t* aString, uint32_t aLength, mozilla::gfx::DrawTarget* aDrawTarget, gfxFont::BoundingBoxType aType) { if (aLength == 0) return nsBoundingMetrics(); StubPropertyProvider provider; AutoTextRun textRun(aMetrics, aDrawTarget, aString, aLength); nsBoundingMetrics m; if (textRun.get()) { gfxTextRun::Metrics theMetrics = textRun->MeasureText( gfxTextRun::Range(0, aLength), aType, aDrawTarget, &provider); m.leftBearing = NSToCoordFloor( theMetrics.mBoundingBox.X()); m.rightBearing = NSToCoordCeil( theMetrics.mBoundingBox.XMost()); m.ascent = NSToCoordCeil( -theMetrics.mBoundingBox.Y()); m.descent = NSToCoordCeil( theMetrics.mBoundingBox.YMost()); m.width = NSToCoordRound( theMetrics.mAdvanceWidth); } return m; }
static nsBoundingMetrics GetTextBoundingMetrics(nsFontMetrics* aMetrics, const PRUnichar *aString, uint32_t aLength, nsRenderingContext *aContext, gfxFont::BoundingBoxType aType) { if (aLength == 0) return nsBoundingMetrics(); StubPropertyProvider provider; AutoTextRun textRun(aMetrics, aContext, aString, aLength); nsBoundingMetrics m; if (textRun.get()) { gfxTextRun::Metrics theMetrics = textRun->MeasureText(0, aLength, aType, aContext->ThebesContext(), &provider); m.leftBearing = NSToCoordFloor( theMetrics.mBoundingBox.X()); m.rightBearing = NSToCoordCeil( theMetrics.mBoundingBox.XMost()); m.ascent = NSToCoordCeil( -theMetrics.mBoundingBox.Y()); m.descent = NSToCoordCeil( theMetrics.mBoundingBox.YMost()); m.width = NSToCoordRound( theMetrics.mAdvanceWidth); } return m; }
//------------------------------------------------------------------------------ void nsPageFrame::PaintPageContent(nsIRenderingContext& aRenderingContext, const nsRect& aDirtyRect, nsPoint aPt) { nsIFrame* pageContentFrame = mFrames.FirstChild(); nsRect rect = aDirtyRect; float scale = PresContext()->GetPageScale(); aRenderingContext.PushState(); nsPoint framePos = aPt + pageContentFrame->GetOffsetTo(this); aRenderingContext.Translate(framePos.x, framePos.y); // aPt translates to coords relative to this, then margins translate to // pageContentFrame's coords rect -= framePos; aRenderingContext.Scale(scale, scale); rect.ScaleRoundOut(1.0f / scale); // Make sure we don't draw where we aren't supposed to draw, especially // when printing selection nsRect clipRect(nsPoint(0, 0), pageContentFrame->GetSize()); // Note: this computation matches how we compute maxSize.height // in nsPageFrame::Reflow nscoord expectedPageContentHeight = NSToCoordCeil((GetSize().height - mPD->mReflowMargin.TopBottom()) / scale); if (clipRect.height > expectedPageContentHeight) { // We're doing print-selection, with one long page-content frame. // Clip to the appropriate page-content slice for the current page. NS_ASSERTION(mPageNum > 0, "page num should be positive"); clipRect.y = expectedPageContentHeight * (mPageNum - 1); clipRect.height = expectedPageContentHeight; NS_ASSERTION(clipRect.y < pageContentFrame->GetSize().height, "Should be clipping to region inside the page content bounds"); } aRenderingContext.SetClipRect(clipRect, nsClipCombine_kIntersect); const nsStyleBorder* border = GetStyleBorder(); const nsStylePadding* padding = GetStylePadding(); nsRect backgroundRect = nsRect(nsPoint(0, 0), pageContentFrame->GetSize()); nsCSSRendering::PaintBackground(PresContext(), aRenderingContext, this, rect, backgroundRect, *border, *padding, PR_TRUE); nsLayoutUtils::PaintFrame(&aRenderingContext, pageContentFrame, nsRegion(rect), NS_RGBA(0,0,0,0)); aRenderingContext.PopState(); }
NS_IMETHODIMP nsPageFrame::Reflow(nsPresContext* aPresContext, nsHTMLReflowMetrics& aDesiredSize, const nsHTMLReflowState& aReflowState, nsReflowStatus& aStatus) { DO_GLOBAL_REFLOW_COUNT("nsPageFrame"); DISPLAY_REFLOW(aPresContext, this, aReflowState, aDesiredSize, aStatus); aStatus = NS_FRAME_COMPLETE; // initialize out parameter NS_ASSERTION(mFrames.FirstChild() && nsGkAtoms::pageContentFrame == mFrames.FirstChild()->GetType(), "pageFrame must have a pageContentFrame child"); // Resize our frame allowing it only to be as big as we are // XXX Pay attention to the page's border and padding... if (mFrames.NotEmpty()) { nsIFrame* frame = mFrames.FirstChild(); // When the reflow size is NS_UNCONSTRAINEDSIZE it means we are reflowing // a single page to print selection. So this means we want to use // NS_UNCONSTRAINEDSIZE without altering it nscoord avHeight; if (mPD->mReflowSize.height == NS_UNCONSTRAINEDSIZE) { avHeight = NS_UNCONSTRAINEDSIZE; } else { avHeight = mPD->mReflowSize.height - mPD->mReflowMargin.TopBottom(); } nsSize maxSize(mPD->mReflowSize.width - mPD->mReflowMargin.LeftRight(), avHeight); float scale = aPresContext->GetPageScale(); maxSize.width = NSToCoordCeil(maxSize.width / scale); if (maxSize.height != NS_UNCONSTRAINEDSIZE) { maxSize.height = NSToCoordCeil(maxSize.height / scale); } // Get the number of Twips per pixel from the PresContext nscoord onePixelInTwips = nsPresContext::CSSPixelsToAppUnits(1); // insurance against infinite reflow, when reflowing less than a pixel // XXX Shouldn't we do something more friendly when invalid margins // are set? if (maxSize.width < onePixelInTwips || maxSize.height < onePixelInTwips) { aDesiredSize.width = 0; aDesiredSize.height = 0; NS_WARNING("Reflow aborted; no space for content"); return NS_OK; } nsHTMLReflowState kidReflowState(aPresContext, aReflowState, frame, maxSize); kidReflowState.mFlags.mIsTopOfPage = PR_TRUE; kidReflowState.mFlags.mTableIsSplittable = PR_TRUE; // calc location of frame nscoord xc = mPD->mReflowMargin.left + mPD->mExtraMargin.left; nscoord yc = mPD->mReflowMargin.top + mPD->mExtraMargin.top; // Get the child's desired size ReflowChild(frame, aPresContext, aDesiredSize, kidReflowState, xc, yc, 0, aStatus); // Place and size the child FinishReflowChild(frame, aPresContext, &kidReflowState, aDesiredSize, xc, yc, 0); NS_ASSERTION(!NS_FRAME_IS_FULLY_COMPLETE(aStatus) || !frame->GetNextInFlow(), "bad child flow list"); } PR_PL(("PageFrame::Reflow %p ", this)); PR_PL(("[%d,%d][%d,%d]\n", aDesiredSize.width, aDesiredSize.height, aReflowState.availableWidth, aReflowState.availableHeight)); // Return our desired size aDesiredSize.width = aReflowState.availableWidth; if (aReflowState.availableHeight != NS_UNCONSTRAINEDSIZE) { aDesiredSize.height = aReflowState.availableHeight; } PR_PL(("PageFrame::Reflow %p ", this)); PR_PL(("[%d,%d]\n", aReflowState.availableWidth, aReflowState.availableHeight)); NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aDesiredSize); return NS_OK; }
//------------------------------------------------------------------------------ NS_IMETHODIMP nsPageFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder, const nsRect& aDirtyRect, const nsDisplayListSet& aLists) { nsDisplayListCollection set; nsresult rv; if (PresContext()->IsScreen()) { rv = DisplayBorderBackgroundOutline(aBuilder, aLists); NS_ENSURE_SUCCESS(rv, rv); } nsDisplayList content; nsIFrame *child = mFrames.FirstChild(); rv = child->BuildDisplayListForStackingContext(aBuilder, aDirtyRect - child->GetOffsetTo(this), &content); NS_ENSURE_SUCCESS(rv, rv); // We may need to paint out-of-flow frames whose placeholders are // on other pages. Add those pages to our display list. Note that // out-of-flow frames can't be placed after their placeholders so // we don't have to process earlier pages. The display lists for // these extra pages are pruned so that only display items for the // page we currently care about (which we would have reached by // following placeholders to their out-of-flows) end up on the list. nsIFrame* page = child; nscoord y = child->GetSize().height; while ((page = GetNextPage(page)) != nullptr) { rv = BuildDisplayListForExtraPage(aBuilder, page, y, &content); if (NS_FAILED(rv)) break; y += page->GetSize().height; } float scale = PresContext()->GetPageScale(); nsRect clipRect(nsPoint(0, 0), child->GetSize()); // Note: this computation matches how we compute maxSize.height // in nsPageFrame::Reflow nscoord expectedPageContentHeight = NSToCoordCeil((GetSize().height - mPD->mReflowMargin.TopBottom()) / scale); if (clipRect.height > expectedPageContentHeight) { // We're doing print-selection, with one long page-content frame. // Clip to the appropriate page-content slice for the current page. NS_ASSERTION(mPageNum > 0, "page num should be positive"); // Note: The pageContentFrame's y-position has been set such that a zero // y-value matches the top edge of the current page. So, to clip to the // current page's content (in coordinates *relative* to the page content // frame), we just negate its y-position and add the top margin. clipRect.y = NSToCoordCeil((-child->GetRect().y + mPD->mReflowMargin.top) / scale); clipRect.height = expectedPageContentHeight; NS_ASSERTION(clipRect.y < child->GetSize().height, "Should be clipping to region inside the page content bounds"); } clipRect += aBuilder->ToReferenceFrame(child); rv = content.AppendNewToTop(new (aBuilder) nsDisplayClip(aBuilder, child, &content, clipRect)); NS_ENSURE_SUCCESS(rv, rv); rv = content.AppendNewToTop(new (aBuilder) nsDisplayTransform(aBuilder, child, &content, ::ComputePageTransform)); NS_ENSURE_SUCCESS(rv, rv); set.Content()->AppendToTop(&content); if (PresContext()->IsRootPaginatedDocument()) { rv = set.Content()->AppendNewToTop(new (aBuilder) nsDisplayGeneric(aBuilder, this, ::PaintHeaderFooter, "HeaderFooter", nsDisplayItem::TYPE_HEADER_FOOTER)); NS_ENSURE_SUCCESS(rv, rv); } set.MoveTo(aLists); return NS_OK; }
NS_IMETHODIMP nsSVGForeignObjectFrame::PaintSVG(nsSVGRenderState *aContext, const nsIntRect *aDirtyRect) { if (IsDisabled()) return NS_OK; nsIFrame* kid = GetFirstChild(nsnull); if (!kid) return NS_OK; gfxMatrix matrixForChildren = GetCanvasTMForChildren(); gfxMatrix matrix = GetCanvasTM(); nsRenderingContext *ctx = aContext->GetRenderingContext(this); if (!ctx || matrixForChildren.IsSingular()) { NS_WARNING("Can't render foreignObject element!"); return NS_ERROR_FAILURE; } /* Check if we need to draw anything. */ if (aDirtyRect) { PRInt32 appUnitsPerDevPx = PresContext()->AppUnitsPerDevPixel(); if (!mRect.ToOutsidePixels(appUnitsPerDevPx).Intersects(*aDirtyRect)) return NS_OK; } gfxContext *gfx = aContext->GetGfxContext(); gfx->Save(); if (GetStyleDisplay()->IsScrollableOverflow()) { float x, y, width, height; static_cast<nsSVGElement*>(mContent)-> GetAnimatedLengthValues(&x, &y, &width, &height, nsnull); gfxRect clipRect = nsSVGUtils::GetClipRectForFrame(this, 0.0f, 0.0f, width, height); nsSVGUtils::SetClipRect(gfx, matrix, clipRect); } gfx->Multiply(matrixForChildren); // Transform the dirty rect into the rectangle containing the // transformed dirty rect. gfxMatrix invmatrix = matrix.Invert(); NS_ASSERTION(!invmatrix.IsSingular(), "inverse of non-singular matrix should be non-singular"); gfxRect transDirtyRect = gfxRect(aDirtyRect->x, aDirtyRect->y, aDirtyRect->width, aDirtyRect->height); transDirtyRect = invmatrix.TransformBounds(transDirtyRect); transDirtyRect.Scale(nsPresContext::AppUnitsPerCSSPixel()); nsPoint tl(NSToCoordFloor(transDirtyRect.X()), NSToCoordFloor(transDirtyRect.Y())); nsPoint br(NSToCoordCeil(transDirtyRect.XMost()), NSToCoordCeil(transDirtyRect.YMost())); nsRect kidDirtyRect(tl.x, tl.y, br.x - tl.x, br.y - tl.y); kidDirtyRect.IntersectRect(kidDirtyRect, kid->GetRect()); PRUint32 flags = nsLayoutUtils::PAINT_IN_TRANSFORM; if (aContext->IsPaintingToWindow()) { flags |= nsLayoutUtils::PAINT_TO_WINDOW; } nsresult rv = nsLayoutUtils::PaintFrame(ctx, kid, nsRegion(kidDirtyRect), NS_RGBA(0,0,0,0), flags); gfx->Restore(); return rv; }