changeset 1693:a9a11a23d707

add lout::misc::roundInt() for double -> int conversion Add lout::misc::roundInt() and use it for double -> int conversion instead of doing the + 0.5 trick all over the place. It was wrong for negative values and we might even replace roundInt() with rint() from libm in the future. Reported-by: corvid
author Johannes Hofmann <Johannes.Hofmann@gmx.de>
date Mon, 23 Aug 2010 22:53:11 +0200
parents 0399d0ecb3b9
children b260148f7771 fea7f14dfaff
files dw/textblock.cc lout/misc.hh src/css.hh src/styleengine.cc
diffstat 4 files changed, 25 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/dw/textblock.cc	Sun Aug 22 06:06:47 2010 +0000
+++ b/dw/textblock.cc	Mon Aug 23 22:53:11 2010 +0200
@@ -1592,8 +1592,8 @@
 
       factor /= (style->font->ascent + style->font->descent);
 
-      size->ascent = size->ascent * factor + 0.5;
-      size->descent = size->descent * factor + 0.5;
+      size->ascent = lout::misc::roundInt(size->ascent * factor);
+      size->descent = lout::misc::roundInt(size->descent * factor);
 
       /* TODO: The containing block's line-height property gives a minimum
        * height for the line boxes. (Even when it's set to 'normal', i.e.,
@@ -1603,8 +1603,9 @@
       if (core::style::isAbsLength (style->lineHeight))
          height = core::style::absLengthVal(style->lineHeight);
       else
-         height = core::style::perLengthVal(style->lineHeight) *
-                  style->font->size;
+         height = lout::misc::roundInt (
+                     core::style::perLengthVal(style->lineHeight) *
+                     style->font->size);
       leading = height - style->font->size;
 
       size->ascent += leading / 2;
--- a/lout/misc.hh	Sun Aug 22 06:06:47 2010 +0000
+++ b/lout/misc.hh	Mon Aug 23 22:53:11 2010 +0200
@@ -38,6 +38,11 @@
    abort ();
 }
 
+inline int roundInt(double d)
+{
+   return (int) ((d > 0) ? (d + 0.5) : (d - 0.5));
+}
+
 /**
  * \brief Instances of a sub class of this interface may be compared (less,
  *    greater).
--- a/src/css.hh	Sun Aug 22 06:06:47 2010 +0000
+++ b/src/css.hh	Mon Aug 23 22:53:11 2010 +0200
@@ -90,7 +90,7 @@
 
    switch (t) {
    case CSS_LENGTH_TYPE_PX:
-      iv = (int) (v + 0.5);
+      iv = lout::misc::roundInt(v);
       if (iv > CSS_LENGTH_INT_MAX)
          iv = CSS_LENGTH_INT_MAX;
       else if (iv < -CSS_LENGTH_INT_MAX)
--- a/src/styleengine.cc	Sun Aug 22 06:06:47 2010 +0000
+++ b/src/styleengine.cc	Mon Aug 23 22:53:11 2010 +0200
@@ -15,6 +15,7 @@
 #include "html_common.hh"
 #include "styleengine.hh"
 
+using namespace lout::misc;
 using namespace dw::core::style;
 
 StyleEngine::StyleEngine (dw::core::Layout *layout) {
@@ -32,7 +33,7 @@
 
    /* Create a dummy font, attribute, and tag for the bottom of the stack. */
    font_attrs.name = prefs.font_sans_serif;
-   font_attrs.size = (int) (14 * prefs.font_factor + 0.5);
+   font_attrs.size = roundInt(14 * prefs.font_factor);
    if (font_attrs.size < prefs.font_min_size)
       font_attrs.size = prefs.font_min_size;
    if (font_attrs.size > prefs.font_max_size)
@@ -233,31 +234,30 @@
             if (p->type == CSS_TYPE_ENUM) {
                switch (p->value.intVal) {
                   case CSS_FONT_SIZE_XX_SMALL:
-                     fontAttrs.size = (int) (11.0 * prefs.font_factor + 0.5);
+                     fontAttrs.size = roundInt(11.0 * prefs.font_factor);
                      break;
                   case CSS_FONT_SIZE_X_SMALL:
-                     fontAttrs.size = (int) (12.0 * prefs.font_factor + 0.5);
+                     fontAttrs.size = roundInt(12.0 * prefs.font_factor);
                      break;
                   case CSS_FONT_SIZE_SMALL:
-                     fontAttrs.size = (int) (13.0 * prefs.font_factor + 0.5);
+                     fontAttrs.size = roundInt(13.0 * prefs.font_factor);
                      break;
                   case CSS_FONT_SIZE_MEDIUM:
-                     fontAttrs.size = (int) (14.0 * prefs.font_factor + 0.5);
+                     fontAttrs.size = roundInt(14.0 * prefs.font_factor);
                      break;
                   case CSS_FONT_SIZE_LARGE:
-                     fontAttrs.size = (int) (15.0 * prefs.font_factor + 0.5);
                      break;
                   case CSS_FONT_SIZE_X_LARGE:
-                     fontAttrs.size = (int) (16.0 * prefs.font_factor + 0.5);
+                     fontAttrs.size = roundInt(16.0 * prefs.font_factor);
                      break;
                   case CSS_FONT_SIZE_XX_LARGE:
-                     fontAttrs.size = (int) (17.0 * prefs.font_factor + 0.5);
+                     fontAttrs.size = roundInt(17.0 * prefs.font_factor);
                      break;
                   case CSS_FONT_SIZE_SMALLER:
-                     fontAttrs.size -= (int) (1.0 * prefs.font_factor + 0.5);
+                     fontAttrs.size -= roundInt(1.0 * prefs.font_factor);
                      break;
                   case CSS_FONT_SIZE_LARGER:
-                     fontAttrs.size += (int) (1.0 * prefs.font_factor + 0.5);
+                     fontAttrs.size += roundInt(1.0 * prefs.font_factor);
                      break;
                   default:
                      assert(false); // invalid font-size enum
@@ -524,13 +524,13 @@
          *dest = (int) CSS_LENGTH_VALUE (value);
          return true;
       case CSS_LENGTH_TYPE_MM:
-         *dest = (int) (CSS_LENGTH_VALUE (value) * dpmm + 0.5);
+         *dest = roundInt (CSS_LENGTH_VALUE (value) * dpmm);
         return true;
       case CSS_LENGTH_TYPE_EM:
-         *dest = (int) (CSS_LENGTH_VALUE (value) * font->size + 0.5);
+         *dest = roundInt (CSS_LENGTH_VALUE (value) * font->size);
         return true;
       case CSS_LENGTH_TYPE_EX:
-         *dest = (int) (CSS_LENGTH_VALUE(value) * font->xHeight + 0.5);
+         *dest = roundInt (CSS_LENGTH_VALUE(value) * font->xHeight);
         return true;
       default:
          break;
@@ -542,7 +542,7 @@
 bool StyleEngine::computeValue (int *dest, CssLength value, Font *font,
                                 int percentageBase) {
    if (CSS_LENGTH_TYPE (value) == CSS_LENGTH_TYPE_PERCENTAGE) {
-      *dest = (int) (CSS_LENGTH_VALUE (value) * percentageBase + 0.5);
+      *dest = roundInt (CSS_LENGTH_VALUE (value) * percentageBase);
       return true;
    } else
       return computeValue (dest, value, font);