2011-02-06 Andreas Kling <kling@webkit.org>
authorandreas.kling@nokia.com <andreas.kling@nokia.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 6 Feb 2011 16:22:23 +0000 (16:22 +0000)
committerandreas.kling@nokia.com <andreas.kling@nokia.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 6 Feb 2011 16:22:23 +0000 (16:22 +0000)
        Reviewed by Dirk Schulze.

        Refactor Font::normalizeSpaces() to avoid an extra String copy.
        https://bugs.webkit.org/show_bug.cgi?id=53871

        Have normalizeSpaces() take UChar*,length arguments instead of a
        String, since all call sites were creating temporaries from these
        components anyway.

        Font::normalizeSpaces() now always makes 1 String copy,
        instead of 1 in the best case and 2 in the worst case.

        * platform/graphics/Font.cpp:
        (WebCore::Font::normalizeSpaces):
        * platform/graphics/Font.h:
        * platform/graphics/qt/FontQt.cpp:
        (WebCore::drawTextCommon):
        (WebCore::Font::floatWidthForSimpleText):
        (WebCore::Font::floatWidthForComplexText):
        (WebCore::Font::offsetForPositionForSimpleText):
        (WebCore::Font::offsetForPositionForComplexText):
        (WebCore::Font::selectionRectForSimpleText):
        (WebCore::Font::selectionRectForComplexText):
        * svg/SVGFont.cpp:
        (WebCore::SVGTextRunWalker::walk):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@77768 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/platform/graphics/Font.cpp
Source/WebCore/platform/graphics/Font.h
Source/WebCore/platform/graphics/qt/FontQt.cpp
Source/WebCore/svg/SVGFont.cpp

index 57104f72b4b02c917c9e5ae4d31714e17400406e..261e8b55c50846a19249b3cb0d187dc26b3ab48b 100644 (file)
@@ -1,3 +1,31 @@
+2011-02-06  Andreas Kling  <kling@webkit.org>
+
+        Reviewed by Dirk Schulze.
+
+        Refactor Font::normalizeSpaces() to avoid an extra String copy.
+        https://bugs.webkit.org/show_bug.cgi?id=53871
+
+        Have normalizeSpaces() take UChar*,length arguments instead of a
+        String, since all call sites were creating temporaries from these
+        components anyway.
+
+        Font::normalizeSpaces() now always makes 1 String copy,
+        instead of 1 in the best case and 2 in the worst case.
+
+        * platform/graphics/Font.cpp:
+        (WebCore::Font::normalizeSpaces):
+        * platform/graphics/Font.h:
+        * platform/graphics/qt/FontQt.cpp:
+        (WebCore::drawTextCommon):
+        (WebCore::Font::floatWidthForSimpleText):
+        (WebCore::Font::floatWidthForComplexText):
+        (WebCore::Font::offsetForPositionForSimpleText):
+        (WebCore::Font::offsetForPositionForComplexText):
+        (WebCore::Font::selectionRectForSimpleText):
+        (WebCore::Font::selectionRectForComplexText):
+        * svg/SVGFont.cpp:
+        (WebCore::SVGTextRunWalker::walk):
+
 2011-02-06  Andreas Kling  <kling@webkit.org>
 
         Reviewed by Dirk Schulze.
index cd48972f56ff7c26f1d3715665a67f1a754693ce..6bdddfc1f3a659307e1d594c472b2573a076b0a6 100644 (file)
@@ -247,21 +247,15 @@ bool Font::isSVGFont() const
 }
 #endif
 
-String Font::normalizeSpaces(const String& string)
+String Font::normalizeSpaces(const UChar* characters, unsigned length)
 {
-    const UChar* characters = string.characters();
-    unsigned length = string.length();
-    Vector<UChar, 256> buffer(length);
-    bool didReplacement = false;
-
-    for (unsigned i = 0; i < length; ++i) {
-        UChar originalCharacter = characters[i];
-        buffer[i] = normalizeSpaces(originalCharacter);
-        if (buffer[i] != originalCharacter)
-            didReplacement = true;
-    }
+    UChar* buffer;
+    String normalized = String::createUninitialized(length, buffer);
+
+    for (unsigned i = 0; i < length; ++i)
+        buffer[i] = normalizeSpaces(characters[i]);
 
-    return didReplacement ? String(buffer.data(), length) : string;
+    return normalized;
 }
 
 static bool shouldUseFontSmoothing = true;
index 1eac913151abd1ae4de9037141d2191b70673dcd..258240b71395b01e98686dff79e98c32e2a04b91 100644 (file)
@@ -223,7 +223,7 @@ public:
         return character;
     }
 
-    static String normalizeSpaces(const String&);
+    static String normalizeSpaces(const UChar*, unsigned length);
 
 #if ENABLE(SVG_FONTS)
     bool isSVGFont() const;
