Bump version to 21.06.18.1
[LibreOffice.git] / starmath / source / mathmlattr.cxx
blobb2a3f2453ec51781f55c756d941ad8bf7242e937
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 <unordered_map>
14 static sal_Int32 ParseMathMLUnsignedNumber(const OUString& rStr, Fraction& rUN)
16 auto nLen = rStr.getLength();
17 sal_Int32 nDecimalPoint = -1;
18 sal_Int32 nIdx;
19 for (nIdx = 0; nIdx < nLen; nIdx++)
21 auto cD = rStr[nIdx];
22 if (cD == u'.')
24 if (nDecimalPoint >= 0)
25 return -1;
26 nDecimalPoint = nIdx;
27 continue;
29 if (cD < u'0' || u'9' < cD)
30 break;
32 if (nIdx == 0 || (nIdx == 1 && nDecimalPoint == 0))
33 return -1;
35 rUN = Fraction(rStr.copy(0, nIdx).toDouble());
37 return nIdx;
40 static sal_Int32 ParseMathMLNumber(const OUString& rStr, Fraction& rN)
42 if (rStr.isEmpty())
43 return -1;
44 bool bNegative = (rStr[0] == '-');
45 sal_Int32 nOffset = bNegative ? 1 : 0;
46 auto nIdx = ParseMathMLUnsignedNumber(rStr.copy(nOffset), rN);
47 if (nIdx <= 0 || !rN.IsValid())
48 return -1;
49 if (bNegative)
50 rN *= -1;
51 return nOffset + nIdx;
54 sal_Int32 ParseMathMLAttributeLengthValue(const OUString& rStr, MathMLAttributeLengthValue& rV)
56 auto nIdx = ParseMathMLNumber(rStr, rV.aNumber);
57 if (nIdx <= 0)
58 return -1;
59 OUString sRest = rStr.copy(nIdx);
60 if (sRest.isEmpty())
62 rV.eUnit = MathMLLengthUnit::None;
63 return nIdx;
65 if (sRest.startsWith("em"))
67 rV.eUnit = MathMLLengthUnit::Em;
68 return nIdx + 2;
70 if (sRest.startsWith("ex"))
72 rV.eUnit = MathMLLengthUnit::Ex;
73 return nIdx + 2;
75 if (sRest.startsWith("px"))
77 rV.eUnit = MathMLLengthUnit::Px;
78 return nIdx + 2;
80 if (sRest.startsWith("in"))
82 rV.eUnit = MathMLLengthUnit::In;
83 return nIdx + 2;
85 if (sRest.startsWith("cm"))
87 rV.eUnit = MathMLLengthUnit::Cm;
88 return nIdx + 2;
90 if (sRest.startsWith("mm"))
92 rV.eUnit = MathMLLengthUnit::Mm;
93 return nIdx + 2;
95 if (sRest.startsWith("pt"))
97 rV.eUnit = MathMLLengthUnit::Pt;
98 return nIdx + 2;
100 if (sRest.startsWith("pc"))
102 rV.eUnit = MathMLLengthUnit::Pc;
103 return nIdx + 2;
105 if (sRest[0] == u'%')
107 rV.eUnit = MathMLLengthUnit::Percent;
108 return nIdx + 2;
110 return nIdx;
113 bool GetMathMLMathvariantValue(const OUString& rStr, MathMLMathvariantValue& rV)
115 static const std::unordered_map<OUString, MathMLMathvariantValue> aMap{
116 { "normal", MathMLMathvariantValue::Normal },
117 { "bold", MathMLMathvariantValue::Bold },
118 { "italic", MathMLMathvariantValue::Italic },
119 { "bold-italic", MathMLMathvariantValue::BoldItalic },
120 { "double-struck", MathMLMathvariantValue::DoubleStruck },
121 { "bold-fraktur", MathMLMathvariantValue::BoldFraktur },
122 { "script", MathMLMathvariantValue::Script },
123 { "bold-script", MathMLMathvariantValue::BoldScript },
124 { "fraktur", MathMLMathvariantValue::Fraktur },
125 { "sans-serif", MathMLMathvariantValue::SansSerif },
126 { "bold-sans-serif", MathMLMathvariantValue::BoldSansSerif },
127 { "sans-serif-italic", MathMLMathvariantValue::SansSerifItalic },
128 { "sans-serif-bold-italic", MathMLMathvariantValue::SansSerifBoldItalic },
129 { "monospace", MathMLMathvariantValue::Monospace },
130 { "initial", MathMLMathvariantValue::Initial },
131 { "tailed", MathMLMathvariantValue::Tailed },
132 { "looped", MathMLMathvariantValue::Looped },
133 { "stretched", MathMLMathvariantValue::Stretched }
136 auto it = aMap.find(rStr);
137 if (it != aMap.end())
139 rV = it->second;
140 return true;
142 return false;
145 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */