Bump version to 21.06.18.1
[LibreOffice.git] / starmath / source / wordexportbase.cxx
blob7f4699dfb952781f0009f96a45f696db4c3d454c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include "wordexportbase.hxx"
11 #include <node.hxx>
12 #include <sal/log.hxx>
13 #include <osl/diagnose.h>
15 SmWordExportBase::SmWordExportBase(const SmNode* pIn)
16 : m_pTree(pIn)
20 SmWordExportBase::~SmWordExportBase() = default;
22 void SmWordExportBase::HandleNode(const SmNode* pNode, int nLevel)
24 SAL_INFO("starmath.wordbase",
25 "Node: " << nLevel << " " << int(pNode->GetType()) << " " << pNode->GetNumSubNodes());
26 switch (pNode->GetType())
28 case SmNodeType::Attribut:
29 HandleAttribute(static_cast<const SmAttributNode*>(pNode), nLevel);
30 break;
31 case SmNodeType::Text:
32 HandleText(pNode, nLevel);
33 break;
34 case SmNodeType::VerticalBrace:
35 HandleVerticalBrace(static_cast<const SmVerticalBraceNode*>(pNode), nLevel);
36 break;
37 case SmNodeType::Brace:
38 HandleBrace(static_cast<const SmBraceNode*>(pNode), nLevel);
39 break;
40 case SmNodeType::Oper:
41 HandleOperator(static_cast<const SmOperNode*>(pNode), nLevel);
42 break;
43 case SmNodeType::UnHor:
44 HandleUnaryOperation(static_cast<const SmUnHorNode*>(pNode), nLevel);
45 break;
46 case SmNodeType::BinHor:
47 HandleBinaryOperation(static_cast<const SmBinHorNode*>(pNode), nLevel);
48 break;
49 case SmNodeType::BinVer:
50 HandleFractions(pNode, nLevel, nullptr);
51 break;
52 case SmNodeType::Root:
53 HandleRoot(static_cast<const SmRootNode*>(pNode), nLevel);
54 break;
55 case SmNodeType::Special:
57 auto pText = static_cast<const SmTextNode*>(pNode);
58 //if the token str and the result text are the same then this
59 //is to be seen as text, else assume it's a mathchar
60 if (pText->GetText() == pText->GetToken().aText)
61 HandleText(pText, nLevel);
62 else
63 HandleMath(pText, nLevel);
64 break;
66 case SmNodeType::Math:
67 case SmNodeType::MathIdent:
68 HandleMath(pNode, nLevel);
69 break;
70 case SmNodeType::SubSup:
71 HandleSubSupScript(static_cast<const SmSubSupNode*>(pNode), nLevel);
72 break;
73 case SmNodeType::Expression:
74 HandleAllSubNodes(pNode, nLevel);
75 break;
76 case SmNodeType::Table:
77 //Root Node, PILE equivalent, i.e. vertical stack
78 HandleTable(pNode, nLevel);
79 break;
80 case SmNodeType::Matrix:
81 HandleMatrix(static_cast<const SmMatrixNode*>(pNode), nLevel);
82 break;
83 case SmNodeType::Line:
85 // TODO
86 HandleAllSubNodes(pNode, nLevel);
88 break;
89 #if 0
90 case SmNodeType::Align:
91 HandleMAlign(pNode,nLevel);
92 break;
93 #endif
94 case SmNodeType::Place:
95 // explicitly do nothing, MSOffice treats that as a placeholder if item is missing
96 break;
97 case SmNodeType::Blank:
98 HandleBlank();
99 break;
100 default:
101 HandleAllSubNodes(pNode, nLevel);
102 break;
106 //Root Node, PILE equivalent, i.e. vertical stack
107 void SmWordExportBase::HandleTable(const SmNode* pNode, int nLevel)
109 //The root of the starmath is a table, if
110 //we convert this them each iteration of
111 //conversion from starmath to Word will
112 //add an extra unnecessary level to the
113 //Word output stack which would grow
114 //without bound in a multi step conversion
115 if (nLevel || pNode->GetNumSubNodes() > 1)
116 HandleVerticalStack(pNode, nLevel);
117 else
118 HandleAllSubNodes(pNode, nLevel);
121 void SmWordExportBase::HandleAllSubNodes(const SmNode* pNode, int nLevel)
123 int size = pNode->GetNumSubNodes();
124 for (int i = 0; i < size; ++i)
126 // TODO remove when all types of nodes are handled properly
127 if (pNode->GetSubNode(i) == nullptr)
129 SAL_WARN("starmath.wordbase", "Subnode is NULL, parent node not handled properly");
130 continue;
132 HandleNode(pNode->GetSubNode(i), nLevel + 1);
136 void SmWordExportBase::HandleUnaryOperation(const SmUnHorNode* pNode, int nLevel)
138 // update HandleMath() when adding new items
139 SAL_INFO("starmath.wordbase", "Unary: " << int(pNode->GetToken().eType));
141 HandleAllSubNodes(pNode, nLevel);
144 void SmWordExportBase::HandleBinaryOperation(const SmBinHorNode* pNode, int nLevel)
146 SAL_INFO("starmath.wordbase", "Binary: " << int(pNode->Symbol()->GetToken().eType));
147 // update HandleMath() when adding new items
148 switch (pNode->Symbol()->GetToken().eType)
150 case TDIVIDEBY:
151 return HandleFractions(pNode, nLevel, "lin");
152 default:
153 HandleAllSubNodes(pNode, nLevel);
154 break;
158 void SmWordExportBase::HandleMath(const SmNode* pNode, int nLevel)
160 SAL_INFO("starmath.wordbase", "Math: " << int(pNode->GetToken().eType));
161 switch (pNode->GetToken().eType)
163 case TDIVIDEBY:
164 case TACUTE:
165 OSL_ASSERT(false);
166 [[fallthrough]]; // the above are handled elsewhere, e.g. when handling BINHOR
167 default:
168 HandleText(pNode, nLevel);
169 break;
173 void SmWordExportBase::HandleSubSupScript(const SmSubSupNode* pNode, int nLevel)
175 // set flags to a bitfield of which sub/sup items exists
176 int flags = (pNode->GetSubSup(CSUB) != nullptr ? (1 << CSUB) : 0)
177 | (pNode->GetSubSup(CSUP) != nullptr ? (1 << CSUP) : 0)
178 | (pNode->GetSubSup(RSUB) != nullptr ? (1 << RSUB) : 0)
179 | (pNode->GetSubSup(RSUP) != nullptr ? (1 << RSUP) : 0)
180 | (pNode->GetSubSup(LSUB) != nullptr ? (1 << LSUB) : 0)
181 | (pNode->GetSubSup(LSUP) != nullptr ? (1 << LSUP) : 0);
182 HandleSubSupScriptInternal(pNode, nLevel, flags);
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */