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/.
10 #include "wordexportbase.hxx"
13 SmWordExportBase::SmWordExportBase(const SmNode
* pIn
)
18 SmWordExportBase::~SmWordExportBase() = default;
20 void SmWordExportBase::HandleNode(const SmNode
* pNode
, int nLevel
)
22 SAL_INFO("starmath.wordbase", "Node: " << nLevel
<< " " << int(pNode
->GetType()) << " " << pNode
->GetNumSubNodes());
23 switch (pNode
->GetType())
25 case SmNodeType::Attribut
:
26 HandleAttribute(static_cast< const SmAttributNode
* >(pNode
), nLevel
);
28 case SmNodeType::Text
:
29 HandleText(pNode
,nLevel
);
31 case SmNodeType::VerticalBrace
:
32 HandleVerticalBrace(static_cast< const SmVerticalBraceNode
* >(pNode
), nLevel
);
34 case SmNodeType::Brace
:
35 HandleBrace(static_cast< const SmBraceNode
* >(pNode
), nLevel
);
37 case SmNodeType::Oper
:
38 HandleOperator(static_cast< const SmOperNode
* >(pNode
), nLevel
);
40 case SmNodeType::UnHor
:
41 HandleUnaryOperation(static_cast< const SmUnHorNode
* >(pNode
), nLevel
);
43 case SmNodeType::BinHor
:
44 HandleBinaryOperation(static_cast< const SmBinHorNode
* >(pNode
), nLevel
);
46 case SmNodeType::BinVer
:
47 HandleFractions(pNode
,nLevel
,nullptr);
49 case SmNodeType::Root
:
50 HandleRoot(static_cast< const SmRootNode
* >(pNode
), nLevel
);
52 case SmNodeType::Special
:
54 auto pText
= static_cast<const SmTextNode
*>(pNode
);
55 //if the token str and the result text are the same then this
56 //is to be seen as text, else assume it's a mathchar
57 if (pText
->GetText() == pText
->GetToken().aText
)
58 HandleText(pText
,nLevel
);
60 HandleMath(pText
,nLevel
);
63 case SmNodeType::Math
:
64 case SmNodeType::MathIdent
:
65 HandleMath(pNode
,nLevel
);
67 case SmNodeType::SubSup
:
68 HandleSubSupScript(static_cast< const SmSubSupNode
* >(pNode
), nLevel
);
70 case SmNodeType::Expression
:
71 HandleAllSubNodes(pNode
, nLevel
);
73 case SmNodeType::Table
:
74 //Root Node, PILE equivalent, i.e. vertical stack
75 HandleTable(pNode
,nLevel
);
77 case SmNodeType::Matrix
:
78 HandleMatrix(static_cast< const SmMatrixNode
* >(pNode
), nLevel
);
80 case SmNodeType::Line
:
83 HandleAllSubNodes(pNode
, nLevel
);
87 case SmNodeType::Align
:
88 HandleMAlign(pNode
,nLevel
);
91 case SmNodeType::Place
:
92 // explicitly do nothing, MSOffice treats that as a placeholder if item is missing
94 case SmNodeType::Blank
:
98 HandleAllSubNodes(pNode
, nLevel
);
103 //Root Node, PILE equivalent, i.e. vertical stack
104 void SmWordExportBase::HandleTable(const SmNode
* pNode
, int nLevel
)
106 //The root of the starmath is a table, if
107 //we convert this them each iteration of
108 //conversion from starmath to Word will
109 //add an extra unnecessary level to the
110 //Word output stack which would grow
111 //without bound in a multi step conversion
112 if (nLevel
|| pNode
->GetNumSubNodes() > 1)
113 HandleVerticalStack(pNode
, nLevel
);
115 HandleAllSubNodes(pNode
, nLevel
);
118 void SmWordExportBase::HandleAllSubNodes(const SmNode
* pNode
, int nLevel
)
120 int size
= pNode
->GetNumSubNodes();
125 // TODO remove when all types of nodes are handled properly
126 if (pNode
->GetSubNode(i
) == nullptr)
128 SAL_WARN("starmath.wordbase", "Subnode is NULL, parent node not handled properly");
131 HandleNode(pNode
->GetSubNode(i
), nLevel
+ 1);
135 void SmWordExportBase::HandleUnaryOperation(const SmUnHorNode
* pNode
, int nLevel
)
137 // update HandleMath() when adding new items
138 SAL_INFO("starmath.wordbase", "Unary: " << int(pNode
->GetToken().eType
));
140 HandleAllSubNodes(pNode
, nLevel
);
143 void SmWordExportBase::HandleBinaryOperation(const SmBinHorNode
* pNode
, int nLevel
)
145 SAL_INFO("starmath.wordbase", "Binary: " << int(pNode
->Symbol()->GetToken().eType
));
146 // update HandleMath() when adding new items
147 switch (pNode
->Symbol()->GetToken().eType
)
150 return HandleFractions(pNode
, nLevel
, "lin");
152 HandleAllSubNodes(pNode
, nLevel
);
157 void SmWordExportBase::HandleMath(const SmNode
* pNode
, int nLevel
)
159 SAL_INFO("starmath.wordbase", "Math: " << int(pNode
->GetToken().eType
));
160 switch (pNode
->GetToken().eType
)
165 SAL_FALLTHROUGH
; // the above are handled elsewhere, e.g. when handling BINHOR
167 HandleText(pNode
, nLevel
);
172 void SmWordExportBase::HandleSubSupScript(const SmSubSupNode
* pNode
, int nLevel
)
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
);
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */