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 "mathmlattr.hxx"
16 sal_Int32
lcl_GetPowerOf10(sal_Int32 nPower
)
19 sal_Int32 nResult
= 1;
27 sal_Int32
ParseMathMLUnsignedNumber(const OUString
&rStr
, Fraction
*pUN
)
30 auto nLen
= rStr
.getLength();
31 sal_Int32 nDecimalPoint
= -1;
33 for (nIdx
= 0; nIdx
< nLen
; nIdx
++)
36 if (cD
== sal_Unicode('.'))
38 if (nDecimalPoint
>= 0)
43 if (cD
< sal_Unicode('0') || sal_Unicode('9') < cD
)
46 if (nIdx
== 0 || (nIdx
== 1 && nDecimalPoint
== 0))
48 if (nDecimalPoint
== -1)
51 *pUN
= Fraction(rStr
.copy(0, nIdx
).toInt32(), 1);
54 if (nDecimalPoint
== 0)
57 *pUN
= Fraction(rStr
.copy(1, nIdx
-1).toInt32(), lcl_GetPowerOf10(nIdx
-1));
60 assert(0 < nDecimalPoint
);
61 assert(nDecimalPoint
< nIdx
);
62 *pUN
= Fraction(rStr
.copy(0, nDecimalPoint
).toInt32(), 1);
63 if (++nDecimalPoint
< nIdx
)
64 *pUN
+= Fraction(rStr
.copy(nDecimalPoint
, nIdx
-nDecimalPoint
).toInt32(),
65 lcl_GetPowerOf10(nIdx
-nDecimalPoint
));
69 sal_Int32
ParseMathMLNumber(const OUString
&rStr
, Fraction
*pN
)
74 bool bNegative
= (rStr
[0] == sal_Unicode('-'));
75 sal_Int32 nOffset
= bNegative
? 1 : 0;
77 auto nIdx
= ParseMathMLUnsignedNumber(rStr
.copy(nOffset
), &aF
);
81 *pN
= Fraction(aF
.GetNumerator(), aF
.GetDenominator());
84 return nOffset
+ nIdx
;
87 sal_Int32
ParseMathMLAttributeLengthValue(const OUString
&rStr
, MathMLAttributeLengthValue
*pV
)
90 auto nIdx
= ParseMathMLNumber(rStr
, &pV
->aNumber
);
93 OUString sRest
= rStr
.copy(nIdx
);
96 pV
->eUnit
= MathMLLengthUnit::None
;
99 if (sRest
.startsWith("em"))
101 pV
->eUnit
= MathMLLengthUnit::Em
;
104 if (sRest
.startsWith("ex"))
106 pV
->eUnit
= MathMLLengthUnit::Ex
;
109 if (sRest
.startsWith("px"))
111 pV
->eUnit
= MathMLLengthUnit::Px
;
114 if (sRest
.startsWith("in"))
116 pV
->eUnit
= MathMLLengthUnit::In
;
119 if (sRest
.startsWith("cm"))
121 pV
->eUnit
= MathMLLengthUnit::Cm
;
124 if (sRest
.startsWith("mm"))
126 pV
->eUnit
= MathMLLengthUnit::Mm
;
129 if (sRest
.startsWith("pt"))
131 pV
->eUnit
= MathMLLengthUnit::Pt
;
134 if (sRest
.startsWith("pc"))
136 pV
->eUnit
= MathMLLengthUnit::Pc
;
139 if (sRest
[0] == sal_Unicode('%'))
141 pV
->eUnit
= MathMLLengthUnit::Percent
;
147 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */