bump product version to 4.1.6.2
[LibreOffice.git] / sw / source / filter / ascii / ascatr.cxx
blobd620c2babde56f56508c7ac61814c12e4a9395b5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #include <hintids.hxx>
21 #include <tools/stream.hxx>
22 #include <comphelper/string.hxx>
23 #include <editeng/fontitem.hxx>
24 #include <pam.hxx>
25 #include <doc.hxx>
26 #include <ndtxt.hxx>
27 #include <wrtasc.hxx>
28 #include <txatbase.hxx>
29 #include <fchrfmt.hxx>
30 #include <txtfld.hxx>
31 #include <txtatr.hxx>
32 #include <fmtftn.hxx>
33 #include <charfmt.hxx>
34 #include <fmtfld.hxx>
35 #include <fldbas.hxx>
36 #include <ftninfo.hxx>
39 * This file contains all output functions of the ASCII-Writer;
40 * For all nodes, attributes, formats and chars.
43 class SwASC_AttrIter
45 SwASCWriter& rWrt;
46 const SwTxtNode& rNd;
47 xub_StrLen nAktSwPos;
49 xub_StrLen SearchNext( xub_StrLen nStartPos );
51 public:
52 SwASC_AttrIter( SwASCWriter& rWrt, const SwTxtNode& rNd, xub_StrLen nStt );
54 void NextPos() { nAktSwPos = SearchNext( nAktSwPos + 1 ); }
56 xub_StrLen WhereNext() const { return nAktSwPos; }
57 bool OutAttr( xub_StrLen nSwPos );
61 SwASC_AttrIter::SwASC_AttrIter( SwASCWriter& rWr, const SwTxtNode& rTxtNd,
62 xub_StrLen nStt )
63 : rWrt( rWr ), rNd( rTxtNd ), nAktSwPos( 0 )
65 nAktSwPos = SearchNext( nStt + 1 );
69 xub_StrLen SwASC_AttrIter::SearchNext( xub_StrLen nStartPos )
71 xub_StrLen nMinPos = STRING_MAXLEN;
72 const SwpHints* pTxtAttrs = rNd.GetpSwpHints();
73 if( pTxtAttrs )
75 // TODO: This can be optimized, if we make use of the fact that the TxtAttrs
76 // are sorted by starting position. We would need to remember two indices, however.
77 for ( sal_uInt16 i = 0; i < pTxtAttrs->Count(); i++ )
79 const SwTxtAttr* pHt = (*pTxtAttrs)[i];
80 if (pHt->HasDummyChar())
82 xub_StrLen nPos = *pHt->GetStart();
84 if( nPos >= nStartPos && nPos <= nMinPos )
85 nMinPos = nPos;
87 if( ( ++nPos ) >= nStartPos && nPos < nMinPos )
88 nMinPos = nPos;
92 return nMinPos;
96 bool SwASC_AttrIter::OutAttr( xub_StrLen nSwPos )
98 bool bRet = false;
99 const SwpHints* pTxtAttrs = rNd.GetpSwpHints();
100 if( pTxtAttrs )
102 sal_uInt16 i;
103 for( i = 0; i < pTxtAttrs->Count(); i++ )
105 const SwTxtAttr* pHt = (*pTxtAttrs)[i];
106 if ( pHt->HasDummyChar() && nSwPos == *pHt->GetStart() )
108 bRet = true;
109 String sOut;
110 switch( pHt->Which() )
112 case RES_TXTATR_FIELD:
113 sOut = static_cast<SwTxtFld const*>(pHt)->GetFld().GetFld()
114 ->ExpandField(true);
115 break;
117 case RES_TXTATR_FTN:
119 const SwFmtFtn& rFtn = pHt->GetFtn();
120 if( rFtn.GetNumStr().Len() )
121 sOut = rFtn.GetNumStr();
122 else if( rFtn.IsEndNote() )
123 sOut = rWrt.pDoc->GetEndNoteInfo().aFmt.
124 GetNumStr( rFtn.GetNumber() );
125 else
126 sOut = rWrt.pDoc->GetFtnInfo().aFmt.
127 GetNumStr( rFtn.GetNumber() );
129 break;
131 if( sOut.Len() )
132 rWrt.Strm().WriteUnicodeOrByteText( sOut );
134 else if( nSwPos < *pHt->GetStart() )
135 break;
138 return bRet;
142 //------------------------
143 // Output of the node
144 //------------------------
146 static Writer& OutASC_SwTxtNode( Writer& rWrt, SwCntntNode& rNode )
148 const SwTxtNode& rNd = (SwTxtNode&)rNode;
150 xub_StrLen nStrPos = rWrt.pCurPam->GetPoint()->nContent.GetIndex();
151 xub_StrLen nNodeEnde = rNd.Len(), nEnde = nNodeEnde;
152 bool bLastNd = rWrt.pCurPam->GetPoint()->nNode == rWrt.pCurPam->GetMark()->nNode;
153 if( bLastNd )
154 nEnde = rWrt.pCurPam->GetMark()->nContent.GetIndex();
156 SwASC_AttrIter aAttrIter( (SwASCWriter&)rWrt, rNd, nStrPos );
158 if( !nStrPos && rWrt.bExportPargraphNumbering )
160 String numString( rNd.GetNumString() );
161 if (numString.Len())
163 numString.Append(' ');
164 rWrt.Strm().WriteUnicodeOrByteText(numString);
168 String aStr( rNd.GetTxt() );
169 if( rWrt.bASCII_ParaAsBlanc )
170 aStr.SearchAndReplaceAll( 0x0A, ' ' );
172 const bool bExportSoftHyphens = RTL_TEXTENCODING_UCS2 == rWrt.GetAsciiOptions().GetCharSet() ||
173 RTL_TEXTENCODING_UTF8 == rWrt.GetAsciiOptions().GetCharSet();
175 do {
176 xub_StrLen nNextAttr = aAttrIter.WhereNext();
178 if( nNextAttr > nEnde )
179 nNextAttr = nEnde;
181 if( !aAttrIter.OutAttr( nStrPos ))
183 String aOutStr( aStr.Copy( nStrPos, nNextAttr - nStrPos ) );
184 if ( !bExportSoftHyphens )
185 aOutStr = comphelper::string::remove(aOutStr, CHAR_SOFTHYPHEN);
187 rWrt.Strm().WriteUnicodeOrByteText( aOutStr );
189 nStrPos = nNextAttr;
190 aAttrIter.NextPos();
191 } while( nStrPos < nEnde );
193 if( !bLastNd ||
194 ( ( !rWrt.bWriteClipboardDoc && !rWrt.bASCII_NoLastLineEnd )
195 && !nStrPos && nEnde == nNodeEnde ) )
196 rWrt.Strm().WriteUnicodeOrByteText( ((SwASCWriter&)rWrt).GetLineEnd());
198 return rWrt;
202 * Create the table for the ASCII function pointers to the output
203 * functon.
204 * There are local structures that only need to be known to the ASCII DLL.
207 SwNodeFnTab aASCNodeFnTab = {
208 /* RES_TXTNODE */ OutASC_SwTxtNode,
209 /* RES_GRFNODE */ 0,
210 /* RES_OLENODE */ 0
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */