- Revision
- 203106
- Author
- [email protected]
- Date
- 2016-07-11 21:47:02 -0700 (Mon, 11 Jul 2016)
Log Message
Create a MathMLLength struct to handle the parsing of MathML length.
https://bugs.webkit.org/show_bug.cgi?id=156792
Patch by Frederic Wang <[email protected]> on 2016-07-11
Reviewed by Brent Fulgham.
We introduce a structure for MathML lengths that will be used in the future to store the
parsed values in the MathElement class. We also rewrite the parsing function for MathML
lengths in order to improve efficiency and code reuse. This function is moved into the
MathElement class and only the conversion to LayoutUnit remains in the renderer classes.
No new tests, already covered by existing tests.
* mathml/MathMLElement.cpp:
(WebCore::parseNamedSpace): Helper function to parse a named space.
(WebCore::MathMLElement::parseMathMLLength): Parsing function for MathML lengths.
* mathml/MathMLElement.h: Declare new function and structure to handle MathML lengths.
* rendering/mathml/RenderMathMLBlock.cpp:
(WebCore::toUserUnits): Helper function to resolve a MathML length.
(WebCore::parseMathMLLength): Remove the old parsing code and just use MathMLElement::parseMathMLLength and toUserUnits instead.
(WebCore::parseMathMLNamedSpace): Deleted.
* rendering/mathml/RenderMathMLBlock.h: Remove unused function.
Modified Paths
Diff
Modified: trunk/Source/WebCore/ChangeLog (203105 => 203106)
--- trunk/Source/WebCore/ChangeLog 2016-07-12 04:34:25 UTC (rev 203105)
+++ trunk/Source/WebCore/ChangeLog 2016-07-12 04:47:02 UTC (rev 203106)
@@ -1,3 +1,27 @@
+2016-07-11 Frederic Wang <[email protected]>
+
+ Create a MathMLLength struct to handle the parsing of MathML length.
+ https://bugs.webkit.org/show_bug.cgi?id=156792
+
+ Reviewed by Brent Fulgham.
+
+ We introduce a structure for MathML lengths that will be used in the future to store the
+ parsed values in the MathElement class. We also rewrite the parsing function for MathML
+ lengths in order to improve efficiency and code reuse. This function is moved into the
+ MathElement class and only the conversion to LayoutUnit remains in the renderer classes.
+
+ No new tests, already covered by existing tests.
+
+ * mathml/MathMLElement.cpp:
+ (WebCore::parseNamedSpace): Helper function to parse a named space.
+ (WebCore::MathMLElement::parseMathMLLength): Parsing function for MathML lengths.
+ * mathml/MathMLElement.h: Declare new function and structure to handle MathML lengths.
+ * rendering/mathml/RenderMathMLBlock.cpp:
+ (WebCore::toUserUnits): Helper function to resolve a MathML length.
+ (WebCore::parseMathMLLength): Remove the old parsing code and just use MathMLElement::parseMathMLLength and toUserUnits instead.
+ (WebCore::parseMathMLNamedSpace): Deleted.
+ * rendering/mathml/RenderMathMLBlock.h: Remove unused function.
+
2016-07-11 Frederic Wang <[email protected]>
Add support for @href attribute in MathML
Modified: trunk/Source/WebCore/mathml/MathMLElement.cpp (203105 => 203106)
--- trunk/Source/WebCore/mathml/MathMLElement.cpp 2016-07-12 04:34:25 UTC (rev 203105)
+++ trunk/Source/WebCore/mathml/MathMLElement.cpp 2016-07-12 04:47:02 UTC (rev 203106)
@@ -397,6 +397,125 @@
return Element::tabIndex();
}
+static inline StringView skipLeadingAndTrailingWhitespace(const String& string)
+{
+ unsigned start = 0, stringLength = string.length();
+ while (stringLength > 0 && isHTMLSpace(string[start])) {
+ start++;
+ stringLength--;
+ }
+ while (stringLength > 0 && isHTMLSpace(string[start + stringLength - 1]))
+ stringLength--;
+ return string.substring(start, stringLength);
}
+MathMLElement::Length MathMLElement::parseNumberAndUnit(const StringView& string)
+{
+ LengthType lengthType = LengthType::UnitLess;
+ unsigned stringLength = string.length();
+ UChar lastChar = string[stringLength - 1];
+ if (lastChar == '%') {
+ lengthType = LengthType::Percentage;
+ stringLength--;
+ } else if (stringLength >= 2) {
+ UChar penultimateChar = string[stringLength - 2];
+ if (penultimateChar == 'c' && lastChar == 'm')
+ lengthType = LengthType::Cm;
+ if (penultimateChar == 'e' && lastChar == 'm')
+ lengthType = LengthType::Em;
+ else if (penultimateChar == 'e' && lastChar == 'x')
+ lengthType = LengthType::Ex;
+ else if (penultimateChar == 'i' && lastChar == 'n')
+ lengthType = LengthType::In;
+ else if (penultimateChar == 'm' && lastChar == 'm')
+ lengthType = LengthType::Mm;
+ else if (penultimateChar == 'p' && lastChar == 'c')
+ lengthType = LengthType::Pc;
+ else if (penultimateChar == 'p' && lastChar == 't')
+ lengthType = LengthType::Pt;
+ else if (penultimateChar == 'p' && lastChar == 'x')
+ lengthType = LengthType::Px;
+
+ if (lengthType != LengthType::UnitLess)
+ stringLength -= 2;
+ }
+
+ bool ok;
+ float lengthValue = string.substring(0, stringLength).toFloat(ok);
+ if (!ok)
+ return Length();
+
+ Length length;
+ length.type = lengthType;
+ length.value = lengthValue;
+ return length;
+}
+
+MathMLElement::Length MathMLElement::parseNamedSpace(const StringView& string)
+{
+ // Named space values are case-sensitive.
+ int namedSpaceValue;
+ if (string == "veryverythinmathspace")
+ namedSpaceValue = 1;
+ else if (string == "verythinmathspace")
+ namedSpaceValue = 2;
+ else if (string == "thinmathspace")
+ namedSpaceValue = 3;
+ else if (string == "mediummathspace")
+ namedSpaceValue = 4;
+ else if (string == "thickmathspace")
+ namedSpaceValue = 5;
+ else if (string == "verythickmathspace")
+ namedSpaceValue = 6;
+ else if (string == "veryverythickmathspace")
+ namedSpaceValue = 7;
+ else if (string == "negativeveryverythinmathspace")
+ namedSpaceValue = -1;
+ else if (string == "negativeverythinmathspace")
+ namedSpaceValue = -2;
+ else if (string == "negativethinmathspace")
+ namedSpaceValue = -3;
+ else if (string == "negativemediummathspace")
+ namedSpaceValue = -4;
+ else if (string == "negativethickmathspace")
+ namedSpaceValue = -5;
+ else if (string == "negativeverythickmathspace")
+ namedSpaceValue = -6;
+ else if (string == "negativeveryverythickmathspace")
+ namedSpaceValue = -7;
+ else
+ return Length();
+
+ Length length;
+ length.type = LengthType::MathUnit;
+ length.value = namedSpaceValue;
+ return length;
+}
+
+MathMLElement::Length MathMLElement::parseMathMLLength(const String& string)
+{
+ // The regular _expression_ from the MathML Relax NG schema is as follows:
+ //
+ // pattern = '\s*((-?[0-9]*([0-9]\.?|\.[0-9])[0-9]*(e[mx]|in|cm|mm|p[xtc]|%)?)|(negative)?((very){0,2}thi(n|ck)|medium)mathspace)\s*'
+ //
+ // We do not perform a strict verification of the syntax of whitespaces and number.
+ // Instead, we just use isHTMLSpace and toFloat to parse these parts.
+
+ // We first skip whitespace from both ends of the string.
+ StringView stringView = skipLeadingAndTrailingWhitespace(string);
+
+ if (stringView.isEmpty())
+ return Length();
+
+ // We consider the most typical case: a number followed by an optional unit.
+ UChar firstChar = stringView[0];
+ if (isASCIIDigit(firstChar) || firstChar == '-' || firstChar == '.')
+ return parseNumberAndUnit(stringView);
+
+ // Otherwise, we try and parse a named space.
+ return parseNamedSpace(stringView);
+}
+
+}
+
#endif // ENABLE(MATHML)
Modified: trunk/Source/WebCore/mathml/MathMLElement.h (203105 => 203106)
--- trunk/Source/WebCore/mathml/MathMLElement.h 2016-07-12 04:34:25 UTC (rev 203105)
+++ trunk/Source/WebCore/mathml/MathMLElement.h 2016-07-12 04:47:02 UTC (rev 203106)
@@ -56,6 +56,16 @@
bool hasTagName(const MathMLQualifiedName& name) const { return hasLocalName(name.localName()); }
+ // MathML lengths (https://www.w3.org/TR/MathML3/chapter2.html#fund.units)
+ // TeX's Math Unit is used internally for named spaces (1 mu = 1/18 em).
+ // Unitless values are interpreted as a multiple of a reference value.
+ enum class LengthType { Cm, Em, Ex, In, MathUnit, Mm, ParsingFailed, Pc, Percentage, Pt, Px, UnitLess };
+ struct Length {
+ LengthType type { LengthType::ParsingFailed };
+ float value { 0 };
+ };
+ static Length parseMathMLLength(const String&);
+
protected:
MathMLElement(const QualifiedName& tagName, Document&);
@@ -74,6 +84,8 @@
private:
virtual void updateSelectedChild() { }
+ static Length parseNumberAndUnit(const StringView&);
+ static Length parseNamedSpace(const StringView&);
bool canStartSelection() const final;
bool isFocusable() const final;
Modified: trunk/Source/WebCore/rendering/mathml/RenderMathMLBlock.cpp (203105 => 203106)
--- trunk/Source/WebCore/rendering/mathml/RenderMathMLBlock.cpp 2016-07-12 04:34:25 UTC (rev 203105)
+++ trunk/Source/WebCore/rendering/mathml/RenderMathMLBlock.cpp 2016-07-12 04:47:02 UTC (rev 203106)
@@ -30,11 +30,12 @@
#include "RenderMathMLBlock.h"
+#include "CSSHelper.h"
#include "GraphicsContext.h"
#include "LayoutRepainter.h"
+#include "MathMLElement.h"
#include "MathMLNames.h"
#include "RenderView.h"
-#include <wtf/text/StringBuilder.h>
#if ENABLE(DEBUG_MATH_LAYOUT)
#include "PaintInfo.h"
@@ -137,179 +138,54 @@
}
#endif // ENABLE(DEBUG_MATH_LAYOUT)
-//
-// The MathML specification says:
-// (http://www.w3.org/TR/MathML/chapter2.html#fund.units)
-//
-// "Most presentation elements have attributes that accept values representing
-// lengths to be used for size, spacing or similar properties. The syntax of a
-// length is specified as
-//
-// number | number unit | namedspace
-//
-// There should be no space between the number and the unit of a length."
-//
-// "A trailing '%' represents a percent of the default value. The default
-// value, or how it is obtained, is listed in the table of attributes for each
-// element. [...] A number without a unit is intepreted as a multiple of the
-// default value."
-//
-// "The possible units in MathML are:
-//
-// Unit Description
-// em an em (font-relative unit traditionally used for horizontal lengths)
-// ex an ex (font-relative unit traditionally used for vertical lengths)
-// px pixels, or size of a pixel in the current display
-// in inches (1 inch = 2.54 centimeters)
-// cm centimeters
-// mm millimeters
-// pt points (1 point = 1/72 inch)
-// pc picas (1 pica = 12 points)
-// % percentage of default value"
-//
-// The numbers are defined that way:
-// - unsigned-number: "a string of decimal digits with up to one decimal point
-// (U+002E), representing a non-negative terminating decimal number (a type of
-// rational number)"
-// - number: "an optional prefix of '-' (U+002D), followed by an unsigned
-// number, representing a terminating decimal number (a type of rational
-// number)"
-//
+LayoutUnit toUserUnits(const MathMLElement::Length& length, const RenderStyle& style, const LayoutUnit& referenceValue)
+{
+ switch (length.type) {
+ case MathMLElement::LengthType::Cm:
+ return length.value * cssPixelsPerInch / 2.54f;
+ case MathMLElement::LengthType::Em:
+ return length.value * style.fontCascade().size();
+ case MathMLElement::LengthType::Ex:
+ return length.value * style.fontMetrics().xHeight();
+ case MathMLElement::LengthType::In:
+ return length.value * cssPixelsPerInch;
+ case MathMLElement::LengthType::MathUnit:
+ return length.value * style.fontCascade().size() / 18;
+ case MathMLElement::LengthType::Mm:
+ return length.value * cssPixelsPerInch / 25.4f;
+ case MathMLElement::LengthType::Pc:
+ return length.value * cssPixelsPerInch / 6;
+ case MathMLElement::LengthType::Percentage:
+ return referenceValue * length.value / 100;
+ case MathMLElement::LengthType::Pt:
+ return length.value * cssPixelsPerInch / 72;
+ case MathMLElement::LengthType::Px:
+ return length.value;
+ case MathMLElement::LengthType::UnitLess:
+ return referenceValue * length.value;
+ case MathMLElement::LengthType::ParsingFailed:
+ return referenceValue;
+ default:
+ ASSERT_NOT_REACHED();
+ return referenceValue;
+ }
+}
+
bool parseMathMLLength(const String& string, LayoutUnit& lengthValue, const RenderStyle* style, bool allowNegative)
{
- String s = string.simplifyWhiteSpace();
-
- int stringLength = s.length();
- if (!stringLength)
+ MathMLElement::Length length = MathMLElement::parseMathMLLength(string);
+ if (length.type == MathMLElement::LengthType::ParsingFailed)
return false;
- if (parseMathMLNamedSpace(s, lengthValue, style, allowNegative))
- return true;
+ LayoutUnit value = toUserUnits(length, *style, lengthValue);
- StringBuilder number;
- String unit;
-
- // This verifies whether the negative sign is there.
- int i = 0;
- UChar c = s[0];
- if (c == '-') {
- number.append(c);
- i++;
- }
-
- // This gathers up characters that make up the number.
- bool gotDot = false;
- for ( ; i < stringLength; i++) {
- c = s[i];
- // The string is invalid if it contains two dots.
- if (gotDot && c == '.')
- return false;
- if (c == '.')
- gotDot = true;
- else if (!isASCIIDigit(c)) {
- unit = s.substring(i, stringLength - i);
- // Some authors leave blanks before the unit, but that shouldn't
- // be allowed, so don't simplifyWhitespace on 'unit'.
- break;
- }
- number.append(c);
- }
-
- // Convert number to floating point
- bool ok;
- float floatValue = number.toString().toFloat(&ok);
- if (!ok)
+ if (!allowNegative && value < 0)
return false;
- if (floatValue < 0 && !allowNegative)
- return false;
- if (unit.isEmpty()) {
- // no explicit unit, this is a number that will act as a multiplier
- lengthValue *= floatValue;
- return true;
- }
- if (unit == "%") {
- lengthValue *= floatValue / 100;
- return true;
- }
- if (unit == "em") {
- lengthValue = floatValue * style->fontCascade().size();
- return true;
- }
- if (unit == "ex") {
- lengthValue = floatValue * style->fontMetrics().xHeight();
- return true;
- }
- if (unit == "px") {
- lengthValue = floatValue;
- return true;
- }
- if (unit == "pt") {
- lengthValue = 4 * (floatValue / 3);
- return true;
- }
- if (unit == "pc") {
- lengthValue = 16 * floatValue;
- return true;
- }
- if (unit == "in") {
- lengthValue = 96 * floatValue;
- return true;
- }
- if (unit == "cm") {
- lengthValue = 96 * (floatValue / 2.54);
- return true;
- }
- if (unit == "mm") {
- lengthValue = 96 * (floatValue / 25.4);
- return true;
- }
-
- // unexpected unit
- return false;
+ lengthValue = value;
+ return true;
}
-bool parseMathMLNamedSpace(const String& string, LayoutUnit& lengthValue, const RenderStyle* style, bool allowNegative)
-{
- float length = 0;
- // See if it is one of the namedspaces (ranging -7/18em, -6/18, ... 7/18em)
- if (string == "veryverythinmathspace")
- length = 1;
- else if (string == "verythinmathspace")
- length = 2;
- else if (string == "thinmathspace")
- length = 3;
- else if (string == "mediummathspace")
- length = 4;
- else if (string == "thickmathspace")
- length = 5;
- else if (string == "verythickmathspace")
- length = 6;
- else if (string == "veryverythickmathspace")
- length = 7;
- else if (allowNegative) {
- if (string == "negativeveryverythinmathspace")
- length = -1;
- else if (string == "negativeverythinmathspace")
- length = -2;
- else if (string == "negativethinmathspace")
- length = -3;
- else if (string == "negativemediummathspace")
- length = -4;
- else if (string == "negativethickmathspace")
- length = -5;
- else if (string == "negativeverythickmathspace")
- length = -6;
- else if (string == "negativeveryverythickmathspace")
- length = -7;
- }
- if (length) {
- lengthValue = length * style->fontCascade().size() / 18;
- return true;
- }
- return false;
-}
-
Optional<int> RenderMathMLTable::firstLineBaseline() const
{
// By default the vertical center of <mtable> is aligned on the math axis.
Modified: trunk/Source/WebCore/rendering/mathml/RenderMathMLBlock.h (203105 => 203106)
--- trunk/Source/WebCore/rendering/mathml/RenderMathMLBlock.h 2016-07-12 04:34:25 UTC (rev 203105)
+++ trunk/Source/WebCore/rendering/mathml/RenderMathMLBlock.h 2016-07-12 04:47:02 UTC (rev 203106)
@@ -29,6 +29,7 @@
#if ENABLE(MATHML)
+#include "MathMLElement.h"
#include "MathMLStyle.h"
#include "RenderBlock.h"
#include "RenderTable.h"
@@ -116,7 +117,7 @@
// Parsing functions for MathML Length values
bool parseMathMLLength(const String&, LayoutUnit&, const RenderStyle*, bool allowNegative = true);
-bool parseMathMLNamedSpace(const String&, LayoutUnit&, const RenderStyle*, bool allowNegative = true);
+LayoutUnit toUserUnits(const MathMLElement::Length&, const RenderStyle&, const LayoutUnit& referenceValue);
} // namespace WebCore