Add a comment to clarify what kind of inputs the class handles
[LibreOffice.git] / starmath / source / mathml / element.cxx
blob69b993beece559ec0ec6e009205677598db03619
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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 <mathml/element.hxx>
12 namespace
14 constexpr SmMlAttributePos MlAttributeListMi[] = {
15 // clang-format off
16 { SmMlAttributeValueType::MlHref, 0 },
17 { SmMlAttributeValueType::MlDir, 1 },
18 { SmMlAttributeValueType::MlMathbackground, 2 },
19 { SmMlAttributeValueType::MlMathcolor, 3 },
20 { SmMlAttributeValueType::MlDisplaystyle, 4 },
21 { SmMlAttributeValueType::MlMathsize, 5 },
22 { SmMlAttributeValueType::MlMathvariant, 6 }
23 // clang-format on
26 constexpr SmMlAttributePos MlAttributeListMerror[] = {
27 // clang-format off
28 { SmMlAttributeValueType::MlHref, 0 },
29 { SmMlAttributeValueType::MlMathbackground, 1 },
30 { SmMlAttributeValueType::MlMathcolor, 2 },
31 { SmMlAttributeValueType::MlDisplaystyle, 3 }
32 // clang-format on
35 constexpr SmMlAttributePos MlAttributeListMn[] = {
36 // clang-format off
37 { SmMlAttributeValueType::MlHref, 0 },
38 { SmMlAttributeValueType::MlDir, 1 },
39 { SmMlAttributeValueType::MlMathbackground, 2 },
40 { SmMlAttributeValueType::MlMathcolor, 3 },
41 { SmMlAttributeValueType::MlDisplaystyle, 4 },
42 { SmMlAttributeValueType::MlMathsize, 5 },
43 { SmMlAttributeValueType::MlMathvariant, 6 }
44 // clang-format on
47 constexpr SmMlAttributePos MlAttributeListMo[] = {
48 // clang-format off
49 { SmMlAttributeValueType::MlHref, 0 },
50 { SmMlAttributeValueType::MlDir, 1 },
51 { SmMlAttributeValueType::MlMathbackground, 2 },
52 { SmMlAttributeValueType::MlMathcolor, 3 },
53 { SmMlAttributeValueType::MlDisplaystyle, 4 },
54 { SmMlAttributeValueType::MlMathsize, 5 },
55 { SmMlAttributeValueType::MlMathvariant, 6 },
56 { SmMlAttributeValueType::MlFence, 7 },
57 { SmMlAttributeValueType::MlForm, 8 },
58 { SmMlAttributeValueType::MlMaxsize, 9 },
59 { SmMlAttributeValueType::MlMinsize, 10 },
60 { SmMlAttributeValueType::MlMovablelimits, 11 },
61 { SmMlAttributeValueType::MlLspace, 12 },
62 { SmMlAttributeValueType::MlRspace, 13 },
63 { SmMlAttributeValueType::MlAccent, 14 },
64 { SmMlAttributeValueType::MlStretchy, 15 },
65 { SmMlAttributeValueType::MlSeparator, 16 },
66 { SmMlAttributeValueType::MlSymmetric, 17 }
67 // clang-format on
70 constexpr SmMlAttributePos MlAttributeListMrow[] = {
71 // clang-format off
72 { SmMlAttributeValueType::MlHref, 0 },
73 { SmMlAttributeValueType::MlDir, 1 },
74 { SmMlAttributeValueType::MlMathbackground, 2 },
75 { SmMlAttributeValueType::MlMathcolor, 3 }
76 // clang-format on
79 constexpr SmMlAttributePos MlAttributeListMtext[] = {
80 // clang-format off
81 { SmMlAttributeValueType::MlHref, 0 },
82 { SmMlAttributeValueType::MlDir, 1 },
83 { SmMlAttributeValueType::MlMathbackground, 2 },
84 { SmMlAttributeValueType::MlMathcolor, 3 },
85 { SmMlAttributeValueType::MlDisplaystyle, 4 },
86 { SmMlAttributeValueType::MlMathsize, 5 },
87 { SmMlAttributeValueType::MlMathvariant, 6 }
88 // clang-format on
91 constexpr SmMlAttributePos MlAttributeListMstyle[] = {
92 // clang-format off
93 { SmMlAttributeValueType::MlHref, 0 },
94 { SmMlAttributeValueType::MlDir, 1 },
95 { SmMlAttributeValueType::MlMathbackground, 2 },
96 { SmMlAttributeValueType::MlMathcolor, 3 },
97 { SmMlAttributeValueType::MlDisplaystyle, 4 },
98 { SmMlAttributeValueType::MlMathsize, 5 },
99 { SmMlAttributeValueType::MlMathvariant, 6 },
100 { SmMlAttributeValueType::MlFence, 7 },
101 { SmMlAttributeValueType::MlForm, 8 },
102 { SmMlAttributeValueType::MlMaxsize, 9 },
103 { SmMlAttributeValueType::MlMinsize, 10 },
104 { SmMlAttributeValueType::MlMovablelimits, 11 },
105 { SmMlAttributeValueType::MlLspace, 12 },
106 { SmMlAttributeValueType::MlRspace, 13 },
107 { SmMlAttributeValueType::MlAccent, 14 },
108 { SmMlAttributeValueType::MlStretchy, 15 },
109 { SmMlAttributeValueType::MlSeparator, 16 },
110 { SmMlAttributeValueType::MlSymmetric, 17 }
111 // clang-format on
114 /// Generates an attribute vector of default values from an attribute position list.
115 /// @param aAttributePosList
116 /// @return attribute vector
117 std::vector<SmMlAttribute> makeMlAttributeList(std::span<SmMlAttributePos> aAttributePosList)
119 std::vector<SmMlAttribute> aAttributeList(aAttributePosList.size());
120 for (size_t i = 0; i < aAttributePosList.size(); ++i)
122 aAttributeList[i].setMlAttributeValueType(aAttributePosList[i].m_aAttributeValueType);
124 return aAttributeList;
128 void SmMlElement::SmImplAttributeType()
130 switch (m_aElementType)
132 case SmMlElementType::NMlEmpty:
133 m_aAttributePosList = std::vector<SmMlAttributePos>(0);
134 break;
135 case SmMlElementType::NMlStructural:
136 m_aAttributePosList = std::vector<SmMlAttributePos>(0);
137 break;
138 case SmMlElementType::NMlSmNode:
139 m_aAttributePosList = std::vector<SmMlAttributePos>(0);
140 break;
141 case SmMlElementType::MlMath:
142 m_aAttributePosList = std::vector<SmMlAttributePos>(0);
143 //m_aAttributePosList = std::vector<SmMlAttributePos>(std::begin(starmathdatabase::MlAttributeListMath), std::end(starmathdatabase::MlAttributeListMath));
144 break;
145 case SmMlElementType::MlMi:
146 m_aAttributePosList = std::vector<SmMlAttributePos>(std::begin(MlAttributeListMi),
147 std::end(MlAttributeListMi));
148 break;
149 case SmMlElementType::MlMerror:
150 m_aAttributePosList = std::vector<SmMlAttributePos>(std::begin(MlAttributeListMerror),
151 std::end(MlAttributeListMerror));
152 break;
153 case SmMlElementType::MlMn:
154 m_aAttributePosList = std::vector<SmMlAttributePos>(std::begin(MlAttributeListMn),
155 std::end(MlAttributeListMn));
156 break;
157 case SmMlElementType::MlMo:
158 m_aAttributePosList = std::vector<SmMlAttributePos>(std::begin(MlAttributeListMo),
159 std::end(MlAttributeListMo));
160 break;
161 case SmMlElementType::MlMrow:
162 m_aAttributePosList = std::vector<SmMlAttributePos>(std::begin(MlAttributeListMrow),
163 std::end(MlAttributeListMrow));
164 break;
165 case SmMlElementType::MlMtext:
166 m_aAttributePosList = std::vector<SmMlAttributePos>(std::begin(MlAttributeListMtext),
167 std::end(MlAttributeListMtext));
168 break;
169 case SmMlElementType::MlMstyle:
170 m_aAttributePosList = std::vector<SmMlAttributePos>(std::begin(MlAttributeListMstyle),
171 std::end(MlAttributeListMstyle));
172 break;
173 default:
174 break;
176 // Create attribute vector with given pattern
177 m_aAttributeList = makeMlAttributeList(m_aAttributePosList);
180 SmMlAttribute SmMlElement::getAttribute(SmMlAttributeValueType aAttributeType) const
182 // Look for the attribute position and return if exists
183 for (size_t i = 0; i < m_aAttributePosList.size(); ++i)
185 if (m_aAttributePosList[i].m_aAttributeValueType == aAttributeType)
186 return m_aAttributeList[m_aAttributePosList[i].m_nPos];
188 return SmMlAttribute();
191 bool SmMlElement::isAttributeSet(SmMlAttributeValueType aAttributeType) const
193 // Look for the attribute position and return if exists
194 for (size_t i = 0; i < m_aAttributePosList.size(); ++i)
196 if (m_aAttributePosList[i].m_aAttributeValueType == aAttributeType)
197 return m_aAttributeList[m_aAttributePosList[i].m_nPos].isSet();
199 return false;
202 void SmMlElement::setAttribute(const SmMlAttribute* aAttribute)
204 // Look for the attribute position and assign if exists
205 for (size_t i = 0; i < m_aAttributePosList.size(); ++i)
207 if (m_aAttributePosList[i].m_aAttributeValueType == aAttribute->getMlAttributeValueType())
209 m_aAttributeList[m_aAttributePosList[i].m_nPos].setMlAttributeValue(aAttribute);
210 break;
215 void SmMlElement::setSubElement(size_t nPos, SmMlElement* aElement)
217 // This is the new parent element
218 aElement->setParentElement(this);
219 aElement->setSubElementId(nPos);
220 // Check if the vector is long enough
221 // Careful nOldSize can be 0 and -1 will underflow
222 // We must put something on the empty locations
223 size_t nOldSize = m_aSubElements.size();
224 if (nPos + 1 > nOldSize)
226 m_aSubElements.resize(nPos + 1);
227 for (; nOldSize < nPos; ++nOldSize)
228 m_aSubElements[nOldSize] = nullptr;
230 // Assign value
231 m_aSubElements[nPos] = aElement;
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */