2 * This file was taken from:
3 * Jexer - Java Text User Interface
5 * The MIT License (MIT)
7 * Copyright (C) 2017 Kevin Lamonte
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
27 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
30 package be
.nikiroo
.utils
;
32 import java
.util
.LinkedList
;
33 import java
.util
.List
;
36 * StringJustifier contains methods to convert one or more long lines of strings
37 * into justified text paragraphs.
39 class StringJustifier
{
41 * Process the given text into a list of left-justified lines of a given
47 * the maximum width of a line
49 * @return the list of justified lines
51 static List
<String
> left(final String data
, final int width
) {
52 return left(data
, width
, false);
56 * Right-justify a string into a list of lines.
61 * the maximum number of characters in a line
62 * @return the list of lines
64 static List
<String
> right(final String str
, final int n
) {
65 List
<String
> result
= new LinkedList
<String
>();
68 * Same as left(), but preceed each line with spaces to make it n chars
71 List
<String
> lines
= left(str
, n
);
72 for (String line
: lines
) {
73 StringBuilder sb
= new StringBuilder();
74 for (int i
= 0; i
< n
- line
.length(); i
++) {
78 result
.add(sb
.toString());
85 * Center a string into a list of lines.
90 * the maximum number of characters in a line
91 * @return the list of lines
93 static List
<String
> center(final String str
, final int n
) {
94 List
<String
> result
= new LinkedList
<String
>();
97 * Same as left(), but preceed/succeed each line with spaces to make it
100 List
<String
> lines
= left(str
, n
);
101 for (String line
: lines
) {
102 StringBuilder sb
= new StringBuilder();
103 int l
= (n
- line
.length()) / 2;
104 int r
= n
- line
.length() - l
;
105 for (int i
= 0; i
< l
; i
++) {
109 for (int i
= 0; i
< r
; i
++) {
112 result
.add(sb
.toString());
119 * Fully-justify a string into a list of lines.
124 * the maximum number of characters in a line
125 * @return the list of lines
127 static List
<String
> full(final String str
, final int n
) {
128 List
<String
> result
= new LinkedList
<String
>();
131 * Same as left(true), but insert spaces between words to make each line
132 * n chars long. The "algorithm" here is pretty dumb: it performs a
133 * split on space and then re-inserts multiples of n between words.
135 List
<String
> lines
= left(str
, n
, true);
136 for (int lineI
= 0; lineI
< lines
.size() - 1; lineI
++) {
137 String line
= lines
.get(lineI
);
138 String
[] words
= line
.split(" ");
139 if (words
.length
> 1) {
141 for (int i
= 0; i
< words
.length
; i
++) {
142 charCount
+= words
[i
].length();
144 int spaceCount
= n
- charCount
;
145 int q
= spaceCount
/ (words
.length
- 1);
146 int r
= spaceCount
% (words
.length
- 1);
147 StringBuilder sb
= new StringBuilder();
148 for (int i
= 0; i
< words
.length
- 1; i
++) {
150 for (int j
= 0; j
< q
; j
++) {
158 for (int j
= 0; j
< r
; j
++) {
161 sb
.append(words
[words
.length
- 1]);
162 result
.add(sb
.toString());
167 if (lines
.size() > 0) {
168 result
.add(lines
.get(lines
.size() - 1));
175 * Process the given text into a list of left-justified lines of a given
179 * the text to justify
181 * the maximum width of a line
183 * use 2 words per line minimum if the text allows it
185 * @return the list of justified lines
187 static private List
<String
> left(final String data
, final int width
,
188 boolean minTwoWords
) {
189 List
<String
> lines
= new LinkedList
<String
>();
191 for (String dataLine
: data
.split("\n")) {
192 String line
= rightTrim(dataLine
.replace("\t", " "));
194 if (width
> 0 && line
.length() > width
) {
195 while (line
.length() > 0) {
196 int i
= Math
.min(line
.length(), width
- 1); // -1 for "-"
198 boolean needDash
= true;
199 // find the best space if any and if needed
201 if (i
< line
.length()) {
203 int space
= line
.indexOf(' ');
206 while (space
> -1 && space
<= i
) {
208 space
= line
.indexOf(' ', space
+ 1);
212 if (prevSpace
> 0 && (!minTwoWords
|| numOfSpaces
>= 2)) {
219 // no dash before space/dash
220 if ((i
+ 1) < line
.length()) {
221 char car
= line
.charAt(i
);
222 char nextCar
= line
.charAt(i
+ 1);
223 if (nextCar
== ' ' || car
== '-' || nextCar
== '-') {
228 // if the space freed by the removed dash allows it, or if
229 // it is the last char, add the next char
230 if (!needDash
|| i
>= line
.length() - 1) {
231 int checkI
= Math
.min(i
+ 1, line
.length());
232 if (checkI
== i
|| checkI
<= width
) {
238 // no dash before parenthesis (but cannot add one more
240 if ((i
+ 1) < line
.length()) {
241 char car
= line
.charAt(i
+ 1);
242 if (car
== '(' || car
== ')') {
248 lines
.add(rightTrim(line
.substring(0, i
)) + "-");
250 lines
.add(rightTrim(line
.substring(0, i
)));
253 // full trim (remove spaces when cutting)
254 line
= line
.substring(i
).trim();
265 * Trim the given {@link String} on the right only.
268 * the source {@link String}
269 * @return the right-trimmed String or Empty if it was NULL
271 static private String
rightTrim(String data
) {
275 return ("|" + data
).trim().substring(1);