LibreOffice Module starmath (master) 1
wordexportbase.cxx
Go to the documentation of this file.
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
10#include "wordexportbase.hxx"
11#include <sal/log.hxx>
12#include <osl/diagnose.h>
13
15 : m_pTree(pIn)
16{
17}
18
20
21void SmWordExportBase::HandleNode(const SmNode* pNode, int nLevel)
22{
23 SAL_INFO("starmath.wordbase",
24 "Node: " << nLevel << " " << int(pNode->GetType()) << " " << pNode->GetNumSubNodes());
25 switch (pNode->GetType())
26 {
28 HandleAttribute(static_cast<const SmAttributeNode*>(pNode), nLevel);
29 break;
31 HandleText(pNode, nLevel);
32 break;
34 HandleVerticalBrace(static_cast<const SmVerticalBraceNode*>(pNode), nLevel);
35 break;
37 HandleBrace(static_cast<const SmBraceNode*>(pNode), nLevel);
38 break;
40 HandleOperator(static_cast<const SmOperNode*>(pNode), nLevel);
41 break;
43 HandleUnaryOperation(static_cast<const SmUnHorNode*>(pNode), nLevel);
44 break;
46 HandleBinaryOperation(static_cast<const SmBinHorNode*>(pNode), nLevel);
47 break;
49 HandleFractions(pNode, nLevel, nullptr);
50 break;
52 HandleRoot(static_cast<const SmRootNode*>(pNode), nLevel);
53 break;
55 {
56 auto pText = static_cast<const SmTextNode*>(pNode);
57 //if the token str and the result text are the same then this
58 //is to be seen as text, else assume it's a mathchar
59 if (pText->GetText() == pText->GetToken().aText)
60 HandleText(pText, nLevel);
61 else
62 HandleMath(pText, nLevel);
63 break;
64 }
67 HandleMath(pNode, nLevel);
68 break;
70 HandleSubSupScript(static_cast<const SmSubSupNode*>(pNode), nLevel);
71 break;
73 HandleAllSubNodes(pNode, nLevel);
74 break;
76 //Root Node, PILE equivalent, i.e. vertical stack
77 HandleTable(pNode, nLevel);
78 break;
80 HandleMatrix(static_cast<const SmMatrixNode*>(pNode), nLevel);
81 break;
83 {
84 // TODO
85 HandleAllSubNodes(pNode, nLevel);
86 }
87 break;
88#if 0
90 HandleMAlign(pNode,nLevel);
91 break;
92#endif
94 // explicitly do nothing, MSOffice treats that as a placeholder if item is missing
95 break;
98 break;
99 default:
100 HandleAllSubNodes(pNode, nLevel);
101 break;
102 }
103}
104
105//Root Node, PILE equivalent, i.e. vertical stack
106void SmWordExportBase::HandleTable(const SmNode* pNode, int nLevel)
107{
108 //The root of the starmath is a table, if
109 //we convert this them each iteration of
110 //conversion from starmath to Word will
111 //add an extra unnecessary level to the
112 //Word output stack which would grow
113 //without bound in a multi step conversion
114 if (nLevel || pNode->GetNumSubNodes() > 1)
115 HandleVerticalStack(pNode, nLevel);
116 else
117 HandleAllSubNodes(pNode, nLevel);
118}
119
120void SmWordExportBase::HandleAllSubNodes(const SmNode* pNode, int nLevel)
121{
122 int size = pNode->GetNumSubNodes();
123 for (int i = 0; i < size; ++i)
124 {
125 // TODO remove when all types of nodes are handled properly
126 if (pNode->GetSubNode(i) == nullptr)
127 {
128 SAL_WARN("starmath.wordbase", "Subnode is NULL, parent node not handled properly");
129 continue;
130 }
131 HandleNode(pNode->GetSubNode(i), nLevel + 1);
132 }
133}
134
136{
137 // update HandleMath() when adding new items
138 SAL_INFO("starmath.wordbase", "Unary: " << int(pNode->GetToken().eType));
139
140 HandleAllSubNodes(pNode, nLevel);
141}
142
144{
145 SAL_INFO("starmath.wordbase", "Binary: " << int(pNode->Symbol()->GetToken().eType));
146 // update HandleMath() when adding new items
147 switch (pNode->Symbol()->GetToken().eType)
148 {
149 case TDIVIDEBY:
150 return HandleFractions(pNode, nLevel, "lin");
151 default:
152 HandleAllSubNodes(pNode, nLevel);
153 break;
154 }
155}
156
157void SmWordExportBase::HandleMath(const SmNode* pNode, int nLevel)
158{
159 SAL_INFO("starmath.wordbase", "Math: " << int(pNode->GetToken().eType));
160 switch (pNode->GetToken().eType)
161 {
162 case TDIVIDEBY:
163 case TACUTE:
164 OSL_ASSERT(false);
165 [[fallthrough]]; // the above are handled elsewhere, e.g. when handling BINHOR
166 default:
167 HandleText(pNode, nLevel);
168 break;
169 }
170}
171
173{
174 // set flags to a bitfield of which sub/sup items exists
175 int flags = (pNode->GetSubSup(CSUB) != nullptr ? (1 << CSUB) : 0)
176 | (pNode->GetSubSup(CSUP) != nullptr ? (1 << CSUP) : 0)
177 | (pNode->GetSubSup(RSUB) != nullptr ? (1 << RSUB) : 0)
178 | (pNode->GetSubSup(RSUP) != nullptr ? (1 << RSUP) : 0)
179 | (pNode->GetSubSup(LSUB) != nullptr ? (1 << LSUB) : 0)
180 | (pNode->GetSubSup(LSUP) != nullptr ? (1 << LSUP) : 0);
181 HandleSubSupScriptInternal(pNode, nLevel, flags);
182}
183
184/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Attribute node.
Definition: node.hxx:1890
Binary horizontal node.
Definition: node.hxx:1368
const SmNode * Symbol() const
Returns the node containing the data of the binary operator.
Definition: node.hxx:1393
Node for brace construction.
Definition: node.hxx:1646
Matrix node.
Definition: node.hxx:2000
Definition: node.hxx:125
virtual size_t GetNumSubNodes() const =0
Gets the number of subnodes.
const SmToken & GetToken() const
Gets the token.
Definition: node.hxx:387
virtual SmNode * GetSubNode(size_t nIndex)=0
Gets the subnode of index nIndex.
SmNodeType GetType() const
Gets the node type.
Definition: node.hxx:379
Operation Node.
Definition: node.hxx:1811
Root node.
Definition: node.hxx:1307
Super- and subscript node.
Definition: node.hxx:1559
const SmNode * GetSubSup(SmSubSup eSubSup) const
Gets the node with the data of what has to be superindex or subindex.
Definition: node.hxx:1595
Text node.
Definition: node.hxx:747
Unary horizontal node.
Definition: node.hxx:1272
Node for vertical brace construction.
Definition: node.hxx:1750
virtual void HandleFractions(const SmNode *pNode, int nLevel, const char *type)=0
virtual void HandleSubSupScriptInternal(const SmSubSupNode *pNode, int nLevel, int flags)=0
virtual void HandleVerticalStack(const SmNode *pNode, int nLevel)=0
void HandleAllSubNodes(const SmNode *pNode, int nLevel)
SmWordExportBase(const SmNode *pIn)
virtual void HandleBrace(const SmBraceNode *pNode, int nLevel)=0
void HandleMath(const SmNode *pNode, int nLevel)
void HandleBinaryOperation(const SmBinHorNode *pNode, int nLevel)
virtual void HandleVerticalBrace(const SmVerticalBraceNode *pNode, int nLevel)=0
virtual void HandleRoot(const SmRootNode *pNode, int nLevel)=0
virtual void HandleMatrix(const SmMatrixNode *pNode, int nLevel)=0
virtual void HandleOperator(const SmOperNode *pNode, int nLevel)=0
void HandleUnaryOperation(const SmUnHorNode *pNode, int nLevel)
virtual void HandleBlank()=0
virtual void HandleText(const SmNode *pNode, int nLevel)=0
virtual void HandleAttribute(const SmAttributeNode *pNode, int nLevel)=0
void HandleSubSupScript(const SmSubSupNode *pNode, int nLevel)
void HandleNode(const SmNode *pNode, int nLevel)
void HandleTable(const SmNode *pNode, int nLevel)
virtual ~SmWordExportBase()
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
size
int i
@ LSUB
Definition: node.hxx:1536
@ CSUB
Definition: node.hxx:1536
@ RSUP
Definition: node.hxx:1536
@ RSUB
Definition: node.hxx:1536
@ LSUP
Definition: node.hxx:1536
@ CSUP
Definition: node.hxx:1536
SmTokenType eType
Definition: token.hxx:213
@ TDIVIDEBY
Definition: token.hxx:79
@ TACUTE
Definition: token.hxx:120