Patch for unimplemented getComputedStyle font properties

Bruce Karsh bruce at machinephasesystems.com
Thu Jan 6 07:02:08 GMT 2005


I've been using libkhtml to index web pages.  I use it to form a DOM tree and 
then I traverse the tree, extracting words, postitions and attributes.  Many 
of the attributes come from the getComputedStyle member of the AbstractView 
class.  The problem is that not all of the properties are implemented.  I've 
produced a patch which impements the font properties, font, fon-family, 
font-size, font-style, font-variant, font-height and line-height.

The changes are to css_renderstyledeclarationimpl.cpp, css_valueimpl.cpp, and
font.h.

Most of the changes are to css_renderstyledeclarationimpl.cpp and are to code 
that was obviously stubbed.  In css_valueimpl.cpp the code for cssText to 
handle CSS_STRING was also stubbed.  In font.h, I added methods to extract 
certain font properties, namely size, italic, weight and smallcaps.

Lars Knoll suggested to met that I send the patch to kfm-devel.  (He hasn't 
reviewed it):

diff -ur kdelibs-3.3.2/khtml/css/css_renderstyledeclarationimpl.cpp 
kdelibs-3.3.2-new/khtml/css/css_renderstyledeclarationimpl.cpp
--- kdelibs-3.3.2/khtml/css/css_renderstyledeclarationimpl.cpp 2004-05-22 
13:55:33.000000000 -0700
+++ kdelibs-3.3.2-new/khtml/css/css_renderstyledeclarationimpl.cpp 2005-01-05 
17:22:11.172068632 -0800
@@ -134,14 +134,40 @@
     case CSS_PROP_FLOAT:
         break;
     case CSS_PROP_FONT_FAMILY:
+        {
+        QString familyname(m_renderer->font(false).family());
+        if(familyname.find(' ')!= -1) {
+            familyname = QString("\"") + familyname + QString("\"");
+        }
+        val = new CSSPrimitiveValueImpl(familyname, 
CSSPrimitiveValue::CSS_STRING);
+        }
         break;
     case CSS_PROP_FONT_SIZE:
+      {
+        int fontsize = m_renderer->style()->htmlFont().getSize();
+        val = new CSSPrimitiveValueImpl(fontsize, CSSPrimitiveValue::CSS_PX);
+      }
         break;
     case CSS_PROP_FONT_STYLE:
+        // ### For now oblique and italic are the same
+        if( m_renderer->style()->htmlFont().getItalic()) {
+            val = new CSSPrimitiveValueImpl(QString("italic"), 
CSSPrimitiveValue::CSS_STRING);
+        } else {
+            val = new CSSPrimitiveValueImpl(QString("normal"), 
CSSPrimitiveValue::CSS_STRING);
+        }
         break;
     case CSS_PROP_FONT_VARIANT:
+        if(m_renderer->style()->htmlFont().getSmallCaps()) {
+            val = new CSSPrimitiveValueImpl(QString("small-caps"), 
CSSPrimitiveValue::CSS_STRING);
+        }  else {
+            val = new CSSPrimitiveValueImpl(QString("normal"), 
CSSPrimitiveValue::CSS_STRING);
+        }
         break;
     case CSS_PROP_FONT_WEIGHT:
+      {
+        int fontweight = m_renderer->style()->htmlFont().getWeight();
+        val = new CSSPrimitiveValueImpl(fontweight, 
CSSPrimitiveValue::CSS_NUMBER);
+      }
         break;
     case CSS_PROP_HEIGHT:
         val = new CSSPrimitiveValueImpl( m_renderer->height(),
@@ -152,6 +178,11 @@
     case CSS_PROP_LETTER_SPACING:
         break;
     case CSS_PROP_LINE_HEIGHT:
+        {
+        int lineheight = m_renderer->lineHeight(false);
+        val = new CSSPrimitiveValueImpl( lineheight,
+                                         CSSPrimitiveValue::CSS_PX );
+        }
         break;
     case CSS_PROP_LIST_STYLE_IMAGE:
         break;
@@ -280,6 +311,61 @@
     case CSS_PROP_BORDER_WIDTH:
         break;
     case CSS_PROP_FONT:
+        {
+        // [ <'font-style'> || <'font-variant'> || <'font-weight'> ]? 
<'font-size'> [ / <'line-height'> ]? <'font-family'> ] | caption | icon | 
menu | message-box | small-caption | status-bar | inherit
+
+        QString fontvalue;
+
+        // Add in the font-style
+
+        if( m_renderer->style()->htmlFont().getItalic()) {
+            fontvalue += "italic ";
+        } else {
+            fontvalue += "";
+        }
+
+        // Add in the font-variant
+
+        if(m_renderer->style()->htmlFont().getSmallCaps()) {
+            fontvalue += "small-caps ";
+        }  else {
+            fontvalue += "";
+        }
+
+        // Add in the font-weight
+
+        int fontweight = m_renderer->style()->htmlFont().getWeight();
+        fontvalue += QString::number(fontweight);
+        fontvalue += " ";
+
+        // Add in the font-size
+
+        int fontsize = m_renderer->style()->htmlFont().getSize();
+        fontvalue += QString::number(fontsize);
+        fontvalue += "px ";
+
+        // Add in the line height
+
+        int lineheight = m_renderer->lineHeight(false);
+        val = new CSSPrimitiveValueImpl( lineheight,
+                                         CSSPrimitiveValue::CSS_PX );
+        fontvalue += QString::number(lineheight);
+        fontvalue += "px ";
+
+        // Add in the font family
+
+        QString familyname(m_renderer->font(false).family());
+        if(familyname.find(' ')!= -1) {
+            familyname = QString("\"") + familyname + QString("\"");
+        }
+        fontvalue += familyname;
+
+        // Return the resulting string
+
+        val = new CSSPrimitiveValueImpl(fontvalue, 
CSSPrimitiveValue::CSS_STRING);
+
+        }
+
         break;
     case CSS_PROP_LIST_STYLE:
         break;
diff -ur kdelibs-3.3.2/khtml/css/css_valueimpl.cpp 
kdelibs-3.3.2-new/khtml/css/css_valueimpl.cpp
--- kdelibs-3.3.2/khtml/css/css_valueimpl.cpp 2004-05-22 13:55:33.000000000 
-0700
+++ kdelibs-3.3.2-new/khtml/css/css_valueimpl.cpp 2005-01-05 
17:22:55.408343696 -0800
@@ -706,7 +706,7 @@
      // ###
      break;
  case CSSPrimitiveValue::CSS_STRING:
-     // ###
+            text = DOMString(m_value.string);
      break;
  case CSSPrimitiveValue::CSS_URI:
             text  = "url(";
diff -ur kdelibs-3.3.2/khtml/rendering/font.h 
kdelibs-3.3.2-new/khtml/rendering/font.h
--- kdelibs-3.3.2/khtml/rendering/font.h 2004-11-28 09:30:53.000000000 -0800
+++ kdelibs-3.3.2-new/khtml/rendering/font.h 2005-01-05 17:25:07.274297024 
-0800
@@ -153,6 +153,10 @@
     /** returns word spacing
      */
     int getWordSpacing() const { return wordSpacing; }
+    int getSize() const { return fontDef.size; }
+    int getItalic() const {return fontDef.italic; }
+    int getWeight() const {return fontDef.weight; }
+    bool getSmallCaps() const {return fontDef.smallCaps; }
 
 private:
     mutable FontDef fontDef;





More information about the kfm-devel mailing list