Changeset 204830 in webkit


Ignore:
Timestamp:
Aug 23, 2016 6:36:26 AM (8 years ago)
Author:
fred.wang@free.fr
Message:

Share and improve extraction of character for operator and token elements
https://bugs.webkit.org/show_bug.cgi?id=160462

Patch by Frederic Wang <fwang@igalia.com> on 2016-08-03
Reviewed by Darin Adler.

No new tests, already covered by existing tests.

  • mathml/MathMLElement.cpp:

(WebCore::MathMLElement::stripLeadingAndTrailingWhitespace): Make this a protected member of
MathMLElement so that it can be used in MathMLTokenElement.
(WebCore::skipLeadingAndTrailingWhitespace): Deleted.

  • mathml/MathMLElement.h: Declare stripLeadingAndTrailingWhitespace.
  • mathml/MathMLOperatorElement.cpp:

(WebCore::MathMLOperatorElement::parseOperatorChar): Use convertToSingleCodePoint to extract
a code point more efficiently. For now, we continue to only handle BMP characters.

  • mathml/MathMLTokenElement.cpp:

(WebCore::MathMLTokenElement::convertToSingleCodePoint): Helper function to try and convert a
string to a single code point after having removed leading and trailing space.

  • mathml/MathMLTokenElement.h: Declare convertToSingleCodePoint.
  • rendering/mathml/RenderMathMLToken.cpp:

(WebCore::RenderMathMLToken::updateMathVariantGlyph): Use convertToSingleCodePoint to extract
a code point more efficiently.

