1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/src/nsDeviceContext.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,281 @@ 1.4 +/* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef _NS_DEVICECONTEXT_H_ 1.10 +#define _NS_DEVICECONTEXT_H_ 1.11 + 1.12 +#include <stdint.h> // for uint32_t 1.13 +#include <sys/types.h> // for int32_t 1.14 +#include "gfxTypes.h" // for gfxFloat 1.15 +#include "mozilla/Assertions.h" // for MOZ_ASSERT_HELPER2 1.16 +#include "nsAutoPtr.h" // for nsRefPtr 1.17 +#include "nsCOMPtr.h" // for nsCOMPtr 1.18 +#include "nsCoord.h" // for nscoord 1.19 +#include "nsError.h" // for nsresult 1.20 +#include "nsISupports.h" // for NS_INLINE_DECL_REFCOUNTING 1.21 +#include "nsMathUtils.h" // for NS_round 1.22 +#include "nscore.h" // for char16_t, nsAString 1.23 +#include "mozilla/AppUnits.h" // for AppUnits 1.24 + 1.25 +class gfxASurface; 1.26 +class gfxTextPerfMetrics; 1.27 +class gfxUserFontSet; 1.28 +class nsFont; 1.29 +class nsFontCache; 1.30 +class nsFontMetrics; 1.31 +class nsIAtom; 1.32 +class nsIDeviceContextSpec; 1.33 +class nsIScreen; 1.34 +class nsIScreenManager; 1.35 +class nsIWidget; 1.36 +class nsRect; 1.37 +class nsRenderingContext; 1.38 + 1.39 +class nsDeviceContext MOZ_FINAL 1.40 +{ 1.41 +public: 1.42 + nsDeviceContext(); 1.43 + 1.44 + NS_INLINE_DECL_REFCOUNTING(nsDeviceContext) 1.45 + 1.46 + /** 1.47 + * Initialize the device context from a widget 1.48 + * @param aWidget a widget to initialize the device context from 1.49 + * @return error status 1.50 + */ 1.51 + nsresult Init(nsIWidget *aWidget); 1.52 + 1.53 + /** 1.54 + * Initialize the device context from a device context spec 1.55 + * @param aDevSpec the specification of the printing device 1.56 + * @return error status 1.57 + */ 1.58 + nsresult InitForPrinting(nsIDeviceContextSpec *aDevSpec); 1.59 + 1.60 + /** 1.61 + * Create a rendering context and initialize it. Only call this 1.62 + * method on device contexts that were initialized for printing. 1.63 + * 1.64 + * @return the new rendering context (guaranteed to be non-null) 1.65 + */ 1.66 + already_AddRefed<nsRenderingContext> CreateRenderingContext(); 1.67 + 1.68 + /** 1.69 + * Gets the number of app units in one CSS pixel; this number is global, 1.70 + * not unique to each device context. 1.71 + */ 1.72 + static int32_t AppUnitsPerCSSPixel() { return mozilla::AppUnitsPerCSSPixel(); } 1.73 + 1.74 + /** 1.75 + * Gets the number of app units in one device pixel; this number 1.76 + * is usually a factor of AppUnitsPerCSSPixel(), although that is 1.77 + * not guaranteed. 1.78 + */ 1.79 + int32_t AppUnitsPerDevPixel() const { return mAppUnitsPerDevPixel; } 1.80 + 1.81 + /** 1.82 + * Convert device pixels which is used for gfx/thebes to nearest 1.83 + * (rounded) app units 1.84 + */ 1.85 + nscoord GfxUnitsToAppUnits(gfxFloat aGfxUnits) const 1.86 + { return nscoord(NS_round(aGfxUnits * AppUnitsPerDevPixel())); } 1.87 + 1.88 + /** 1.89 + * Convert app units to device pixels which is used for gfx/thebes. 1.90 + */ 1.91 + gfxFloat AppUnitsToGfxUnits(nscoord aAppUnits) const 1.92 + { return gfxFloat(aAppUnits) / AppUnitsPerDevPixel(); } 1.93 + 1.94 + /** 1.95 + * Gets the number of app units in one physical inch; this is the 1.96 + * device's DPI times AppUnitsPerDevPixel(). 1.97 + */ 1.98 + int32_t AppUnitsPerPhysicalInch() const 1.99 + { return mAppUnitsPerPhysicalInch; } 1.100 + 1.101 + /** 1.102 + * Gets the number of app units in one CSS inch; this is 1.103 + * 96 times AppUnitsPerCSSPixel. 1.104 + */ 1.105 + static int32_t AppUnitsPerCSSInch() { return mozilla::AppUnitsPerCSSInch(); } 1.106 + 1.107 + /** 1.108 + * Get the unscaled ratio of app units to dev pixels; useful if something 1.109 + * needs to be converted from to unscaled pixels 1.110 + */ 1.111 + int32_t UnscaledAppUnitsPerDevPixel() const 1.112 + { return mAppUnitsPerDevNotScaledPixel; } 1.113 + 1.114 + /** 1.115 + * Get the nsFontMetrics that describe the properties of 1.116 + * an nsFont. 1.117 + * @param aFont font description to obtain metrics for 1.118 + * @param aLanguage the language of the document 1.119 + * @param aMetrics out parameter for font metrics 1.120 + * @param aUserFontSet user font set 1.121 + * @return error status 1.122 + */ 1.123 + nsresult GetMetricsFor(const nsFont& aFont, nsIAtom* aLanguage, 1.124 + gfxUserFontSet* aUserFontSet, 1.125 + gfxTextPerfMetrics* aTextPerf, 1.126 + nsFontMetrics*& aMetrics); 1.127 + 1.128 + /** 1.129 + * Notification when a font metrics instance created for this device is 1.130 + * about to be deleted 1.131 + */ 1.132 + nsresult FontMetricsDeleted(const nsFontMetrics* aFontMetrics); 1.133 + 1.134 + /** 1.135 + * Attempt to free up resources by flushing out any fonts no longer 1.136 + * referenced by anything other than the font cache itself. 1.137 + * @return error status 1.138 + */ 1.139 + nsresult FlushFontCache(); 1.140 + 1.141 + /** 1.142 + * Return the bit depth of the device. 1.143 + */ 1.144 + nsresult GetDepth(uint32_t& aDepth); 1.145 + 1.146 + /** 1.147 + * Get the size of the displayable area of the output device 1.148 + * in app units. 1.149 + * @param aWidth out parameter for width 1.150 + * @param aHeight out parameter for height 1.151 + * @return error status 1.152 + */ 1.153 + nsresult GetDeviceSurfaceDimensions(nscoord& aWidth, nscoord& aHeight); 1.154 + 1.155 + /** 1.156 + * Get the size of the content area of the output device in app 1.157 + * units. This corresponds on a screen device, for instance, to 1.158 + * the entire screen. 1.159 + * @param aRect out parameter for full rect. Position (x,y) will 1.160 + * be (0,0) or relative to the primary monitor if 1.161 + * this is not the primary. 1.162 + * @return error status 1.163 + */ 1.164 + nsresult GetRect(nsRect& aRect); 1.165 + 1.166 + /** 1.167 + * Get the size of the content area of the output device in app 1.168 + * units. This corresponds on a screen device, for instance, to 1.169 + * the area reported by GetDeviceSurfaceDimensions, minus the 1.170 + * taskbar (Windows) or menubar (Macintosh). 1.171 + * @param aRect out parameter for client rect. Position (x,y) will 1.172 + * be (0,0) adjusted for any upper/left non-client 1.173 + * space if present or relative to the primary 1.174 + * monitor if this is not the primary. 1.175 + * @return error status 1.176 + */ 1.177 + nsresult GetClientRect(nsRect& aRect); 1.178 + 1.179 + /** 1.180 + * Inform the output device that output of a document is beginning 1.181 + * Used for print related device contexts. Must be matched 1:1 with 1.182 + * EndDocument() or AbortDocument(). 1.183 + * 1.184 + * @param aTitle - title of Document 1.185 + * @param aPrintToFileName - name of file to print to, if nullptr 1.186 + * then don't print to file 1.187 + * @param aStartPage - starting page number (must be greater than zero) 1.188 + * @param aEndPage - ending page number (must be less than or 1.189 + * equal to number of pages) 1.190 + * 1.191 + * @return error status 1.192 + */ 1.193 + nsresult BeginDocument(const nsAString& aTitle, 1.194 + char16_t* aPrintToFileName, 1.195 + int32_t aStartPage, 1.196 + int32_t aEndPage); 1.197 + 1.198 + /** 1.199 + * Inform the output device that output of a document is ending. 1.200 + * Used for print related device contexts. Must be matched 1:1 with 1.201 + * BeginDocument() 1.202 + * @return error status 1.203 + */ 1.204 + nsresult EndDocument(); 1.205 + 1.206 + /** 1.207 + * Inform the output device that output of a document is being aborted. 1.208 + * Must be matched 1:1 with BeginDocument() 1.209 + * @return error status 1.210 + */ 1.211 + nsresult AbortDocument(); 1.212 + 1.213 + /** 1.214 + * Inform the output device that output of a page is beginning 1.215 + * Used for print related device contexts. Must be matched 1:1 with 1.216 + * EndPage() and within a BeginDocument()/EndDocument() pair. 1.217 + * @return error status 1.218 + */ 1.219 + nsresult BeginPage(); 1.220 + 1.221 + /** 1.222 + * Inform the output device that output of a page is ending 1.223 + * Used for print related device contexts. Must be matched 1:1 with 1.224 + * BeginPage() and within a BeginDocument()/EndDocument() pair. 1.225 + * @return error status 1.226 + */ 1.227 + nsresult EndPage(); 1.228 + 1.229 + /** 1.230 + * Check to see if the DPI has changed 1.231 + * @return whether there was actually a change in the DPI (whether 1.232 + * AppUnitsPerDevPixel() or AppUnitsPerPhysicalInch() 1.233 + * changed) 1.234 + */ 1.235 + bool CheckDPIChange(); 1.236 + 1.237 + /** 1.238 + * Set the pixel scaling factor: all lengths are multiplied by this factor 1.239 + * when we convert them to device pixels. Returns whether the ratio of 1.240 + * app units to dev pixels changed because of the scale factor. 1.241 + */ 1.242 + bool SetPixelScale(float aScale); 1.243 + 1.244 + /** 1.245 + * Returns the pixel scaling factor (page zoom factor) applied. 1.246 + */ 1.247 + float GetPixelScale() const { return mPixelScale; } 1.248 + 1.249 + /** 1.250 + * True if this device context was created for printing. 1.251 + */ 1.252 + bool IsPrinterSurface(); 1.253 + 1.254 +private: 1.255 + // Private destructor, to discourage deletion outside of Release(): 1.256 + ~nsDeviceContext(); 1.257 + 1.258 + void SetDPI(); 1.259 + void ComputeClientRectUsingScreen(nsRect *outRect); 1.260 + void ComputeFullAreaUsingScreen(nsRect *outRect); 1.261 + void FindScreen(nsIScreen **outScreen); 1.262 + void CalcPrintingSize(); 1.263 + void UpdateScaledAppUnits(); 1.264 + 1.265 + nscoord mWidth; 1.266 + nscoord mHeight; 1.267 + uint32_t mDepth; 1.268 + int32_t mAppUnitsPerDevPixel; 1.269 + int32_t mAppUnitsPerDevNotScaledPixel; 1.270 + int32_t mAppUnitsPerPhysicalInch; 1.271 + float mPixelScale; 1.272 + float mPrintingScale; 1.273 + 1.274 + nsFontCache* mFontCache; 1.275 + nsCOMPtr<nsIWidget> mWidget; 1.276 + nsCOMPtr<nsIScreenManager> mScreenManager; 1.277 + nsCOMPtr<nsIDeviceContextSpec> mDeviceContextSpec; 1.278 + nsRefPtr<gfxASurface> mPrintingSurface; 1.279 +#ifdef XP_MACOSX 1.280 + nsRefPtr<gfxASurface> mCachedPrintingSurface; 1.281 +#endif 1.282 +}; 1.283 + 1.284 +#endif /* _NS_DEVICECONTEXT_H_ */