Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / starmath / source / mathmlattr.cxx
blob344c079b35e910904612af93cf0638d221319980
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 "mathmlattr.hxx"
12 #include <cassert>
13 #include <unordered_map>
15 sal_Int32 ParseMathMLUnsignedNumber(const OUString &rStr, Fraction& rUN)
17 auto nLen = rStr.getLength();
18 sal_Int32 nDecimalPoint = -1;
19 sal_Int32 nIdx;
20 for (nIdx = 0; nIdx < nLen; nIdx++)
22 auto cD = rStr[nIdx];
23 if (cD == u'.')
25 if (nDecimalPoint >= 0)
26 return -1;
27 nDecimalPoint = nIdx;
28 continue;
30 if (cD < u'0' || u'9' < cD)
31 break;
33 if (nIdx == 0 || (nIdx == 1 && nDecimalPoint == 0))
34 return -1;
36 rUN = Fraction(rStr.copy(0, nIdx).toDouble());
38 return nIdx;
41 sal_Int32 ParseMathMLNumber(const OUString &rStr, Fraction& rN)
43 if (rStr.isEmpty())
44 return -1;
45 bool bNegative = (rStr[0] == '-');
46 sal_Int32 nOffset = bNegative ? 1 : 0;
47 auto nIdx = ParseMathMLUnsignedNumber(rStr.copy(nOffset), rN);
48 if (nIdx <= 0 || !rN.IsValid())
49 return -1;
50 if (bNegative)
51 rN *= -1;
52 return nOffset + nIdx;
55 sal_Int32 ParseMathMLAttributeLengthValue(const OUString &rStr, MathMLAttributeLengthValue& rV)
57 auto nIdx = ParseMathMLNumber(rStr, rV.aNumber);
58 if (nIdx <= 0)
59 return -1;
60 OUString sRest = rStr.copy(nIdx);
61 if (sRest.isEmpty())
63 rV.eUnit = MathMLLengthUnit::None;
64 return nIdx;
66 if (sRest.startsWith("em"))
68 rV.eUnit = MathMLLengthUnit::Em;
69 return nIdx + 2;
71 if (sRest.startsWith("ex"))
73 rV.eUnit = MathMLLengthUnit::Ex;
74 return nIdx + 2;
76 if (sRest.startsWith("px"))
78 rV.eUnit = MathMLLengthUnit::Px;
79 return nIdx + 2;
81 if (sRest.startsWith("in"))
83 rV.eUnit = MathMLLengthUnit::In;
84 return nIdx + 2;
86 if (sRest.startsWith("cm"))
88 rV.eUnit = MathMLLengthUnit::Cm;
89 return nIdx + 2;
91 if (sRest.startsWith("mm"))
93 rV.eUnit = MathMLLengthUnit::Mm;
94 return nIdx + 2;
96 if (sRest.startsWith("pt"))
98 rV.eUnit = MathMLLengthUnit::Pt;
99 return nIdx + 2;
101 if (sRest.startsWith("pc"))
103 rV.eUnit = MathMLLengthUnit::Pc;
104 return nIdx + 2;
106 if (sRest[0] == u'%')
108 rV.eUnit = MathMLLengthUnit::Percent;
109 return nIdx + 2;
111 return nIdx;
114 bool GetMathMLMathvariantValue(const OUString &rStr, MathMLMathvariantValue& rV)
116 static const std::unordered_map<OUString, MathMLMathvariantValue> aMap{
117 {"normal", MathMLMathvariantValue::Normal},
118 {"bold", MathMLMathvariantValue::Bold},
119 {"italic", MathMLMathvariantValue::Italic},
120 {"bold-italic", MathMLMathvariantValue::BoldItalic},
121 {"double-struck", MathMLMathvariantValue::DoubleStruck},
122 {"bold-fraktur", MathMLMathvariantValue::BoldFraktur},
123 {"script", MathMLMathvariantValue::Script},
124 {"bold-script", MathMLMathvariantValue::BoldScript},
125 {"fraktur", MathMLMathvariantValue::Fraktur},
126 {"sans-serif", MathMLMathvariantValue::SansSerif},
127 {"bold-sans-serif", MathMLMathvariantValue::BoldSansSerif},
128 {"sans-serif-italic", MathMLMathvariantValue::SansSerifItalic},
129 {"sans-serif-bold-italic", MathMLMathvariantValue::SansSerifBoldItalic},
130 {"monospace", MathMLMathvariantValue::Monospace},
131 {"initial", MathMLMathvariantValue::Initial},
132 {"tailed", MathMLMathvariantValue::Tailed},
133 {"looped", MathMLMathvariantValue::Looped},
134 {"stretched", MathMLMathvariantValue::Stretched}
137 auto it = aMap.find(rStr);
138 if (it != aMap.end())
140 rV = it->second;
141 return true;
143 return false;
146 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */