aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/dw-line-breaking.doc9
-rw-r--r--dw/textblock.hh11
-rw-r--r--dw/textblock_linebreaking.cc21
3 files changed, 31 insertions, 10 deletions
diff --git a/doc/dw-line-breaking.doc b/doc/dw-line-breaking.doc
index ed6e573d..12e89829 100644
--- a/doc/dw-line-breaking.doc
+++ b/doc/dw-line-breaking.doc
@@ -1,7 +1,7 @@
/** \page dw-line-breaking Changes in Line-Breaking and Hyphenation
-<div style="border: 2px solid #ff0000; margin-bottom: 0.5em;
-padding: 0.5em 1em; background-color: #ffe0e0"><b>Warning:</b>
+<div style="border: 2px solid #ffff00; margin-bottom: 0.5em;
+padding: 0.5em 1em; background-color: #ffffe0"><b>Info:</b>
Should be incorporated into dw::Textblock.</div>
<h2>Introduction</h2>
@@ -105,10 +105,7 @@ details in the comment there) has to be included when calculating line
widths.
Some values should be configurable: dw::Textblock::HYPHEN_BREAK, the
-penalty for hyphens. (Unfortunately, this value is currently not well
-defined, because of the integer arithmetics in
-dw::Textblock::BadnessAndPenalty.) Also
-dw::Textblock::Word::stretchability,
+penalty for hyphens. Also dw::Textblock::Word::stretchability,
dw::Textblock::Word::shrinkability, which are both set in
dw::Textblock::addSpace.
diff --git a/dw/textblock.hh b/dw/textblock.hh
index 3242b05f..0c3c6870 100644
--- a/dw/textblock.hh
+++ b/dw/textblock.hh
@@ -17,9 +17,9 @@ namespace dw {
* \brief A Widget for rendering text blocks, i.e. paragraphs or sequences
* of paragraphs.
*
- * <div style="border: 2px solid #ff0000; margin-top: 0.5em;
+ * <div style="border: 2px solid #ffff00; margin-top: 0.5em;
* margin-bottom: 0.5em; padding: 0.5em 1em;
- * background-color: #ffe0e0"><b>Warning:</b> The recent changes (line
+ * background-color: #ffffe0"><b>Info:</b> The recent changes (line
* breaking and hyphenation) have not yet been incorporated into this
* documentation. See \ref dw-line-breaking.</div>
*
@@ -209,7 +209,12 @@ private:
protected:
enum {
- HYPHEN_BREAK = 1000000 // to be tested and tuned
+ /**
+ * The penalty for hyphens, multiplied with 100. So, 100 means
+ * 1.0. See dw::Textblock::BadnessAndPenalty::setPenalty for
+ * more details.
+ */
+ HYPHEN_BREAK = 100
};
struct Line
diff --git a/dw/textblock_linebreaking.cc b/dw/textblock_linebreaking.cc
index dd415769..2e2a23c9 100644
--- a/dw/textblock_linebreaking.cc
+++ b/dw/textblock_linebreaking.cc
@@ -91,9 +91,28 @@ void Textblock::BadnessAndPenalty::calcBadness (int totalWidth, int idealWidth,
}
}
+/**
+ * Sets the penalty, multiplied with 100. Multiplication is necessary
+ * to deal with fractional numbers, without having to use floating
+ * point numbers. So, to set a penalty to 0.5, pass 50.
+ *
+ * The definition of penalties depends on the definition of badness,
+ * which adheres to the description in \ref dw-line-breaking, section
+ * "Criteria for Line-Breaking". The exact calculation may vary, but
+ * this definition of should be rather stable: (i)&nbsp;A perfectly
+ * fitting line has a badness of 0. (ii)&nbsp;A line, where all spaces
+ * are extended by exactly the stretchabilty, as well as a line, where
+ * all spaces are reduced by the shrinkability, have a badness of 1.
+ */
void Textblock::BadnessAndPenalty::setPenalty (int penalty)
{
- this->penalty = penalty;
+ // This factor consists of: (i) 100^3, since in calcBadness(), the
+ // ratio is multiplied with 100 (again, to use integer numbers for
+ // fractional numbers), and the badness (which has to be compared
+ // to the penalty!) is the third power or it; (ii) the denominator
+ // 100, of course, since 100 times the penalty is passed to this
+ // method.
+ this->penalty = penalty * (100 * 100 * 100 / 100);
penaltyState = PENALTY_VALUE;
}