Subversion Repositories Scribus

Rev

Rev 24049 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4485 cbradney 1
/*
2
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
to the COPYING file provided with the program. Following this notice may exist
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
for which a new license (GPL+exception) is in place.
6
*/
10212 cbradney 7
#include "util_printer.h"
4504 cbradney 8
#include "scconfig.h"
21179 fschmid 9
#include <QPrinterInfo>
10
#include <QPrinter>
11
#include <QPageLayout>
4485 cbradney 12
 
21179 fschmid 13
#if defined(_WIN32)
4485 cbradney 14
 #include <windows.h>
15
 #include <winspool.h>
16
#endif
17
 
10223 cbradney 18
#include <QStringList>
10004 fschmid 19
#include <QDataStream>
9535 fschmid 20
#include <QByteArray>
23845 jghali 21
 
10947 jghali 22
#include "commonstrings.h"
23845 jghali 23
#include "prefscontext.h"
24
#include "prefsfile.h"
25
#include "prefsmanager.h"
5784 jghali 26
#include "scribuscore.h"
24049 jghali 27
#include "util_os.h"
4485 cbradney 28
 
23845 jghali 29
void PrinterUtil::getDefaultPrintOptions(PrintOptions& options, const MarginStruct& docBleeds)
30
{
31
        PrefsManager& prefsManager = PrefsManager::instance();
32
        PrefsContext *prnPrefs = prefsManager.prefsFile->getContext("print_options");
33
 
34
        options.firstUse = true;
35
        options.printer  = prnPrefs->get("CurrentPrn", QString());
36
        options.useAltPrintCommand = prnPrefs->getBool("OtherCom", false);
37
        options.printerCommand = prnPrefs->get("Command", QString());
38
        options.outputSeparations = prnPrefs->getInt("Separations", 0);
39
        options.useColor = (prnPrefs->getInt("PrintColor", 0) == 0);
40
        QStringList spots { "All" , "Cyan", "Magenta", "Yellow", "Black" };
41
        int selectedSep  = prnPrefs->getInt("SepArt", 0);
42
        if ((selectedSep < 0) || (selectedSep > 4))
43
                selectedSep = 0;
44
        options.separationName = spots.at(selectedSep);
24049 jghali 45
        if (prnPrefs->contains("PrintLanguage"))
46
                options.prnLanguage = (PrintLanguage) prnPrefs->getInt("PrintLanguage", (int) PrinterUtil::getDefaultPrintLanguage(options.printer, false));
47
        else
48
                options.prnLanguage = (PrintLanguage) prnPrefs->getInt("PSLevel", (int) PrintLanguage::PostScript3);
23845 jghali 49
        options.mirrorH = prnPrefs->getBool("MirrorH", false);
50
        options.mirrorV = prnPrefs->getBool("MirrorV", false);
51
        options.setDevParam = prnPrefs->getBool("doDev", false);
52
        options.doGCR   = prnPrefs->getBool("DoGCR", prefsManager.appPrefs.printerPrefs.GCRMode);
53
        options.doClip  = prnPrefs->getBool("Clip", false);
54
        options.useSpotColors = prnPrefs->getBool("doSpot", true);
55
        options.useDocBleeds  = true;
56
        options.bleeds = docBleeds;
57
        options.markLength = prnPrefs->getDouble("markLength", 20.0);
58
        options.markOffset = prnPrefs->getDouble("markOffset", 0.0);
59
        options.cropMarks  = prnPrefs->getBool("cropMarks", false);
60
        options.bleedMarks = prnPrefs->getBool("bleedMarks", false);
61
        options.registrationMarks = prnPrefs->getBool("registrationMarks", false);
62
        options.colorMarks = prnPrefs->getBool("colorMarks", false);
63
        options.includePDFMarks = prnPrefs->getBool("includePDFMarks", true);
64
}
65
 
23777 jghali 66
QString PrinterUtil::getDefaultPrinterName()
67
{
68
        return QPrinterInfo::defaultPrinterName();
69
}
70
 
4485 cbradney 71
QStringList PrinterUtil::getPrinterNames()
72
{
21179 fschmid 73
        return QPrinterInfo::availablePrinterNames();
4485 cbradney 74
}
4504 cbradney 75
 
