From 279cfa170d20cf5ac7a14da17d7909c650a1006f Mon Sep 17 00:00:00 2001 From: "sharparrow1%yahoo.com" Date: Tue, 13 Feb 2007 22:36:59 +0000 Subject: [PATCH] Bug 369834: Get rid of SetAltDevice and friends (and fix print preview). r+sr=roc. git-svn-id: svn://10.0.0.236/trunk@220085 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/gfx/public/nsDeviceContext.h | 11 --- mozilla/gfx/public/nsIDeviceContext.h | 19 ---- mozilla/gfx/src/nsDeviceContext.cpp | 87 +------------------ .../gfx/src/thebes/nsThebesDeviceContext.cpp | 22 ----- .../gfx/src/thebes/nsThebesDeviceContext.h | 3 - mozilla/layout/base/nsDocumentViewer.cpp | 21 +---- mozilla/layout/base/nsPresContext.cpp | 3 +- mozilla/layout/base/nsPresContext.h | 12 ++- .../layout/generic/nsSimplePageSequence.cpp | 51 +++++++++-- mozilla/layout/generic/nsSimplePageSequence.h | 6 +- mozilla/layout/printing/nsPrintEngine.cpp | 21 ++--- 11 files changed, 74 insertions(+), 182 deletions(-) diff --git a/mozilla/gfx/public/nsDeviceContext.h b/mozilla/gfx/public/nsDeviceContext.h index eef220ccd3f..38c44d61267 100644 --- a/mozilla/gfx/public/nsDeviceContext.h +++ b/mozilla/gfx/public/nsDeviceContext.h @@ -121,12 +121,6 @@ public: PRUnichar* aPrintToFileName) { return NS_OK; } NS_IMETHOD AbortDocument(void) { return NS_OK; } -#ifdef NS_PRINT_PREVIEW - NS_IMETHOD SetAltDevice(nsIDeviceContext* aAltDC); - NS_IMETHOD GetAltDevice(nsIDeviceContext** aAltDC) { *aAltDC = mAltDC.get(); NS_IF_ADDREF(*aAltDC); return NS_OK;} - NS_IMETHOD SetUseAltDC(PRUint8 aValue, PRBool aOn); -#endif - NS_IMETHOD PrepareNativeWidget(nsIWidget *aWidget, void **aOut); NS_IMETHOD ClearCachedSystemFonts(); @@ -150,11 +144,6 @@ protected: nsCOMPtr mLocaleLangGroup; // XXX temp fix for performance bug - erik nsHashtable* mFontAliasTable; -#ifdef NS_PRINT_PREVIEW - nsCOMPtr mAltDC; - PRUint8 mUseAltDC; -#endif - public: nsNativeWidget mWidget; #ifdef NS_DEBUG diff --git a/mozilla/gfx/public/nsIDeviceContext.h b/mozilla/gfx/public/nsIDeviceContext.h index 74b3391ecd3..2708d4003dd 100644 --- a/mozilla/gfx/public/nsIDeviceContext.h +++ b/mozilla/gfx/public/nsIDeviceContext.h @@ -469,25 +469,6 @@ public: */ NS_IMETHOD EndPage(void) = 0; -#ifdef NS_PRINT_PREVIEW - /** - * Set an Alternative Device Context where some of the calls - * are deferred to it - */ - NS_IMETHOD SetAltDevice(nsIDeviceContext* aAltDC) = 0; - - /** - * Get the Alternate Device Context - */ - NS_IMETHOD GetAltDevice(nsIDeviceContext** aAltDC) = 0; - - /** - * Turn on/off which types of information is retrieved - * via the alt device context - */ - NS_IMETHOD SetUseAltDC(PRUint8 aValue, PRBool aOn) = 0; -#endif - /** * Clear cached system fonts (refresh from theme when * requested). This method is effectively static, diff --git a/mozilla/gfx/src/nsDeviceContext.cpp b/mozilla/gfx/src/nsDeviceContext.cpp index 86f58dcf5b3..bfa914690f7 100644 --- a/mozilla/gfx/src/nsDeviceContext.cpp +++ b/mozilla/gfx/src/nsDeviceContext.cpp @@ -49,7 +49,7 @@ #include "nsIServiceManager.h" #include "nsUnicharUtils.h" #include "nsCRT.h" - +#include "nsIRenderingContext.h" NS_IMPL_ISUPPORTS3(DeviceContextImpl, nsIDeviceContext, nsIObserver, nsISupportsWeakReference) @@ -61,9 +61,6 @@ DeviceContextImpl::DeviceContextImpl() mWidget = nsnull; mFontAliasTable = nsnull; -#ifdef NS_PRINT_PREVIEW - mUseAltDC = kUseAltDCFor_NONE; -#endif #ifdef NS_DEBUG mInitialized = PR_FALSE; #endif @@ -128,14 +125,7 @@ void DeviceContextImpl::CommonInit(void) NS_IMETHODIMP DeviceContextImpl::CreateRenderingContext(nsIView *aView, nsIRenderingContext *&aContext) { -#ifdef NS_PRINT_PREVIEW - // AltDC NEVER use widgets to create their DCs - if (mAltDC && (mUseAltDC & kUseAltDCFor_CREATERC_PAINT)) { - return mAltDC->CreateRenderingContext(aContext); - } -#endif - - nsresult rv; + nsresult rv; aContext = nsnull; nsCOMPtr pContext; @@ -153,13 +143,6 @@ NS_IMETHODIMP DeviceContextImpl::CreateRenderingContext(nsIView *aView, nsIRende NS_IMETHODIMP DeviceContextImpl::CreateRenderingContext(nsIDrawingSurface* aSurface, nsIRenderingContext *&aContext) { -#ifdef NS_PRINT_PREVIEW - // AltDC NEVER use widgets to create their DCs - if (mAltDC && (mUseAltDC & kUseAltDCFor_CREATERC_PAINT)) { - return mAltDC->CreateRenderingContext(aContext); - } -#endif /* NS_PRINT_PREVIEW */ - nsresult rv; aContext = nsnull; @@ -180,15 +163,6 @@ NS_IMETHODIMP DeviceContextImpl::CreateRenderingContext(nsIWidget *aWidget, nsIR { nsresult rv; -#ifdef NS_PRINT_PREVIEW - // AltDC NEVER use widgets to create their DCs - // NOTE: The mAltDC will call it;s own init - // so we can return here - if (mAltDC && (mUseAltDC & kUseAltDCFor_CREATERC_REFLOW)) { - return mAltDC->CreateRenderingContext(aContext); - } -#endif - aContext = nsnull; nsCOMPtr pContext; rv = CreateRenderingContextInstance(*getter_AddRefs(pContext)); @@ -218,32 +192,12 @@ NS_IMETHODIMP DeviceContextImpl::CreateRenderingContextInstance(nsIRenderingCont nsresult DeviceContextImpl::InitRenderingContext(nsIRenderingContext *aContext, nsIWidget *aWin) { -#ifdef NS_PRINT_PREVIEW - // there are a couple of cases where the kUseAltDCFor_CREATERC_xxx flag has been turned off - // but we still need to initialize with the Alt DC - if (mAltDC) { - return aContext->Init(mAltDC, aWin); - } else { - return aContext->Init(this, aWin); - } -#else return aContext->Init(this, aWin); -#endif } nsresult DeviceContextImpl::InitRenderingContext(nsIRenderingContext *aContext, nsIDrawingSurface* aSurface) { -#ifdef NS_PRINT_PREVIEW - // there are a couple of cases where the kUseAltDCFor_CREATERC_xxx flag has been turned off - // but we still need to initialize with the Alt DC - if (mAltDC) { - return aContext->Init(mAltDC, aSurface); - } else { - return aContext->Init(this, aSurface); - } -#else return aContext->Init(this, aSurface); -#endif /* NS_PRINT_PREVIEW */ } NS_IMETHODIMP DeviceContextImpl::CreateFontCache() @@ -281,13 +235,6 @@ DeviceContextImpl::GetLocaleLangGroup(void) NS_IMETHODIMP DeviceContextImpl::GetMetricsFor(const nsFont& aFont, nsIAtom* aLangGroup, nsIFontMetrics*& aMetrics) { -#ifdef NS_PRINT_PREVIEW - // Defer to Alt when there is one - if (mAltDC != nsnull && (mUseAltDC & kUseAltDCFor_FONTMETRICS)) { - return mAltDC->GetMetricsFor(aFont, aLangGroup, aMetrics); - } -#endif - if (nsnull == mFontCache) { nsresult rv = CreateFontCache(); if (NS_FAILED(rv)) { @@ -308,13 +255,6 @@ NS_IMETHODIMP DeviceContextImpl::GetMetricsFor(const nsFont& aFont, NS_IMETHODIMP DeviceContextImpl::GetMetricsFor(const nsFont& aFont, nsIFontMetrics*& aMetrics) { -#ifdef NS_PRINT_PREVIEW - // Defer to Alt when there is one - if (mAltDC != nsnull && (mUseAltDC & kUseAltDCFor_FONTMETRICS)) { - return mAltDC->GetMetricsFor(aFont, aMetrics); - } -#endif - if (nsnull == mFontCache) { nsresult rv = CreateFontCache(); if (NS_FAILED(rv)) { @@ -515,29 +455,6 @@ NS_IMETHODIMP DeviceContextImpl::FlushFontCache(void) return NS_OK; } -#ifdef NS_PRINT_PREVIEW -NS_IMETHODIMP DeviceContextImpl::SetAltDevice(nsIDeviceContext* aAltDC) -{ - mAltDC = aAltDC; - - // Can't use it if it isn't there - if (aAltDC == nsnull) { - mUseAltDC = kUseAltDCFor_NONE; - } - return NS_OK; -} - -NS_IMETHODIMP DeviceContextImpl::SetUseAltDC(PRUint8 aValue, PRBool aOn) -{ - if (aOn) { - mUseAltDC |= aValue; - } else { - mUseAltDC &= ~aValue; - } - return NS_OK; -} -#endif - ///////////////////////////////////////////////////////////// nsFontCache::nsFontCache() diff --git a/mozilla/gfx/src/thebes/nsThebesDeviceContext.cpp b/mozilla/gfx/src/thebes/nsThebesDeviceContext.cpp index 946b95bc861..0220011ce30 100644 --- a/mozilla/gfx/src/thebes/nsThebesDeviceContext.cpp +++ b/mozilla/gfx/src/thebes/nsThebesDeviceContext.cpp @@ -584,28 +584,6 @@ nsThebesDeviceContext::EndPage(void) return NS_OK; } - -NS_IMETHODIMP -nsThebesDeviceContext::SetAltDevice(nsIDeviceContext* aAltDC) -{ - return NS_OK; -} - - -NS_IMETHODIMP -nsThebesDeviceContext::GetAltDevice(nsIDeviceContext** aAltDC) -{ - *aAltDC = nsnull; - return NS_OK; -} - - -NS_IMETHODIMP -nsThebesDeviceContext::SetUseAltDC(PRUint8 aValue, PRBool aOn) -{ - return NS_OK; -} - /** End printing methods **/ int diff --git a/mozilla/gfx/src/thebes/nsThebesDeviceContext.h b/mozilla/gfx/src/thebes/nsThebesDeviceContext.h index 6eaddd831bc..a87c510f253 100644 --- a/mozilla/gfx/src/thebes/nsThebesDeviceContext.h +++ b/mozilla/gfx/src/thebes/nsThebesDeviceContext.h @@ -108,9 +108,6 @@ public: NS_IMETHOD AbortDocument(void); NS_IMETHOD BeginPage(void); NS_IMETHOD EndPage(void); - NS_IMETHOD SetAltDevice(nsIDeviceContext* aAltDC); - NS_IMETHOD GetAltDevice(nsIDeviceContext** aAltDC); - NS_IMETHOD SetUseAltDC(PRUint8 aValue, PRBool aOn); /* end printing goop */ static void DebugShowCairoSurface (const char *aName, cairo_surface_t *aSurface); diff --git a/mozilla/layout/base/nsDocumentViewer.cpp b/mozilla/layout/base/nsDocumentViewer.cpp index 892ed4b42ed..e5f9da4906f 100644 --- a/mozilla/layout/base/nsDocumentViewer.cpp +++ b/mozilla/layout/base/nsDocumentViewer.cpp @@ -794,13 +794,6 @@ DocumentViewerImpl::InitInternal(nsIWidget* aParentWidget, mDeviceContext = aDeviceContext; -#if defined(NS_PRINTING) && defined(NS_PRINT_PREVIEW) - // Clear PrintPreview Alternate Device - if (mDeviceContext) { - mDeviceContext->SetAltDevice(nsnull); - } -#endif - PRBool makeCX = PR_FALSE; if (aDoCreation) { if (aParentWidget && !mPresContext) { @@ -857,9 +850,10 @@ DocumentViewerImpl::InitInternal(nsIWidget* aParentWidget, // In fact I'm just adding a line that makes this block // get compiled *less* often. mDeviceContext->GetDeviceContextFor(devspec, *getter_AddRefs(devctx)); - mDeviceContext->SetAltDevice(devctx); - mDeviceContext->SetUseAltDC(kUseAltDCFor_SURFACE_DIM, PR_TRUE); - //Get paper dims: + // XXX I'm breaking this code; I'm not sure I really want to mess with + // the document viewer at the moment to get the right device context + // (this won't break anyone, since page layout mode was never really + // usable) PRInt32 pageWidth, pageHeight; devctx->GetDeviceSurfaceDimensions(pageWidth, pageHeight); mPresContext->SetPageSize(nsSize(pageWidth, pageHeight)); @@ -1980,13 +1974,6 @@ DocumentViewerImpl::Show(void) mDeviceContext = mParentWidget->GetDeviceContext(); -#if defined(NS_PRINTING) && defined(NS_PRINT_PREVIEW) - // Clear PrintPreview Alternate Device - if (mDeviceContext) { - mDeviceContext->SetAltDevice(nsnull); - } -#endif - // Create presentation context NS_ASSERTION(!mPresContext, "Shouldn't have a prescontext if we have no shell!"); mPresContext = new nsPresContext(mDocument, nsPresContext::eContext_Galley); diff --git a/mozilla/layout/base/nsPresContext.cpp b/mozilla/layout/base/nsPresContext.cpp index e96804ecc3b..f6b664b7695 100644 --- a/mozilla/layout/base/nsPresContext.cpp +++ b/mozilla/layout/base/nsPresContext.cpp @@ -178,7 +178,8 @@ nsPresContext::nsPresContext(nsIDocument* aDocument, nsPresContextType aType) mDefaultFantasyFont("fantasy", NS_FONT_STYLE_NORMAL, NS_FONT_VARIANT_NORMAL, NS_FONT_WEIGHT_NORMAL, 0, 0), mCanPaginatedScroll(PR_FALSE), - mIsRootPaginatedDocument(PR_FALSE) + mIsRootPaginatedDocument(PR_FALSE), + mPPScale(1.0f) { // NOTE! nsPresContext::operator new() zeroes out all members, so don't // bother initializing members to 0. diff --git a/mozilla/layout/base/nsPresContext.h b/mozilla/layout/base/nsPresContext.h index 2009b8d9616..e2a39a65497 100644 --- a/mozilla/layout/base/nsPresContext.h +++ b/mozilla/layout/base/nsPresContext.h @@ -434,7 +434,16 @@ public: float GetPageScale() { return mPageScale; } void SetPageScale(float aScale) { mPageScale = aScale; } - + /** + * Get/set the scaling facor to use when rendering the pages for print preview. + * Only safe to get after print preview set up; safe to set anytime. + * This is a scaling factor for the display of the print preview. It + * does not affect layout. It only affects the size of the onscreen pages + * in print preview. + * XXX Temporary: see http://wiki.mozilla.org/Gecko:PrintPreview + */ + float GetPrintPreviewScale() { return mPPScale; } + void SetPrintPreviewScale(float aScale) { mPPScale = aScale; } nsIDeviceContext* DeviceContext() { return mDeviceContext; } nsIEventStateManager* EventStateManager() { return mEventManager; } @@ -775,6 +784,7 @@ protected: nsRect mVisibleArea; nsSize mPageSize; float mPageScale; + float mPPScale; nscolor mDefaultColor; nscolor mBackgroundColor; diff --git a/mozilla/layout/generic/nsSimplePageSequence.cpp b/mozilla/layout/generic/nsSimplePageSequence.cpp index 3753ebbc84c..d5a38d4bb43 100644 --- a/mozilla/layout/generic/nsSimplePageSequence.cpp +++ b/mozilla/layout/generic/nsSimplePageSequence.cpp @@ -369,8 +369,10 @@ nsSimplePageSequenceFrame::Reflow(nsPresContext* aPresContext, #endif // Return our desired size - aDesiredSize.height = y; // includes page heights and dead space - aDesiredSize.width = x + availSize.width + deadSpaceGap; + // Adjustr the reflow size by PrintPreviewScale so the scrollbars end up the + // correct size + aDesiredSize.height = y * GetPresContext()->GetPrintPreviewScale(); // includes page heights and dead space + aDesiredSize.width = (x + availSize.width + deadSpaceGap) * GetPresContext()->GetPrintPreviewScale(); aDesiredSize.mOverflowArea = nsRect(0, 0, aDesiredSize.width, aDesiredSize.height); @@ -743,7 +745,43 @@ nsSimplePageSequenceFrame::DoPageEnd() return rv; } +static void PaintPageSequence(nsIFrame* aFrame, nsIRenderingContext* aCtx, + const nsRect& aDirtyRect, nsPoint aPt) +{ + NS_STATIC_CAST(nsSimplePageSequenceFrame*, aFrame)->PaintPageSequence(*aCtx, aDirtyRect, aPt); +} + //------------------------------------------------------------------------------ +void +nsSimplePageSequenceFrame::PaintPageSequence(nsIRenderingContext& aRenderingContext, + const nsRect& aDirtyRect, + nsPoint aPt) { + nsRect rect = aDirtyRect; + float scale = GetPresContext()->GetPrintPreviewScale(); + aRenderingContext.PushState(); + nsPoint framePos = aPt; + aRenderingContext.Translate(framePos.x, framePos.y); + rect -= framePos; + aRenderingContext.Scale(scale, scale); + rect.ScaleRoundOut(1.0f / scale); + + // Now the rect and the rendering coordinates are are relative to this frame. + // Loop over the pages and paint them. + nsIFrame* child = GetFirstChild(nsnull); + while (child) { + nsPoint pt = child->GetPosition(); + // The rendering context has to be translated before each call to PaintFrame + aRenderingContext.PushState(); + aRenderingContext.Translate(pt.x, pt.y); + nsLayoutUtils::PaintFrame(&aRenderingContext, child, + nsRegion(rect - pt), NS_RGBA(0,0,0,0)); + aRenderingContext.PopState(); + child = child->GetNextSibling(); + } + + aRenderingContext.PopState(); +} + NS_IMETHODIMP nsSimplePageSequenceFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder, const nsRect& aDirtyRect, @@ -752,10 +790,11 @@ nsSimplePageSequenceFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder, nsresult rv = DisplayBorderBackgroundOutline(aBuilder, aLists); NS_ENSURE_SUCCESS(rv, rv); - // Treat each page as a psuedo-stack so everything goes in the Content() list. - return - BuildDisplayListForNonBlockChildren(aBuilder, aDirtyRect, aLists, - DISPLAY_CHILD_FORCE_PSEUDO_STACKING_CONTEXT); + rv = aLists.Content()->AppendNewToTop(new (aBuilder) + nsDisplayGeneric(this, ::PaintPageSequence, "PageSequence")); + NS_ENSURE_SUCCESS(rv, rv); + + return NS_OK; } nsIAtom* diff --git a/mozilla/layout/generic/nsSimplePageSequence.h b/mozilla/layout/generic/nsSimplePageSequence.h index ecd38ad94f1..16f1b23948a 100644 --- a/mozilla/layout/generic/nsSimplePageSequence.h +++ b/mozilla/layout/generic/nsSimplePageSequence.h @@ -131,6 +131,9 @@ public: NS_IMETHOD GetFrameName(nsAString& aResult) const; #endif + void PaintPageSequence(nsIRenderingContext& aRenderingContext, + const nsRect& aDirtyRect, + nsPoint aPt); protected: nsSimplePageSequenceFrame(nsStyleContext* aContext); @@ -175,9 +178,6 @@ protected: // I18N date formatter service which we'll want to cache locally. nsCOMPtr mDateFormatter; -private: - void CacheBackground(nsPresContext* aPresContext); - }; #endif /* nsSimplePageSequence_h___ */ diff --git a/mozilla/layout/printing/nsPrintEngine.cpp b/mozilla/layout/printing/nsPrintEngine.cpp index b38508d9883..4a583a1d6bd 100644 --- a/mozilla/layout/printing/nsPrintEngine.cpp +++ b/mozilla/layout/printing/nsPrintEngine.cpp @@ -926,9 +926,6 @@ nsPrintEngine::PrintPreview(nsIPrintSettings* aPrintSettings, rv = devspec->Init(mParentWidget, mPrt->mPrintSettings, PR_TRUE); if (NS_SUCCEEDED(rv)) { rv = mDeviceContext->GetDeviceContextFor(devspec, *getter_AddRefs(ppDC)); - if (NS_SUCCEEDED(rv)) { - mDeviceContext->SetAltDevice(ppDC); - } } } @@ -938,13 +935,7 @@ nsPrintEngine::PrintPreview(nsIPrintSettings* aPrintSettings, // we want to view every page in PrintPreview each time mPrt->mPrintSettings->SetPrintRange(nsIPrintSettings::kRangeAllPages); - mPrt->mPrintDC = mDeviceContext; - - if (mDeviceContext) { - mDeviceContext->SetUseAltDC(kUseAltDCFor_FONTMETRICS, PR_TRUE); - mDeviceContext->SetUseAltDC(kUseAltDCFor_CREATERC_REFLOW, PR_TRUE); - mDeviceContext->SetUseAltDC(kUseAltDCFor_SURFACE_DIM, PR_TRUE); - } + mPrt->mPrintDC = ppDC ? ppDC : mDeviceContext; if (aWebProgressListener != nsnull) { mPrt->mPrintProgressListeners.AppendObject(aWebProgressListener); @@ -2163,6 +2154,12 @@ nsPrintEngine::ReflowPrintObject(nsPrintObject * aPO) aPO->mPresContext->SetPageSize(adjSize); aPO->mPresContext->SetIsRootPaginatedDocument(documentIsTopLevel); aPO->mPresContext->SetPageScale(aPO->mZoomRatio); + // Calculate scale factor from printer to screen + PRInt32 printDPI = mPrt->mPrintDC->AppUnitsPerInch() / + mPrt->mPrintDC->AppUnitsPerDevPixel(); + PRInt32 screenDPI = mDeviceContext->AppUnitsPerInch() / + mDeviceContext->AppUnitsPerDevPixel(); + aPO->mPresContext->SetPrintPreviewScale(float(screenDPI) / float(printDPI)); rv = aPO->mPresShell->InitialReflow(adjSize.width, adjSize.height); @@ -3267,10 +3264,6 @@ nsPrintEngine::FinishPrintPreview() SetIsCreatingPrintPreview(PR_FALSE); - if (mPrt->mPrintDC) { - mPrt->mPrintDC->SetAltDevice(nsnull); - } - /* cleaup on failure + notify user */ if (NS_FAILED(rv)) { /* cleanup done, let's fire-up an error dialog to notify the user