1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/src/nsRenderingContext.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,149 @@ 1.4 +/* -*- Mode: C++; tab-width: 20; 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 NSRENDERINGCONTEXT__H__ 1.10 +#define NSRENDERINGCONTEXT__H__ 1.11 + 1.12 +#include <stdint.h> // for uint32_t 1.13 +#include <sys/types.h> // for int32_t 1.14 +#include "gfxContext.h" // for gfxContext 1.15 +#include "mozilla/Assertions.h" // for MOZ_ASSERT_HELPER2 1.16 +#include "mozilla/gfx/2D.h" 1.17 +#include "mozilla/gfx/UserData.h" // for UserData, UserDataKey 1.18 +#include "nsAutoPtr.h" // for nsRefPtr 1.19 +#include "nsBoundingMetrics.h" // for nsBoundingMetrics 1.20 +#include "nsColor.h" // for nscolor 1.21 +#include "nsCoord.h" // for nscoord, NSToIntRound 1.22 +#include "nsDeviceContext.h" // for nsDeviceContext 1.23 +#include "nsFontMetrics.h" // for nsFontMetrics 1.24 +#include "nsISupports.h" // for NS_INLINE_DECL_REFCOUNTING, etc 1.25 +#include "nsString.h" // for nsString 1.26 +#include "nscore.h" // for char16_t 1.27 + 1.28 +class gfxASurface; 1.29 +class nsIntRegion; 1.30 +struct nsPoint; 1.31 +struct nsRect; 1.32 + 1.33 +typedef enum { 1.34 + nsLineStyle_kNone = 0, 1.35 + nsLineStyle_kSolid = 1, 1.36 + nsLineStyle_kDashed = 2, 1.37 + nsLineStyle_kDotted = 3 1.38 +} nsLineStyle; 1.39 + 1.40 +class nsRenderingContext MOZ_FINAL 1.41 +{ 1.42 + typedef mozilla::gfx::UserData UserData; 1.43 + typedef mozilla::gfx::UserDataKey UserDataKey; 1.44 + typedef mozilla::gfx::DrawTarget DrawTarget; 1.45 + 1.46 +public: 1.47 + nsRenderingContext() : mP2A(0.) {} 1.48 + 1.49 + NS_INLINE_DECL_REFCOUNTING(nsRenderingContext) 1.50 + 1.51 + void Init(nsDeviceContext* aContext, gfxASurface* aThebesSurface); 1.52 + void Init(nsDeviceContext* aContext, gfxContext* aThebesContext); 1.53 + void Init(nsDeviceContext* aContext, DrawTarget* aDrawTarget); 1.54 + 1.55 + // These accessors will never return null. 1.56 + gfxContext *ThebesContext() { return mThebes; } 1.57 + DrawTarget *GetDrawTarget() { return mThebes->GetDrawTarget(); } 1.58 + nsDeviceContext *DeviceContext() { return mDeviceContext; } 1.59 + int32_t AppUnitsPerDevPixel() { return NSToIntRound(mP2A); } 1.60 + 1.61 + // Graphics state 1.62 + 1.63 + void PushState(void); 1.64 + void PopState(void); 1.65 + void IntersectClip(const nsRect& aRect); 1.66 + void SetClip(const nsIntRegion& aRegion); 1.67 + void SetLineStyle(nsLineStyle aLineStyle); 1.68 + void SetColor(nscolor aColor); 1.69 + void Translate(const nsPoint& aPt); 1.70 + void Scale(float aSx, float aSy); 1.71 + 1.72 + class AutoPushTranslation { 1.73 + nsRenderingContext* mCtx; 1.74 + public: 1.75 + AutoPushTranslation(nsRenderingContext* aCtx, const nsPoint& aPt) 1.76 + : mCtx(aCtx) { 1.77 + mCtx->PushState(); 1.78 + mCtx->Translate(aPt); 1.79 + } 1.80 + ~AutoPushTranslation() { 1.81 + mCtx->PopState(); 1.82 + } 1.83 + }; 1.84 + 1.85 + // Shapes 1.86 + 1.87 + void DrawLine(const nsPoint& aStartPt, const nsPoint& aEndPt); 1.88 + void DrawLine(nscoord aX0, nscoord aY0, nscoord aX1, nscoord aY1); 1.89 + void DrawRect(const nsRect& aRect); 1.90 + void DrawRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight); 1.91 + void DrawEllipse(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight); 1.92 + 1.93 + void FillRect(const nsRect& aRect); 1.94 + void FillRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight); 1.95 + void FillPolygon(const nsPoint aPoints[], int32_t aNumPoints); 1.96 + 1.97 + void FillEllipse(const nsRect& aRect); 1.98 + void FillEllipse(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight); 1.99 + 1.100 + void InvertRect(const nsRect& aRect); 1.101 + 1.102 + // Text 1.103 + 1.104 + void SetFont(nsFontMetrics *aFontMetrics); 1.105 + nsFontMetrics *FontMetrics() { return mFontMetrics; } // may be null 1.106 + 1.107 + void SetTextRunRTL(bool aIsRTL); 1.108 + 1.109 + nscoord GetWidth(char aC); 1.110 + nscoord GetWidth(char16_t aC); 1.111 + nscoord GetWidth(const nsString& aString); 1.112 + nscoord GetWidth(const char* aString); 1.113 + nscoord GetWidth(const char* aString, uint32_t aLength); 1.114 + nscoord GetWidth(const char16_t *aString, uint32_t aLength); 1.115 + 1.116 + nsBoundingMetrics GetBoundingMetrics(const char16_t *aString, 1.117 + uint32_t aLength); 1.118 + 1.119 + void DrawString(const nsString& aString, nscoord aX, nscoord aY); 1.120 + void DrawString(const char *aString, uint32_t aLength, 1.121 + nscoord aX, nscoord aY); 1.122 + void DrawString(const char16_t *aString, uint32_t aLength, 1.123 + nscoord aX, nscoord aY); 1.124 + 1.125 + void AddUserData(UserDataKey *key, void *userData, void (*destroy)(void*)) { 1.126 + mUserData.Add(key, userData, destroy); 1.127 + } 1.128 + void *GetUserData(UserDataKey *key) { 1.129 + return mUserData.Get(key); 1.130 + } 1.131 + void *RemoveUserData(UserDataKey *key) { 1.132 + return mUserData.Remove(key); 1.133 + } 1.134 + 1.135 +private: 1.136 + // Private destructor, to discourage deletion outside of Release(): 1.137 + ~nsRenderingContext() 1.138 + { 1.139 + } 1.140 + 1.141 + int32_t GetMaxChunkLength(); 1.142 + 1.143 + nsRefPtr<gfxContext> mThebes; 1.144 + nsRefPtr<nsDeviceContext> mDeviceContext; 1.145 + nsRefPtr<nsFontMetrics> mFontMetrics; 1.146 + 1.147 + double mP2A; // cached app units per device pixel value 1.148 + 1.149 + UserData mUserData; 1.150 +}; 1.151 + 1.152 +#endif // NSRENDERINGCONTEXT__H__