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",
25 "Node: " << nLevel
<< " " << int(pNode
->GetType()) << " " << pNode
->GetNumSubNodes());
26 switch (pNode
->GetType())
28 case SmNodeType::Attribut
:
29 HandleAttribute(static_cast<const SmAttributNode
*>(pNode
), nLevel
);
31 case SmNodeType::Text
:
32 HandleText(pNode
, nLevel
);
34 case SmNodeType::VerticalBrace
:
35 HandleVerticalBrace(static_cast<const SmVerticalBraceNode
*>(pNode
), nLevel
);
37 case SmNodeType::Brace
:
38 HandleBrace(static_cast<const SmBraceNode
*>(pNode
), nLevel
);
40 case SmNodeType::Oper
:
41 HandleOperator(static_cast<const SmOperNode
*>(pNode
), nLevel
);
43 case SmNodeType::UnHor
:
44 HandleUnaryOperation(static_cast<const SmUnHorNode
*>(pNode
), nLevel
);
46 case SmNodeType::BinHor
:
47 HandleBinaryOperation(static_cast<const SmBinHorNode
*>(pNode
), nLevel
);
49 case SmNodeType::BinVer
:
50 HandleFractions(pNode
, nLevel
, nullptr);
52 case SmNodeType::Root
:
53 HandleRoot(static_cast<const SmRootNode
*>(pNode
), nLevel
);
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
);
63 HandleMath(pText
, nLevel
);
66 case SmNodeType::Math
:
67 case SmNodeType::MathIdent
:
68 HandleMath(pNode
, nLevel
);
70 case SmNodeType::SubSup
:
71 HandleSubSupScript(static_cast<const SmSubSupNode
*>(pNode
), nLevel
);
73 case SmNodeType::Expression
:
74 HandleAllSubNodes(pNode
, nLevel
);
76 case SmNodeType::Table
:
77 //Root Node, PILE equivalent, i.e. vertical stack
78 HandleTable(pNode
, nLevel
);
80 case SmNodeType::Matrix
:
81 HandleMatrix(static_cast<const SmMatrixNode
*>(pNode
), nLevel
);
83 case SmNodeType::Line
:
86 HandleAllSubNodes(pNode
, nLevel
);
90 case SmNodeType::Align
:
91 HandleMAlign(pNode
,nLevel
);
94 case SmNodeType::Place
:
95 // explicitly do nothing, MSOffice treats that as a placeholder if item is missing
97 case SmNodeType::Blank
:
101 HandleAllSubNodes(pNode
, nLevel
);
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
);
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");
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
)
151 return HandleFractions(pNode
, nLevel
, "lin");
153 HandleAllSubNodes(pNode
, nLevel
);
158 void SmWordExportBase::HandleMath(const SmNode
* pNode
, int nLevel
)
160 SAL_INFO("starmath.wordbase", "Math: " << int(pNode
->GetToken().eType
));
161 switch (pNode
->GetToken().eType
)
166 [[fallthrough
]]; // the above are handled elsewhere, e.g. when handling BINHOR
168 HandleText(pNode
, nLevel
);
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: */