4506 cbradney 76
#if defined(_WIN32)
20447 jghali 77
bool PrinterUtil::getDefaultSettings(QString printerName, QByteArray& devModeA)
4504 cbradney 78
{
79
        bool done;
80
        uint size;
81
        LONG result = IDOK+1;
22516 craig 82
        Qt::HANDLE handle = nullptr;
4504 cbradney 83
        // Get the printer handle
22516 craig 84
        done = OpenPrinterW((LPWSTR) printerName.utf16(), &handle, nullptr);
20447 jghali 85
        if (!done)
4504 cbradney 86
                return false;
87
        // Get size of DEVMODE structure (public + private data)
22516 craig 88
        size = DocumentPropertiesW((HWND) ScCore->primaryMainWindow()->winId(), handle, (LPWSTR) printerName.utf16(), nullptr, nullptr, 0);
4504 cbradney 89
        // Allocate the memory needed by the DEVMODE structure
20447 jghali 90
        devModeA.resize(size);
4504 cbradney 91
        // Retrieve printer default settings
22516 craig 92
        result = DocumentPropertiesW((HWND) ScCore->primaryMainWindow()->winId(), handle, (LPWSTR) printerName.utf16(), (DEVMODEW*) devModeA.data(), nullptr, DM_OUT_BUFFER);
4504 cbradney 93
        // Free the printer handle
20447 jghali 94
        ClosePrinter(handle);
95
        return (result == IDOK);
4506 cbradney 96
}
4504 cbradney 97
#endif
98
 
4506 cbradney 99
#if defined(_WIN32)
20447 jghali 100
bool PrinterUtil::initDeviceSettings(QString printerName, QByteArray& devModeA)
4504 cbradney 101
{
102
        bool done;
103
        uint size;
104
        LONG result = IDOK+1;
22516 craig 105
        Qt::HANDLE handle = nullptr;
4504 cbradney 106
        // Get the printer handle
22516 craig 107
        done = OpenPrinterW((LPWSTR) printerName.utf16(), &handle, nullptr);
20447 jghali 108
        if (!done)
4504 cbradney 109
                return false;
110
        // Get size of DEVMODE structure (public + private data)
22516 craig 111
        size = DocumentPropertiesW((HWND) ScCore->primaryMainWindow()->winId(), handle, (LPWSTR) printerName.utf16(), nullptr, nullptr, 0);
4504 cbradney 112
        // Compare size with DevMode structure size
20447 jghali 113
        if (devModeA.size() == size)
4504 cbradney 114
        {
115
                // Merge printer settings
18419 jghali 116
                result = DocumentPropertiesW((HWND) ScCore->primaryMainWindow()->winId(), handle, (LPWSTR) printerName.utf16(), (DEVMODEW*) devModeA.data(), (DEVMODEW*) devModeA.data(), DM_IN_BUFFER | DM_OUT_BUFFER);
4504 cbradney 117
        }
118
        else
119
        {
120
                // Retrieve default settings
20447 jghali 121
                devModeA.resize(size);
22516 craig 122
                result = DocumentPropertiesW((HWND) ScCore->primaryMainWindow()->winId(), handle, (LPWSTR) printerName.utf16(), (DEVMODEW*) devModeA.data(), nullptr, DM_OUT_BUFFER);
4504 cbradney 123
        }
20447 jghali 124
        done = (result == IDOK);
4504 cbradney 125
        // Free the printer handle
20447 jghali 126
        ClosePrinter(handle);
4504 cbradney 127
        return done;
4506 cbradney 128
}
4504 cbradney 129
#endif
130
 
23137 jghali 131
bool PrinterUtil::getPrinterMarginValues(const QString& printerName, const QSizeF& pageSize, QMarginsF& margins)
4504 cbradney 132
{
21179 fschmid 133
        QPrinterInfo pInfo = QPrinterInfo::printerInfo(printerName);
23137 jghali 134
        if (pInfo.isNull())
135
                return false;
136
 
137
        QPrinter printer(pInfo, QPrinter::HighResolution);
23138 jghali 138
        margins = printer.pageLayout().margins();
139
 
140
        // Unfortunately margin values are not updated when calling QPrinter or QPageLayout's setOrientation()
141
        // so we have to adapt margin values according to orientation ourselves
142
        if (pageSize.width() > pageSize.height())
143
        {
144
                double l = margins.left();
145
                double r = margins.right();
146
                double b = margins.bottom();
147
                double t = margins.top();
148
                margins = QMarginsF(b, l, t, r);
149
        }
23137 jghali 150
        return true;
4504 cbradney 151
}
152
 
24049 jghali 153
PrintLanguage PrinterUtil::getDefaultPrintLanguage(const QString&  /*printerName*/, bool toFile)
10947 jghali 154
{
20447 jghali 155
        if (!toFile)
10947 jghali 156
        {
157
#if defined(_WIN32)
24049 jghali 158
                return PrintLanguage::WindowsGDI;
10947 jghali 159
#else
24049 jghali 160
                return PrintLanguage::PostScript3;
10947 jghali 161
#endif
162
        }
24049 jghali 163
        return PrintLanguage::PostScript3;
10947 jghali 164
}
165
 
