2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Oracle Corporation code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Oracle Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2005
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Stuart Parmenter <pavlov@pavlov.net>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "gfxWindowsSurface.h"
|
|
|
|
#include "gfxContext.h"
|
|
|
|
#include "gfxPlatform.h"
|
|
|
|
|
|
|
|
#include "cairo.h"
|
|
|
|
#include "cairo-win32.h"
|
|
|
|
|
|
|
|
#include "nsString.h"
|
|
|
|
|
2010-07-15 14:08:10 -07:00
|
|
|
gfxWindowsSurface::gfxWindowsSurface(HWND wnd, PRUint32 flags) :
|
2007-09-24 15:50:44 -07:00
|
|
|
mOwnsDC(PR_TRUE), mForPrinting(PR_FALSE), mWnd(wnd)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mDC = ::GetDC(mWnd);
|
2010-07-15 14:08:10 -07:00
|
|
|
InitWithDC(flags);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-09-24 15:50:44 -07:00
|
|
|
gfxWindowsSurface::gfxWindowsSurface(HDC dc, PRUint32 flags) :
|
|
|
|
mOwnsDC(PR_FALSE), mForPrinting(PR_FALSE), mDC(dc), mWnd(nsnull)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-09-24 15:50:44 -07:00
|
|
|
if (flags & FLAG_TAKE_DC)
|
|
|
|
mOwnsDC = PR_TRUE;
|
|
|
|
|
2008-11-04 07:43:45 -08:00
|
|
|
#ifdef NS_PRINTING
|
2007-09-24 15:50:44 -07:00
|
|
|
if (flags & FLAG_FOR_PRINTING) {
|
2007-11-29 12:06:56 -08:00
|
|
|
Init(cairo_win32_printing_surface_create(mDC));
|
2007-09-24 15:50:44 -07:00
|
|
|
mForPrinting = PR_TRUE;
|
2008-11-04 07:43:45 -08:00
|
|
|
} else
|
|
|
|
#endif
|
2010-07-15 14:08:10 -07:00
|
|
|
InitWithDC(flags);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxWindowsSurface::gfxWindowsSurface(const gfxIntSize& size, gfxImageFormat imageFormat) :
|
2007-09-24 15:50:44 -07:00
|
|
|
mOwnsDC(PR_FALSE), mForPrinting(PR_FALSE), mWnd(nsnull)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-05-10 12:58:09 -07:00
|
|
|
if (!CheckSurfaceSize(size))
|
|
|
|
return;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_surface_t *surf = cairo_win32_surface_create_with_dib((cairo_format_t)imageFormat,
|
|
|
|
size.width, size.height);
|
2010-05-21 21:10:14 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
Init(surf);
|
|
|
|
|
2010-05-21 21:10:14 -07:00
|
|
|
RecordMemoryUsed(size.width * size.height * 4 + sizeof(gfxWindowsSurface));
|
|
|
|
|
2007-10-03 14:27:42 -07:00
|
|
|
if (CairoStatus() == 0)
|
|
|
|
mDC = cairo_win32_surface_get_dc(CairoSurface());
|
|
|
|
else
|
|
|
|
mDC = nsnull;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxWindowsSurface::gfxWindowsSurface(HDC dc, const gfxIntSize& size, gfxImageFormat imageFormat) :
|
2007-09-24 15:50:44 -07:00
|
|
|
mOwnsDC(PR_FALSE), mForPrinting(PR_FALSE), mWnd(nsnull)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-05-10 12:58:09 -07:00
|
|
|
if (!CheckSurfaceSize(size))
|
|
|
|
return;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_surface_t *surf = cairo_win32_surface_create_with_ddb(dc, (cairo_format_t)imageFormat,
|
|
|
|
size.width, size.height);
|
2010-05-21 21:10:14 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
Init(surf);
|
|
|
|
|
2010-05-21 21:10:14 -07:00
|
|
|
// DDBs will generally only use 3 bytes per pixel when RGB24
|
|
|
|
int bytesPerPixel = ((imageFormat == gfxASurface::ImageFormatRGB24) ? 3 : 4);
|
|
|
|
RecordMemoryUsed(size.width * size.height * bytesPerPixel + sizeof(gfxWindowsSurface));
|
|
|
|
|
2007-10-03 14:27:42 -07:00
|
|
|
if (CairoStatus() == 0)
|
|
|
|
mDC = cairo_win32_surface_get_dc(CairoSurface());
|
|
|
|
else
|
|
|
|
mDC = nsnull;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxWindowsSurface::gfxWindowsSurface(cairo_surface_t *csurf) :
|
2007-09-24 15:50:44 -07:00
|
|
|
mOwnsDC(PR_FALSE), mForPrinting(PR_FALSE), mWnd(nsnull)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-10-03 14:27:42 -07:00
|
|
|
if (cairo_surface_status(csurf) == 0)
|
|
|
|
mDC = cairo_win32_surface_get_dc(csurf);
|
|
|
|
else
|
|
|
|
mDC = nsnull;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-09-24 15:50:44 -07:00
|
|
|
if (cairo_surface_get_type(csurf) == CAIRO_SURFACE_TYPE_WIN32_PRINTING)
|
|
|
|
mForPrinting = PR_TRUE;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
Init(csurf, PR_TRUE);
|
|
|
|
}
|
|
|
|
|
2010-07-15 14:08:10 -07:00
|
|
|
void
|
|
|
|
gfxWindowsSurface::InitWithDC(PRUint32 flags)
|
|
|
|
{
|
|
|
|
if (flags & FLAG_IS_TRANSPARENT) {
|
|
|
|
Init(cairo_win32_surface_create_with_alpha(mDC));
|
|
|
|
} else {
|
|
|
|
Init(cairo_win32_surface_create(mDC));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxWindowsSurface::~gfxWindowsSurface()
|
|
|
|
{
|
|
|
|
if (mOwnsDC) {
|
|
|
|
if (mWnd)
|
|
|
|
::ReleaseDC(mWnd, mDC);
|
|
|
|
else
|
|
|
|
::DeleteDC(mDC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-05 19:28:54 -07:00
|
|
|
HDC
|
|
|
|
gfxWindowsSurface::GetDCWithClip(gfxContext *ctx)
|
|
|
|
{
|
|
|
|
return cairo_win32_get_dc_with_clip (ctx->GetCairo());
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
already_AddRefed<gfxImageSurface>
|
|
|
|
gfxWindowsSurface::GetImageSurface()
|
|
|
|
{
|
2008-01-28 11:05:44 -08:00
|
|
|
if (!mSurfaceValid) {
|
|
|
|
NS_WARNING ("GetImageSurface on an invalid (null) surface; who's calling this without checking for surface errors?");
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(CairoSurface() != nsnull, "CairoSurface() shouldn't be nsnull when mSurfaceValid is TRUE!");
|
|
|
|
|
2007-09-24 15:50:44 -07:00
|
|
|
if (mForPrinting)
|
|
|
|
return nsnull;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_surface_t *isurf = cairo_win32_surface_get_image(CairoSurface());
|
|
|
|
if (!isurf)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
nsRefPtr<gfxASurface> asurf = gfxASurface::Wrap(isurf);
|
|
|
|
gfxImageSurface *imgsurf = (gfxImageSurface*) asurf.get();
|
|
|
|
NS_ADDREF(imgsurf);
|
|
|
|
return imgsurf;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxWindowsSurface>
|
|
|
|
gfxWindowsSurface::OptimizeToDDB(HDC dc, const gfxIntSize& size, gfxImageFormat format)
|
|
|
|
{
|
2007-09-24 15:50:44 -07:00
|
|
|
if (mForPrinting)
|
|
|
|
return nsnull;
|
|
|
|
|
2007-10-03 14:27:42 -07:00
|
|
|
if (format != ImageFormatRGB24)
|
2007-03-22 10:30:00 -07:00
|
|
|
return nsnull;
|
|
|
|
|
2007-10-03 14:27:42 -07:00
|
|
|
nsRefPtr<gfxWindowsSurface> wsurf = new gfxWindowsSurface(dc, size, format);
|
|
|
|
if (wsurf->CairoStatus() != 0)
|
|
|
|
return nsnull;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-31 20:42:49 -08:00
|
|
|
gfxContext tmpCtx(wsurf);
|
|
|
|
tmpCtx.SetOperator(gfxContext::OPERATOR_SOURCE);
|
|
|
|
tmpCtx.SetSource(this);
|
|
|
|
tmpCtx.Paint();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
gfxWindowsSurface *raw = (gfxWindowsSurface*) (wsurf.get());
|
|
|
|
NS_ADDREF(raw);
|
2009-08-12 07:44:53 -07:00
|
|
|
|
|
|
|
// we let the new DDB surfaces be converted back to dibsections if
|
|
|
|
// acquire_source_image is called on them
|
|
|
|
cairo_win32_surface_set_can_convert_to_dib(raw->CairoSurface(), TRUE);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return raw;
|
|
|
|
}
|
|
|
|
|
2009-06-16 11:57:34 -07:00
|
|
|
nsresult
|
|
|
|
gfxWindowsSurface::BeginPrinting(const nsAString& aTitle,
|
|
|
|
const nsAString& aPrintToFileName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-06-16 11:57:34 -07:00
|
|
|
#ifdef NS_PRINTING
|
2007-03-22 10:30:00 -07:00
|
|
|
#define DOC_TITLE_LENGTH 30
|
2008-08-27 14:44:55 -07:00
|
|
|
DOCINFOW docinfo;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-08-27 14:44:55 -07:00
|
|
|
nsString titleStr(aTitle);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (titleStr.Length() > DOC_TITLE_LENGTH) {
|
|
|
|
titleStr.SetLength(DOC_TITLE_LENGTH-3);
|
|
|
|
titleStr.AppendLiteral("...");
|
|
|
|
}
|
2008-03-11 15:46:26 -07:00
|
|
|
|
2008-08-27 14:44:55 -07:00
|
|
|
nsString docName(aPrintToFileName);
|
2007-03-22 10:30:00 -07:00
|
|
|
docinfo.cbSize = sizeof(docinfo);
|
2008-08-27 14:44:55 -07:00
|
|
|
docinfo.lpszDocName = titleStr.Length() > 0 ? titleStr.get() : L"Mozilla Document";
|
|
|
|
docinfo.lpszOutput = docName.Length() > 0 ? docName.get() : nsnull;
|
2007-03-22 10:30:00 -07:00
|
|
|
docinfo.lpszDatatype = NULL;
|
|
|
|
docinfo.fwType = 0;
|
|
|
|
|
2008-08-27 14:44:55 -07:00
|
|
|
::StartDocW(mDC, &docinfo);
|
2008-03-26 18:21:46 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2009-06-16 11:57:34 -07:00
|
|
|
#else
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-06-16 11:57:34 -07:00
|
|
|
nsresult
|
|
|
|
gfxWindowsSurface::EndPrinting()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-06-16 11:57:34 -07:00
|
|
|
#ifdef NS_PRINTING
|
2008-03-26 18:21:46 -07:00
|
|
|
int result = ::EndDoc(mDC);
|
|
|
|
if (result <= 0)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2009-06-16 11:57:34 -07:00
|
|
|
#else
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-06-16 11:57:34 -07:00
|
|
|
nsresult
|
|
|
|
gfxWindowsSurface::AbortPrinting()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-06-16 11:57:34 -07:00
|
|
|
#ifdef NS_PRINTING
|
2008-03-26 18:21:46 -07:00
|
|
|
int result = ::AbortDoc(mDC);
|
|
|
|
if (result <= 0)
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2009-06-16 11:57:34 -07:00
|
|
|
#else
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-06-16 11:57:34 -07:00
|
|
|
nsresult
|
|
|
|
gfxWindowsSurface::BeginPage()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-06-16 11:57:34 -07:00
|
|
|
#ifdef NS_PRINTING
|
2008-03-26 18:21:46 -07:00
|
|
|
int result = ::StartPage(mDC);
|
|
|
|
if (result <= 0)
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2009-06-16 11:57:34 -07:00
|
|
|
#else
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-06-16 11:57:34 -07:00
|
|
|
nsresult
|
|
|
|
gfxWindowsSurface::EndPage()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-06-16 11:57:34 -07:00
|
|
|
#ifdef NS_PRINTING
|
2007-09-24 15:50:44 -07:00
|
|
|
if (mForPrinting)
|
|
|
|
cairo_surface_show_page(CairoSurface());
|
2008-03-26 18:21:46 -07:00
|
|
|
int result = ::EndPage(mDC);
|
|
|
|
if (result <= 0)
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2009-06-16 11:57:34 -07:00
|
|
|
#else
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-11-29 12:06:56 -08:00
|
|
|
|
2009-06-16 11:57:34 -07:00
|
|
|
PRInt32
|
|
|
|
gfxWindowsSurface::GetDefaultContextFlags() const
|
2007-11-29 12:06:56 -08:00
|
|
|
{
|
|
|
|
if (mForPrinting)
|
2008-01-06 16:50:18 -08:00
|
|
|
return gfxContext::FLAG_SIMPLIFY_OPERATORS |
|
|
|
|
gfxContext::FLAG_DISABLE_SNAPPING;
|
2007-11-29 12:06:56 -08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|