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
39class PPDCache;
40class PPDTranslator;
41
43
45{
47 //CustomOption stuff for fdo#43049
48 //see http://www.cups.org/documentation.php/spec-ppd.html#OPTIONS
49 //for full specs, only the basics are implemented here
52 mutable OUString m_aCustomOption;
53 OUString m_aOption;
54 OUString m_aValue;
55};
56
57
58/*
59 * PPDKey - a container for the available options (=values) of a PPD keyword
60 */
61
62class PPDKey
63{
64 friend class PPDParser;
65 friend class CPDManager;
66
67 typedef std::unordered_map< OUString, PPDValue > hash_type;
68 typedef std::vector< PPDValue* > value_type;
69
70 OUString m_aKey;
75 OUString m_aGroup;
76
77public:
78 enum class SetupType { ExitServer, Prolog, DocumentSetup, PageSetup, JCLSetup, AnySetup };
79private:
80
84
85 void eraseValue( const OUString& rOption );
86public:
87 PPDKey( OUString aKey );
89
90 PPDValue* insertValue(const OUString& rOption, PPDValueType eType, bool bCustomOption = false);
91 int countValues() const
92 { return m_aValues.size(); }
93 // neither getValue will return the query option
94 const PPDValue* getValue( int n ) const;
95 const PPDValue* getValue( const OUString& rOption ) const;
96 const PPDValue* getValueCaseInsensitive( const OUString& rOption ) const;
97 const PPDValue* getDefaultValue() const { return m_pDefaultValue; }
98 const OUString& getGroup() const { return m_aGroup; }
99
100 const OUString& getKey() const { return m_aKey; }
101 bool isUIKey() const { return m_bUIOption; }
104};
105
106// define a hash for PPDKey
108{
109 size_t operator()( const PPDKey * pKey) const
110 { return reinterpret_cast<size_t>(pKey); }
111};
112
113
114/*
115 * PPDParser - parses a PPD file and contains all available keys from it
116 */
117
119{
120 friend class PPDContext;
121 friend class CUPSManager;
122 friend class CPDManager;
123 friend class PPDCache;
124
125 typedef std::unordered_map< OUString, std::unique_ptr<PPDKey> > hash_type;
126 typedef std::vector< PPDKey* > value_type;
127
128 void insertKey( std::unique_ptr<PPDKey> pKey );
129public:
131 {
136
137 PPDConstraint() : m_pKey1( nullptr ), m_pOption1( nullptr ), m_pKey2( nullptr ), m_pOption2( nullptr ) {}
138 };
139private:
142 ::std::vector< PPDConstraint > m_aConstraints;
143
144 // the full path of the PPD file
145 OUString m_aFile;
146 // some basic attributes
150 rtl_TextEncoding m_aFileEncoding;
151
152
153 // shortcuts to important keys and their default values
154 // imageable area
156 // paper dimensions
159 // paper trays
161 // resolutions
163
164 // translations
165 std::unique_ptr<PPDTranslator> m_pTranslator;
166
167 PPDParser( OUString aFile );
168 PPDParser(OUString aFile, const std::vector<PPDKey*>& keys);
169
170 void parseOrderDependency(const OString& rLine);
171 void parseOpenUI(const OString& rLine, std::string_view rPPDGroup);
172 void parseConstraint(const OString& rLine);
173 void parse( std::vector< OString >& rLines );
174
175 OUString handleTranslation(const OString& i_rString, bool i_bIsGlobalized);
176
177 static void scanPPDDir( const OUString& rDir );
178 static void initPPDFiles(PPDCache &rPPDCache);
179 static OUString getPPDFile( const OUString& rFile );
180public:
182 static const PPDParser* getParser( const OUString& rFile );
183
184 const PPDKey* getKey( int n ) const;
185 const PPDKey* getKey( const OUString& rKey ) const;
186 int getKeys() const { return m_aKeys.size(); }
187 bool hasKey( const PPDKey* ) const;
188
189 const ::std::vector< PPDConstraint >& getConstraints() const { return m_aConstraints; }
190
191 bool isColorDevice() const { return m_bColorDevice; }
192 bool isType42Capable() const { return m_bType42Capable; }
194
195 OUString getDefaultPaperDimension() const;
196 void getDefaultPaperDimension( int& rWidth, int& rHeight ) const
197 { getPaperDimension( getDefaultPaperDimension(), rWidth, rHeight ); }
198 bool getPaperDimension( std::u16string_view rPaperName,
199 int& rWidth, int& rHeight ) const;
200 // width and height in pt
201 // returns false if paper not found
202
203 // match the best paper for width and height
204 OUString matchPaper( int nWidth, int nHeight ) const;
205
206 bool getMargins( std::u16string_view rPaperName,
207 int &rLeft, int& rRight,
208 int &rUpper, int& rLower ) const;
209 // values in pt
210 // returns true if paper found
211
212 // values int pt
213
214 OUString getDefaultInputSlot() const;
215
216 void getDefaultResolution( int& rXRes, int& rYRes ) const;
217 // values in dpi
218 static void getResolutionFromString( std::u16string_view, int&, int& );
219 // helper function
220
221 OUString translateKey( const OUString& i_rKey ) const;
222 OUString translateOption( std::u16string_view i_rKey,
223 const OUString& i_rOption ) const;
224};
225
226
227/*
228 * PPDContext - a class to manage user definable states based on the
229 * contents of a PPDParser.
230 */
231
233{
234 typedef std::unordered_map< const PPDKey*, const PPDValue*, PPDKeyhash > hash_type;
237
238 // returns false: check failed, new value is constrained
239 // true: check succeeded, new value can be set
240 bool checkConstraints( const PPDKey*, const PPDValue*, bool bDoReset );
241 bool resetValue( const PPDKey*, bool bDefaultable = false );
242public:
244 PPDContext( const PPDContext& rContext ) { operator=( rContext ); }
245 PPDContext& operator=( const PPDContext& rContext ) = default;
247
248 void setParser( const PPDParser* );
249 const PPDParser* getParser() const { return m_pParser; }
250
251 const PPDValue* getValue( const PPDKey* ) const;
252 const PPDValue* setValue( const PPDKey*, const PPDValue*, bool bDontCareForConstraints = false );
253
254 std::size_t countValuesModified() const { return m_aCurrentValues.size(); }
255 const PPDKey* getModifiedKey( std::size_t n ) const;
256
257 // public wrapper for the private method
258 bool checkConstraints( const PPDKey*, const PPDValue* );
259
260 // for printer setup
261 char* getStreamableBuffer( sal_uLong& rBytes ) const;
262 void rebuildFromStreamBuffer(const std::vector<char> &rBuffer);
263
264 // convenience
266
267 // width, height in points, paper will contain the name of the selected
268 // paper after the call
269 void getPageSize( OUString& rPaper, int& rWidth, int& rHeight ) const;
270};
271
272} // namespace
273
274#endif // INCLUDED_VCL_PPDPARSER_HXX
275
276/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool checkConstraints(const PPDKey *, const PPDValue *, bool bDoReset)
const PPDParser * getParser() const
Definition: ppdparser.hxx:249
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:254
PPDContext(const PPDContext &rContext)
Definition: ppdparser.hxx:244
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:235
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:236
std::unordered_map< const PPDKey *, const PPDValue *, PPDKeyhash > hash_type
Definition: ppdparser.hxx:234
const PPDValue * setValue(const PPDKey *, const PPDValue *, bool bDontCareForConstraints=false)
const OUString & getGroup() const
Definition: ppdparser.hxx:98
SetupType m_eSetupType
Definition: ppdparser.hxx:83
bool isUIKey() const
Definition: ppdparser.hxx:101
const PPDValue * getDefaultValue() const
Definition: ppdparser.hxx:97
SetupType getSetupType() const
Definition: ppdparser.hxx:102
value_type m_aOrderedValues
Definition: ppdparser.hxx:72
const PPDValue * m_pDefaultValue
Definition: ppdparser.hxx:73
const PPDValue * getValueCaseInsensitive(const OUString &rOption) const
bool m_bUIOption
Definition: ppdparser.hxx:81
PPDKey(OUString aKey)
hash_type m_aValues
Definition: ppdparser.hxx:71
int m_nOrderDependency
Definition: ppdparser.hxx:82
int countValues() const
Definition: ppdparser.hxx:91
OUString m_aGroup
Definition: ppdparser.hxx:75
OUString m_aKey
Definition: ppdparser.hxx:70
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:103
const OUString & getKey() const
Definition: ppdparser.hxx:100
void eraseValue(const OUString &rOption)
std::unordered_map< OUString, PPDValue > hash_type
Definition: ppdparser.hxx:67
std::vector< PPDValue * > value_type
Definition: ppdparser.hxx:68
bool m_bQueryValue
Definition: ppdparser.hxx:74
static const PPDParser * getParser(const OUString &rFile)
const PPDValue * m_pDefaultPaperDimension
Definition: ppdparser.hxx:157
void insertKey(std::unique_ptr< PPDKey > pKey)
void getDefaultResolution(int &rXRes, int &rYRes) const
static void scanPPDDir(const OUString &rDir)
const PPDKey * m_pPaperDimensions
Definition: ppdparser.hxx:158
OUString handleTranslation(const OString &i_rString, bool i_bIsGlobalized)
value_type m_aOrderedKeys
Definition: ppdparser.hxx:141
::std::vector< PPDConstraint > m_aConstraints
Definition: ppdparser.hxx:142
PPDParser(OUString aFile, const std::vector< PPDKey * > &keys)
void getDefaultPaperDimension(int &rWidth, int &rHeight) const
Definition: ppdparser.hxx:196
OUString translateOption(std::u16string_view i_rKey, const OUString &i_rOption) const
rtl_TextEncoding m_aFileEncoding
Definition: ppdparser.hxx:150
void parse(std::vector< OString > &rLines)
std::vector< PPDKey * > value_type
Definition: ppdparser.hxx:126
const ::std::vector< PPDConstraint > & getConstraints() const
Definition: ppdparser.hxx:189
void parseConstraint(const OString &rLine)
bool getPaperDimension(std::u16string_view rPaperName, int &rWidth, int &rHeight) const
bool m_bType42Capable
Definition: ppdparser.hxx:148
OUString getDefaultPaperDimension() const
sal_uLong m_nLanguageLevel
Definition: ppdparser.hxx:149
int getKeys() const
Definition: ppdparser.hxx:186
std::unique_ptr< PPDTranslator > m_pTranslator
Definition: ppdparser.hxx:165
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:145
const PPDValue * m_pDefaultInputSlot
Definition: ppdparser.hxx:160
const PPDKey * getKey(int n) const
void parseOrderDependency(const OString &rLine)
sal_uLong getLanguageLevel() const
Definition: ppdparser.hxx:193
bool hasKey(const PPDKey *) const
hash_type m_aKeys
Definition: ppdparser.hxx:140
static void initPPDFiles(PPDCache &rPPDCache)
PPDParser(OUString aFile)
const PPDKey * m_pImageableAreas
Definition: ppdparser.hxx:155
static OUString getPPDFile(const OUString &rFile)
bool isColorDevice() const
Definition: ppdparser.hxx:191
const PPDKey * getKey(const OUString &rKey) const
std::unordered_map< OUString, std::unique_ptr< PPDKey > > hash_type
Definition: ppdparser.hxx:125
void parseOpenUI(const OString &rLine, std::string_view rPPDGroup)
bool isType42Capable() const
Definition: ppdparser.hxx:192
friend class PPDCache
Definition: ppdparser.hxx:123
OUString getDefaultInputSlot() const
OUString matchPaper(int nWidth, int nHeight) const
static void getResolutionFromString(std::u16string_view, int &, int &)
const PPDValue * m_pDefaultResolution
Definition: ppdparser.hxx:162
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
PPDValueType
Definition: ppdparser.hxx:42
@ eNo
Definition: ppdparser.hxx:42
@ eString
Definition: ppdparser.hxx:42
@ eSymbol
Definition: ppdparser.hxx:42
@ eInvocation
Definition: ppdparser.hxx:42
@ eQuoted
Definition: ppdparser.hxx:42
sal_uIntPtr sal_uLong
size_t operator()(const PPDKey *pKey) const
Definition: ppdparser.hxx:109
const PPDValue * m_pOption2
Definition: ppdparser.hxx:135
const PPDValue * m_pOption1
Definition: ppdparser.hxx:133
bool m_bCustomOption
Definition: ppdparser.hxx:50
OUString m_aOption
Definition: ppdparser.hxx:53
bool m_bCustomOptionSetViaApp
Definition: ppdparser.hxx:51
OUString m_aCustomOption
Definition: ppdparser.hxx:52
OUString m_aValue
Definition: ppdparser.hxx:54
PPDValueType m_eType
Definition: ppdparser.hxx:46