diff options
-rw-r--r-- | dw/hyphenator.cc | 21 | ||||
-rw-r--r-- | dw/hyphenator.hh | 6 | ||||
-rw-r--r-- | dw/textblock_linebreaking.cc | 19 | ||||
-rw-r--r-- | test/liang.cc | 18 |
4 files changed, 39 insertions, 25 deletions
diff --git a/dw/hyphenator.cc b/dw/hyphenator.cc index b617993a..f6f85009 100644 --- a/dw/hyphenator.cc +++ b/dw/hyphenator.cc @@ -130,7 +130,7 @@ bool Hyphenator::isHyphenationCandidate (const char *word) * Given a word, returns a list of pieces, broken at the possible * hyphenation points. */ -Vector <String> *Hyphenator::hyphenateWord(const char *word) +Vector <String> *Hyphenator::_hyphenateWord(const char *word) { Vector <String> *pieces = new Vector <String> (1, true); @@ -207,4 +207,23 @@ Vector <String> *Hyphenator::hyphenateWord(const char *word) return pieces; } +int *Hyphenator::hyphenateWord(const char *word, int *numBreaks) +{ + Vector <String> *pieces = _hyphenateWord (word); + *numBreaks = pieces->size () - 1; + int *breakPos; + if (numBreaks == 0) + breakPos = NULL; + else { + breakPos = new int[*numBreaks]; + + for (int i = 0; i < *numBreaks; i++) + breakPos[i] = + strlen (pieces->get(i)->chars()) + (i == 0 ? 0 : breakPos[i - 1]); + } + + delete pieces; + return breakPos; +} + } // namespace dw diff --git a/dw/hyphenator.hh b/dw/hyphenator.hh index 839f6cc9..aa60ce86 100644 --- a/dw/hyphenator.hh +++ b/dw/hyphenator.hh @@ -26,6 +26,9 @@ private: <lout::object::Integer> > *tree; void insertPattern (char *s); + lout::container::typed::Vector <lout::object::String> + *_hyphenateWord(const char *word); + public: Hyphenator (core::Platform *platform, const char *filename); ~Hyphenator(); @@ -34,8 +37,7 @@ public: const char *language); static bool isHyphenationCandidate (const char *word); - lout::container::typed::Vector <lout::object::String> - *hyphenateWord(const char *word); + int *hyphenateWord(const char *word, int *numBreaks); }; } // namespace dw diff --git a/dw/textblock_linebreaking.cc b/dw/textblock_linebreaking.cc index 3d612097..dd0816e0 100644 --- a/dw/textblock_linebreaking.cc +++ b/dw/textblock_linebreaking.cc @@ -503,18 +503,9 @@ void Textblock::hyphenateWord (int wordIndex) Hyphenator *hyphenator = Hyphenator::getHyphenator (layout->getPlatform (), "de"); // TODO lang - // TODO Change interface of Hyphenator. - container::typed::Vector <object::String> *pieces = - hyphenator->hyphenateWord (word->content.text); - if (pieces->size () > 1) { - int numBreaks = pieces->size () - 1; - int breakPos[numBreaks]; - for (int i = 0; i < numBreaks; i++) - breakPos[i] = - strlen (pieces->get(i)->chars()) + (i == 0 ? 0 : breakPos[i - 1]); - - for (int i = 0; i < numBreaks; i++) - printf (" breakPos[%d]: %d\n", i, breakPos[i]); + int numBreaks; + int *breakPos = hyphenator->hyphenateWord (word->content.text, &numBreaks); + if (numBreaks > 0) { // TODO unref also spaceStyle and hyphenStyle @@ -575,10 +566,10 @@ void Textblock::hyphenateWord (int wordIndex) //delete origText; TODO: Via textZone? origStyle->unref (); + + delete breakPos; } else word->canBeHyphenated = false; - - delete pieces; } void Textblock::accumulateWordForLine (int lineIndex, int wordIndex) diff --git a/test/liang.cc b/test/liang.cc index e6cda15d..d878420c 100644 --- a/test/liang.cc +++ b/test/liang.cc @@ -1,21 +1,23 @@ #include "../dw/fltkcore.hh" #include "../dw/hyphenator.hh" -using namespace lout::object; -using namespace lout::container::typed; - void hyphenateWord (dw::core::Platform *p, const char *word) { dw::Hyphenator *h = dw::Hyphenator::getHyphenator (p, "de"); - - Vector <String> *pieces = h->hyphenateWord (word); - for (int i = 0; i < pieces->size (); i++) { + + int numBreaks; + int *breakPos = h->hyphenateWord (word, &numBreaks); + for (int i = 0; i < numBreaks + 1; i++) { if (i != 0) putchar ('-'); - printf ("%s", pieces->get(i)->chars ()); + int start = (i == 0 ? 0 : breakPos[i - 1]); + int end = (i == numBreaks ? strlen (word) : breakPos[i]); + for (int j = start; j < end; j++) + putchar (word[j]); } putchar ('\n'); - delete pieces; + if (breakPos) + delete breakPos; } int main (int argc, char *argv[]) |