We're changing the way color is specified as a parameter in the
authorrjw <rjw@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 28 Jul 2004 20:43:31 +0000 (20:43 +0000)
committerrjw <rjw@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 28 Jul 2004 20:43:31 +0000 (20:43 +0000)
        <canvas> API.  Colors are now specified using the CSS color
        functions (or old style names or "#").  For example
        'context.setStrokeColor ("rgba(128,128,128,0.5)")'.  Most of the
        patch is cleanup of duplicated code in cssparser.cpp and a new
        static function that'll crank up the CSS parser to parse the
        color. This patch leaves the old mechanisms in place for now, so
        we don't break any existing gadgets.

        Reviewed by John.

        * WebCore.pbproj/project.pbxproj:
        * khtml/css/cssparser.cpp:
        (CSSParser::CSSParser):
        (CSSParser::setupParser):
        (CSSParser::parseSheet):
        (CSSParser::parseRule):
        (CSSParser::parseValue):
        (CSSParser::parseColor):
        (CSSParser::parseDeclaration):
        * khtml/css/cssparser.h:
        * khtml/ecma/kjs_html.cpp:
        (KJS::Context2DFunction::tryCall):

        * kwq/KWQColor.h:
        (QColor::alpha):
Added alpha() function to QColor, use instead of quirky
qAlpha() global function.

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

WebCore/ChangeLog-2005-08-23
WebCore/WebCore.pbproj/project.pbxproj
WebCore/khtml/css/cssparser.cpp
WebCore/khtml/css/cssparser.h
WebCore/khtml/ecma/kjs_html.cpp
WebCore/kwq/KWQColor.h

index b086a648465d7c45e47d20c48876f4c3363f3445..f460673b835eed23ef249962e54948d1ef9aa70f 100644 (file)
@@ -1,3 +1,34 @@
+2004-07-28  Richard Williamson   <rjw@apple.com>
+
+        We're changing the way color is specified as a parameter in the
+        <canvas> API.  Colors are now specified using the CSS color
+        functions (or old style names or "#").  For example
+        'context.setStrokeColor ("rgba(128,128,128,0.5)")'.  Most of the
+        patch is cleanup of duplicated code in cssparser.cpp and a new
+        static function that'll crank up the CSS parser to parse the
+        color. This patch leaves the old mechanisms in place for now, so
+        we don't break any existing gadgets.
+
+        Reviewed by John.
+
+        * WebCore.pbproj/project.pbxproj:
+        * khtml/css/cssparser.cpp:
+        (CSSParser::CSSParser):
+        (CSSParser::setupParser):
+        (CSSParser::parseSheet):
+        (CSSParser::parseRule):
+        (CSSParser::parseValue):
+        (CSSParser::parseColor):
+        (CSSParser::parseDeclaration):
+        * khtml/css/cssparser.h:
+        * khtml/ecma/kjs_html.cpp:
+        (KJS::Context2DFunction::tryCall):
+
+        * kwq/KWQColor.h:
+        (QColor::alpha):
+       Added alpha() function to QColor, use instead of quirky
+       qAlpha() global function.
+
 2004-07-28  Ken Kocienda  <kocienda@apple.com>
 
         Reviewed by Trey
index 20cbeb1b4a99158e6007c7b4f3a78da5df301a17..8acc777cdb2a97995f308306068260b7fa431a3d 100644 (file)
@@ -13,7 +13,7 @@
                                GCC_ENABLE_FIX_AND_CONTINUE = YES;
                                GCC_GENERATE_DEBUGGING_SYMBOLS = YES;
                                GCC_OPTIMIZATION_LEVEL = 0;
-                               INSTALL_PATH = "@executable_path/../Frameworks";
+                               INSTALL_PATH_MODIFIED_BY_SCRIPT_DO_NOT_COMMIT = "@executable_path/../Frameworks";
                                OPTIMIZATION_CFLAGS = "-O0";
                                OTHER_LDFLAGS = "-seg1addr $(LOCAL_SEG1_ADDR)";
                                SECTORDER_FLAGS = "";
