• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef SKIA_EXT_VECTOR_PLATFORM_DEVICE_EMF_WIN_H_
6 #define SKIA_EXT_VECTOR_PLATFORM_DEVICE_EMF_WIN_H_
7 
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "skia/ext/platform_device.h"
11 #include "third_party/skia/include/core/SkMatrix.h"
12 #include "third_party/skia/include/core/SkRegion.h"
13 
14 namespace skia {
15 
16 // A device is basically a wrapper around SkBitmap that provides a surface for
17 // SkCanvas to draw into. This specific device is not not backed by a surface
18 // and is thus unreadable. This is because the backend is completely vectorial.
19 // This device is a simple wrapper over a Windows device context (HDC) handle.
20 // TODO(robertphillips): Once Skia's SkBaseDevice is refactored to remove
21 // the bitmap-specific entry points, this class should derive from it.
22 class VectorPlatformDeviceEmf : public SkBitmapDevice, public PlatformDevice {
23  public:
24   SK_API static SkBaseDevice* CreateDevice(int width, int height, bool isOpaque,
25                                            HANDLE shared_section);
26 
27   // Factory function. The DC is kept as the output context.
28   static SkBaseDevice* create(HDC dc, int width, int height);
29 
30   VectorPlatformDeviceEmf(HDC dc, const SkBitmap& bitmap);
31   virtual ~VectorPlatformDeviceEmf();
32 
33   // PlatformDevice methods
34   virtual PlatformSurface BeginPlatformPaint() OVERRIDE;
35   virtual void DrawToNativeContext(HDC dc, int x, int y,
36                                    const RECT* src_rect) OVERRIDE;
37   // SkBaseDevice methods.
38   virtual uint32_t getDeviceCapabilities();
39   virtual void drawPaint(const SkDraw& draw, const SkPaint& paint) OVERRIDE;
40   virtual void drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
41                           size_t count, const SkPoint[],
42                           const SkPaint& paint) OVERRIDE;
43   virtual void drawRect(const SkDraw& draw, const SkRect& r,
44                         const SkPaint& paint) OVERRIDE;
45   virtual void drawRRect(const SkDraw&, const SkRRect& rr,
46                          const SkPaint& paint) OVERRIDE;
47   virtual void drawPath(const SkDraw& draw, const SkPath& path,
48                         const SkPaint& paint,
49                         const SkMatrix* prePathMatrix = NULL,
50                         bool pathIsMutable = false) OVERRIDE;
51   virtual void drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
52                               const SkRect* src, const SkRect& dst,
53                               const SkPaint& paint,
54                               SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE;
55   virtual void drawBitmap(const SkDraw& draw, const SkBitmap& bitmap,
56                           const SkMatrix& matrix,
57                           const SkPaint& paint) OVERRIDE;
58   virtual void drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
59                           int x, int y, const SkPaint& paint) OVERRIDE;
60   virtual void drawText(const SkDraw& draw, const void* text, size_t len,
61                         SkScalar x, SkScalar y, const SkPaint& paint) OVERRIDE;
62   virtual void drawPosText(const SkDraw& draw, const void* text, size_t len,
63                            const SkScalar pos[], SkScalar constY,
64                            int scalarsPerPos, const SkPaint& paint) OVERRIDE;
65   virtual void drawTextOnPath(const SkDraw& draw, const void* text, size_t len,
66                               const SkPath& path, const SkMatrix* matrix,
67                               const SkPaint& paint) OVERRIDE;
68   virtual void drawVertices(const SkDraw& draw, SkCanvas::VertexMode,
69                             int vertexCount,
70                             const SkPoint verts[], const SkPoint texs[],
71                             const SkColor colors[], SkXfermode* xmode,
72                             const uint16_t indices[], int indexCount,
73                             const SkPaint& paint) OVERRIDE;
74   virtual void drawDevice(const SkDraw& draw, SkBaseDevice*, int x, int y,
75                           const SkPaint&) OVERRIDE;
76 
77   virtual void setMatrixClip(const SkMatrix& transform, const SkRegion& region,
78                              const SkClipStack&) OVERRIDE;
79 
80   void LoadClipRegion();
81 
82  protected:
83   virtual SkBaseDevice* onCreateCompatibleDevice(SkBitmap::Config, int width,
84                                                  int height, bool isOpaque,
85                                                  Usage usage) OVERRIDE;
86 
87  private:
88   // Applies the SkPaint's painting properties in the current GDI context, if
89   // possible. If GDI can't support all paint's properties, returns false. It
90   // doesn't execute the "commands" in SkPaint.
91   bool ApplyPaint(const SkPaint& paint);
92 
93   // Selects a new object in the device context. It can be a pen, a brush, a
94   // clipping region, a bitmap or a font. Returns the old selected object.
95   HGDIOBJ SelectObject(HGDIOBJ object);
96 
97   // Creates a brush according to SkPaint's properties.
98   bool CreateBrush(bool use_brush, const SkPaint& paint);
99 
100   // Creates a pen according to SkPaint's properties.
101   bool CreatePen(bool use_pen, const SkPaint& paint);
102 
103   // Restores back the previous objects (pen, brush, etc) after a paint command.
104   void Cleanup();
105 
106   // Creates a brush according to SkPaint's properties.
107   bool CreateBrush(bool use_brush, COLORREF color);
108 
109   // Creates a pen according to SkPaint's properties.
110   bool CreatePen(bool use_pen, COLORREF color, int stroke_width,
111                  float stroke_miter, DWORD pen_style);
112 
113   // Draws a bitmap in the the device, using the currently loaded matrix.
114   void InternalDrawBitmap(const SkBitmap& bitmap, int x, int y,
115                           const SkPaint& paint);
116 
117   // The Windows Device Context handle. It is the backend used with GDI drawing.
118   // This backend is write-only and vectorial.
119   HDC hdc_;
120 
121   // Translation assigned to the DC: we need to keep track of this separately
122   // so it can be updated even if the DC isn't created yet.
123   SkMatrix transform_;
124 
125   // The current clipping
126   SkRegion clip_region_;
127 
128   // Previously selected brush before the current drawing.
129   HGDIOBJ previous_brush_;
130 
131   // Previously selected pen before the current drawing.
132   HGDIOBJ previous_pen_;
133 
134   DISALLOW_COPY_AND_ASSIGN(VectorPlatformDeviceEmf);
135 };
136 
137 typedef void (*SkiaEnsureTypefaceCharactersAccessible)
138     (const LOGFONT& font, const wchar_t* text, unsigned int text_length);
139 
140 SK_API void SetSkiaEnsureTypefaceCharactersAccessible(
141     SkiaEnsureTypefaceCharactersAccessible func);
142 
143 }  // namespace skia
144 
145 #endif  // SKIA_EXT_VECTOR_PLATFORM_DEVICE_EMF_WIN_H_
146