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 "mozilla/StaticPrefs_gfx.h"
19 #include "nsReadableUtils.h"
21 #include "nsUTF8Utils.h"
23 // IPP spec disallow the job-name which is over 255 characters.
24 // RFC: https://tools.ietf.org/html/rfc2911#section-4.1.2
25 #define IPP_JOB_NAME_LIMIT_LENGTH 255
27 namespace mozilla::gfx
{
29 PrintTarget::PrintTarget(cairo_surface_t
* aCairoSurface
, const IntSize
& aSize
)
30 : mCairoSurface(aCairoSurface
),
40 // aCairoSurface is null when our PrintTargetThebes subclass's ctor calls us.
41 // Once PrintTargetThebes is removed, enable this assertion.
42 MOZ_ASSERT(aCairoSurface
&& !cairo_surface_status(aCairoSurface
),
43 "CreateOrNull factory methods should not call us without a "
44 "valid cairo_surface_t*");
47 // CreateOrNull factory methods hand over ownership of aCairoSurface,
48 // so we don't call cairo_surface_reference(aSurface) here.
50 // 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
);
58 PrintTarget::~PrintTarget() {
59 // null surfaces are allowed here
60 cairo_surface_destroy(mCairoSurface
);
61 mCairoSurface
= nullptr;
64 already_AddRefed
<DrawTarget
> PrintTarget::MakeDrawTarget(
65 const IntSize
& aSize
, DrawEventRecorder
* aRecorder
) {
66 MOZ_ASSERT(mCairoSurface
,
67 "We shouldn't have been constructed without a cairo surface");
69 // This should not be called outside of BeginPage()/EndPage() calls since
70 // some backends can only provide a valid DrawTarget at that time.
71 MOZ_ASSERT(mHasActivePage
, "We can't guarantee a valid DrawTarget");
73 if (cairo_surface_status(mCairoSurface
)) {
77 // Note than aSize may not be the same as mSize (the size of mCairoSurface).
78 // See the comments in our header. If the sizes are different a clip will
79 // be applied to mCairoSurface.
80 RefPtr
<DrawTarget
> dt
=
81 Factory::CreateDrawTargetForCairoSurface(mCairoSurface
, aSize
);
82 if (!dt
|| !dt
->IsValid()) {
87 dt
= CreateRecordingDrawTarget(aRecorder
, dt
);
88 if (!dt
|| !dt
->IsValid()) {
96 already_AddRefed
<DrawTarget
> PrintTarget::GetReferenceDrawTarget() {
98 const IntSize
size(1, 1);
100 cairo_surface_t
* similar
;
101 switch (cairo_surface_get_type(mCairoSurface
)) {
102 #ifdef CAIRO_HAS_WIN32_SURFACE
103 case CAIRO_SURFACE_TYPE_WIN32
:
104 similar
= cairo_win32_surface_create_with_dib(
105 CairoContentToCairoFormat(cairo_surface_get_content(mCairoSurface
)),
106 size
.width
, size
.height
);
109 #ifdef CAIRO_HAS_QUARTZ_SURFACE
110 case CAIRO_SURFACE_TYPE_QUARTZ
:
111 if (StaticPrefs::gfx_cairo_quartz_cg_layer_enabled()) {
112 similar
= cairo_quartz_surface_create_cg_layer(
113 mCairoSurface
, cairo_surface_get_content(mCairoSurface
),
114 size
.width
, size
.height
);
120 similar
= cairo_surface_create_similar(
121 mCairoSurface
, cairo_surface_get_content(mCairoSurface
), size
.width
,
126 if (cairo_surface_status(similar
)) {
130 RefPtr
<DrawTarget
> dt
=
131 Factory::CreateDrawTargetForCairoSurface(similar
, size
);
133 // The DT addrefs the surface, so we need drop our own reference to it:
134 cairo_surface_destroy(similar
);
136 if (!dt
|| !dt
->IsValid()) {
139 mRefDT
= std::move(dt
);
142 return do_AddRef(mRefDT
);
146 void PrintTarget::AdjustPrintJobNameForIPP(const nsAString
& aJobName
,
147 nsCString
& aAdjustedJobName
) {
148 CopyUTF16toUTF8(aJobName
, aAdjustedJobName
);
150 if (aAdjustedJobName
.Length() > IPP_JOB_NAME_LIMIT_LENGTH
) {
151 uint32_t length
= RewindToPriorUTF8Codepoint(
152 aAdjustedJobName
.get(), (IPP_JOB_NAME_LIMIT_LENGTH
- 3U));
153 aAdjustedJobName
.SetLength(length
);
154 aAdjustedJobName
.AppendLiteral("...");
159 void PrintTarget::AdjustPrintJobNameForIPP(const nsAString
& aJobName
,
160 nsString
& aAdjustedJobName
) {
161 nsAutoCString jobName
;
162 AdjustPrintJobNameForIPP(aJobName
, jobName
);
164 CopyUTF8toUTF16(jobName
, aAdjustedJobName
);
168 already_AddRefed
<DrawTarget
> PrintTarget::CreateRecordingDrawTarget(
169 DrawEventRecorder
* aRecorder
, DrawTarget
* aDrawTarget
) {
170 MOZ_ASSERT(aRecorder
);
171 MOZ_ASSERT(aDrawTarget
);
173 RefPtr
<DrawTarget
> dt
;
176 // It doesn't really matter what we pass as the DrawTarget here.
177 dt
= gfx::Factory::CreateRecordingDrawTarget(aRecorder
, aDrawTarget
,
178 aDrawTarget
->GetRect());
181 if (!dt
|| !dt
->IsValid()) {
183 << "Failed to create a recording DrawTarget for PrintTarget";
190 void PrintTarget::Finish() {
196 // null surfaces are allowed here
197 cairo_surface_finish(mCairoSurface
);
200 } // namespace mozilla::gfx