summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--dw/textblock.cc9
-rw-r--r--lout/misc.hh5
-rw-r--r--src/css.hh2
-rw-r--r--src/styleengine.cc28
4 files changed, 25 insertions, 19 deletions
diff --git a/dw/textblock.cc b/dw/textblock.cc
index c2a803d1..4c02287c 100644
--- a/dw/textblock.cc
+++ b/dw/textblock.cc
@@ -1592,8 +1592,8 @@ void Textblock::calcTextSize (const char *text, size_t len,
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 @@ void Textblock::calcTextSize (const char *text, size_t len,
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;
diff --git a/lout/misc.hh b/lout/misc.hh
index 393bac0c..e78e7576 100644
--- a/lout/misc.hh
+++ b/lout/misc.hh
@@ -38,6 +38,11 @@ inline void assertNotReached ()
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).
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);