1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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 <mathml/element.hxx>
14 constexpr SmMlAttributePos MlAttributeListMi
[] = {
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 }
26 constexpr SmMlAttributePos MlAttributeListMerror
[] = {
28 { SmMlAttributeValueType::MlHref
, 0 },
29 { SmMlAttributeValueType::MlMathbackground
, 1 },
30 { SmMlAttributeValueType::MlMathcolor
, 2 },
31 { SmMlAttributeValueType::MlDisplaystyle
, 3 }
35 constexpr SmMlAttributePos MlAttributeListMn
[] = {
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 }
47 constexpr SmMlAttributePos MlAttributeListMo
[] = {
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 }
70 constexpr SmMlAttributePos MlAttributeListMrow
[] = {
72 { SmMlAttributeValueType::MlHref
, 0 },
73 { SmMlAttributeValueType::MlDir
, 1 },
74 { SmMlAttributeValueType::MlMathbackground
, 2 },
75 { SmMlAttributeValueType::MlMathcolor
, 3 }
79 constexpr SmMlAttributePos MlAttributeListMtext
[] = {
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 }
91 constexpr SmMlAttributePos MlAttributeListMstyle
[] = {
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 }
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);
135 case SmMlElementType::NMlStructural
:
136 m_aAttributePosList
= std::vector
<SmMlAttributePos
>(0);
138 case SmMlElementType::NMlSmNode
:
139 m_aAttributePosList
= std::vector
<SmMlAttributePos
>(0);
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));
145 case SmMlElementType::MlMi
:
146 m_aAttributePosList
= std::vector
<SmMlAttributePos
>(std::begin(MlAttributeListMi
),
147 std::end(MlAttributeListMi
));
149 case SmMlElementType::MlMerror
:
150 m_aAttributePosList
= std::vector
<SmMlAttributePos
>(std::begin(MlAttributeListMerror
),
151 std::end(MlAttributeListMerror
));
153 case SmMlElementType::MlMn
:
154 m_aAttributePosList
= std::vector
<SmMlAttributePos
>(std::begin(MlAttributeListMn
),
155 std::end(MlAttributeListMn
));
157 case SmMlElementType::MlMo
:
158 m_aAttributePosList
= std::vector
<SmMlAttributePos
>(std::begin(MlAttributeListMo
),
159 std::end(MlAttributeListMo
));
161 case SmMlElementType::MlMrow
:
162 m_aAttributePosList
= std::vector
<SmMlAttributePos
>(std::begin(MlAttributeListMrow
),
163 std::end(MlAttributeListMrow
));
165 case SmMlElementType::MlMtext
:
166 m_aAttributePosList
= std::vector
<SmMlAttributePos
>(std::begin(MlAttributeListMtext
),
167 std::end(MlAttributeListMtext
));
169 case SmMlElementType::MlMstyle
:
170 m_aAttributePosList
= std::vector
<SmMlAttributePos
>(std::begin(MlAttributeListMstyle
),
171 std::end(MlAttributeListMstyle
));
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();
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
);
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;
231 m_aSubElements
[nPos
] = aElement
;
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */