LibreOffice Module vcl (master) 1
ppdparser.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19#ifndef INCLUDED_VCL_PPDPARSER_HXX
20#define INCLUDED_VCL_PPDPARSER_HXX
21
22#include <sal/config.h>
23
24#include <cstddef>
25#include <memory>
26#include <string_view>
27#include <unordered_map>
28#include <vector>
29
30#include <rtl/string.hxx>
31#include <rtl/ustring.hxx>
32#include <tools/solar.h>
33#include <vcl/dllapi.h>
34
35#define PRINTER_PPDDIR "driver"
36
37namespace psp {
38
39enum class orientation;
40
41class PPDCache;
42class PPDTranslator;
43
45
47{
49 //CustomOption stuff for fdo#43049
50 //see http://www.cups.org/documentation.php/spec-ppd.html#OPTIONS
51 //for full specs, only the basics are implemented here
54 mutable OUString m_aCustomOption;
55 OUString m_aOption;
56 OUString m_aValue;
57};
58
59
60/*
61 * PPDKey - a container for the available options (=values) of a PPD keyword
62 */
63
64class PPDKey
65{
66 friend class PPDParser;
67 friend class CPDManager;
68
69 typedef std::unordered_map< OUString, PPDValue > hash_type;
70 typedef std::vector< PPDValue* > value_type;
71
72 OUString m_aKey;
77 OUString m_aGroup;
78
79public:
80 enum class SetupType { ExitServer, Prolog, DocumentSetup, PageSetup, JCLSetup, AnySetup };
81private:
82
86
87 void eraseValue( const OUString& rOption );
88public:
89 PPDKey( OUString aKey );
91
92 PPDValue* insertValue(const OUString& rOption, PPDValueType eType, bool bCustomOption = false);
93 int countValues() const
94 { return m_aValues.size(); }
95 // neither getValue will return the query option
96 const PPDValue* getValue( int n ) const;
97 const PPDValue* getValue( const OUString& rOption ) const;
98 const PPDValue* getValueCaseInsensitive( const OUString& rOption ) const;
99 const PPDValue* getDefaultValue() const { return m_pDefaultValue; }
100 const OUString& getGroup() const { return m_aGroup; }
101
102 const OUString& getKey() const { return m_aKey; }
103 bool isUIKey() const { return m_bUIOption; }
106};
107
108// define a hash for PPDKey
110{
111 size_t operator()( const PPDKey * pKey) const
112 { return reinterpret_cast<size_t>(pKey); }
113};
114
115/*
116 * PPDParser - parses a PPD file and contains all available keys from it
117 */
118
120{
121 friend class PPDContext;
122 friend class CUPSManager;
123 friend class CPDManager;
124 friend class PPDCache;
125
126 typedef std::unordered_map< OUString, std::unique_ptr<PPDKey> > hash_type;
127 typedef std::vector< PPDKey* > value_type;
128
129 void insertKey( std::unique_ptr<PPDKey> pKey );
130public:
132 {
137
138 PPDConstraint() : m_pKey1( nullptr ), m_pOption1( nullptr ), m_pKey2( nullptr ), m_pOption2( nullptr ) {}
139 };
140private:
143 ::std::vector< PPDConstraint > m_aConstraints;
144
145 // the full path of the PPD file
146 OUString m_aFile;
147 // some basic attributes
151 rtl_TextEncoding m_aFileEncoding;
152
153
154 // shortcuts to important keys and their default values
155 // imageable area
157 // paper dimensions
160 // paper trays
162 // resolutions
164
165 // translations
166 std::unique_ptr<PPDTranslator> m_pTranslator;
167
168 PPDParser( OUString aFile );
169 PPDParser(OUString aFile, const std::vector<PPDKey*>& keys);
170
171 void parseOrderDependency(const OString& rLine);
172 void parseOpenUI(const OString& rLine, std::string_view rPPDGroup);
173 void parseConstraint(const OString& rLine);
174 void parse( std::vector< OString >& rLines );
175
176 OUString handleTranslation(const OString& i_rString, bool i_bIsGlobalized);
177
178 static void scanPPDDir( const OUString& rDir );
179 static void initPPDFiles(PPDCache &rPPDCache);
180 static OUString getPPDFile( const OUString& rFile );
181
182 OUString matchPaperImpl(int nWidth, int nHeight, bool bDontSwap = false, psp::orientation* pOrientation = nullptr) const;
183
184public:
186 static const PPDParser* getParser( const OUString& rFile );
187
188 const PPDKey* getKey( int n ) const;
189 const PPDKey* getKey( const OUString& rKey ) const;
190 int getKeys() const { return m_aKeys.size(); }
191 bool hasKey( const PPDKey* ) const;
192
193 const ::std::vector< PPDConstraint >& getConstraints() const { return m_aConstraints; }
194
195 bool isColorDevice() const { return m_bColorDevice; }
196 bool isType42Capable() const { return m_bType42Capable; }
198
199 OUString getDefaultPaperDimension() const;
200 void getDefaultPaperDimension( int& rWidth, int& rHeight ) const
201 { getPaperDimension( getDefaultPaperDimension(), rWidth, rHeight ); }
202 bool getPaperDimension( std::u16string_view rPaperName,
203 int& rWidth, int& rHeight ) const;
204 // width and height in pt
205 // returns false if paper not found
206
207 // match the best paper for width and height
208 OUString matchPaper( int nWidth, int nHeight, psp::orientation* pOrientation = nullptr ) const;
209
210 bool getMargins( std::u16string_view rPaperName,
211 int &rLeft, int& rRight,
212 int &rUpper, int& rLower ) const;
213 // values in pt
214 // returns true if paper found
215
216 // values int pt
217
218 OUString getDefaultInputSlot() const;
219
220 void getDefaultResolution( int& rXRes, int& rYRes ) const;
221 // values in dpi
222 static void getResolutionFromString( std::u16string_view, int&, int& );
223 // helper function
224
225 OUString translateKey( const OUString& i_rKey ) const;
226 OUString translateOption( std::u16string_view i_rKey,
227 const OUString& i_rOption ) const;
228};
229
230
231/*
232 * PPDContext - a class to manage user definable states based on the
233 * contents of a PPDParser.
234 */
235
237{
238 typedef std::unordered_map< const PPDKey*, const PPDValue*, PPDKeyhash > hash_type;
241
242 // returns false: check failed, new value is constrained
243 // true: check succeeded, new value can be set
244 bool checkConstraints( const PPDKey*, const PPDValue*, bool bDoReset );
245 bool resetValue( const PPDKey*, bool bDefaultable = false );
246public:
248 PPDContext( const PPDContext& rContext ) { operator=( rContext ); }
249 PPDContext& operator=( const PPDContext& rContext ) = default;
251
252 void setParser( const PPDParser* );
253 const PPDParser* getParser() const { return m_pParser; }
254
255 const PPDValue* getValue( const PPDKey* ) const;
256 const PPDValue* setValue( const PPDKey*, const PPDValue*, bool bDontCareForConstraints = false );
257
258 std::size_t countValuesModified() const { return m_aCurrentValues.size(); }
259 const PPDKey* getModifiedKey( std::size_t n ) const;
260
261 // public wrapper for the private method
262 bool checkConstraints( const PPDKey*, const PPDValue* );
263
264 // for printer setup
265 char* getStreamableBuffer( sal_uLong& rBytes ) const;
266 void rebuildFromStreamBuffer(const std::vector<char> &rBuffer);
267
268 // convenience
270
271 // width, height in points, paper will contain the name of the selected
272 // paper after the call
273 void getPageSize( OUString& rPaper, int& rWidth, int& rHeight ) const;
274};
275
276} // namespace
277
278#endif // INCLUDED_VCL_PPDPARSER_HXX
279
280/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool checkConstraints(const PPDKey *, const PPDValue *, bool bDoReset)
const PPDParser * getParser() const
Definition: ppdparser.hxx:253
void setParser(const PPDParser *)
void getPageSize(OUString &rPaper, int &rWidth, int &rHeight) const
const PPDValue * getValue(const PPDKey *) const
PPDContext & operator=(PPDContext &&rContext)
std::size_t countValuesModified() const
Definition: ppdparser.hxx:258
PPDContext(const PPDContext &rContext)
Definition: ppdparser.hxx:248
char * getStreamableBuffer(sal_uLong &rBytes) const
bool checkConstraints(const PPDKey *, const PPDValue *)
PPDContext & operator=(const PPDContext &rContext)=default
hash_type m_aCurrentValues
Definition: ppdparser.hxx:239
bool resetValue(const PPDKey *, bool bDefaultable=false)
void rebuildFromStreamBuffer(const std::vector< char > &rBuffer)
const PPDKey * getModifiedKey(std::size_t n) const
int getRenderResolution() const
const PPDParser * m_pParser
Definition: ppdparser.hxx:240
std::unordered_map< const PPDKey *, const PPDValue *, PPDKeyhash > hash_type
Definition: ppdparser.hxx:238
const PPDValue * setValue(const PPDKey *, const PPDValue *, bool bDontCareForConstraints=false)
const OUString & getGroup() const
Definition: ppdparser.hxx:100
SetupType m_eSetupType
Definition: ppdparser.hxx:85
bool isUIKey() const
Definition: ppdparser.hxx:103
const PPDValue * getDefaultValue() const
Definition: ppdparser.hxx:99
SetupType getSetupType() const
Definition: ppdparser.hxx:104
value_type m_aOrderedValues
Definition: ppdparser.hxx:74
const PPDValue * m_pDefaultValue
Definition: ppdparser.hxx:75
const PPDValue * getValueCaseInsensitive(const OUString &rOption) const
bool m_bUIOption
Definition: ppdparser.hxx:83
PPDKey(OUString aKey)
hash_type m_aValues
Definition: ppdparser.hxx:73
int m_nOrderDependency
Definition: ppdparser.hxx:84
int countValues() const
Definition: ppdparser.hxx:93
OUString m_aGroup
Definition: ppdparser.hxx:77
OUString m_aKey
Definition: ppdparser.hxx:72
const PPDValue * getValue(const OUString &rOption) const
const PPDValue * getValue(int n) const
PPDValue * insertValue(const OUString &rOption, PPDValueType eType, bool bCustomOption=false)
int getOrderDependency() const
Definition: ppdparser.hxx:105
const OUString & getKey() const
Definition: ppdparser.hxx:102
void eraseValue(const OUString &rOption)
std::unordered_map< OUString, PPDValue > hash_type
Definition: ppdparser.hxx:69
std::vector< PPDValue * > value_type
Definition: ppdparser.hxx:70
bool m_bQueryValue
Definition: ppdparser.hxx:76
static const PPDParser * getParser(const OUString &rFile)
const PPDValue * m_pDefaultPaperDimension
Definition: ppdparser.hxx:158
void insertKey(std::unique_ptr< PPDKey > pKey)
void getDefaultResolution(int &rXRes, int &rYRes) const
OUString matchPaperImpl(int nWidth, int nHeight, bool bDontSwap=false, psp::orientation *pOrientation=nullptr) const
static void scanPPDDir(const OUString &rDir)
const PPDKey * m_pPaperDimensions
Definition: ppdparser.hxx:159
OUString handleTranslation(const OString &i_rString, bool i_bIsGlobalized)
value_type m_aOrderedKeys
Definition: ppdparser.hxx:142
::std::vector< PPDConstraint > m_aConstraints
Definition: ppdparser.hxx:143
PPDParser(OUString aFile, const std::vector< PPDKey * > &keys)
void getDefaultPaperDimension(int &rWidth, int &rHeight) const
Definition: ppdparser.hxx:200
OUString translateOption(std::u16string_view i_rKey, const OUString &i_rOption) const
rtl_TextEncoding m_aFileEncoding
Definition: ppdparser.hxx:151
void parse(std::vector< OString > &rLines)
std::vector< PPDKey * > value_type
Definition: ppdparser.hxx:127
const ::std::vector< PPDConstraint > & getConstraints() const
Definition: ppdparser.hxx:193
void parseConstraint(const OString &rLine)
bool getPaperDimension(std::u16string_view rPaperName, int &rWidth, int &rHeight) const
bool m_bType42Capable
Definition: ppdparser.hxx:149
OUString getDefaultPaperDimension() const
sal_uLong m_nLanguageLevel
Definition: ppdparser.hxx:150
int getKeys() const
Definition: ppdparser.hxx:190
std::unique_ptr< PPDTranslator > m_pTranslator
Definition: ppdparser.hxx:166
OUString translateKey(const OUString &i_rKey) const
bool getMargins(std::u16string_view rPaperName, int &rLeft, int &rRight, int &rUpper, int &rLower) const
OUString m_aFile
Definition: ppdparser.hxx:146
const PPDValue * m_pDefaultInputSlot
Definition: ppdparser.hxx:161
const PPDKey * getKey(int n) const
void parseOrderDependency(const OString &rLine)
sal_uLong getLanguageLevel() const
Definition: ppdparser.hxx:197
bool hasKey(const PPDKey *) const
OUString matchPaper(int nWidth, int nHeight, psp::orientation *pOrientation=nullptr) const
hash_type m_aKeys
Definition: ppdparser.hxx:141
static void initPPDFiles(PPDCache &rPPDCache)
PPDParser(OUString aFile)
const PPDKey * m_pImageableAreas
Definition: ppdparser.hxx:156
static OUString getPPDFile(const OUString &rFile)
bool isColorDevice() const
Definition: ppdparser.hxx:195
const PPDKey * getKey(const OUString &rKey) const
std::unordered_map< OUString, std::unique_ptr< PPDKey > > hash_type
Definition: ppdparser.hxx:126
void parseOpenUI(const OString &rLine, std::string_view rPPDGroup)
bool isType42Capable() const
Definition: ppdparser.hxx:196
friend class PPDCache
Definition: ppdparser.hxx:124
OUString getDefaultInputSlot() const
static void getResolutionFromString(std::u16string_view, int &, int &)
const PPDValue * m_pDefaultResolution
Definition: ppdparser.hxx:163
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
orientation
Definition: jobdata.hxx:27
PPDValueType
Definition: ppdparser.hxx:44
@ eNo
Definition: ppdparser.hxx:44
@ eString
Definition: ppdparser.hxx:44
@ eSymbol
Definition: ppdparser.hxx:44
@ eInvocation
Definition: ppdparser.hxx:44
@ eQuoted
Definition: ppdparser.hxx:44
sal_uIntPtr sal_uLong
size_t operator()(const PPDKey *pKey) const
Definition: ppdparser.hxx:111
const PPDValue * m_pOption2
Definition: ppdparser.hxx:136
const PPDValue * m_pOption1
Definition: ppdparser.hxx:134
bool m_bCustomOption
Definition: ppdparser.hxx:52
OUString m_aOption
Definition: ppdparser.hxx:55
bool m_bCustomOptionSetViaApp
Definition: ppdparser.hxx:53
OUString m_aCustomOption
Definition: ppdparser.hxx:54
OUString m_aValue
Definition: ppdparser.hxx:56
PPDValueType m_eType
Definition: ppdparser.hxx:48