xref: /aoo42x/main/sw/source/filter/inc/msfilter.hxx (revision 86e1cf34)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil -*- */
24 /// @HTML
25 
26 #ifndef SW_MS_MSFILTER_HXX
27 #define SW_MS_MSFILTER_HXX
28 
29 #include <set>
30 #include <map>
31 #include <vector>
32 #   include <swtypes.hxx>       //SwTwips
33 #   include <tools/string.hxx>  //String
34 #   include "wwstyles.hxx"      //ww::sti
35 #   include <rtl/textenc.h>     //rtl_TextEncoding
36 #   include <tools/gen.hxx>     //Size
37 #include <tools/datetime.hxx>
38 #include <fltshell.hxx>         // fuer den Attribut Stack
39 #include <redline.hxx>
40 #include <shellio.hxx>
41 #include <svl/zforlist.hxx>
42 
43 #define CREATE_CONST_ASC(s) String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM(s))
44 
45 class SwDoc;
46 class SwPaM;
47 class String;
48 class SwTableNode;
49 class SwNodeIndex;
50 class SwNoTxtNode;
51 class SwTxtNode;
52 class WW8TabDesc;
53 
54 namespace myImplHelpers
55 {
56 template<class C> class StyleMapperImpl;
57 }
58 
59 class SwTxtFmtColl;
60 class SwCharFmt;
61 typedef myImplHelpers::StyleMapperImpl<SwTxtFmtColl> ParaMapper;
62 typedef myImplHelpers::StyleMapperImpl<SwCharFmt> CharMapper;
63 
64 namespace sw
65 {
66     namespace ms
67     {
68         /** MSOffice appears to set the charset of unicode fonts to MS 932
69 
70             Arial Unicode MS for example is a unicode font, but word sets
71             exported uses of it to the MS 932 charset
72 
73             @param eTextEncoding
74                 the OOo encoding to convert from
75 
76             @return
77                 a msoffice equivalent charset identifier
78 
79             @author
80                 <a href="mailto:cmc@openoffice.org">Caol&aacute;n McNamara</a>
81         */
82 	    sal_uInt8 rtl_TextEncodingToWinCharset(rtl_TextEncoding eTextEncoding);
83 
84         /** Import a MSWord XE field. Suitable for .doc and .rtf
85 
86             @param rDoc
87                 the document to insert into
88 
89             @param rPaM
90                 the position in the document to insert into
91 
92             @param rXE
93                 the arguments of the original word XE field
94 
95             @author
96                 <a href="mailto:cmc@openoffice.org">Caol&aacute;n McNamara</a>
97         */
98 	    void ImportXE(SwDoc &rDoc, SwPaM &rPaM, const String &rXE);
99 
100         /** Convert from DTTM to Writer's DateTime
101 
102             @author
103                 <a href="mailto:mmaher@openoffice.org">Martin Maher</a
104         */
105         DateTime DTTM2DateTime( long lDTTM );
106 
107         /** Convert from DTTM to Writer's DateTime
108 
109             @author
110                 <a href="mailto:mmaher@openoffice.org">Martin Maher</a
111         */
112         long DateTime2DTTM( const DateTime& rDT );
113 
114         /** Convert from Word Date/Time field str to Writer's Date Time str
115 
116             @author
117                 <a href="mailto:mmaher@openoffice.org">Martin Maher</a
118         */
119         sal_uLong MSDateTimeFormatToSwFormat(String& rParams, SvNumberFormatter *pFormatter, sal_uInt16 &rLang, bool bHijri);
120 		/*Used to identify if the previous is AM time field*/
121 		sal_Bool IsPreviousAM(String& rParams, xub_StrLen nPos);
122 
123 		/*Used to identify if the next is PM time field*/
124 		sal_Bool IsNextPM(String& rParams, xub_StrLen nPos);
125 
126         /** Used by MSDateTimeFormatToSwFormat to identify AM time fields
127 
128             @author
129                 <a href="mailto:mmaher@openoffice.org">Martin Maher</a
130         */
131         bool IsNotAM(String& rParams, xub_StrLen nPos);
132 
133         /** Another function used by MSDateTimeFormatToSwFormat
134 
135             @author
136                 <a href="mailto:mmaher@openoffice.org">Martin Maher</a
137         */
138         void SwapQuotesInField(String &rFmt);
139 
140     }
141 
142     namespace util
143     {
144         struct AuthorInfo;
145         typedef AuthorInfo* AuthorInfo_Ptr;
146 
147         /// Redlining Authors
148         struct AuthorInfo
149         {
150             sal_uInt16 nWWAuthorId;
151             sal_uInt16 nOurId;
152 
AuthorInfosw::util::AuthorInfo153             AuthorInfo(sal_uInt16 nWWAuthorId_, sal_uInt16 nOurId_ = 0):
154                 nWWAuthorId( nWWAuthorId_ ),
155                 nOurId(      nOurId_ )
156                 {}
operator ==sw::util::AuthorInfo157             bool operator==(const AuthorInfo& rEntry) const
158             {
159                 return (nWWAuthorId == rEntry.nWWAuthorId);
160             }
operator <sw::util::AuthorInfo161             bool operator<(const AuthorInfo& rEntry) const
162             {
163                 return (nWWAuthorId < rEntry.nWWAuthorId);
164             }
165         };
166 
167         SV_DECL_PTRARR_SORT_DEL(AuthorInfos, AuthorInfo_Ptr,16,16)
168 
169         /** Clips a value to MAX/MIN 16bit value to make it safe for use
170             as a position value to give to writer. i.e. +-57.8cm. Sometimes
171             we see ridiculous values for positioning in rtf and word document,
172             this captures such ones and clips them to values which are
173             still outside the document, but of a value that doesn't cause
174             problems for writer's layout, e.g. see
175             http://www.openoffice.org/issues/show_bug.cgi?id=i9245
176 
177             @param nIn
178 
179             @return nIn clipped to min/max 16bit value
180 
181             @author
182                 <a href="mailto:cmc@openoffice.org">Caol&aacute;n McNamara</a>
183         */
184         SwTwips MakeSafePositioningValue(SwTwips nIn);
185 
186         /** Knows which writer style a given word style should be imported as
187 
188             Mapping a word style to a writer style has to consider mapping
189             the word builtin styles like "Normal" as the default root style
190             to our default root style which is called "Default" in english,
191             and "Normal" in german.
192 
193             Additionally it then has to avoid name collisions such as
194 
195             a) styles "Normal" and "Default" in a single document, where
196             we can use the original distinct names "Normal" and "Default" and..
197             b) styles "Normal" and "Default" in a single document, where
198             we can not use the original names, and must come up with an
199             alternative name for one of them..
200 
201             And it needs to report to the importer if the style being mapped to
202             was already in existence, for the cut and paste/insert file mode we
203             should not modify the returned style if it is already in use as it
204             is does not belong to us to change.
205 
206             @author
207                 <a href="mailto:cmc@openoffice.org">Caol&aacute;n McNamara</a>
208         */
209         class ParaStyleMapper
210         {
211         private:
212             //I hate these things stupid pImpl things, but its warranted here
213             ParaMapper *mpImpl;
214         public:
215             ParaStyleMapper(SwDoc &rDoc);
216             ~ParaStyleMapper();
217 
218             /** StyleResult
219                 StyleResult is a std::pair of a pointer to a style and a flag
220                 which is true if the style existed previously in the document.
221             */
222             typedef std::pair<SwTxtFmtColl*, bool> StyleResult;
223 
224             /** Get the writer style which the word style should map to
225 
226                 @param rName
227                 The name of the word style
228 
229                 @param eSti
230                 The style id of the word style, we are really only interested
231                 in knowing if the style has either a builtin standard id, or is
232                 a user defined style.
233 
234                 @return
235                 The equivalent writer style packaged as a StyleResult to use
236                 for this word style.
237 
238                 It will only return a failure in the pathological case of
239                 catastropic failure elsewhere of there exist already styles
240                 rName and WW-rName[0..SAL_MAX_INT32], which is both unlikely
241                 and impossible.
242             */
243             StyleResult GetStyle(const String& rName, ww::sti eSti);
244         };
245 
246         /** Knows which writer style a given word style should be imported as
247 
248             Mapping a word style to a writer style has to consider mapping
249             the word builtin styles like "Normal" as the default root style
250             to our default root style which is called "Default" in english,
251             and "Normal" in german.
252 
253             Additionally it then has to avoid name collisions such as
254 
255             a) styles "Normal" and "Default" in a single document, where
256             we can use the original distinct names "Normal" and "Default" and..
257             b) styles "Normal" and "Default" in a single document, where
258             we can not use the original names, and must come up with an
259             alternative name for one of them..
260 
261             And it needs to report to the importer if the style being mapped to
262             was already in existence, for the cut and paste/insert file mode we
263             should not modify the returned style if it is already in use as it
264             is does not belong to us to change.
265 
266             @author
267                 <a href="mailto:cmc@openoffice.org">Caol&aacute;n McNamara</a>
268         */
269         class CharStyleMapper
270         {
271         private:
272             //I hate these things stupid pImpl things, but its warranted here
273             CharMapper *mpImpl;
274         public:
275             CharStyleMapper(SwDoc &rDoc);
276             ~CharStyleMapper();
277 
278             /** StyleResult
279                 StyleResult is a std::pair of a pointer to a style and a flag
280                 which is true if the style existed previously in the document.
281             */
282             typedef std::pair<SwCharFmt*, bool> StyleResult;
283 
284             /** Get the writer style which the word style should map to
285 
286                 @param rName
287                 The name of the word style
288 
289                 @param eSti
290                 The style id of the word style, we are really only interested
291                 in knowing if the style has either a builtin standard id, or is
292                 a user defined style.
293 
294                 @return
295                 The equivalent writer style packaged as a StyleResult to use
296                 for this word style.
297 
298                 It will only return a failure in the pathological case of
299                 catastropic failure elsewhere of there exist already styles
300                 rName and WW-rName[0..SAL_MAX_INT32], which is both unlikely
301                 and impossible.
302             */
303             StyleResult GetStyle(const String& rName, ww::sti eSti);
304         };
305 
306         /** Find suitable names for exporting this font
307 
308             Given a fontname description find the best primary and secondary
309             fallback font to use from MSWord's persp font
310 
311             @author
312             <a href="mailto:cmc@openoffice.org">Caol&aacute;n McNamara</a>
313 
314             @see #i10242#/#i19164# for examples
315         */
316         class FontMapExport
317         {
318         public:
319             String msPrimary;
320             String msSecondary;
321             bool HasDistinctSecondary() const;
322             FontMapExport(const String &rFontDescription);
323         };
324 
325         class InsertedTableClient : public SwClient
326         {
327         public:
328             InsertedTableClient(SwTableNode & rNode);
329             SwTableNode * GetTableNode();
330         };
331 
332         /** Handle requirements for table formatting in insert->file mode.
333 
334             When inserting a table into a document which already has been
335             formatted and laid out (e.g using insert->file) then tables
336             must be handled in a special way, (or so previous comments and
337             code in the filters leads me to believe).
338 
339             Before the document is finalized the new tables need to have
340             their layout frms deleted and recalculated. This TableManager
341             detects the necessity to do this, and all tables inserted into
342             a document should be registered with this manager with
343             InsertTable, and before finialization DelAndMakeTblFrms should
344             be called.
345 
346             @author
347             <a href="mailto:cmc@openoffice.org">Caol&aacute;n McNamara</a>
348 
349             @see #i25782# for examples
350         */
351         class InsertedTablesManager
352         {
353         public:
354             typedef std::map<InsertedTableClient *, SwNodeIndex *> TblMap;
355             typedef TblMap::iterator TblMapIter;
356             void DelAndMakeTblFrms();
357             void InsertTable(SwTableNode &rTableNode, SwPaM &rPaM);
358             InsertedTablesManager(const SwDoc &rDoc);
359         private:
360             bool mbHasRoot;
361             TblMap maTables;
362         };
363 
364         /**
365             @author
366                 <a href="mailto:mmaher@openoffice.org">Martin Maher</a>
367          */
368         class RedlineStack
369         {
370         private:
371             std::vector<SwFltStackEntry *> maStack;
372             typedef std::vector<SwFltStackEntry *>::reverse_iterator myriter;
373             SwDoc &mrDoc;
374         public:
RedlineStack(SwDoc & rDoc)375             explicit RedlineStack(SwDoc &rDoc) : mrDoc(rDoc) {}
376             void open(const SwPosition& rPos, const SfxPoolItem& rAttr);
377             bool close(const SwPosition& rPos, RedlineType_t eType);
378             void close(const SwPosition& rPos, RedlineType_t eType,
379                 WW8TabDesc* pTabDesc );
380             void closeall(const SwPosition& rPos);
381             ~RedlineStack();
382         private:
383             //No copying
384             RedlineStack(const RedlineStack&);
385             RedlineStack& operator=(const RedlineStack&);
386         };
387 
388         /**
389             @author
390                 <a href="mailto:mmaher@openoffice.org">Martin Maher</a>
391          */
392         class SetInDocAndDelete
393         {
394         private:
395             SwDoc &mrDoc;
396         public:
SetInDocAndDelete(SwDoc & rDoc)397             explicit SetInDocAndDelete(SwDoc &rDoc) : mrDoc(rDoc) {}
398             void operator()(SwFltStackEntry *pEntry);
399         private:
400             //No assignment
401             SetInDocAndDelete& operator=(const SetInDocAndDelete&);
402         };
403 
404         /**
405             @author
406                 <a href="mailto:mmaher@openoffice.org">Martin Maher</a>
407          */
408         class CloseIfOpen       //Subclass from something ?
409         {
410         private:
411             const SwPosition &mrPos;
412         public:
CloseIfOpen(const SwPosition & rPos)413             explicit CloseIfOpen(const SwPosition &rPos) : mrPos(rPos) {}
operator ()(SwFltStackEntry * pEntry) const414                 void operator()(SwFltStackEntry *pEntry) const
415             {
416                 if (pEntry->bLocked)
417                     pEntry->SetEndPos(mrPos);
418             }
419         private:
420             //No assignment
421             CloseIfOpen& operator=(const CloseIfOpen&);
422         };
423 
424         /**
425             @author
426                 <a href="mailto:mmaher@openoffice.org">Martin Maher</a>
427          */
428         class CompareRedlines:
429             public std::binary_function<const SwFltStackEntry*, const SwFltStackEntry*,
430             bool>
431         {
432         public:
433             bool operator()(const SwFltStackEntry *pOneE, const SwFltStackEntry *pTwoE)
434                 const;
435         };
436 
437         class WrtRedlineAuthor
438         {
439         protected:
440             std::vector<String> maAuthors;          // Array of Sw - Bookmarknames
441 
442             sal_uInt16 GetPos( const String& rNm );
443 
444             //No copying
445             WrtRedlineAuthor(const WrtRedlineAuthor&);
446             WrtRedlineAuthor& operator=(const WrtRedlineAuthor&);
447         public:
WrtRedlineAuthor()448             WrtRedlineAuthor() {}
~WrtRedlineAuthor()449             virtual ~WrtRedlineAuthor() {}
450 
451             sal_uInt16 AddName( const String& rNm );
452             virtual void Write(Writer &rWrt) = 0;
453             // std::vector<String> GetNames();
454         };
455 
456         /** Given a SwNoTxtNode (ole/graphic) get original size
457 
458             Get the uncropped and unscaled size of the underlying graphic or
459             ole object associated with a given SwNoTxtNode.
460 
461             This function will swap in the graphic if it is swapped out from
462             the graphic or object cache, but will swap it out if that was the
463             case, i.e.  rNd is logically unchanged before and after
464             GetSwappedInSize, though not physically const
465 
466             @param rNd
467                 the SwNoTxtNode whose objects original size we want
468 
469             @return
470                 the uncropped unscaled size of the SwNoTxtNode
471 
472             @author
473                 <a href="mailto:cmc@openoffice.org">Caol&aacute;n McNamara</a>
474         */
475         Size GetSwappedInSize(const SwNoTxtNode& rNd);
476 
477         struct CharRunEntry
478         {
479             xub_StrLen mnEndPos;
480             sal_uInt16 mnScript;
481             rtl_TextEncoding meCharSet;
482             bool mbRTL;
CharRunEntrysw::util::CharRunEntry483             CharRunEntry(xub_StrLen nEndPos, sal_uInt16 nScript,
484                 rtl_TextEncoding eCharSet, bool bRTL)
485             : mnEndPos(nEndPos), mnScript(nScript), meCharSet(eCharSet),
486             mbRTL(bRTL)
487             {
488             }
489         };
490 
491         typedef std::vector<CharRunEntry> CharRuns;
492         typedef CharRuns::const_iterator cCharRunIter;
493 
494         /** Collect the ranges of Text which share
495 
496             Word generally requires characters which share the same direction,
497             the same script, and occasionally (depending on the format) the
498             same charset to be exported in independent chunks.
499 
500             So this function finds these ranges and returns a STL container
501             of CharRuns
502 
503             @param rTxtNd
504                 The TextNode we want to ranges from
505 
506             @param nStart
507                 The position in the TxtNode to start processing from
508 
509             @param bSplitOnCharSet
510                 Set to true is we want to split on ranges of characters that
511                 share a plausible charset for export to e.g. WW7- or perhaps
512                 RTF format, not necessary for a unicode aware format like WW8+
513 
514             @return STL container of CharRuns which describe the shared
515             direction, script and optionally script of the contiguous sequences
516             of characters
517 
518             @author
519             <a href="mailto:cmc@openoffice.org">Caol&aacute;n McNamara</a>
520 
521             @see #i22537# for example
522         */
523         CharRuns GetPseudoCharRuns(const SwTxtNode& rTxtNd,
524             xub_StrLen nStart = 0, bool bSplitOnCharSet = false);
525     }
526 }
527 
528 #endif
529 /* vi:set tabstop=4 shiftwidth=4 expandtab: */
530