Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsDeviceContextSpecX.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "nsCRT.h" |
michael@0 | 9 | #include <unistd.h> |
michael@0 | 10 | |
michael@0 | 11 | #include "nsAutoPtr.h" |
michael@0 | 12 | #include "nsIServiceManager.h" |
michael@0 | 13 | #include "nsIPrintOptions.h" |
michael@0 | 14 | #include "nsPrintSettingsX.h" |
michael@0 | 15 | |
michael@0 | 16 | #include "gfxQuartzSurface.h" |
michael@0 | 17 | |
michael@0 | 18 | // This must be the last include: |
michael@0 | 19 | #include "nsObjCExceptions.h" |
michael@0 | 20 | |
michael@0 | 21 | nsDeviceContextSpecX::nsDeviceContextSpecX() |
michael@0 | 22 | : mPrintSession(NULL) |
michael@0 | 23 | , mPageFormat(kPMNoPageFormat) |
michael@0 | 24 | , mPrintSettings(kPMNoPrintSettings) |
michael@0 | 25 | { |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | nsDeviceContextSpecX::~nsDeviceContextSpecX() |
michael@0 | 29 | { |
michael@0 | 30 | NS_OBJC_BEGIN_TRY_ABORT_BLOCK; |
michael@0 | 31 | |
michael@0 | 32 | if (mPrintSession) |
michael@0 | 33 | ::PMRelease(mPrintSession); |
michael@0 | 34 | |
michael@0 | 35 | NS_OBJC_END_TRY_ABORT_BLOCK; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | NS_IMPL_ISUPPORTS(nsDeviceContextSpecX, nsIDeviceContextSpec) |
michael@0 | 39 | |
michael@0 | 40 | NS_IMETHODIMP nsDeviceContextSpecX::Init(nsIWidget *aWidget, |
michael@0 | 41 | nsIPrintSettings* aPS, |
michael@0 | 42 | bool aIsPrintPreview) |
michael@0 | 43 | { |
michael@0 | 44 | NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 45 | |
michael@0 | 46 | nsRefPtr<nsPrintSettingsX> settings(do_QueryObject(aPS)); |
michael@0 | 47 | if (!settings) |
michael@0 | 48 | return NS_ERROR_NO_INTERFACE; |
michael@0 | 49 | |
michael@0 | 50 | mPrintSession = settings->GetPMPrintSession(); |
michael@0 | 51 | ::PMRetain(mPrintSession); |
michael@0 | 52 | mPageFormat = settings->GetPMPageFormat(); |
michael@0 | 53 | mPrintSettings = settings->GetPMPrintSettings(); |
michael@0 | 54 | |
michael@0 | 55 | return NS_OK; |
michael@0 | 56 | |
michael@0 | 57 | NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | NS_IMETHODIMP nsDeviceContextSpecX::BeginDocument(const nsAString& aTitle, |
michael@0 | 61 | char16_t* aPrintToFileName, |
michael@0 | 62 | int32_t aStartPage, |
michael@0 | 63 | int32_t aEndPage) |
michael@0 | 64 | { |
michael@0 | 65 | NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 66 | |
michael@0 | 67 | if (!aTitle.IsEmpty()) { |
michael@0 | 68 | CFStringRef cfString = |
michael@0 | 69 | ::CFStringCreateWithCharacters(NULL, reinterpret_cast<const UniChar*>(aTitle.BeginReading()), |
michael@0 | 70 | aTitle.Length()); |
michael@0 | 71 | if (cfString) { |
michael@0 | 72 | ::PMPrintSettingsSetJobName(mPrintSettings, cfString); |
michael@0 | 73 | ::CFRelease(cfString); |
michael@0 | 74 | } |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | OSStatus status; |
michael@0 | 78 | status = ::PMSetFirstPage(mPrintSettings, aStartPage, false); |
michael@0 | 79 | NS_ASSERTION(status == noErr, "PMSetFirstPage failed"); |
michael@0 | 80 | status = ::PMSetLastPage(mPrintSettings, aEndPage, false); |
michael@0 | 81 | NS_ASSERTION(status == noErr, "PMSetLastPage failed"); |
michael@0 | 82 | |
michael@0 | 83 | status = ::PMSessionBeginCGDocumentNoDialog(mPrintSession, mPrintSettings, mPageFormat); |
michael@0 | 84 | if (status != noErr) |
michael@0 | 85 | return NS_ERROR_ABORT; |
michael@0 | 86 | |
michael@0 | 87 | return NS_OK; |
michael@0 | 88 | |
michael@0 | 89 | NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | NS_IMETHODIMP nsDeviceContextSpecX::EndDocument() |
michael@0 | 93 | { |
michael@0 | 94 | NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 95 | |
michael@0 | 96 | ::PMSessionEndDocumentNoDialog(mPrintSession); |
michael@0 | 97 | return NS_OK; |
michael@0 | 98 | |
michael@0 | 99 | NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | NS_IMETHODIMP nsDeviceContextSpecX::BeginPage() |
michael@0 | 103 | { |
michael@0 | 104 | NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 105 | |
michael@0 | 106 | PMSessionError(mPrintSession); |
michael@0 | 107 | OSStatus status = ::PMSessionBeginPageNoDialog(mPrintSession, mPageFormat, NULL); |
michael@0 | 108 | if (status != noErr) return NS_ERROR_ABORT; |
michael@0 | 109 | return NS_OK; |
michael@0 | 110 | |
michael@0 | 111 | NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | NS_IMETHODIMP nsDeviceContextSpecX::EndPage() |
michael@0 | 115 | { |
michael@0 | 116 | NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 117 | |
michael@0 | 118 | OSStatus status = ::PMSessionEndPageNoDialog(mPrintSession); |
michael@0 | 119 | if (status != noErr) return NS_ERROR_ABORT; |
michael@0 | 120 | return NS_OK; |
michael@0 | 121 | |
michael@0 | 122 | NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | void nsDeviceContextSpecX::GetPaperRect(double* aTop, double* aLeft, double* aBottom, double* aRight) |
michael@0 | 126 | { |
michael@0 | 127 | NS_OBJC_BEGIN_TRY_ABORT_BLOCK; |
michael@0 | 128 | |
michael@0 | 129 | PMRect paperRect; |
michael@0 | 130 | ::PMGetAdjustedPaperRect(mPageFormat, &paperRect); |
michael@0 | 131 | |
michael@0 | 132 | *aTop = paperRect.top, *aLeft = paperRect.left; |
michael@0 | 133 | *aBottom = paperRect.bottom, *aRight = paperRect.right; |
michael@0 | 134 | |
michael@0 | 135 | NS_OBJC_END_TRY_ABORT_BLOCK; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | NS_IMETHODIMP nsDeviceContextSpecX::GetSurfaceForPrinter(gfxASurface **surface) |
michael@0 | 139 | { |
michael@0 | 140 | NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 141 | |
michael@0 | 142 | double top, left, bottom, right; |
michael@0 | 143 | GetPaperRect(&top, &left, &bottom, &right); |
michael@0 | 144 | const double width = right - left; |
michael@0 | 145 | const double height = bottom - top; |
michael@0 | 146 | |
michael@0 | 147 | CGContextRef context; |
michael@0 | 148 | ::PMSessionGetCGGraphicsContext(mPrintSession, &context); |
michael@0 | 149 | |
michael@0 | 150 | nsRefPtr<gfxASurface> newSurface; |
michael@0 | 151 | |
michael@0 | 152 | if (context) { |
michael@0 | 153 | // Initially, origin is at bottom-left corner of the paper. |
michael@0 | 154 | // Here, we translate it to top-left corner of the paper. |
michael@0 | 155 | CGContextTranslateCTM(context, 0, height); |
michael@0 | 156 | CGContextScaleCTM(context, 1.0, -1.0); |
michael@0 | 157 | newSurface = new gfxQuartzSurface(context, gfxSize(width, height), true); |
michael@0 | 158 | } else { |
michael@0 | 159 | newSurface = new gfxQuartzSurface(gfxSize((int32_t)width, (int32_t)height), gfxImageFormat::ARGB32, true); |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | if (!newSurface) |
michael@0 | 163 | return NS_ERROR_FAILURE; |
michael@0 | 164 | |
michael@0 | 165 | *surface = newSurface; |
michael@0 | 166 | NS_ADDREF(*surface); |
michael@0 | 167 | |
michael@0 | 168 | return NS_OK; |
michael@0 | 169 | |
michael@0 | 170 | NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 171 | } |