xref: /AOO41X/main/sc/source/filter/rtf/rtfexp.cxx (revision b3f79822e811ac3493b185030a72c3c5a51f32d8)
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_sc.hxx"
26 
27 
28 
29 #include "scitems.hxx"
30 #include <editeng/eeitem.hxx>
31 
32 
33 #include <svx/algitem.hxx>
34 #include <editeng/wghtitem.hxx>
35 #include <editeng/postitem.hxx>
36 #include <editeng/udlnitem.hxx>
37 #include <editeng/fontitem.hxx>
38 #include <editeng/fhgtitem.hxx>
39 #include <svl/style.hxx>
40 #include <svtools/rtfout.hxx>
41 #include <svtools/rtfkeywd.hxx>
42 
43 #include "rtfexp.hxx"
44 #include "filter.hxx"
45 #include "document.hxx"
46 #include "patattr.hxx"
47 #include "attrib.hxx"
48 #include "cell.hxx"
49 #include "cellform.hxx"
50 #include "editutil.hxx"
51 #include "stlpool.hxx"
52 #include "ftools.hxx"
53 
54 //------------------------------------------------------------------
55 
ScExportRTF(SvStream & rStrm,ScDocument * pDoc,const ScRange & rRange,const CharSet)56 FltError ScFormatFilterPluginImpl::ScExportRTF( SvStream& rStrm, ScDocument* pDoc,
57         const ScRange& rRange, const CharSet /*eNach*/ )
58 {
59     ScRTFExport aEx( rStrm, pDoc, rRange );
60     return aEx.Write();
61 }
62 
63 
ScRTFExport(SvStream & rStrmP,ScDocument * pDocP,const ScRange & rRangeP)64 ScRTFExport::ScRTFExport( SvStream& rStrmP, ScDocument* pDocP, const ScRange& rRangeP )
65             :
66             ScExportBase( rStrmP, pDocP, rRangeP ),
67             pCellX( new sal_uLong[ MAXCOL+2 ] )
68 {
69 }
70 
71 
~ScRTFExport()72 ScRTFExport::~ScRTFExport()
73 {
74     delete [] pCellX;
75 }
76 
77 
Write()78 sal_uLong ScRTFExport::Write()
79 {
80     rStrm << '{' << OOO_STRING_SVTOOLS_RTF_RTF;
81     rStrm << OOO_STRING_SVTOOLS_RTF_ANSI << sNewLine;
82 
83 #if 0
84 // das ist noch nicht ausgegoren
85 /*
86     SfxStyleSheetBasePool*  pStylePool  = pDoc->GetStyleSheetPool();
87     SfxStyleSheetBase*      pStyleSheet = NULL;
88     DBG_ASSERT( pStylePool, "StylePool not found! :-(" );
89     pStylePool->SetSearchMask( SFX_STYLE_FAMILY_PARA, SFXSTYLEBIT_ALL );
90     pStyleSheet = pStylePool->Find( STRING_STANDARD, SFX_STYLE_FAMILY_PARA );
91     DBG_ASSERT( pStyleSheet, "ParaStyle not found! :-(" );
92     const SfxItemSet& rSetPara = pStyleSheet->GetItemSet();
93 
94     // fonttbl
95     String aFontFamilyName(
96         ((const SvxFontItem&)(rSetPara.Get( ATTR_FONT ))).GetFamilyName() );
97     rStrm << OOO_STRING_SVTOOLS_RTF_DEFF << '0'
98         << '{' << OOO_STRING_SVTOOLS_RTF_FONTTBL
99         << '{' << OOO_STRING_SVTOOLS_RTF_F << '0' << OOO_STRING_SVTOOLS_RTF_FNIL << ' ' << aFontFamilyName.GetStr() << ";}"
100         << '}' << sNewLine;
101 
102     // hier kaeme die colortbl
103 
104     // stylesheet
105     sal_uInt32 nFontHeight =
106         ((const SvxFontHeightItem&)(rSetPara.Get( ATTR_FONT_HEIGHT ))).GetHeight();
107     rStrm << '{' << OOO_STRING_SVTOOLS_RTF_STYLESHEET
108         << '{' << OOO_STRING_SVTOOLS_RTF_FS << String( sal_uInt32(nFontHeight / TWIPS_PER_POINT) ).GetStr()
109             << ' ' << pStyleSheet->GetName().GetStr() << ";}"
110         << '}' << sNewLine;
111 */
112 #endif
113 
114     // Daten
115     for ( SCTAB nTab = aRange.aStart.Tab(); nTab <= aRange.aEnd.Tab(); nTab++ )
116     {
117         if ( nTab > aRange.aStart.Tab() )
118             rStrm << OOO_STRING_SVTOOLS_RTF_PAR;
119         WriteTab( nTab );
120     }
121 
122     rStrm << '}' << sNewLine;
123     return rStrm.GetError();
124 }
125 
126 
WriteTab(SCTAB nTab)127 void ScRTFExport::WriteTab( SCTAB nTab )
128 {
129     rStrm << '{' << sNewLine;
130     if ( pDoc->HasTable( nTab ) )
131     {
132         memset( &pCellX[0], 0, (MAXCOL+2) * sizeof(sal_uLong) );
133         SCCOL nCol;
134         SCCOL nEndCol = aRange.aEnd.Col();
135         for ( nCol = aRange.aStart.Col(); nCol <= nEndCol; nCol++ )
136         {
137             pCellX[nCol+1] = pCellX[nCol] + pDoc->GetColWidth( nCol, nTab );
138         }
139 
140         SCROW nEndRow = aRange.aEnd.Row();
141         for ( SCROW nRow = aRange.aStart.Row(); nRow <= nEndRow; nRow++ )
142         {
143             WriteRow( nTab, nRow );
144         }
145     }
146     rStrm << '}' << sNewLine;
147 }
148 
149 
WriteRow(SCTAB nTab,SCROW nRow)150 void ScRTFExport::WriteRow( SCTAB nTab, SCROW nRow )
151 {
152     rStrm << OOO_STRING_SVTOOLS_RTF_TROWD << OOO_STRING_SVTOOLS_RTF_TRGAPH << "30" << OOO_STRING_SVTOOLS_RTF_TRLEFT << "-30";
153     rStrm << OOO_STRING_SVTOOLS_RTF_TRRH << ByteString::CreateFromInt32( pDoc->GetRowHeight( nRow, nTab ) ).GetBuffer();
154     SCCOL nCol;
155     SCCOL nEndCol = aRange.aEnd.Col();
156     for ( nCol = aRange.aStart.Col(); nCol <= nEndCol; nCol++ )
157     {
158         const ScPatternAttr* pAttr = pDoc->GetPattern( nCol, nRow, nTab );
159         const ScMergeAttr&      rMergeAttr      = (const ScMergeAttr&)      pAttr->GetItem( ATTR_MERGE );
160         const SvxVerJustifyItem& rVerJustifyItem= (const SvxVerJustifyItem&)pAttr->GetItem( ATTR_VER_JUSTIFY );
161 
162         const sal_Char* pChar;
163 
164         if ( rMergeAttr.GetColMerge() != 0 )
165             rStrm << OOO_STRING_SVTOOLS_RTF_CLMGF;
166         else
167         {
168             const ScMergeFlagAttr& rMergeFlagAttr = (const ScMergeFlagAttr&) pAttr->GetItem( ATTR_MERGE_FLAG );
169             if ( rMergeFlagAttr.IsHorOverlapped() )
170                 rStrm << OOO_STRING_SVTOOLS_RTF_CLMRG;
171         }
172 
173         switch( rVerJustifyItem.GetValue() )
174         {
175             case SVX_VER_JUSTIFY_TOP:       pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALT;   break;
176             case SVX_VER_JUSTIFY_CENTER:    pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALC;   break;
177             case SVX_VER_JUSTIFY_BOTTOM:    pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALB;   break;
178             case SVX_VER_JUSTIFY_STANDARD:  pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALB;   break;  //! Bottom
179             default:                        pChar = NULL;           break;
180         }
181         if ( pChar )
182             rStrm << pChar;
183 
184         rStrm << OOO_STRING_SVTOOLS_RTF_CELLX << ByteString::CreateFromInt32( pCellX[nCol+1] ).GetBuffer();
185         if ( (nCol & 0x0F) == 0x0F )
186             rStrm << sNewLine;      // Zeilen nicht zu lang werden lassen
187     }
188     rStrm << OOO_STRING_SVTOOLS_RTF_PARD << OOO_STRING_SVTOOLS_RTF_PLAIN << OOO_STRING_SVTOOLS_RTF_INTBL << sNewLine;
189 
190     sal_uLong nStrmPos = rStrm.Tell();
191     for ( nCol = aRange.aStart.Col(); nCol <= nEndCol; nCol++ )
192     {
193         WriteCell( nTab, nRow, nCol );
194         if ( rStrm.Tell() - nStrmPos > 255 )
195         {   // Zeilen nicht zu lang werden lassen
196             rStrm << sNewLine;
197             nStrmPos = rStrm.Tell();
198         }
199     }
200     rStrm << OOO_STRING_SVTOOLS_RTF_ROW << sNewLine;
201 }
202 
203 
WriteCell(SCTAB nTab,SCROW nRow,SCCOL nCol)204 void ScRTFExport::WriteCell( SCTAB nTab, SCROW nRow, SCCOL nCol )
205 {
206     const ScPatternAttr* pAttr = pDoc->GetPattern( nCol, nRow, nTab );
207 
208     const ScMergeFlagAttr& rMergeFlagAttr = (const ScMergeFlagAttr&) pAttr->GetItem( ATTR_MERGE_FLAG );
209     if ( rMergeFlagAttr.IsHorOverlapped() )
210     {
211         rStrm << OOO_STRING_SVTOOLS_RTF_CELL;
212         return ;
213     }
214 
215     ScBaseCell* pCell;
216     pDoc->GetCell( nCol, nRow, nTab, pCell );
217     sal_Bool bValueData;
218     String aContent;
219     if ( pCell )
220     {
221         switch ( pCell->GetCellType() )
222         {
223             case CELLTYPE_NOTE :
224                 bValueData = sal_False;
225             break;      // nix
226             case CELLTYPE_EDIT :
227             {
228                 bValueData = sal_False;
229                 EditEngine& rEngine = GetEditEngine();
230                 const EditTextObject* pObj;
231                 ((const ScEditCell*)pCell)->GetData( pObj );
232                 if ( pObj )
233                 {
234                     rEngine.SetText( *pObj );
235                     aContent = rEngine.GetText( LINEEND_LF );   // LineFeed zwischen Absaetzen!
236                 }
237             }
238             break;
239             default:
240             {
241                 bValueData = pCell->HasValueData();
242                 sal_uLong nFormat = pAttr->GetNumberFormat( pFormatter );
243                 Color* pColor;
244                 ScCellFormat::GetString( pCell, nFormat, aContent, &pColor, *pFormatter );
245             }
246         }
247     }
248     else
249         bValueData = sal_False;
250 
251     sal_Bool bResetPar, bResetAttr;
252     bResetPar = bResetAttr = sal_False;
253 
254     const SvxHorJustifyItem&    rHorJustifyItem = (const SvxHorJustifyItem&)pAttr->GetItem( ATTR_HOR_JUSTIFY );
255     const SvxWeightItem&        rWeightItem     = (const SvxWeightItem&)    pAttr->GetItem( ATTR_FONT_WEIGHT );
256     const SvxPostureItem&       rPostureItem    = (const SvxPostureItem&)   pAttr->GetItem( ATTR_FONT_POSTURE );
257     const SvxUnderlineItem&     rUnderlineItem  = (const SvxUnderlineItem&) pAttr->GetItem( ATTR_FONT_UNDERLINE );
258 
259     const sal_Char* pChar;
260 
261     switch( rHorJustifyItem.GetValue() )
262     {
263         case SVX_HOR_JUSTIFY_STANDARD:
264             pChar = (bValueData ? OOO_STRING_SVTOOLS_RTF_QR : OOO_STRING_SVTOOLS_RTF_QL);
265             break;
266         case SVX_HOR_JUSTIFY_CENTER:    pChar = OOO_STRING_SVTOOLS_RTF_QC;  break;
267         case SVX_HOR_JUSTIFY_BLOCK:     pChar = OOO_STRING_SVTOOLS_RTF_QJ;  break;
268         case SVX_HOR_JUSTIFY_RIGHT:     pChar = OOO_STRING_SVTOOLS_RTF_QR;  break;
269         case SVX_HOR_JUSTIFY_LEFT:
270         case SVX_HOR_JUSTIFY_REPEAT:
271         default:                        pChar = OOO_STRING_SVTOOLS_RTF_QL;  break;
272     }
273     rStrm << pChar;
274 
275     if ( rWeightItem.GetWeight() >= WEIGHT_BOLD )
276     {   // bold
277         bResetAttr = sal_True;
278         rStrm << OOO_STRING_SVTOOLS_RTF_B;
279     }
280     if ( rPostureItem.GetPosture() != ITALIC_NONE )
281     {   // italic
282         bResetAttr = sal_True;
283         rStrm << OOO_STRING_SVTOOLS_RTF_I;
284     }
285     if ( rUnderlineItem.GetLineStyle() != UNDERLINE_NONE )
286     {   // underline
287         bResetAttr = sal_True;
288         rStrm << OOO_STRING_SVTOOLS_RTF_UL;
289     }
290 
291     rStrm << ' ';
292     RTFOutFuncs::Out_String( rStrm, aContent );
293     rStrm << OOO_STRING_SVTOOLS_RTF_CELL;
294 
295     if ( bResetPar )
296         rStrm << OOO_STRING_SVTOOLS_RTF_PARD << OOO_STRING_SVTOOLS_RTF_INTBL;
297     if ( bResetAttr )
298         rStrm << OOO_STRING_SVTOOLS_RTF_PLAIN;
299 }
300 
301 
302