QPdfWriter: use DeviceGray when in Grayscale mode

QPdfWriter has "always" supported a grayscale mode in order to
support grayscale printing from QPrinter. When in this mode,
colors were still emitted as RGB, just with equal quantities for
each channel.

But the PDF format itself supports grayscale colors, so
avoid the conversions and emit colors directly in that.

Change-Id: Id5ec3bc42b710909f32986c59d8fbd6f748e02f9
Reviewed-by: Lars Knoll <lars@knoll.priv.no>
bb10
Giuseppe D'Angelo 2024-02-01 18:35:02 +01:00
parent 3a7cda715a
commit 97ca5f6161
1 changed files with 17 additions and 6 deletions

View File

@ -1658,7 +1658,6 @@ void QPdfEnginePrivate::writeColor(ColorDomain domain, const QColor &color)
switch (actualColorModel) {
case QPdfEngine::ColorModel::RGB:
case QPdfEngine::ColorModel::Grayscale:
switch (domain) {
case ColorDomain::Stroking:
*currentPage << "/CSp CS\n"; break;
@ -1668,6 +1667,16 @@ void QPdfEnginePrivate::writeColor(ColorDomain domain, const QColor &color)
*currentPage << "/PCSp cs\n"; break;
}
break;
case QPdfEngine::ColorModel::Grayscale:
switch (domain) {
case ColorDomain::Stroking:
*currentPage << "/CSpg CS\n"; break;
case ColorDomain::NonStroking:
*currentPage << "/CSpg cs\n"; break;
case ColorDomain::NonStrokingPattern:
*currentPage << "/PCSpg cs\n"; break;
}
break;
case QPdfEngine::ColorModel::CMYK:
switch (domain) {
case ColorDomain::Stroking:
@ -1694,7 +1703,7 @@ void QPdfEnginePrivate::writeColor(ColorDomain domain, const QColor &color)
break;
case QPdfEngine::ColorModel::Grayscale: {
const qreal gray = qGray(color.rgba()) / 255.;
*currentPage << gray << gray << gray;
*currentPage << gray;
break;
}
case QPdfEngine::ColorModel::CMYK:
@ -2480,8 +2489,9 @@ void QPdfEnginePrivate::ShadingFunctionResult::writeColorSpace(QPdf::ByteStream
*stream << "/ColorSpace ";
switch (colorModel) {
case QPdfEngine::ColorModel::RGB:
case QPdfEngine::ColorModel::Grayscale:
*stream << "/DeviceRGB\n"; break;
case QPdfEngine::ColorModel::Grayscale:
*stream << "/DeviceGray\n"; break;
case QPdfEngine::ColorModel::CMYK:
*stream << "/DeviceCMYK\n"; break;
case QPdfEngine::ColorModel::Auto:
@ -2548,12 +2558,13 @@ QPdfEnginePrivate::createShadingFunction(const QGradient *gradient, int from, in
} else {
switch (result.colorModel) {
case QPdfEngine::ColorModel::RGB:
case QPdfEngine::ColorModel::Grayscale:
// For backwards compatibility, Grayscale emits RGB colors
s << "/C0 [" << stops.at(i).second.redF() << stops.at(i).second.greenF() << stops.at(i).second.blueF() << "]\n"
"/C1 [" << stops.at(i + 1).second.redF() << stops.at(i + 1).second.greenF() << stops.at(i + 1).second.blueF() << "]\n";
break;
case QPdfEngine::ColorModel::Grayscale:
s << "/C0 [" << (qGray(stops.at(i).second.rgba()) / 255.) << "]\n"
"/C1 [" << (qGray(stops.at(i + 1).second.rgba()) / 255.) << "]\n";
break;
case QPdfEngine::ColorModel::CMYK:
s << "/C0 [" << stops.at(i).second.cyanF()
<< stops.at(i).second.magentaF()