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"
12 #include <sal/log.hxx>
13 #include <osl/diagnose.h>
15 SmWordExportBase::SmWordExportBase(const SmNode
* pIn
)
20 SmWordExportBase::~SmWordExportBase() = default;
22 void SmWordExportBase::HandleNode(const SmNode
* pNode
, int nLevel
)
24 SAL_INFO("starmath.wordbase", "Node: " << nLevel
<< " " << int(pNode
->GetType()) << " " << pNode
->GetNumSubNodes());
25 switch (pNode
->GetType())
27 case SmNodeType::Attribut
:
28 HandleAttribute(static_cast< const SmAttributNode
* >(pNode
), nLevel
);
30 case SmNodeType::Text
:
31 HandleText(pNode
,nLevel
);
33 case SmNodeType::VerticalBrace
:
34 HandleVerticalBrace(static_cast< const SmVerticalBraceNode
* >(pNode
), nLevel
);
36 case SmNodeType::Brace
:
37 HandleBrace(static_cast< const SmBraceNode
* >(pNode
), nLevel
);
39 case SmNodeType::Oper
:
40 HandleOperator(static_cast< const SmOperNode
* >(pNode
), nLevel
);
42 case SmNodeType::UnHor
:
43 HandleUnaryOperation(static_cast< const SmUnHorNode
* >(pNode
), nLevel
);
45 case SmNodeType::BinHor
:
46 HandleBinaryOperation(static_cast< const SmBinHorNode
* >(pNode
), nLevel
);
48 case SmNodeType::BinVer
:
49 HandleFractions(pNode
,nLevel
,nullptr);
51 case SmNodeType::Root
:
52 HandleRoot(static_cast< const SmRootNode
* >(pNode
), nLevel
);
54 case SmNodeType::Special
:
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
);
62 HandleMath(pText
,nLevel
);
65 case SmNodeType::Math
:
66 case SmNodeType::MathIdent
:
67 HandleMath(pNode
,nLevel
);
69 case SmNodeType::SubSup
:
70 HandleSubSupScript(static_cast< const SmSubSupNode
* >(pNode
), nLevel
);
72 case SmNodeType::Expression
:
73 HandleAllSubNodes(pNode
, nLevel
);
75 case SmNodeType::Table
:
76 //Root Node, PILE equivalent, i.e. vertical stack
77 HandleTable(pNode
,nLevel
);
79 case SmNodeType::Matrix
:
80 HandleMatrix(static_cast< const SmMatrixNode
* >(pNode
), nLevel
);
82 case SmNodeType::Line
:
85 HandleAllSubNodes(pNode
, nLevel
);
89 case SmNodeType::Align
:
90 HandleMAlign(pNode
,nLevel
);
93 case SmNodeType::Place
:
94 // explicitly do nothing, MSOffice treats that as a placeholder if item is missing
96 case SmNodeType::Blank
:
100 HandleAllSubNodes(pNode
, nLevel
);
105 //Root Node, PILE equivalent, i.e. vertical stack
106 void SmWordExportBase::HandleTable(const SmNode
* pNode
, int nLevel
)
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
);
117 HandleAllSubNodes(pNode
, nLevel
);
120 void SmWordExportBase::HandleAllSubNodes(const SmNode
* pNode
, int nLevel
)
122 int size
= pNode
->GetNumSubNodes();
127 // TODO remove when all types of nodes are handled properly
128 if (pNode
->GetSubNode(i
) == nullptr)
130 SAL_WARN("starmath.wordbase", "Subnode is NULL, parent node not handled properly");
133 HandleNode(pNode
->GetSubNode(i
), nLevel
+ 1);
137 void SmWordExportBase::HandleUnaryOperation(const SmUnHorNode
* pNode
, int nLevel
)
139 // update HandleMath() when adding new items
140 SAL_INFO("starmath.wordbase", "Unary: " << int(pNode
->GetToken().eType
));
142 HandleAllSubNodes(pNode
, nLevel
);
145 void SmWordExportBase::HandleBinaryOperation(const SmBinHorNode
* pNode
, int nLevel
)
147 SAL_INFO("starmath.wordbase", "Binary: " << int(pNode
->Symbol()->GetToken().eType
));
148 // update HandleMath() when adding new items
149 switch (pNode
->Symbol()->GetToken().eType
)
152 return HandleFractions(pNode
, nLevel
, "lin");
154 HandleAllSubNodes(pNode
, nLevel
);
159 void SmWordExportBase::HandleMath(const SmNode
* pNode
, int nLevel
)
161 SAL_INFO("starmath.wordbase", "Math: " << int(pNode
->GetToken().eType
));
162 switch (pNode
->GetToken().eType
)
167 [[fallthrough
]]; // the above are handled elsewhere, e.g. when handling BINHOR
169 HandleText(pNode
, nLevel
);
174 void SmWordExportBase::HandleSubSupScript(const SmSubSupNode
* pNode
, int nLevel
)
176 // set flags to a bitfield of which sub/sup items exists
177 int flags
= (pNode
->GetSubSup(CSUB
) != nullptr ? (1 << CSUB
) : 0)
178 | (pNode
->GetSubSup(CSUP
) != nullptr ? (1 << CSUP
) : 0)
179 | (pNode
->GetSubSup(RSUB
) != nullptr ? (1 << RSUB
) : 0)
180 | (pNode
->GetSubSup(RSUP
) != nullptr ? (1 << RSUP
) : 0)
181 | (pNode
->GetSubSup(LSUB
) != nullptr ? (1 << LSUB
) : 0)
182 | (pNode
->GetSubSup(LSUP
) != nullptr ? (1 << LSUP
) : 0);
183 HandleSubSupScriptInternal(pNode
, nLevel
, flags
);
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */