diff options
author | Sebastian Geerken <devnull@localhost> | 2013-10-16 16:50:29 +0200 |
---|---|---|
committer | Sebastian Geerken <devnull@localhost> | 2013-10-16 16:50:29 +0200 |
commit | 75f98608a0e799ce294f39ddf538ec9ce63a5e8b (patch) | |
tree | 393a18a40cae538a29e4b00af34290b37298a56f /dw | |
parent | d43b841b1afa34a45d4cfd4435ef02f540708c68 (diff) | |
parent | 02e7d9209c898565be5fb6f70ec84117e07c3633 (diff) |
Merge with main repo.
Diffstat (limited to 'dw')
-rw-r--r-- | dw/textblock.cc | 7 | ||||
-rw-r--r-- | dw/textblock.hh | 17 | ||||
-rw-r--r-- | dw/textblock_linebreaking.cc | 85 |
3 files changed, 78 insertions, 31 deletions
diff --git a/dw/textblock.cc b/dw/textblock.cc index 7950ba96..a79462e1 100644 --- a/dw/textblock.cc +++ b/dw/textblock.cc @@ -176,6 +176,8 @@ int Textblock::penalties[PENALTY_NUM][2] = { { 100, 800 } }; +int Textblock::stretchabilityFactor = 100; + /** * The character which is used to draw a hyphen at the end of a line, * either caused by automatic hyphenation, or by soft hyphens. @@ -212,6 +214,11 @@ void Textblock::setPenaltyEmDashRight2 (int penaltyRightEmDash2) penalties[PENALTY_EM_DASH_RIGHT][1] = penaltyRightEmDash2; } +void Textblock::setStretchabilityFactor (int stretchabilityFactor) +{ + Textblock::stretchabilityFactor = stretchabilityFactor; +} + Textblock::Textblock (bool limitTextWidth) { registerName ("dw::Textblock", &CLASS_ID); diff --git a/dw/textblock.hh b/dw/textblock.hh index 0f5ae38e..b85937ba 100644 --- a/dw/textblock.hh +++ b/dw/textblock.hh @@ -379,8 +379,9 @@ protected: words: the value compared to the ideal width of the line, if the line would be broken after this word. */ - int totalStretchability; // includes all *before* current word - int totalShrinkability; // includes all *before* current word + int maxAscent, maxDescent; + int totalSpaceStretchability; // includes all *before* current word + int totalSpaceShrinkability; // includes all *before* current word BadnessAndPenalty badnessAndPenalty; /* when line is broken after this * word */ @@ -465,6 +466,11 @@ protected: */ static int penalties[PENALTY_NUM][2]; + /** + * ... + */ + static int stretchabilityFactor; + bool limitTextWidth; /* from preferences */ int redrawY; @@ -621,8 +627,10 @@ protected: void handleWordExtremes (int wordIndex); void correctLastWordExtremes (); - static int getShrinkability(struct Word *word); - static int getStretchability(struct Word *word); + static int getSpaceShrinkability(struct Word *word); + static int getSpaceStretchability(struct Word *word); + static int getLineShrinkability(Word *lastWord); + static int getLineStretchability(Word *lastWord); int hyphenateWord (int wordIndex); void accumulateWordForLine (int lineIndex, int wordIndex); void accumulateWordData (int wordIndex); @@ -665,6 +673,7 @@ public: static void setPenaltyEmDashLeft (int penaltyLeftEmDash); static void setPenaltyEmDashRight (int penaltyRightEmDash); static void setPenaltyEmDashRight2 (int penaltyRightEmDash2); + static void setStretchabilityFactor (int stretchabilityFactor); Textblock(bool limitTextWidth); ~Textblock(); 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 |