Text justification WIP
authorNiki Roo <niki@nikiroo.be>
Tue, 13 Mar 2018 06:41:39 +0000 (07:41 +0100)
committerNiki Roo <niki@nikiroo.be>
Tue, 13 Mar 2018 06:41:39 +0000 (07:41 +0100)
src/be/nikiroo/utils/StringJustifier.java
src/be/nikiroo/utils/test/StringUtilsTest.java
src/be/nikiroo/utils/test/Test.java

index 4aab2e78ad1fcb3bf1c2cfbb1c4647c771138366..d968098aa9e02f5fc895e578bd42b79c8619d163 100644 (file)
@@ -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<String> left2(final String data, final int width) {
+               List<String> result = new LinkedList<String>();
+
+               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;
        }
index 274e5e881bf7bcf676bdbabda8ff6abe0b6f7e14..452b9061185f9838b2145fb74c7e533bb99d0c11 100644 (file)
@@ -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<String> 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);
                                }
                        }
                });
index dd6bf611792c302fc239082c8557583fff111271..bb08e2b4c19ac94e0b103647ed03e28e18713855 100644 (file)
@@ -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;