From 6a493e0518e4b88cc68f6d4aae1faea08aa3afce Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Tue, 13 Mar 2018 07:41:39 +0100 Subject: [PATCH] Text justification WIP --- src/be/nikiroo/utils/StringJustifier.java | 20 +++++++++++-- .../nikiroo/utils/test/StringUtilsTest.java | 30 ++++++++++++++++--- src/be/nikiroo/utils/test/Test.java | 5 ++-- 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/be/nikiroo/utils/StringJustifier.java b/src/be/nikiroo/utils/StringJustifier.java index 4aab2e7..d968098 100644 --- a/src/be/nikiroo/utils/StringJustifier.java +++ b/src/be/nikiroo/utils/StringJustifier.java @@ -37,6 +37,22 @@ import java.util.List; * into justified text paragraphs. */ class StringJustifier { + /** + * Process the given text into a list of left-justified lines of a given + * max-width. + * + * @param data + * the text to justify + * @param width + * the maximum width of a line + * + * @return the list of justified lines + */ + static List left2(final String data, final int width) { + List result = new LinkedList(); + + return result; + } /** * Left-justify a string into a list of lines. @@ -104,7 +120,7 @@ class StringJustifier { inWord = true; } } - } // for (int j = 0; j < rawLines[i].length(); j++) + } // next j if (word.length() + line.length() > n) { // This word will exceed the line length. Wrap at it @@ -118,7 +134,7 @@ class StringJustifier { line.append(word); } result.add(line.toString()); - } // for (int i = 0; i < rawLines.length; i++) { + } // next i return result; } diff --git a/src/be/nikiroo/utils/test/StringUtilsTest.java b/src/be/nikiroo/utils/test/StringUtilsTest.java index 274e5e8..452b906 100644 --- a/src/be/nikiroo/utils/test/StringUtilsTest.java +++ b/src/be/nikiroo/utils/test/StringUtilsTest.java @@ -2,6 +2,7 @@ package be.nikiroo.utils.test; import java.util.Date; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -141,10 +142,31 @@ class StringUtilsTest extends TestLauncher { addTest(new TestCase("Justifying") { @Override public void test() throws Exception { - for (String data : new String[] {}) { - // TODO: test it! - // String result = StringUtils.justifyText(data, 5, - // StringUtils.Alignment.LEFT); + for (String data : new String[] { "test", + "let's test some words", "" }) { + int total = 0; + for (String word : data.split((" "))) { + total += word.replace("-", "").replace(" ", "") + .length(); + } + List result = StringUtils.justifyText(data, 5, + StringUtils.Alignment.LEFT); + + System.out.println("["+data+"] -> ["); + + int totalResult = 0; + for (String resultLine : result) { + System.out.println(resultLine); + for (String word : resultLine.split((" "))) { + totalResult += word.replace("-", "") + .replace(" ", "").length(); + } + } + System.out.println("]"); + + assertEquals( + "The number of letters ('-' not included) should be identical before and after", + total, totalResult); } } }); diff --git a/src/be/nikiroo/utils/test/Test.java b/src/be/nikiroo/utils/test/Test.java index dd6bf61..bb08e2b 100644 --- a/src/be/nikiroo/utils/test/Test.java +++ b/src/be/nikiroo/utils/test/Test.java @@ -19,14 +19,15 @@ public class Test extends TestLauncher { public Test(String[] args) { super("Nikiroo-utils", args); + /* addSeries(new ProgressTest(args)); addSeries(new BundleTest(args)); addSeries(new IOUtilsTest(args)); addSeries(new VersionTest(args)); addSeries(new SerialTest(args)); addSeries(new SerialServerTest(args)); - addSeries(new StringUtilsTest(args)); - addSeries(new TempFilesTest(args)); + */addSeries(new StringUtilsTest(args)); + //addSeries(new TempFilesTest(args)); // TODO: test cache and downloader Cache cache = null; -- 2.27.0