changeset 573:79599d1a3fe9

compute relative values
author Johannes Hofmann <Johannes.Hofmann@gmx.de>
date Thu, 13 Nov 2008 16:56:46 +0100
parents 2a712706829a
children 8a7400997d83
files src/css.cc src/cssparser.hh src/styleengine.cc src/styleengine.hh
diffstat 4 files changed, 64 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/css.cc	Thu Nov 13 16:18:56 2008 +0100
+++ b/src/css.cc	Thu Nov 13 16:56:46 2008 +0100
@@ -149,7 +149,7 @@
 void CssContext::buildUserAgentStyle () {
    char *cssBuf =
      "body  {background-color: 0xdcd1ba; font-family: helvetica; color: black;" 
-//     "       margin-left: 5px; margin-top: 5px; margin-bottom: 5px; margin-right: 5px;"
+     "       margin-left: 5px; margin-top: 5px; margin-bottom: 5px; margin-right: 5px;"
      " }"
      ":link {color: blue; text-decoration: underline; cursor: pointer; } "
      ":visited {color: green; text-decoration: underline; cursor: pointer; } "
@@ -159,6 +159,16 @@
      "h4 {font-weight: bold} ";
 
    a_Css_parse (this, cssBuf, strlen (cssBuf), 0, CSS_ORIGIN_USER_AGENT);
+
+   char buf[10000];
+
+   FILE *fp = fopen ("/tmp/style.css", "r");
+   if (fp) {
+      fread (buf, 1, sizeof (buf), fp);
+
+      a_Css_parse (this, buf, strlen (buf), 0, CSS_ORIGIN_AUTHOR);
+
+   }
 }
 
 void CssContext::buildUserStyle () {
--- a/src/cssparser.hh	Thu Nov 13 16:18:56 2008 +0100
+++ b/src/cssparser.hh	Thu Nov 13 16:56:46 2008 +0100
@@ -1,6 +1,8 @@
 #ifndef __CSSPARSER_HH__
 #define __CSSPARSER_HH__
 
+#include "css.hh"
+
 /* The last three ones are never parsed. */
 #define CSS_NUM_INTERNAL_PROPERTIES 3
 #define CSS_NUM_PARSED_PROPERTIES \
--- a/src/styleengine.cc	Thu Nov 13 16:18:56 2008 +0100
+++ b/src/styleengine.cc	Thu Nov 13 16:56:46 2008 +0100
@@ -10,6 +10,8 @@
  */
 
 #include <stdio.h>
+#include <math.h>
+#include "cssparser.hh"
 #include "styleengine.hh"
 
 using namespace dw::core::style;
@@ -207,10 +209,57 @@
       }
    }
 
+   fontAttrs.size = computeValue (fontAttrs.size,
+      stack->get (stack->size () - 2).style->font);
    attrs->font = Font::create (layout, &fontAttrs);
+
+   computeValues (&attrs->borderWidth, attrs->font);
+   computeValues (&attrs->margin, attrs->font);
+   computeValues (&attrs->padding, attrs->font);
+   attrs->width = computeValue (attrs->width, attrs->font);
+   attrs->height = computeValue (attrs->height, attrs->font);
 }
 
 /**
+ * \brief Resolve relative lengths to absolute values.
+ *    The font must be set already.
+ */
+void StyleEngine::computeValues (Box *box, Font *font) {
+   box->bottom = computeValue (box->bottom, font);
+   box->left = computeValue (box->left, font);
+   box->right = computeValue (box->right, font);
+   box->top = computeValue (box->top, font);
+}
+
+int StyleEngine::computeValue (int value, Font *font) {
+   int ret;
+   static float dpmm;
+
+   if (dpmm == 0.0)
+      dpmm = layout->dpiX () / 2.54; /* assume dpiX == dpiY */
+
+   switch (CSS_LENGTH_TYPE (value)) {
+      case CSS_LENGTH_TYPE_PX:
+         ret = (int) rintf (CSS_LENGTH_VALUE(value));
+         break;
+      case CSS_LENGTH_TYPE_MM:
+         ret = (int) rintf (CSS_LENGTH_VALUE(value) * dpmm);
+         break;
+      case CSS_LENGTH_TYPE_EM:
+         ret = (int) rintf (CSS_LENGTH_VALUE(value) * font->size);
+         break;
+      case CSS_LENGTH_TYPE_EX:
+         ret = (int) rintf (CSS_LENGTH_VALUE(value) * font->xHeight);
+         break;
+      default:
+         ret = value;
+         break;
+   }
+   return ret;
+}
+
+
+/**
  * \brief Create a new style object based on the previously opened / closed
  *    HTML elements and the nonCssProperties that have been set.
  *    This method is private. Call style() to get a current style object.
--- a/src/styleengine.hh	Thu Nov 13 16:18:56 2008 +0100
+++ b/src/styleengine.hh	Thu Nov 13 16:56:46 2008 +0100
@@ -19,6 +19,8 @@
 
       dw::core::style::Style *style0 (CssPropertyList *nonCssProperties = NULL);
       void apply (dw::core::style::StyleAttrs *attrs, CssPropertyList *props);
+      int computeValue (int value, dw::core::style::Font *font);
+      void computeValues (dw::core::style::Box *box, dw::core::style::Font *font);
 
    public:
       StyleEngine (dw::core::Layout *layout);