Location:
trunk/Source/WebCore
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r204809 r204830  
     12016-08-03  Frederic Wang  <fwang@igalia.com>
     2
     3        Share and improve extraction of character for operator and token elements
     4        https://bugs.webkit.org/show_bug.cgi?id=160462
     5
     6        Reviewed by Darin Adler.
     7
     8        No new tests, already covered by existing tests.
     9
     10        * mathml/MathMLElement.cpp:
     11        (WebCore::MathMLElement::stripLeadingAndTrailingWhitespace): Make this a protected member of
     12        MathMLElement so that it can be used in MathMLTokenElement.
     13        (WebCore::skipLeadingAndTrailingWhitespace): Deleted.
     14        * mathml/MathMLElement.h: Declare stripLeadingAndTrailingWhitespace.
     15        * mathml/MathMLOperatorElement.cpp:
     16        (WebCore::MathMLOperatorElement::parseOperatorChar): Use convertToSingleCodePoint to extract
     17        a code point more efficiently. For now, we continue to only handle BMP characters.
     18        * mathml/MathMLTokenElement.cpp:
     19        (WebCore::MathMLTokenElement::convertToSingleCodePoint): Helper function to try and convert a
     20        string to a single code point after having removed leading and trailing space.
     21        * mathml/MathMLTokenElement.h: Declare convertToSingleCodePoint.
     22        * rendering/mathml/RenderMathMLToken.cpp:
     23        (WebCore::RenderMathMLToken::updateMathVariantGlyph): Use convertToSingleCodePoint to extract
     24        a code point more efficiently.
     25
    1262016-08-23  Frederic Wang  <fwang@igalia.com>
    227
  • trunk/Source/WebCore/mathml/MathMLElement.cpp

    r204692 r204830  
    366366}
    367367
    368 static inline StringView skipLeadingAndTrailingWhitespace(const StringView& stringView)
     368StringView MathMLElement::stripLeadingAndTrailingWhitespace(const StringView& stringView)
    369369{
    370370    unsigned start = 0, stringLength = stringView.length();
     
    471471
    472472    // We first skip whitespace from both ends of the string.
    473     StringView stringView = skipLeadingAndTrailingWhitespace(string);
     473    StringView stringView = stripLeadingAndTrailingWhitespace(string);
    474474
    475475    if (stringView.isEmpty())
  • trunk/Source/WebCore/mathml/MathMLElement.h

    r204692 r204830  
    9494    MathMLElement(const QualifiedName& tagName, Document&);
    9595
     96    static StringView stripLeadingAndTrailingWhitespace(const StringView&);
     97
    9698    void parseAttribute(const QualifiedName&, const AtomicString&) override;
    9799    bool childShouldCreateRenderer(const Node&) const override;
  • trunk/Source/WebCore/mathml/MathMLOperatorElement.cpp

    r204715 r204830  
    5151{
    5252    OperatorChar operatorChar;
    53 
    54     // We collapse the whitespace and replace the hyphens by minus signs.
    55     AtomicString textContent = string.stripWhiteSpace().simplifyWhiteSpace().replace(hyphenMinus, minusSign).impl();
    56 
    57     // We verify whether the operator text can be represented by a single UChar.
    58     // FIXME: This is a really inefficient way to extract a character from a string (https://webkit.org/b/160241#c7).
    59     // FIXME: This does not handle surrogate pairs (https://webkit.org/b/122296).
    60     // FIXME: This does not handle <mo> operators with multiple characters (https://webkit.org/b/124828).
    61     operatorChar.character = textContent.length() == 1 ? textContent[0] : 0;
    62     operatorChar.isVertical = MathMLOperatorDictionary::isVertical(operatorChar.character);
     53    // FIXME: This operator dictionary does not accept multiple characters (https://webkit.org/b/124828).
     54    if (auto codePoint = convertToSingleCodePoint(string)) {
     55        // FIXME: MathMLOperatorDictionary/RenderMathMLOperator/MathOperator do not support non-BMP characters (https://webkit.org/b/122296).
     56        if (U_IS_BMP(codePoint.value())) {
     57            UChar character = codePoint.value();
     58            // The minus sign renders better than the hyphen sign used in some MathML formulas.
     59            if (character == hyphenMinus)
     60                character = minusSign;
     61            operatorChar.character = character;
     62            operatorChar.isVertical = MathMLOperatorDictionary::isVertical(operatorChar.character);
     63        }
     64    }
    6365    return operatorChar;
    6466}
  • trunk/Source/WebCore/mathml/MathMLTokenElement.cpp

    r204715 r204830  
    8989}
    9090
     91Optional<UChar32> MathMLTokenElement::convertToSingleCodePoint(StringView string)
     92{
     93    auto codePoints = stripLeadingAndTrailingWhitespace(string).codePoints();
     94    auto iterator = codePoints.begin();
     95    if (iterator == codePoints.end())
     96        return Nullopt;
     97    Optional<UChar32> character = *iterator;
     98    ++iterator;
     99    return iterator == codePoints.end() ? character : Nullopt;
     100}
     101
    91102}
    92103
  • trunk/Source/WebCore/mathml/MathMLTokenElement.h

    r204715 r204830  
    3838    static Ref<MathMLTokenElement> create(const QualifiedName& tagName, Document&);
    3939
     40    static Optional<UChar32> convertToSingleCodePoint(StringView);
     41
    4042protected:
    4143    MathMLTokenElement(const QualifiedName& tagName, Document&);
  • trunk/Source/WebCore/rendering/mathml/RenderMathMLToken.cpp

    r204715 r204830  
    528528
    529529    const auto& tokenElement = element();
    530     AtomicString textContent = element().textContent().stripWhiteSpace().simplifyWhiteSpace();
    531     if (textContent.length() == 1) {
    532         UChar32 codePoint = textContent[0];
     530    if (auto codePoint = MathMLTokenElement::convertToSingleCodePoint(element().textContent())) {
    533531        MathMLElement::MathVariant mathvariant = mathMLStyle()->mathVariant();
    534532        if (mathvariant == MathMLElement::MathVariant::None)
    535533            mathvariant = tokenElement.hasTagName(MathMLNames::miTag) ? MathMLElement::MathVariant::Italic : MathMLElement::MathVariant::Normal;
    536         UChar32 transformedCodePoint = mathVariant(codePoint, mathvariant);
    537         if (transformedCodePoint != codePoint)
     534        UChar32 transformedCodePoint = mathVariant(codePoint.value(), mathvariant);
     535        if (transformedCodePoint != codePoint.value())
    538536            m_mathVariantGlyph = style().fontCascade().glyphDataForCharacter(transformedCodePoint, !style().isLeftToRightDirection());
    539537    }
Note: See TracChangeset for help on using the changeset viewer.