xref: /trunk/main/sw/source/filter/ww8/writerwordglue.cxx (revision e32ec1a966a7222d04c4ed2904dd376185cdeba0)
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 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26 /// @HTML
27 #include <msfilter.hxx>
28 #   include "writerwordglue.hxx"
29 #include <doc.hxx>
30 #   include "writerhelper.hxx"
31 
32 #include <algorithm>                //std::find_if
33 
34 #include <unicode/ubidi.h>          //ubidi_getLogicalRun
35 #   include <tools/tenccvt.hxx>     //GetExtendedTextEncoding
36 #   include <i18nutil/unicode.hxx>  //unicode::getUnicodeScriptType
37 #ifndef _COM_SUN_STAR_I18N_SCRIPTTYPE_HDL_
38 #   include <com/sun/star/i18n/ScriptType.hdl> //ScriptType
39 #endif
40 
41 #ifndef SV_FONTCVT_HXX
42 #   include <unotools/fontcvt.hxx>   //GetSubsFontName
43 #endif
44 #   include <editeng/paperinf.hxx>      //lA0Width...
45 #   include <editeng/lrspitem.hxx>      //SvxLRSpaceItem
46 #   include <editeng/ulspitem.hxx>      //SvxULSpaceItem
47 #   include <editeng/boxitem.hxx>       //SvxBoxItem
48 #   include <editeng/fontitem.hxx>      //SvxFontItem
49 #   include <frmfmt.hxx>            //SwFrmFmt
50 #   include <fmtclds.hxx>           //SwFmtCol
51 #   include <hfspacingitem.hxx>     //SwHeaderAndFooterEatSpacingItem
52 #   include <fmtfsize.hxx>          //SwFmtFrmSize
53 #   include <swrect.hxx>            //SwRect
54 #   include <fmthdft.hxx>           //SwFmtHeader/SwFmtFooter
55 #   include <frmatr.hxx>            //GetLRSpace...
56 #   include <ndtxt.hxx>             //SwTxtNode
57 #   include <breakit.hxx>           //pBreakIt
58 
59 #define ASSIGN_CONST_ASC(s) AssignAscii(RTL_CONSTASCII_STRINGPARAM(s))
60 
61 namespace myImplHelpers
62 {
CalcHdFtDist(const SwFrmFmt & rFmt,sal_uInt16 nSpacing)63     SwTwips CalcHdFtDist(const SwFrmFmt& rFmt, sal_uInt16 nSpacing)
64     {
65         /*
66         #98506#
67         The normal case for reexporting word docs is to have dynamic spacing,
68         as this is word's only setting, and the reason for the existence of the
69         dynamic spacing features. If we have dynamic spacing active then we can
70         add its spacing to the value height of the h/f and get the wanted total
71         size for word.
72 
73         Otherwise we have to get the real layout rendered
74         height, which is totally nonoptimum, but the best we can do.
75         */
76         long nDist=0;
77         const SwFmtFrmSize& rSz = rFmt.GetFrmSize();
78 
79         const SwHeaderAndFooterEatSpacingItem &rSpacingCtrl =
80             sw::util::ItemGet<SwHeaderAndFooterEatSpacingItem>
81             (rFmt, RES_HEADER_FOOTER_EAT_SPACING);
82         if (rSpacingCtrl.GetValue())
83             nDist += rSz.GetHeight();
84         else
85         {
86             SwRect aRect(rFmt.FindLayoutRect(false));
87             if (aRect.Height())
88                 nDist += aRect.Height();
89             else
90             {
91                 const SwFmtFrmSize& rSize = rFmt.GetFrmSize();
92                 if (ATT_VAR_SIZE != rSize.GetHeightSizeType())
93                     nDist += rSize.GetHeight();
94                 else
95                 {
96                     nDist += 274;       // default for 12pt text
97                     nDist += nSpacing;
98                 }
99             }
100         }
101         return nDist;
102     }
103 
CalcHdDist(const SwFrmFmt & rFmt)104     SwTwips CalcHdDist(const SwFrmFmt& rFmt)
105     {
106         return CalcHdFtDist(rFmt, rFmt.GetULSpace().GetUpper());
107     }
108 
CalcFtDist(const SwFrmFmt & rFmt)109     SwTwips CalcFtDist(const SwFrmFmt& rFmt)
110     {
111         return CalcHdFtDist(rFmt, rFmt.GetULSpace().GetLower());
112     }
113 
114     /*
115      SwTxtFmtColl and SwCharFmt are quite distinct types and how they are
116      gotten is also distinct, but the algorithm to match word's eqivalents into
117      them is the same, so we put the different stuff into two separate helper
118      implementations and a core template that uses the helpers that uses the
119      same algorithm to do the work. We'll make the helpers specializations of a
120      non existing template so I can let the compiler figure out the right one
121      to use from a simple argument to the algorithm class
122     */
123     template <class C> class MapperImpl;
124     template<> class MapperImpl<SwTxtFmtColl>
125     {
126     private:
127         SwDoc &mrDoc;
128     public:
MapperImpl(SwDoc & rDoc)129         MapperImpl(SwDoc &rDoc) : mrDoc(rDoc) {}
130         SwTxtFmtColl* GetBuiltInStyle(ww::sti eSti);
131         SwTxtFmtColl* GetStyle(const String &rName);
132         SwTxtFmtColl* MakeStyle(const String &rName);
133     };
134 
GetBuiltInStyle(ww::sti eSti)135     SwTxtFmtColl* MapperImpl<SwTxtFmtColl>::GetBuiltInStyle(ww::sti eSti)
136     {
137         const RES_POOL_COLLFMT_TYPE RES_NONE  = RES_POOLCOLL_DOC_END;
138         static const RES_POOL_COLLFMT_TYPE aArr[]=
139         {
140             RES_POOLCOLL_STANDARD, RES_POOLCOLL_HEADLINE1,
141             RES_POOLCOLL_HEADLINE2, RES_POOLCOLL_HEADLINE3,
142             RES_POOLCOLL_HEADLINE4, RES_POOLCOLL_HEADLINE5,
143             RES_POOLCOLL_HEADLINE6, RES_POOLCOLL_HEADLINE7,
144             RES_POOLCOLL_HEADLINE8, RES_POOLCOLL_HEADLINE9,
145             RES_POOLCOLL_TOX_IDX1, RES_POOLCOLL_TOX_IDX2,
146             RES_POOLCOLL_TOX_IDX3, RES_NONE, RES_NONE, RES_NONE, RES_NONE,
147             RES_NONE, RES_NONE, RES_POOLCOLL_TOX_CNTNT1,
148             RES_POOLCOLL_TOX_CNTNT2, RES_POOLCOLL_TOX_CNTNT3,
149             RES_POOLCOLL_TOX_CNTNT4, RES_POOLCOLL_TOX_CNTNT5,
150             RES_POOLCOLL_TOX_CNTNT6, RES_POOLCOLL_TOX_CNTNT7,
151             RES_POOLCOLL_TOX_CNTNT8, RES_POOLCOLL_TOX_CNTNT9, RES_NONE,
152             RES_POOLCOLL_FOOTNOTE, RES_NONE, RES_POOLCOLL_HEADER,
153             RES_POOLCOLL_FOOTER, RES_POOLCOLL_TOX_IDXH, RES_NONE, RES_NONE,
154             RES_POOLCOLL_JAKETADRESS, RES_POOLCOLL_SENDADRESS, RES_NONE,
155             RES_NONE, RES_NONE, RES_NONE, RES_NONE, RES_POOLCOLL_ENDNOTE,
156             RES_NONE, RES_NONE, RES_NONE, RES_POOLCOLL_LISTS_BEGIN,
157             RES_NONE, RES_NONE, RES_NONE, RES_NONE, RES_NONE, RES_NONE,
158             RES_NONE, RES_NONE, RES_NONE, RES_NONE, RES_NONE, RES_NONE,
159             RES_NONE, RES_NONE, RES_POOLCOLL_DOC_TITEL, RES_NONE,
160             RES_POOLCOLL_SIGNATURE, RES_NONE, RES_POOLCOLL_TEXT,
161             RES_POOLCOLL_TEXT_MOVE, RES_NONE, RES_NONE, RES_NONE, RES_NONE,
162             RES_NONE, RES_NONE, RES_POOLCOLL_DOC_SUBTITEL
163         };
164 
165         const size_t nArrSize = (sizeof(aArr) / sizeof(aArr[0]));
166         ASSERT(nArrSize == 75, "Style Array has false size");
167 
168         SwTxtFmtColl* pRet = 0;
169         //If this is a built-in word style that has a built-in writer
170         //equivalent, then map it to one of our built in styles regardless
171         //of its name
172         if (sal::static_int_cast< size_t >(eSti) < nArrSize && aArr[eSti] != RES_NONE)
173             pRet = mrDoc.GetTxtCollFromPool( static_cast< sal_uInt16 >(aArr[eSti]), false);
174         return pRet;
175     }
176 
GetStyle(const String & rName)177     SwTxtFmtColl* MapperImpl<SwTxtFmtColl>::GetStyle(const String &rName)
178     {
179         return sw::util::GetParaStyle(mrDoc, rName);
180     }
181 
MakeStyle(const String & rName)182     SwTxtFmtColl* MapperImpl<SwTxtFmtColl>::MakeStyle(const String &rName)
183     {
184         return mrDoc.MakeTxtFmtColl(rName,
185             const_cast<SwTxtFmtColl *>(mrDoc.GetDfltTxtFmtColl()));
186     }
187 
188     template<> class MapperImpl<SwCharFmt>
189     {
190     private:
191         SwDoc &mrDoc;
192     public:
MapperImpl(SwDoc & rDoc)193         MapperImpl(SwDoc &rDoc) : mrDoc(rDoc) {}
194         SwCharFmt* GetBuiltInStyle(ww::sti eSti);
195         SwCharFmt* GetStyle(const String &rName);
196         SwCharFmt* MakeStyle(const String &rName);
197     };
198 
GetBuiltInStyle(ww::sti eSti)199     SwCharFmt* MapperImpl<SwCharFmt>::GetBuiltInStyle(ww::sti eSti)
200     {
201         RES_POOL_CHRFMT_TYPE eLookup = RES_POOLCHR_NORMAL_END;
202         switch (eSti)
203         {
204             case ww::stiFtnRef:
205                 eLookup = RES_POOLCHR_FOOTNOTE;
206                 break;
207             case ww::stiLnn:
208                 eLookup = RES_POOLCHR_LINENUM;
209                 break;
210             case ww::stiPgn:
211                 eLookup = RES_POOLCHR_PAGENO;
212                 break;
213             case ww::stiEdnRef:
214                 eLookup = RES_POOLCHR_ENDNOTE;
215                 break;
216             case ww::stiHyperlink:
217                 eLookup = RES_POOLCHR_INET_NORMAL;
218                 break;
219             case ww::stiHyperlinkFollowed:
220                 eLookup = RES_POOLCHR_INET_VISIT;
221                 break;
222             case ww::stiStrong:
223                 eLookup = RES_POOLCHR_HTML_STRONG;
224                 break;
225             case ww::stiEmphasis:
226                 eLookup = RES_POOLCHR_HTML_EMPHASIS;
227                 break;
228             default:
229                 eLookup = RES_POOLCHR_NORMAL_END;
230                 break;
231         }
232         SwCharFmt *pRet = 0;
233         if (eLookup != RES_POOLCHR_NORMAL_END)
234             pRet = mrDoc.GetCharFmtFromPool( static_cast< sal_uInt16 >(eLookup) );
235         return pRet;
236     }
237 
GetStyle(const String & rName)238     SwCharFmt* MapperImpl<SwCharFmt>::GetStyle(const String &rName)
239     {
240         return sw::util::GetCharStyle(mrDoc, rName);
241     }
242 
MakeStyle(const String & rName)243     SwCharFmt* MapperImpl<SwCharFmt>::MakeStyle(const String &rName)
244     {
245         return mrDoc.MakeCharFmt(rName, mrDoc.GetDfltCharFmt());
246     }
247 
248     template<class C> class StyleMapperImpl
249     {
250     private:
251         MapperImpl<C> maHelper;
252         std::set<const C*> maUsedStyles;
253         C* MakeNonCollidingStyle(const String& rName);
254     public:
255         typedef std::pair<C*, bool> StyleResult;
StyleMapperImpl(SwDoc & rDoc)256         StyleMapperImpl(SwDoc &rDoc) : maHelper(rDoc) {}
257         StyleResult GetStyle(const String& rName, ww::sti eSti);
258     };
259 
260     template<class C>
261     typename StyleMapperImpl<C>::StyleResult
GetStyle(const String & rName,ww::sti eSti)262     StyleMapperImpl<C>::GetStyle(const String& rName, ww::sti eSti)
263     {
264         C *pRet = maHelper.GetBuiltInStyle(eSti);
265 
266         //If we've used it once, don't reuse it
267         if (pRet && (maUsedStyles.end() != maUsedStyles.find(pRet)))
268             pRet = 0;
269 
270         if (!pRet)
271         {
272             pRet = maHelper.GetStyle(rName);
273             //If we've used it once, don't reuse it
274             if (pRet && (maUsedStyles.end() != maUsedStyles.find(pRet)))
275                 pRet = 0;
276         }
277 
278         bool bStyExist = pRet ? true : false;
279 
280         if (!pRet)
281         {
282             String aName(rName);
283             xub_StrLen nPos = aName.Search(',');
284             // No commas allow in SW style names
285             if (STRING_NOTFOUND != nPos)
286                 aName.Erase(nPos);
287             pRet = MakeNonCollidingStyle(aName);
288         }
289 
290         if (pRet)
291             maUsedStyles.insert(pRet);
292 
293         return StyleResult(pRet, bStyExist);
294     }
295 
296     template<class C>
MakeNonCollidingStyle(const String & rName)297     C* StyleMapperImpl<C>::MakeNonCollidingStyle(const String& rName)
298     {
299         String aName(rName);
300         C* pColl = 0;
301 
302         if (0 != (pColl = maHelper.GetStyle(aName)))
303         {
304             //If the style collides first stick WW- in front of it, unless
305             //it already has it and then successively add a larger and
306             //larger number after it, its got to work at some stage!
307             if (!aName.EqualsIgnoreCaseAscii("WW-", 0, 3))
308                 aName.InsertAscii("WW-" , 0);
309 
310             sal_Int32 nI = 1;
311             while (
312                     0 != (pColl = maHelper.GetStyle(aName)) &&
313                     (nI < SAL_MAX_INT32)
314                   )
315             {
316                 aName += String::CreateFromInt32(nI++);
317             }
318         }
319 
320         return pColl ? 0 : maHelper.MakeStyle(aName);
321     }
322 
FindBestMSSubstituteFont(const String & rFont)323     String FindBestMSSubstituteFont(const String &rFont)
324     {
325         String sRet;
326         if (sw::util::IsStarSymbol(rFont))
327             sRet.ASSIGN_CONST_ASC("Arial Unicode MS");
328         else
329             sRet = GetSubsFontName(rFont, SUBSFONT_ONLYONE | SUBSFONT_MS);
330         return sRet;
331     }
332 
333     /*
334      Utility to categorize unicode characters into the best fit windows charset
335      range for exporting to ww6, or as a hint to non \u unicode token aware rtf
336      readers
337     */
getScriptClass(sal_Unicode cChar)338     rtl_TextEncoding getScriptClass(sal_Unicode cChar)
339     {
340         using namespace ::com::sun::star::i18n;
341 
342         static ScriptTypeList aScripts[] =
343         {
344             { UnicodeScript_kBasicLatin, UnicodeScript_kBasicLatin, RTL_TEXTENCODING_MS_1252},
345             { UnicodeScript_kLatin1Supplement, UnicodeScript_kLatin1Supplement, RTL_TEXTENCODING_MS_1252},
346             { UnicodeScript_kLatinExtendedA, UnicodeScript_kLatinExtendedA, RTL_TEXTENCODING_MS_1250},
347             { UnicodeScript_kLatinExtendedB, UnicodeScript_kLatinExtendedB, RTL_TEXTENCODING_MS_1257},
348             { UnicodeScript_kGreek, UnicodeScript_kGreek, RTL_TEXTENCODING_MS_1253},
349             { UnicodeScript_kCyrillic, UnicodeScript_kCyrillic, RTL_TEXTENCODING_MS_1251},
350             { UnicodeScript_kHebrew, UnicodeScript_kHebrew, RTL_TEXTENCODING_MS_1255},
351             { UnicodeScript_kArabic, UnicodeScript_kArabic, RTL_TEXTENCODING_MS_1256},
352             { UnicodeScript_kThai, UnicodeScript_kThai, RTL_TEXTENCODING_MS_1258},
353             { UnicodeScript_kHangulJamo, UnicodeScript_kHangulJamo, RTL_TEXTENCODING_MS_949},
354             { UnicodeScript_kHangulCompatibilityJamo, UnicodeScript_kHangulCompatibilityJamo, RTL_TEXTENCODING_MS_949},
355             { UnicodeScript_kHangulSyllable, UnicodeScript_kHangulSyllable, RTL_TEXTENCODING_MS_949},
356             { UnicodeScript_kScriptCount, UnicodeScript_kScriptCount, RTL_TEXTENCODING_MS_1252}
357         };
358 
359         return unicode::getUnicodeScriptType(cChar, aScripts,
360             RTL_TEXTENCODING_MS_1252);
361     }
362 
363     //Utility to remove entries before a given starting position
364     class IfBeforeStart
365 
366     {
367     public:
368         typedef const sw::util::CharRunEntry& argument_type;
369         typedef bool result_type;
370     private:
371     private:
372         xub_StrLen mnStart;
373     public:
IfBeforeStart(xub_StrLen nStart)374         IfBeforeStart(xub_StrLen nStart) : mnStart(nStart) {}
operator ()(const sw::util::CharRunEntry & rEntry) const375         bool operator()(const sw::util::CharRunEntry &rEntry) const
376         {
377             return rEntry.mnEndPos < mnStart;
378         }
379     };
380 }
381 
382 namespace sw
383 {
384     namespace util
385     {
386 
IsPlausableSingleWordSection(const SwFrmFmt & rTitleFmt,const SwFrmFmt & rFollowFmt,sal_Int8 nDocType)387         bool IsPlausableSingleWordSection(const SwFrmFmt &rTitleFmt,
388             const SwFrmFmt &rFollowFmt, sal_Int8 nDocType )
389         {
390             bool bPlausableTitlePage = true;
391 
392             const SwFmtCol& rFirstCols = rTitleFmt.GetCol();
393             const SwFmtCol& rFollowCols = rFollowFmt.GetCol();
394             const SwColumns& rFirstColumns = rFirstCols.GetColumns();
395             const SwColumns& rFollowColumns = rFollowCols.GetColumns();
396             const SvxLRSpaceItem &rOneLR = rTitleFmt.GetLRSpace();
397             const SvxLRSpaceItem &rTwoLR= rFollowFmt.GetLRSpace();
398 
399             if (rFirstColumns.Count() != rFollowColumns.Count())
400             {
401                 //e.g. #i4320#
402                 bPlausableTitlePage = false;
403             }
404             else if (rOneLR != rTwoLR)
405                 bPlausableTitlePage = false;
406             else
407             {
408                 HdFtDistanceGlue aOne(rTitleFmt.GetAttrSet());
409                 HdFtDistanceGlue aTwo(rFollowFmt.GetAttrSet());
410                 //if one doesn't own a footer, then assume it equals with the other one.
411                 const sal_Bool bCheckFooter = ( aOne.HasFooter() && aTwo.HasFooter() ) ? sal_True : sal_False;
412                 //e.g. #i14509#
413                 if (!aOne.EqualTopBottom(aTwo) && nDocType != 1 && bCheckFooter )
414                     bPlausableTitlePage = false;
415             }
416             return bPlausableTitlePage;
417         }
418 
HdFtDistanceGlue(const SfxItemSet & rPage)419         HdFtDistanceGlue::HdFtDistanceGlue(const SfxItemSet &rPage)
420         {
421             if (const SvxBoxItem *pBox = HasItem<SvxBoxItem>(rPage, RES_BOX))
422             {
423                 dyaHdrTop = pBox->CalcLineSpace(BOX_LINE_TOP);
424                 dyaHdrBottom = pBox->CalcLineSpace(BOX_LINE_BOTTOM);
425             }
426             else
427             {
428                 dyaHdrTop = dyaHdrBottom = 0;
429                 dyaHdrBottom = 0;
430             }
431             const SvxULSpaceItem &rUL =
432                 ItemGet<SvxULSpaceItem>(rPage, RES_UL_SPACE);
433             dyaHdrTop = dyaHdrTop + rUL.GetUpper();
434             dyaHdrBottom = dyaHdrBottom + rUL.GetLower();
435 
436             dyaTop = dyaHdrTop;
437             dyaBottom = dyaHdrBottom;
438 
439             using sw::types::msword_cast;
440 
441             const SwFmtHeader *pHd = HasItem<SwFmtHeader>(rPage, RES_HEADER);
442             if (pHd && pHd->IsActive() && pHd->GetHeaderFmt())
443             {
444                 mbHasHeader = true;
445                 dyaTop = dyaTop + static_cast< sal_uInt16 >( (myImplHelpers::CalcHdDist(*(pHd->GetHeaderFmt()))) );
446             }
447             else
448                 mbHasHeader = false;
449 
450             const SwFmtFooter *pFt = HasItem<SwFmtFooter>(rPage, RES_FOOTER);
451             if (pFt && pFt->IsActive() && pFt->GetFooterFmt())
452             {
453                 mbHasFooter = true;
454                 dyaBottom = dyaBottom + static_cast< sal_uInt16 >( (myImplHelpers::CalcFtDist(*(pFt->GetFooterFmt()))) );
455             }
456             else
457                 mbHasFooter = false;
458         }
459 
EqualTopBottom(const HdFtDistanceGlue & rOther) const460         bool HdFtDistanceGlue::EqualTopBottom(const HdFtDistanceGlue &rOther)
461             const
462         {
463             return (dyaTop == rOther.dyaTop && dyaBottom == rOther.dyaBottom);
464         }
465 
ParaStyleMapper(SwDoc & rDoc)466         ParaStyleMapper::ParaStyleMapper(SwDoc &rDoc)
467             : mpImpl(new myImplHelpers::StyleMapperImpl<SwTxtFmtColl>(rDoc))
468         {
469         }
470 
~ParaStyleMapper()471         ParaStyleMapper::~ParaStyleMapper()
472         {
473             delete mpImpl;
474         }
475 
GetStyle(const String & rName,ww::sti eSti)476         ParaStyleMapper::StyleResult ParaStyleMapper::GetStyle(
477             const String& rName, ww::sti eSti)
478         {
479             return mpImpl->GetStyle(rName, eSti);
480         }
481 
CharStyleMapper(SwDoc & rDoc)482         CharStyleMapper::CharStyleMapper(SwDoc &rDoc)
483             : mpImpl(new myImplHelpers::StyleMapperImpl<SwCharFmt>(rDoc))
484         {
485         }
486 
~CharStyleMapper()487         CharStyleMapper::~CharStyleMapper()
488         {
489             delete mpImpl;
490         }
491 
GetStyle(const String & rName,ww::sti eSti)492         CharStyleMapper::StyleResult CharStyleMapper::GetStyle(
493             const String& rName, ww::sti eSti)
494         {
495             return mpImpl->GetStyle(rName, eSti);
496         }
497 
FontMapExport(const String & rFamilyName)498         FontMapExport::FontMapExport(const String &rFamilyName)
499         {
500             msPrimary = GetFontToken(rFamilyName, 0);
501             msSecondary = myImplHelpers::FindBestMSSubstituteFont(msPrimary);
502             if (!msSecondary.Len())
503                 msSecondary = GetFontToken(rFamilyName, 1);
504         }
505 
HasDistinctSecondary() const506         bool FontMapExport::HasDistinctSecondary() const
507         {
508             if (msSecondary.Len() && msSecondary != msPrimary)
509                 return true;
510             return false;
511         }
512 
operator ()(sal_uInt16 nA,sal_uInt16 nB) const513         bool ItemSort::operator()(sal_uInt16 nA, sal_uInt16 nB) const
514         {
515             /*
516              #i24291#
517              All we want to do is ensure for now is that if a charfmt exist
518              in the character properties that it rises to the top and is
519              exported first.  In the future we might find more ordering
520              depandancies for export, in which case this is the place to do
521              it
522             */
523             if (nA == nB)
524                 return false;
525             if (nA == RES_TXTATR_CHARFMT)
526                 return true;
527             if (nB == RES_TXTATR_CHARFMT)
528                 return false;
529             if (nA == RES_TXTATR_INETFMT)
530                 return true;
531             if (nB == RES_TXTATR_INETFMT)
532                return false;
533             return nA < nB;
534         }
535 
GetPseudoCharRuns(const SwTxtNode & rTxtNd,xub_StrLen nTxtStart,bool bSplitOnCharSet)536         CharRuns GetPseudoCharRuns(const SwTxtNode& rTxtNd,
537             xub_StrLen nTxtStart, bool bSplitOnCharSet)
538         {
539             const String &rTxt = rTxtNd.GetTxt();
540 
541             bool bParaIsRTL = false;
542             ASSERT(rTxtNd.GetDoc(), "No document for node?, suspicious");
543             if (rTxtNd.GetDoc())
544             {
545                 if (FRMDIR_HORI_RIGHT_TOP ==
546                     rTxtNd.GetDoc()->GetTextDirection(SwPosition(rTxtNd)))
547                 {
548                     bParaIsRTL = true;
549                 }
550             }
551 
552             using namespace ::com::sun::star::i18n;
553 
554             sal_uInt16 nScript = i18n::ScriptType::LATIN;
555             if (rTxt.Len() && pBreakIt && pBreakIt->GetBreakIter().is())
556                 nScript = pBreakIt->GetBreakIter()->getScriptType(rTxt, 0);
557 
558             rtl_TextEncoding eChrSet = ItemGet<SvxFontItem>(rTxtNd,
559                 GetWhichOfScript(RES_CHRATR_FONT, nScript)).GetCharSet();
560             eChrSet = GetExtendedTextEncoding(eChrSet);
561 
562             CharRuns aRunChanges;
563 
564             if (!rTxt.Len())
565             {
566                 aRunChanges.push_back(CharRunEntry(0, nScript, eChrSet,
567                     bParaIsRTL));
568                 return aRunChanges;
569             }
570 
571             typedef std::pair<int32_t, bool> DirEntry;
572             typedef std::vector<DirEntry> DirChanges;
573             typedef DirChanges::const_iterator cDirIter;
574 
575             typedef std::pair<xub_StrLen, sal_Int16> CharSetEntry;
576             typedef std::vector<CharSetEntry> CharSetChanges;
577             typedef CharSetChanges::const_iterator cCharSetIter;
578 
579             typedef std::pair<xub_StrLen, sal_uInt16> ScriptEntry;
580             typedef std::vector<ScriptEntry> ScriptChanges;
581             typedef ScriptChanges::const_iterator cScriptIter;
582 
583             DirChanges aDirChanges;
584             CharSetChanges aCharSets;
585             ScriptChanges aScripts;
586 
587             UBiDiDirection eDefaultDir = bParaIsRTL ? UBIDI_RTL : UBIDI_LTR;
588             UErrorCode nError = U_ZERO_ERROR;
589             UBiDi* pBidi = ubidi_openSized(rTxt.Len(), 0, &nError);
590             ubidi_setPara(pBidi, reinterpret_cast<const UChar *>(rTxt.GetBuffer()), rTxt.Len(),
591                     static_cast< UBiDiLevel >(eDefaultDir), 0, &nError);
592 
593             sal_Int32 nCount = ubidi_countRuns(pBidi, &nError);
594             aDirChanges.reserve(nCount);
595 
596             int32_t nStart = 0;
597             int32_t nEnd;
598             UBiDiLevel nCurrDir;
599 
600             for (sal_Int32 nIdx = 0; nIdx < nCount; ++nIdx)
601             {
602                 ubidi_getLogicalRun(pBidi, nStart, &nEnd, &nCurrDir);
603                 /*
604                 UBiDiLevel is the type of the level values in this BiDi
605                 implementation.
606 
607                 It holds an embedding level and indicates the visual direction
608                 by its bit 0 (even/odd value).
609 
610                 The value for UBIDI_DEFAULT_LTR is even and the one for
611                 UBIDI_DEFAULT_RTL is odd
612                 */
613                 aDirChanges.push_back(DirEntry(nEnd, nCurrDir & 0x1));
614                 nStart = nEnd;
615             }
616             ubidi_close(pBidi);
617 
618             if (bSplitOnCharSet)
619             {
620                 //Split unicode text into plausible 8bit ranges for export to
621                 //older non unicode aware format
622                 xub_StrLen nLen = rTxt.Len();
623                 xub_StrLen nPos = 0;
624                 while (nPos != nLen)
625                 {
626                     rtl_TextEncoding ScriptType =
627                         myImplHelpers::getScriptClass(rTxt.GetChar(nPos++));
628                     while (
629                             (nPos != nLen) &&
630                             (ScriptType == myImplHelpers::getScriptClass(rTxt.GetChar(nPos)))
631                           )
632                     {
633                         ++nPos;
634                     }
635 
636                     aCharSets.push_back(CharSetEntry(nPos, ScriptType));
637                 }
638             }
639 
640             using sw::types::writer_cast;
641 
642             if (pBreakIt && pBreakIt->GetBreakIter().is())
643             {
644                 xub_StrLen nLen = rTxt.Len();
645                 xub_StrLen nPos = 0;
646                 while (nPos < nLen)
647                 {
648                     sal_Int32 nEnd2 = pBreakIt->GetBreakIter()->endOfScript(rTxt, nPos,
649                         nScript);
650                     if (nEnd2 < 0)
651                         break;
652 //                    nPos = writer_cast<xub_StrLen>(nEnd2);
653                     nPos = static_cast< xub_StrLen >(nEnd2);
654                     aScripts.push_back(ScriptEntry(nPos, nScript));
655                     nScript = pBreakIt->GetBreakIter()->getScriptType(rTxt, nPos);
656                 }
657             }
658 
659             cDirIter aBiDiEnd = aDirChanges.end();
660             cCharSetIter aCharSetEnd = aCharSets.end();
661             cScriptIter aScriptEnd = aScripts.end();
662 
663             cDirIter aBiDiIter = aDirChanges.begin();
664             cCharSetIter aCharSetIter = aCharSets.begin();
665             cScriptIter aScriptIter = aScripts.begin();
666 
667             bool bCharIsRTL = bParaIsRTL;
668 
669             while (
670                     aBiDiIter != aBiDiEnd ||
671                     aCharSetIter != aCharSetEnd ||
672                     aScriptIter != aScriptEnd
673                   )
674             {
675                 xub_StrLen nMinPos = rTxt.Len();
676 
677                 if (aBiDiIter != aBiDiEnd)
678                 {
679                     if (aBiDiIter->first < nMinPos)
680 //                        nMinPos = writer_cast<xub_StrLen>(aBiDiIter->first);
681                         nMinPos = static_cast< xub_StrLen >(aBiDiIter->first);
682                     bCharIsRTL = aBiDiIter->second;
683                 }
684 
685                 if (aCharSetIter != aCharSetEnd)
686                 {
687                     if (aCharSetIter->first < nMinPos)
688                         nMinPos = aCharSetIter->first;
689                     eChrSet = aCharSetIter->second;
690                 }
691 
692                 if (aScriptIter != aScriptEnd)
693                 {
694                     if (aScriptIter->first < nMinPos)
695                         nMinPos = aScriptIter->first;
696                     nScript = aScriptIter->second;
697                 }
698 
699                 aRunChanges.push_back(
700                     CharRunEntry(nMinPos, nScript, eChrSet, bCharIsRTL));
701 
702                 if (aBiDiIter != aBiDiEnd)
703                 {
704                     if (aBiDiIter->first == nMinPos)
705                         ++aBiDiIter;
706                 }
707 
708                 if (aCharSetIter != aCharSetEnd)
709                 {
710                     if (aCharSetIter->first == nMinPos)
711                         ++aCharSetIter;
712                 }
713 
714                 if (aScriptIter != aScriptEnd)
715                 {
716                     if (aScriptIter->first == nMinPos)
717                         ++aScriptIter;
718                 }
719             }
720 
721             aRunChanges.erase(std::remove_if(aRunChanges.begin(),
722                 aRunChanges.end(), myImplHelpers::IfBeforeStart(nTxtStart)), aRunChanges.end());
723 
724             return aRunChanges;
725         }
726     }
727 
728     namespace ms
729     {
rtl_TextEncodingToWinCharset(rtl_TextEncoding eTextEncoding)730         sal_uInt8 rtl_TextEncodingToWinCharset(rtl_TextEncoding eTextEncoding)
731         {
732             sal_uInt8 nRet =
733                 rtl_getBestWindowsCharsetFromTextEncoding(eTextEncoding);
734             switch (eTextEncoding)
735             {
736                 case RTL_TEXTENCODING_DONTKNOW:
737                 case RTL_TEXTENCODING_UCS2:
738                 case RTL_TEXTENCODING_UTF7:
739                 case RTL_TEXTENCODING_UTF8:
740                 case RTL_TEXTENCODING_JAVA_UTF8:
741                     ASSERT(nRet != 0x80, "This method may be redundant");
742                     nRet = 0x80;
743                     break;
744                 default:
745                     break;
746             }
747             return nRet;
748         }
749 
DateTime2DTTM(const DateTime & rDT)750         long DateTime2DTTM( const DateTime& rDT )
751         {
752         /*
753         mint    short   :6  0000003F    minutes (0-59)
754         hr      short   :5  000007C0    hours (0-23)
755         dom     short   :5  0000F800    days of month (1-31)
756         mon     short   :4  000F0000    months (1-12)
757         yr      short   :9  1FF00000    years (1900-2411)-1900
758         wdy     short   :3  E0000000    weekday(Sunday=0
759                                                 Monday=1
760         ( wdy can be ignored )                  Tuesday=2
761                                                 Wednesday=3
762                                                 Thursday=4
763                                                 Friday=5
764                                                 Saturday=6)
765         */
766 
767             if ( rDT.GetDate() == 0L )
768                 return 0L;
769             long nDT = ( rDT.GetDayOfWeek() + 1 ) % 7;
770             nDT <<= 9;
771             nDT += ( rDT.GetYear() - 1900 ) & 0x1ff;
772             nDT <<= 4;
773             nDT += rDT.GetMonth() & 0xf;
774             nDT <<= 5;
775             nDT += rDT.GetDay() & 0x1f;
776             nDT <<= 5;
777             nDT += rDT.GetHour() & 0x1f;
778             nDT <<= 6;
779             nDT += rDT.GetMin() & 0x3f;
780             return nDT;
781         }
782 
DTTM2DateTime(long lDTTM)783         DateTime DTTM2DateTime( long lDTTM )
784         {
785             /*
786             mint    short   :6  0000003F    minutes (0-59)
787             hr      short   :5  000007C0    hours (0-23)
788             dom     short   :5  0000F800    days of month (1-31)
789             mon     short   :4  000F0000    months (1-12)
790             yr      short   :9  1FF00000    years (1900-2411)-1900
791             wdy     short   :3  E0000000    weekday(Sunday=0
792                                                     Monday=1
793             ( wdy can be ignored )                  Tuesday=2
794                                                     Wednesday=3
795                                                     Thursday=4
796                                                     Friday=5
797                                                     Saturday=6)
798             */
799             DateTime aDateTime(Date( 0 ), Time( 0 ));
800             if( lDTTM )
801             {
802                 sal_uInt16 lMin = (sal_uInt16)(lDTTM & 0x0000003F);
803                 lDTTM >>= 6;
804                 sal_uInt16 lHour= (sal_uInt16)(lDTTM & 0x0000001F);
805                 lDTTM >>= 5;
806                 sal_uInt16 lDay = (sal_uInt16)(lDTTM & 0x0000001F);
807                 lDTTM >>= 5;
808                 sal_uInt16 lMon = (sal_uInt16)(lDTTM & 0x0000000F);
809                 lDTTM >>= 4;
810                 sal_uInt16 lYear= (sal_uInt16)(lDTTM & 0x000001FF) + 1900;
811                 aDateTime = DateTime(Date(lDay, lMon, lYear), Time(lHour, lMin));
812             }
813             return aDateTime;
814         }
815 
MSDateTimeFormatToSwFormat(String & rParams,SvNumberFormatter * pFormatter,sal_uInt16 & rLang,bool bHijri)816         sal_uLong MSDateTimeFormatToSwFormat(String& rParams,
817             SvNumberFormatter *pFormatter, sal_uInt16 &rLang, bool bHijri)
818         {
819             // tell the Formatter about the new entry
820             sal_uInt16 nCheckPos = 0;
821             short  nType = NUMBERFORMAT_DEFINED;
822             sal_uInt32  nKey = 0;
823 
824             SwapQuotesInField(rParams);
825 
826             //#102782#, #102815#, #108341# & #111944# have to work at the same time :-)
827             bool bForceJapanese(false);
828             bool bForceNatNum(false);
829             xub_StrLen nLen = rParams.Len();
830             xub_StrLen nI = 0;
831             while (nI < nLen)
832             {
833                 if (rParams.GetChar(nI) == '\\')
834                     nI++;
835                 else if (rParams.GetChar(nI) == '\"')
836                 {
837                     ++nI;
838                     //While not at the end and not at an unescaped end quote
839                     while ((nI < nLen) && (!(rParams.GetChar(nI) == '\"') && (rParams.GetChar(nI-1) != '\\')))
840                         ++nI;
841                 }
842                 else //normal unquoted section
843                 {
844                     sal_Unicode nChar = rParams.GetChar(nI);
845                     if (nChar == 'O')
846                     {
847                         rParams.SetChar(nI, 'M');
848                         bForceNatNum = true;
849                     }
850                     else if (nChar == 'o')
851                     {
852                         rParams.SetChar(nI, 'm');
853                         bForceNatNum = true;
854                     }
855                     else if ((nChar == 'A') && IsNotAM(rParams, nI))
856                     {
857                         rParams.SetChar(nI, 'D');
858                         bForceNatNum = true;
859                     }
860                     else if ((nChar == 'g') || (nChar == 'G'))
861                         bForceJapanese = true;
862                     else if ((nChar == 'a') && IsNotAM(rParams, nI))
863                         bForceJapanese = true;
864                     else if (nChar == 'E')
865                     {
866                         if ((nI != nLen-1) && (rParams.GetChar(nI+1) == 'E'))
867                         {
868                             rParams.Replace(nI, 2, CREATE_CONST_ASC("YYYY"));
869                             nLen+=2;
870                             nI+=3;
871                         }
872                         bForceJapanese = true;
873                     }
874                     else if (nChar == 'e')
875                     {
876                         if ((nI != nLen-1) && (rParams.GetChar(nI+1) == 'e'))
877                         {
878                             rParams.Replace(nI, 2, CREATE_CONST_ASC("yyyy"));
879                             nLen+=2;
880                             nI+=3;
881                         }
882                         bForceJapanese = true;
883                     }
884                     else if (nChar == '/')
885                     {
886                         if(!(IsPreviousAM(rParams, nI) && IsNextPM(rParams, nI)))
887                             // MM We have to escape '/' in case it's used as a char
888                             rParams.Replace(nI, 1, CREATE_CONST_ASC("\\/"));
889                         // rParams.Insert( nI, '\\' );
890                         nI++;
891                         nLen++;
892                     }
893 
894                     // Deal with language differences in date format expression.
895                     // Should be made with i18n framework.
896                     // The list of the mappings and of those "special" locales is to be found at:
897                     // http://l10n.openoffice.org/i18n_framework/LocaleData.html
898                     switch ( rLang )
899                     {
900                     case LANGUAGE_FINNISH:
901                         {
902                             if (nChar == 'y' || nChar == 'Y')
903                                 rParams.SetChar (nI, 'V');
904                             else if (nChar == 'm' || nChar == 'M')
905                                 rParams.SetChar (nI, 'K');
906                             else if (nChar == 'd' || nChar == 'D')
907                                 rParams.SetChar (nI, 'P');
908                             else if (nChar == 'h' || nChar == 'H')
909                                 rParams.SetChar (nI, 'T');
910                         }
911                         break;
912                     case LANGUAGE_DANISH:
913                     case LANGUAGE_NORWEGIAN:
914                     case LANGUAGE_NORWEGIAN_BOKMAL:
915                     case LANGUAGE_NORWEGIAN_NYNORSK:
916                     case LANGUAGE_SWEDISH:
917                     case LANGUAGE_SWEDISH_FINLAND:
918                         {
919                             if (nChar == 'h' || nChar == 'H')
920                                 rParams.SetChar (nI, 'T');
921                         }
922                         break;
923                     case LANGUAGE_PORTUGUESE:
924                     case LANGUAGE_PORTUGUESE_BRAZILIAN:
925                     case LANGUAGE_SPANISH_MODERN:
926                     case LANGUAGE_SPANISH_DATED:
927                     case LANGUAGE_SPANISH_MEXICAN:
928                     case LANGUAGE_SPANISH_GUATEMALA:
929                     case LANGUAGE_SPANISH_COSTARICA:
930                     case LANGUAGE_SPANISH_PANAMA:
931                     case LANGUAGE_SPANISH_DOMINICAN_REPUBLIC:
932                     case LANGUAGE_SPANISH_VENEZUELA:
933                     case LANGUAGE_SPANISH_COLOMBIA:
934                     case LANGUAGE_SPANISH_PERU:
935                     case LANGUAGE_SPANISH_ARGENTINA:
936                     case LANGUAGE_SPANISH_ECUADOR:
937                     case LANGUAGE_SPANISH_CHILE:
938                     case LANGUAGE_SPANISH_URUGUAY:
939                     case LANGUAGE_SPANISH_PARAGUAY:
940                     case LANGUAGE_SPANISH_BOLIVIA:
941                     case LANGUAGE_SPANISH_EL_SALVADOR:
942                     case LANGUAGE_SPANISH_HONDURAS:
943                     case LANGUAGE_SPANISH_NICARAGUA:
944                     case LANGUAGE_SPANISH_PUERTO_RICO:
945                         {
946                             if (nChar == 'a' || nChar == 'A')
947                                 rParams.SetChar (nI, 'O');
948                             else if (nChar == 'y' || nChar == 'Y')
949                                 rParams.SetChar (nI, 'A');
950                         }
951                         break;
952                     case LANGUAGE_DUTCH:
953                     case LANGUAGE_DUTCH_BELGIAN:
954                         {
955                             if (nChar == 'y' || nChar == 'Y')
956                                 rParams.SetChar (nI, 'J');
957                             else if (nChar == 'u' || nChar == 'U')
958                                 rParams.SetChar (nI, 'H');
959                         }
960                         break;
961                     case LANGUAGE_ITALIAN:
962                     case LANGUAGE_ITALIAN_SWISS:
963                         {
964                             if (nChar == 'a' || nChar == 'A')
965                                 rParams.SetChar (nI, 'O');
966                             else if (nChar == 'g' || nChar == 'G')
967                                 rParams.SetChar (nI, 'X');
968                             else if (nChar == 'y' || nChar == 'Y')
969                                 rParams.SetChar(nI, 'A');
970                             else if (nChar == 'd' || nChar == 'D')
971                                 rParams.SetChar (nI, 'G');
972                         }
973                         break;
974                     case LANGUAGE_GERMAN:
975                     case LANGUAGE_GERMAN_SWISS:
976                     case LANGUAGE_GERMAN_AUSTRIAN:
977                     case LANGUAGE_GERMAN_LUXEMBOURG:
978                     case LANGUAGE_GERMAN_LIECHTENSTEIN:
979                         {
980                             if (nChar == 'y' || nChar == 'Y')
981                                 rParams.SetChar (nI, 'J');
982                             else if (nChar == 'd' || nChar == 'D')
983                                 rParams.SetChar (nI, 'T');
984                         }
985                         break;
986                     case LANGUAGE_FRENCH:
987                     case LANGUAGE_FRENCH_BELGIAN:
988                     case LANGUAGE_FRENCH_CANADIAN:
989                     case LANGUAGE_FRENCH_SWISS:
990                     case LANGUAGE_FRENCH_LUXEMBOURG:
991                     case LANGUAGE_FRENCH_MONACO:
992                         {
993                             if (nChar == 'a' || nChar == 'A')
994                                 rParams.SetChar (nI, 'O');
995                             else if (nChar == 'y' || nChar == 'Y')
996                                 rParams.SetChar (nI, 'A');
997                             else if (nChar == 'd' || nChar == 'D')
998                                 rParams.SetChar (nI, 'J');
999                         }
1000                         break;
1001                     default:
1002                         {
1003                             ; // Nothing
1004                         }
1005                     }
1006                 }
1007                 ++nI;
1008             }
1009 
1010             if (bForceNatNum)
1011                 bForceJapanese = true;
1012 
1013             if (bForceJapanese)
1014                 rLang = LANGUAGE_JAPANESE;
1015 
1016             if (bForceNatNum)
1017                 rParams.Insert(CREATE_CONST_ASC("[NatNum1][$-411]"),0);
1018 
1019             if (bHijri)
1020                 rParams.Insert(CREATE_CONST_ASC("[~hijri]"), 0);
1021 
1022             pFormatter->PutEntry(rParams, nCheckPos, nType, nKey, rLang);
1023 
1024             return nKey;
1025         }
1026 
IsPreviousAM(String & rParams,xub_StrLen nPos)1027         sal_Bool IsPreviousAM(String& rParams, xub_StrLen nPos){
1028             xub_StrLen nPos1 = nPos - 1;
1029             xub_StrLen nPos2 = nPos - 2;
1030 
1031             if(nPos1 > nPos || nPos2 > nPos){
1032                 return sal_False;
1033             }else{
1034                 return (
1035                     (rParams.GetChar(nPos1) == 'M'||rParams.GetChar(nPos1) == 'm')&&
1036                     (rParams.GetChar(nPos2) == 'A'||rParams.GetChar(nPos2) == 'a')
1037                     );
1038             }
1039         }
IsNextPM(String & rParams,xub_StrLen nPos)1040         sal_Bool IsNextPM(String& rParams, xub_StrLen nPos){
1041             xub_StrLen nPos1 = nPos + 1;
1042             xub_StrLen nPos2 = nPos + 2;
1043 
1044 
1045             if(nPos1 >= rParams.Len() - 1 || nPos2 > rParams.Len() - 1){
1046                 return sal_False;
1047             }else{
1048                 return (
1049                     (rParams.GetChar(nPos1) == 'P'||rParams.GetChar(nPos1) == 'p')&&
1050                     (rParams.GetChar(nPos2) == 'M'||rParams.GetChar(nPos2) == 'm')
1051                     );
1052             }
1053 
1054         }
IsNotAM(String & rParams,xub_StrLen nPos)1055         bool IsNotAM(String& rParams, xub_StrLen nPos)
1056         {
1057             return (
1058                     (nPos == rParams.Len() - 1) ||
1059                     (
1060                     (rParams.GetChar(nPos+1) != 'M') &&
1061                     (rParams.GetChar(nPos+1) != 'm')
1062                     )
1063                 );
1064         }
1065 
SwapQuotesInField(String & rFmt)1066         void SwapQuotesInField(String &rFmt)
1067         {
1068             //Swap unescaped " and ' with ' and "
1069             xub_StrLen nLen = rFmt.Len();
1070             for (xub_StrLen nI = 0; nI < nLen; ++nI)
1071             {
1072                 if ((rFmt.GetChar(nI) == '\"') && (!nI || rFmt.GetChar(nI-1) != '\\'))
1073                     rFmt.SetChar(nI, '\'');
1074                 else if ((rFmt.GetChar(nI) == '\'') && (!nI || rFmt.GetChar(nI-1) != '\\'))
1075                     rFmt.SetChar(nI, '\"');
1076             }
1077         }
1078 
1079 
1080     }
1081 }
1082 
1083 /* vi:set tabstop=4 shiftwidth=4 expandtab: */
1084