Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
37 :
38 : /*
39 : * This file contains all output functions of the ASCII-Writer;
40 : * For all nodes, attributes, formats and chars.
41 : */
42 :
43 : class SwASC_AttrIter
44 : {
45 : SwASCWriter& rWrt;
46 : const SwTxtNode& rNd;
47 : xub_StrLen nAktSwPos;
48 :
49 : xub_StrLen SearchNext( xub_StrLen nStartPos );
50 :
51 : public:
52 : SwASC_AttrIter( SwASCWriter& rWrt, const SwTxtNode& rNd, xub_StrLen nStt );
53 :
54 21 : void NextPos() { nAktSwPos = SearchNext( nAktSwPos + 1 ); }
55 :
56 21 : xub_StrLen WhereNext() const { return nAktSwPos; }
57 : bool OutAttr( xub_StrLen nSwPos );
58 : };
59 :
60 :
61 21 : SwASC_AttrIter::SwASC_AttrIter( SwASCWriter& rWr, const SwTxtNode& rTxtNd,
62 : xub_StrLen nStt )
63 21 : : rWrt( rWr ), rNd( rTxtNd ), nAktSwPos( 0 )
64 : {
65 21 : nAktSwPos = SearchNext( nStt + 1 );
66 21 : }
67 :
68 :
69 42 : xub_StrLen SwASC_AttrIter::SearchNext( xub_StrLen nStartPos )
70 : {
71 42 : xub_StrLen nMinPos = STRING_MAXLEN;
72 42 : const SwpHints* pTxtAttrs = rNd.GetpSwpHints();
73 42 : if( pTxtAttrs )
74 : {
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 44 : for ( sal_uInt16 i = 0; i < pTxtAttrs->Count(); i++ )
78 : {
79 24 : const SwTxtAttr* pHt = (*pTxtAttrs)[i];
80 24 : if (pHt->HasDummyChar())
81 : {
82 0 : xub_StrLen nPos = *pHt->GetStart();
83 :
84 0 : if( nPos >= nStartPos && nPos <= nMinPos )
85 0 : nMinPos = nPos;
86 :
87 0 : if( ( ++nPos ) >= nStartPos && nPos < nMinPos )
88 0 : nMinPos = nPos;
89 : }
90 : }
91 : }
92 42 : return nMinPos;
93 : }
94 :
95 :
96 21 : bool SwASC_AttrIter::OutAttr( xub_StrLen nSwPos )
97 : {
98 21 : bool bRet = false;
99 21 : const SwpHints* pTxtAttrs = rNd.GetpSwpHints();
100 21 : if( pTxtAttrs )
101 : {
102 : sal_uInt16 i;
103 18 : for( i = 0; i < pTxtAttrs->Count(); i++ )
104 : {
105 10 : const SwTxtAttr* pHt = (*pTxtAttrs)[i];
106 10 : if ( pHt->HasDummyChar() && nSwPos == *pHt->GetStart() )
107 : {
108 0 : bRet = true;
109 0 : String sOut;
110 0 : switch( pHt->Which() )
111 : {
112 : case RES_TXTATR_FIELD:
113 0 : sOut = static_cast<SwTxtFld const*>(pHt)->GetFld().GetFld()
114 0 : ->ExpandField(true);
115 0 : break;
116 :
117 : case RES_TXTATR_FTN:
118 : {
119 0 : const SwFmtFtn& rFtn = pHt->GetFtn();
120 0 : if( rFtn.GetNumStr().Len() )
121 0 : sOut = rFtn.GetNumStr();
122 0 : else if( rFtn.IsEndNote() )
123 0 : sOut = rWrt.pDoc->GetEndNoteInfo().aFmt.
124 0 : GetNumStr( rFtn.GetNumber() );
125 : else
126 0 : sOut = rWrt.pDoc->GetFtnInfo().aFmt.
127 0 : GetNumStr( rFtn.GetNumber() );
128 : }
129 0 : break;
130 : }
131 0 : if( sOut.Len() )
132 0 : rWrt.Strm().WriteUnicodeOrByteText( sOut );
133 : }
134 10 : else if( nSwPos < *pHt->GetStart() )
135 2 : break;
136 : }
137 : }
138 21 : return bRet;
139 : }
140 :
141 :
142 : //------------------------
143 : // Output of the node
144 : //------------------------
145 :
146 21 : static Writer& OutASC_SwTxtNode( Writer& rWrt, SwCntntNode& rNode )
147 : {
148 21 : const SwTxtNode& rNd = (SwTxtNode&)rNode;
149 :
150 21 : xub_StrLen nStrPos = rWrt.pCurPam->GetPoint()->nContent.GetIndex();
151 21 : xub_StrLen nNodeEnde = rNd.Len(), nEnde = nNodeEnde;
152 21 : bool bLastNd = rWrt.pCurPam->GetPoint()->nNode == rWrt.pCurPam->GetMark()->nNode;
153 21 : if( bLastNd )
154 20 : nEnde = rWrt.pCurPam->GetMark()->nContent.GetIndex();
155 :
156 21 : SwASC_AttrIter aAttrIter( (SwASCWriter&)rWrt, rNd, nStrPos );
157 :
158 21 : if( !nStrPos && rWrt.bExportPargraphNumbering )
159 : {
160 1 : String numString( rNd.GetNumString() );
161 1 : if (numString.Len())
162 : {
163 0 : numString.Append(' ');
164 0 : rWrt.Strm().WriteUnicodeOrByteText(numString);
165 1 : }
166 : }
167 :
168 21 : String aStr( rNd.GetTxt() );
169 21 : if( rWrt.bASCII_ParaAsBlanc )
170 0 : aStr.SearchAndReplaceAll( 0x0A, ' ' );
171 :
172 21 : const bool bExportSoftHyphens = RTL_TEXTENCODING_UCS2 == rWrt.GetAsciiOptions().GetCharSet() ||
173 21 : RTL_TEXTENCODING_UTF8 == rWrt.GetAsciiOptions().GetCharSet();
174 :
175 21 : do {
176 21 : xub_StrLen nNextAttr = aAttrIter.WhereNext();
177 :
178 21 : if( nNextAttr > nEnde )
179 21 : nNextAttr = nEnde;
180 :
181 21 : if( !aAttrIter.OutAttr( nStrPos ))
182 : {
183 21 : String aOutStr( aStr.Copy( nStrPos, nNextAttr - nStrPos ) );
184 21 : if ( !bExportSoftHyphens )
185 0 : aOutStr = comphelper::string::remove(aOutStr, CHAR_SOFTHYPHEN);
186 :
187 21 : rWrt.Strm().WriteUnicodeOrByteText( aOutStr );
188 : }
189 21 : nStrPos = nNextAttr;
190 21 : aAttrIter.NextPos();
191 : } while( nStrPos < nEnde );
192 :
193 61 : if( !bLastNd ||
194 40 : ( ( !rWrt.bWriteClipboardDoc && !rWrt.bASCII_NoLastLineEnd )
195 : && !nStrPos && nEnde == nNodeEnde ) )
196 1 : rWrt.Strm().WriteUnicodeOrByteText( ((SwASCWriter&)rWrt).GetLineEnd());
197 :
198 21 : return rWrt;
199 : }
200 :
201 : /*
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.
205 : */
206 :
207 : SwNodeFnTab aASCNodeFnTab = {
208 : /* RES_TXTNODE */ OutASC_SwTxtNode,
209 : /* RES_GRFNODE */ 0,
210 : /* RES_OLENODE */ 0
211 : };
212 :
213 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|