index 818096f481cd35d9e848c916cbb8851aced7f602..646cd0e75ba59471a78267277ec213be4762d88c 100644 (file)
@@ -107,7 +107,7 @@ static void drawTextCommon(GraphicsContext* ctx, const TextRun& run, const Float
             textStrokePen = QPen(QColor(ctx->strokeColor()), ctx->strokeThickness());
     }
 
-    String sanitized = Font::normalizeSpaces(String(run.characters(), run.length()));
+    String sanitized = Font::normalizeSpaces(run.characters(), run.length());
     QString string = fromRawDataWithoutRef(sanitized);
     QPointF pt(point.x(), point.y());
 
@@ -299,7 +299,7 @@ float Font::floatWidthForSimpleText(const TextRun& run, GlyphBuffer* glyphBuffer
     if (!run.length())
         return 0;
 
-    String sanitized = Font::normalizeSpaces(String(run.characters(), run.length()));
+    String sanitized = Font::normalizeSpaces(run.characters(), run.length());
     QString string = fromRawDataWithoutRef(sanitized);
 
     int w = QFontMetrics(font()).width(string, -1, Qt::TextBypassShaping);
@@ -326,7 +326,7 @@ float Font::floatWidthForComplexText(const TextRun& run, HashSet<const SimpleFon
     if (run.length() == 1 && treatAsSpace(run[0]))
         return QFontMetrics(font()).width(space) + run.expansion();
 
-    String sanitized = Font::normalizeSpaces(String(run.characters(), run.length()));
+    String sanitized = Font::normalizeSpaces(run.characters(), run.length());
     QString string = fromRawDataWithoutRef(sanitized);
 
     int w = QFontMetrics(font()).width(string);
@@ -340,7 +340,7 @@ float Font::floatWidthForComplexText(const TextRun& run, HashSet<const SimpleFon
 int Font::offsetForPositionForSimpleText(const TextRun& run, float position, bool includePartialGlyphs) const
 {
 #if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
-    String sanitized = Font::normalizeSpaces(String(run.characters(), run.length()));
+    String sanitized = Font::normalizeSpaces(run.characters(), run.length());
     QString string = fromRawDataWithoutRef(sanitized);
 
     QFontMetrics fm(font());
@@ -367,7 +367,7 @@ int Font::offsetForPositionForSimpleText(const TextRun& run, float position, boo
 
 int Font::offsetForPositionForComplexText(const TextRun& run, float position, bool) const
 {
-    String sanitized = Font::normalizeSpaces(String(run.characters(), run.length()));
+    String sanitized = Font::normalizeSpaces(run.characters(), run.length());
     QString string = fromRawDataWithoutRef(sanitized);
 
     QTextLayout layout(string, font());
@@ -378,7 +378,7 @@ int Font::offsetForPositionForComplexText(const TextRun& run, float position, bo
 FloatRect Font::selectionRectForSimpleText(const TextRun& run, const FloatPoint& pt, int h, int from, int to) const
 {
 #if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
-    String sanitized = Font::normalizeSpaces(String(run.characters(), run.length()));
+    String sanitized = Font::normalizeSpaces(run.characters(), run.length());
     QString wholeText = fromRawDataWithoutRef(sanitized);
     QString selectedText = fromRawDataWithoutRef(sanitized, from, qMin(to - from, wholeText.length() - from));
 
@@ -394,7 +394,7 @@ FloatRect Font::selectionRectForSimpleText(const TextRun& run, const FloatPoint&
 
 FloatRect Font::selectionRectForComplexText(const TextRun& run, const FloatPoint& pt, int h, int from, int to) const
 {
-    String sanitized = Font::normalizeSpaces(String(run.characters(), run.length()));
+    String sanitized = Font::normalizeSpaces(run.characters(), run.length());
     QString string = fromRawDataWithoutRef(sanitized);
 
     QTextLayout layout(string, font());
index 679320f0f650dfdfbf37bd3dea58d27dffb9b988..a92b9b6b4db9c3c8934356efee74635157bae057 100644 (file)
@@ -244,7 +244,7 @@ struct SVGTextRunWalker {
     {
         ASSERT(0 <= from && from <= to && to - from <= run.length());
 
-        const String text = Font::normalizeSpaces(String(run.data(from), to - from));
+        const String text = Font::normalizeSpaces(run.data(from), to - from);
         Vector<SVGGlyphIdentifier::ArabicForm> chars(charactersWithArabicForm(text, run.rtl()));
 
         SVGGlyphIdentifier identifier;
@@ -274,7 +274,7 @@ struct SVGTextRunWalker {
             // extended to the n-th next character (where n is 'characterLookupRange'), to check for any possible ligature.
             characterLookupRange = endOfScanRange - i;
 
-            String lookupString = Font::normalizeSpaces(String(run.data(i), characterLookupRange));
+            String lookupString = Font::normalizeSpaces(run.data(i), characterLookupRange);
 
             Vector<SVGGlyphIdentifier> glyphs;
             if (haveAltGlyph)