Bug 1670943 - Add monochrome support for OSX. r=jwatt,jfkthame

See comments for fun.

Differential Revision: https://phabricator.services.mozilla.com/D93527
This commit is contained in:
Emilio Cobos Álvarez 2020-10-23 11:58:42 +00:00
Родитель ccea7aadbb
Коммит 5a616bbd7e
4 изменённых файлов: 70 добавлений и 5 удалений

Просмотреть файл

@ -8868,6 +8868,12 @@
value: 144.0f
mirror: always
# Whether support for monochrome printing is enabled on mac.
- name: print.mac.monochrome.enabled
type: RelaxedAtomicBool
value: true
mirror: always
#---------------------------------------------------------------------------
# Prefs starting with "privacy."
#---------------------------------------------------------------------------

Просмотреть файл

@ -13,6 +13,7 @@
#include "nsXULAppAPI.h"
#include "mozilla/Preferences.h"
#include "mozilla/StaticPrefs_print.h"
using namespace mozilla;
@ -143,6 +144,38 @@ NS_IMETHODIMP nsPrintSettingsX::_Assign(nsIPrintSettings* aPS) {
return NS_OK;
}
struct KnownMonochromeSetting {
const NSString* mName;
const NSString* mValue;
};
// There's no standard setting in Core Printing for monochrome. Or rather,
// there is (PMSetColorMode) but it does nothing.
//
// The way to set the relevant setting is via the raw printSettings API, and it
// depends on the print driver which key it listens to.
//
// So we set / look for a variety of driver-specific keys that are known to
// work across printers.
//
// We set all the known settings, because the alternative to that is parsing
// ppd files from the printer and find the relevant known choices that can
// apply, and that is a lot more complex, similarly sketchy (requires the same
// amount of driver-specific knowledge), and requires using deprecated CUPS
// APIs.
static const KnownMonochromeSetting kKnownMonochromeSettings[] = {
{@"ColorModel", @"Gray"}, // Generic
{@"BRMonoColor", @"Mono"}, // Brother
{@"BRPrintQuality", @"Black"}, // Brother
{@"INK", @"MONO"}, // Epson
{@"HPColorMode", @"GrayscalePrint"}, // HP
{@"ColorMode", @"Mono"}, // Samsung
{@"PrintoutMode", @"Normal.Gray"}, // Foomatic
{@"ProcessColorModel", @"Mono"}, // Samsung
{@"ARCMode", @"CMBW"}, // Sharp
{@"XRXColor", @"BW"} // Xerox
};
void nsPrintSettingsX::SetPMPageFormat(PMPageFormat aPageFormat) {
// Get a printInfo based on our current properties.
NSPrintInfo* printInfo = CreateOrCopyPrintInfo();
@ -269,6 +302,12 @@ NSPrintInfo* nsPrintSettingsX::CreateOrCopyPrintInfo(bool aWithScaling) {
}
}
if (StaticPrefs::print_mac_monochrome_enabled() && !GetPrintInColor()) {
for (const auto& setting : kKnownMonochromeSettings) {
[printSettings setObject:setting.mValue forKey:setting.mName];
}
}
return printInfo;
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(nullptr);
@ -375,5 +414,24 @@ void nsPrintSettingsX::SetFromPrintInfo(NSPrintInfo* aPrintInfo, bool aAdoptPrin
mDestination = [value unsignedShortValue];
}
const bool color = [&] {
if (StaticPrefs::print_mac_monochrome_enabled()) {
for (const auto& setting : kKnownMonochromeSettings) {
NSString* value = [printSettings objectForKey:setting.mName];
if (!value) {
continue;
}
if ([setting.mValue isEqualToString:value]) {
return false;
}
}
}
return true;
}();
SetPrintInColor(color);
SetIsInitializedFromPrinter(true);
NS_OBJC_END_TRY_ABORT_BLOCK;
}

Просмотреть файл

@ -193,7 +193,7 @@ interface nsIPrintSettings : nsISupports
attribute short paperSizeUnit; /* paper is in inches or mm */
attribute boolean printReversed;
attribute boolean printInColor; /* a false means grayscale */
[infallible] attribute boolean printInColor; /* a false means grayscale */
attribute long orientation; /* see orientation consts */
attribute long numCopies;

Просмотреть файл

@ -6,6 +6,7 @@
#include "nsPrinterCUPS.h"
#include "mozilla/GkRustUtils.h"
#include "mozilla/StaticPrefs_print.h"
#include "nsTHashtable.h"
#include "nsPaper.h"
#include "nsPrinterBase.h"
@ -199,10 +200,10 @@ bool nsPrinterCUPS::SupportsDuplex() const {
bool nsPrinterCUPS::SupportsMonochrome() const {
#ifdef XP_MACOSX
// On Cocoa/CorePrinting there's no driver-independent API to switch a printer
// from color to monochrome printing, so we can't claim to support it if we
// support color.
return !SupportsColor();
if (!SupportsColor()) {
return true;
}
return StaticPrefs::print_mac_monochrome_enabled();
#else
return true;
#endif