24049 jghali 166
PrintLanguageMap PrinterUtil::getPrintLanguageSupport(const QString& printerName, bool toFile)
10947 jghali 167
{
24049 jghali 168
        PrintLanguageMap prnMap;
10947 jghali 169
        if (toFile || PrinterUtil::isPostscriptPrinter(printerName))
170
        {
171
                if (ScCore->haveGS())
172
                {
24049 jghali 173
                        prnMap.insert(CommonStrings::trPostScript1, PrintLanguage::PostScript1);
174
                        prnMap.insert(CommonStrings::trPostScript2, PrintLanguage::PostScript2);
10947 jghali 175
                }
24049 jghali 176
                prnMap.insert(CommonStrings::trPostScript3, PrintLanguage::PostScript3);
10947 jghali 177
        }
24049 jghali 178
        if (toFile || PrinterUtil::supportsPDF(printerName))
179
                prnMap.insert(CommonStrings::trPDF, PrintLanguage::PDF);
10947 jghali 180
#if defined(_WIN32)
181
        if (!toFile)
24049 jghali 182
                prnMap.insert(CommonStrings::trWindowsGDI, PrintLanguage::WindowsGDI);
10947 jghali 183
#endif
184
        return prnMap;
185
}
186
 
24049 jghali 187
bool PrinterUtil::checkPrintLanguageSupport(const QString& printerName, PrintLanguage engine, bool toFile)
10947 jghali 188
{
24049 jghali 189
        if (engine >= PrintLanguage::PostScript1 && engine <= PrintLanguage::PostScript3)
190
                return (toFile || PrinterUtil::isPostscriptPrinter(printerName));
191
 
192
        if (engine == PrintLanguage::WindowsGDI)
193
                return os_is_win();
194
 
195
        if (engine == PrintLanguage::PDF)
196
                return toFile || os_is_unix();
197
 
198
        return false;
199
}
200
 
201
bool PrinterUtil::supportsPDF(const QString& /*printerName*/)
202
{
203
#ifdef _WIN32
204
        return false;
10947 jghali 205
#else
24049 jghali 206
        return true;
10947 jghali 207
#endif
208
}
209
 
4504 cbradney 210
//Parameter needed on win32..
23273 jghali 211
bool PrinterUtil::isPostscriptPrinter(const QString& printerName)
4504 cbradney 212
{
213
#ifdef _WIN32
214
        HDC dc;
215
        int     escapeCode;
216
        char technology[MAX_PATH] = {0};
217
 
218
        // Create the default device context
22516 craig 219
        dc = CreateDCW(nullptr, (LPCWSTR) printerName.utf16(), nullptr, nullptr);
20447 jghali 220
        if (!dc)
4504 cbradney 221
        {
18194 fschmid 222
                qWarning("isPostscriptPrinter() failed to create device context for %s", printerName.toLatin1().data());
4504 cbradney 223
                return false;
224
        }
225
        // test if printer support the POSTSCRIPT_PASSTHROUGH escape code
226
        escapeCode = POSTSCRIPT_PASSTHROUGH;
22516 craig 227
        if (ExtEscape(dc, QUERYESCSUPPORT, sizeof(int), (LPCSTR) &escapeCode, 0, nullptr) > 0)
4504 cbradney 228
        {
20447 jghali 229
                DeleteDC(dc);
4504 cbradney 230
                return true;
231
        }
232
        // test if printer support the POSTSCRIPT_DATA escape code
233
        escapeCode = POSTSCRIPT_DATA;
22516 craig 234
        if (ExtEscape(dc, QUERYESCSUPPORT, sizeof(int), (LPCSTR) &escapeCode, 0, nullptr) > 0)
4504 cbradney 235
        {
20447 jghali 236
                DeleteDC(dc);
4504 cbradney 237
                return true;
238
        }
239
        // try to get postscript support by testing the printer technology
240
        escapeCode = GETTECHNOLOGY;
22516 craig 241
        if (ExtEscape(dc, QUERYESCSUPPORT, sizeof(int), (LPCSTR) &escapeCode, 0, nullptr) > 0)
4504 cbradney 242
        {
243
                // if GETTECHNOLOGY is supported, then ... get technology
22516 craig 244
                if (ExtEscape(dc, GETTECHNOLOGY, 0, nullptr, MAX_PATH, (LPSTR) technology) > 0)
4504 cbradney 245
                {
246
                        // check technology string for postscript word
20447 jghali 247
                        strupr(technology);
248
                        if (strstr(technology, "POSTSCRIPT"))
4504 cbradney 249
                        {
20447 jghali 250
                                DeleteDC(dc);
4504 cbradney 251
                                return true;
252
                        }
253
                }
254
        }
20447 jghali 255
        DeleteDC(dc);
4504 cbradney 256
        return false;
257
#else
258
        return true;
259
#endif
260
}