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
23 #include <unordered_map>
26 #include <rtl/string.hxx>
27 #include <rtl/ustring.hxx>
28 #include <tools/solar.h>
29 #include <vcl/dllapi.h>
31 #include <com/sun/star/lang/Locale.hpp>
33 #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
60 class VCL_DLLPUBLIC PPDKey
62 friend class PPDParser
;
64 typedef std::unordered_map
< OUString
, PPDValue
, OUStringHash
> hash_type
;
65 typedef std::vector
< PPDValue
* > value_type
;
69 value_type m_aOrderedValues
;
70 const PPDValue
* m_pDefaultValue
;
72 PPDValue m_aQueryValue
;
76 enum UIType
{ PickOne
, PickMany
, Boolean
};
77 enum class SetupType
{ ExitServer
, Prolog
, DocumentSetup
, PageSetup
, JCLSetup
, AnySetup
};
82 int m_nOrderDependency
;
83 SetupType m_eSetupType
;
85 void eraseValue( const OUString
& rOption
);
87 PPDKey( const OUString
& rKey
);
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
; }
100 const OUString
& getKey() const { return m_aKey
; }
101 bool isUIKey() const { return m_bUIOption
; }
102 SetupType
getSetupType() const { return m_eSetupType
; }
103 int getOrderDependency() const { return m_nOrderDependency
; }
106 // define a hash for PPDKey
109 size_t operator()( const PPDKey
* pKey
) const
110 { return reinterpret_cast<size_t>(pKey
); }
115 * PPDParser - parses a PPD file and contains all available keys from it
121 class VCL_DLLPUBLIC PPDParser
123 friend class PPDContext
;
124 friend class CUPSManager
;
125 friend class PPDCache
;
127 typedef std::unordered_map
< OUString
, PPDKey
*, OUStringHash
> hash_type
;
128 typedef std::vector
< PPDKey
* > value_type
;
130 void insertKey( const OUString
& rKey
, PPDKey
* pKey
);
134 const PPDKey
* m_pKey1
;
135 const PPDValue
* m_pOption1
;
136 const PPDKey
* m_pKey2
;
137 const PPDValue
* m_pOption2
;
139 PPDConstraint() : m_pKey1( nullptr ), m_pOption1( nullptr ), m_pKey2( nullptr ), m_pOption2( nullptr ) {}
143 value_type m_aOrderedKeys
;
144 ::std::list
< PPDConstraint
> m_aConstraints
;
146 // some identifying fields
147 OUString m_aPrinterName
;
148 OUString m_aNickName
;
149 // the full path of the PPD file
151 // some basic attributes
153 bool m_bType42Capable
;
154 sal_uLong m_nLanguageLevel
;
155 rtl_TextEncoding m_aFileEncoding
;
158 // shortcuts to important keys and their default values
160 const PPDValue
* m_pDefaultImageableArea
;
161 const PPDKey
* m_pImageableAreas
;
163 const PPDValue
* m_pDefaultPaperDimension
;
164 const PPDKey
* m_pPaperDimensions
;
166 const PPDValue
* m_pDefaultInputSlot
;
167 const PPDKey
* m_pInputSlots
;
169 const PPDValue
* m_pDefaultResolution
;
170 const PPDKey
* m_pResolutions
;
172 const PPDValue
* m_pDefaultDuplexType
;
173 const PPDKey
* m_pDuplexTypes
;
176 const PPDKey
* m_pFontList
;
179 PPDTranslator
* m_pTranslator
;
181 PPDParser( const OUString
& rFile
);
184 void parseOrderDependency(const OString
& rLine
);
185 void parseOpenUI(const OString
& rLine
, const OString
& rPPDGroup
);
186 void parseConstraint(const OString
& rLine
);
187 void parse( std::list
< OString
>& rLines
);
189 OUString
handleTranslation(const OString
& i_rString
, bool i_bIsGlobalized
);
191 static void scanPPDDir( const OUString
& rDir
);
192 static void initPPDFiles(PPDCache
&rPPDCache
);
193 static OUString
getPPDFile( const OUString
& rFile
);
195 static const PPDParser
* getParser( const OUString
& rFile
);
197 const PPDKey
* getKey( int n
) const;
198 const PPDKey
* getKey( const OUString
& rKey
) const;
199 int getKeys() const { return m_aKeys
.size(); }
200 bool hasKey( const PPDKey
* ) const;
202 const ::std::list
< PPDConstraint
>& getConstraints() const { return m_aConstraints
; }
204 bool isColorDevice() const { return m_bColorDevice
; }
205 bool isType42Capable() const { return m_bType42Capable
; }
206 sal_uLong
getLanguageLevel() const { return m_nLanguageLevel
; }
208 OUString
getDefaultPaperDimension() const;
209 void getDefaultPaperDimension( int& rWidth
, int& rHeight
) const
210 { getPaperDimension( getDefaultPaperDimension(), rWidth
, rHeight
); }
211 bool getPaperDimension( const OUString
& rPaperName
,
212 int& rWidth
, int& rHeight
) const;
213 // width and height in pt
214 // returns false if paper not found
216 // match the best paper for width and height
217 OUString
matchPaper( int nWidth
, int nHeight
) const;
219 bool getMargins( const OUString
& rPaperName
,
220 int &rLeft
, int& rRight
,
221 int &rUpper
, int& rLower
) const;
223 // returns true if paper found
227 OUString
getDefaultInputSlot() const;
229 void getDefaultResolution( int& rXRes
, int& rYRes
) const;
231 static void getResolutionFromString( const OUString
&, int&, int& );
234 OUString
translateKey( const OUString
& i_rKey
) const;
235 OUString
translateOption( const OUString
& i_rKey
,
236 const OUString
& i_rOption
) const;
241 * PPDContext - a class to manage user definable states based on the
242 * contents of a PPDParser.
245 class VCL_DLLPUBLIC PPDContext
247 typedef std::unordered_map
< const PPDKey
*, const PPDValue
*, PPDKeyhash
> hash_type
;
248 hash_type m_aCurrentValues
;
249 const PPDParser
* m_pParser
;
251 // returns false: check failed, new value is constrained
252 // true: check succeeded, new value can be set
253 bool checkConstraints( const PPDKey
*, const PPDValue
*, bool bDoReset
);
254 bool resetValue( const PPDKey
*, bool bDefaultable
= false );
257 PPDContext( const PPDContext
& rContext
) { operator=( rContext
); }
258 PPDContext
& operator=( const PPDContext
& rContext
);
259 PPDContext
& operator=( PPDContext
&& rContext
);
262 void setParser( const PPDParser
* );
263 const PPDParser
* getParser() const { return m_pParser
; }
265 const PPDValue
* getValue( const PPDKey
* ) const;
266 const PPDValue
* setValue( const PPDKey
*, const PPDValue
*, bool bDontCareForConstraints
= false );
268 int countValuesModified() const { return m_aCurrentValues
.size(); }
269 const PPDKey
* getModifiedKey( int n
) const;
271 // public wrapper for the private method
272 bool checkConstraints( const PPDKey
*, const PPDValue
* );
275 char* getStreamableBuffer( sal_uLong
& rBytes
) const;
276 void rebuildFromStreamBuffer( char* pBuffer
, sal_uLong nBytes
);
279 int getRenderResolution() const;
281 // width, height in points, paper will contain the name of the selected
282 // paper after the call
283 void getPageSize( OUString
& rPaper
, int& rWidth
, int& rHeight
) const;
288 #endif // INCLUDED_VCL_PPDPARSER_HXX
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */