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>
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>
36 #include "document.hxx"
37 #include "patattr.hxx"
39 #include "cellform.hxx"
40 #include "editutil.hxx"
41 #include "stlpool.hxx"
44 FltError
ScFormatFilterPluginImpl::ScExportRTF( SvStream
& rStrm
, ScDocument
* pDoc
,
45 const ScRange
& rRange
, const rtl_TextEncoding
/*eNach*/ )
47 ScRTFExport
aEx( rStrm
, pDoc
, rRange
);
51 ScRTFExport::ScRTFExport( SvStream
& rStrmP
, ScDocument
* pDocP
, const ScRange
& rRangeP
)
53 ScExportBase( rStrmP
, pDocP
, rRangeP
),
54 pCellX( new sal_uLong
[ MAXCOL
+2 ] )
58 ScRTFExport::~ScRTFExport()
63 sal_uLong
ScRTFExport::Write()
65 rStrm
.WriteChar( '{' ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_RTF
);
66 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_ANSI
).WriteCharPtr( SAL_NEWLINE_STRING
);
69 for ( SCTAB nTab
= aRange
.aStart
.Tab(); nTab
<= aRange
.aEnd
.Tab(); nTab
++ )
71 if ( nTab
> aRange
.aStart
.Tab() )
72 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PAR
);
76 rStrm
.WriteChar( '}' ).WriteCharPtr( SAL_NEWLINE_STRING
);
77 return rStrm
.GetError();
80 void ScRTFExport::WriteTab( SCTAB nTab
)
82 rStrm
.WriteChar( '{' ).WriteCharPtr( SAL_NEWLINE_STRING
);
83 if ( pDoc
->HasTable( nTab
) )
85 memset( &pCellX
[0], 0, (MAXCOL
+2) * sizeof(sal_uLong
) );
87 SCCOL nEndCol
= aRange
.aEnd
.Col();
88 for ( nCol
= aRange
.aStart
.Col(); nCol
<= nEndCol
; nCol
++ )
90 pCellX
[nCol
+1] = pCellX
[nCol
] + pDoc
->GetColWidth( nCol
, nTab
);
93 SCROW nEndRow
= aRange
.aEnd
.Row();
94 for ( SCROW nRow
= aRange
.aStart
.Row(); nRow
<= nEndRow
; nRow
++ )
96 WriteRow( nTab
, nRow
);
99 rStrm
.WriteChar( '}' ).WriteCharPtr( SAL_NEWLINE_STRING
);
102 void ScRTFExport::WriteRow( SCTAB nTab
, SCROW nRow
)
104 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TROWD
).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRGAPH
).WriteCharPtr( "30" ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRLEFT
).WriteCharPtr( "-30" );
105 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRRH
).WriteCharPtr( OString::number(pDoc
->GetRowHeight(nRow
, nTab
)).getStr() );
107 SCCOL nEndCol
= aRange
.aEnd
.Col();
108 for ( nCol
= aRange
.aStart
.Col(); nCol
<= nEndCol
; nCol
++ )
110 const ScPatternAttr
* pAttr
= pDoc
->GetPattern( nCol
, nRow
, nTab
);
111 const ScMergeAttr
& rMergeAttr
= static_cast<const ScMergeAttr
&>( pAttr
->GetItem( ATTR_MERGE
));
112 const SvxVerJustifyItem
& rVerJustifyItem
= static_cast<const SvxVerJustifyItem
&>(pAttr
->GetItem( ATTR_VER_JUSTIFY
));
114 const sal_Char
* pChar
;
116 if ( rMergeAttr
.GetColMerge() != 0 )
117 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CLMGF
);
120 const ScMergeFlagAttr
& rMergeFlagAttr
= static_cast<const ScMergeFlagAttr
&>( pAttr
->GetItem( ATTR_MERGE_FLAG
) );
121 if ( rMergeFlagAttr
.IsHorOverlapped() )
122 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CLMRG
);
125 switch( rVerJustifyItem
.GetValue() )
127 case SVX_VER_JUSTIFY_TOP
: pChar
= OOO_STRING_SVTOOLS_RTF_CLVERTALT
; break;
128 case SVX_VER_JUSTIFY_CENTER
: pChar
= OOO_STRING_SVTOOLS_RTF_CLVERTALC
; break;
129 case SVX_VER_JUSTIFY_BOTTOM
: pChar
= OOO_STRING_SVTOOLS_RTF_CLVERTALB
; break;
130 case SVX_VER_JUSTIFY_STANDARD
: pChar
= OOO_STRING_SVTOOLS_RTF_CLVERTALB
; break; //! Bottom
131 default: pChar
= NULL
; break;
134 rStrm
.WriteCharPtr( pChar
);
136 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELLX
).WriteCharPtr( OString::number(pCellX
[nCol
+1]).getStr() );
137 if ( (nCol
& 0x0F) == 0x0F )
138 rStrm
.WriteCharPtr( SAL_NEWLINE_STRING
); // Do not let lines get too long
140 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PARD
).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PLAIN
).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_INTBL
).WriteCharPtr( SAL_NEWLINE_STRING
);
142 sal_uLong nStrmPos
= rStrm
.Tell();
143 for ( nCol
= aRange
.aStart
.Col(); nCol
<= nEndCol
; nCol
++ )
145 WriteCell( nTab
, nRow
, nCol
);
146 if ( rStrm
.Tell() - nStrmPos
> 255 )
147 { // Do not let lines get too long
148 rStrm
.WriteCharPtr( SAL_NEWLINE_STRING
);
149 nStrmPos
= rStrm
.Tell();
152 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_ROW
).WriteCharPtr( SAL_NEWLINE_STRING
);
155 void ScRTFExport::WriteCell( SCTAB nTab
, SCROW nRow
, SCCOL nCol
)
157 const ScPatternAttr
* pAttr
= pDoc
->GetPattern( nCol
, nRow
, nTab
);
159 const ScMergeFlagAttr
& rMergeFlagAttr
= static_cast<const ScMergeFlagAttr
&>( pAttr
->GetItem( ATTR_MERGE_FLAG
) );
160 if ( rMergeFlagAttr
.IsHorOverlapped() )
162 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELL
);
166 bool bValueData
= false;
168 ScAddress
aPos(nCol
, nRow
, nTab
);
169 switch (pDoc
->GetCellType(aPos
))
177 const EditTextObject
* pObj
= pDoc
->GetEditText(aPos
);
180 EditEngine
& rEngine
= GetEditEngine();
181 rEngine
.SetText(*pObj
);
182 aContent
= rEngine
.GetText(LINEEND_LF
); // LineFeed in between paragraphs!
188 bValueData
= pDoc
->HasValueData(aPos
);
189 sal_uLong nFormat
= pAttr
->GetNumberFormat(pFormatter
);
191 aContent
= ScCellFormat::GetString(*pDoc
, aPos
, nFormat
, &pColor
, *pFormatter
);
195 bool bResetAttr(false);
197 const SvxHorJustifyItem
& rHorJustifyItem
= static_cast<const SvxHorJustifyItem
&>(pAttr
->GetItem( ATTR_HOR_JUSTIFY
));
198 const SvxWeightItem
& rWeightItem
= static_cast<const SvxWeightItem
&>( pAttr
->GetItem( ATTR_FONT_WEIGHT
));
199 const SvxPostureItem
& rPostureItem
= static_cast<const SvxPostureItem
&>( pAttr
->GetItem( ATTR_FONT_POSTURE
));
200 const SvxUnderlineItem
& rUnderlineItem
= static_cast<const SvxUnderlineItem
&>( pAttr
->GetItem( ATTR_FONT_UNDERLINE
));
202 const sal_Char
* pChar
;
204 switch( rHorJustifyItem
.GetValue() )
206 case SVX_HOR_JUSTIFY_STANDARD
:
207 pChar
= (bValueData
? OOO_STRING_SVTOOLS_RTF_QR
: OOO_STRING_SVTOOLS_RTF_QL
);
209 case SVX_HOR_JUSTIFY_CENTER
: pChar
= OOO_STRING_SVTOOLS_RTF_QC
; break;
210 case SVX_HOR_JUSTIFY_BLOCK
: pChar
= OOO_STRING_SVTOOLS_RTF_QJ
; break;
211 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 default: pChar
= OOO_STRING_SVTOOLS_RTF_QL
; break;
216 rStrm
.WriteCharPtr( pChar
);
218 if ( rWeightItem
.GetWeight() >= WEIGHT_BOLD
)
221 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_B
);
223 if ( rPostureItem
.GetPosture() != ITALIC_NONE
)
226 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_I
);
228 if ( rUnderlineItem
.GetLineStyle() != UNDERLINE_NONE
)
231 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_UL
);
234 rStrm
.WriteChar( ' ' );
235 RTFOutFuncs::Out_String( rStrm
, aContent
);
236 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELL
);
239 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PLAIN
);
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */