2016-11-18 14:30:25 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* 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 "PrintTargetSkPDF.h"
|
|
|
|
|
|
|
|
#include "mozilla/gfx/2D.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
|
|
|
PrintTargetSkPDF::PrintTargetSkPDF(const IntSize& aSize,
|
|
|
|
UniquePtr<SkWStream> aStream)
|
|
|
|
: PrintTarget(/* not using cairo_surface_t */ nullptr, aSize)
|
2018-05-30 22:15:35 +03:00
|
|
|
, mOStream(std::move(aStream))
|
2016-11-18 14:30:25 +03:00
|
|
|
, mPageCanvas(nullptr)
|
2018-06-16 17:42:33 +03:00
|
|
|
, mRefCanvas(nullptr)
|
2016-11-18 14:30:25 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintTargetSkPDF::~PrintTargetSkPDF()
|
|
|
|
{
|
|
|
|
Finish(); // ensure stream is flushed
|
|
|
|
|
|
|
|
// Make sure mPDFDoc and mRefPDFDoc are destroyed before our member streams
|
|
|
|
// (which they wrap) are destroyed:
|
|
|
|
mPDFDoc = nullptr;
|
|
|
|
mRefPDFDoc = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<PrintTargetSkPDF>
|
|
|
|
PrintTargetSkPDF::CreateOrNull(UniquePtr<SkWStream> aStream,
|
|
|
|
const IntSize& aSizeInPoints)
|
|
|
|
{
|
2018-05-30 22:15:35 +03:00
|
|
|
return do_AddRef(new PrintTargetSkPDF(aSizeInPoints, std::move(aStream)));
|
2016-11-18 14:30:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PrintTargetSkPDF::BeginPrinting(const nsAString& aTitle,
|
|
|
|
const nsAString& aPrintToFileName,
|
|
|
|
int32_t aStartPage,
|
|
|
|
int32_t aEndPage)
|
|
|
|
{
|
|
|
|
// We need to create the SkPDFDocument here rather than in CreateOrNull
|
|
|
|
// because it's only now that we are given aTitle which we want for the
|
|
|
|
// PDF metadata.
|
|
|
|
|
|
|
|
SkDocument::PDFMetadata metadata;
|
|
|
|
metadata.fTitle = NS_ConvertUTF16toUTF8(aTitle).get();
|
|
|
|
metadata.fCreator = "Firefox";
|
|
|
|
SkTime::DateTime now;
|
|
|
|
SkTime::GetDateTime(&now);
|
|
|
|
metadata.fCreation.fEnabled = true;
|
|
|
|
metadata.fCreation.fDateTime = now;
|
|
|
|
metadata.fModified.fEnabled = true;
|
|
|
|
metadata.fModified.fDateTime = now;
|
|
|
|
|
|
|
|
// SkDocument stores a non-owning raw pointer to aStream
|
2018-03-12 23:37:10 +03:00
|
|
|
mPDFDoc = SkDocument::MakePDF(mOStream.get(), metadata);
|
2016-11-18 14:30:25 +03:00
|
|
|
|
|
|
|
return mPDFDoc ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PrintTargetSkPDF::BeginPage()
|
|
|
|
{
|
2017-05-10 05:31:07 +03:00
|
|
|
mPageCanvas = mPDFDoc->beginPage(mSize.width, mSize.height);
|
2016-11-18 14:30:25 +03:00
|
|
|
|
|
|
|
return !mPageCanvas ? NS_ERROR_FAILURE : PrintTarget::BeginPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PrintTargetSkPDF::EndPage()
|
|
|
|
{
|
|
|
|
mPageCanvas = nullptr;
|
|
|
|
mPageDT = nullptr;
|
|
|
|
return PrintTarget::EndPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PrintTargetSkPDF::EndPrinting()
|
|
|
|
{
|
|
|
|
mPDFDoc->close();
|
|
|
|
if (mRefPDFDoc) {
|
|
|
|
mRefPDFDoc->close();
|
|
|
|
}
|
|
|
|
mPageCanvas = nullptr;
|
|
|
|
mPageDT = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PrintTargetSkPDF::Finish()
|
|
|
|
{
|
|
|
|
if (mIsFinished) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mOStream->flush();
|
|
|
|
PrintTarget::Finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<DrawTarget>
|
|
|
|
PrintTargetSkPDF::MakeDrawTarget(const IntSize& aSize,
|
|
|
|
DrawEventRecorder* aRecorder)
|
|
|
|
{
|
|
|
|
if (aRecorder) {
|
|
|
|
return PrintTarget::MakeDrawTarget(aSize, aRecorder);
|
|
|
|
}
|
|
|
|
//MOZ_ASSERT(aSize == mSize, "Should mPageCanvas size match?");
|
|
|
|
if (!mPageCanvas) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-05-10 05:31:07 +03:00
|
|
|
mPageDT = Factory::CreateDrawTargetWithSkCanvas(mPageCanvas);
|
2016-11-18 14:30:25 +03:00
|
|
|
if (!mPageDT) {
|
|
|
|
mPageCanvas = nullptr;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return do_AddRef(mPageDT);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<DrawTarget>
|
2018-01-18 15:45:18 +03:00
|
|
|
PrintTargetSkPDF::GetReferenceDrawTarget()
|
2016-11-18 14:30:25 +03:00
|
|
|
{
|
|
|
|
if (!mRefDT) {
|
|
|
|
SkDocument::PDFMetadata metadata;
|
|
|
|
// SkDocument stores a non-owning raw pointer to aStream
|
2018-03-12 23:37:10 +03:00
|
|
|
mRefPDFDoc = SkDocument::MakePDF(&mRefOStream, metadata);
|
2016-11-18 14:30:25 +03:00
|
|
|
if (!mRefPDFDoc) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-05-10 05:31:07 +03:00
|
|
|
mRefCanvas = mRefPDFDoc->beginPage(mSize.width, mSize.height);
|
2016-11-18 14:30:25 +03:00
|
|
|
if (!mRefCanvas) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
RefPtr<DrawTarget> dt =
|
2017-05-10 05:31:07 +03:00
|
|
|
Factory::CreateDrawTargetWithSkCanvas(mRefCanvas);
|
2016-11-18 14:30:25 +03:00
|
|
|
if (!dt) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-03-21 12:36:08 +03:00
|
|
|
mRefDT = dt.forget();
|
|
|
|
}
|
2016-11-18 14:30:25 +03:00
|
|
|
|
|
|
|
return do_AddRef(mRefDT);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|