2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2016-11-29 00:49:09 +03:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "PrintTargetCG.h"
|
|
|
|
|
|
|
|
#include "cairo.h"
|
|
|
|
#include "cairo-quartz.h"
|
|
|
|
#include "mozilla/gfx/HelpersCairo.h"
|
2016-11-29 01:40:43 +03:00
|
|
|
#include "nsObjCExceptions.h"
|
2016-11-29 00:49:09 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
PrintTargetCG::PrintTargetCG(PMPrintSession aPrintSession, PMPageFormat aPageFormat,
|
|
|
|
PMPrintSettings aPrintSettings, const IntSize& aSize)
|
|
|
|
: PrintTarget(/* aCairoSurface */ nullptr, aSize),
|
|
|
|
mPrintSession(aPrintSession),
|
|
|
|
mPageFormat(aPageFormat),
|
|
|
|
mPrintSettings(aPrintSettings) {
|
2016-11-29 01:40:43 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
|
|
|
::PMRetain(mPrintSession);
|
|
|
|
|
2016-11-29 00:49:09 +03:00
|
|
|
// TODO: Add memory reporting like gfxQuartzSurface.
|
2019-01-21 20:18:16 +03:00
|
|
|
// RecordMemoryUsed(mSize.height * 4 + sizeof(gfxQuartzSurface));
|
2016-11-29 01:40:43 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2016-11-29 00:49:09 +03:00
|
|
|
}
|
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
PrintTargetCG::~PrintTargetCG() {
|
2016-11-29 01:40:43 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
2016-11-29 00:49:09 +03:00
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
if (mPrintSession) ::PMRelease(mPrintSession);
|
2016-11-29 00:49:09 +03:00
|
|
|
|
2016-11-29 01:40:43 +03:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2016-11-29 00:49:09 +03:00
|
|
|
}
|
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
/* static */ already_AddRefed<PrintTargetCG> PrintTargetCG::CreateOrNull(
|
|
|
|
PMPrintSession aPrintSession, PMPageFormat aPageFormat, PMPrintSettings aPrintSettings,
|
|
|
|
const IntSize& aSize) {
|
2016-11-29 00:49:09 +03:00
|
|
|
if (!Factory::CheckSurfaceSize(aSize)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
RefPtr<PrintTargetCG> target =
|
|
|
|
new PrintTargetCG(aPrintSession, aPageFormat, aPrintSettings, aSize);
|
2016-11-29 00:49:09 +03:00
|
|
|
|
|
|
|
return target.forget();
|
|
|
|
}
|
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
static size_t PutBytesNull(void* info, const void* buffer, size_t count) { return count; }
|
2016-11-29 00:49:09 +03:00
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
already_AddRefed<DrawTarget> PrintTargetCG::GetReferenceDrawTarget() {
|
2016-11-29 00:49:09 +03:00
|
|
|
if (!mRefDT) {
|
|
|
|
const IntSize size(1, 1);
|
|
|
|
|
|
|
|
CGDataConsumerCallbacks callbacks = {PutBytesNull, nullptr};
|
|
|
|
CGDataConsumerRef consumer = CGDataConsumerCreate(nullptr, &callbacks);
|
|
|
|
CGContextRef pdfContext = CGPDFContextCreate(consumer, nullptr, nullptr);
|
|
|
|
CGDataConsumerRelease(consumer);
|
|
|
|
|
|
|
|
cairo_surface_t* similar =
|
2019-01-21 20:18:16 +03:00
|
|
|
cairo_quartz_surface_create_for_cg_context(pdfContext, size.width, size.height);
|
2016-11-29 00:49:09 +03:00
|
|
|
|
|
|
|
CGContextRelease(pdfContext);
|
|
|
|
|
|
|
|
if (cairo_surface_status(similar)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
RefPtr<DrawTarget> dt = Factory::CreateDrawTargetForCairoSurface(similar, size);
|
2016-11-29 00:49:09 +03:00
|
|
|
|
|
|
|
// The DT addrefs the surface, so we need drop our own reference to it:
|
|
|
|
cairo_surface_destroy(similar);
|
|
|
|
|
|
|
|
if (!dt || !dt->IsValid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-03-21 12:36:08 +03:00
|
|
|
mRefDT = dt.forget();
|
|
|
|
}
|
2016-11-29 00:49:09 +03:00
|
|
|
|
|
|
|
return do_AddRef(mRefDT);
|
|
|
|
}
|
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
nsresult PrintTargetCG::BeginPrinting(const nsAString& aTitle, const nsAString& aPrintToFileName,
|
|
|
|
int32_t aStartPage, int32_t aEndPage) {
|
2016-11-29 10:55:00 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
|
|
|
OSStatus status;
|
|
|
|
status = ::PMSetFirstPage(mPrintSettings, aStartPage, false);
|
|
|
|
NS_ASSERTION(status == noErr, "PMSetFirstPage failed");
|
|
|
|
status = ::PMSetLastPage(mPrintSettings, aEndPage, false);
|
|
|
|
NS_ASSERTION(status == noErr, "PMSetLastPage failed");
|
|
|
|
|
|
|
|
status = ::PMSessionBeginCGDocumentNoDialog(mPrintSession, mPrintSettings, mPageFormat);
|
|
|
|
|
|
|
|
return status == noErr ? NS_OK : NS_ERROR_ABORT;
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
}
|
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
nsresult PrintTargetCG::EndPrinting() {
|
2016-11-29 10:55:00 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
|
|
|
::PMSessionEndDocumentNoDialog(mPrintSession);
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
}
|
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
nsresult PrintTargetCG::AbortPrinting() {
|
2016-11-29 10:55:00 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
mHasActivePage = false;
|
|
|
|
#endif
|
|
|
|
return EndPrinting();
|
|
|
|
}
|
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
nsresult PrintTargetCG::BeginPage() {
|
2016-11-29 01:40:43 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2016-11-29 10:55:00 +03:00
|
|
|
PMSessionError(mPrintSession);
|
|
|
|
OSStatus status = ::PMSessionBeginPageNoDialog(mPrintSession, mPageFormat, NULL);
|
|
|
|
if (status != noErr) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
2016-11-29 01:40:43 +03:00
|
|
|
CGContextRef context;
|
2016-11-29 10:55:00 +03:00
|
|
|
// This call will fail if it wasn't called between the PMSessionBeginPage/
|
2016-11-29 01:40:43 +03:00
|
|
|
// PMSessionEndPage calls:
|
|
|
|
::PMSessionGetCGGraphicsContext(mPrintSession, &context);
|
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int width = static_cast<unsigned int>(mSize.width);
|
|
|
|
unsigned int height = static_cast<unsigned int>(mSize.height);
|
|
|
|
|
|
|
|
// Initially, origin is at bottom-left corner of the paper.
|
|
|
|
// Here, we translate it to top-left corner of the paper.
|
|
|
|
CGContextTranslateCTM(context, 0, height);
|
|
|
|
CGContextScaleCTM(context, 1.0, -1.0);
|
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(context, width, height);
|
2016-11-29 01:40:43 +03:00
|
|
|
|
|
|
|
if (cairo_surface_status(surface)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCairoSurface = surface;
|
|
|
|
|
|
|
|
return PrintTarget::BeginPage();
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
}
|
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
nsresult PrintTargetCG::EndPage() {
|
2016-11-29 10:55:00 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2016-11-29 01:40:43 +03:00
|
|
|
cairo_surface_finish(mCairoSurface);
|
|
|
|
mCairoSurface = nullptr;
|
2016-11-29 10:55:00 +03:00
|
|
|
|
|
|
|
OSStatus status = ::PMSessionEndPageNoDialog(mPrintSession);
|
|
|
|
if (status != noErr) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
2016-11-29 01:40:43 +03:00
|
|
|
return PrintTarget::EndPage();
|
2016-11-29 10:55:00 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2016-11-29 01:40:43 +03:00
|
|
|
}
|
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|