changeset 659:920e6090d6d6

use output parameter in StyleEngine::computeValue() This way an assignment is avoided in case of an invalid or unsupported value for the given property. E.g. it is not allowed to specify a percentage for border-width.
author Johannes Hofmann <Johannes.Hofmann@gmx.de>
date Tue, 09 Dec 2008 20:09:35 +0100
parents ea961706be66
children a21202b56624
files src/html.cc src/styleengine.cc src/styleengine.hh
diffstat 3 files changed, 53 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/src/html.cc	Tue Dec 09 18:48:47 2008 +0100
+++ b/src/html.cc	Tue Dec 09 20:09:35 2008 +0100
@@ -2010,11 +2010,11 @@
    // TODO: the same for percentage and relative lengths.
    if (width_ptr) {
       l_w = a_Html_parse_length (html, width_ptr);
-      w = CSS_LENGTH_TYPE(l_w) == CSS_LENGTH_TYPE_PX ? CSS_LENGTH_VALUE(l_w) : 0;
+      w = (int) CSS_LENGTH_TYPE(l_w) == CSS_LENGTH_TYPE_PX ? CSS_LENGTH_VALUE(l_w) : 0;
    }
    if (height_ptr) {
       l_h = a_Html_parse_length (html, height_ptr);
-      h = CSS_LENGTH_TYPE(l_h) == CSS_LENGTH_TYPE_PX ? CSS_LENGTH_VALUE(l_h) : 0;
+      h = (int) CSS_LENGTH_TYPE(l_h) == CSS_LENGTH_TYPE_PX ? CSS_LENGTH_VALUE(l_h) : 0;
    }
    if (w < 0 || h < 0 || abs(w*h) > MAX_W * MAX_H) {
       dFree(width_ptr);
@@ -2022,8 +2022,10 @@
       width_ptr = height_ptr = NULL;
       MSG("a_Html_add_new_image: suspicious image size request %dx%d\n", w, h);
    } else {
-      props.set (CssProperty::CSS_PROPERTY_WIDTH, l_w);
-      props.set (CssProperty::CSS_PROPERTY_HEIGHT, l_h);
+      if (width_ptr)
+         props.set (CssProperty::CSS_PROPERTY_WIDTH, l_w);
+      if (height_ptr)
+         props.set (CssProperty::CSS_PROPERTY_HEIGHT, l_h);
    }
 
    /* TODO: we should scale the image respecting its ratio.
@@ -2037,6 +2039,7 @@
    if ((attrbuf = a_Html_get_attr(html, tag, tagsize, "hspace"))) {
       space = strtol(attrbuf, NULL, 10);
       if (space > 0) {
+         space = CSS_CREATE_LENGTH(space, CSS_LENGTH_TYPE_PX);
          props.set (CssProperty::CSS_PROPERTY_MARGIN_LEFT, space);
          props.set (CssProperty::CSS_PROPERTY_MARGIN_RIGHT, space);
       }
@@ -2046,6 +2049,7 @@
    if ((attrbuf = a_Html_get_attr(html, tag, tagsize, "vspace"))) {
       space = strtol(attrbuf, NULL, 10);
       if (space > 0) {
+         space = CSS_CREATE_LENGTH(space, CSS_LENGTH_TYPE_PX);
          props.set (CssProperty::CSS_PROPERTY_MARGIN_TOP, space);
          props.set (CssProperty::CSS_PROPERTY_MARGIN_BOTTOM, space);
       }
--- a/src/styleengine.cc	Tue Dec 09 18:48:47 2008 +0100
+++ b/src/styleengine.cc	Tue Dec 09 20:09:35 2008 +0100
@@ -159,11 +159,8 @@
             break;
          case CssProperty::CSS_PROPERTY_FONT_SIZE:
             parentFont = stack->get (stack->size () - 2).style->font;
-            if (CSS_LENGTH_TYPE (p->value.intVal) == CSS_LENGTH_TYPE_PERCENTAGE)
-               fontAttrs.size = (int) (CSS_LENGTH_VALUE (p->value.intVal) * 
-                  parentFont->size);
-             else
-               fontAttrs.size = computeValue (p->value.intVal, parentFont);
+            computeValue (&fontAttrs.size, p->value.intVal, parentFont,
+               parentFont->size);
             break;
          case CssProperty::CSS_PROPERTY_FONT_STYLE:
             fontAttrs.style = (FontStyle) p->value.intVal;
@@ -230,20 +227,20 @@
             attrs->borderStyle.top = (BorderStyle) p->value.intVal;
             break;
          case CssProperty::CSS_PROPERTY_BORDER_BOTTOM_WIDTH:
-            attrs->borderWidth.bottom = computeValue (p->value.intVal, attrs->font);
+            computeValue (&attrs->borderWidth.bottom, p->value.intVal, attrs->font);
             break;
          case CssProperty::CSS_PROPERTY_BORDER_LEFT_WIDTH:
-            attrs->borderWidth.left = computeValue (p->value.intVal, attrs->font);
+            computeValue (&attrs->borderWidth.left, p->value.intVal, attrs->font);
             break;
          case CssProperty::CSS_PROPERTY_BORDER_RIGHT_WIDTH:
-            attrs->borderWidth.right = computeValue (p->value.intVal, attrs->font);
+            computeValue (&attrs->borderWidth.right, p->value.intVal, attrs->font);
             break;
          case CssProperty::CSS_PROPERTY_BORDER_TOP_WIDTH:
-            attrs->borderWidth.top = computeValue (p->value.intVal, attrs->font);
+            computeValue (&attrs->borderWidth.top, p->value.intVal, attrs->font);
             break;
          case CssProperty::CSS_PROPERTY_BORDER_SPACING:
-            attrs->hBorderSpacing = computeValue (p->value.intVal, attrs->font);
-            attrs->vBorderSpacing = attrs->hBorderSpacing;
+            computeValue (&attrs->hBorderSpacing, p->value.intVal, attrs->font);
+            computeValue (&attrs->vBorderSpacing, p->value.intVal, attrs->font);
             break;
          case CssProperty::CSS_PROPERTY_COLOR:
             attrs->color = Color::createSimple (layout, p->value.intVal);
@@ -255,28 +252,28 @@
             attrs->listStyleType = (ListStyleType) p->value.intVal;
             break;
          case CssProperty::CSS_PROPERTY_MARGIN_BOTTOM:
-            attrs->margin.bottom = computeValue (p->value.intVal, attrs->font);
+            computeValue (&attrs->margin.bottom, p->value.intVal, attrs->font);
             break;
          case CssProperty::CSS_PROPERTY_MARGIN_LEFT:
-            attrs->margin.left = computeValue (p->value.intVal, attrs->font);
+            computeValue (&attrs->margin.left, p->value.intVal, attrs->font);
             break;
          case CssProperty::CSS_PROPERTY_MARGIN_RIGHT:
-            attrs->margin.right = computeValue (p->value.intVal, attrs->font);
+            computeValue (&attrs->margin.right, p->value.intVal, attrs->font);
             break;
          case CssProperty::CSS_PROPERTY_MARGIN_TOP:
-            attrs->margin.top = computeValue (p->value.intVal, attrs->font);
+            computeValue (&attrs->margin.top, p->value.intVal, attrs->font);
             break;
          case CssProperty::CSS_PROPERTY_PADDING_TOP:
-            attrs->padding.top = computeValue (p->value.intVal, attrs->font);
+            computeValue (&attrs->padding.top, p->value.intVal, attrs->font);
             break;
          case CssProperty::CSS_PROPERTY_PADDING_BOTTOM:
-            attrs->padding.bottom = computeValue (p->value.intVal, attrs->font);
+            computeValue (&attrs->padding.bottom, p->value.intVal, attrs->font);
             break;
          case CssProperty::CSS_PROPERTY_PADDING_LEFT:
-            attrs->padding.left = computeValue (p->value.intVal, attrs->font);
+            computeValue (&attrs->padding.left, p->value.intVal, attrs->font);
             break;
          case CssProperty::CSS_PROPERTY_PADDING_RIGHT:
-            attrs->padding.right = computeValue (p->value.intVal, attrs->font);
+            computeValue (&attrs->padding.right, p->value.intVal, attrs->font);
             break;
          case CssProperty::CSS_PROPERTY_TEXT_ALIGN:
             attrs->textAlign = (TextAlignType) p->value.intVal;
@@ -288,10 +285,11 @@
             attrs->valign = (VAlignType) p->value.intVal;
             break;
          case CssProperty::CSS_PROPERTY_WIDTH:
-               attrs->width = computeLength (p->value.intVal, attrs->font);
+            computeLength (&attrs->width, p->value.intVal, attrs->font);
             break;
          case CssProperty::CSS_PROPERTY_HEIGHT:
-               attrs->height = computeLength (p->value.intVal, attrs->font);
+            computeLength (&attrs->height, p->value.intVal, attrs->font);
+            break;
             break;
          case CssProperty::PROPERTY_X_LINK:
             attrs->x_link = p->value.intVal;
@@ -310,8 +308,7 @@
  * \brief Resolve relative lengths to absolute values.
  */
 
