update dev300-m58
[ooovba.git] / canvas / source / cairo / cairo_win32_cairo.cxx
blob9df46bd6cde1847c3d404a4b11bf63ba90fee3e3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: cairo_win32_cairo.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_canvas.hxx"
34 #ifdef WNT
35 /************************************************************************
36 * Win32 surface backend for OpenOffice.org Cairo Canvas *
37 ************************************************************************/
39 #include <tools/prewin.h>
40 #include <windows.h>
41 #include <tools/postwin.h>
43 #include <osl/diagnose.h>
44 #include <vcl/bitmap.hxx>
45 #include <vcl/virdev.hxx>
46 #include <vcl/sysdata.hxx>
48 #include "cairo_win32_cairo.hxx"
50 #ifdef CAIRO_HAS_WIN32_SURFACE
52 namespace cairo
55 #include <cairo-win32.h>
57 bool IsCairoWorking( OutputDevice* )
59 // trivially true for Windows
60 return true;
63 /**
64 * Surface::Surface: Create generic Canvas surface using given Cairo Surface
66 * @param pSurface Cairo Surface
68 * This constructor only stores data, it does no processing.
69 * It is used with e.g. cairo_image_surface_create_for_data()
70 * and Surface::getSimilar()
72 * Set the mpSurface to the new surface or NULL
73 **/
74 Win32Surface::Win32Surface( const CairoSurfaceSharedPtr& pSurface ) :
75 mpSurface( pSurface )
78 /**
79 * Surface::Surface: Create Canvas surface from Window reference.
80 * @param pSysData Platform native system environment data (struct SystemEnvData in vcl/inc/sysdata.hxx)
81 * @param x horizontal location of the new surface
82 * @param y vertical location of the new surface
84 * pSysData contains the platform native Window reference.
85 * pSysData is used to create a surface on the Window
87 * Set the mpSurface to the new surface or NULL
88 **/
89 Win32Surface::Win32Surface( HDC hDC, int x, int y) :
90 mpSurface(
91 cairo_win32_surface_create(hDC),
92 &cairo_surface_destroy)
94 cairo_surface_set_device_offset( mpSurface.get(), x, y );
97 /**
98 * Surface::Surface: Create platfrom native Canvas surface from BitmapSystemData
99 * @param pBmpData Platform native image data (struct BitmapSystemData in vcl/inc/bitmap.hxx)
101 * Create a surface based on image data on pBmpData
103 * Set the mpSurface to the new surface or NULL
105 Win32Surface::Win32Surface( const BitmapSystemData& rBmpData ) :
106 mpSurface()
108 OSL_ASSERT(rBmpData.pDIB == NULL);
110 if(rBmpData.pDIB != NULL) {
111 // So just leave mpSurface to NULL, little else we can do at
112 // this stage. Hopefully the Win32 patch to
113 // cairocanvas::DeviceHelper::getSurface(BitmapSystemData&,
114 // const Size&) will catch the cases where this
115 // constructor would be called with a DIB bitmap, and we
116 // will never get here. At least it worked for Ballmer.ppt.
118 else
120 HDC hDC = CreateCompatibleDC(NULL);
121 void* hOrigBitmap;
122 OSL_TRACE ("Surface::Surface(): Selecting bitmap %p into DC %p", rBmpData.pDDB, hDC);
123 hOrigBitmap = SelectObject( hDC, (HANDLE)rBmpData.pDDB );
124 if(hOrigBitmap == NULL)
125 OSL_TRACE ("SelectObject failed: %d", GetLastError ());
126 mpSurface.reset(
127 cairo_win32_surface_create(hDC),
128 &cairo_surface_destroy);
133 * Surface::getCairo: Create Cairo (drawing object) for the Canvas surface
135 * @return new Cairo or NULL
137 CairoSharedPtr Win32Surface::getCairo() const
139 return CairoSharedPtr( cairo_create(mpSurface.get()),
140 &cairo_destroy );
144 * Surface::getSimilar: Create new similar Canvas surface
145 * @param aContent format of the new surface (cairo_content_t from cairo/src/cairo.h)
146 * @param width width of the new surface
147 * @param height height of the new surface
149 * Creates a new Canvas surface. This normally creates platform native surface, even though
150 * generic function is used.
152 * Cairo surface from aContent (cairo_content_t)
154 * @return new surface or NULL
155 **/
156 SurfaceSharedPtr Win32Surface::getSimilar( Content aContent, int width, int height ) const
158 return SurfaceSharedPtr(
159 new Win32Surface(
160 CairoSurfaceSharedPtr(
161 cairo_surface_create_similar( mpSurface.get(), aContent, width, height ),
162 &cairo_surface_destroy )));
166 * Surface::Resize: Resizes the Canvas surface.
167 * @param width new width of the surface
168 * @param height new height of the surface
170 * Only used on X11.
172 * @return The new surface or NULL
174 void Win32Surface::Resize( int /*width*/, int /*height*/ )
176 OSL_ENSURE(false,"not supposed to be called!");
179 void Win32Surface::flush() const
181 GdiFlush();
185 * Surface::getDepth: Get the color depth of the Canvas surface.
187 * @return color depth
189 int Win32Surface::getDepth() const
191 if (mpSurface) {
192 switch (cairo_surface_get_content (mpSurface.get())) {
193 case CAIRO_CONTENT_ALPHA: return 8; break;
194 case CAIRO_CONTENT_COLOR: return 24; break;
195 case CAIRO_CONTENT_COLOR_ALPHA: return 32; break;
198 OSL_TRACE("Canvas::cairo::Surface::getDepth(): ERROR - depth unspecified!");
199 return -1;
204 * cairo::createVirtualDevice: Create a VCL virtual device for the CGContext in the cairo Surface
206 * @return The new virtual device
208 boost::shared_ptr<VirtualDevice> Win32Surface::createVirtualDevice() const
210 SystemGraphicsData aSystemGraphicsData;
211 aSystemGraphicsData.nSize = sizeof(SystemGraphicsData);
212 aSystemGraphicsData.hDC = cairo_win32_surface_get_dc( mpSurface.get() );
214 return boost::shared_ptr<VirtualDevice>(
215 new VirtualDevice( &aSystemGraphicsData, sal::static_int_cast<USHORT>(getDepth()) ));
220 * cairo::createSurface: Create generic Canvas surface using given Cairo Surface
222 * @param rSurface Cairo Surface
224 * @return new Surface
226 SurfaceSharedPtr createSurface( const CairoSurfaceSharedPtr& rSurface )
228 return SurfaceSharedPtr(new Win32Surface(rSurface));
233 * cairo::createSurface: Create Canvas surface using given VCL Window or Virtualdevice
235 * @param rSurface Cairo Surface
237 * For VCL Window, use platform native system environment data (struct SystemEnvData in vcl/inc/sysdata.hxx)
238 * For VCL Virtualdevice, use platform native system graphics data (struct SystemGraphicsData in vcl/inc/sysdata.hxx)
240 * @return new Surface
242 SurfaceSharedPtr createSurface( const OutputDevice& rRefDevice,
243 int x, int y, int /* width */, int /* height */)
245 SurfaceSharedPtr surf;
247 if( rRefDevice.GetOutDevType() == OUTDEV_WINDOW )
249 const Window &rWindow = (const Window &) rRefDevice;
250 const SystemEnvData* pSysData = GetSysData(&rWindow);
251 if (pSysData && pSysData->hWnd)
252 surf = SurfaceSharedPtr(new Win32Surface(GetDC((HWND) pSysData->hWnd), x, y));
254 else if( rRefDevice.GetOutDevType() == OUTDEV_VIRDEV )
256 SystemGraphicsData aSysData = ((const VirtualDevice&) rRefDevice).GetSystemGfxData();
257 if (aSysData.hDC)
258 surf = SurfaceSharedPtr(new Win32Surface((HDC) aSysData.hDC, x, y));
260 return surf;
265 * cairo::createBitmapSurface: Create platfrom native Canvas surface from BitmapSystemData
266 * @param OutputDevice (not used)
267 * @param rData Platform native image data (struct BitmapSystemData in vcl/inc/bitmap.hxx)
268 * @param rSize width and height of the new surface
270 * Create a surface based on image data on rData
272 * @return new surface or empty surface
274 SurfaceSharedPtr createBitmapSurface( const OutputDevice& /* rRefDevice */,
275 const BitmapSystemData& rData,
276 const Size& rSize )
278 OSL_TRACE( "requested size: %d x %d available size: %d x %d",
279 rSize.Width(), rSize.Height(), rData.mnWidth, rData.mnHeight );
281 if ( rData.mnWidth == rSize.Width() && rData.mnHeight == rSize.Height() )
282 return SurfaceSharedPtr(new Win32Surface( rData ));
283 else
284 return SurfaceSharedPtr();
289 * cairo::ucs4toindex: Convert ucs4 char to glyph index
290 * @param ucs4 an ucs4 char
291 * @param hfont current font
293 * @return true if successful
295 unsigned long ucs4toindex(unsigned int ucs4, HFONT hfont)
297 wchar_t unicode[2];
298 WORD glyph_index;
299 HDC hdc = NULL;
300 int i = 0;
302 hdc = CreateCompatibleDC (NULL);
304 if (!hdc) return 0;
305 if (!SetGraphicsMode (hdc, GM_ADVANCED)) {
306 DeleteDC (hdc);
307 return 0;
310 SelectObject (hdc, hfont);
311 SetMapMode (hdc, MM_TEXT);
313 unicode[0] = ucs4;
314 unicode[1] = 0;
315 if (GetGlyphIndicesW (hdc, unicode, 1, &glyph_index, 0) == GDI_ERROR) {
316 glyph_index = 0;
319 DeleteDC (hdc);
320 return glyph_index;
324 } // namespace cairo
326 #endif // CAIRO_HAS_WIN32_SURFACE
328 #endif // WNT