merged tag ooo/OOO330_m14
[LibreOffice.git] / sc / source / filter / rtf / rtfexp.cxx
blobc817fc2844ebb6b22c87e3b4064dc9a9d11611ad
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sc.hxx"
33 #include "scitems.hxx"
34 #include <editeng/eeitem.hxx>
37 #include <svx/algitem.hxx>
38 #include <editeng/wghtitem.hxx>
39 #include <editeng/postitem.hxx>
40 #include <editeng/udlnitem.hxx>
41 #include <editeng/fontitem.hxx>
42 #include <editeng/fhgtitem.hxx>
43 #include <svl/style.hxx>
44 #include <svtools/rtfout.hxx>
45 #include <svtools/rtfkeywd.hxx>
47 #include "rtfexp.hxx"
48 #include "filter.hxx"
49 #include "document.hxx"
50 #include "patattr.hxx"
51 #include "attrib.hxx"
52 #include "cell.hxx"
53 #include "cellform.hxx"
54 #include "editutil.hxx"
55 #include "stlpool.hxx"
56 #include "ftools.hxx"
58 //------------------------------------------------------------------
60 FltError ScFormatFilterPluginImpl::ScExportRTF( SvStream& rStrm, ScDocument* pDoc,
61 const ScRange& rRange, const CharSet /*eNach*/ )
63 ScRTFExport aEx( rStrm, pDoc, rRange );
64 return aEx.Write();
68 ScRTFExport::ScRTFExport( SvStream& rStrmP, ScDocument* pDocP, const ScRange& rRangeP )
70 ScExportBase( rStrmP, pDocP, rRangeP ),
71 pCellX( new ULONG[ MAXCOL+2 ] )
76 ScRTFExport::~ScRTFExport()
78 delete [] pCellX;
82 ULONG ScRTFExport::Write()
84 rStrm << '{' << OOO_STRING_SVTOOLS_RTF_RTF;
85 rStrm << OOO_STRING_SVTOOLS_RTF_ANSI << sNewLine;
87 #if 0
88 // das ist noch nicht ausgegoren
90 SfxStyleSheetBasePool* pStylePool = pDoc->GetStyleSheetPool();
91 SfxStyleSheetBase* pStyleSheet = NULL;
92 DBG_ASSERT( pStylePool, "StylePool not found! :-(" );
93 pStylePool->SetSearchMask( SFX_STYLE_FAMILY_PARA, SFXSTYLEBIT_ALL );
94 pStyleSheet = pStylePool->Find( STRING_STANDARD, SFX_STYLE_FAMILY_PARA );
95 DBG_ASSERT( pStyleSheet, "ParaStyle not found! :-(" );
96 const SfxItemSet& rSetPara = pStyleSheet->GetItemSet();
98 // fonttbl
99 String aFontFamilyName(
100 ((const SvxFontItem&)(rSetPara.Get( ATTR_FONT ))).GetFamilyName() );
101 rStrm << OOO_STRING_SVTOOLS_RTF_DEFF << '0'
102 << '{' << OOO_STRING_SVTOOLS_RTF_FONTTBL
103 << '{' << OOO_STRING_SVTOOLS_RTF_F << '0' << OOO_STRING_SVTOOLS_RTF_FNIL << ' ' << aFontFamilyName.GetStr() << ";}"
104 << '}' << sNewLine;
106 // hier kaeme die colortbl
108 // stylesheet
109 UINT32 nFontHeight =
110 ((const SvxFontHeightItem&)(rSetPara.Get( ATTR_FONT_HEIGHT ))).GetHeight();
111 rStrm << '{' << OOO_STRING_SVTOOLS_RTF_STYLESHEET
112 << '{' << OOO_STRING_SVTOOLS_RTF_FS << String( UINT32(nFontHeight / TWIPS_PER_POINT) ).GetStr()
113 << ' ' << pStyleSheet->GetName().GetStr() << ";}"
114 << '}' << sNewLine;
116 #endif
118 // Daten
119 for ( SCTAB nTab = aRange.aStart.Tab(); nTab <= aRange.aEnd.Tab(); nTab++ )
121 if ( nTab > aRange.aStart.Tab() )
122 rStrm << OOO_STRING_SVTOOLS_RTF_PAR;
123 WriteTab( nTab );
126 rStrm << '}' << sNewLine;
127 return rStrm.GetError();
131 void ScRTFExport::WriteTab( SCTAB nTab )
133 rStrm << '{' << sNewLine;
134 if ( pDoc->HasTable( nTab ) )
136 memset( &pCellX[0], 0, (MAXCOL+2) * sizeof(ULONG) );
137 SCCOL nCol;
138 SCCOL nEndCol = aRange.aEnd.Col();
139 for ( nCol = aRange.aStart.Col(); nCol <= nEndCol; nCol++ )
141 pCellX[nCol+1] = pCellX[nCol] + pDoc->GetColWidth( nCol, nTab );
144 SCROW nEndRow = aRange.aEnd.Row();
145 for ( SCROW nRow = aRange.aStart.Row(); nRow <= nEndRow; nRow++ )
147 WriteRow( nTab, nRow );
150 rStrm << '}' << sNewLine;
154 void ScRTFExport::WriteRow( SCTAB nTab, SCROW nRow )
156 rStrm << OOO_STRING_SVTOOLS_RTF_TROWD << OOO_STRING_SVTOOLS_RTF_TRGAPH << "30" << OOO_STRING_SVTOOLS_RTF_TRLEFT << "-30";
157 rStrm << OOO_STRING_SVTOOLS_RTF_TRRH << ByteString::CreateFromInt32( pDoc->GetRowHeight( nRow, nTab ) ).GetBuffer();
158 SCCOL nCol;
159 SCCOL nEndCol = aRange.aEnd.Col();
160 for ( nCol = aRange.aStart.Col(); nCol <= nEndCol; nCol++ )
162 const ScPatternAttr* pAttr = pDoc->GetPattern( nCol, nRow, nTab );
163 const ScMergeAttr& rMergeAttr = (const ScMergeAttr&) pAttr->GetItem( ATTR_MERGE );
164 const SvxVerJustifyItem& rVerJustifyItem= (const SvxVerJustifyItem&)pAttr->GetItem( ATTR_VER_JUSTIFY );
166 const sal_Char* pChar;
168 if ( rMergeAttr.GetColMerge() != 0 )
169 rStrm << OOO_STRING_SVTOOLS_RTF_CLMGF;
170 else
172 const ScMergeFlagAttr& rMergeFlagAttr = (const ScMergeFlagAttr&) pAttr->GetItem( ATTR_MERGE_FLAG );
173 if ( rMergeFlagAttr.IsHorOverlapped() )
174 rStrm << OOO_STRING_SVTOOLS_RTF_CLMRG;
177 switch( rVerJustifyItem.GetValue() )
179 case SVX_VER_JUSTIFY_TOP: pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALT; break;
180 case SVX_VER_JUSTIFY_CENTER: pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALC; break;
181 case SVX_VER_JUSTIFY_BOTTOM: pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALB; break;
182 case SVX_VER_JUSTIFY_STANDARD: pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALB; break; //! Bottom
183 default: pChar = NULL; break;
185 if ( pChar )
186 rStrm << pChar;
188 rStrm << OOO_STRING_SVTOOLS_RTF_CELLX << ByteString::CreateFromInt32( pCellX[nCol+1] ).GetBuffer();
189 if ( (nCol & 0x0F) == 0x0F )
190 rStrm << sNewLine; // Zeilen nicht zu lang werden lassen
192 rStrm << OOO_STRING_SVTOOLS_RTF_PARD << OOO_STRING_SVTOOLS_RTF_PLAIN << OOO_STRING_SVTOOLS_RTF_INTBL << sNewLine;
194 ULONG nStrmPos = rStrm.Tell();
195 for ( nCol = aRange.aStart.Col(); nCol <= nEndCol; nCol++ )
197 WriteCell( nTab, nRow, nCol );
198 if ( rStrm.Tell() - nStrmPos > 255 )
199 { // Zeilen nicht zu lang werden lassen
200 rStrm << sNewLine;
201 nStrmPos = rStrm.Tell();
204 rStrm << OOO_STRING_SVTOOLS_RTF_ROW << sNewLine;
208 void ScRTFExport::WriteCell( SCTAB nTab, SCROW nRow, SCCOL nCol )
210 const ScPatternAttr* pAttr = pDoc->GetPattern( nCol, nRow, nTab );
212 const ScMergeFlagAttr& rMergeFlagAttr = (const ScMergeFlagAttr&) pAttr->GetItem( ATTR_MERGE_FLAG );
213 if ( rMergeFlagAttr.IsHorOverlapped() )
215 rStrm << OOO_STRING_SVTOOLS_RTF_CELL;
216 return ;
219 ScBaseCell* pCell;
220 pDoc->GetCell( nCol, nRow, nTab, pCell );
221 BOOL bValueData;
222 String aContent;
223 if ( pCell )
225 switch ( pCell->GetCellType() )
227 case CELLTYPE_NOTE :
228 bValueData = FALSE;
229 break; // nix
230 case CELLTYPE_EDIT :
232 bValueData = FALSE;
233 EditEngine& rEngine = GetEditEngine();
234 const EditTextObject* pObj;
235 ((const ScEditCell*)pCell)->GetData( pObj );
236 if ( pObj )
238 rEngine.SetText( *pObj );
239 aContent = rEngine.GetText( LINEEND_LF ); // LineFeed zwischen Absaetzen!
242 break;
243 default:
245 bValueData = pCell->HasValueData();
246 ULONG nFormat = pAttr->GetNumberFormat( pFormatter );
247 Color* pColor;
248 ScCellFormat::GetString( pCell, nFormat, aContent, &pColor, *pFormatter );
252 else
253 bValueData = FALSE;
255 BOOL bResetPar, bResetAttr;
256 bResetPar = bResetAttr = FALSE;
258 const SvxHorJustifyItem& rHorJustifyItem = (const SvxHorJustifyItem&)pAttr->GetItem( ATTR_HOR_JUSTIFY );
259 const SvxWeightItem& rWeightItem = (const SvxWeightItem&) pAttr->GetItem( ATTR_FONT_WEIGHT );
260 const SvxPostureItem& rPostureItem = (const SvxPostureItem&) pAttr->GetItem( ATTR_FONT_POSTURE );
261 const SvxUnderlineItem& rUnderlineItem = (const SvxUnderlineItem&) pAttr->GetItem( ATTR_FONT_UNDERLINE );
263 const sal_Char* pChar;
265 switch( rHorJustifyItem.GetValue() )
267 case SVX_HOR_JUSTIFY_STANDARD:
268 pChar = (bValueData ? OOO_STRING_SVTOOLS_RTF_QR : OOO_STRING_SVTOOLS_RTF_QL);
269 break;
270 case SVX_HOR_JUSTIFY_CENTER: pChar = OOO_STRING_SVTOOLS_RTF_QC; break;
271 case SVX_HOR_JUSTIFY_BLOCK: pChar = OOO_STRING_SVTOOLS_RTF_QJ; break;
272 case SVX_HOR_JUSTIFY_RIGHT: pChar = OOO_STRING_SVTOOLS_RTF_QR; break;
273 case SVX_HOR_JUSTIFY_LEFT:
274 case SVX_HOR_JUSTIFY_REPEAT:
275 default: pChar = OOO_STRING_SVTOOLS_RTF_QL; break;
277 rStrm << pChar;
279 if ( rWeightItem.GetWeight() >= WEIGHT_BOLD )
280 { // bold
281 bResetAttr = TRUE;
282 rStrm << OOO_STRING_SVTOOLS_RTF_B;
284 if ( rPostureItem.GetPosture() != ITALIC_NONE )
285 { // italic
286 bResetAttr = TRUE;
287 rStrm << OOO_STRING_SVTOOLS_RTF_I;
289 if ( rUnderlineItem.GetLineStyle() != UNDERLINE_NONE )
290 { // underline
291 bResetAttr = TRUE;
292 rStrm << OOO_STRING_SVTOOLS_RTF_UL;
295 rStrm << ' ';
296 RTFOutFuncs::Out_String( rStrm, aContent );
297 rStrm << OOO_STRING_SVTOOLS_RTF_CELL;
299 if ( bResetPar )
300 rStrm << OOO_STRING_SVTOOLS_RTF_PARD << OOO_STRING_SVTOOLS_RTF_INTBL;
301 if ( bResetAttr )
302 rStrm << OOO_STRING_SVTOOLS_RTF_PLAIN;