246void QWin32PrintEngine::drawTextItem(
const QPointF &p,
const QTextItem &textItem)
248 Q_D(
const QWin32PrintEngine);
250 QAlphaPaintEngine::drawTextItem(p, textItem);
254 const QTextItemInt &ti =
static_cast<
const QTextItemInt &>(textItem);
255 QRgb brushColor = state->pen().brush().color().rgb();
256 bool fallBack = state->pen().brush().style() != Qt::SolidPattern
257 || qAlpha(brushColor) != 0xff
258 || d->txop >= QTransform::TxProject
262 bool deleteFont =
false;
263 HFONT hfont =
nullptr;
264 if (ti.fontEngine->type() == QFontEngine::Win) {
265 hfont =
static_cast<HFONT>(ti.fontEngine->handle());
267#if QT_CONFIG(directwrite)
268 else if (ti.fontEngine->type() == QFontEngine::DirectWrite) {
269 QWindowsFontEngineDirectWrite *fedw =
static_cast<QWindowsFontEngineDirectWrite *>(ti.fontEngine);
270 hfont = fedw->createHFONT();
278 SelectObject(d->hdc, hfont);
279 if (GetDeviceCaps(d->hdc, TECHNOLOGY) != DT_CHARSTREAM) {
281 GetObject(hfont,
sizeof(LOGFONT), &logFont);
284 GetTextFace(d->hdc, 64, n);
285 fallBack = QString::fromWCharArray(n)
286 != QString::fromWCharArray(logFont.lfFaceName);
298 QPaintEngine::drawTextItem(p, textItem);
302 COLORREF cf = RGB(qRed(brushColor), qGreen(brushColor), qBlue(brushColor));
303 SelectObject(d->hdc, CreateSolidBrush(cf));
304 SelectObject(d->hdc, CreatePen(PS_SOLID, 1, cf));
305 SetTextColor(d->hdc, cf);
307 draw_text_item_win(p, ti, d->hdc, d->matrix, QPointF(0.0, 0.0));
308 DeleteObject(SelectObject(d->hdc,GetStockObject(HOLLOW_BRUSH)));
309 DeleteObject(SelectObject(d->hdc,GetStockObject(BLACK_PEN)));
312int QWin32PrintEngine::metric(QPaintDevice::PaintDeviceMetric m)
const
314 Q_D(
const QWin32PrintEngine);
320 int res = d->resolution;
323 case QPaintDevice::PdmWidth:
324 val = d->m_paintRectPixels.width();
325#ifdef QT_DEBUG_METRICS
326 qDebug() <<
"QWin32PrintEngine::metric(PdmWidth) = " << val;
330 case QPaintDevice::PdmHeight:
331 val = d->m_paintRectPixels.height();
332#ifdef QT_DEBUG_METRICS
333 qDebug() <<
"QWin32PrintEngine::metric(PdmHeight) = " << val;
337 case QPaintDevice::PdmDpiX:
340 case QPaintDevice::PdmDpiY:
343 case QPaintDevice::PdmPhysicalDpiX:
344 val = GetDeviceCaps(d->hdc, LOGPIXELSX);
346 case QPaintDevice::PdmPhysicalDpiY:
347 val = GetDeviceCaps(d->hdc, LOGPIXELSY);
349 case QPaintDevice::PdmWidthMM:
350 val = d->m_paintSizeMM.width();
351#ifdef QT_DEBUG_METRICS
352 qDebug() <<
"QWin32PrintEngine::metric(PdmWidthMM) = " << val;
356 case QPaintDevice::PdmHeightMM:
357 val = d->m_paintSizeMM.height();
358#ifdef QT_DEBUG_METRICS
359 qDebug() <<
"QWin32PrintEngine::metric(PdmHeightMM) = " << val;
363 case QPaintDevice::PdmNumColors:
365 int bpp = GetDeviceCaps(d->hdc, BITSPIXEL);
369 val = GetDeviceCaps(d->hdc, NUMCOLORS);
371 val = 1 << (bpp * GetDeviceCaps(d->hdc, PLANES));
374 case QPaintDevice::PdmDepth:
375 val = GetDeviceCaps(d->hdc, PLANES);
377 case QPaintDevice::PdmDevicePixelRatio:
380 case QPaintDevice::PdmDevicePixelRatioScaled:
381 val = 1 * QPaintDevice::devicePixelRatioFScale();
384 qWarning(
"QPrinter::metric: Invalid metric command");
390void QWin32PrintEngine::updateState(
const QPaintEngineState &state)
392 Q_D(QWin32PrintEngine);
394 QAlphaPaintEngine::updateState(state);
398 if (state.state() & DirtyTransform) {
399 updateMatrix(state.transform());
402 if (state.state() & DirtyPen) {
403 d->pen = state.pen();
404 d->has_pen = d->pen.style() != Qt::NoPen && d->pen.isSolid();
407 if (state.state() & DirtyBrush) {
408 QBrush brush = state.brush();
409 d->has_brush = brush.style() == Qt::SolidPattern;
410 d->brush_color = brush.color();
413 if (state.state() & DirtyClipEnabled) {
414 if (state.isClipEnabled())
415 updateClipPath(painter()->clipPath(), Qt::ReplaceClip);
417 updateClipPath(QPainterPath(), Qt::NoClip);
420 if (state.state() & DirtyClipPath) {
421 updateClipPath(state.clipPath(), state.clipOperation());
424 if (state.state() & DirtyClipRegion) {
425 QRegion clipRegion = state.clipRegion();
426 QPainterPath clipPath = qt_regionToPath(clipRegion);
427 updateClipPath(clipPath, state.clipOperation());
431void QWin32PrintEngine::updateClipPath(
const QPainterPath &clipPath, Qt::ClipOperation op)
433 Q_D(QWin32PrintEngine);
436 if (op == Qt::NoClip) {
437 SelectClipRgn(d->hdc,
nullptr);
442 QPainterPath xformed = clipPath * d->matrix;
444 if (xformed.isEmpty()) {
446 HRGN empty = CreateRectRgn(-0x1000000, -0x1000000, -0x0fffffff, -0x0ffffff);
447 SelectClipRgn(d->hdc, empty);
450 d->composeGdiPath(xformed);
457 Q_ASSERT(op > 0 &&
unsigned(op) <=
sizeof(ops) /
sizeof(
int));
458 SelectClipPath(d->hdc, ops[op]);
462 QPainterPath aclip = qt_regionToPath(alphaClipping());
463 if (!aclip.isEmpty()) {
464 QTransform tx(d->stretch_x, 0, 0, d->stretch_y, d->origin_x, d->origin_y);
465 d->composeGdiPath(tx.map(aclip));
466 SelectClipPath(d->hdc, RGN_DIFF);
491void QWin32PrintEngine::drawPixmap(
const QRectF &targetRect,
492 const QPixmap &originalPixmap,
493 const QRectF &sourceRect)
495 Q_D(QWin32PrintEngine);
497 QAlphaPaintEngine::drawPixmap(targetRect, originalPixmap, sourceRect);
501 const int tileSize = 2048;
503 QRectF r = targetRect;
504 QRectF sr = sourceRect;
506 QPixmap pixmap = originalPixmap;
507 if (sr.size() != pixmap.size()) {
508 pixmap = pixmap.copy(sr.toRect());
514 QTransform scaleMatrix = QTransform::fromScale(r.width() / pixmap.width(), r.height() / pixmap.height());
515 QTransform adapted = QPixmap::trueMatrix(d->painterMatrix * scaleMatrix,
516 pixmap.width(), pixmap.height());
518 qreal xform_offset_x = adapted.dx();
519 qreal xform_offset_y = adapted.dy();
521 if (d->complex_xform) {
522 pixmap = pixmap.transformed(adapted);
523 scaleX = d->stretch_x;
524 scaleY = d->stretch_y;
526 scaleX = d->stretch_x * (r.width() / pixmap.width()) * d->painterMatrix.m11();
527 scaleY = d->stretch_y * (r.height() / pixmap.height()) * d->painterMatrix.m22();
530 QPointF topLeft = r.topLeft() * d->painterMatrix;
531 int tx =
int(topLeft.x() * d->stretch_x + d->origin_x);
532 int ty =
int(topLeft.y() * d->stretch_y + d->origin_y);
533 int tw = qAbs(
int(pixmap.width() * scaleX));
534 int th = qAbs(
int(pixmap.height() * scaleY));
536 xform_offset_x *= d->stretch_x;
537 xform_offset_y *= d->stretch_y;
539 int dc_state = SaveDC(d->hdc);
541 int tilesw = pixmap.width() / tileSize;
542 int tilesh = pixmap.height() / tileSize;
546 int txinc = tileSize*scaleX;
547 int tyinc = tileSize*scaleY;
549 for (
int y = 0; y < tilesh; ++y) {
550 int tposy = ty + (y * tyinc);
553 if (y == (tilesh - 1)) {
554 imgh = pixmap.height() - (y * tileSize);
555 height = (th - (y * tyinc));
557 for (
int x = 0; x < tilesw; ++x) {
558 int tposx = tx + (x * txinc);
561 if (x == (tilesw - 1)) {
562 imgw = pixmap.width() - (x * tileSize);
563 width = (tw - (x * txinc));
567 QImage img(QSize(imgw, imgh), QImage::Format_RGB32);
568 img.setDevicePixelRatio(pixmap.devicePixelRatio());
570 QPainter painter(&img);
571 painter.drawPixmap(0,0, pixmap, tileSize * x, tileSize * y, imgw, imgh);
572 QPixmap p = QPixmap::fromImage(img);
574 HBITMAP hbitmap = qt_pixmapToWinHBITMAP(p, HBitmapNoAlpha);
575 HDC hbitmap_hdc = CreateCompatibleDC(d->hdc);
576 HGDIOBJ null_bitmap = SelectObject(hbitmap_hdc, hbitmap);
578 if (!StretchBlt(d->hdc, qRound(tposx - xform_offset_x), qRound(tposy - xform_offset_y), width, height,
579 hbitmap_hdc, 0, 0, p.width(), p.height(), SRCCOPY))
580 qErrnoWarning(
"QWin32PrintEngine::drawPixmap, StretchBlt failed");
582 SelectObject(hbitmap_hdc, null_bitmap);
583 DeleteObject(hbitmap);
584 DeleteDC(hbitmap_hdc);
588 RestoreDC(d->hdc, dc_state);
592void QWin32PrintEngine::drawTiledPixmap(
const QRectF &r,
const QPixmap &pm,
const QPointF &pos)
594 Q_D(QWin32PrintEngine);
596 QAlphaPaintEngine::drawTiledPixmap(r, pm, pos);
600 if (d->complex_xform || !pos.isNull()) {
601 QPaintEngine::drawTiledPixmap(r, pm, pos);
603 int dc_state = SaveDC(d->hdc);
605 HBITMAP hbitmap = qt_pixmapToWinHBITMAP(pm, HBitmapNoAlpha);
606 HDC hbitmap_hdc = CreateCompatibleDC(d->hdc);
607 HGDIOBJ null_bitmap = SelectObject(hbitmap_hdc, hbitmap);
609 QRectF trect = d->painterMatrix.mapRect(r);
610 int tx =
int(trect.left() * d->stretch_x + d->origin_x);
611 int ty =
int(trect.top() * d->stretch_y + d->origin_y);
613 int xtiles =
int(trect.width() / pm.width()) + 1;
614 int ytiles =
int(trect.height() / pm.height()) + 1;
615 int xinc =
int(pm.width() * d->stretch_x);
616 int yinc =
int(pm.height() * d->stretch_y);
618 for (
int y = 0; y < ytiles; ++y) {
619 int ity = ty + (yinc * y);
620 int ith = pm.height();
621 if (y == (ytiles - 1)) {
622 ith =
int(trect.height() - (pm.height() * y));
625 for (
int x = 0; x < xtiles; ++x) {
626 int itx = tx + (xinc * x);
627 int itw = pm.width();
628 if (x == (xtiles - 1)) {
629 itw =
int(trect.width() - (pm.width() * x));
632 if (!StretchBlt(d->hdc, itx, ity,
int(itw * d->stretch_x),
int(ith * d->stretch_y),
633 hbitmap_hdc, 0, 0, itw, ith, SRCCOPY))
634 qErrnoWarning(
"QWin32PrintEngine::drawPixmap, StretchBlt failed");
639 SelectObject(hbitmap_hdc, null_bitmap);
640 DeleteObject(hbitmap);
641 DeleteDC(hbitmap_hdc);
643 RestoreDC(d->hdc, dc_state);
651 qErrnoWarning(
"QWin32PrintEnginePrivate::drawPath: BeginPath failed");
655 for (
int i=0; i<path.elementCount(); ++i) {
656 const QPainterPath::Element &elm = path.elementAt(i);
658 case QPainterPath::MoveToElement:
660 && path.elementAt(start).x == path.elementAt(i-1).x
661 && path.elementAt(start).y == path.elementAt(i-1).y)
664 MoveToEx(hdc, qRound(elm.x), qRound(elm.y), 0);
666 case QPainterPath::LineToElement:
667 LineTo(hdc, qRound(elm.x), qRound(elm.y));
669 case QPainterPath::CurveToElement: {
671 { qRound(elm.x), qRound(elm.y) },
672 { qRound(path.elementAt(i+1).x), qRound(path.elementAt(i+1).y) },
673 { qRound(path.elementAt(i+2).x), qRound(path.elementAt(i+2).y) }
676 PolyBezierTo(hdc, pts, 3);
680 qFatal(
"QWin32PaintEngine::drawPath: Unhandled type: %d", elm.type);
685 && path.elementAt(start).x == path.elementAt(path.elementCount()-1).x
686 && path.elementAt(start).y == path.elementAt(path.elementCount()-1).y)
690 qErrnoWarning(
"QWin32PaintEngine::drawPath: EndPath failed");
692 SetPolyFillMode(hdc, path.fillRule() == Qt::WindingFill ? WINDING : ALTERNATE);
712 composeGdiPath(path);
714 brush.lbStyle = BS_SOLID;
715 brush.lbColor = RGB(color.red(), color.green(), color.blue());
716 DWORD capStyle = PS_ENDCAP_SQUARE;
717 DWORD joinStyle = PS_JOIN_BEVEL;
718 if (pen.capStyle() == Qt::FlatCap)
719 capStyle = PS_ENDCAP_FLAT;
720 else if (pen.capStyle() == Qt::RoundCap)
721 capStyle = PS_ENDCAP_ROUND;
723 if (pen.joinStyle() == Qt::MiterJoin)
724 joinStyle = PS_JOIN_MITER;
725 else if (pen.joinStyle() == Qt::RoundJoin)
726 joinStyle = PS_JOIN_ROUND;
728 HPEN pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | capStyle | joinStyle,
729 (penWidth == 0) ? 1 : penWidth, &brush, 0,
nullptr);
731 HGDIOBJ old_pen = SelectObject(hdc, pen);
733 DeleteObject(SelectObject(hdc, old_pen));
744 QPainterPathStroker stroker;
745 if (pen.style() == Qt::CustomDashLine) {
746 stroker.setDashPattern(pen.dashPattern());
747 stroker.setDashOffset(pen.dashOffset());
749 stroker.setDashPattern(pen.style());
751 stroker.setCapStyle(pen.capStyle());
752 stroker.setJoinStyle(pen.joinStyle());
753 stroker.setMiterLimit(pen.miterLimit());
756 qreal width = pen.widthF();
757 bool cosmetic = pen.isCosmetic();
758 if (pen.style() == Qt::SolidLine && (cosmetic || matrix.type() < QTransform::TxScale)) {
759 strokePath_dev(path * matrix, color, width);
761 stroker.setWidth(width);
763 stroke = stroker.createStroke(path * matrix);
765 stroke = stroker.createStroke(path) * painterMatrix;
766 QTransform stretch(stretch_x, 0, 0, stretch_y, origin_x, origin_y);
767 stroke = stroke * stretch;
770 if (stroke.isEmpty())
773 fillPath_dev(stroke, color);
843 if (!m_printDevice.isValid())
846 txop = QTransform::TxNone;
848 QString printerName = m_printDevice.id();
849 bool ok = OpenPrinter(
reinterpret_cast<LPWSTR>(
const_cast<ushort *>(printerName.utf16())),
850 reinterpret_cast<LPHANDLE>(&hPrinter),
nullptr);
852 qErrnoWarning(
"QWin32PrintEngine::initialize: OpenPrinter failed");
858 DWORD infoSize, numBytes;
859 GetPrinter(hPrinter, 2,
nullptr, 0, &infoSize);
860 hMem = GlobalAlloc(GHND, infoSize);
861 pInfo =
reinterpret_cast<PRINTER_INFO_2*>(GlobalLock(hMem));
862 ok = GetPrinter(hPrinter, 2,
reinterpret_cast<LPBYTE>(pInfo), infoSize, &numBytes);
865 qErrnoWarning(
"QWin32PrintEngine::initialize: GetPrinter failed");
870 devMode = pInfo->pDevMode;
880 auto *lpwPrinterName =
reinterpret_cast<LPWSTR>(
const_cast<ushort *>(printerName.utf16()));
881 LONG result = DocumentProperties(
nullptr, hPrinter, lpwPrinterName,
882 nullptr,
nullptr, 0);
883 devMode =
reinterpret_cast<DEVMODE *>(malloc(result));
884 initializeDevMode(devMode);
888 result = DocumentProperties(
nullptr, hPrinter, lpwPrinterName,
889 devMode,
nullptr, DM_OUT_BUFFER);
890 if (result != IDOK) {
891 qErrnoWarning(
"QWin32PrintEngine::initialize: Failed to obtain devMode");
898 hdc = CreateDC(
nullptr,
reinterpret_cast<LPCWSTR>(printerName.utf16()),
902 qErrnoWarning(
"QWin32PrintEngine::initialize: CreateDC failed");
913 num_copies = devMode->dmCopies;
914 devMode->dmCollate = DMCOLLATE_TRUE;
918#if defined QT_DEBUG_DRAW || defined QT_DEBUG_METRICS
919 qDebug(
"QWin32PrintEngine::initialize()");
1033void QWin32PrintEngine::setProperty(PrintEnginePropertyKey key,
const QVariant &value)
1035 Q_D(QWin32PrintEngine);
1043 case PPK_PaperSources:
1045 case PPK_SupportsMultipleCopies:
1047 case PPK_SupportedResolutions:
1051 case PPK_CustomBase:
1055 case PPK_PrinterProgram:
1057 case PPK_SelectionOption:
1061 case PPK_FontEmbedding:
1062 d->embed_fonts = value.toBool();
1065 case PPK_CollateCopies:
1069 d->devMode->dmCollate = value.toBool() ? DMCOLLATE_TRUE : DMCOLLATE_FALSE;
1070 d->devMode->dmFields |= DM_COLLATE;
1079 d->devMode->dmColor = (value.toInt() == QPrinter::Color) ? DMCOLOR_COLOR : DMCOLOR_MONOCHROME;
1080 d->devMode->dmFields |= DM_COLOR;
1086 d->m_creator = value.toString();
1089 case PPK_DocumentName:
1091 qWarning(
"QWin32PrintEngine: Cannot change document name while printing is active");
1094 d->docName = value.toString();
1100 QPrint::DuplexMode mode = QPrint::DuplexMode(value.toInt());
1101 if (mode == property(PPK_Duplex).toInt() || !d->m_printDevice.supportedDuplexModes().contains(mode))
1104 case QPrint::DuplexNone:
1105 d->devMode->dmDuplex = DMDUP_SIMPLEX;
1106 d->devMode->dmFields |= DM_DUPLEX;
1108 case QPrint::DuplexAuto:
1109 d->devMode->dmDuplex = d->m_pageLayout.orientation() == QPageLayout::Landscape ? DMDUP_HORIZONTAL : DMDUP_VERTICAL;
1110 d->devMode->dmFields |= DM_DUPLEX;
1112 case QPrint::DuplexLongSide:
1113 d->devMode->dmDuplex = DMDUP_VERTICAL;
1114 d->devMode->dmFields |= DM_DUPLEX;
1116 case QPrint::DuplexShortSide:
1117 d->devMode->dmDuplex = DMDUP_HORIZONTAL;
1118 d->devMode->dmFields |= DM_DUPLEX;
1130 d->m_pageLayout.setMode(QPageLayout::FullPageMode);
1132 d->m_pageLayout.setMode(QPageLayout::StandardMode);
1134#ifdef QT_DEBUG_METRICS
1135 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_FullPage," << value.toBool() << +
")";
1141 case PPK_NumberOfCopies:
1144 d->num_copies = value.toInt();
1145 d->devMode->dmCopies = d->num_copies;
1146 d->devMode->dmFields |= DM_COPIES;
1150 case PPK_Orientation: {
1153 QPageLayout::Orientation orientation = QPageLayout::Orientation(value.toInt());
1154 d->devMode->dmOrientation = orientation == QPageLayout::Landscape ? DMORIENT_LANDSCAPE : DMORIENT_PORTRAIT;
1155 d->devMode->dmFields |= DM_ORIENTATION;
1156 d->m_pageLayout.setOrientation(orientation);
1159#ifdef QT_DEBUG_METRICS
1160 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_Orientation," << orientation <<
')';
1166 case PPK_OutputFileName:
1168 qWarning(
"QWin32PrintEngine: Cannot change filename while printing");
1170 d->fileName = value.toString();
1171 d->printToFile = !value.toString().isEmpty();
1175 case PPK_PageSize: {
1178 const QPageSize pageSize = QPageSize(QPageSize::PageSizeId(value.toInt()));
1179 if (pageSize.isValid()) {
1180 d->setPageSize(pageSize);
1182#ifdef QT_DEBUG_METRICS
1183 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_PageSize," << value.toInt() <<
')';
1190 case PPK_PaperName: {
1194 const QPageSize pageSize = d->m_printDevice.supportedPageSize(value.toString());
1195 if (pageSize.isValid()) {
1196 d->setPageSize(pageSize);
1198#ifdef QT_DEBUG_METRICS
1199 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_PaperName," << value.toString() <<
')';
1206 case PPK_PaperSource: {
1209 const auto inputSlots = d->m_printDevice.supportedInputSlots();
1210 const int paperSource = value.toInt();
1211 const int index = paperSource >= DMBIN_USER ?
1212 indexOfWindowsId(inputSlots, paperSource) : indexOfId(inputSlots, QPrint::InputSlotId(paperSource));
1213 d->devMode->dmDefaultSource = index >= 0 ? inputSlots.at(index).windowsId : DMBIN_AUTO;
1218 case PPK_PrinterName: {
1219 QString id = value.toString();
1220 QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
1224 QVariant pageSize = QVariant::fromValue(d->m_pageLayout.pageSize());
1225 const bool isFullPage = (d->m_pageLayout.mode() == QPageLayout::FullPageMode);
1226 QVariant orientation = QVariant::fromValue(d->m_pageLayout.orientation());
1227 QVariant margins = QVariant::fromValue(
1228 std::pair<QMarginsF, QPageLayout::Unit>(d->m_pageLayout.margins(), d->m_pageLayout.units()));
1229 QPrintDevice printDevice = ps->createPrintDevice(id.isEmpty() ? ps->defaultPrintDeviceId() : id);
1230 if (printDevice.isValid()) {
1231 d->m_printDevice = printDevice;
1233 if (d->m_printDevice.supportedPageSize(pageSize.value<QPageSize>()).isValid())
1234 setProperty(PPK_QPageSize, pageSize);
1236 setProperty(PPK_CustomPaperSize, pageSize.value<QPageSize>().size(QPageSize::Point));
1237 setProperty(PPK_FullPage, QVariant(isFullPage));
1238 setProperty(PPK_Orientation, orientation);
1239 setProperty(PPK_QPageMargins, margins);
1244 case PPK_Resolution: {
1245 d->resolution = value.toInt();
1246 d->stretch_x = d->dpi_x /
double(d->resolution);
1247 d->stretch_y = d->dpi_y /
double(d->resolution);
1249#ifdef QT_DEBUG_METRICS
1250 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_Resolution," << value.toInt() <<
')';
1256 case PPK_WindowsPageSize: {
1259 const QPageSize pageSize = QPageSize(QPageSize::id(value.toInt()));
1260 if (pageSize.isValid()) {
1261 d->setPageSize(pageSize);
1263#ifdef QT_DEBUG_METRICS
1264 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_WindowsPageSize," << value.toInt() <<
')';
1272 case PPK_CustomPaperSize: {
1275 const QPageSize pageSize = QPageSize(value.toSizeF(), QPageSize::Point);
1276 if (pageSize.isValid()) {
1277 d->setPageSize(pageSize);
1279#ifdef QT_DEBUG_METRICS
1280 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_CustomPaperSize," << value.toSizeF() <<
')';
1287 case PPK_PageMargins: {
1288 QList<QVariant> margins(value.toList());
1289 Q_ASSERT(margins.size() == 4);
1290 d->m_pageLayout.setUnits(QPageLayout::Point);
1291 d->m_pageLayout.setMargins(QMarginsF(margins.at(0).toReal(), margins.at(1).toReal(),
1292 margins.at(2).toReal(), margins.at(3).toReal()),
1293 QPageLayout::OutOfBoundsPolicy::Clamp);
1295#ifdef QT_DEBUG_METRICS
1296 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_PageMargins," << margins <<
')';
1302 case PPK_QPageSize: {
1306 const QPageSize pageSize = value.value<QPageSize>();
1307 if (pageSize.isValid()) {
1308 d->setPageSize(pageSize);
1310#ifdef QT_DEBUG_METRICS
1311 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_QPageSize," << pageSize <<
')';
1318 case PPK_QPageMargins: {
1319 auto pair = value.value<std::pair<QMarginsF, QPageLayout::Unit>>();
1320 d->m_pageLayout.setUnits(pair.second);
1321 d->m_pageLayout.setMargins(pair.first, QPageLayout::OutOfBoundsPolicy::Clamp);
1323#ifdef QT_DEBUG_METRICS
1324 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_QPageMargins," << pair.first << pair.second <<
')';
1330 case PPK_QPageLayout: {
1331 QPageLayout pageLayout = value.value<QPageLayout>();
1332 if (pageLayout.isValid() && d->m_printDevice.isValidPageLayout(pageLayout, d->resolution)) {
1333 setProperty(PPK_QPageSize, QVariant::fromValue(pageLayout.pageSize()));
1334 setProperty(PPK_FullPage, pageLayout.mode() == QPageLayout::FullPageMode);
1335 setProperty(PPK_Orientation, QVariant::fromValue(pageLayout.orientation()));
1336 d->m_pageLayout.setUnits(pageLayout.units());
1337 d->m_pageLayout.setMargins(pageLayout.margins(), QPageLayout::OutOfBoundsPolicy::Clamp);
1339#ifdef QT_DEBUG_METRICS
1340 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_QPageLayout," << pageLayout <<
')';
1351QVariant QWin32PrintEngine::property(PrintEnginePropertyKey key)
const
1353 Q_D(
const QWin32PrintEngine);
1360 value = QPrinter::FirstPageFirst;
1362 case PPK_PrinterProgram:
1365 case PPK_SelectionOption:
1370 case PPK_FontEmbedding:
1371 value = d->embed_fonts;
1374 case PPK_CollateCopies:
1378 value = d->devMode->dmCollate == DMCOLLATE_TRUE;
1384 value = QPrinter::Color;
1386 value = (d->devMode->dmColor == DMCOLOR_COLOR) ? QPrinter::Color : QPrinter::GrayScale;
1392 value = d->m_creator;
1395 case PPK_DocumentName:
1401 value = QPrinter::DuplexNone;
1403 switch (d->devMode->dmDuplex) {
1404 case DMDUP_VERTICAL:
1405 value = QPrinter::DuplexLongSide;
1407 case DMDUP_HORIZONTAL:
1408 value = QPrinter::DuplexShortSide;
1412 value = QPrinter::DuplexNone;
1420 value = d->m_pageLayout.mode() == QPageLayout::FullPageMode;
1424 value = d->num_copies;
1427 case PPK_SupportsMultipleCopies:
1431 case PPK_NumberOfCopies:
1435 case PPK_Orientation:
1436 value = d->m_pageLayout.orientation();
1439 case PPK_OutputFileName:
1440 value = d->fileName;
1445 value = d->m_pageLayout.paintRectPixels(d->resolution);
1449 value = d->m_pageLayout.pageSize().id();
1454 value = d->m_pageLayout.fullRectPixels(d->resolution);
1458 value = d->m_pageLayout.pageSize().name();
1461 case PPK_PaperSource:
1463 value = d->m_printDevice.defaultInputSlot().id;
1465 if (d->devMode->dmDefaultSource >= DMBIN_USER) {
1466 value =
int(d->devMode->dmDefaultSource);
1468 const auto inputSlots = d->m_printDevice.supportedInputSlots();
1469 const int index = indexOfWindowsId(inputSlots, d->devMode->dmDefaultSource);
1470 value = index >= 0 ? inputSlots.at(index).id : QPrint::Auto;
1475 case PPK_PrinterName:
1476 value = d->m_printDevice.id();
1479 case PPK_Resolution:
1480 if (d->resolution || d->m_printDevice.isValid())
1481 value = d->resolution;
1484 case PPK_SupportedResolutions: {
1485 QList<QVariant> list;
1486 const auto resolutions = d->m_printDevice.supportedResolutions();
1487 list.reserve(resolutions.size());
1488 for (
int resolution : resolutions)
1494 case PPK_WindowsPageSize:
1495 value = d->m_pageLayout.pageSize().windowsId();
1498 case PPK_PaperSources: {
1499 QList<QVariant> out;
1500 const auto inputSlots = d->m_printDevice.supportedInputSlots();
1501 out.reserve(inputSlots.size());
1502 for (
const QPrint::InputSlot &inputSlot : inputSlots)
1503 out << QVariant(inputSlot.id == QPrint::CustomInputSlot ? inputSlot.windowsId :
int(inputSlot.id));
1508 case PPK_CustomPaperSize:
1509 value = d->m_pageLayout.fullRectPoints().size();
1512 case PPK_PageMargins: {
1513 QList<QVariant> list;
1514 QMarginsF margins = d->m_pageLayout.margins(QPageLayout::Point);
1515 list << margins.left() << margins.top() << margins.right() << margins.bottom();
1521 value.setValue(d->m_pageLayout.pageSize());
1524 case PPK_QPageMargins: {
1525 std::pair<QMarginsF, QPageLayout::Unit> pair(d->m_pageLayout.margins(), d->m_pageLayout.units());
1526 value.setValue(pair);
1530 case PPK_QPageLayout:
1531 value.setValue(d->m_pageLayout);
1534 case PPK_CustomBase:
1625 if (!pageSize.isValid())
1631 const QPageSize printerPageSize = m_printDevice.supportedPageSize(pageSize);
1632 const QPageSize usePageSize = printerPageSize.isValid() ? printerPageSize : pageSize;
1634 const QMarginsF printable = m_printDevice.printableMargins(usePageSize, m_pageLayout.orientation(), resolution);
1635 m_pageLayout.setPageSize(usePageSize, qt_convertMargins(printable, QPageLayout::Point, m_pageLayout.units()));
1638 if (printerPageSize.isValid()) {
1639 has_custom_paper_size =
false;
1640 devMode->dmPaperSize = m_pageLayout.pageSize().windowsId();
1641 devMode->dmFields &= ~(DM_PAPERLENGTH | DM_PAPERWIDTH);
1642 devMode->dmPaperWidth = 0;
1643 devMode->dmPaperLength = 0;
1645 devMode->dmPaperSize = DMPAPER_USER;
1646 devMode->dmFields |= DM_PAPERLENGTH | DM_PAPERWIDTH;
1648 const QSizeF sizeMM = m_pageLayout.pageSize().size(QPageSize::Millimeter);
1649 devMode->dmPaperWidth = qRound(sizeMM.width() * 10.0);
1650 devMode->dmPaperLength = qRound(sizeMM.height() * 10.0);
1661 m_pageLayout.setOrientation(devMode->dmOrientation == DMORIENT_LANDSCAPE ? QPageLayout::Landscape : QPageLayout::Portrait);
1662 if (devMode->dmPaperSize >= DMPAPER_LAST) {
1665 bool hasCustom =
false;
1666 int feature = PSIDENT_GDICENTRIC;
1667 if (ExtEscape(hdc, POSTSCRIPT_IDENTIFY,
1668 sizeof(DWORD),
reinterpret_cast<LPCSTR>(&feature), 0, 0) >= 0) {
1669 PSFEATURE_CUSTPAPER custPaper;
1670 feature = FEATURESETTING_CUSTPAPER;
1671 if (ExtEscape(hdc, GET_PS_FEATURESETTING,
sizeof(INT),
reinterpret_cast<LPCSTR>(&feature),
1672 sizeof(custPaper),
reinterpret_cast<LPSTR>(&custPaper)) > 0) {
1674 if (!(custPaper.lOrientation == 1 && custPaper.lWidth == 0 && custPaper.lHeight == 0)) {
1675 if (custPaper.lOrientation == 0 || custPaper.lOrientation == 2)
1676 m_pageLayout.setOrientation(QPageLayout::Portrait);
1678 m_pageLayout.setOrientation(QPageLayout::Landscape);
1679 QPageSize pageSize = QPageSize(QSizeF(custPaper.lWidth, custPaper.lHeight),
1681 setPageSize(pageSize);
1687 QPageSize pageSize = QPageSize(QSizeF(devMode->dmPaperWidth / 10.0f, devMode->dmPaperLength / 10.0f),
1688 QPageSize::Millimeter);
1689 setPageSize(pageSize);
1693 setPageSize(QPageSize(QPageSize::id(devMode->dmPaperSize)));
1701 m_paintRectPixels = m_pageLayout.paintRectPixels(resolution);
1703 const int devWidth = GetDeviceCaps(hdc, PHYSICALWIDTH);
1704 const int devHeight = GetDeviceCaps(hdc, PHYSICALHEIGHT);
1705 const int pageWidth = m_pageLayout.fullRectPixels(dpi_x).width();
1706 const int pageHeight = m_pageLayout.fullRectPixels(dpi_y).height();
1707 const qreal pageScaleX = (devWidth && pageWidth) ? qreal(devWidth) / pageWidth : 1;
1708 const qreal pageScaleY = (devHeight && pageHeight) ? qreal(devHeight) / pageHeight : 1;
1709 m_paintRectPixels = QTransform::fromScale(pageScaleX, pageScaleY).mapRect(m_paintRectPixels);
1711 QSizeF sizeMM = m_pageLayout.paintRect(QPageLayout::Millimeter).size();
1712 m_paintSizeMM = QSize(qRound(sizeMM.width()), qRound(sizeMM.height()));
1715 const bool isFullPage = (m_pageLayout.mode() == QPageLayout::FullPageMode);
1716 const QMarginsF margins = isFullPage ? QMarginsF() : (m_pageLayout.margins(QPageLayout::Millimeter) / 25.4);
1717 origin_x = qRound(pageScaleX * margins.left() *
dpi_x) - GetDeviceCaps(hdc, PHYSICALOFFSETX);
1718 origin_y = qRound(pageScaleY * margins.top() *
dpi_y) - GetDeviceCaps(hdc, PHYSICALOFFSETY);
1734 const QTransform &xform,
const QPointF &topLeft)
1736 QPointF baseline_pos = xform.inverted().map(xform.map(pos) - topLeft);
1738 SetTextAlign(hdc, TA_BASELINE);
1739 SetBkMode(hdc, TRANSPARENT);
1741 const bool has_kerning = ti.f && ti.f->kerning();
1743 HFONT hfont =
nullptr;
1744 bool deleteFont =
false;
1746 if (ti.fontEngine->type() == QFontEngine::Win) {
1747 if (ti.fontEngine->supportsTransformation(QTransform::fromScale(0.5, 0.5)))
1748 hfont =
static_cast<HFONT>(ti.fontEngine->handle());
1750#if QT_CONFIG(directwrite)
1751 else if (ti.fontEngine->type() == QFontEngine::DirectWrite) {
1752 QWindowsFontEngineDirectWrite *fedw =
static_cast<QWindowsFontEngineDirectWrite *>(ti.fontEngine);
1753 hfont = fedw->createHFONT();
1760 hfont = (HFONT)GetStockObject(ANSI_VAR_FONT);
1762 HGDIOBJ old_font = SelectObject(hdc, hfont);
1763 unsigned int options = ETO_GLYPH_INDEX;
1764 QGlyphLayout glyphs = ti.glyphs;
1766 bool fast = !has_kerning && !(ti.flags & QTextItem::RightToLeft);
1767 for (
int i = 0; fast && i < glyphs.numGlyphs; i++) {
1768 if (glyphs.offsets[i].x != 0 || glyphs.offsets[i].y != 0 || glyphs.justifications[i].space_18d6 != 0
1769 || glyphs.attributes[i].dontPrint) {
1777 win_xform.eM11 = xform.m11();
1778 win_xform.eM12 = xform.m12();
1779 win_xform.eM21 = xform.m21();
1780 win_xform.eM22 = xform.m22();
1781 win_xform.eDx = xform.dx();
1782 win_xform.eDy = xform.dy();
1784 SetGraphicsMode(hdc, GM_ADVANCED);
1785 SetWorldTransform(hdc, &win_xform);
1789 QVarLengthArray<
wchar_t> g(glyphs.numGlyphs);
1790 for (
int i = 0; i < glyphs.numGlyphs; ++i)
1791 g[i] = glyphs.glyphs[i];
1793 qRound(baseline_pos.x() + glyphs.offsets[0].x.toReal()),
1794 qRound(baseline_pos.y() + glyphs.offsets[0].y.toReal()),
1795 options, 0, g.constData(), glyphs.numGlyphs, 0);
1797 QVarLengthArray<QFixedPoint> positions;
1798 QVarLengthArray<glyph_t> _glyphs;
1800 QTransform matrix = QTransform::fromTranslate(baseline_pos.x(), baseline_pos.y());
1801 ti.fontEngine->getGlyphPositions(ti.glyphs, matrix, ti.flags,
1802 _glyphs, positions);
1803 if (_glyphs.isEmpty()) {
1804 SelectObject(hdc, old_font);
1809 QVarLengthArray<INT> glyphDistances(_glyphs.size() * 2);
1810 QVarLengthArray<
wchar_t> g(_glyphs.size());
1811 const int lastGlyph = _glyphs.size() - 1;
1812 for (
int i = 0; i < lastGlyph; ++i) {
1813 glyphDistances[i * 2] = qRound(positions[i + 1].x) - qRound(positions[i].x);
1814 glyphDistances[i * 2 + 1] = qRound(positions[i + 1].y) - qRound(positions[i].y);
1817 glyphDistances[lastGlyph * 2] = 0;
1818 glyphDistances[lastGlyph * 2 + 1] = 0;
1819 g[lastGlyph] = _glyphs[lastGlyph];
1820 ExtTextOut(hdc, qRound(positions[0].x), qRound(positions[0].y), options,
nullptr,
1821 g.constData(), _glyphs.size(),
1822 glyphDistances.data());
1825 win_xform.eM11 = win_xform.eM22 = 1.0;
1826 win_xform.eM12 = win_xform.eM21 = win_xform.eDx = win_xform.eDy = 0.0;
1827 SetWorldTransform(hdc, &win_xform);
1829 SelectObject(hdc, old_font);
1832 DeleteObject(hfont);
QMarginsF qt_convertMargins(const QMarginsF &margins, QPageLayout::Unit fromUnits, QPageLayout::Unit toUnits)