aboutsummaryrefslogtreecommitdiff
path: root/dw/textblock_linebreaking.cc
diff options
context:
space:
mode:
Diffstat (limited to 'dw/textblock_linebreaking.cc')
-rw-r--r--dw/textblock_linebreaking.cc85
1 files changed, 58 insertions, 27 deletions
diff --git a/dw/textblock_linebreaking.cc b/dw/textblock_linebreaking.cc
index 58b8c494..b1a2cbd9 100644
--- a/dw/textblock_linebreaking.cc
+++ b/dw/textblock_linebreaking.cc
@@ -91,7 +91,7 @@ void Textblock::BadnessAndPenalty::calcBadness (int totalWidth, int idealWidth,
badness = ratio * ratio * ratio;
}
}
- } else { // if (word->totalWidth > availWidth)
+ } else { // if (totalWidth > availWidth)
if (totalShrinkability == 0)
badnessState = TOO_TIGHT;
else {
@@ -274,9 +274,9 @@ void Textblock::printWord (Word *word)
printWordWithFlags (word);
printf (" [%d / %d + %d - %d => %d + %d - %d] => ",
- word->size.width, word->origSpace, getStretchability(word),
- getShrinkability(word), word->totalWidth, word->totalStretchability,
- word->totalShrinkability);
+ word->size.width, word->origSpace, getSpaceStretchability(word),
+ getSpaceShrinkability(word), word->totalWidth,
+ word->totalSpaceStretchability, word->totalSpaceShrinkability);
word->badnessAndPenalty.print ();
}
@@ -291,18 +291,19 @@ void Textblock::justifyLine (Line *line, int diff)
* values. */
if (diff > 0) {
- int stretchabilitySum = 0;
+ int spaceStretchabilitySum = 0;
for (int i = line->firstWord; i < line->lastWord; i++)
- stretchabilitySum += getStretchability(words->getRef(i));
+ spaceStretchabilitySum += getSpaceStretchability(words->getRef(i));
- if (stretchabilitySum > 0) {
- int stretchabilityCum = 0;
+ if (spaceStretchabilitySum > 0) {
+ int spaceStretchabilityCum = 0;
int spaceDiffCum = 0;
for (int i = line->firstWord; i < line->lastWord; i++) {
Word *word = words->getRef (i);
- stretchabilityCum += getStretchability(word);
+ spaceStretchabilityCum += getSpaceStretchability(word);
int spaceDiff =
- stretchabilityCum * diff / stretchabilitySum - spaceDiffCum;
+ spaceStretchabilityCum * diff / spaceStretchabilitySum
+ - spaceDiffCum;
spaceDiffCum += spaceDiff;
PRINTF (" %d (of %d): diff = %d\n", i, words->size (),
@@ -312,18 +313,19 @@ void Textblock::justifyLine (Line *line, int diff)
}
}
} else if (diff < 0) {
- int shrinkabilitySum = 0;
+ int spaceShrinkabilitySum = 0;
for (int i = line->firstWord; i < line->lastWord; i++)
- shrinkabilitySum += getShrinkability(words->getRef(i));
+ spaceShrinkabilitySum += getSpaceShrinkability(words->getRef(i));
- if (shrinkabilitySum > 0) {
- int shrinkabilityCum = 0;
+ if (spaceShrinkabilitySum > 0) {
+ int spaceShrinkabilityCum = 0;
int spaceDiffCum = 0;
for (int i = line->firstWord; i < line->lastWord; i++) {
Word *word = words->getRef (i);
- shrinkabilityCum += getShrinkability(word);
+ spaceShrinkabilityCum += getSpaceShrinkability(word);
int spaceDiff =
- shrinkabilityCum * diff / shrinkabilitySum - spaceDiffCum;
+ spaceShrinkabilityCum * diff / spaceShrinkabilitySum
+ - spaceDiffCum;
spaceDiffCum += spaceDiff;
word->effSpace = word->origSpace + spaceDiff;
@@ -1045,23 +1047,31 @@ void Textblock::accumulateWordData (int wordIndex)
if (wordIndex == firstWordOfLine) {
// first word of the (not neccessarily yet existing) line
word->totalWidth = word->size.width + word->hyphenWidth;
- word->totalStretchability = 0;
- word->totalShrinkability = 0;
+ word->maxAscent = word->size.ascent;
+ word->maxDescent = word->size.descent;
+ word->totalSpaceStretchability = 0;
+ word->totalSpaceShrinkability = 0;
} else {
Word *prevWord = words->getRef (wordIndex - 1);
word->totalWidth = prevWord->totalWidth
+ prevWord->origSpace - prevWord->hyphenWidth
+ word->size.width + word->hyphenWidth;
- word->totalStretchability =
- prevWord->totalStretchability + getStretchability(prevWord);
- word->totalShrinkability =
- prevWord->totalShrinkability + getShrinkability(prevWord);
+ word->maxAscent = misc::max (prevWord->size.ascent, word->size.ascent);
+ word->maxDescent = misc::max (prevWord->size.descent, word->size.descent);
+ word->totalSpaceStretchability =
+ prevWord->totalSpaceStretchability + getSpaceStretchability(prevWord);
+ word->totalSpaceShrinkability =
+ prevWord->totalSpaceShrinkability + getSpaceShrinkability(prevWord);
}
+ int totalStretchability =
+ word->totalSpaceStretchability + getLineStretchability (word);
+ int totalShrinkability =
+ word->totalSpaceShrinkability + getLineShrinkability (word);
word->badnessAndPenalty.calcBadness (word->totalWidth, availWidth,
- word->totalStretchability,
- word->totalShrinkability);
+ totalStretchability,
+ totalShrinkability);
//printf (" => ");
//printWord (word);
@@ -1272,7 +1282,7 @@ void Textblock::removeTemporaryLines ()
lines->setSize (nonTemporaryLines);
}
-int Textblock::getShrinkability(struct Word *word)
+int Textblock::getSpaceShrinkability(struct Word *word)
{
if (word->spaceStyle->textAlign == core::style::TEXT_ALIGN_JUSTIFY)
return word->origSpace / 3;
@@ -1280,9 +1290,30 @@ int Textblock::getShrinkability(struct Word *word)
return 0;
}
-int Textblock::getStretchability(struct Word *word)
+int Textblock::getSpaceStretchability(struct Word *word)
{
- return word->origSpace / 2;
+ if (word->spaceStyle->textAlign == core::style::TEXT_ALIGN_JUSTIFY)
+ return word->origSpace / 2;
+ else
+ return 0;
+
+ // Alternative: return word->origSpace / 2;
+}
+
+int Textblock::getLineShrinkability(Word *lastWord)
+{
+ return 0;
+}
+
+int Textblock::getLineStretchability(Word *lastWord)
+{
+ if (lastWord->spaceStyle->textAlign == core::style::TEXT_ALIGN_JUSTIFY)
+ return 0;
+ else
+ return stretchabilityFactor * (lastWord->maxAscent
+ + lastWord->maxDescent) / 100;
+
+ // Alternative: return 0;
}
} // namespace dw