michael@0: /* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef _NS_DEVICECONTEXT_H_ michael@0: #define _NS_DEVICECONTEXT_H_ michael@0: michael@0: #include // for uint32_t michael@0: #include // for int32_t michael@0: #include "gfxTypes.h" // for gfxFloat michael@0: #include "mozilla/Assertions.h" // for MOZ_ASSERT_HELPER2 michael@0: #include "nsAutoPtr.h" // for nsRefPtr michael@0: #include "nsCOMPtr.h" // for nsCOMPtr michael@0: #include "nsCoord.h" // for nscoord michael@0: #include "nsError.h" // for nsresult michael@0: #include "nsISupports.h" // for NS_INLINE_DECL_REFCOUNTING michael@0: #include "nsMathUtils.h" // for NS_round michael@0: #include "nscore.h" // for char16_t, nsAString michael@0: #include "mozilla/AppUnits.h" // for AppUnits michael@0: michael@0: class gfxASurface; michael@0: class gfxTextPerfMetrics; michael@0: class gfxUserFontSet; michael@0: class nsFont; michael@0: class nsFontCache; michael@0: class nsFontMetrics; michael@0: class nsIAtom; michael@0: class nsIDeviceContextSpec; michael@0: class nsIScreen; michael@0: class nsIScreenManager; michael@0: class nsIWidget; michael@0: class nsRect; michael@0: class nsRenderingContext; michael@0: michael@0: class nsDeviceContext MOZ_FINAL michael@0: { michael@0: public: michael@0: nsDeviceContext(); michael@0: michael@0: NS_INLINE_DECL_REFCOUNTING(nsDeviceContext) michael@0: michael@0: /** michael@0: * Initialize the device context from a widget michael@0: * @param aWidget a widget to initialize the device context from michael@0: * @return error status michael@0: */ michael@0: nsresult Init(nsIWidget *aWidget); michael@0: michael@0: /** michael@0: * Initialize the device context from a device context spec michael@0: * @param aDevSpec the specification of the printing device michael@0: * @return error status michael@0: */ michael@0: nsresult InitForPrinting(nsIDeviceContextSpec *aDevSpec); michael@0: michael@0: /** michael@0: * Create a rendering context and initialize it. Only call this michael@0: * method on device contexts that were initialized for printing. michael@0: * michael@0: * @return the new rendering context (guaranteed to be non-null) michael@0: */ michael@0: already_AddRefed CreateRenderingContext(); michael@0: michael@0: /** michael@0: * Gets the number of app units in one CSS pixel; this number is global, michael@0: * not unique to each device context. michael@0: */ michael@0: static int32_t AppUnitsPerCSSPixel() { return mozilla::AppUnitsPerCSSPixel(); } michael@0: michael@0: /** michael@0: * Gets the number of app units in one device pixel; this number michael@0: * is usually a factor of AppUnitsPerCSSPixel(), although that is michael@0: * not guaranteed. michael@0: */ michael@0: int32_t AppUnitsPerDevPixel() const { return mAppUnitsPerDevPixel; } michael@0: michael@0: /** michael@0: * Convert device pixels which is used for gfx/thebes to nearest michael@0: * (rounded) app units michael@0: */ michael@0: nscoord GfxUnitsToAppUnits(gfxFloat aGfxUnits) const michael@0: { return nscoord(NS_round(aGfxUnits * AppUnitsPerDevPixel())); } michael@0: michael@0: /** michael@0: * Convert app units to device pixels which is used for gfx/thebes. michael@0: */ michael@0: gfxFloat AppUnitsToGfxUnits(nscoord aAppUnits) const michael@0: { return gfxFloat(aAppUnits) / AppUnitsPerDevPixel(); } michael@0: michael@0: /** michael@0: * Gets the number of app units in one physical inch; this is the michael@0: * device's DPI times AppUnitsPerDevPixel(). michael@0: */ michael@0: int32_t AppUnitsPerPhysicalInch() const michael@0: { return mAppUnitsPerPhysicalInch; } michael@0: michael@0: /** michael@0: * Gets the number of app units in one CSS inch; this is michael@0: * 96 times AppUnitsPerCSSPixel. michael@0: */ michael@0: static int32_t AppUnitsPerCSSInch() { return mozilla::AppUnitsPerCSSInch(); } michael@0: michael@0: /** michael@0: * Get the unscaled ratio of app units to dev pixels; useful if something michael@0: * needs to be converted from to unscaled pixels michael@0: */ michael@0: int32_t UnscaledAppUnitsPerDevPixel() const michael@0: { return mAppUnitsPerDevNotScaledPixel; } michael@0: michael@0: /** michael@0: * Get the nsFontMetrics that describe the properties of michael@0: * an nsFont. michael@0: * @param aFont font description to obtain metrics for michael@0: * @param aLanguage the language of the document michael@0: * @param aMetrics out parameter for font metrics michael@0: * @param aUserFontSet user font set michael@0: * @return error status michael@0: */ michael@0: nsresult GetMetricsFor(const nsFont& aFont, nsIAtom* aLanguage, michael@0: gfxUserFontSet* aUserFontSet, michael@0: gfxTextPerfMetrics* aTextPerf, michael@0: nsFontMetrics*& aMetrics); michael@0: michael@0: /** michael@0: * Notification when a font metrics instance created for this device is michael@0: * about to be deleted michael@0: */ michael@0: nsresult FontMetricsDeleted(const nsFontMetrics* aFontMetrics); michael@0: michael@0: /** michael@0: * Attempt to free up resources by flushing out any fonts no longer michael@0: * referenced by anything other than the font cache itself. michael@0: * @return error status michael@0: */ michael@0: nsresult FlushFontCache(); michael@0: michael@0: /** michael@0: * Return the bit depth of the device. michael@0: */ michael@0: nsresult GetDepth(uint32_t& aDepth); michael@0: michael@0: /** michael@0: * Get the size of the displayable area of the output device michael@0: * in app units. michael@0: * @param aWidth out parameter for width michael@0: * @param aHeight out parameter for height michael@0: * @return error status michael@0: */ michael@0: nsresult GetDeviceSurfaceDimensions(nscoord& aWidth, nscoord& aHeight); michael@0: michael@0: /** michael@0: * Get the size of the content area of the output device in app michael@0: * units. This corresponds on a screen device, for instance, to michael@0: * the entire screen. michael@0: * @param aRect out parameter for full rect. Position (x,y) will michael@0: * be (0,0) or relative to the primary monitor if michael@0: * this is not the primary. michael@0: * @return error status michael@0: */ michael@0: nsresult GetRect(nsRect& aRect); michael@0: michael@0: /** michael@0: * Get the size of the content area of the output device in app michael@0: * units. This corresponds on a screen device, for instance, to michael@0: * the area reported by GetDeviceSurfaceDimensions, minus the michael@0: * taskbar (Windows) or menubar (Macintosh). michael@0: * @param aRect out parameter for client rect. Position (x,y) will michael@0: * be (0,0) adjusted for any upper/left non-client michael@0: * space if present or relative to the primary michael@0: * monitor if this is not the primary. michael@0: * @return error status michael@0: */ michael@0: nsresult GetClientRect(nsRect& aRect); michael@0: michael@0: /** michael@0: * Inform the output device that output of a document is beginning michael@0: * Used for print related device contexts. Must be matched 1:1 with michael@0: * EndDocument() or AbortDocument(). michael@0: * michael@0: * @param aTitle - title of Document michael@0: * @param aPrintToFileName - name of file to print to, if nullptr michael@0: * then don't print to file michael@0: * @param aStartPage - starting page number (must be greater than zero) michael@0: * @param aEndPage - ending page number (must be less than or michael@0: * equal to number of pages) michael@0: * michael@0: * @return error status michael@0: */ michael@0: nsresult BeginDocument(const nsAString& aTitle, michael@0: char16_t* aPrintToFileName, michael@0: int32_t aStartPage, michael@0: int32_t aEndPage); michael@0: michael@0: /** michael@0: * Inform the output device that output of a document is ending. michael@0: * Used for print related device contexts. Must be matched 1:1 with michael@0: * BeginDocument() michael@0: * @return error status michael@0: */ michael@0: nsresult EndDocument(); michael@0: michael@0: /** michael@0: * Inform the output device that output of a document is being aborted. michael@0: * Must be matched 1:1 with BeginDocument() michael@0: * @return error status michael@0: */ michael@0: nsresult AbortDocument(); michael@0: michael@0: /** michael@0: * Inform the output device that output of a page is beginning michael@0: * Used for print related device contexts. Must be matched 1:1 with michael@0: * EndPage() and within a BeginDocument()/EndDocument() pair. michael@0: * @return error status michael@0: */ michael@0: nsresult BeginPage(); michael@0: michael@0: /** michael@0: * Inform the output device that output of a page is ending michael@0: * Used for print related device contexts. Must be matched 1:1 with michael@0: * BeginPage() and within a BeginDocument()/EndDocument() pair. michael@0: * @return error status michael@0: */ michael@0: nsresult EndPage(); michael@0: michael@0: /** michael@0: * Check to see if the DPI has changed michael@0: * @return whether there was actually a change in the DPI (whether michael@0: * AppUnitsPerDevPixel() or AppUnitsPerPhysicalInch() michael@0: * changed) michael@0: */ michael@0: bool CheckDPIChange(); michael@0: michael@0: /** michael@0: * Set the pixel scaling factor: all lengths are multiplied by this factor michael@0: * when we convert them to device pixels. Returns whether the ratio of michael@0: * app units to dev pixels changed because of the scale factor. michael@0: */ michael@0: bool SetPixelScale(float aScale); michael@0: michael@0: /** michael@0: * Returns the pixel scaling factor (page zoom factor) applied. michael@0: */ michael@0: float GetPixelScale() const { return mPixelScale; } michael@0: michael@0: /** michael@0: * True if this device context was created for printing. michael@0: */ michael@0: bool IsPrinterSurface(); michael@0: michael@0: private: michael@0: // Private destructor, to discourage deletion outside of Release(): michael@0: ~nsDeviceContext(); michael@0: michael@0: void SetDPI(); michael@0: void ComputeClientRectUsingScreen(nsRect *outRect); michael@0: void ComputeFullAreaUsingScreen(nsRect *outRect); michael@0: void FindScreen(nsIScreen **outScreen); michael@0: void CalcPrintingSize(); michael@0: void UpdateScaledAppUnits(); michael@0: michael@0: nscoord mWidth; michael@0: nscoord mHeight; michael@0: uint32_t mDepth; michael@0: int32_t mAppUnitsPerDevPixel; michael@0: int32_t mAppUnitsPerDevNotScaledPixel; michael@0: int32_t mAppUnitsPerPhysicalInch; michael@0: float mPixelScale; michael@0: float mPrintingScale; michael@0: michael@0: nsFontCache* mFontCache; michael@0: nsCOMPtr mWidget; michael@0: nsCOMPtr mScreenManager; michael@0: nsCOMPtr mDeviceContextSpec; michael@0: nsRefPtr mPrintingSurface; michael@0: #ifdef XP_MACOSX michael@0: nsRefPtr mCachedPrintingSurface; michael@0: #endif michael@0: }; michael@0: michael@0: #endif /* _NS_DEVICECONTEXT_H_ */