-int StyleEngine::computeValue (CssLength value, Font *font) {
-   int ret;
+void StyleEngine::computeValue (int *dest, CssLength value, Font *font) {
    static float dpmm;
 
    if (dpmm == 0.0)
@@ -319,29 +316,40 @@
 
    switch (CSS_LENGTH_TYPE (value)) {
       case CSS_LENGTH_TYPE_PX:
-         ret = (int) CSS_LENGTH_VALUE (value);
+         *dest = (int) CSS_LENGTH_VALUE (value);
          break;
       case CSS_LENGTH_TYPE_MM:
-         ret = (int) (CSS_LENGTH_VALUE (value) * dpmm);
+         *dest = (int) (CSS_LENGTH_VALUE (value) * dpmm);
          break;
       case CSS_LENGTH_TYPE_EM:
-         ret = (int) (CSS_LENGTH_VALUE (value) * font->size);
+         *dest = (int) (CSS_LENGTH_VALUE (value) * font->size);
          break;
       case CSS_LENGTH_TYPE_EX:
-         ret = (int) (CSS_LENGTH_VALUE(value) * font->xHeight);
+         *dest = (int) (CSS_LENGTH_VALUE(value) * font->xHeight);
          break;
       default:
-         ret = value;
          break;
    }
-   return ret;
 }
 
-dw::core::style::Length StyleEngine::computeLength (CssLength value, Font *font) {
+void StyleEngine::computeValue (int *dest, CssLength value, Font *font,
+                                int percentageBase) {
    if (CSS_LENGTH_TYPE (value) == CSS_LENGTH_TYPE_PERCENTAGE)
-      return createPerLength (CSS_LENGTH_VALUE (value));
-    else
-      return createAbsLength (computeValue (value, font));
+      *dest = (int) (CSS_LENGTH_VALUE (value) * percentageBase);
+   else
+      computeValue (dest, value, font);
+}
+
+void StyleEngine::computeLength (dw::core::style::Length *dest,
+                                 CssLength value, Font *font) {
+   int v;
+
+   if (CSS_LENGTH_TYPE (value) == CSS_LENGTH_TYPE_PERCENTAGE)
+      *dest = createPerLength (CSS_LENGTH_VALUE (value));
+    else {
+      computeValue (&v, value, font);
+      *dest = createAbsLength (v);
+   }
 }
 
 /**
--- a/src/styleengine.hh	Tue Dec 09 18:48:47 2008 +0100
+++ b/src/styleengine.hh	Tue Dec 09 20:09:35 2008 +0100
@@ -23,8 +23,10 @@
       dw::core::style::Style *style0 (CssPropertyList *nonCssHints = NULL);
       dw::core::style::Style *wordStyle0 (CssPropertyList *nonCssHints = NULL);
       void apply (dw::core::style::StyleAttrs *attrs, CssPropertyList *props);
-      int computeValue (CssLength value, dw::core::style::Font *font);
-      dw::core::style::Length computeLength (CssLength value, dw::core::style::Font *font);
+      void computeValue (int *dest, CssLength value, dw::core::style::Font *font);
+      void computeValue (int *dest, CssLength value, dw::core::style::Font *font,
+         int percentageBase);
+      void computeLength (dw::core::style::Length *dest, CssLength value, dw::core::style::Font *font);
 
    public:
       StyleEngine (dw::core::Layout *layout);