@@ -25,7 +25,7 @@
                014CEA450018CDF011CA2923 = {
                        buildSettings = {
                                COPY_PHASE_STRIP = YES;
-                               INSTALL_PATH = "@executable_path/../Frameworks";
+                               INSTALL_PATH_MODIFIED_BY_SCRIPT_DO_NOT_COMMIT = "@executable_path/../Frameworks";
                                OTHER_LDFLAGS = "-seg1addr $(LOCAL_SEG1_ADDR)";
                                SECTORDER_FLAGS = "";
                        };
                                FRAMEWORK_VERSION = A;
                                GCC_TREAT_WARNINGS_AS_ERRORS = YES;
                                HEADER_SEARCH_PATHS = "ForwardingHeaders /usr/include/libxml2";
-                               INSTALL_PATH = /System/Library/Frameworks/WebKit.framework/Versions/A/Frameworks;
+                               INSTALL_PATH_MODIFIED_BY_SCRIPT_DO_NOT_COMMIT = /System/Library/Frameworks/WebKit.framework/Versions/A/Frameworks;
                                LIBRARY_SEARCH_PATHS = "";
                                LOCAL_SEG1_ADDR = 0x9000000;
                                MACOSX_DEPLOYMENT_TARGET = 10.2;
                F58C8A07025BD3BC018635CA = {
                        buildSettings = {
                                GCC_GENERATE_DEBUGGING_SYMBOLS = YES;
-                               INSTALL_PATH = "@executable_path/../Frameworks";
+                               INSTALL_PATH_MODIFIED_BY_SCRIPT_DO_NOT_COMMIT = "@executable_path/../Frameworks";
                                OTHER_LDFLAGS = "-seg1addr $(LOCAL_SEG1_ADDR)";
                                SECTORDER_FLAGS = "";
                        };
index 17c047e05930a523a6e4113158f791c06d83c38c..86ce8bd5cb87c67a5902490b3ab9b7cfd3b9694c 100644 (file)
@@ -35,6 +35,7 @@
 #include "cssproperties.h"
 #include "cssvalues.h"
 #include "misc/helper.h"
+#include "xml/dom_docimpl.h"
 #include "csshelper.h"
 using namespace DOM;
 
@@ -95,6 +96,7 @@ CSSParser::CSSParser( bool strictParsing )
     numParsedProperties = 0;
     maxParsedProperties = 32;
 
+    data = 0;
     valueList = 0;
     rule = 0;
     id = 0;
@@ -133,21 +135,36 @@ void ParseString::lower()
         string[i] = QChar(string[i]).lower().unicode();
 }
 
-void CSSParser::parseSheet( CSSStyleSheetImpl *sheet, const DOMString &string )
+void CSSParser::setupParser(const char *prefix, const DOMString &string, const char *suffix)
 {
-    styleElement = sheet;
-    defaultNamespace = anyNamespace; // Reset the default namespace.
+    int length = string.length() + strlen(prefix) + strlen(suffix) + 2;
     
-    int length = string.length() + 3;
     data = (unsigned short *)malloc( length *sizeof( unsigned short ) );
-    memcpy( data, string.unicode(), string.length()*sizeof( unsigned short) );
+    for ( unsigned int i = 0; i < strlen(prefix); i++ )
+       data[i] = prefix[i];
+    
+    memcpy( data + strlen( prefix ), string.unicode(), string.length()*sizeof( unsigned short) );
+
+    unsigned int start = strlen( prefix ) + string.length();
+    unsigned int end = start + strlen(suffix);
+    for ( unsigned int i = start; i < end; i++ )
+        data[i] = suffix[i-start];
+
     data[length-1] = 0;
     data[length-2] = 0;
-    data[length-3] = ' ';
+
     yy_hold_char = 0;
     yyleng = 0;
     yytext = yy_c_buf_p = data;
     yy_hold_char = *yy_c_buf_p;
+}
+
+void CSSParser::parseSheet( CSSStyleSheetImpl *sheet, const DOMString &string )
+{
+    styleElement = sheet;
+    defaultNamespace = anyNamespace; // Reset the default namespace.
+    
+    setupParser ("", string, "");
 
 #ifdef CSS_DEBUG
     kdDebug( 6080 ) << ">>>>>>> start parsing style sheet" << endl;
@@ -168,21 +185,7 @@ CSSRuleImpl *CSSParser::parseRule( DOM::CSSStyleSheetImpl *sheet, const DOM::DOM
 {
     styleElement = sheet;
     
-    const char khtml_rule[] = "@-khtml-rule{";
-    int length = string.length() + 4 + strlen(khtml_rule);
-    data = (unsigned short *)malloc( length *sizeof( unsigned short ) );
-    for ( unsigned int i = 0; i < strlen(khtml_rule); i++ )
-       data[i] = khtml_rule[i];
-    memcpy( data + strlen( khtml_rule ), string.unicode(), string.length()*sizeof( unsigned short) );
-    // qDebug("parse string = '%s'", QConstString( (const QChar *)data, length ).string().latin1() );
-    data[length-1] = 0;
-    data[length-2] = 0;
-    data[length-3] = ' ';
-    data[length-4] = '}';
-    yy_hold_char = 0;
-    yyleng = 0;
-    yytext = yy_c_buf_p = data;
-    yy_hold_char = *yy_c_buf_p;
+    setupParser ("@-khtml-rule{", string, "} ");
 
     CSSParser *old = currentParser;
     currentParser = this;
@@ -205,21 +208,7 @@ bool CSSParser::parseValue( DOM::CSSStyleDeclarationImpl *declaration, int _id,
 
     styleElement = declaration->stylesheet();
 
-    const char khtml_value[] = "@-khtml-value{";
-    int length = string.length() + 4 + strlen(khtml_value);
-    data = (unsigned short *)malloc( length *sizeof( unsigned short ) );
-    for ( unsigned int i = 0; i < strlen(khtml_value); i++ )
-       data[i] = khtml_value[i];
-    memcpy( data + strlen( khtml_value ), string.unicode(), string.length()*sizeof( unsigned short) );
-    data[length-1] = 0;
-    data[length-2] = 0;
-    data[length-3] = ' ';
-    data[length-4] = '}';
-    // qDebug("parse string = '%s'", QConstString( (const QChar *)data, length ).string().latin1() );
-    yy_hold_char = 0;
-    yyleng = 0;
-    yytext = yy_c_buf_p = data;
-    yy_hold_char = *yy_c_buf_p;
+    setupParser ("@-khtml-value{", string, "} ");
 
     id = _id;
     important = _important;
@@ -245,6 +234,57 @@ bool CSSParser::parseValue( DOM::CSSStyleDeclarationImpl *declaration, int _id,
     return ok;
 }
 
+QRgb CSSParser::parseColor( const DOM::DOMString &string )
+{
+    QRgb color = 0;
+    DOM::CSSStyleDeclarationImpl *dummyStyleDeclaration = new DOM::CSSStyleDeclarationImpl(0);
+    
+    dummyStyleDeclaration->ref();
+
+    DOM::CSSParser parser(true);
+
+    // First try creating a color specified by name or the "#" syntax.
+    if (!parser.parseColor(string.string(), color)) {
+    
+        // Now try to create a color from the rgb() or rgba() syntax.
+        bool ok = parser.parseColor(dummyStyleDeclaration, string);
+        if ( ok ) {
+            CSSValueImpl *value = parser.parsedProperties[0]->value();
+            if (value->cssValueType() == DOM::CSSValue::CSS_PRIMITIVE_VALUE) {
+                DOM::CSSPrimitiveValueImpl *primitiveValue = static_cast<DOM::CSSPrimitiveValueImpl *>(value);
+                color = primitiveValue->getRGBColorValue();
+            }
+        }
+    
+    }
+    
+    dummyStyleDeclaration->deref();
+    
+    return color;
+}
+
+bool CSSParser::parseColor( DOM::CSSStyleDeclarationImpl *declaration, const DOM::DOMString &string )
+{
+    styleElement = declaration->stylesheet();
+
+    setupParser ( "@-khtml-decls{color:", string, "} ");
+
+    CSSParser *old = currentParser;
+    currentParser = this;
+    cssyyparse( this );
+    currentParser = old;
+
+    delete rule;
+    rule = 0;
+
+    bool ok = false;
+    if ( numParsedProperties && parsedProperties[0]->m_id == CSS_PROP_COLOR) {
+       ok = true;
+    }
+
+    return ok;
+}
+
 bool CSSParser::parseDeclaration( DOM::CSSStyleDeclarationImpl *declaration, const DOM::DOMString &string )
 {
 #ifdef CSS_DEBUG
@@ -253,21 +293,7 @@ bool CSSParser::parseDeclaration( DOM::CSSStyleDeclarationImpl *declaration, con
 
     styleElement = declaration->stylesheet();
 
-    const char khtml_decls[] = "@-khtml-decls{";
-    int length = string.length() + 4 + strlen(khtml_decls);
-    data = (unsigned short *)malloc( length *sizeof( unsigned short ) );
-    for ( unsigned int i = 0; i < strlen(khtml_decls); i++ )
-       data[i] = khtml_decls[i];
-    memcpy( data + strlen( khtml_decls ), string.unicode(), string.length()*sizeof( unsigned short) );
-    data[length-1] = 0;
-    data[length-2] = 0;
-    data[length-3] = ' ';
-    data[length-4] = '}';
-    // qDebug("parse string = '%s'", QConstString( (const QChar *)data, length ).string().latin1() );
-    yy_hold_char = 0;
-    yyleng = 0;
-    yytext = yy_c_buf_p = data;
-    yy_hold_char = *yy_c_buf_p;
+    setupParser ( "@-khtml-decls{", string, "} ");
 
     CSSParser *old = currentParser;
     currentParser = this;
@@ -290,6 +316,7 @@ bool CSSParser::parseDeclaration( DOM::CSSStyleDeclarationImpl *declaration, con
     return ok;
 }
 
+
 void CSSParser::addProperty( int propId, CSSValueImpl *value, bool important )
 {
     CSSProperty *prop = new CSSProperty;
index e2b4b35cecaa7fd6ed958d206577d3b3b94d6b1d..3e80f66a059dc7f79163d9f58304c573f538c751 100644 (file)
@@ -34,6 +34,7 @@ namespace DOM {
     class CSSRuleImpl;
     class CSSStyleRuleImpl;
     class DocumentImpl;
+    class NodeImpl;
     class CSSValueImpl;
     class CSSValueListImpl;
     class CSSPrimitiveValueImpl;
@@ -110,6 +111,8 @@ namespace DOM {
        DOM::CSSRuleImpl *parseRule( DOM::CSSStyleSheetImpl *sheet, const DOM::DOMString &string );
        bool parseValue( DOM::CSSStyleDeclarationImpl *decls, int id, const DOM::DOMString &string,
                         bool _important );
+        static QRgb CSSParser::parseColor( const DOM::DOMString &string );
+       bool parseColor( DOM::CSSStyleDeclarationImpl *declaration, const DOM::DOMString &string );
        bool parseDeclaration( DOM::CSSStyleDeclarationImpl *decls, const DOM::DOMString &string );
 
        static CSSParser *current() { return currentParser; }
@@ -160,8 +163,10 @@ namespace DOM {
        int token() { return yyTok; }
        unsigned short *text( int *length);
        int lex();
+        
     private:
-
+        void setupParser(const char *prefix, const DOMString &string, const char *suffix);
+        
        unsigned short *data;
        unsigned short *yytext;
        unsigned short *yy_c_buf_p;
index 7038505f66e96410a1b19f7c7ad383fab9d44648..3ae15dd17d738fa1870a9aad307927f3041d8566 100644 (file)
@@ -53,6 +53,7 @@
 #include <kdebug.h>
 
 #include "cssparser.h"
+#include "css_stylesheetimpl.h"
 
 #include "qcolor.h"
 #include "qpixmap.h"
@@ -3411,11 +3412,11 @@ Value KJS::Context2DFunction::tryCall(ExecState *exec, Object &thisObj, const Li
             int numArgs = args.size();
             switch (numArgs) {
                 case 1: {
-                    if (args[0].type() == StringType) {
-                        QRgb rgb = 0;
-                        DOM::CSSParser::parseColor(args[0].toString(exec).qstring(), rgb);
-                        QColor color(rgb);
-                        CGContextSetRGBStrokeColor(drawingContext, color.red(), color.green(), color.blue(), 1.);
+                    if (args[0].type() == StringType) {                    
+                        QRgb color = DOM::CSSParser::parseColor(args[0].toString(exec).string());
+                        QColor qc(color);
+                        CGContextSetRGBStrokeColor(drawingContext, qc.red()/255., qc.green()/255., qc.blue()/255., qc.alpha()/255.);
+
                     }
                     else {
                         float g = (float)args[0].toNumber(exec);
@@ -3426,10 +3427,9 @@ Value KJS::Context2DFunction::tryCall(ExecState *exec, Object &thisObj, const Li
                 case 2: {
                     float a = args[1].toNumber(exec);
                     if (args[0].type() == StringType) {
-                        QRgb rgb = 0;
-                        DOM::CSSParser::parseColor(args[0].toString(exec).qstring(), rgb);
-                        QColor color(rgb);
-                        CGContextSetRGBStrokeColor(drawingContext, color.red(), color.green(), color.blue(), a);
+                        QRgb color = DOM::CSSParser::parseColor(args[0].toString(exec).string());
+                        QColor qc(color);
+                        CGContextSetRGBStrokeColor(drawingContext, qc.red()/255., qc.green()/255., qc.blue()/255., a);
                     }
                     else {
                         float g = (float)args[0].toNumber(exec);
@@ -3473,10 +3473,9 @@ Value KJS::Context2DFunction::tryCall(ExecState *exec, Object &thisObj, const Li
             switch (numArgs) {
                 case 1: {
                     if (args[0].type() == StringType) {
-                        QRgb rgb = 0;
-                        DOM::CSSParser::parseColor(args[0].toString(exec).qstring(), rgb);
-                        QColor color(rgb);
-                        CGContextSetRGBFillColor(drawingContext, color.red(), color.green(), color.blue(), 1.);
+                        QRgb color = DOM::CSSParser::parseColor(args[0].toString(exec).string());
+                        QColor qc(color);
+                        CGContextSetRGBFillColor(drawingContext, qc.red()/255., qc.green()/255., qc.blue()/255., qc.alpha()/255.);
                     }
                     else {
                         float g = (float)args[0].toNumber(exec);
@@ -3487,10 +3486,9 @@ Value KJS::Context2DFunction::tryCall(ExecState *exec, Object &thisObj, const Li
                 case 2: {
                     float a = args[1].toNumber(exec);
                     if (args[0].type() == StringType) {
-                        QRgb rgb = 0;
-                        DOM::CSSParser::parseColor(args[0].toString(exec).qstring(), rgb);
-                        QColor color(rgb);
-                        CGContextSetRGBFillColor(drawingContext, color.red(), color.green(), color.blue(), a);
+                        QRgb color = DOM::CSSParser::parseColor(args[0].toString(exec).string());
+                        QColor qc(color);
+                        CGContextSetRGBFillColor(drawingContext, qc.red()/255., qc.green()/255., qc.blue()/255., a);
                     }
                     else {
                         float g = (float)args[0].toNumber(exec);
@@ -3805,12 +3803,11 @@ Value KJS::Context2DFunction::tryCall(ExecState *exec, Object &thisObj, const Li
                 switch (numArgs - 3) {
                     case 1: {
                         if (args[3].type() == StringType) {
-                            QRgb rgb = 0;
-                            DOM::CSSParser::parseColor(args[3].toString(exec).qstring(), rgb);
-                            QColor color(rgb);
-                            components[0] = color.red();
-                            components[1] = color.green();
-                            components[2] = color.blue();
+                            QRgb color = DOM::CSSParser::parseColor(args[3].toString(exec).string());
+                            QColor qc(color);
+                            components[0] = qc.red()/255.;
+                            components[1] = qc.green()/255.;
+                            components[2] = qc.blue()/255.;
                             components[3] = 1.0f;
                             colorSpace = CGColorSpaceCreateDeviceRGB();
                         }
@@ -3824,12 +3821,11 @@ Value KJS::Context2DFunction::tryCall(ExecState *exec, Object &thisObj, const Li
                     case 2: {
                         float a = args[4].toNumber(exec);
                         if (args[3].type() == StringType) {
-                            QRgb rgb = 0;
-                            DOM::CSSParser::parseColor(args[3].toString(exec).qstring(), rgb);
-                            QColor color(rgb);
-                            components[0] = color.red();
-                            components[1] = color.green();
-                            components[2] = color.blue();
+                            QRgb color = DOM::CSSParser::parseColor(args[3].toString(exec).string());
+                            QColor qc(color);
+                            components[0] = qc.red()/255.;
+                            components[1] = qc.green()/255.;
+                            components[2] = qc.blue()/255.;
                             components[3] = a;
                             colorSpace = CGColorSpaceCreateDeviceRGB();
                         }
index d914720d09ba68d20fc286b5b195c2127bb2967b..b9364dc4ec588418fb78637a0938db5b5ae3e4f9 100644 (file)
@@ -55,6 +55,7 @@ public:
     int red() const { return (color >> 16) & 0xFF; }
     int green() const { return (color >> 8) & 0xFF; }
     int blue() const { return color & 0xFF; }
+    int alpha() const { return (color >> 24) & 0xFF; }
     QRgb rgb() const { return color & 0xFFFFFFFF; } // Preserve the alpha.
     void setRgb(int r, int g, int b) { color = qRgb(r, g, b); valid = true; }
     void setRgb(int rgb) { color = rgb; valid = true; /* Alpha may be set. Preserve it. */ }