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:
52 cairo_surface_get_content(mCairoSurface
) != CAIRO_CONTENT_COLOR
) {
53 cairo_surface_set_subpixel_antialiasing(
54 mCairoSurface
, CAIRO_SUBPIXEL_ANTIALIASING_DISABLED
);
59 PrintTarget::~PrintTarget() {
60 // null surfaces are allowed here
61 cairo_surface_destroy(mCairoSurface
);
62 mCairoSurface
= nullptr;
65 already_AddRefed
<DrawTarget
> PrintTarget::MakeDrawTarget(
66 const IntSize
& aSize
, DrawEventRecorder
* aRecorder
) {
67 MOZ_ASSERT(mCairoSurface
,
68 "We shouldn't have been constructed without a cairo surface");
70 // This should not be called outside of BeginPage()/EndPage() calls since
71 // some backends can only provide a valid DrawTarget at that time.
72 MOZ_ASSERT(mHasActivePage
, "We can't guarantee a valid DrawTarget");
74 if (cairo_surface_status(mCairoSurface
)) {
78 // Note than aSize may not be the same as mSize (the size of mCairoSurface).
79 // See the comments in our header. If the sizes are different a clip will
80 // be applied to mCairoSurface.
81 RefPtr
<DrawTarget
> dt
=
82 Factory::CreateDrawTargetForCairoSurface(mCairoSurface
, aSize
);
83 if (!dt
|| !dt
->IsValid()) {
88 dt
= CreateRecordingDrawTarget(aRecorder
, dt
);
89 if (!dt
|| !dt
->IsValid()) {
97 already_AddRefed
<DrawTarget
> PrintTarget::GetReferenceDrawTarget() {
99 const IntSize
size(1, 1);
101 cairo_surface_t
* similar
;
102 switch (cairo_surface_get_type(mCairoSurface
)) {
103 #ifdef CAIRO_HAS_WIN32_SURFACE
104 case CAIRO_SURFACE_TYPE_WIN32
:
105 similar
= cairo_win32_surface_create_with_dib(
106 CairoContentToCairoFormat(cairo_surface_get_content(mCairoSurface
)),
107 size
.width
, size
.height
);
110 #ifdef CAIRO_HAS_QUARTZ_SURFACE
111 case CAIRO_SURFACE_TYPE_QUARTZ
:
112 similar
= cairo_quartz_surface_create_cg_layer(
113 mCairoSurface
, cairo_surface_get_content(mCairoSurface
), size
.width
,
118 similar
= cairo_surface_create_similar(
119 mCairoSurface
, cairo_surface_get_content(mCairoSurface
), size
.width
,
124 if (cairo_surface_status(similar
)) {
128 RefPtr
<DrawTarget
> dt
=
129 Factory::CreateDrawTargetForCairoSurface(similar
, size
);
131 // The DT addrefs the surface, so we need drop our own reference to it:
132 cairo_surface_destroy(similar
);
134 if (!dt
|| !dt
->IsValid()) {
137 mRefDT
= std::move(dt
);
140 return do_AddRef(mRefDT
);
144 void PrintTarget::AdjustPrintJobNameForIPP(const nsAString
& aJobName
,
145 nsCString
& aAdjustedJobName
) {
146 CopyUTF16toUTF8(aJobName
, aAdjustedJobName
);
148 if (aAdjustedJobName
.Length() > IPP_JOB_NAME_LIMIT_LENGTH
) {
149 uint32_t length
= RewindToPriorUTF8Codepoint(
150 aAdjustedJobName
.get(), (IPP_JOB_NAME_LIMIT_LENGTH
- 3U));
151 aAdjustedJobName
.SetLength(length
);
152 aAdjustedJobName
.AppendLiteral("...");
157 void PrintTarget::AdjustPrintJobNameForIPP(const nsAString
& aJobName
,
158 nsString
& aAdjustedJobName
) {
159 nsAutoCString jobName
;
160 AdjustPrintJobNameForIPP(aJobName
, jobName
);
162 CopyUTF8toUTF16(jobName
, aAdjustedJobName
);
166 already_AddRefed
<DrawTarget
> PrintTarget::CreateRecordingDrawTarget(
167 DrawEventRecorder
* aRecorder
, DrawTarget
* aDrawTarget
) {
168 MOZ_ASSERT(aRecorder
);
169 MOZ_ASSERT(aDrawTarget
);
171 RefPtr
<DrawTarget
> dt
;
174 // It doesn't really matter what we pass as the DrawTarget here.
175 dt
= gfx::Factory::CreateRecordingDrawTarget(aRecorder
, aDrawTarget
,
176 aDrawTarget
->GetRect());
179 if (!dt
|| !dt
->IsValid()) {
181 << "Failed to create a recording DrawTarget for PrintTarget";
188 void PrintTarget::Finish() {
194 // null surfaces are allowed here
195 cairo_surface_finish(mCairoSurface
);
198 void PrintTarget::RegisterPageDoneCallback(PageDoneCallback
&& aCallback
) {
199 MOZ_ASSERT(aCallback
&& !IsSyncPagePrinting());
200 mPageDoneCallback
= std::move(aCallback
);
203 void PrintTarget::UnregisterPageDoneCallback() { mPageDoneCallback
= nullptr; }
205 } // namespace mozilla::gfx