2010-07-12 Andreas Kling <andreas.kling@nokia.com>

        Reviewed by Oliver Hunt.

        CSS color parsing optimizations
        https://bugs.webkit.org/show_bug.cgi?id=42073

        - Avoid instantiating a CSSParser in parseColor() unless necessary.
        - Fixed hex color fast-path to support strings starting with '#'.
        - Avoid allocating a new string for the value part of a '#' color.

        * css/CSSParser.cpp:
        (WebCore::CSSParser::parseColor):
        * platform/graphics/Color.cpp:
        (WebCore::Color::parseHexColor):
        (WebCore::Color::Color):
        * platform/graphics/Color.h:

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@63099 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent da21751e
2010-07-12 Andreas Kling <andreas.kling@nokia.com>
Reviewed by Oliver Hunt.
CSS color parsing optimizations
https://bugs.webkit.org/show_bug.cgi?id=42073
- Avoid instantiating a CSSParser in parseColor() unless necessary.
- Fixed hex color fast-path to support strings starting with '#'.
- Avoid allocating a new string for the value part of a '#' color.
* css/CSSParser.cpp:
(WebCore::CSSParser::parseColor):
* platform/graphics/Color.cpp:
(WebCore::Color::parseHexColor):
(WebCore::Color::Color):
* platform/graphics/Color.h:
2010-07-09 Alexey Proskuryakov <ap@apple.com>
Reviewed by Darin Adler.
......@@ -291,21 +291,21 @@ bool CSSParser::parseValue(CSSMutableStyleDeclaration* declaration, int id, cons
// possible to set up a default color.
bool CSSParser::parseColor(RGBA32& color, const String& string, bool strict)
{
// First try creating a color specified by name, rgb() or "#" syntax.
if (parseColor(string, color, strict))
return true;
CSSParser parser(true);
RefPtr<CSSMutableStyleDeclaration> dummyStyleDeclaration = CSSMutableStyleDeclaration::create();
// First try creating a color specified by name or the "#" syntax.
if (!parser.parseColor(string, color, strict)) {
RefPtr<CSSMutableStyleDeclaration> dummyStyleDeclaration = CSSMutableStyleDeclaration::create();
// Now try to create a color from rgba() syntax.
if (!parser.parseColor(dummyStyleDeclaration.get(), string))
return false;
// Now try to create a color from the rgb() or rgba() syntax.
if (parser.parseColor(dummyStyleDeclaration.get(), string)) {
CSSValue* value = parser.m_parsedProperties[0]->value();
if (value->cssValueType() == CSSValue::CSS_PRIMITIVE_VALUE) {
CSSPrimitiveValue* primitiveValue = static_cast<CSSPrimitiveValue*>(value);
color = primitiveValue->getRGBA32Value();
}
} else
return false;
CSSValue* value = parser.m_parsedProperties[0]->value();
if (value->cssValueType() == CSSValue::CSS_PRIMITIVE_VALUE) {
CSSPrimitiveValue* primitiveValue = static_cast<CSSPrimitiveValue*>(value);
color = primitiveValue->getRGBA32Value();
}
return true;
......@@ -3716,13 +3716,23 @@ static inline bool parseColorInt(const UChar*& string, const UChar* end, UChar t
bool CSSParser::parseColor(const String &name, RGBA32& rgb, bool strict)
{
if (!strict && Color::parseHexColor(name, rgb))
return true;
const UChar* characters = name.characters();
unsigned length = name.length();
if (!strict && length >= 3) {
if (name[0] == '#') {
if (Color::parseHexColor(characters + 1, length - 1, rgb))
return true;
} else {
if (Color::parseHexColor(characters, length, rgb))
return true;
}
}
// Try rgb() syntax.
if (name.startsWith("rgb(")) {
const UChar* current = name.characters() + 4;
const UChar* end = name.characters() + name.length();
const UChar* current = characters + 4;
const UChar* end = characters + length;
int red;
int green;
int blue;
......
......@@ -26,7 +26,6 @@
#include "config.h"
#include "Color.h"
#include "PlatformString.h"
#include <math.h>
#include <wtf/Assertions.h>
#include <wtf/MathExtras.h>
......@@ -126,9 +125,8 @@ RGBA32 makeRGBAFromCMYKA(float c, float m, float y, float k, float a)
}
// originally moved here from the CSS parser
bool Color::parseHexColor(const String& name, RGBA32& rgb)
bool Color::parseHexColor(const UChar* name, unsigned length, RGBA32& rgb)
{
unsigned length = name.length();
if (length != 3 && length != 6)
return false;
unsigned value = 0;
......@@ -150,6 +148,11 @@ bool Color::parseHexColor(const String& name, RGBA32& rgb)
return true;
}
bool Color::parseHexColor(const String& name, RGBA32& rgb)
{
return parseHexColor(name.characters(), name.length(), rgb);
}
int differenceSquared(const Color& c1, const Color& c2)
{
int dR = c1.red() - c2.red();
......@@ -160,8 +163,8 @@ int differenceSquared(const Color& c1, const Color& c2)
Color::Color(const String& name)
{
if (name.startsWith("#"))
m_valid = parseHexColor(name.substring(1), m_color);
if (name[0] == '#')
m_valid = parseHexColor(name.characters() + 1, name.length() - 1, m_color);
else
setNamedColor(name);
}
......
......@@ -26,6 +26,7 @@
#ifndef Color_h
#define Color_h
#include "PlatformString.h"
#include <wtf/FastAllocBase.h>
#if PLATFORM(CG)
......@@ -53,7 +54,6 @@ struct rgb_color;
namespace WebCore {
class String;
class Color;
typedef unsigned RGBA32; // RGBA quadruplet
......@@ -135,6 +135,7 @@ public:
#endif
static bool parseHexColor(const String& name, RGBA32& rgb);
static bool parseHexColor(const UChar* name, unsigned length, RGBA32& rgb);
static const RGBA32 black = 0xFF000000;
static const RGBA32 white = 0xFFFFFFFF;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment