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/.
11 #include "rtfexport.hxx"
13 #include <rtl/ustring.hxx>
14 #include <svtools/rtfkeywd.hxx>
15 #include <filter/msfilter/rtfutil.hxx>
17 SmRtfExport::SmRtfExport(const SmNode
* pIn
)
18 : SmWordExportBase(pIn
)
23 bool SmRtfExport::ConvertFromStarMath(OStringBuffer
& rBuffer
, rtl_TextEncoding nEncoding
)
28 m_nEncoding
= nEncoding
;
29 m_pBuffer
->append("{" OOO_STRING_SVTOOLS_RTF_IGNORE LO_STRING_SVTOOLS_RTF_MOMATH
" ");
30 HandleNode(m_pTree
, 0);
31 m_pBuffer
->append("}"); // moMath
35 // NOTE: This is still work in progress and unfinished, but it already covers a good
36 // part of the rtf math stuff.
38 void SmRtfExport::HandleVerticalStack(const SmNode
* pNode
, int nLevel
)
40 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MEQARR
" ");
41 int size
= pNode
->GetNumSubNodes();
42 for (int i
= 0; i
< size
; ++i
)
44 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_ME
" ");
45 HandleNode(pNode
->GetSubNode( i
), nLevel
+ 1);
46 m_pBuffer
->append("}"); // me
48 m_pBuffer
->append("}"); // meqArr
51 void SmRtfExport::HandleText(const SmNode
* pNode
, int /*nLevel*/)
53 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MR
" ");
55 if( pNode
->GetToken().eType
== TTEXT
) // literal text
56 m_pBuffer
->append(LO_STRING_SVTOOLS_RTF_MNOR
" ");
58 SmTextNode
* pTemp
=(SmTextNode
* )pNode
;
59 SAL_INFO("starmath.rtf", "Text: " << pTemp
->GetText());
60 for (sal_Int32 i
= 0; i
< pTemp
->GetText().getLength(); i
++)
62 sal_uInt16 nChar
= pTemp
->GetText()[i
];
63 OUString
aValue(SmTextNode::ConvertSymbolToUnicode(nChar
));
64 m_pBuffer
->append(msfilter::rtfutil::OutString(aValue
, m_nEncoding
));
67 m_pBuffer
->append("}"); // mr
70 void SmRtfExport::HandleFractions(const SmNode
* pNode
, int nLevel
, const char* type
)
72 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MF
" ");
75 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MFPR
" ");
76 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MTYPE
" ");
77 m_pBuffer
->append(type
);
78 m_pBuffer
->append("}"); // mtype
79 m_pBuffer
->append("}"); // mfPr
81 OSL_ASSERT(pNode
->GetNumSubNodes() == 3);
82 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MNUM
" ");
83 HandleNode(pNode
->GetSubNode(0), nLevel
+ 1);
84 m_pBuffer
->append("}"); // mnum
85 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MDEN
" ");
86 HandleNode(pNode
->GetSubNode(2), nLevel
+ 1);
87 m_pBuffer
->append("}"); // mden
88 m_pBuffer
->append("}"); // mf
91 void SmRtfExport::HandleAttribute(const SmAttributNode
* pNode
, int nLevel
)
93 switch (pNode
->Attribute()->GetToken().eType
)
111 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MACC
" ");
112 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MACCPR
" ");
113 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MCHR
" ");
114 OUString
aValue(pNode
->Attribute()->GetToken().cMathChar
);
115 m_pBuffer
->append(msfilter::rtfutil::OutString(aValue
, m_nEncoding
));
116 m_pBuffer
->append("}"); // mchr
117 m_pBuffer
->append("}"); // maccPr
118 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_ME
" ");
119 HandleNode( pNode
->Body(), nLevel
+ 1 );
120 m_pBuffer
->append("}"); // me
121 m_pBuffer
->append("}"); // macc
126 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MBAR
" ");
127 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MBARPR
" ");
128 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MPOS
" ");
129 m_pBuffer
->append((pNode
->Attribute()->GetToken().eType
== TUNDERLINE
) ? "bot" : "top");
130 m_pBuffer
->append("}"); // mpos
131 m_pBuffer
->append("}"); // mbarPr
132 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_ME
" ");
133 HandleNode( pNode
->Body(), nLevel
+ 1 );
134 m_pBuffer
->append("}"); // me
135 m_pBuffer
->append("}"); // mbar
138 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MBORDERBOX
" ");
139 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MBORDERBOXPR
" ");
140 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MHIDETOP
" 1}");
141 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MHIDEBOT
" 1}");
142 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MHIDELEFT
" 1}");
143 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MHIDERIGHT
" 1}");
144 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MSTRIKEH
" 1}");
145 m_pBuffer
->append("}"); // mborderBoxPr
146 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_ME
" ");
147 HandleNode( pNode
->Body(), nLevel
+ 1 );
148 m_pBuffer
->append("}"); // me
149 m_pBuffer
->append("}"); // mborderBox
152 HandleAllSubNodes( pNode
, nLevel
);
157 void SmRtfExport::HandleRoot(const SmRootNode
* pNode
, int nLevel
)
159 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MRAD
" ");
160 if (const SmNode
* argument
= pNode
->Argument())
162 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MDEG
" ");
163 HandleNode(argument
, nLevel
+ 1);
164 m_pBuffer
->append("}"); // mdeg
168 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MRADPR
" ");
169 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MDEGHIDE
" 1}");
170 m_pBuffer
->append("}"); // mradPr
171 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MDEG
" }"); // empty but present
173 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_ME
" ");
174 HandleNode(pNode
->Body(), nLevel
+ 1);
175 m_pBuffer
->append("}"); // me
176 m_pBuffer
->append("}"); // mrad
180 OString
mathSymbolToString(const SmNode
* node
, rtl_TextEncoding nEncoding
)
182 assert(node
->GetType() == NMATH
);
183 const SmTextNode
* txtnode
= static_cast<const SmTextNode
*>(node
);
184 if (txtnode
->GetText().isEmpty())
186 assert(txtnode
->GetText().getLength() == 1);
187 sal_Unicode chr
= SmTextNode::ConvertSymbolToUnicode(txtnode
->GetText()[0]);
188 OUString
aValue(chr
);
189 return msfilter::rtfutil::OutString(aValue
, nEncoding
);
193 void SmRtfExport::HandleOperator(const SmOperNode
* pNode
, int nLevel
)
195 SAL_INFO("starmath.rtf", "Operator: " << int(pNode
->GetToken().eType
));
196 switch (pNode
->GetToken().eType
)
208 const SmSubSupNode
* subsup
= pNode
->GetSubNode(0)->GetType() == NSUBSUP
? static_cast<const SmSubSupNode
*>(pNode
->GetSubNode(0)) : 0;
209 const SmNode
* operation
= subsup
? subsup
->GetBody() : pNode
->GetSubNode(0);
210 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MNARY
" ");
211 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MNARYPR
" ");
212 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MCHR
" ");
213 m_pBuffer
->append(mathSymbolToString(operation
, m_nEncoding
));
214 m_pBuffer
->append("}"); // mchr
215 if (!subsup
|| !subsup
->GetSubSup(CSUB
))
216 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MSUBHIDE
" 1}");
217 if (!subsup
|| !subsup
->GetSubSup(CSUP
))
218 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MSUPHIDE
" 1}");
219 m_pBuffer
->append("}"); // mnaryPr
220 if (!subsup
|| !subsup
->GetSubSup(CSUB
))
221 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MSUB
" }");
224 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MSUB
" ");
225 HandleNode(subsup
->GetSubSup(CSUB
), nLevel
+ 1);
226 m_pBuffer
->append("}"); // msub
228 if (!subsup
|| !subsup
->GetSubSup( CSUP
))
229 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MSUP
" }");
232 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MSUP
" ");
233 HandleNode(subsup
->GetSubSup(CSUP
), nLevel
+ 1);
234 m_pBuffer
->append("}"); // msup
236 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_ME
" ");
237 HandleNode(pNode
->GetSubNode(1), nLevel
+ 1); // body
238 m_pBuffer
->append("}"); // me
239 m_pBuffer
->append("}"); // mnary
243 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MFUNC
" ");
244 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MFNAME
" ");
245 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MLIMLOW
" ");
246 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_ME
" ");
247 HandleNode(pNode
->GetSymbol(), nLevel
+ 1);
248 m_pBuffer
->append("}"); // me
249 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MLIM
" ");
250 if (const SmSubSupNode
* subsup
= pNode
->GetSubNode(0)->GetType() == NSUBSUP
? static_cast<const SmSubSupNode
*>( pNode
->GetSubNode(0)) : 0)
251 if (subsup
->GetSubSup(CSUB
))
252 HandleNode(subsup
->GetSubSup(CSUB
), nLevel
+ 1);
253 m_pBuffer
->append("}"); // mlim
254 m_pBuffer
->append("}"); // mlimLow
255 m_pBuffer
->append("}"); // mfName
256 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_ME
" ");
257 HandleNode(pNode
->GetSubNode(1), nLevel
+ 1); // body
258 m_pBuffer
->append("}"); // me
259 m_pBuffer
->append("}"); // mfunc
262 SAL_INFO("starmath.rtf", "TODO: " << OSL_THIS_FUNC
<< " unhandled oper type");
267 void SmRtfExport::HandleSubSupScriptInternal(const SmSubSupNode
* pNode
, int nLevel
, int flags
)
269 // rtf supports only a certain combination of sub/super scripts, but LO can have any,
270 // so try to merge it using several tags if necessary
271 if (flags
== 0) // none
273 if ((flags
& (1 << RSUP
| 1 << RSUB
)) == (1 << RSUP
| 1 << RSUB
))
275 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MSSUBSUP
" ");
276 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_ME
" ");
277 flags
&= ~(1 << RSUP
| 1 << RSUB
);
279 HandleNode(pNode
->GetBody(), nLevel
+ 1);
281 HandleSubSupScriptInternal(pNode
, nLevel
, flags
);
282 m_pBuffer
->append("}"); // me
283 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MSUB
" ");
284 HandleNode(pNode
->GetSubSup(RSUB
), nLevel
+ 1);
285 m_pBuffer
->append("}"); // msub
286 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MSUP
" ");
287 HandleNode(pNode
->GetSubSup(RSUP
), nLevel
+ 1);
288 m_pBuffer
->append("}"); // msup
289 m_pBuffer
->append("}"); // msubSup
291 else if ((flags
& (1 << RSUB
)) == 1 << RSUB
)
293 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MSSUB
" ");
294 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_ME
" ");
295 flags
&= ~(1 << RSUB
);
297 HandleNode(pNode
->GetBody(), nLevel
+ 1);
299 HandleSubSupScriptInternal(pNode
, nLevel
, flags
);
300 m_pBuffer
->append("}"); // me
301 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MSUB
" ");
302 HandleNode(pNode
->GetSubSup(RSUB
), nLevel
+ 1);
303 m_pBuffer
->append("}"); // msub
304 m_pBuffer
->append("}"); // msSub
306 else if ((flags
& (1 << RSUP
)) == 1 << RSUP
)
308 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MSSUP
" ");
309 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_ME
" ");
310 flags
&= ~(1 << RSUP
);
312 HandleNode(pNode
->GetBody(), nLevel
+ 1);
314 HandleSubSupScriptInternal(pNode
, nLevel
, flags
);
315 m_pBuffer
->append("}"); // me
316 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MSUP
" ");
317 HandleNode(pNode
->GetSubSup(RSUP
), nLevel
+ 1);
318 m_pBuffer
->append("}"); // msup
319 m_pBuffer
->append("}"); // msSup
321 else if ((flags
& (1 << LSUP
| 1 << LSUB
)) == (1 << LSUP
| 1 << LSUB
))
323 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MSPRE
" ");
324 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MSUB
" ");
325 HandleNode(pNode
->GetSubSup(LSUB
), nLevel
+ 1);
326 m_pBuffer
->append("}"); // msub
327 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MSUP
" ");
328 HandleNode(pNode
->GetSubSup(LSUP
), nLevel
+ 1);
329 m_pBuffer
->append("}"); // msup
330 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_ME
" ");
331 flags
&= ~(1 << LSUP
| 1 << LSUB
);
333 HandleNode(pNode
->GetBody(), nLevel
+ 1);
335 HandleSubSupScriptInternal(pNode
, nLevel
, flags
);
336 m_pBuffer
->append("}"); // me
337 m_pBuffer
->append("}"); // msPre
339 else if ((flags
& (1 << CSUB
)) == (1 << CSUB
))
340 { // m:limLow looks like a good element for central superscript
341 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MLIMLOW
" ");
342 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_ME
" ");
343 flags
&= ~(1 << CSUB
);
345 HandleNode(pNode
->GetBody(), nLevel
+ 1);
347 HandleSubSupScriptInternal(pNode
, nLevel
, flags
);
348 m_pBuffer
->append("}"); // me
349 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MLIM
" ");
350 HandleNode(pNode
->GetSubSup(CSUB
), nLevel
+ 1);
351 m_pBuffer
->append("}"); // mlim
352 m_pBuffer
->append("}"); // mlimLow
354 else if ((flags
& (1 << CSUP
)) == (1 << CSUP
))
355 { // m:limUpp looks like a good element for central superscript
356 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MLIMUPP
" ");
357 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_ME
" ");
358 flags
&= ~(1 << CSUP
);
360 HandleNode(pNode
->GetBody(), nLevel
+ 1);
362 HandleSubSupScriptInternal(pNode
, nLevel
, flags
);
363 m_pBuffer
->append("}"); // me
364 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MLIM
" ");
365 HandleNode(pNode
->GetSubSup(CSUP
), nLevel
+ 1);
366 m_pBuffer
->append("}"); // mlim
367 m_pBuffer
->append("}"); // mlimUpp
370 SAL_INFO("starmath.rtf", "TODO: " << OSL_THIS_FUNC
<< " unhandled subsup type");
373 void SmRtfExport::HandleMatrix(const SmMatrixNode
* pNode
, int nLevel
)
375 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MM
" ");
376 for (int row
= 0; row
< pNode
->GetNumRows(); ++row
)
378 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MMR
" ");
379 for (int col
= 0; col
< pNode
->GetNumCols(); ++col
)
381 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_ME
" ");
382 if (const SmNode
* node
= pNode
->GetSubNode(row
* pNode
->GetNumCols() + col
))
383 HandleNode(node
, nLevel
+ 1);
384 m_pBuffer
->append("}"); // me
386 m_pBuffer
->append("}"); // mmr
388 m_pBuffer
->append("}"); // mm
391 void SmRtfExport::HandleBrace(const SmBraceNode
* pNode
, int nLevel
)
393 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MD
" ");
394 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MDPR
" ");
395 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MBEGCHR
" ");
396 m_pBuffer
->append(mathSymbolToString(pNode
->OpeningBrace(), m_nEncoding
));
397 m_pBuffer
->append("}"); // mbegChr
398 std::vector
< const SmNode
* > subnodes
;
399 if (pNode
->Body()->GetType() == NBRACEBODY
)
401 const SmBracebodyNode
* body
= static_cast<const SmBracebodyNode
*>( pNode
->Body());
402 bool separatorWritten
= false; // assume all separators are the same
403 for (int i
= 0; i
< body
->GetNumSubNodes(); ++i
)
405 const SmNode
* subnode
= body
->GetSubNode(i
);
406 if (subnode
->GetType() == NMATH
)
407 { // do not write, but write what separator it is
408 const SmMathSymbolNode
* math
= static_cast<const SmMathSymbolNode
*>(subnode
);
409 if(!separatorWritten
)
411 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MSEPCHR
" ");
412 m_pBuffer
->append(mathSymbolToString(math
, m_nEncoding
));
413 m_pBuffer
->append("}"); // msepChr
414 separatorWritten
= true;
418 subnodes
.push_back(subnode
);
422 subnodes
.push_back(pNode
->Body());
423 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MENDCHR
" ");
424 m_pBuffer
->append(mathSymbolToString(pNode
->ClosingBrace(), m_nEncoding
));
425 m_pBuffer
->append("}"); // mendChr
426 m_pBuffer
->append("}"); // mdPr
427 for (unsigned int i
= 0; i
< subnodes
.size(); ++i
)
429 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_ME
" ");
430 HandleNode(subnodes
[ i
], nLevel
+ 1);
431 m_pBuffer
->append("}"); // me
433 m_pBuffer
->append("}"); // md
436 void SmRtfExport::HandleVerticalBrace(const SmVerticalBraceNode
* pNode
, int nLevel
)
438 SAL_INFO("starmath.rtf", "Vertical: " << int(pNode
->GetToken().eType
));
439 switch (pNode
->GetToken().eType
)
444 bool top
= (pNode
->GetToken().eType
== TOVERBRACE
);
446 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MLIMUPP
" ");
448 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MLIMLOW
" ");
449 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_ME
" ");
450 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MGROUPCHR
" ");
451 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MGROUPCHRPR
" ");
452 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MCHR
" ");
453 m_pBuffer
->append(mathSymbolToString(pNode
->Brace(), m_nEncoding
));
454 m_pBuffer
->append("}"); // mchr
455 // TODO not sure if pos and vertJc are correct
456 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MPOS
" ").append(top
? "top" : "bot").append("}");
457 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MVERTJC
" ").append(top
? "bot" : "top").append("}");
458 m_pBuffer
->append("}"); // mgroupChrPr
459 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_ME
" ");
460 HandleNode(pNode
->Body(), nLevel
+ 1);
461 m_pBuffer
->append("}"); // me
462 m_pBuffer
->append("}"); // mgroupChr
463 m_pBuffer
->append("}"); // me
464 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MLIM
" ");
465 HandleNode(pNode
->Script(), nLevel
+ 1);
466 m_pBuffer
->append("}"); // mlim
467 m_pBuffer
->append("}"); // mlimUpp or mlimLow
471 SAL_INFO("starmath.rtf", "TODO: " << OSL_THIS_FUNC
<< " unhandled vertical brace type");
476 void SmRtfExport::HandleBlank()
478 m_pBuffer
->append("{" LO_STRING_SVTOOLS_RTF_MR
" ");
479 m_pBuffer
->append(" ");
480 m_pBuffer
->append("}"); // mr
483 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */