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