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);
488void QWin32PrintEngine::drawPixmap(
const QRectF &targetRect,
489 const QPixmap &originalPixmap,
490 const QRectF &sourceRect)
492 Q_D(QWin32PrintEngine);
494 QAlphaPaintEngine::drawPixmap(targetRect, originalPixmap, sourceRect);
498 const int tileSize = 2048;
500 QRectF r = targetRect;
501 QRectF sr = sourceRect;
503 QPixmap pixmap = originalPixmap;
504 if (sr.size() != pixmap.size()) {
505 pixmap = pixmap.copy(sr.toRect());
511 QTransform scaleMatrix = QTransform::fromScale(r.width() / pixmap.width(), r.height() / pixmap.height());
512 QTransform adapted = QPixmap::trueMatrix(d->painterMatrix * scaleMatrix,
513 pixmap.width(), pixmap.height());
515 qreal xform_offset_x = adapted.dx();
516 qreal xform_offset_y = adapted.dy();
518 if (d->complex_xform) {
519 pixmap = pixmap.transformed(adapted);
520 scaleX = d->stretch_x;
521 scaleY = d->stretch_y;
523 scaleX = d->stretch_x * (r.width() / pixmap.width()) * d->painterMatrix.m11();
524 scaleY = d->stretch_y * (r.height() / pixmap.height()) * d->painterMatrix.m22();
527 QPointF topLeft = r.topLeft() * d->painterMatrix;
528 int tx =
int(topLeft.x() * d->stretch_x + d->origin_x);
529 int ty =
int(topLeft.y() * d->stretch_y + d->origin_y);
530 int tw = qAbs(
int(pixmap.width() * scaleX));
531 int th = qAbs(
int(pixmap.height() * scaleY));
533 xform_offset_x *= d->stretch_x;
534 xform_offset_y *= d->stretch_y;
536 int dc_state = SaveDC(d->hdc);
538 int tilesw = pixmap.width() / tileSize;
539 int tilesh = pixmap.height() / tileSize;
543 int txinc = tileSize*scaleX;
544 int tyinc = tileSize*scaleY;
546 for (
int y = 0; y < tilesh; ++y) {
547 int tposy = ty + (y * tyinc);
550 if (y == (tilesh - 1)) {
551 imgh = pixmap.height() - (y * tileSize);
552 height = (th - (y * tyinc));
554 for (
int x = 0; x < tilesw; ++x) {
555 int tposx = tx + (x * txinc);
558 if (x == (tilesw - 1)) {
559 imgw = pixmap.width() - (x * tileSize);
560 width = (tw - (x * txinc));
564 QImage img(QSize(imgw, imgh), QImage::Format_RGB32);
565 img.setDevicePixelRatio(pixmap.devicePixelRatio());
567 QPainter painter(&img);
568 painter.drawPixmap(0,0, pixmap, tileSize * x, tileSize * y, imgw, imgh);
569 QPixmap p = QPixmap::fromImage(img);
571 HBITMAP hbitmap = qt_pixmapToWinHBITMAP(p, HBitmapNoAlpha);
572 HDC hbitmap_hdc = CreateCompatibleDC(d->hdc);
573 HGDIOBJ null_bitmap = SelectObject(hbitmap_hdc, hbitmap);
575 if (!StretchBlt(d->hdc, qRound(tposx - xform_offset_x), qRound(tposy - xform_offset_y), width, height,
576 hbitmap_hdc, 0, 0, p.width(), p.height(), SRCCOPY))
577 qErrnoWarning(
"QWin32PrintEngine::drawPixmap, StretchBlt failed");
579 SelectObject(hbitmap_hdc, null_bitmap);
580 DeleteObject(hbitmap);
581 DeleteDC(hbitmap_hdc);
585 RestoreDC(d->hdc, dc_state);
589void QWin32PrintEngine::drawTiledPixmap(
const QRectF &r,
const QPixmap &pm,
const QPointF &pos)
591 Q_D(QWin32PrintEngine);
593 QAlphaPaintEngine::drawTiledPixmap(r, pm, pos);
597 if (d->complex_xform || !pos.isNull()) {
598 QPaintEngine::drawTiledPixmap(r, pm, pos);
600 int dc_state = SaveDC(d->hdc);
602 HBITMAP hbitmap = qt_pixmapToWinHBITMAP(pm, HBitmapNoAlpha);
603 HDC hbitmap_hdc = CreateCompatibleDC(d->hdc);
604 HGDIOBJ null_bitmap = SelectObject(hbitmap_hdc, hbitmap);
606 QRectF trect = d->painterMatrix.mapRect(r);
607 int tx =
int(trect.left() * d->stretch_x + d->origin_x);
608 int ty =
int(trect.top() * d->stretch_y + d->origin_y);
610 int xtiles =
int(trect.width() / pm.width()) + 1;
611 int ytiles =
int(trect.height() / pm.height()) + 1;
612 int xinc =
int(pm.width() * d->stretch_x);
613 int yinc =
int(pm.height() * d->stretch_y);
615 for (
int y = 0; y < ytiles; ++y) {
616 int ity = ty + (yinc * y);
617 int ith = pm.height();
618 if (y == (ytiles - 1)) {
619 ith =
int(trect.height() - (pm.height() * y));
622 for (
int x = 0; x < xtiles; ++x) {
623 int itx = tx + (xinc * x);
624 int itw = pm.width();
625 if (x == (xtiles - 1)) {
626 itw =
int(trect.width() - (pm.width() * x));
629 if (!StretchBlt(d->hdc, itx, ity,
int(itw * d->stretch_x),
int(ith * d->stretch_y),
630 hbitmap_hdc, 0, 0, itw, ith, SRCCOPY))
631 qErrnoWarning(
"QWin32PrintEngine::drawPixmap, StretchBlt failed");
636 SelectObject(hbitmap_hdc, null_bitmap);
637 DeleteObject(hbitmap);
638 DeleteDC(hbitmap_hdc);
640 RestoreDC(d->hdc, dc_state);
648 qErrnoWarning(
"QWin32PrintEnginePrivate::drawPath: BeginPath failed");
652 for (
int i=0; i<path.elementCount(); ++i) {
653 const QPainterPath::Element &elm = path.elementAt(i);
655 case QPainterPath::MoveToElement:
657 && path.elementAt(start).x == path.elementAt(i-1).x
658 && path.elementAt(start).y == path.elementAt(i-1).y)
661 MoveToEx(hdc, qRound(elm.x), qRound(elm.y), 0);
663 case QPainterPath::LineToElement:
664 LineTo(hdc, qRound(elm.x), qRound(elm.y));
666 case QPainterPath::CurveToElement: {
668 { qRound(elm.x), qRound(elm.y) },
669 { qRound(path.elementAt(i+1).x), qRound(path.elementAt(i+1).y) },
670 { qRound(path.elementAt(i+2).x), qRound(path.elementAt(i+2).y) }
673 PolyBezierTo(hdc, pts, 3);
677 qFatal(
"QWin32PaintEngine::drawPath: Unhandled type: %d", elm.type);
682 && path.elementAt(start).x == path.elementAt(path.elementCount()-1).x
683 && path.elementAt(start).y == path.elementAt(path.elementCount()-1).y)
687 qErrnoWarning(
"QWin32PaintEngine::drawPath: EndPath failed");
689 SetPolyFillMode(hdc, path.fillRule() == Qt::WindingFill ? WINDING : ALTERNATE);
709 composeGdiPath(path);
711 brush.lbStyle = BS_SOLID;
712 brush.lbColor = RGB(color.red(), color.green(), color.blue());
713 DWORD capStyle = PS_ENDCAP_SQUARE;
714 DWORD joinStyle = PS_JOIN_BEVEL;
715 if (pen.capStyle() == Qt::FlatCap)
716 capStyle = PS_ENDCAP_FLAT;
717 else if (pen.capStyle() == Qt::RoundCap)
718 capStyle = PS_ENDCAP_ROUND;
720 if (pen.joinStyle() == Qt::MiterJoin)
721 joinStyle = PS_JOIN_MITER;
722 else if (pen.joinStyle() == Qt::RoundJoin)
723 joinStyle = PS_JOIN_ROUND;
725 HPEN pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | capStyle | joinStyle,
726 (penWidth == 0) ? 1 : penWidth, &brush, 0,
nullptr);
728 HGDIOBJ old_pen = SelectObject(hdc, pen);
730 DeleteObject(SelectObject(hdc, old_pen));
741 QPainterPathStroker stroker;
742 if (pen.style() == Qt::CustomDashLine) {
743 stroker.setDashPattern(pen.dashPattern());
744 stroker.setDashOffset(pen.dashOffset());
746 stroker.setDashPattern(pen.style());
748 stroker.setCapStyle(pen.capStyle());
749 stroker.setJoinStyle(pen.joinStyle());
750 stroker.setMiterLimit(pen.miterLimit());
753 qreal width = pen.widthF();
754 bool cosmetic = pen.isCosmetic();
755 if (pen.style() == Qt::SolidLine && (cosmetic || matrix.type() < QTransform::TxScale)) {
756 strokePath_dev(path * matrix, color, width);
758 stroker.setWidth(width);
760 stroke = stroker.createStroke(path * matrix);
762 stroke = stroker.createStroke(path) * painterMatrix;
763 QTransform stretch(stretch_x, 0, 0, stretch_y, origin_x, origin_y);
764 stroke = stroke * stretch;
767 if (stroke.isEmpty())
770 fillPath_dev(stroke, color);
840 if (!m_printDevice.isValid())
843 txop = QTransform::TxNone;
845 QString printerName = m_printDevice.id();
846 bool ok = OpenPrinter(
reinterpret_cast<LPWSTR>(
const_cast<ushort *>(printerName.utf16())),
847 reinterpret_cast<LPHANDLE>(&hPrinter),
nullptr);
849 qErrnoWarning(
"QWin32PrintEngine::initialize: OpenPrinter failed");
855 DWORD infoSize, numBytes;
856 GetPrinter(hPrinter, 2,
nullptr, 0, &infoSize);
857 hMem = GlobalAlloc(GHND, infoSize);
858 pInfo =
reinterpret_cast<PRINTER_INFO_2*>(GlobalLock(hMem));
859 ok = GetPrinter(hPrinter, 2,
reinterpret_cast<LPBYTE>(pInfo), infoSize, &numBytes);
862 qErrnoWarning(
"QWin32PrintEngine::initialize: GetPrinter failed");
867 devMode = pInfo->pDevMode;
877 auto *lpwPrinterName =
reinterpret_cast<LPWSTR>(
const_cast<ushort *>(printerName.utf16()));
878 LONG result = DocumentProperties(
nullptr, hPrinter, lpwPrinterName,
879 nullptr,
nullptr, 0);
880 devMode =
reinterpret_cast<DEVMODE *>(malloc(result));
881 initializeDevMode(devMode);
885 result = DocumentProperties(
nullptr, hPrinter, lpwPrinterName,
886 devMode,
nullptr, DM_OUT_BUFFER);
887 if (result != IDOK) {
888 qErrnoWarning(
"QWin32PrintEngine::initialize: Failed to obtain devMode");
895 hdc = CreateDC(
nullptr,
reinterpret_cast<LPCWSTR>(printerName.utf16()),
899 qErrnoWarning(
"QWin32PrintEngine::initialize: CreateDC failed");
910 num_copies = devMode->dmCopies;
911 devMode->dmCollate = DMCOLLATE_TRUE;
915#if defined QT_DEBUG_DRAW || defined QT_DEBUG_METRICS
916 qDebug(
"QWin32PrintEngine::initialize()");
1030void QWin32PrintEngine::setProperty(PrintEnginePropertyKey key,
const QVariant &value)
1032 Q_D(QWin32PrintEngine);
1040 case PPK_PaperSources:
1042 case PPK_SupportsMultipleCopies:
1044 case PPK_SupportedResolutions:
1048 case PPK_CustomBase:
1052 case PPK_PrinterProgram:
1054 case PPK_SelectionOption:
1058 case PPK_FontEmbedding:
1059 d->embed_fonts = value.toBool();
1062 case PPK_CollateCopies:
1066 d->devMode->dmCollate = value.toBool() ? DMCOLLATE_TRUE : DMCOLLATE_FALSE;
1067 d->devMode->dmFields |= DM_COLLATE;
1076 d->devMode->dmColor = (value.toInt() == QPrinter::Color) ? DMCOLOR_COLOR : DMCOLOR_MONOCHROME;
1077 d->devMode->dmFields |= DM_COLOR;
1083 d->m_creator = value.toString();
1086 case PPK_DocumentName:
1088 qWarning(
"QWin32PrintEngine: Cannot change document name while printing is active");
1091 d->docName = value.toString();
1097 QPrint::DuplexMode mode = QPrint::DuplexMode(value.toInt());
1098 if (mode == property(PPK_Duplex).toInt() || !d->m_printDevice.supportedDuplexModes().contains(mode))
1101 case QPrint::DuplexNone:
1102 d->devMode->dmDuplex = DMDUP_SIMPLEX;
1103 d->devMode->dmFields |= DM_DUPLEX;
1105 case QPrint::DuplexAuto:
1106 d->devMode->dmDuplex = d->m_pageLayout.orientation() == QPageLayout::Landscape ? DMDUP_HORIZONTAL : DMDUP_VERTICAL;
1107 d->devMode->dmFields |= DM_DUPLEX;
1109 case QPrint::DuplexLongSide:
1110 d->devMode->dmDuplex = DMDUP_VERTICAL;
1111 d->devMode->dmFields |= DM_DUPLEX;
1113 case QPrint::DuplexShortSide:
1114 d->devMode->dmDuplex = DMDUP_HORIZONTAL;
1115 d->devMode->dmFields |= DM_DUPLEX;
1127 d->m_pageLayout.setMode(QPageLayout::FullPageMode);
1129 d->m_pageLayout.setMode(QPageLayout::StandardMode);
1131#ifdef QT_DEBUG_METRICS
1132 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_FullPage," << value.toBool() << +
")";
1138 case PPK_NumberOfCopies:
1141 d->num_copies = value.toInt();
1142 d->devMode->dmCopies = d->num_copies;
1143 d->devMode->dmFields |= DM_COPIES;
1147 case PPK_Orientation: {
1150 QPageLayout::Orientation orientation = QPageLayout::Orientation(value.toInt());
1151 d->devMode->dmOrientation = orientation == QPageLayout::Landscape ? DMORIENT_LANDSCAPE : DMORIENT_PORTRAIT;
1152 d->devMode->dmFields |= DM_ORIENTATION;
1153 d->m_pageLayout.setOrientation(orientation);
1156#ifdef QT_DEBUG_METRICS
1157 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_Orientation," << orientation <<
')';
1163 case PPK_OutputFileName:
1165 qWarning(
"QWin32PrintEngine: Cannot change filename while printing");
1167 d->fileName = value.toString();
1168 d->printToFile = !value.toString().isEmpty();
1172 case PPK_PageSize: {
1175 const QPageSize pageSize = QPageSize(QPageSize::PageSizeId(value.toInt()));
1176 if (pageSize.isValid()) {
1177 d->setPageSize(pageSize);
1179#ifdef QT_DEBUG_METRICS
1180 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_PageSize," << value.toInt() <<
')';
1187 case PPK_PaperName: {
1191 const QPageSize pageSize = d->m_printDevice.supportedPageSize(value.toString());
1192 if (pageSize.isValid()) {
1193 d->setPageSize(pageSize);
1195#ifdef QT_DEBUG_METRICS
1196 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_PaperName," << value.toString() <<
')';
1203 case PPK_PaperSource: {
1206 const auto inputSlots = d->m_printDevice.supportedInputSlots();
1207 const int paperSource = value.toInt();
1208 const int index = paperSource >= DMBIN_USER ?
1209 indexOfWindowsId(inputSlots, paperSource) : indexOfId(inputSlots, QPrint::InputSlotId(paperSource));
1210 d->devMode->dmDefaultSource = index >= 0 ? inputSlots.at(index).windowsId : DMBIN_AUTO;
1215 case PPK_PrinterName: {
1216 QString id = value.toString();
1217 QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
1221 QVariant pageSize = QVariant::fromValue(d->m_pageLayout.pageSize());
1222 const bool isFullPage = (d->m_pageLayout.mode() == QPageLayout::FullPageMode);
1223 QVariant orientation = QVariant::fromValue(d->m_pageLayout.orientation());
1224 QVariant margins = QVariant::fromValue(
1225 std::pair<QMarginsF, QPageLayout::Unit>(d->m_pageLayout.margins(), d->m_pageLayout.units()));
1226 QPrintDevice printDevice = ps->createPrintDevice(id.isEmpty() ? ps->defaultPrintDeviceId() : id);
1227 if (printDevice.isValid()) {
1228 d->m_printDevice = printDevice;
1230 if (d->m_printDevice.supportedPageSize(pageSize.value<QPageSize>()).isValid())
1231 setProperty(PPK_QPageSize, pageSize);
1233 setProperty(PPK_CustomPaperSize, pageSize.value<QPageSize>().size(QPageSize::Point));
1234 setProperty(PPK_FullPage, QVariant(isFullPage));
1235 setProperty(PPK_Orientation, orientation);
1236 setProperty(PPK_QPageMargins, margins);
1241 case PPK_Resolution: {
1242 d->resolution = value.toInt();
1243 d->stretch_x = d->dpi_x /
double(d->resolution);
1244 d->stretch_y = d->dpi_y /
double(d->resolution);
1246#ifdef QT_DEBUG_METRICS
1247 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_Resolution," << value.toInt() <<
')';
1253 case PPK_WindowsPageSize: {
1256 const QPageSize pageSize = QPageSize(QPageSize::id(value.toInt()));
1257 if (pageSize.isValid()) {
1258 d->setPageSize(pageSize);
1260#ifdef QT_DEBUG_METRICS
1261 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_WindowsPageSize," << value.toInt() <<
')';
1269 case PPK_CustomPaperSize: {
1272 const QPageSize pageSize = QPageSize(value.toSizeF(), QPageSize::Point);
1273 if (pageSize.isValid()) {
1274 d->setPageSize(pageSize);
1276#ifdef QT_DEBUG_METRICS
1277 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_CustomPaperSize," << value.toSizeF() <<
')';
1284 case PPK_PageMargins: {
1285 QList<QVariant> margins(value.toList());
1286 Q_ASSERT(margins.size() == 4);
1287 d->m_pageLayout.setUnits(QPageLayout::Point);
1288 d->m_pageLayout.setMargins(QMarginsF(margins.at(0).toReal(), margins.at(1).toReal(),
1289 margins.at(2).toReal(), margins.at(3).toReal()),
1290 QPageLayout::OutOfBoundsPolicy::Clamp);
1292#ifdef QT_DEBUG_METRICS
1293 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_PageMargins," << margins <<
')';
1299 case PPK_QPageSize: {
1303 const QPageSize pageSize = value.value<QPageSize>();
1304 if (pageSize.isValid()) {
1305 d->setPageSize(pageSize);
1307#ifdef QT_DEBUG_METRICS
1308 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_QPageSize," << pageSize <<
')';
1315 case PPK_QPageMargins: {
1316 auto pair = value.value<std::pair<QMarginsF, QPageLayout::Unit>>();
1317 d->m_pageLayout.setUnits(pair.second);
1318 d->m_pageLayout.setMargins(pair.first, QPageLayout::OutOfBoundsPolicy::Clamp);
1320#ifdef QT_DEBUG_METRICS
1321 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_QPageMargins," << pair.first << pair.second <<
')';
1327 case PPK_QPageLayout: {
1328 QPageLayout pageLayout = value.value<QPageLayout>();
1329 if (pageLayout.isValid() && d->m_printDevice.isValidPageLayout(pageLayout, d->resolution)) {
1330 setProperty(PPK_QPageSize, QVariant::fromValue(pageLayout.pageSize()));
1331 setProperty(PPK_FullPage, pageLayout.mode() == QPageLayout::FullPageMode);
1332 setProperty(PPK_Orientation, QVariant::fromValue(pageLayout.orientation()));
1333 d->m_pageLayout.setUnits(pageLayout.units());
1334 d->m_pageLayout.setMargins(pageLayout.margins(), QPageLayout::OutOfBoundsPolicy::Clamp);
1336#ifdef QT_DEBUG_METRICS
1337 qDebug() <<
"QWin32PrintEngine::setProperty(PPK_QPageLayout," << pageLayout <<
')';
1348QVariant QWin32PrintEngine::property(PrintEnginePropertyKey key)
const
1350 Q_D(
const QWin32PrintEngine);
1357 value = QPrinter::FirstPageFirst;
1359 case PPK_PrinterProgram:
1362 case PPK_SelectionOption:
1367 case PPK_FontEmbedding:
1368 value = d->embed_fonts;
1371 case PPK_CollateCopies:
1375 value = d->devMode->dmCollate == DMCOLLATE_TRUE;
1381 value = QPrinter::Color;
1383 value = (d->devMode->dmColor == DMCOLOR_COLOR) ? QPrinter::Color : QPrinter::GrayScale;
1389 value = d->m_creator;
1392 case PPK_DocumentName:
1398 value = QPrinter::DuplexNone;
1400 switch (d->devMode->dmDuplex) {
1401 case DMDUP_VERTICAL:
1402 value = QPrinter::DuplexLongSide;
1404 case DMDUP_HORIZONTAL:
1405 value = QPrinter::DuplexShortSide;
1409 value = QPrinter::DuplexNone;
1417 value = d->m_pageLayout.mode() == QPageLayout::FullPageMode;
1421 value = d->num_copies;
1424 case PPK_SupportsMultipleCopies:
1428 case PPK_NumberOfCopies:
1432 case PPK_Orientation:
1433 value = d->m_pageLayout.orientation();
1436 case PPK_OutputFileName:
1437 value = d->fileName;
1442 value = d->m_pageLayout.paintRectPixels(d->resolution);
1446 value = d->m_pageLayout.pageSize().id();
1451 value = d->m_pageLayout.fullRectPixels(d->resolution);
1455 value = d->m_pageLayout.pageSize().name();
1458 case PPK_PaperSource:
1460 value = d->m_printDevice.defaultInputSlot().id;
1462 if (d->devMode->dmDefaultSource >= DMBIN_USER) {
1463 value =
int(d->devMode->dmDefaultSource);
1465 const auto inputSlots = d->m_printDevice.supportedInputSlots();
1466 const int index = indexOfWindowsId(inputSlots, d->devMode->dmDefaultSource);
1467 value = index >= 0 ? inputSlots.at(index).id : QPrint::Auto;
1472 case PPK_PrinterName:
1473 value = d->m_printDevice.id();
1476 case PPK_Resolution:
1477 if (d->resolution || d->m_printDevice.isValid())
1478 value = d->resolution;
1481 case PPK_SupportedResolutions: {
1482 QList<QVariant> list;
1483 const auto resolutions = d->m_printDevice.supportedResolutions();
1484 list.reserve(resolutions.size());
1485 for (
int resolution : resolutions)
1491 case PPK_WindowsPageSize:
1492 value = d->m_pageLayout.pageSize().windowsId();
1495 case PPK_PaperSources: {
1496 QList<QVariant> out;
1497 const auto inputSlots = d->m_printDevice.supportedInputSlots();
1498 out.reserve(inputSlots.size());
1499 for (
const QPrint::InputSlot &inputSlot : inputSlots)
1500 out << QVariant(inputSlot.id == QPrint::CustomInputSlot ? inputSlot.windowsId :
int(inputSlot.id));
1505 case PPK_CustomPaperSize:
1506 value = d->m_pageLayout.fullRectPoints().size();
1509 case PPK_PageMargins: {
1510 QList<QVariant> list;
1511 QMarginsF margins = d->m_pageLayout.margins(QPageLayout::Point);
1512 list << margins.left() << margins.top() << margins.right() << margins.bottom();
1518 value.setValue(d->m_pageLayout.pageSize());
1521 case PPK_QPageMargins: {
1522 std::pair<QMarginsF, QPageLayout::Unit> pair(d->m_pageLayout.margins(), d->m_pageLayout.units());
1523 value.setValue(pair);
1527 case PPK_QPageLayout:
1528 value.setValue(d->m_pageLayout);
1531 case PPK_CustomBase:
1622 if (!pageSize.isValid())
1628 const QPageSize printerPageSize = m_printDevice.supportedPageSize(pageSize);
1629 const QPageSize usePageSize = printerPageSize.isValid() ? printerPageSize : pageSize;
1631 const QMarginsF printable = m_printDevice.printableMargins(usePageSize, m_pageLayout.orientation(), resolution);
1632 m_pageLayout.setPageSize(usePageSize, qt_convertMargins(printable, QPageLayout::Point, m_pageLayout.units()));
1635 if (printerPageSize.isValid()) {
1636 has_custom_paper_size =
false;
1637 devMode->dmPaperSize = m_pageLayout.pageSize().windowsId();
1638 devMode->dmFields &= ~(DM_PAPERLENGTH | DM_PAPERWIDTH);
1639 devMode->dmPaperWidth = 0;
1640 devMode->dmPaperLength = 0;
1642 devMode->dmPaperSize = DMPAPER_USER;
1643 devMode->dmFields |= DM_PAPERLENGTH | DM_PAPERWIDTH;
1645 const QSizeF sizeMM = m_pageLayout.pageSize().size(QPageSize::Millimeter);
1646 devMode->dmPaperWidth = qRound(sizeMM.width() * 10.0);
1647 devMode->dmPaperLength = qRound(sizeMM.height() * 10.0);
1658 m_pageLayout.setOrientation(devMode->dmOrientation == DMORIENT_LANDSCAPE ? QPageLayout::Landscape : QPageLayout::Portrait);
1659 if (devMode->dmPaperSize >= DMPAPER_LAST) {
1662 bool hasCustom =
false;
1663 int feature = PSIDENT_GDICENTRIC;
1664 if (ExtEscape(hdc, POSTSCRIPT_IDENTIFY,
1665 sizeof(DWORD),
reinterpret_cast<LPCSTR>(&feature), 0, 0) >= 0) {
1666 PSFEATURE_CUSTPAPER custPaper;
1667 feature = FEATURESETTING_CUSTPAPER;
1668 if (ExtEscape(hdc, GET_PS_FEATURESETTING,
sizeof(INT),
reinterpret_cast<LPCSTR>(&feature),
1669 sizeof(custPaper),
reinterpret_cast<LPSTR>(&custPaper)) > 0) {
1671 if (!(custPaper.lOrientation == 1 && custPaper.lWidth == 0 && custPaper.lHeight == 0)) {
1672 if (custPaper.lOrientation == 0 || custPaper.lOrientation == 2)
1673 m_pageLayout.setOrientation(QPageLayout::Portrait);
1675 m_pageLayout.setOrientation(QPageLayout::Landscape);
1676 QPageSize pageSize = QPageSize(QSizeF(custPaper.lWidth, custPaper.lHeight),
1678 setPageSize(pageSize);
1684 QPageSize pageSize = QPageSize(QSizeF(devMode->dmPaperWidth / 10.0f, devMode->dmPaperLength / 10.0f),
1685 QPageSize::Millimeter);
1686 setPageSize(pageSize);
1690 setPageSize(QPageSize(QPageSize::id(devMode->dmPaperSize)));
1698 m_paintRectPixels = m_pageLayout.paintRectPixels(resolution);
1700 const int devWidth = GetDeviceCaps(hdc, PHYSICALWIDTH);
1701 const int devHeight = GetDeviceCaps(hdc, PHYSICALHEIGHT);
1702 const int pageWidth = m_pageLayout.fullRectPixels(dpi_x).width();
1703 const int pageHeight = m_pageLayout.fullRectPixels(dpi_y).height();
1704 const qreal pageScaleX = (devWidth && pageWidth) ? qreal(devWidth) / pageWidth : 1;
1705 const qreal pageScaleY = (devHeight && pageHeight) ? qreal(devHeight) / pageHeight : 1;
1706 m_paintRectPixels = QTransform::fromScale(pageScaleX, pageScaleY).mapRect(m_paintRectPixels);
1708 QSizeF sizeMM = m_pageLayout.paintRect(QPageLayout::Millimeter).size();
1709 m_paintSizeMM = QSize(qRound(sizeMM.width()), qRound(sizeMM.height()));
1712 const bool isFullPage = (m_pageLayout.mode() == QPageLayout::FullPageMode);
1713 const QMarginsF margins = isFullPage ? QMarginsF() : (m_pageLayout.margins(QPageLayout::Millimeter) / 25.4);
1714 origin_x = qRound(pageScaleX * margins.left() * dpi_x) - GetDeviceCaps(hdc, PHYSICALOFFSETX);
1715 origin_y = qRound(pageScaleY * margins.top() * dpi_y) - GetDeviceCaps(hdc, PHYSICALOFFSETY);
1731 const QTransform &xform,
const QPointF &topLeft)
1733 QPointF baseline_pos = xform.inverted().map(xform.map(pos) - topLeft);
1735 SetTextAlign(hdc, TA_BASELINE);
1736 SetBkMode(hdc, TRANSPARENT);
1738 const bool has_kerning = ti.f && ti.f->kerning();
1740 HFONT hfont =
nullptr;
1741 bool deleteFont =
false;
1743 if (ti.fontEngine->type() == QFontEngine::Win) {
1744 if (ti.fontEngine->supportsTransformation(QTransform::fromScale(0.5, 0.5)))
1745 hfont =
static_cast<HFONT>(ti.fontEngine->handle());
1747#if QT_CONFIG(directwrite)
1748 else if (ti.fontEngine->type() == QFontEngine::DirectWrite) {
1749 QWindowsFontEngineDirectWrite *fedw =
static_cast<QWindowsFontEngineDirectWrite *>(ti.fontEngine);
1750 hfont = fedw->createHFONT();
1757 hfont = (HFONT)GetStockObject(ANSI_VAR_FONT);
1759 HGDIOBJ old_font = SelectObject(hdc, hfont);
1760 unsigned int options = ETO_GLYPH_INDEX;
1761 QGlyphLayout glyphs = ti.glyphs;
1763 bool fast = !has_kerning && !(ti.flags & QTextItem::RightToLeft);
1764 for (
int i = 0; fast && i < glyphs.numGlyphs; i++) {
1765 if (glyphs.offsets[i].x != 0 || glyphs.offsets[i].y != 0 || glyphs.justifications[i].space_18d6 != 0
1766 || glyphs.attributes[i].dontPrint) {
1774 win_xform.eM11 = xform.m11();
1775 win_xform.eM12 = xform.m12();
1776 win_xform.eM21 = xform.m21();
1777 win_xform.eM22 = xform.m22();
1778 win_xform.eDx = xform.dx();
1779 win_xform.eDy = xform.dy();
1781 SetGraphicsMode(hdc, GM_ADVANCED);
1782 SetWorldTransform(hdc, &win_xform);
1786 QVarLengthArray<
wchar_t> g(glyphs.numGlyphs);
1787 for (
int i = 0; i < glyphs.numGlyphs; ++i)
1788 g[i] = glyphs.glyphs[i];
1790 qRound(baseline_pos.x() + glyphs.offsets[0].x.toReal()),
1791 qRound(baseline_pos.y() + glyphs.offsets[0].y.toReal()),
1792 options, 0, g.constData(), glyphs.numGlyphs, 0);
1794 QVarLengthArray<QFixedPoint> positions;
1795 QVarLengthArray<glyph_t> _glyphs;
1797 QTransform matrix = QTransform::fromTranslate(baseline_pos.x(), baseline_pos.y());
1798 ti.fontEngine->getGlyphPositions(ti.glyphs, matrix, ti.flags,
1799 _glyphs, positions);
1800 if (_glyphs.isEmpty()) {
1801 SelectObject(hdc, old_font);
1806 QVarLengthArray<INT> glyphDistances(_glyphs.size() * 2);
1807 QVarLengthArray<
wchar_t> g(_glyphs.size());
1808 const int lastGlyph = _glyphs.size() - 1;
1809 for (
int i = 0; i < lastGlyph; ++i) {
1810 glyphDistances[i * 2] = qRound(positions[i + 1].x) - qRound(positions[i].x);
1811 glyphDistances[i * 2 + 1] = qRound(positions[i + 1].y) - qRound(positions[i].y);
1814 glyphDistances[lastGlyph * 2] = 0;
1815 glyphDistances[lastGlyph * 2 + 1] = 0;
1816 g[lastGlyph] = _glyphs[lastGlyph];
1817 ExtTextOut(hdc, qRound(positions[0].x), qRound(positions[0].y), options,
nullptr,
1818 g.constData(), _glyphs.size(),
1819 glyphDistances.data());
1822 win_xform.eM11 = win_xform.eM22 = 1.0;
1823 win_xform.eM12 = win_xform.eM21 = win_xform.eDx = win_xform.eDy = 0.0;
1824 SetWorldTransform(hdc, &win_xform);
1826 SelectObject(hdc, old_font);
1829 DeleteObject(hfont);
QMarginsF qt_convertMargins(const QMarginsF &margins, QPageLayout::Unit fromUnits, QPageLayout::Unit toUnits)