2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2016-06-03 13:27:31 +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 "PrintTarget.h"
|
|
|
|
|
2016-06-06 18:10:28 +03:00
|
|
|
#include "cairo.h"
|
2016-11-16 02:09:49 +03:00
|
|
|
#ifdef CAIRO_HAS_QUARTZ_SURFACE
|
|
|
|
# include "cairo-quartz.h"
|
|
|
|
#endif
|
|
|
|
#ifdef CAIRO_HAS_WIN32_SURFACE
|
|
|
|
# include "cairo-win32.h"
|
|
|
|
#endif
|
2016-06-03 13:27:31 +03:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2016-11-16 02:12:09 +03:00
|
|
|
#include "mozilla/gfx/HelpersCairo.h"
|
2016-06-03 13:27:31 +03:00
|
|
|
#include "mozilla/gfx/Logging.h"
|
2017-10-13 09:47:33 +03:00
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsUTF8Utils.h"
|
|
|
|
|
|
|
|
// IPP spec disallow the job-name which is over 255 characters.
|
|
|
|
// RFC: https://tools.ietf.org/html/rfc2911#section-4.1.2
|
|
|
|
#define IPP_JOB_NAME_LIMIT_LENGTH 255
|
2016-06-03 13:27:31 +03:00
|
|
|
|
2020-01-18 16:48:34 +03:00
|
|
|
namespace mozilla::gfx {
|
2016-06-03 13:27:31 +03:00
|
|
|
|
2016-06-06 18:10:28 +03:00
|
|
|
PrintTarget::PrintTarget(cairo_surface_t* aCairoSurface, const IntSize& aSize)
|
|
|
|
: mCairoSurface(aCairoSurface),
|
|
|
|
mSize(aSize),
|
2016-06-03 13:27:31 +03:00
|
|
|
mIsFinished(false)
|
2016-10-27 21:25:02 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
,
|
|
|
|
mHasActivePage(false)
|
|
|
|
#endif
|
|
|
|
|
2016-06-03 13:27:31 +03:00
|
|
|
{
|
2016-06-06 18:10:28 +03:00
|
|
|
#if 0
|
|
|
|
// aCairoSurface is null when our PrintTargetThebes subclass's ctor calls us.
|
|
|
|
// Once PrintTargetThebes is removed, enable this assertion.
|
|
|
|
MOZ_ASSERT(aCairoSurface && !cairo_surface_status(aCairoSurface),
|
|
|
|
"CreateOrNull factory methods should not call us without a "
|
|
|
|
"valid cairo_surface_t*");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// CreateOrNull factory methods hand over ownership of aCairoSurface,
|
|
|
|
// so we don't call cairo_surface_reference(aSurface) here.
|
|
|
|
|
|
|
|
// This code was copied from gfxASurface::Init:
|
|
|
|
if (mCairoSurface &&
|
|
|
|
cairo_surface_get_content(mCairoSurface) != CAIRO_CONTENT_COLOR) {
|
|
|
|
cairo_surface_set_subpixel_antialiasing(
|
|
|
|
mCairoSurface, CAIRO_SUBPIXEL_ANTIALIASING_DISABLED);
|
|
|
|
}
|
2016-06-03 13:27:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PrintTarget::~PrintTarget() {
|
2016-06-06 18:10:28 +03:00
|
|
|
// null surfaces are allowed here
|
|
|
|
cairo_surface_destroy(mCairoSurface);
|
|
|
|
mCairoSurface = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<DrawTarget> PrintTarget::MakeDrawTarget(
|
|
|
|
const IntSize& aSize, DrawEventRecorder* aRecorder) {
|
|
|
|
MOZ_ASSERT(mCairoSurface,
|
|
|
|
"We shouldn't have been constructed without a cairo surface");
|
|
|
|
|
2016-10-27 21:25:02 +03:00
|
|
|
// This should not be called outside of BeginPage()/EndPage() calls since
|
|
|
|
// some backends can only provide a valid DrawTarget at that time.
|
|
|
|
MOZ_ASSERT(mHasActivePage, "We can't guarantee a valid DrawTarget");
|
|
|
|
|
2016-06-06 18:10:28 +03:00
|
|
|
if (cairo_surface_status(mCairoSurface)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note than aSize may not be the same as mSize (the size of mCairoSurface).
|
|
|
|
// See the comments in our header. If the sizes are different a clip will
|
|
|
|
// be applied to mCairoSurface.
|
|
|
|
RefPtr<DrawTarget> dt =
|
|
|
|
Factory::CreateDrawTargetForCairoSurface(mCairoSurface, aSize);
|
|
|
|
if (!dt || !dt->IsValid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-06-16 19:28:50 +03:00
|
|
|
if (aRecorder) {
|
2020-10-21 18:09:22 +03:00
|
|
|
dt = CreateRecordingDrawTarget(aRecorder, dt);
|
2016-06-16 19:28:50 +03:00
|
|
|
if (!dt || !dt->IsValid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dt.forget();
|
|
|
|
}
|
|
|
|
|
2018-01-18 15:45:18 +03:00
|
|
|
already_AddRefed<DrawTarget> PrintTarget::GetReferenceDrawTarget() {
|
2016-10-27 21:24:12 +03:00
|
|
|
if (!mRefDT) {
|
2016-11-16 02:09:49 +03:00
|
|
|
const IntSize size(1, 1);
|
2016-11-15 03:44:52 +03:00
|
|
|
|
2016-11-16 02:09:49 +03:00
|
|
|
cairo_surface_t* similar;
|
|
|
|
switch (cairo_surface_get_type(mCairoSurface)) {
|
|
|
|
#ifdef CAIRO_HAS_WIN32_SURFACE
|
|
|
|
case CAIRO_SURFACE_TYPE_WIN32:
|
2016-11-16 02:12:09 +03:00
|
|
|
similar = cairo_win32_surface_create_with_dib(
|
|
|
|
CairoContentToCairoFormat(cairo_surface_get_content(mCairoSurface)),
|
|
|
|
size.width, size.height);
|
|
|
|
break;
|
2016-11-16 02:09:49 +03:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
similar = cairo_surface_create_similar(
|
|
|
|
mCairoSurface, cairo_surface_get_content(mCairoSurface), size.width,
|
|
|
|
size.height);
|
|
|
|
break;
|
|
|
|
}
|
2016-10-27 21:24:12 +03:00
|
|
|
|
2016-11-16 02:09:49 +03:00
|
|
|
if (cairo_surface_status(similar)) {
|
2016-10-27 21:24:12 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<DrawTarget> dt =
|
2016-11-16 02:09:49 +03:00
|
|
|
Factory::CreateDrawTargetForCairoSurface(similar, size);
|
2016-10-27 21:24:12 +03:00
|
|
|
|
|
|
|
// The DT addrefs the surface, so we need drop our own reference to it:
|
2016-11-16 02:09:49 +03:00
|
|
|
cairo_surface_destroy(similar);
|
2016-10-27 21:24:12 +03:00
|
|
|
|
|
|
|
if (!dt || !dt->IsValid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-02-13 17:38:48 +03:00
|
|
|
mRefDT = std::move(dt);
|
2017-03-21 12:36:08 +03:00
|
|
|
}
|
2016-10-27 21:24:12 +03:00
|
|
|
|
|
|
|
return do_AddRef(mRefDT);
|
|
|
|
}
|
|
|
|
|
2017-10-13 09:47:33 +03:00
|
|
|
/* static */
|
|
|
|
void PrintTarget::AdjustPrintJobNameForIPP(const nsAString& aJobName,
|
|
|
|
nsCString& aAdjustedJobName) {
|
|
|
|
CopyUTF16toUTF8(aJobName, aAdjustedJobName);
|
|
|
|
|
|
|
|
if (aAdjustedJobName.Length() > IPP_JOB_NAME_LIMIT_LENGTH) {
|
|
|
|
uint32_t length = RewindToPriorUTF8Codepoint(
|
|
|
|
aAdjustedJobName.get(), (IPP_JOB_NAME_LIMIT_LENGTH - 3U));
|
|
|
|
aAdjustedJobName.SetLength(length);
|
|
|
|
aAdjustedJobName.AppendLiteral("...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 09:48:39 +03:00
|
|
|
/* static */
|
|
|
|
void PrintTarget::AdjustPrintJobNameForIPP(const nsAString& aJobName,
|
|
|
|
nsString& aAdjustedJobName) {
|
|
|
|
nsAutoCString jobName;
|
|
|
|
AdjustPrintJobNameForIPP(aJobName, jobName);
|
|
|
|
|
|
|
|
CopyUTF8toUTF16(jobName, aAdjustedJobName);
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:19 +03:00
|
|
|
/* static */
|
2020-10-21 18:09:22 +03:00
|
|
|
already_AddRefed<DrawTarget> PrintTarget::CreateRecordingDrawTarget(
|
2019-02-26 01:07:19 +03:00
|
|
|
DrawEventRecorder* aRecorder, DrawTarget* aDrawTarget) {
|
2016-06-16 19:28:50 +03:00
|
|
|
MOZ_ASSERT(aRecorder);
|
|
|
|
MOZ_ASSERT(aDrawTarget);
|
|
|
|
|
|
|
|
RefPtr<DrawTarget> dt;
|
|
|
|
|
|
|
|
if (aRecorder) {
|
|
|
|
// It doesn't really matter what we pass as the DrawTarget here.
|
2020-10-01 17:23:59 +03:00
|
|
|
dt = gfx::Factory::CreateRecordingDrawTarget(aRecorder, aDrawTarget,
|
|
|
|
aDrawTarget->GetRect());
|
2016-06-16 19:28:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!dt || !dt->IsValid()) {
|
|
|
|
gfxCriticalNote
|
|
|
|
<< "Failed to create a recording DrawTarget for PrintTarget";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-06-03 13:27:31 +03:00
|
|
|
return dt.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintTarget::Finish() {
|
|
|
|
if (mIsFinished) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mIsFinished = true;
|
2016-06-06 18:10:28 +03:00
|
|
|
|
|
|
|
// null surfaces are allowed here
|
|
|
|
cairo_surface_finish(mCairoSurface);
|
2016-06-03 13:27:31 +03:00
|
|
|
}
|
|
|
|
|
2017-11-04 23:36:37 +03:00
|
|
|
void PrintTarget::RegisterPageDoneCallback(PageDoneCallback&& aCallback) {
|
|
|
|
MOZ_ASSERT(aCallback && !IsSyncPagePrinting());
|
2018-05-30 22:15:35 +03:00
|
|
|
mPageDoneCallback = std::move(aCallback);
|
2017-11-04 23:36:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintTarget::UnregisterPageDoneCallback() { mPageDoneCallback = nullptr; }
|
|
|
|
|
2020-01-18 16:48:34 +03:00
|
|
|
} // namespace mozilla::gfx
|