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 "scitems.hxx"
21 : #include <editeng/eeitem.hxx>
22 :
23 :
24 : #include <svx/algitem.hxx>
25 : #include <editeng/wghtitem.hxx>
26 : #include <editeng/postitem.hxx>
27 : #include <editeng/udlnitem.hxx>
28 : #include <editeng/fontitem.hxx>
29 : #include <editeng/fhgtitem.hxx>
30 : #include <editeng/justifyitem.hxx>
31 : #include <svl/style.hxx>
32 : #include <svtools/rtfout.hxx>
33 : #include <svtools/rtfkeywd.hxx>
34 :
35 : #include "rtfexp.hxx"
36 : #include "filter.hxx"
37 : #include "document.hxx"
38 : #include "patattr.hxx"
39 : #include "attrib.hxx"
40 : #include "cellform.hxx"
41 : #include "editutil.hxx"
42 : #include "stlpool.hxx"
43 : #include "ftools.hxx"
44 :
45 0 : FltError ScFormatFilterPluginImpl::ScExportRTF( SvStream& rStrm, ScDocument* pDoc,
46 : const ScRange& rRange, const rtl_TextEncoding /*eNach*/ )
47 : {
48 0 : ScRTFExport aEx( rStrm, pDoc, rRange );
49 0 : return aEx.Write();
50 : }
51 :
52 :
53 0 : ScRTFExport::ScRTFExport( SvStream& rStrmP, ScDocument* pDocP, const ScRange& rRangeP )
54 : :
55 : ScExportBase( rStrmP, pDocP, rRangeP ),
56 0 : pCellX( new sal_uLong[ MAXCOL+2 ] )
57 : {
58 0 : }
59 :
60 :
61 0 : ScRTFExport::~ScRTFExport()
62 : {
63 0 : delete [] pCellX;
64 0 : }
65 :
66 :
67 0 : sal_uLong ScRTFExport::Write()
68 : {
69 0 : rStrm.WriteChar( '{' ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_RTF );
70 0 : rStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_ANSI ).WriteCharPtr( SAL_NEWLINE_STRING );
71 :
72 : // Data
73 0 : for ( SCTAB nTab = aRange.aStart.Tab(); nTab <= aRange.aEnd.Tab(); nTab++ )
74 : {
75 0 : if ( nTab > aRange.aStart.Tab() )
76 0 : rStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PAR );
77 0 : WriteTab( nTab );
78 : }
79 :
80 0 : rStrm.WriteChar( '}' ).WriteCharPtr( SAL_NEWLINE_STRING );
81 0 : return rStrm.GetError();
82 : }
83 :
84 :
85 0 : void ScRTFExport::WriteTab( SCTAB nTab )
86 : {
87 0 : rStrm.WriteChar( '{' ).WriteCharPtr( SAL_NEWLINE_STRING );
88 0 : if ( pDoc->HasTable( nTab ) )
89 : {
90 0 : memset( &pCellX[0], 0, (MAXCOL+2) * sizeof(sal_uLong) );
91 : SCCOL nCol;
92 0 : SCCOL nEndCol = aRange.aEnd.Col();
93 0 : for ( nCol = aRange.aStart.Col(); nCol <= nEndCol; nCol++ )
94 : {
95 0 : pCellX[nCol+1] = pCellX[nCol] + pDoc->GetColWidth( nCol, nTab );
96 : }
97 :
98 0 : SCROW nEndRow = aRange.aEnd.Row();
99 0 : for ( SCROW nRow = aRange.aStart.Row(); nRow <= nEndRow; nRow++ )
100 : {
101 0 : WriteRow( nTab, nRow );
102 : }
103 : }
104 0 : rStrm.WriteChar( '}' ).WriteCharPtr( SAL_NEWLINE_STRING );
105 0 : }
106 :
107 :
108 0 : void ScRTFExport::WriteRow( SCTAB nTab, SCROW nRow )
109 : {
110 0 : rStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TROWD ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRGAPH ).WriteCharPtr( "30" ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRLEFT ).WriteCharPtr( "-30" );
111 0 : rStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRRH ).WriteCharPtr( OString::number(pDoc->GetRowHeight(nRow, nTab)).getStr() );
112 : SCCOL nCol;
113 0 : SCCOL nEndCol = aRange.aEnd.Col();
114 0 : for ( nCol = aRange.aStart.Col(); nCol <= nEndCol; nCol++ )
115 : {
116 0 : const ScPatternAttr* pAttr = pDoc->GetPattern( nCol, nRow, nTab );
117 0 : const ScMergeAttr& rMergeAttr = (const ScMergeAttr&) pAttr->GetItem( ATTR_MERGE );
118 0 : const SvxVerJustifyItem& rVerJustifyItem= (const SvxVerJustifyItem&)pAttr->GetItem( ATTR_VER_JUSTIFY );
119 :
120 : const sal_Char* pChar;
121 :
122 0 : if ( rMergeAttr.GetColMerge() != 0 )
123 0 : rStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CLMGF );
124 : else
125 : {
126 0 : const ScMergeFlagAttr& rMergeFlagAttr = (const ScMergeFlagAttr&) pAttr->GetItem( ATTR_MERGE_FLAG );
127 0 : if ( rMergeFlagAttr.IsHorOverlapped() )
128 0 : rStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CLMRG );
129 : }
130 :
131 0 : switch( rVerJustifyItem.GetValue() )
132 : {
133 0 : case SVX_VER_JUSTIFY_TOP: pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALT; break;
134 0 : case SVX_VER_JUSTIFY_CENTER: pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALC; break;
135 0 : case SVX_VER_JUSTIFY_BOTTOM: pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALB; break;
136 0 : case SVX_VER_JUSTIFY_STANDARD: pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALB; break; //! Bottom
137 0 : default: pChar = NULL; break;
138 : }
139 0 : if ( pChar )
140 0 : rStrm.WriteCharPtr( pChar );
141 :
142 0 : rStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELLX ).WriteCharPtr( OString::number(pCellX[nCol+1]).getStr() );
143 0 : if ( (nCol & 0x0F) == 0x0F )
144 0 : rStrm.WriteCharPtr( SAL_NEWLINE_STRING ); // Do not let lines get too long
145 : }
146 0 : rStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PARD ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PLAIN ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_INTBL ).WriteCharPtr( SAL_NEWLINE_STRING );
147 :
148 0 : sal_uLong nStrmPos = rStrm.Tell();
149 0 : for ( nCol = aRange.aStart.Col(); nCol <= nEndCol; nCol++ )
150 : {
151 0 : WriteCell( nTab, nRow, nCol );
152 0 : if ( rStrm.Tell() - nStrmPos > 255 )
153 : { // Do not let lines get too long
154 0 : rStrm.WriteCharPtr( SAL_NEWLINE_STRING );
155 0 : nStrmPos = rStrm.Tell();
156 : }
157 : }
158 0 : rStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_ROW ).WriteCharPtr( SAL_NEWLINE_STRING );
159 0 : }
160 :
161 :
162 0 : void ScRTFExport::WriteCell( SCTAB nTab, SCROW nRow, SCCOL nCol )
163 : {
164 0 : const ScPatternAttr* pAttr = pDoc->GetPattern( nCol, nRow, nTab );
165 :
166 0 : const ScMergeFlagAttr& rMergeFlagAttr = (const ScMergeFlagAttr&) pAttr->GetItem( ATTR_MERGE_FLAG );
167 0 : if ( rMergeFlagAttr.IsHorOverlapped() )
168 : {
169 0 : rStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELL );
170 0 : return ;
171 : }
172 :
173 0 : bool bValueData = false;
174 0 : OUString aContent;
175 0 : ScAddress aPos(nCol, nRow, nTab);
176 0 : switch (pDoc->GetCellType(aPos))
177 : {
178 : case CELLTYPE_NONE:
179 0 : bValueData = false;
180 0 : break;
181 : case CELLTYPE_EDIT:
182 : {
183 0 : bValueData = false;
184 0 : const EditTextObject* pObj = pDoc->GetEditText(aPos);
185 0 : if (pObj)
186 : {
187 0 : EditEngine& rEngine = GetEditEngine();
188 0 : rEngine.SetText(*pObj);
189 0 : aContent = rEngine.GetText(LINEEND_LF); // LineFeed in between paragraphs!
190 : }
191 : }
192 0 : break;
193 : default:
194 : {
195 0 : bValueData = pDoc->HasValueData(aPos);
196 0 : sal_uLong nFormat = pAttr->GetNumberFormat(pFormatter);
197 : Color* pColor;
198 0 : aContent = ScCellFormat::GetString(*pDoc, aPos, nFormat, &pColor, *pFormatter);
199 : }
200 : }
201 :
202 : sal_Bool bResetPar, bResetAttr;
203 0 : bResetPar = bResetAttr = false;
204 :
205 0 : const SvxHorJustifyItem& rHorJustifyItem = (const SvxHorJustifyItem&)pAttr->GetItem( ATTR_HOR_JUSTIFY );
206 0 : const SvxWeightItem& rWeightItem = (const SvxWeightItem&) pAttr->GetItem( ATTR_FONT_WEIGHT );
207 0 : const SvxPostureItem& rPostureItem = (const SvxPostureItem&) pAttr->GetItem( ATTR_FONT_POSTURE );
208 0 : const SvxUnderlineItem& rUnderlineItem = (const SvxUnderlineItem&) pAttr->GetItem( ATTR_FONT_UNDERLINE );
209 :
210 : const sal_Char* pChar;
211 :
212 0 : switch( rHorJustifyItem.GetValue() )
213 : {
214 : case SVX_HOR_JUSTIFY_STANDARD:
215 0 : pChar = (bValueData ? OOO_STRING_SVTOOLS_RTF_QR : OOO_STRING_SVTOOLS_RTF_QL);
216 0 : break;
217 0 : case SVX_HOR_JUSTIFY_CENTER: pChar = OOO_STRING_SVTOOLS_RTF_QC; break;
218 0 : case SVX_HOR_JUSTIFY_BLOCK: pChar = OOO_STRING_SVTOOLS_RTF_QJ; break;
219 0 : case SVX_HOR_JUSTIFY_RIGHT: pChar = OOO_STRING_SVTOOLS_RTF_QR; break;
220 : case SVX_HOR_JUSTIFY_LEFT:
221 : case SVX_HOR_JUSTIFY_REPEAT:
222 0 : default: pChar = OOO_STRING_SVTOOLS_RTF_QL; break;
223 : }
224 0 : rStrm.WriteCharPtr( pChar );
225 :
226 0 : if ( rWeightItem.GetWeight() >= WEIGHT_BOLD )
227 : { // bold
228 0 : bResetAttr = sal_True;
229 0 : rStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_B );
230 : }
231 0 : if ( rPostureItem.GetPosture() != ITALIC_NONE )
232 : { // italic
233 0 : bResetAttr = sal_True;
234 0 : rStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_I );
235 : }
236 0 : if ( rUnderlineItem.GetLineStyle() != UNDERLINE_NONE )
237 : { // underline
238 0 : bResetAttr = sal_True;
239 0 : rStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_UL );
240 : }
241 :
242 0 : rStrm.WriteChar( ' ' );
243 0 : RTFOutFuncs::Out_String( rStrm, aContent );
244 0 : rStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELL );
245 :
246 0 : if ( bResetPar )
247 0 : rStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PARD ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_INTBL );
248 0 : if ( bResetAttr )
249 0 : rStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PLAIN );
250 : }
251 :
252 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|