1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 John Layt <jlayt@kde.org>
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtPrintSupport module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QPRINT_P_H
41 #define QPRINT_P_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API.  It exists purely as an
48 // implementation detail.  This header file may change from version to
49 // version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include <QtPrintSupport/private/qtprintsupportglobal_p.h>
55 #include <QtPrintSupport/qprinter.h>
56 
57 #include <QtCore/qstring.h>
58 #include <QtCore/qlist.h>
59 
60 #if (defined Q_OS_MACOS) || (defined Q_OS_UNIX && QT_CONFIG(cups))
61 #include <cups/ppd.h>  // Use for type defs only, don't want to actually link in main module
Q_DECLARE_METATYPE(ppd_file_t *)62 Q_DECLARE_METATYPE(ppd_file_t *)
63 #endif
64 
65 QT_BEGIN_NAMESPACE
66 
67 #ifndef QT_NO_PRINTER
68 
69 // From windgdi.h
70 #define DMBIN_UPPER           1
71 #define DMBIN_ONLYONE         1
72 #define DMBIN_LOWER           2
73 #define DMBIN_MIDDLE          3
74 #define DMBIN_MANUAL          4
75 #define DMBIN_ENVELOPE        5
76 #define DMBIN_ENVMANUAL       6
77 #define DMBIN_AUTO            7
78 #define DMBIN_TRACTOR         8
79 #define DMBIN_SMALLFMT        9
80 #define DMBIN_LARGEFMT       10
81 #define DMBIN_LARGECAPACITY  11
82 #define DMBIN_CASSETTE       14
83 #define DMBIN_FORMSOURCE     15
84 #define DMBIN_USER          256
85 
86 namespace QPrint {
87 
88     // Note: Keep in sync with QPrinter::PrinterState for now
89     // Replace later with more detailed status reporting
90     enum DeviceState {
91         Idle,
92         Active,
93         Aborted,
94         Error
95     };
96 
97     // Note: Keep in sync with QPrinter::DuplexMode
98     enum DuplexMode {
99         DuplexNone = 0,
100         DuplexAuto,
101         DuplexLongSide,
102         DuplexShortSide
103     };
104 
105     enum ColorMode {
106         GrayScale,
107         Color
108     };
109 
110     // Note: Keep in sync with QPrinter::PaperSource for now
111     // If/when made public, rearrange and rename
112     enum InputSlotId {
113         Upper,
114         Lower,
115         Middle,
116         Manual,
117         Envelope,
118         EnvelopeManual,
119         Auto,
120         Tractor,
121         SmallFormat,
122         LargeFormat,
123         LargeCapacity,
124         Cassette,
125         FormSource,
126         MaxPageSource, // Deprecated, kept for compatibility to QPrinter
127         CustomInputSlot,
128         LastInputSlot = CustomInputSlot,
129         OnlyOne = Upper
130     };
131 
132     struct InputSlot {
133         QByteArray key;
134         QString name;
135         QPrint::InputSlotId id;
136         int windowsId;
137     };
138 
139     enum OutputBinId {
140         AutoOutputBin,
141         UpperBin,
142         LowerBin,
143         RearBin,
144         CustomOutputBin,
145         LastOutputBin = CustomOutputBin
146     };
147 
148     struct OutputBin {
149         QByteArray key;
150         QString name;
151         QPrint::OutputBinId id;
152     };
153 
154 }
155 
156 struct InputSlotMap {
157     QPrint::InputSlotId id;
158     int windowsId;
159     const char *key;
160 };
161 
162 // Note: PPD standard does not define a standard set of InputSlot keywords,
163 // it is a free form text field left to the PPD writer to decide,
164 // but it does suggest some names for consistency with the Windows enum.
165 static const InputSlotMap inputSlotMap[] = {
166     { QPrint::Upper,           DMBIN_UPPER,          "Upper"          },
167     { QPrint::Lower,           DMBIN_LOWER,          "Lower"          },
168     { QPrint::Middle,          DMBIN_MIDDLE,         "Middle"         },
169     { QPrint::Manual,          DMBIN_MANUAL,         "Manual"         },
170     { QPrint::Envelope,        DMBIN_ENVELOPE,       "Envelope"       },
171     { QPrint::EnvelopeManual,  DMBIN_ENVMANUAL,      "EnvelopeManual" },
172     { QPrint::Auto,            DMBIN_AUTO,           "Auto"           },
173     { QPrint::Tractor,         DMBIN_TRACTOR,        "Tractor"        },
174     { QPrint::SmallFormat,     DMBIN_SMALLFMT,       "AnySmallFormat" },
175     { QPrint::LargeFormat,     DMBIN_LARGEFMT,       "AnyLargeFormat" },
176     { QPrint::LargeCapacity,   DMBIN_LARGECAPACITY,  "LargeCapacity"  },
177     { QPrint::Cassette,        DMBIN_CASSETTE,       "Cassette"       },
178     { QPrint::FormSource,      DMBIN_FORMSOURCE,     "FormSource"     },
179     { QPrint::Manual,          DMBIN_MANUAL,         "ManualFeed"     },
180     { QPrint::OnlyOne,         DMBIN_ONLYONE,        "OnlyOne"        }, // = QPrint::Upper
181     { QPrint::CustomInputSlot, DMBIN_USER,           ""               }  // Must always be last row
182 };
183 
184 struct OutputBinMap {
185     QPrint::OutputBinId id;
186     const char *key;
187 };
188 
189 static const OutputBinMap outputBinMap[] = {
190     { QPrint::AutoOutputBin,   ""      }, // Not a PPD defined value, internal use only
191     { QPrint::UpperBin,        "Upper" },
192     { QPrint::LowerBin,        "Lower" },
193     { QPrint::RearBin,         "Rear"  },
194     { QPrint::CustomOutputBin, ""      }  // Must always be last row
195 };
196 
197 // Print utilities shared by print plugins
198 
199 class QPrintUtils
200 {
201 
202 public:
203 
inputSlotKeyToInputSlotId(const QByteArray & key)204     static QPrint::InputSlotId inputSlotKeyToInputSlotId(const QByteArray &key)
205     {
206         for (int i = 0; inputSlotMap[i].id != QPrint::CustomInputSlot; ++i) {
207             if (inputSlotMap[i].key == key)
208                 return inputSlotMap[i].id;
209         }
210         return QPrint::CustomInputSlot;
211     }
212 
inputSlotIdToInputSlotKey(QPrint::InputSlotId id)213     static QByteArray inputSlotIdToInputSlotKey(QPrint::InputSlotId id)
214     {
215         for (int i = 0; inputSlotMap[i].id != QPrint::CustomInputSlot; ++i) {
216             if (inputSlotMap[i].id == id)
217                 return QByteArray(inputSlotMap[i].key);
218         }
219         return QByteArray();
220     }
221 
inputSlotIdToWindowsId(QPrint::InputSlotId id)222     static int inputSlotIdToWindowsId(QPrint::InputSlotId id)
223     {
224         for (int i = 0; inputSlotMap[i].id != QPrint::CustomInputSlot; ++i) {
225             if (inputSlotMap[i].id == id)
226                 return inputSlotMap[i].windowsId;
227         }
228         return 0;
229     }
230 
outputBinKeyToOutputBinId(const QByteArray & key)231     static QPrint::OutputBinId outputBinKeyToOutputBinId(const QByteArray &key)
232     {
233         for (int i = 0; outputBinMap[i].id != QPrint::CustomOutputBin; ++i) {
234             if (outputBinMap[i].key == key)
235                 return outputBinMap[i].id;
236         }
237         return QPrint::CustomOutputBin;
238     }
239 
outputBinIdToOutputBinKey(QPrint::OutputBinId id)240     static QByteArray outputBinIdToOutputBinKey(QPrint::OutputBinId id)
241     {
242         for (int i = 0; outputBinMap[i].id != QPrint::CustomOutputBin; ++i) {
243             if (outputBinMap[i].id == id)
244                 return QByteArray(outputBinMap[i].key);
245         }
246         return QByteArray();
247     }
248 
249 #if (defined Q_OS_MACOS) || (defined Q_OS_UNIX && QT_CONFIG(cups))
250 
251     // PPD utilities shared by CUPS and Mac plugins requiring CUPS headers
252     // May turn into a proper internal QPpd class if enough shared between Mac and CUPS,
253     // but where would it live?  Not in base module as don't want to link to CUPS.
254     // May have to have two copies in plugins to keep in sync.
255 
ppdChoiceToInputSlot(const ppd_choice_t & choice)256     static QPrint::InputSlot ppdChoiceToInputSlot(const ppd_choice_t &choice)
257     {
258         QPrint::InputSlot input;
259         input.key = choice.choice;
260         input.name = QString::fromUtf8(choice.text);
261         input.id = inputSlotKeyToInputSlotId(input.key);
262         input.windowsId = inputSlotMap[input.id].windowsId;
263         return input;
264     }
265 
ppdChoiceToOutputBin(const ppd_choice_t & choice)266     static QPrint::OutputBin ppdChoiceToOutputBin(const ppd_choice_t &choice)
267     {
268         QPrint::OutputBin output;
269         output.key = choice.choice;
270         output.name = QString::fromUtf8(choice.text);
271         output.id = outputBinKeyToOutputBinId(output.key);
272         return output;
273     }
274 
parsePpdResolution(const QByteArray & value)275     static int parsePpdResolution(const QByteArray &value)
276     {
277         if (value.isEmpty())
278             return -1;
279         // value can be in form 600dpi or 600x600dpi
280         QByteArray result = value.split('x').at(0);
281         if (result.endsWith("dpi"))
282             result.chop(3);
283         return result.toInt();
284     }
285 
ppdChoiceToDuplexMode(const QByteArray & choice)286     static QPrint::DuplexMode ppdChoiceToDuplexMode(const QByteArray &choice)
287     {
288         if (choice == "DuplexTumble")
289             return QPrint::DuplexShortSide;
290         else if (choice == "DuplexNoTumble")
291             return QPrint::DuplexLongSide;
292         else // None or SimplexTumble or SimplexNoTumble
293             return QPrint::DuplexNone;
294     }
295 
296 #endif // Mac and CUPS PPD Utilities
297 
298 };
299 
300 #endif // QT_NO_PRINTER
301 
302 QT_END_NAMESPACE
303 
304 #endif // QPRINT_P_H
305