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 .
20 #ifndef INCLUDED_SC_SOURCE_FILTER_INC_FTOOLS_HXX
21 #define INCLUDED_SC_SOURCE_FILTER_INC_FTOOLS_HXX
25 #include <tools/ref.hxx>
28 // Common macros ==============================================================
30 // items and item sets --------------------------------------------------------
32 /** Expands to the item (with type 'itemtype') with Which-ID 'which'. */
33 #define GETITEM( itemset, itemtype, which ) \
34 static_cast< const itemtype & >( (itemset).Get( which ) )
36 /** Expands to the value of the SfxBoolItem with Which-ID 'which'. */
37 #define GETITEMBOOL( itemset, which ) \
38 (static_cast<const SfxBoolItem &>( (itemset).Get( which )).GetValue() )
40 // Global static helpers ======================================================
42 // Value range limit helpers --------------------------------------------------
44 /** Returns the value, if it is not less than nMin, otherwise nMin. */
45 template< typename ReturnType
, typename Type
>
46 inline ReturnType
llimit_cast( Type nValue
, ReturnType nMin
)
47 { return static_cast< ReturnType
>( ::std::max
< Type
>( nValue
, nMin
) ); }
49 /** Returns the value, if it is not greater than nMax, otherwise nMax. */
50 template< typename ReturnType
, typename Type
>
51 inline ReturnType
ulimit_cast( Type nValue
, ReturnType nMax
)
52 { return static_cast< ReturnType
>( ::std::min
< Type
>( nValue
, nMax
) ); }
54 /** Returns the value, if it fits into ReturnType, otherwise the maximum value of ReturnType. */
55 template< typename ReturnType
, typename Type
>
56 inline ReturnType
ulimit_cast( Type nValue
)
57 { return ulimit_cast( nValue
, ::std::numeric_limits
< ReturnType
>::max() ); }
59 /** Returns the value, if it is not less than nMin and not greater than nMax, otherwise one of the limits. */
60 template< typename ReturnType
, typename Type
>
61 inline ReturnType
limit_cast( Type nValue
, ReturnType nMin
, ReturnType nMax
)
62 { return static_cast< ReturnType
>( ::std::max
< Type
>( ::std::min
< Type
>( nValue
, nMax
), nMin
) ); }
64 /** Returns the value, if it fits into ReturnType, otherwise one of the limits of ReturnType. */
65 template< typename ReturnType
, typename Type
>
66 inline ReturnType
limit_cast( Type nValue
)
67 { return limit_cast( nValue
, ::std::numeric_limits
< ReturnType
>::min(), ::std::numeric_limits
< ReturnType
>::max() ); }
69 // Read from bitfields --------------------------------------------------------
71 /** Returns true, if at least one of the bits set in nMask is set in nBitField. */
72 template< typename Type
>
73 inline bool get_flag( Type nBitField
, Type nMask
)
74 { return (nBitField
& nMask
) != 0; }
76 /** Returns nSet, if at least one bit of nMask is set in nBitField, otherwise nUnset. */
77 template< typename ReturnType
, typename Type
>
78 inline ReturnType
get_flagvalue( Type nBitField
, Type nMask
, ReturnType nSet
, ReturnType nUnset
)
79 { return ::get_flag( nBitField
, nMask
) ? nSet
: nUnset
; }
81 /** Extracts a value from a bit field.
82 @descr Returns in rnRet the data fragment from nBitField, that starts at bit nStartBit
83 (0-based, bit 0 is rightmost) with the width of nBitCount. rnRet will be right-aligned (normalized).
84 For instance: extract_value( n, 0x4321, 8, 4 ) stores 3 in n (value in bits 8-11). */
85 template< typename ReturnType
, typename Type
>
86 inline ReturnType
extract_value( Type nBitField
, sal_uInt8 nStartBit
, sal_uInt8 nBitCount
)
87 { return static_cast< ReturnType
>( ((1UL << nBitCount
) - 1) & (nBitField
>> nStartBit
) ); }
89 // Write to bitfields ---------------------------------------------------------
91 /** Sets or clears (according to bSet) all set bits of nMask in rnBitField. */
92 template< typename Type
>
93 inline void set_flag( Type
& rnBitField
, Type nMask
, bool bSet
= true )
94 { if( bSet
) rnBitField
|= nMask
; else rnBitField
&= ~nMask
; }
96 /** Inserts a value into a bitfield.
97 @descr Inserts the lower nBitCount bits of nValue into rnBitField, starting
98 there at bit nStartBit. Other contents of rnBitField keep unchanged. */
99 template< typename Type
, typename InsertType
>
100 void insert_value( Type
& rnBitField
, InsertType nValue
, sal_uInt8 nStartBit
, sal_uInt8 nBitCount
)
102 unsigned int nMask
= (1U << nBitCount
) - 1;
103 Type nNewValue
= static_cast< Type
>( nValue
& nMask
);
104 rnBitField
= (rnBitField
& ~(nMask
<< nStartBit
)) | (nNewValue
<< nStartBit
);
111 class ScStyleSheetPool
;
114 class SotStorageStream
;
116 /** Contains static methods used anywhere in the filters. */
120 /** We don't want anybody to instantiate this class, since it is just a
121 collection of static items. */
123 ScfTools(const ScfTools
&) = delete;
124 const ScfTools
& operator=(const ScfTools
&) = delete;
126 // *** common methods *** -----------------------------------------------------
128 /** Reads a 10-byte-long-double and converts it to double. */
129 static double ReadLongDouble( SvStream
& rStrm
);
130 /** Returns system text encoding for byte string conversion. */
131 static rtl_TextEncoding
GetSystemTextEncoding();
132 /** Returns a string representing the hexadecimal value of nValue. */
133 static OUString
GetHexStr( sal_uInt16 nValue
);
135 /** Mixes RGB components with given transparence.
136 @param nTrans Foreground transparence (0x00 == full nFore ... 0x80 = full nBack). */
137 static sal_uInt8
GetMixedColorComp( sal_uInt8 nFore
, sal_uInt8 nBack
, sal_uInt8 nTrans
);
138 /** Mixes colors with given transparence.
139 @param nTrans Foreground transparence (0x00 == full rFore ... 0x80 = full rBack). */
140 static Color
GetMixedColor( const Color
& rFore
, const Color
& rBack
, sal_uInt8 nTrans
);
142 // *** conversion of names *** ------------------------------------------------
144 /** Converts a string to a valid Calc defined name or database range name.
145 @descr Defined names in Calc may contain letters, digits (*), underscores, periods (*),
146 colons (*), question marks, and dollar signs.
147 (*) = not allowed at first position. */
148 static OUString
ConvertToScDefinedName( const OUString
& rName
);
150 // *** streams and storages *** -----------------------------------------------
152 /** Tries to open an existing storage with the specified name in the passed storage (read-only). */
153 static tools::SvRef
<SotStorage
> OpenStorageRead( tools::SvRef
<SotStorage
> const & xStrg
, const OUString
& rStrgName
);
154 /** Creates and opens a storage with the specified name in the passed storage (read/write). */
155 static tools::SvRef
<SotStorage
> OpenStorageWrite( tools::SvRef
<SotStorage
> const & xStrg
, const OUString
& rStrgName
);
157 /** Tries to open an existing stream with the specified name in the passed storage (read-only). */
158 static tools::SvRef
<SotStorageStream
> OpenStorageStreamRead( tools::SvRef
<SotStorage
> const & xStrg
, const OUString
& rStrmName
);
159 /** Creates and opens a stream with the specified name in the passed storage (read/write). */
160 static tools::SvRef
<SotStorageStream
> OpenStorageStreamWrite( tools::SvRef
<SotStorage
> const & xStrg
, const OUString
& rStrmName
);
162 // *** item handling *** ------------------------------------------------------
164 /** Returns true, if the passed item set contains the item.
165 @param bDeep true = Searches in parent item sets too. */
166 static bool CheckItem( const SfxItemSet
& rItemSet
, sal_uInt16 nWhichId
, bool bDeep
);
167 /** Returns true, if the passed item set contains at least one of the items.
168 @param pnWhichIds Zero-terminated array of Which-IDs.
169 @param bDeep true = Searches in parent item sets too. */
170 static bool CheckItems( const SfxItemSet
& rItemSet
, const sal_uInt16
* pnWhichIds
, bool bDeep
);
172 /** Puts the item into the passed item set.
173 @descr The item will be put into the item set, if bSkipPoolDef is false,
174 or if the item differs from the default pool item.
175 @param rItemSet The destination item set.
176 @param rItem The item to put into the item set.
177 @param nWhichId The Which-ID to set with the item.
178 @param bSkipPoolDef true = Do not put item if it is equal to pool default; false = Always put the item. */
180 SfxItemSet
& rItemSet
, const SfxPoolItem
& rItem
,
181 sal_uInt16 nWhichId
, bool bSkipPoolDef
);
183 /** Puts the item into the passed item set.
184 @descr The item will be put into the item set, if bSkipPoolDef is false,
185 or if the item differs from the default pool item.
186 @param rItemSet The destination item set.
187 @param rItem The item to put into the item set.
188 @param bSkipPoolDef true = Do not put item if it is equal to pool default; false = Always put the item. */
189 static void PutItem( SfxItemSet
& rItemSet
, const SfxPoolItem
& rItem
, bool bSkipPoolDef
);
191 // *** style sheet handling *** -----------------------------------------------
193 /** Creates and returns a cell style sheet and inserts it into the pool.
194 @descr If the style sheet is already in the pool, another unused style name is used.
195 @param bForceName Controls behaviour, if the style already exists:
196 true = Old existing style will be renamed; false = New style gets another name. */
197 static ScStyleSheet
& MakeCellStyleSheet(
198 ScStyleSheetPool
& rPool
,
199 const OUString
& rStyleName
, bool bForceName
);
200 /** Creates and returns a page style sheet and inserts it into the pool.
201 @descr If the style sheet is already in the pool, another unused style name is used.
202 @param bForceName Controls behaviour, if the style already exists:
203 true = Old existing style will be renamed; false = New style gets another name. */
204 static ScStyleSheet
& MakePageStyleSheet(
205 ScStyleSheetPool
& rPool
,
206 const OUString
& rStyleName
, bool bForceName
);
208 // *** byte string import operations *** --------------------------------------
210 /** Reads and returns a zero terminated byte string and decreases a stream counter. */
211 static OString
read_zeroTerminated_uInt8s_ToOString(SvStream
& rStrm
, sal_Int32
& rnBytesLeft
);
212 /** Reads and returns a zero terminated byte string and decreases a stream counter. */
213 static OUString
read_zeroTerminated_uInt8s_ToOUString(SvStream
& rStrm
, sal_Int32
& rnBytesLeft
, rtl_TextEncoding eTextEnc
)
215 return OStringToOUString(read_zeroTerminated_uInt8s_ToOString(rStrm
, rnBytesLeft
), eTextEnc
);
218 /** Appends a zero terminated byte string. */
219 static void AppendCString( SvStream
& rStrm
, OUString
& rString
, rtl_TextEncoding eTextEnc
);
221 // *** HTML table names <-> named range names *** -----------------------------
223 /** Returns the built-in range name for an HTML document. */
224 static const OUString
& GetHTMLDocName();
225 /** Returns the built-in range name for all HTML tables. */
226 static const OUString
& GetHTMLTablesName();
227 /** Returns the built-in range name for an HTML table, specified by table index. */
228 static OUString
GetNameFromHTMLIndex( sal_uInt32 nIndex
);
229 /** Returns the built-in range name for an HTML table, specified by table name. */
230 static OUString
GetNameFromHTMLName( const OUString
& rTabName
);
232 /** Returns true, if rSource is the built-in range name for an HTML document. */
233 static bool IsHTMLDocName( const OUString
& rSource
);
234 /** Returns true, if rSource is the built-in range name for all HTML tables. */
235 static bool IsHTMLTablesName( const OUString
& rSource
);
236 /** Converts a built-in range name to an HTML table name.
237 @param rSource The string to be determined.
238 @param rName The HTML table name.
239 @return true, if conversion was successful. */
240 static bool GetHTMLNameFromName( const OUString
& rSource
, OUString
& rName
);
243 /** Returns the prefix for table index names. */
244 static const OUString
& GetHTMLIndexPrefix();
245 /** Returns the prefix for table names. */
246 static const OUString
& GetHTMLNamePrefix();
249 // Containers =================================================================
251 typedef ::std::vector
< sal_uInt8
> ScfUInt8Vec
;
252 typedef ::std::vector
< sal_Int16
> ScfInt16Vec
;
253 typedef ::std::vector
< sal_uInt16
> ScfUInt16Vec
;
254 typedef ::std::vector
< sal_Int32
> ScfInt32Vec
;
255 typedef ::std::vector
< sal_uInt32
> ScfUInt32Vec
;
256 typedef ::std::vector
< OUString
> ScfStringVec
;
258 class ScFormatFilterPluginImpl
: public ScFormatFilterPlugin
261 ScFormatFilterPluginImpl();
262 virtual ~ScFormatFilterPluginImpl();
263 // various import filters
264 virtual ErrCode
ScImportLotus123( SfxMedium
&, ScDocument
*, rtl_TextEncoding eSrc
) override
;
265 virtual ErrCode
ScImportQuattroPro(SvStream
* pStream
, ScDocument
*pDoc
) override
;
266 virtual ErrCode
ScImportExcel( SfxMedium
&, ScDocument
*, const EXCIMPFORMAT
) override
;
267 // eFormat == EIF_AUTO -> matching filter is used automatically
268 // eFormat == EIF_BIFF5 -> only Biff5 stream leads to success (even in an Excel97 doc)
269 // eFormat == EIF_BIFF8 -> only Biff8 stream leads to success (only in Excel97 docs)
270 // eFormat == EIF_BIFF_LE4 -> only non-storage files _could_ lead to success
271 virtual ErrCode
ScImportDif( SvStream
&, ScDocument
*, const ScAddress
& rInsPos
,
272 const rtl_TextEncoding eSrc
) override
;
273 virtual ErrCode
ScImportRTF( SvStream
&, const OUString
& rBaseURL
, ScDocument
*, ScRange
& rRange
) override
;
274 virtual ErrCode
ScImportHTML( SvStream
&, const OUString
& rBaseURL
, ScDocument
*, ScRange
& rRange
,
275 double nOutputFactor
, bool bCalcWidthHeight
,
276 SvNumberFormatter
* pFormatter
, bool bConvertDate
) override
;
278 virtual std::unique_ptr
<ScEEAbsImport
> CreateRTFImport( ScDocument
* pDoc
, const ScRange
& rRange
) override
;
279 virtual std::unique_ptr
<ScEEAbsImport
> CreateHTMLImport( ScDocument
* pDocP
, const OUString
& rBaseURL
, const ScRange
& rRange
) override
;
280 virtual OUString
GetHTMLRangeNameList( ScDocument
* pDoc
, const OUString
& rOrigName
) override
;
282 // various export filters
283 virtual ErrCode
ScExportExcel5( SfxMedium
&, ScDocument
*, ExportFormatExcel eFormat
, rtl_TextEncoding eDest
) override
;
284 virtual void ScExportDif( SvStream
&, ScDocument
*, const ScAddress
& rOutPos
, const rtl_TextEncoding eDest
) override
;
285 virtual void ScExportDif( SvStream
&, ScDocument
*, const ScRange
& rRange
, const rtl_TextEncoding eDest
) override
;
286 virtual void ScExportHTML( SvStream
&, const OUString
& rBaseURL
, ScDocument
*, const ScRange
& rRange
, const rtl_TextEncoding eDest
, bool bAll
,
287 const OUString
& rStreamPath
, OUString
& rNonConvertibleChars
, const OUString
& rFilterOptions
) override
;
288 virtual void ScExportRTF( SvStream
&, ScDocument
*, const ScRange
& rRange
, const rtl_TextEncoding eDest
) override
;
290 virtual ScOrcusFilters
* GetOrcusFilters() override
;
295 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */