1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "PrintTarget.h"
9 #ifdef CAIRO_HAS_QUARTZ_SURFACE
10 # include "cairo-quartz.h"
12 #ifdef CAIRO_HAS_WIN32_SURFACE
13 # include "cairo-win32.h"
15 #include "mozilla/gfx/2D.h"
16 #include "mozilla/gfx/HelpersCairo.h"
17 #include "mozilla/gfx/Logging.h"
18 #include "nsReadableUtils.h"
20 #include "nsUTF8Utils.h"
22 // IPP spec disallow the job-name which is over 255 characters.
23 // RFC: https://tools.ietf.org/html/rfc2911#section-4.1.2
24 #define IPP_JOB_NAME_LIMIT_LENGTH 255
26 namespace mozilla::gfx
{
28 PrintTarget::PrintTarget(cairo_surface_t
* aCairoSurface
, const IntSize
& aSize
)
29 : mCairoSurface(aCairoSurface
),
39 // aCairoSurface is null when our PrintTargetThebes subclass's ctor calls us.
40 // Once PrintTargetThebes is removed, enable this assertion.
41 MOZ_ASSERT(aCairoSurface
&& !cairo_surface_status(aCairoSurface
),
42 "CreateOrNull factory methods should not call us without a "
43 "valid cairo_surface_t*");
46 // CreateOrNull factory methods hand over ownership of aCairoSurface,
47 // so we don't call cairo_surface_reference(aSurface) here.
49 // This code was copied from gfxASurface::Init:
51 cairo_surface_get_content(mCairoSurface
) != CAIRO_CONTENT_COLOR
) {
52 cairo_surface_set_subpixel_antialiasing(
53 mCairoSurface
, CAIRO_SUBPIXEL_ANTIALIASING_DISABLED
);
57 PrintTarget::~PrintTarget() {
58 // null surfaces are allowed here
59 cairo_surface_destroy(mCairoSurface
);
60 mCairoSurface
= nullptr;
63 already_AddRefed
<DrawTarget
> PrintTarget::MakeDrawTarget(
64 const IntSize
& aSize
, DrawEventRecorder
* aRecorder
) {
65 MOZ_ASSERT(mCairoSurface
,
66 "We shouldn't have been constructed without a cairo surface");
68 // This should not be called outside of BeginPage()/EndPage() calls since
69 // some backends can only provide a valid DrawTarget at that time.
70 MOZ_ASSERT(mHasActivePage
, "We can't guarantee a valid DrawTarget");
72 if (cairo_surface_status(mCairoSurface
)) {
76 // Note than aSize may not be the same as mSize (the size of mCairoSurface).
77 // See the comments in our header. If the sizes are different a clip will
78 // be applied to mCairoSurface.
79 RefPtr
<DrawTarget
> dt
=
80 Factory::CreateDrawTargetForCairoSurface(mCairoSurface
, aSize
);
81 if (!dt
|| !dt
->IsValid()) {
86 dt
= CreateRecordingDrawTarget(aRecorder
, dt
);
87 if (!dt
|| !dt
->IsValid()) {
95 already_AddRefed
<DrawTarget
> PrintTarget::GetReferenceDrawTarget() {
97 const IntSize
size(1, 1);
99 cairo_surface_t
* similar
;
100 switch (cairo_surface_get_type(mCairoSurface
)) {
101 #ifdef CAIRO_HAS_WIN32_SURFACE
102 case CAIRO_SURFACE_TYPE_WIN32
:
103 similar
= cairo_win32_surface_create_with_dib(
104 CairoContentToCairoFormat(cairo_surface_get_content(mCairoSurface
)),
105 size
.width
, size
.height
);
109 similar
= cairo_surface_create_similar(
110 mCairoSurface
, cairo_surface_get_content(mCairoSurface
), size
.width
,
115 if (cairo_surface_status(similar
)) {
119 RefPtr
<DrawTarget
> dt
=
120 Factory::CreateDrawTargetForCairoSurface(similar
, size
);
122 // The DT addrefs the surface, so we need drop our own reference to it:
123 cairo_surface_destroy(similar
);
125 if (!dt
|| !dt
->IsValid()) {
128 mRefDT
= std::move(dt
);
131 return do_AddRef(mRefDT
);
135 void PrintTarget::AdjustPrintJobNameForIPP(const nsAString
& aJobName
,
136 nsCString
& aAdjustedJobName
) {
137 CopyUTF16toUTF8(aJobName
, aAdjustedJobName
);
139 if (aAdjustedJobName
.Length() > IPP_JOB_NAME_LIMIT_LENGTH
) {
140 uint32_t length
= RewindToPriorUTF8Codepoint(
141 aAdjustedJobName
.get(), (IPP_JOB_NAME_LIMIT_LENGTH
- 3U));
142 aAdjustedJobName
.SetLength(length
);
143 aAdjustedJobName
.AppendLiteral("...");
148 void PrintTarget::AdjustPrintJobNameForIPP(const nsAString
& aJobName
,
149 nsString
& aAdjustedJobName
) {
150 nsAutoCString jobName
;
151 AdjustPrintJobNameForIPP(aJobName
, jobName
);
153 CopyUTF8toUTF16(jobName
, aAdjustedJobName
);
157 already_AddRefed
<DrawTarget
> PrintTarget::CreateRecordingDrawTarget(
158 DrawEventRecorder
* aRecorder
, DrawTarget
* aDrawTarget
) {
159 MOZ_ASSERT(aRecorder
);
160 MOZ_ASSERT(aDrawTarget
);
162 RefPtr
<DrawTarget
> dt
;
165 // It doesn't really matter what we pass as the DrawTarget here.
166 dt
= gfx::Factory::CreateRecordingDrawTarget(aRecorder
, aDrawTarget
,
167 aDrawTarget
->GetRect());
170 if (!dt
|| !dt
->IsValid()) {
172 << "Failed to create a recording DrawTarget for PrintTarget";
179 void PrintTarget::Finish() {
185 // null surfaces are allowed here
186 cairo_surface_finish(mCairoSurface
);
189 void PrintTarget::RegisterPageDoneCallback(PageDoneCallback
&& aCallback
) {
190 MOZ_ASSERT(aCallback
&& !IsSyncPagePrinting());
191 mPageDoneCallback
= std::move(aCallback
);
194 void PrintTarget::UnregisterPageDoneCallback() { mPageDoneCallback
= nullptr; }
196 } // namespace mozilla::gfx