void qprinterSize2CarbonSize(QPrinter::PageSize &size, PMPageFormat &format, PMPrintSession session) { if (size >= QPrinter::Custom) return; // Basically, get the supported pages supported by the printer and see what will work, // Since we document undefined behavior when we don't specify anything, don't worry if we can't. PaperSize newSize = sizes[size]; CFArrayRef formats; PMPrinter printer; if (PMSessionGetCurrentPrinter(session, &printer) == noErr && PMSessionCreatePageFormatList(session, printer, &formats) == noErr) { CFIndex total = CFArrayGetCount(formats); PMPageFormat tmp; PMRect paper; for (CFIndex idx = 0; idx < total; ++idx) { tmp = (PMPageFormat)CFArrayGetValueAtIndex(formats, idx); PMGetUnadjustedPaperRect(tmp, &paper); int wMM = int((paper.right - paper.left) / 72 * 25.4 + 0.5); int hMM = int((paper.bottom - paper.top) / 72 * 25.4 + 0.5); if (newSize.w == wMM && newSize.h == hMM) { PMCopyPageFormat(tmp, format); break; } } CFRelease(formats); } }
void QMacPrintEnginePrivate::setPaperSize(QPrinter::PaperSize ps) { Q_Q(QMacPrintEngine); QSizeF newSize = qt_paperSizeToQSizeF(ps); QCFType<CFArrayRef> formats; PMPrinter printer; if (PMSessionGetCurrentPrinter(session, &printer) == noErr && PMSessionCreatePageFormatList(session, printer, &formats) == noErr) { CFIndex total = CFArrayGetCount(formats); PMPageFormat tmp; PMRect paper; for (CFIndex idx = 0; idx < total; ++idx) { tmp = static_cast<PMPageFormat>( const_cast<void *>(CFArrayGetValueAtIndex(formats, idx))); PMGetUnadjustedPaperRect(tmp, &paper); int wMM = int((paper.right - paper.left) / 72 * 25.4 + 0.5); int hMM = int((paper.bottom - paper.top) / 72 * 25.4 + 0.5); if (newSize.width() == wMM && newSize.height() == hMM) { PMCopyPageFormat(tmp, format); // reset the orientation and resolution as they are lost in the copy. q->setProperty(QPrintEngine::PPK_Orientation, orient); if (PMSessionValidatePageFormat(session, format, kPMDontWantBoolean) != noErr) { // Don't know, warn for the moment. qWarning("QMacPrintEngine, problem setting format and resolution for this page size"); } break; } } } }