248void QWin32PrintEngine::drawTextItem(
const QPointF &p,
const QTextItem &textItem)
250 Q_D(
const QWin32PrintEngine);
252 QAlphaPaintEngine::drawTextItem(p, textItem);
256 const QTextItemInt &ti =
static_cast<
const QTextItemInt &>(textItem);
257 QRgb brushColor = state->pen().brush().color().rgb();
258 bool fallBack = state->pen().brush().style() != Qt::SolidPattern
259 || qAlpha(brushColor) != 0xff
260 || d->txop >= QTransform::TxProject
264 bool deleteFont =
false;
265 HFONT hfont =
nullptr;
266 if (ti.fontEngine->type() == QFontEngine::Win) {
267 hfont =
static_cast<HFONT>(ti.fontEngine->handle());
269#if QT_CONFIG(directwrite)
270 else if (ti.fontEngine->type() == QFontEngine::DirectWrite) {
271 QWindowsFontEngineDirectWrite *fedw =
static_cast<QWindowsFontEngineDirectWrite *>(ti.fontEngine);
272 hfont = fedw->createHFONT();
280 SelectObject(d->hdc, hfont);
281 if (GetDeviceCaps(d->hdc, TECHNOLOGY) != DT_CHARSTREAM) {
283 GetObject(hfont,
sizeof(LOGFONT), &logFont);
286 GetTextFace(d->hdc, 64, n);
287 fallBack = QString::fromWCharArray(n)
288 != QString::fromWCharArray(logFont.lfFaceName);
300 QPaintEngine::drawTextItem(p, textItem);
304 COLORREF cf = RGB(qRed(brushColor), qGreen(brushColor), qBlue(brushColor));
305 SelectObject(d->hdc, CreateSolidBrush(cf));
306 SelectObject(d->hdc, CreatePen(PS_SOLID, 1, cf));
307 SetTextColor(d->hdc, cf);
309 draw_text_item_win(p, ti, d->hdc, d->matrix, QPointF(0.0, 0.0));
310 DeleteObject(SelectObject(d->hdc,GetStockObject(HOLLOW_BRUSH)));
311 DeleteObject(SelectObject(d->hdc,GetStockObject(BLACK_PEN)));
314int QWin32PrintEngine::metric(QPaintDevice::PaintDeviceMetric m)
const
316 Q_D(
const QWin32PrintEngine);
322 int res = d->resolution;
325 case QPaintDevice::PdmWidth:
326 val = d->m_paintRectPixels.width();
327#ifdef QT_DEBUG_METRICS
328 qDebug() <<
"QWin32PrintEngine::metric(PdmWidth) = " << val;
332 case QPaintDevice::PdmHeight:
333 val = d->m_paintRectPixels.height();
334#ifdef QT_DEBUG_METRICS
335 qDebug() <<
"QWin32PrintEngine::metric(PdmHeight) = " << val;
339 case QPaintDevice::PdmDpiX:
342 case QPaintDevice::PdmDpiY:
345 case QPaintDevice::PdmPhysicalDpiX:
346 val = GetDeviceCaps(d->hdc, LOGPIXELSX);
348 case QPaintDevice::PdmPhysicalDpiY:
349 val = GetDeviceCaps(d->hdc, LOGPIXELSY);
351 case QPaintDevice::PdmWidthMM:
352 val = d->m_paintSizeMM.width();
353#ifdef QT_DEBUG_METRICS
354 qDebug() <<
"QWin32PrintEngine::metric(PdmWidthMM) = " << val;
358 case QPaintDevice::PdmHeightMM:
359 val = d->m_paintSizeMM.height();
360#ifdef QT_DEBUG_METRICS
361 qDebug() <<
"QWin32PrintEngine::metric(PdmHeightMM) = " << val;
365 case QPaintDevice::PdmNumColors:
367 int bpp = GetDeviceCaps(d->hdc, BITSPIXEL);
371 val = GetDeviceCaps(d->hdc, NUMCOLORS);
373 val = 1 << (bpp * GetDeviceCaps(d->hdc, PLANES));
376 case QPaintDevice::PdmDepth:
377 val = GetDeviceCaps(d->hdc, PLANES);
379 case QPaintDevice::PdmDevicePixelRatio:
382 case QPaintDevice::PdmDevicePixelRatioScaled:
383 val = 1 * QPaintDevice::devicePixelRatioFScale();
386 qWarning(
"QPrinter::metric: Invalid metric command");
392void QWin32PrintEngine::updateState(
const QPaintEngineState &state)
394 Q_D(QWin32PrintEngine);
396 QAlphaPaintEngine::updateState(state);
400 if (state.state() & DirtyTransform) {
401 updateMatrix(state.transform());
404 if (state.state() & DirtyPen) {
405 d->pen = state.pen();
406 d->has_pen = d->pen.style() != Qt::NoPen && d->pen.isSolid();
409 if (state.state() & DirtyBrush) {
410 QBrush brush = state.brush();
411 d->has_brush = brush.style() == Qt::SolidPattern;
412 d->brush_color = brush.color();
415 if (state.state() & DirtyClipEnabled) {
416 if (state.isClipEnabled())
417 updateClipPath(painter()->clipPath(), Qt::ReplaceClip);
419 updateClipPath(QPainterPath(), Qt::NoClip);
422 if (state.state() & DirtyClipPath) {
423 updateClipPath(state.clipPath(), state.clipOperation());
426 if (state.state() & DirtyClipRegion) {
427 QRegion clipRegion = state.clipRegion();
428 QPainterPath clipPath = qt_regionToPath(clipRegion);
429 updateClipPath(clipPath, state.clipOperation());
433void QWin32PrintEngine::updateClipPath(
const QPainterPath &clipPath, Qt::ClipOperation op)
435 Q_D(QWin32PrintEngine);
438 if (op == Qt::NoClip) {
439 SelectClipRgn(d->hdc,
nullptr);
444 QPainterPath xformed = clipPath * d->matrix;
446 if (xformed.isEmpty()) {
448 HRGN empty = CreateRectRgn(-0x1000000, -0x1000000, -0x0fffffff, -0x0ffffff);
449 SelectClipRgn(d->hdc, empty);
452 d->composeGdiPath(xformed);
459 Q_ASSERT(op > 0 &&
unsigned(op) <=
sizeof(ops) /
sizeof(
int));
460 SelectClipPath(d->hdc, ops[op]);
464 QPainterPath aclip = qt_regionToPath(alphaClipping());
465 if (!aclip.isEmpty()) {
466 QTransform tx(d->stretch_x, 0, 0, d->stretch_y, d->origin_x, d->origin_y);
467 d->composeGdiPath(tx.map(aclip));
468 SelectClipPath(d->hdc, RGN_DIFF);
493void QWin32PrintEngine::drawPixmap(
const QRectF &targetRect,
494 const QPixmap &originalPixmap,
495 const QRectF &sourceRect)
497 Q_D(QWin32PrintEngine);
499 QAlphaPaintEngine::drawPixmap(targetRect, originalPixmap, sourceRect);
503 const int tileSize = 2048;
505 QRectF r = targetRect;
506 QRectF sr = sourceRect;
508 QPixmap pixmap = originalPixmap;
509 if (sr.size() != pixmap.size()) {
510 pixmap = pixmap.copy(sr.toRect());
516 QTransform scaleMatrix = QTransform::fromScale(r.width() / pixmap.width(), r.height() / pixmap.height());
517 QTransform adapted = QPixmap::trueMatrix(d->painterMatrix * scaleMatrix,
518 pixmap.width(), pixmap.height());
520 qreal xform_offset_x = adapted.dx();
521 qreal xform_offset_y = adapted.dy();
523 if (d->complex_xform) {
524 pixmap = pixmap.transformed(adapted);
525 scaleX = d->stretch_x;
526 scaleY = d->stretch_y;
528 scaleX = d->stretch_x * (r.width() / pixmap.width()) * d->painterMatrix.m11();
529 scaleY = d->stretch_y * (r.height() / pixmap.height()) * d->painterMatrix.m22();
532 QPointF topLeft = r.topLeft() * d->painterMatrix;
533 int tx =
int(topLeft.x() * d->stretch_x + d->origin_x);
534 int ty =
int(topLeft.y() * d->stretch_y + d->origin_y);
535 int tw = qAbs(
int(pixmap.width() * scaleX));
536 int th = qAbs(
int(pixmap.height() * scaleY));
538 xform_offset_x *= d->stretch_x;
539 xform_offset_y *= d->stretch_y;
541 int dc_state = SaveDC(d->hdc);
543 int tilesw = pixmap.width() / tileSize;
544 int tilesh = pixmap.height() / tileSize;
548 int txinc = tileSize*scaleX;
549 int tyinc = tileSize*scaleY;
551 for (
int y = 0; y < tilesh; ++y) {
552 int tposy = ty + (y * tyinc);
555 if (y == (tilesh - 1)) {
556 imgh = pixmap.height() - (y * tileSize);
557 height = (th - (y * tyinc));
559 for (
int x = 0; x < tilesw; ++x) {
560 int tposx = tx + (x * txinc);
563 if (x == (tilesw - 1)) {
564 imgw = pixmap.width() - (x * tileSize);
565 width = (tw - (x * txinc));
569 QImage img(QSize(imgw, imgh), QImage::Format_RGB32);
570 img.setDevicePixelRatio(pixmap.devicePixelRatio());
572 QPainter painter(&img);
573 painter.drawPixmap(0,0, pixmap, tileSize * x, tileSize * y, imgw, imgh);
574 QPixmap p = QPixmap::fromImage(img);
576 HBITMAP hbitmap = qt_pixmapToWinHBITMAP(p, HBitmapNoAlpha);
577 HDC hbitmap_hdc = CreateCompatibleDC(d->hdc);
578 HGDIOBJ null_bitmap = SelectObject(hbitmap_hdc, hbitmap);
580 if (!StretchBlt(d->hdc, qRound(tposx - xform_offset_x), qRound(tposy - xform_offset_y), width, height,
581 hbitmap_hdc, 0, 0, p.width(), p.height(), SRCCOPY))
582 qErrnoWarning(
"QWin32PrintEngine::drawPixmap, StretchBlt failed");
584 SelectObject(hbitmap_hdc, null_bitmap);
585 DeleteObject(hbitmap);
586 DeleteDC(hbitmap_hdc);
590 RestoreDC(d->hdc, dc_state);
594void QWin32PrintEngine::drawTiledPixmap(
const QRectF &r,
const QPixmap &pm,
const QPointF &pos)
596 Q_D(QWin32PrintEngine);
598 QAlphaPaintEngine::drawTiledPixmap(r, pm, pos);
602 if (d->complex_xform || !pos.isNull()) {
603 QPaintEngine::drawTiledPixmap(r, pm, pos);
605 int dc_state = SaveDC(d->hdc);
607 HBITMAP hbitmap = qt_pixmapToWinHBITMAP(pm, HBitmapNoAlpha);
608 HDC hbitmap_hdc = CreateCompatibleDC(d->hdc);
609 HGDIOBJ null_bitmap = SelectObject(hbitmap_hdc, hbitmap);
611 QRectF trect = d->painterMatrix.mapRect(r);
612 int tx =
int(trect.left() * d->stretch_x + d->origin_x);
613 int ty =
int(trect.top() * d->stretch_y + d->origin_y);
615 int xtiles =
int(trect.width() / pm.width()) + 1;
616 int ytiles =
int(trect.height() / pm.height()) + 1;
617 int xinc =
int(pm.width() * d->stretch_x);
618 int yinc =
int(pm.height() * d->stretch_y);
620 for (
int y = 0; y < ytiles; ++y) {
621 int ity = ty + (yinc * y);
622 int ith = pm.height();
623 if (y == (ytiles - 1)) {
624 ith =
int(trect.height() - (pm.height() * y));
627 for (
int x = 0; x < xtiles; ++x) {
628 int itx = tx + (xinc * x);
629 int itw = pm.width();
630 if (x == (xtiles - 1)) {
631 itw =
int(trect.width() - (pm.width() * x));
634 if (!StretchBlt(d->hdc, itx, ity,
int(itw * d->stretch_x),
int(ith * d->stretch_y),
635 hbitmap_hdc, 0, 0, itw, ith, SRCCOPY))
636 qErrnoWarning(
"QWin32PrintEngine::drawPixmap, StretchBlt failed");
641 SelectObject(hbitmap_hdc, null_bitmap);
642 DeleteObject(hbitmap);
643 DeleteDC(hbitmap_hdc);
645 RestoreDC(d->hdc, dc_state);
653 qErrnoWarning(
"QWin32PrintEnginePrivate::drawPath: BeginPath failed");
657 for (
int i=0; i<path.elementCount(); ++i) {
658 const QPainterPath::Element &elm = path.elementAt(i);
660 case QPainterPath::MoveToElement:
662 && path.elementAt(start).x == path.elementAt(i-1).x
663 && path.elementAt(start).y == path.elementAt(i-1).y)
666 MoveToEx(hdc, qRound(elm.x), qRound(elm.y), 0);
668 case QPainterPath::LineToElement:
669 LineTo(hdc, qRound(elm.x), qRound(elm.y));
671 case QPainterPath::CurveToElement: {
673 { qRound(elm.x), qRound(elm.y) },
674 { qRound(path.elementAt(i+1).x), qRound(path.elementAt(i+1).y) },
675 { qRound(path.elementAt(i+2).x), qRound(path.elementAt(i+2).y) }
678 PolyBezierTo(hdc, pts, 3);
682 qFatal(
"QWin32PaintEngine::drawPath: Unhandled type: %d", elm.type);
687 && path.elementAt(start).x == path.elementAt(path.elementCount()-1).x
688 && path.elementAt(start).y == path.elementAt(path.elementCount()-1).y)
692 qErrnoWarning(
"QWin32PaintEngine::drawPath: EndPath failed");
694 SetPolyFillMode(hdc, path.fillRule() == Qt::WindingFill ? WINDING : ALTERNATE);
714 composeGdiPath(path);
716 brush.lbStyle = BS_SOLID;
717 brush.lbColor = RGB(color.red(), color.green(), color.blue());
718 DWORD capStyle = PS_ENDCAP_SQUARE;
719 DWORD joinStyle = PS_JOIN_BEVEL;
720 if (pen.capStyle() == Qt::FlatCap)
721 capStyle = PS_ENDCAP_FLAT;
722 else if (pen.capStyle() == Qt::RoundCap)
723 capStyle = PS_ENDCAP_ROUND;
725 if (pen.joinStyle() == Qt::MiterJoin)
726 joinStyle = PS_JOIN_MITER;
727 else if (pen.joinStyle() == Qt::RoundJoin)
728 joinStyle = PS_JOIN_ROUND;
730 HPEN pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | capStyle | joinStyle,
731 (penWidth == 0) ? 1 : penWidth, &brush, 0,
nullptr);
733 HGDIOBJ old_pen = SelectObject(hdc, pen);
735 DeleteObject(SelectObject(hdc, old_pen));
746 QPainterPathStroker stroker;
747 if (pen.style() == Qt::CustomDashLine) {
748 stroker.setDashPattern(pen.dashPattern());
749 stroker.setDashOffset(pen.dashOffset());
751 stroker.setDashPattern(pen.style());
753 stroker.setCapStyle(pen.capStyle());
754 stroker.setJoinStyle(pen.joinStyle());
755 stroker.setMiterLimit(pen.miterLimit());
758 qreal width = pen.widthF();
759 bool cosmetic = pen.isCosmetic();
760 if (pen.style() == Qt::SolidLine && (cosmetic || matrix.type() < QTransform::TxScale)) {
761 strokePath_dev(path * matrix, color, width);
763 stroker.setWidth(width);
765 stroke = stroker.createStroke(path * matrix);
767 stroke = stroker.createStroke(path) * painterMatrix;
768 QTransform stretch(stretch_x, 0, 0, stretch_y, origin_x, origin_y);
769 stroke = stroke * stretch;
772 if (stroke.isEmpty())
775 fillPath_dev(stroke, color);
845 if (!m_printDevice.isValid())
848 txop = QTransform::TxNone;
850 QString printerName = m_printDevice.id();
851 bool ok = OpenPrinter(
reinterpret_cast<LPWSTR>(
const_cast<ushort *>(printerName.utf16())),
852 reinterpret_cast<LPHANDLE>(&hPrinter),
nullptr);
854 qErrnoWarning(
"QWin32PrintEngine::initialize: OpenPrinter failed");
860 DWORD infoSize, numBytes;
861 GetPrinter(hPrinter, 2,
nullptr, 0, &infoSize);
862 hMem = GlobalAlloc(GHND, infoSize);
863 pInfo =
reinterpret_cast<PRINTER_INFO_2*>(GlobalLock(hMem));
864 ok = GetPrinter(hPrinter, 2,
reinterpret_cast<LPBYTE>(pInfo), infoSize, &numBytes);
867 qErrnoWarning(
"QWin32PrintEngine::initialize: GetPrinter failed");
872 devMode = pInfo->pDevMode;
882 auto *lpwPrinterName =
reinterpret_cast<LPWSTR>(
const_cast<ushort *>(printerName.utf16()));
883 LONG result = DocumentProperties(
nullptr, hPrinter, lpwPrinterName,
884 nullptr,
nullptr, 0);
885 devMode =
reinterpret_cast<DEVMODE *>(malloc(result));
886 initializeDevMode(devMode);
890 result = DocumentProperties(
nullptr, hPrinter, lpwPrinterName,
891 devMode,
nullptr, DM_OUT_BUFFER);
892 if (result != IDOK) {
893 qErrnoWarning(
"QWin32PrintEngine::initialize: Failed to obtain devMode");
900 hdc = CreateDC(
nullptr,
reinterpret_cast<LPCWSTR>(printerName.utf16()),
904 qErrnoWarning(
"QWin32PrintEngine::initialize: CreateDC failed");
915 num_copies = devMode->dmCopies;
916 devMode->dmCollate = DMCOLLATE_TRUE;
920#if defined QT_DEBUG_DRAW || defined QT_DEBUG_METRICS
921 qDebug(
"QWin32PrintEngine::initialize()");
1036void QWin32PrintEngine::setProperty(PrintEnginePropertyKey key,
const QVariant &value)
1038 Q_D(QWin32PrintEngine);
1046 case PPK_PaperSources:
1048 case PPK_SupportsMultipleCopies:
1050 case PPK_SupportedResolutions:
1054 case PPK_CustomBase:
1058 case PPK_PrinterProgram:
1060 case PPK_SelectionOption:
1064 case PPK_FontEmbedding:
1065 d->embed_fonts = value.toBool();
1068 case PPK_CollateCopies:
1072 d->devMode->dmCollate = value.toBool() ? DMCOLLATE_TRUE : DMCOLLATE_FALSE;
1073 d->devMode->dmFields |= DM_COLLATE;
1082 d->devMode->dmColor = (value.toInt() == QPrinter::Color) ? DMCOLOR_COLOR : DMCOLOR_MONOCHROME;
1083 d->devMode->dmFields |= DM_COLOR;
1089 d->m_creator = value.toString();
1092 case PPK_DocumentName:
1094 qWarning(
"QWin32PrintEngine: Cannot change document name while printing is active");
1097 d->docName = value.toString();
1103 QPrint::DuplexMode mode = QPrint::DuplexMode(value.toInt());
1104 if (mode == property(PPK_Duplex).toInt() || !d->m_printDevice.supportedDuplexModes().contains(mode))
1107 case QPrint::DuplexNone:
1108 d->devMode->dmDuplex = DMDUP_SIMPLEX;
1109 d->devMode->dmFields |= DM_DUPLEX;
1111 case QPrint::DuplexAuto:
1112 d->devMode->dmDuplex = d->m_pageLayout.orientation() == QPageLayout::Landscape ? DMDUP_HORIZONTAL : DMDUP_VERTICAL;
1113 d->devMode->dmFields |= DM_DUPLEX;
1115 case QPrint::DuplexLongSide:
1116 d->devMode->dmDuplex = DMDUP_VERTICAL;
1117 d->devMode->dmFields |= DM_DUPLEX;
1119 case QPrint::DuplexShortSide:
1120 d->devMode->dmDuplex = DMDUP_HORIZONTAL;
1121 d->devMode->dmFields |= DM_DUPLEX;
1133 d->m_pageLayout.setMode(QPageLayout::FullPageMode);
1135 d->m_pageLayout.setMode(QPageLayout::StandardMode);
1137#ifdef QT_DEBUG_METRICS
1138 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_FullPage," << value.toBool() << +
")";
1144 case PPK_NumberOfCopies:
1147 d->num_copies = value.toInt();
1148 d->devMode->dmCopies = d->num_copies;
1149 d->devMode->dmFields |= DM_COPIES;
1153 case PPK_Orientation: {
1156 QPageLayout::Orientation orientation = QPageLayout::Orientation(value.toInt());
1157 d->devMode->dmOrientation = orientation == QPageLayout::Landscape ? DMORIENT_LANDSCAPE : DMORIENT_PORTRAIT;
1158 d->devMode->dmFields |= DM_ORIENTATION;
1159 d->m_pageLayout.setOrientation(orientation);
1162#ifdef QT_DEBUG_METRICS
1163 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_Orientation," << orientation <<
')';
1169 case PPK_OutputFileName:
1171 qWarning(
"QWin32PrintEngine: Cannot change filename while printing");
1173 d->fileName = value.toString();
1174 d->printToFile = !value.toString().isEmpty();
1178 case PPK_PageSize: {
1181 const QPageSize pageSize = QPageSize(QPageSize::PageSizeId(value.toInt()));
1182 if (pageSize.isValid()) {
1183 d->setPageSize(pageSize);
1185#ifdef QT_DEBUG_METRICS
1186 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_PageSize," << value.toInt() <<
')';
1193 case PPK_PaperName: {
1197 const QPageSize pageSize = d->m_printDevice.supportedPageSize(value.toString());
1198 if (pageSize.isValid()) {
1199 d->setPageSize(pageSize);
1201#ifdef QT_DEBUG_METRICS
1202 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_PaperName," << value.toString() <<
')';
1209 case PPK_PaperSource: {
1212 const auto inputSlots = d->m_printDevice.supportedInputSlots();
1213 const int paperSource = value.toInt();
1214 const int index = paperSource >= DMBIN_USER ?
1215 indexOfWindowsId(inputSlots, paperSource) : indexOfId(inputSlots, QPrint::InputSlotId(paperSource));
1216 d->devMode->dmDefaultSource = index >= 0 ? inputSlots.at(index).windowsId : DMBIN_AUTO;
1221 case PPK_PrinterName: {
1222 QString id = value.toString();
1223 QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
1227 QVariant pageSize = QVariant::fromValue(d->m_pageLayout.pageSize());
1228 const bool isFullPage = (d->m_pageLayout.mode() == QPageLayout::FullPageMode);
1229 QVariant orientation = QVariant::fromValue(d->m_pageLayout.orientation());
1230 QVariant margins = QVariant::fromValue(
1231 std::pair<QMarginsF, QPageLayout::Unit>(d->m_pageLayout.margins(), d->m_pageLayout.units()));
1232 QPrintDevice printDevice = ps->createPrintDevice(id.isEmpty() ? ps->defaultPrintDeviceId() : id);
1233 if (printDevice.isValid()) {
1234 d->m_printDevice = printDevice;
1236 if (d->m_printDevice.supportedPageSize(pageSize.value<QPageSize>()).isValid())
1237 setProperty(PPK_QPageSize, pageSize);
1239 setProperty(PPK_CustomPaperSize, pageSize.value<QPageSize>().size(QPageSize::Point));
1240 setProperty(PPK_FullPage, QVariant(isFullPage));
1241 setProperty(PPK_Orientation, orientation);
1242 setProperty(PPK_QPageMargins, margins);
1247 case PPK_Resolution: {
1248 d->resolution = value.toInt();
1249 d->stretch_x = d->dpi_x /
double(d->resolution);
1250 d->stretch_y = d->dpi_y /
double(d->resolution);
1252#ifdef QT_DEBUG_METRICS
1253 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_Resolution," << value.toInt() <<
')';
1259 case PPK_WindowsPageSize: {
1262 const QPageSize pageSize = QPageSize(QPageSize::id(value.toInt()));
1263 if (pageSize.isValid()) {
1264 d->setPageSize(pageSize);
1266#ifdef QT_DEBUG_METRICS
1267 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_WindowsPageSize," << value.toInt() <<
')';
1275 case PPK_CustomPaperSize: {
1278 const QPageSize pageSize = QPageSize(value.toSizeF(), QPageSize::Point);
1279 if (pageSize.isValid()) {
1280 d->setPageSize(pageSize);
1282#ifdef QT_DEBUG_METRICS
1283 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_CustomPaperSize," << value.toSizeF() <<
')';
1290 case PPK_PageMargins: {
1291 QList<QVariant> margins(value.toList());
1292 Q_ASSERT(margins.size() == 4);
1293 d->m_pageLayout.setUnits(QPageLayout::Point);
1294 d->m_pageLayout.setMargins(QMarginsF(margins.at(0).toReal(), margins.at(1).toReal(),
1295 margins.at(2).toReal(), margins.at(3).toReal()),
1296 QPageLayout::OutOfBoundsPolicy::Clamp);
1298#ifdef QT_DEBUG_METRICS
1299 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_PageMargins," << margins <<
')';
1305 case PPK_QPageSize: {
1309 const QPageSize pageSize = value.value<QPageSize>();
1310 if (pageSize.isValid()) {
1311 d->setPageSize(pageSize);
1313#ifdef QT_DEBUG_METRICS
1314 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_QPageSize," << pageSize <<
')';
1321 case PPK_QPageMargins: {
1322 auto pair = value.value<std::pair<QMarginsF, QPageLayout::Unit>>();
1323 d->m_pageLayout.setUnits(pair.second);
1324 d->m_pageLayout.setMargins(pair.first, QPageLayout::OutOfBoundsPolicy::Clamp);
1326#ifdef QT_DEBUG_METRICS
1327 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_QPageMargins," << pair.first << pair.second <<
')';
1333 case PPK_QPageLayout: {
1334 QPageLayout pageLayout = value.value<QPageLayout>();
1335 if (pageLayout.isValid() && d->m_printDevice.isValidPageLayout(pageLayout, d->resolution)) {
1336 setProperty(PPK_QPageSize, QVariant::fromValue(pageLayout.pageSize()));
1337 setProperty(PPK_FullPage, pageLayout.mode() == QPageLayout::FullPageMode);
1338 setProperty(PPK_Orientation, QVariant::fromValue(pageLayout.orientation()));
1339 d->m_pageLayout.setUnits(pageLayout.units());
1340 d->m_pageLayout.setMargins(pageLayout.margins(), QPageLayout::OutOfBoundsPolicy::Clamp);
1342#ifdef QT_DEBUG_METRICS
1343 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_QPageLayout," << pageLayout <<
')';
1354QVariant QWin32PrintEngine::property(PrintEnginePropertyKey key)
const
1356 Q_D(
const QWin32PrintEngine);
1363 value = QPrinter::FirstPageFirst;
1365 case PPK_PrinterProgram:
1368 case PPK_SelectionOption:
1373 case PPK_FontEmbedding:
1374 value = d->embed_fonts;
1377 case PPK_CollateCopies:
1381 value = d->devMode->dmCollate == DMCOLLATE_TRUE;
1387 value = QPrinter::Color;
1389 value = (d->devMode->dmColor == DMCOLOR_COLOR) ? QPrinter::Color : QPrinter::GrayScale;
1395 value = d->m_creator;
1398 case PPK_DocumentName:
1404 value = QPrinter::DuplexNone;
1406 switch (d->devMode->dmDuplex) {
1407 case DMDUP_VERTICAL:
1408 value = QPrinter::DuplexLongSide;
1410 case DMDUP_HORIZONTAL:
1411 value = QPrinter::DuplexShortSide;
1415 value = QPrinter::DuplexNone;
1423 value = d->m_pageLayout.mode() == QPageLayout::FullPageMode;
1427 value = d->num_copies;
1430 case PPK_SupportsMultipleCopies:
1434 case PPK_NumberOfCopies:
1438 case PPK_Orientation:
1439 value = d->m_pageLayout.orientation();
1442 case PPK_OutputFileName:
1443 value = d->fileName;
1448 value = d->m_pageLayout.paintRectPixels(d->resolution);
1452 value = d->m_pageLayout.pageSize().id();
1457 value = d->m_pageLayout.fullRectPixels(d->resolution);
1461 value = d->m_pageLayout.pageSize().name();
1464 case PPK_PaperSource:
1466 value = d->m_printDevice.defaultInputSlot().id;
1468 if (d->devMode->dmDefaultSource >= DMBIN_USER) {
1469 value =
int(d->devMode->dmDefaultSource);
1471 const auto inputSlots = d->m_printDevice.supportedInputSlots();
1472 const int index = indexOfWindowsId(inputSlots, d->devMode->dmDefaultSource);
1473 value = index >= 0 ? inputSlots.at(index).id : QPrint::Auto;
1478 case PPK_PrinterName:
1479 value = d->m_printDevice.id();
1482 case PPK_Resolution:
1483 if (d->resolution || d->m_printDevice.isValid())
1484 value = d->resolution;
1487 case PPK_SupportedResolutions: {
1488 QList<QVariant> list;
1489 const auto resolutions = d->m_printDevice.supportedResolutions();
1490 list.reserve(resolutions.size());
1491 for (
int resolution : resolutions)
1497 case PPK_WindowsPageSize:
1498 value = d->m_pageLayout.pageSize().windowsId();
1501 case PPK_PaperSources: {
1502 QList<QVariant> out;
1503 const auto inputSlots = d->m_printDevice.supportedInputSlots();
1504 out.reserve(inputSlots.size());
1505 for (
const QPrint::InputSlot &inputSlot : inputSlots)
1506 out << QVariant(inputSlot.id == QPrint::CustomInputSlot ? inputSlot.windowsId :
int(inputSlot.id));
1511 case PPK_CustomPaperSize:
1512 value = d->m_pageLayout.fullRectPoints().size();
1515 case PPK_PageMargins: {
1516 QList<QVariant> list;
1517 QMarginsF margins = d->m_pageLayout.margins(QPageLayout::Point);
1518 list << margins.left() << margins.top() << margins.right() << margins.bottom();
1524 value.setValue(d->m_pageLayout.pageSize());
1527 case PPK_QPageMargins: {
1528 std::pair<QMarginsF, QPageLayout::Unit> pair(d->m_pageLayout.margins(), d->m_pageLayout.units());
1529 value.setValue(pair);
1533 case PPK_QPageLayout:
1534 value.setValue(d->m_pageLayout);
1537 case PPK_CustomBase:
1628 if (!pageSize.isValid())
1634 const QPageSize printerPageSize = m_printDevice.supportedPageSize(pageSize);
1635 const QPageSize usePageSize = printerPageSize.isValid() ? printerPageSize : pageSize;
1637 const QMarginsF printable = m_printDevice.printableMargins(usePageSize, m_pageLayout.orientation(), resolution);
1638 m_pageLayout.setPageSize(usePageSize, qt_convertMargins(printable, QPageLayout::Point, m_pageLayout.units()));
1641 if (printerPageSize.isValid()) {
1642 has_custom_paper_size =
false;
1643 devMode->dmPaperSize = m_pageLayout.pageSize().windowsId();
1644 devMode->dmFields &= ~(DM_PAPERLENGTH | DM_PAPERWIDTH);
1645 devMode->dmPaperWidth = 0;
1646 devMode->dmPaperLength = 0;
1648 has_custom_paper_size =
true;
1649 devMode->dmPaperSize = DMPAPER_USER;
1650 devMode->dmFields |= DM_PAPERLENGTH | DM_PAPERWIDTH;
1652 const QSizeF sizeMM = m_pageLayout.pageSize().size(QPageSize::Millimeter);
1653 devMode->dmPaperWidth = qRound(sizeMM.width() * 10.0);
1654 devMode->dmPaperLength = qRound(sizeMM.height() * 10.0);
1665 m_pageLayout.setOrientation(devMode->dmOrientation == DMORIENT_LANDSCAPE ? QPageLayout::Landscape : QPageLayout::Portrait);
1666 if (devMode->dmPaperSize >= DMPAPER_LAST) {
1669 bool hasCustom =
false;
1670 int feature = PSIDENT_GDICENTRIC;
1671 if (ExtEscape(hdc, POSTSCRIPT_IDENTIFY,
1672 sizeof(DWORD),
reinterpret_cast<LPCSTR>(&feature), 0, 0) >= 0) {
1673 PSFEATURE_CUSTPAPER custPaper;
1674 feature = FEATURESETTING_CUSTPAPER;
1675 if (ExtEscape(hdc, GET_PS_FEATURESETTING,
sizeof(INT),
reinterpret_cast<LPCSTR>(&feature),
1676 sizeof(custPaper),
reinterpret_cast<LPSTR>(&custPaper)) > 0) {
1678 if (!(custPaper.lOrientation == 1 && custPaper.lWidth == 0 && custPaper.lHeight == 0)) {
1679 if (custPaper.lOrientation == 0 || custPaper.lOrientation == 2)
1680 m_pageLayout.setOrientation(QPageLayout::Portrait);
1682 m_pageLayout.setOrientation(QPageLayout::Landscape);
1683 QPageSize pageSize = QPageSize(QSizeF(custPaper.lWidth, custPaper.lHeight),
1685 setPageSize(pageSize);
1691 QPageSize pageSize = QPageSize(QSizeF(devMode->dmPaperWidth / 10.0f, devMode->dmPaperLength / 10.0f),
1692 QPageSize::Millimeter);
1693 setPageSize(pageSize);
1697 setPageSize(QPageSize(QPageSize::id(devMode->dmPaperSize)));
1705 m_paintRectPixels = m_pageLayout.paintRectPixels(resolution);
1707 qreal pageScaleX = 1;
1708 qreal pageScaleY = 1;
1714 if (!has_custom_paper_size) {
1715 const int devWidth = GetDeviceCaps(hdc, PHYSICALWIDTH);
1716 const int devHeight = GetDeviceCaps(hdc, PHYSICALHEIGHT);
1717 const int pageWidth = m_pageLayout.fullRectPixels(dpi_x).width();
1718 const int pageHeight = m_pageLayout.fullRectPixels(dpi_y).height();
1719 pageScaleX = (devWidth && pageWidth) ? qreal(devWidth) / pageWidth : 1;
1720 pageScaleY = (devHeight && pageHeight) ? qreal(devHeight) / pageHeight : 1;
1721 m_paintRectPixels = QTransform::fromScale(pageScaleX, pageScaleY).mapRect(m_paintRectPixels);
1724 QSizeF sizeMM = m_pageLayout.paintRect(QPageLayout::Millimeter).size();
1725 m_paintSizeMM = QSize(qRound(sizeMM.width()), qRound(sizeMM.height()));
1728 const bool isFullPage = (m_pageLayout.mode() == QPageLayout::FullPageMode);
1729 const QMarginsF margins = isFullPage ? QMarginsF() : (m_pageLayout.margins(QPageLayout::Millimeter) / 25.4);
1730 origin_x = qRound(pageScaleX * margins.left() *
dpi_x) - GetDeviceCaps(hdc, PHYSICALOFFSETX);
1731 origin_y = qRound(pageScaleY * margins.top() *
dpi_y) - GetDeviceCaps(hdc, PHYSICALOFFSETY);
1747 const QTransform &xform,
const QPointF &topLeft)
1749 QPointF baseline_pos = xform.inverted().map(xform.map(pos) - topLeft);
1751 SetTextAlign(hdc, TA_BASELINE);
1752 SetBkMode(hdc, TRANSPARENT);
1754 const bool has_kerning = ti.f && ti.f->kerning();
1756 HFONT hfont =
nullptr;
1757 bool deleteFont =
false;
1759 if (ti.fontEngine->type() == QFontEngine::Win) {
1760 if (ti.fontEngine->supportsTransformation(QTransform::fromScale(0.5, 0.5)))
1761 hfont =
static_cast<HFONT>(ti.fontEngine->handle());
1763#if QT_CONFIG(directwrite)
1764 else if (ti.fontEngine->type() == QFontEngine::DirectWrite) {
1765 QWindowsFontEngineDirectWrite *fedw =
static_cast<QWindowsFontEngineDirectWrite *>(ti.fontEngine);
1766 hfont = fedw->createHFONT();
1773 hfont = (HFONT)GetStockObject(ANSI_VAR_FONT);
1775 HGDIOBJ old_font = SelectObject(hdc, hfont);
1776 unsigned int options = ETO_GLYPH_INDEX;
1777 QGlyphLayout glyphs = ti.glyphs;
1779 bool fast = !has_kerning && !(ti.flags & QTextItem::RightToLeft);
1780 for (
int i = 0; fast && i < glyphs.numGlyphs; i++) {
1781 if (glyphs.offsets[i].x != 0 || glyphs.offsets[i].y != 0 || glyphs.justifications[i].space_18d6 != 0
1782 || glyphs.attributes[i].dontPrint) {
1790 win_xform.eM11 = xform.m11();
1791 win_xform.eM12 = xform.m12();
1792 win_xform.eM21 = xform.m21();
1793 win_xform.eM22 = xform.m22();
1794 win_xform.eDx = xform.dx();
1795 win_xform.eDy = xform.dy();
1797 SetGraphicsMode(hdc, GM_ADVANCED);
1798 SetWorldTransform(hdc, &win_xform);
1802 QVarLengthArray<
wchar_t> g(glyphs.numGlyphs);
1803 for (
int i = 0; i < glyphs.numGlyphs; ++i)
1804 g[i] = glyphs.glyphs[i];
1806 qRound(baseline_pos.x() + glyphs.offsets[0].x.toReal()),
1807 qRound(baseline_pos.y() + glyphs.offsets[0].y.toReal()),
1808 options, 0, g.constData(), glyphs.numGlyphs, 0);
1810 QVarLengthArray<QFixedPoint> positions;
1811 QVarLengthArray<glyph_t> _glyphs;
1813 QTransform matrix = QTransform::fromTranslate(baseline_pos.x(), baseline_pos.y());
1814 ti.fontEngine->getGlyphPositions(ti.glyphs, matrix, ti.flags,
1815 _glyphs, positions);
1816 if (_glyphs.isEmpty()) {
1817 SelectObject(hdc, old_font);
1822 QVarLengthArray<INT> glyphDistances(_glyphs.size() * 2);
1823 QVarLengthArray<
wchar_t> g(_glyphs.size());
1824 const int lastGlyph = _glyphs.size() - 1;
1825 for (
int i = 0; i < lastGlyph; ++i) {
1826 glyphDistances[i * 2] = qRound(positions[i + 1].x) - qRound(positions[i].x);
1827 glyphDistances[i * 2 + 1] = qRound(positions[i + 1].y) - qRound(positions[i].y);
1830 glyphDistances[lastGlyph * 2] = 0;
1831 glyphDistances[lastGlyph * 2 + 1] = 0;
1832 g[lastGlyph] = _glyphs[lastGlyph];
1833 ExtTextOut(hdc, qRound(positions[0].x), qRound(positions[0].y), options,
nullptr,
1834 g.constData(), _glyphs.size(),
1835 glyphDistances.data());
1838 win_xform.eM11 = win_xform.eM22 = 1.0;
1839 win_xform.eM12 = win_xform.eM21 = win_xform.eDx = win_xform.eDy = 0.0;
1840 SetWorldTransform(hdc, &win_xform);
1842 SelectObject(hdc, old_font);
1845 DeleteObject(hfont);
QMarginsF qt_convertMargins(const QMarginsF &margins, QPageLayout::Unit fromUnits, QPageLayout::Unit toUnits)