1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #ifndef INCLUDED_VCL_PPDPARSER_HXX
20 #define INCLUDED_VCL_PPDPARSER_HXX
22 #include <sal/config.h>
26 #include <unordered_map>
29 #include <rtl/string.hxx>
30 #include <rtl/ustring.hxx>
31 #include <tools/solar.h>
32 #include <vcl/dllapi.h>
34 #define PRINTER_PPDDIR "driver"
41 enum PPDValueType
{ eInvocation
, eQuoted
, eSymbol
, eString
, eNo
};
43 struct VCL_DLLPUBLIC PPDValue
46 //CustomOption stuff for fdo#43049
47 //see http://www.cups.org/documentation.php/spec-ppd.html#OPTIONS
48 //for full specs, only the basics are implemented here
50 mutable OUString m_aCustomOption
;
57 * PPDKey - a container for the available options (=values) of a PPD keyword
62 friend class PPDParser
;
63 friend class CPDManager
;
65 typedef std::unordered_map
< OUString
, PPDValue
> hash_type
;
66 typedef std::vector
< PPDValue
* > value_type
;
70 value_type m_aOrderedValues
;
71 const PPDValue
* m_pDefaultValue
;
76 enum class SetupType
{ ExitServer
, Prolog
, DocumentSetup
, PageSetup
, JCLSetup
, AnySetup
};
80 int m_nOrderDependency
;
81 SetupType m_eSetupType
;
83 void eraseValue( const OUString
& rOption
);
85 PPDKey( const OUString
& rKey
);
88 PPDValue
* insertValue(const OUString
& rOption
, PPDValueType eType
, bool bCustomOption
= false);
89 int countValues() const
90 { return m_aValues
.size(); }
91 // neither getValue will return the query option
92 const PPDValue
* getValue( int n
) const;
93 const PPDValue
* getValue( const OUString
& rOption
) const;
94 const PPDValue
* getValueCaseInsensitive( const OUString
& rOption
) const;
95 const PPDValue
* getDefaultValue() const { return m_pDefaultValue
; }
96 const OUString
& getGroup() const { return m_aGroup
; }
98 const OUString
& getKey() const { return m_aKey
; }
99 bool isUIKey() const { return m_bUIOption
; }
100 SetupType
getSetupType() const { return m_eSetupType
; }
101 int getOrderDependency() const { return m_nOrderDependency
; }
104 // define a hash for PPDKey
107 size_t operator()( const PPDKey
* pKey
) const
108 { return reinterpret_cast<size_t>(pKey
); }
113 * PPDParser - parses a PPD file and contains all available keys from it
118 friend class PPDContext
;
119 friend class CUPSManager
;
120 friend class CPDManager
;
121 friend class PPDCache
;
123 typedef std::unordered_map
< OUString
, std::unique_ptr
<PPDKey
> > hash_type
;
124 typedef std::vector
< PPDKey
* > value_type
;
126 void insertKey( std::unique_ptr
<PPDKey
> pKey
);
130 const PPDKey
* m_pKey1
;
131 const PPDValue
* m_pOption1
;
132 const PPDKey
* m_pKey2
;
133 const PPDValue
* m_pOption2
;
135 PPDConstraint() : m_pKey1( nullptr ), m_pOption1( nullptr ), m_pKey2( nullptr ), m_pOption2( nullptr ) {}
139 value_type m_aOrderedKeys
;
140 ::std::vector
< PPDConstraint
> m_aConstraints
;
142 // the full path of the PPD file
144 // some basic attributes
146 bool m_bType42Capable
;
147 sal_uLong m_nLanguageLevel
;
148 rtl_TextEncoding m_aFileEncoding
;
151 // shortcuts to important keys and their default values
153 const PPDKey
* m_pImageableAreas
;
155 const PPDValue
* m_pDefaultPaperDimension
;
156 const PPDKey
* m_pPaperDimensions
;
158 const PPDValue
* m_pDefaultInputSlot
;
160 const PPDValue
* m_pDefaultResolution
;
163 std::unique_ptr
<PPDTranslator
> m_pTranslator
;
165 PPDParser( const OUString
& rFile
);
166 PPDParser(const OUString
& rFile
, const std::vector
<PPDKey
*>& keys
);
168 void parseOrderDependency(const OString
& rLine
);
169 void parseOpenUI(const OString
& rLine
, const OString
& rPPDGroup
);
170 void parseConstraint(const OString
& rLine
);
171 void parse( std::vector
< OString
>& rLines
);
173 OUString
handleTranslation(const OString
& i_rString
, bool i_bIsGlobalized
);
175 static void scanPPDDir( const OUString
& rDir
);
176 static void initPPDFiles(PPDCache
&rPPDCache
);
177 static OUString
getPPDFile( const OUString
& rFile
);
180 static const PPDParser
* getParser( const OUString
& rFile
);
182 const PPDKey
* getKey( int n
) const;
183 const PPDKey
* getKey( const OUString
& rKey
) const;
184 int getKeys() const { return m_aKeys
.size(); }
185 bool hasKey( const PPDKey
* ) const;
187 const ::std::vector
< PPDConstraint
>& getConstraints() const { return m_aConstraints
; }
189 bool isColorDevice() const { return m_bColorDevice
; }
190 bool isType42Capable() const { return m_bType42Capable
; }
191 sal_uLong
getLanguageLevel() const { return m_nLanguageLevel
; }
193 OUString
getDefaultPaperDimension() const;
194 void getDefaultPaperDimension( int& rWidth
, int& rHeight
) const
195 { getPaperDimension( getDefaultPaperDimension(), rWidth
, rHeight
); }
196 bool getPaperDimension( const OUString
& rPaperName
,
197 int& rWidth
, int& rHeight
) const;
198 // width and height in pt
199 // returns false if paper not found
201 // match the best paper for width and height
202 OUString
matchPaper( int nWidth
, int nHeight
) const;
204 bool getMargins( const OUString
& rPaperName
,
205 int &rLeft
, int& rRight
,
206 int &rUpper
, int& rLower
) const;
208 // returns true if paper found
212 OUString
getDefaultInputSlot() const;
214 void getDefaultResolution( int& rXRes
, int& rYRes
) const;
216 static void getResolutionFromString( const OUString
&, int&, int& );
219 OUString
translateKey( const OUString
& i_rKey
) const;
220 OUString
translateOption( const OUString
& i_rKey
,
221 const OUString
& i_rOption
) const;
226 * PPDContext - a class to manage user definable states based on the
227 * contents of a PPDParser.
232 typedef std::unordered_map
< const PPDKey
*, const PPDValue
*, PPDKeyhash
> hash_type
;
233 hash_type m_aCurrentValues
;
234 const PPDParser
* m_pParser
;
236 // returns false: check failed, new value is constrained
237 // true: check succeeded, new value can be set
238 bool checkConstraints( const PPDKey
*, const PPDValue
*, bool bDoReset
);
239 bool resetValue( const PPDKey
*, bool bDefaultable
= false );
242 PPDContext( const PPDContext
& rContext
) { operator=( rContext
); }
243 PPDContext
& operator=( const PPDContext
& rContext
) = default;
244 PPDContext
& operator=( PPDContext
&& rContext
);
246 void setParser( const PPDParser
* );
247 const PPDParser
* getParser() const { return m_pParser
; }
249 const PPDValue
* getValue( const PPDKey
* ) const;
250 const PPDValue
* setValue( const PPDKey
*, const PPDValue
*, bool bDontCareForConstraints
= false );
252 std::size_t countValuesModified() const { return m_aCurrentValues
.size(); }
253 const PPDKey
* getModifiedKey( std::size_t n
) const;
255 // public wrapper for the private method
256 bool checkConstraints( const PPDKey
*, const PPDValue
* );
259 char* getStreamableBuffer( sal_uLong
& rBytes
) const;
260 void rebuildFromStreamBuffer(const std::vector
<char> &rBuffer
);
263 int getRenderResolution() const;
265 // width, height in points, paper will contain the name of the selected
266 // paper after the call
267 void getPageSize( OUString
& rPaper
, int& rWidth
, int& rHeight
) const;
272 #endif // INCLUDED_VCL_PPDPARSER_HXX
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */