1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "scitems.hxx"
21 #include <editeng/eeitem.hxx>
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>
37 #include "document.hxx"
38 #include "patattr.hxx"
40 #include "cellform.hxx"
41 #include "editutil.hxx"
42 #include "stlpool.hxx"
45 FltError
ScFormatFilterPluginImpl::ScExportRTF( SvStream
& rStrm
, ScDocument
* pDoc
,
46 const ScRange
& rRange
, const rtl_TextEncoding
/*eNach*/ )
48 ScRTFExport
aEx( rStrm
, pDoc
, rRange
);
53 ScRTFExport::ScRTFExport( SvStream
& rStrmP
, ScDocument
* pDocP
, const ScRange
& rRangeP
)
55 ScExportBase( rStrmP
, pDocP
, rRangeP
),
56 pCellX( new sal_uLong
[ MAXCOL
+2 ] )
61 ScRTFExport::~ScRTFExport()
67 sal_uLong
ScRTFExport::Write()
69 rStrm
.WriteChar( '{' ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_RTF
);
70 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_ANSI
).WriteCharPtr( SAL_NEWLINE_STRING
);
73 for ( SCTAB nTab
= aRange
.aStart
.Tab(); nTab
<= aRange
.aEnd
.Tab(); nTab
++ )
75 if ( nTab
> aRange
.aStart
.Tab() )
76 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PAR
);
80 rStrm
.WriteChar( '}' ).WriteCharPtr( SAL_NEWLINE_STRING
);
81 return rStrm
.GetError();
85 void ScRTFExport::WriteTab( SCTAB nTab
)
87 rStrm
.WriteChar( '{' ).WriteCharPtr( SAL_NEWLINE_STRING
);
88 if ( pDoc
->HasTable( nTab
) )
90 memset( &pCellX
[0], 0, (MAXCOL
+2) * sizeof(sal_uLong
) );
92 SCCOL nEndCol
= aRange
.aEnd
.Col();
93 for ( nCol
= aRange
.aStart
.Col(); nCol
<= nEndCol
; nCol
++ )
95 pCellX
[nCol
+1] = pCellX
[nCol
] + pDoc
->GetColWidth( nCol
, nTab
);
98 SCROW nEndRow
= aRange
.aEnd
.Row();
99 for ( SCROW nRow
= aRange
.aStart
.Row(); nRow
<= nEndRow
; nRow
++ )
101 WriteRow( nTab
, nRow
);
104 rStrm
.WriteChar( '}' ).WriteCharPtr( SAL_NEWLINE_STRING
);
108 void ScRTFExport::WriteRow( SCTAB nTab
, SCROW nRow
)
110 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TROWD
).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRGAPH
).WriteCharPtr( "30" ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRLEFT
).WriteCharPtr( "-30" );
111 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRRH
).WriteCharPtr( OString::number(pDoc
->GetRowHeight(nRow
, nTab
)).getStr() );
113 SCCOL nEndCol
= aRange
.aEnd
.Col();
114 for ( nCol
= aRange
.aStart
.Col(); nCol
<= nEndCol
; nCol
++ )
116 const ScPatternAttr
* pAttr
= pDoc
->GetPattern( nCol
, nRow
, nTab
);
117 const ScMergeAttr
& rMergeAttr
= (const ScMergeAttr
&) pAttr
->GetItem( ATTR_MERGE
);
118 const SvxVerJustifyItem
& rVerJustifyItem
= (const SvxVerJustifyItem
&)pAttr
->GetItem( ATTR_VER_JUSTIFY
);
120 const sal_Char
* pChar
;
122 if ( rMergeAttr
.GetColMerge() != 0 )
123 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CLMGF
);
126 const ScMergeFlagAttr
& rMergeFlagAttr
= (const ScMergeFlagAttr
&) pAttr
->GetItem( ATTR_MERGE_FLAG
);
127 if ( rMergeFlagAttr
.IsHorOverlapped() )
128 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CLMRG
);
131 switch( rVerJustifyItem
.GetValue() )
133 case SVX_VER_JUSTIFY_TOP
: pChar
= OOO_STRING_SVTOOLS_RTF_CLVERTALT
; break;
134 case SVX_VER_JUSTIFY_CENTER
: pChar
= OOO_STRING_SVTOOLS_RTF_CLVERTALC
; break;
135 case SVX_VER_JUSTIFY_BOTTOM
: pChar
= OOO_STRING_SVTOOLS_RTF_CLVERTALB
; break;
136 case SVX_VER_JUSTIFY_STANDARD
: pChar
= OOO_STRING_SVTOOLS_RTF_CLVERTALB
; break; //! Bottom
137 default: pChar
= NULL
; break;
140 rStrm
.WriteCharPtr( pChar
);
142 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELLX
).WriteCharPtr( OString::number(pCellX
[nCol
+1]).getStr() );
143 if ( (nCol
& 0x0F) == 0x0F )
144 rStrm
.WriteCharPtr( SAL_NEWLINE_STRING
); // Do not let lines get too long
146 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PARD
).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PLAIN
).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_INTBL
).WriteCharPtr( SAL_NEWLINE_STRING
);
148 sal_uLong nStrmPos
= rStrm
.Tell();
149 for ( nCol
= aRange
.aStart
.Col(); nCol
<= nEndCol
; nCol
++ )
151 WriteCell( nTab
, nRow
, nCol
);
152 if ( rStrm
.Tell() - nStrmPos
> 255 )
153 { // Do not let lines get too long
154 rStrm
.WriteCharPtr( SAL_NEWLINE_STRING
);
155 nStrmPos
= rStrm
.Tell();
158 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_ROW
).WriteCharPtr( SAL_NEWLINE_STRING
);
162 void ScRTFExport::WriteCell( SCTAB nTab
, SCROW nRow
, SCCOL nCol
)
164 const ScPatternAttr
* pAttr
= pDoc
->GetPattern( nCol
, nRow
, nTab
);
166 const ScMergeFlagAttr
& rMergeFlagAttr
= (const ScMergeFlagAttr
&) pAttr
->GetItem( ATTR_MERGE_FLAG
);
167 if ( rMergeFlagAttr
.IsHorOverlapped() )
169 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELL
);
173 bool bValueData
= false;
175 ScAddress
aPos(nCol
, nRow
, nTab
);
176 switch (pDoc
->GetCellType(aPos
))
184 const EditTextObject
* pObj
= pDoc
->GetEditText(aPos
);
187 EditEngine
& rEngine
= GetEditEngine();
188 rEngine
.SetText(*pObj
);
189 aContent
= rEngine
.GetText(LINEEND_LF
); // LineFeed in between paragraphs!
195 bValueData
= pDoc
->HasValueData(aPos
);
196 sal_uLong nFormat
= pAttr
->GetNumberFormat(pFormatter
);
198 aContent
= ScCellFormat::GetString(*pDoc
, aPos
, nFormat
, &pColor
, *pFormatter
);
202 bool bResetAttr(false);
204 const SvxHorJustifyItem
& rHorJustifyItem
= (const SvxHorJustifyItem
&)pAttr
->GetItem( ATTR_HOR_JUSTIFY
);
205 const SvxWeightItem
& rWeightItem
= (const SvxWeightItem
&) pAttr
->GetItem( ATTR_FONT_WEIGHT
);
206 const SvxPostureItem
& rPostureItem
= (const SvxPostureItem
&) pAttr
->GetItem( ATTR_FONT_POSTURE
);
207 const SvxUnderlineItem
& rUnderlineItem
= (const SvxUnderlineItem
&) pAttr
->GetItem( ATTR_FONT_UNDERLINE
);
209 const sal_Char
* pChar
;
211 switch( rHorJustifyItem
.GetValue() )
213 case SVX_HOR_JUSTIFY_STANDARD
:
214 pChar
= (bValueData
? OOO_STRING_SVTOOLS_RTF_QR
: OOO_STRING_SVTOOLS_RTF_QL
);
216 case SVX_HOR_JUSTIFY_CENTER
: pChar
= OOO_STRING_SVTOOLS_RTF_QC
; break;
217 case SVX_HOR_JUSTIFY_BLOCK
: pChar
= OOO_STRING_SVTOOLS_RTF_QJ
; break;
218 case SVX_HOR_JUSTIFY_RIGHT
: pChar
= OOO_STRING_SVTOOLS_RTF_QR
; break;
219 case SVX_HOR_JUSTIFY_LEFT
:
220 case SVX_HOR_JUSTIFY_REPEAT
:
221 default: pChar
= OOO_STRING_SVTOOLS_RTF_QL
; break;
223 rStrm
.WriteCharPtr( pChar
);
225 if ( rWeightItem
.GetWeight() >= WEIGHT_BOLD
)
228 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_B
);
230 if ( rPostureItem
.GetPosture() != ITALIC_NONE
)
233 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_I
);
235 if ( rUnderlineItem
.GetLineStyle() != UNDERLINE_NONE
)
238 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_UL
);
241 rStrm
.WriteChar( ' ' );
242 RTFOutFuncs::Out_String( rStrm
, aContent
);
243 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELL
);
246 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PLAIN
);
249 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */