1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* Version: MPL 1.1 / GPLv3+ / LGPLv3+
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License or as specified alternatively below. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* Major Contributor(s):
* Copyright (C) 2012 Miklos Vajna <vmiklos@suse.cz> (SUSE, Inc.)
*
* All Rights Reserved.
*
* For minor contributions see the git repository.
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 3 or later (the "GPLv3+"), or
* the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
* in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
* instead of those above.
*/
#include "rtfexport.hxx"
#include <rtl/oustringostreaminserter.hxx>
#include <svtools/rtfkeywd.hxx>
#include <filter/msfilter/rtfutil.hxx>
SmRtfExport::SmRtfExport(const SmNode* pIn)
: m_pTree(pIn)
, m_pBuffer(0)
{
}
bool SmRtfExport::ConvertFromStarMath(OStringBuffer& rBuffer)
{
if (!m_pTree)
return false;
m_pBuffer = &rBuffer;
m_pBuffer->append("{" OOO_STRING_SVTOOLS_RTF_IGNORE "\\moMath");
HandleNode(m_pTree, 0);
m_pBuffer->append("}");
return true;
}
// NOTE: This is still work in progress and unfinished, but it already covers a good
// part of the rtf math stuff.
void SmRtfExport::HandleNode(const SmNode* pNode, int nLevel)
{
SAL_INFO("starmath.rtf", "Node: " << nLevel << " " << int(pNode->GetType()) << " " << pNode->GetNumSubNodes());
switch(pNode->GetType())
{
case NATTRIBUT:
HandleAttribute( static_cast< const SmAttributNode* >( pNode ), nLevel );
break;
case NTEXT:
HandleText(pNode,nLevel);
break;
case NBRACE:
HandleBrace( static_cast< const SmBraceNode* >( pNode ), nLevel );
break;
case NOPER:
HandleOperator(static_cast<const SmOperNode*>(pNode), nLevel);
break;
case NBINHOR:
HandleBinaryOperation(static_cast<const SmBinHorNode*>(pNode), nLevel);
break;
case NBINVER:
HandleFractions(pNode, nLevel);
break;
case NMATH:
HandleMath(pNode, nLevel);
break;
case NEXPRESSION:
HandleAllSubNodes(pNode, nLevel);
break;
case NTABLE:
//Root Node, PILE equivalent, i.e. vertical stack
HandleTable(pNode,nLevel);
break;
case NLINE:
HandleAllSubNodes(pNode, nLevel);
break;
default:
SAL_INFO("starmath.rtf", "TODO: " << OSL_THIS_FUNC << " unhandled node type");
break;
}
}
//Root Node, PILE equivalent, i.e. vertical stack
void SmRtfExport::HandleTable(const SmNode* pNode, int nLevel)
{
if (nLevel || pNode->GetNumSubNodes() > 1)
HandleVerticalStack(pNode, nLevel);
else
HandleAllSubNodes(pNode, nLevel);
}
void SmRtfExport::HandleAllSubNodes(const SmNode* pNode, int nLevel)
{
int size = pNode->GetNumSubNodes();
for (int i = 0; i < size; ++i)
{
if (!pNode->GetSubNode(i))
{
OSL_FAIL("Subnode is NULL, parent node not handled properly");
continue;
}
HandleNode(pNode->GetSubNode(i), nLevel + 1);
}
}
void SmRtfExport::HandleVerticalStack(const SmNode* /*pNode*/, int /*nLevel*/)
{
SAL_INFO("starmath.rtf", "TODO: " << OSL_THIS_FUNC);
}
void SmRtfExport::HandleText(const SmNode* pNode, int /*nLevel*/)
{
m_pBuffer->append("{\\mr ");
SmTextNode* pTemp=(SmTextNode* )pNode;
SAL_INFO("starmath.rtf", "Text: " << pTemp->GetText());
for (xub_StrLen i = 0; i < pTemp->GetText().Len(); i++)
{
sal_uInt16 nChar = pTemp->GetText().GetChar(i);
OUString aValue(SmTextNode::ConvertSymbolToUnicode(nChar));
m_pBuffer->append(msfilter::rtfutil::OutString(aValue, RTL_TEXTENCODING_MS_1252));
}
m_pBuffer->append("}");
}
void SmRtfExport::HandleFractions(const SmNode* pNode, int nLevel, const char* type)
{
m_pBuffer->append("{\\mf ");
if (type)
{
m_pBuffer->append("{\\mfPr ");
m_pBuffer->append("{\\mtype ");
m_pBuffer->append(type);
m_pBuffer->append("}"); // mtype
m_pBuffer->append("}"); // mfPr
}
OSL_ASSERT(pNode->GetNumSubNodes() == 3);
m_pBuffer->append("{\\mnum ");
HandleNode(pNode->GetSubNode(0), nLevel + 1);
m_pBuffer->append("}"); // mnum
m_pBuffer->append("{\\mden ");
HandleNode(pNode->GetSubNode(2), nLevel + 1);
m_pBuffer->append("}"); // mden
m_pBuffer->append("}"); // mf
}
void SmRtfExport::HandleUnaryOperation(const SmUnHorNode* /*pNode*/, int /*nLevel*/)
{
SAL_INFO("starmath.rtf", "TODO: " << OSL_THIS_FUNC);
}
void SmRtfExport::HandleBinaryOperation(const SmBinHorNode* pNode, int nLevel)
{
SAL_INFO("starmath.rtf", "Binary: " << int(pNode->Symbol()->GetToken().eType));
// update HandleMath() when adding new items
switch (pNode->Symbol()->GetToken().eType)
{
case TDIVIDEBY:
return HandleFractions(pNode, nLevel, "lin");
default:
HandleAllSubNodes(pNode, nLevel);
break;
}
}
void SmRtfExport::HandleAttribute(const SmAttributNode* pNode, int nLevel)
{
switch (pNode->Attribute()->GetToken().eType)
{
case TCHECK:
case TACUTE:
case TGRAVE:
case TBREVE:
case TCIRCLE:
case TVEC:
case TTILDE:
case THAT:
case TDOT:
case TDDOT:
case TDDDOT:
case TWIDETILDE:
case TWIDEHAT:
case TWIDEVEC:
case TBAR:
{
m_pBuffer->append("{\\macc ");
m_pBuffer->append("{\\maccPr ");
m_pBuffer->append("{\\mchr ");
OUString aValue(pNode->Attribute()->GetToken().cMathChar);
m_pBuffer->append(msfilter::rtfutil::OutString(aValue, RTL_TEXTENCODING_MS_1252));
m_pBuffer->append("}"); // mchr
m_pBuffer->append("}"); // maccPr
m_pBuffer->append("{\\me ");
HandleNode( pNode->Body(), nLevel + 1 );
m_pBuffer->append("}"); // me
m_pBuffer->append("}"); // macc
break;
}
case TOVERLINE:
case TUNDERLINE:
m_pBuffer->append("{\\mbar ");
m_pBuffer->append("{\\mbarPr ");
m_pBuffer->append("{\\mpos ");
m_pBuffer->append((pNode->Attribute()->GetToken().eType == TUNDERLINE ) ? "bot" : "top");
m_pBuffer->append("}"); // mpos
m_pBuffer->append("}"); // mbarPr
m_pBuffer->append("{\\me ");
HandleNode( pNode->Body(), nLevel + 1 );
m_pBuffer->append("}"); // me
m_pBuffer->append("}"); // mbar
break;
case TOVERSTRIKE:
m_pBuffer->append("{\\mborderBox ");
m_pBuffer->append("{\\mborderBoxPr ");
m_pBuffer->append("{\\mhideTop 1}");
m_pBuffer->append("{\\mhideBot 1}");
m_pBuffer->append("{\\mhideLeft 1}");
m_pBuffer->append("{\\mhideRight 1}");
m_pBuffer->append("{\\mstrikeH 1}");
m_pBuffer->append("}"); // mborderBoxPr
m_pBuffer->append("{\\me ");
HandleNode( pNode->Body(), nLevel + 1 );
m_pBuffer->append("}"); // me
m_pBuffer->append("}"); // mborderBox
break;
default:
HandleAllSubNodes( pNode, nLevel );
break;
}
}
void SmRtfExport::HandleMath(const SmNode* pNode, int nLevel)
{
SAL_INFO("starmath.rtf", "Math: " << int(pNode->GetToken().eType));
switch (pNode->GetToken().eType)
{
case TDIVIDEBY:
case TACUTE:
// these are handled elsewhere, e.g. when handling BINHOR
OSL_ASSERT(false);
default:
HandleText(pNode, nLevel);
break;
}
}
void SmRtfExport::HandleRoot(const SmRootNode* /*pNode*/, int /*nLevel*/)
{
SAL_INFO("starmath.rtf", "TODO: " << OSL_THIS_FUNC);
}
namespace {
OString mathSymbolToString(const SmNode* node)
{
assert(node->GetType() == NMATH);
const SmTextNode* txtnode = static_cast<const SmTextNode*>(node);
assert(txtnode->GetText().Len() == 1);
sal_Unicode chr = SmTextNode::ConvertSymbolToUnicode(txtnode->GetText().GetChar(0));
OUString aValue(chr);
return msfilter::rtfutil::OutString(aValue, RTL_TEXTENCODING_MS_1252);
}
}
void SmRtfExport::HandleOperator(const SmOperNode* pNode, int nLevel)
{
SAL_INFO("starmath.rtf", "Operator: " << int(pNode->GetToken().eType));
switch (pNode->GetToken().eType)
{
case TINT:
case TIINT:
case TIIINT:
case TLINT:
case TLLINT:
case TLLLINT:
case TPROD:
case TCOPROD:
case TSUM:
{
const SmSubSupNode* subsup = pNode->GetSubNode(0)->GetType() == NSUBSUP ? static_cast<const SmSubSupNode*>(pNode->GetSubNode(0)) : 0;
const SmNode* operation = subsup ? subsup->GetBody() : pNode->GetSubNode(0);
m_pBuffer->append("{\\mnary ");
m_pBuffer->append("{\\mnaryPr ");
m_pBuffer->append("{\\mchr ");
m_pBuffer->append(mathSymbolToString(operation));
m_pBuffer->append("}"); // mchr
if (!subsup || !subsup->GetSubSup(CSUB))
m_pBuffer->append("{\\msubHide 1}");
if (!subsup || !subsup->GetSubSup(CSUP))
m_pBuffer->append("{\\msupHide 1}");
m_pBuffer->append("}"); // mnaryPr
if (!subsup || !subsup->GetSubSup(CSUB))
m_pBuffer->append("{\\msub }");
else
{
m_pBuffer->append("{\\msub ");
HandleNode(subsup->GetSubSup(CSUB), nLevel + 1);
m_pBuffer->append("}"); // msub
}
if (!subsup || !subsup->GetSubSup( CSUP ))
m_pBuffer->append("{\\msup }");
else
{
m_pBuffer->append("{\\msup ");
HandleNode(subsup->GetSubSup(CSUP), nLevel + 1);
m_pBuffer->append("}"); // msup
}
m_pBuffer->append("{\\me ");
HandleNode(pNode->GetSubNode(1), nLevel + 1); // body
m_pBuffer->append("}"); // me
m_pBuffer->append("}"); // mnary
break;
}
case TLIM:
m_pBuffer->append("{\\mfunc ");
m_pBuffer->append("{\\mfName ");
m_pBuffer->append("{\\mlimLow ");
m_pBuffer->append("{\\me ");
HandleNode(pNode->GetSymbol(), nLevel + 1);
m_pBuffer->append("}"); // me
m_pBuffer->append("{\\mlim ");
if (const SmSubSupNode* subsup = pNode->GetSubNode(0)->GetType() == NSUBSUP ? static_cast<const SmSubSupNode*>( pNode->GetSubNode(0)) : 0)
if (subsup->GetSubSup(CSUB))
HandleNode(subsup->GetSubSup(CSUB), nLevel + 1);
m_pBuffer->append("}"); // mlim
m_pBuffer->append("}"); // mlimLow
m_pBuffer->append("}"); // mfName
m_pBuffer->append("{\\me ");
HandleNode(pNode->GetSubNode(1), nLevel + 1); // body
m_pBuffer->append("}"); // me
m_pBuffer->append("}"); // mfunc
break;
default:
SAL_INFO("starmath.rtf", "TODO: " << OSL_THIS_FUNC << " unhandled oper type");
break;
}
}
void SmRtfExport::HandleSubSupScript(const SmSubSupNode* /*pNode*/, int /*nLevel*/)
{
SAL_INFO("starmath.rtf", "TODO: " << OSL_THIS_FUNC);
}
void SmRtfExport::HandleSubSupScriptInternal(const SmSubSupNode* /*pNode*/, int /*nLevel*/, int /*flags*/)
{
SAL_INFO("starmath.rtf", "TODO: " << OSL_THIS_FUNC);
}
void SmRtfExport::HandleMatrix(const SmMatrixNode* /*pNode*/, int /*nLevel*/)
{
SAL_INFO("starmath.rtf", "TODO: " << OSL_THIS_FUNC);
}
void SmRtfExport::HandleBrace(const SmBraceNode* pNode, int nLevel)
{
m_pBuffer->append("{\\md ");
m_pBuffer->append("{\\mdPr ");
m_pBuffer->append("{\\mbegChr ");
m_pBuffer->append(mathSymbolToString(pNode->OpeningBrace()));
m_pBuffer->append("}"); // mbegChr
std::vector< const SmNode* > subnodes;
if (pNode->Body()->GetType() == NBRACEBODY)
{
const SmBracebodyNode* body = static_cast<const SmBracebodyNode*>( pNode->Body());
bool separatorWritten = false; // assume all separators are the same
for (int i = 0; i < body->GetNumSubNodes(); ++i)
{
const SmNode* subnode = body->GetSubNode(i);
if (subnode->GetType() == NMATH)
{ // do not write, but write what separator it is
const SmMathSymbolNode* math = static_cast<const SmMathSymbolNode*>(subnode);
if(!separatorWritten)
{
m_pBuffer->append("{\\msepChr ");
m_pBuffer->append(mathSymbolToString(math));
m_pBuffer->append("}"); // msepChr
separatorWritten = true;
}
}
else
subnodes.push_back(subnode);
}
}
else
subnodes.push_back(pNode->Body());
m_pBuffer->append("{\\mendChr ");
m_pBuffer->append(mathSymbolToString(pNode->ClosingBrace()));
m_pBuffer->append("}"); // mendChr
m_pBuffer->append("}"); // mdPr
for (unsigned int i = 0; i < subnodes.size(); ++i)
{
m_pBuffer->append("{\\me ");
HandleNode(subnodes[ i ], nLevel + 1);
m_pBuffer->append("}"); // me
}
m_pBuffer->append("}"); // md
}
void SmRtfExport::HandleVerticalBrace(const SmVerticalBraceNode* /*pNode*/, int /*nLevel*/)
{
SAL_INFO("starmath.rtf", "TODO: " << OSL_THIS_FUNC);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|