From d4d54185af6c2217316cab75dae5de26a34d7ae2 Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Mon, 23 Aug 2010 22:53:11 +0200 Subject: 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 --- src/css.hh | 2 +- src/styleengine.cc | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/css.hh b/src/css.hh index b23eb9a3..2e9dd2b5 100644 --- a/src/css.hh +++ b/src/css.hh @@ -90,7 +90,7 @@ inline CssLength CSS_CREATE_LENGTH (float v, CssLengthType t) { 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) diff --git a/src/styleengine.cc b/src/styleengine.cc index 906f47ee..b9f30054 100644 --- a/src/styleengine.cc +++ b/src/styleengine.cc @@ -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 @@ StyleEngine::StyleEngine (dw::core::Layout *layout) { /* 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 @@ void StyleEngine::apply (StyleAttrs *attrs, CssPropertyList *props) { 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 @@ bool StyleEngine::computeValue (int *dest, CssLength value, Font *font) { *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) { 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); -- cgit v1.2.3