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 <string_view>
27 #include <unordered_map>
30 #include <rtl/string.hxx>
31 #include <rtl/ustring.hxx>
32 #include <tools/solar.h>
33 #include <vcl/dllapi.h>
35 #define PRINTER_PPDDIR "driver"
39 enum class orientation
;
44 enum PPDValueType
{ eInvocation
, eQuoted
, eSymbol
, eString
, eNo
};
46 struct VCL_DLLPUBLIC PPDValue
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
53 mutable bool m_bCustomOptionSetViaApp
;
54 mutable OUString m_aCustomOption
;
61 * PPDKey - a container for the available options (=values) of a PPD keyword
66 friend class PPDParser
;
67 friend class CPDManager
;
69 typedef std::unordered_map
< OUString
, PPDValue
> hash_type
;
70 typedef std::vector
< PPDValue
* > value_type
;
74 value_type m_aOrderedValues
;
75 const PPDValue
* m_pDefaultValue
;
80 enum class SetupType
{ ExitServer
, Prolog
, DocumentSetup
, PageSetup
, JCLSetup
, AnySetup
};
84 int m_nOrderDependency
;
85 SetupType m_eSetupType
;
87 void eraseValue( const OUString
& rOption
);
89 PPDKey( OUString aKey
);
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
; }
102 const OUString
& getKey() const { return m_aKey
; }
103 bool isUIKey() const { return m_bUIOption
; }
104 SetupType
getSetupType() const { return m_eSetupType
; }
105 int getOrderDependency() const { return m_nOrderDependency
; }
108 // define a hash for PPDKey
111 size_t operator()( const PPDKey
* pKey
) const
112 { return reinterpret_cast<size_t>(pKey
); }
116 * PPDParser - parses a PPD file and contains all available keys from it
121 friend class PPDContext
;
122 friend class CUPSManager
;
123 friend class CPDManager
;
124 friend class PPDCache
;
126 typedef std::unordered_map
< OUString
, std::unique_ptr
<PPDKey
> > hash_type
;
127 typedef std::vector
< PPDKey
* > value_type
;
129 void insertKey( std::unique_ptr
<PPDKey
> pKey
);
133 const PPDKey
* m_pKey1
;
134 const PPDValue
* m_pOption1
;
135 const PPDKey
* m_pKey2
;
136 const PPDValue
* m_pOption2
;
138 PPDConstraint() : m_pKey1( nullptr ), m_pOption1( nullptr ), m_pKey2( nullptr ), m_pOption2( nullptr ) {}
142 value_type m_aOrderedKeys
;
143 ::std::vector
< PPDConstraint
> m_aConstraints
;
145 // the full path of the PPD file
147 // some basic attributes
149 bool m_bType42Capable
;
150 sal_uLong m_nLanguageLevel
;
151 rtl_TextEncoding m_aFileEncoding
;
154 // shortcuts to important keys and their default values
156 const PPDKey
* m_pImageableAreas
;
158 const PPDValue
* m_pDefaultPaperDimension
;
159 const PPDKey
* m_pPaperDimensions
;
161 const PPDValue
* m_pDefaultInputSlot
;
163 const PPDValue
* m_pDefaultResolution
;
166 std::unique_ptr
<PPDTranslator
> m_pTranslator
;
168 PPDParser( OUString aFile
);
169 PPDParser(OUString aFile
, const std::vector
<PPDKey
*>& keys
);
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
);
176 OUString
handleTranslation(const OString
& i_rString
, bool i_bIsGlobalized
);
178 static void scanPPDDir( const OUString
& rDir
);
179 static void initPPDFiles(PPDCache
&rPPDCache
);
180 static OUString
getPPDFile( const OUString
& rFile
);
182 OUString
matchPaperImpl(int nWidth
, int nHeight
, bool bDontSwap
= false, psp::orientation
* pOrientation
= nullptr) const;
186 static const PPDParser
* getParser( const OUString
& rFile
);
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;
193 const ::std::vector
< PPDConstraint
>& getConstraints() const { return m_aConstraints
; }
195 bool isColorDevice() const { return m_bColorDevice
; }
196 bool isType42Capable() const { return m_bType42Capable
; }
197 sal_uLong
getLanguageLevel() const { return m_nLanguageLevel
; }
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
207 // match the best paper for width and height
208 OUString
matchPaper( int nWidth
, int nHeight
, psp::orientation
* pOrientation
= nullptr ) const;
210 bool getMargins( std::u16string_view rPaperName
,
211 int &rLeft
, int& rRight
,
212 int &rUpper
, int& rLower
) const;
214 // returns true if paper found
218 OUString
getDefaultInputSlot() const;
220 void getDefaultResolution( int& rXRes
, int& rYRes
) const;
222 static void getResolutionFromString( std::u16string_view
, int&, int& );
225 OUString
translateKey( const OUString
& i_rKey
) const;
226 OUString
translateOption( std::u16string_view i_rKey
,
227 const OUString
& i_rOption
) const;
232 * PPDContext - a class to manage user definable states based on the
233 * contents of a PPDParser.
238 typedef std::unordered_map
< const PPDKey
*, const PPDValue
*, PPDKeyhash
> hash_type
;
239 hash_type m_aCurrentValues
;
240 const PPDParser
* m_pParser
;
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 );
248 PPDContext( const PPDContext
& rContext
) { operator=( rContext
); }
249 PPDContext
& operator=( const PPDContext
& rContext
) = default;
250 PPDContext
& operator=( PPDContext
&& rContext
);
252 void setParser( const PPDParser
* );
253 const PPDParser
* getParser() const { return m_pParser
; }
255 const PPDValue
* getValue( const PPDKey
* ) const;
256 const PPDValue
* setValue( const PPDKey
*, const PPDValue
*, bool bDontCareForConstraints
= false );
258 std::size_t countValuesModified() const { return m_aCurrentValues
.size(); }
259 const PPDKey
* getModifiedKey( std::size_t n
) const;
261 // public wrapper for the private method
262 bool checkConstraints( const PPDKey
*, const PPDValue
* );
265 char* getStreamableBuffer( sal_uLong
& rBytes
) const;
266 void rebuildFromStreamBuffer(const std::vector
<char> &rBuffer
);
269 int getRenderResolution() const;
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;
278 #endif // INCLUDED_VCL_PPDPARSER_HXX
280 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */