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 List
<String
> lines
= new LinkedList
<String
>();
54 for (String dataLine
: data
.split("\n")) {
55 String line
= rightTrim(dataLine
.replace("\t", " "));
57 if (width
> 0 && line
.length() > width
) {
58 while (line
.length() > 0) {
59 int i
= Math
.min(line
.length(), width
- 1); // -1 for "-"
61 boolean needDash
= true;
62 // find the best space if any and if needed
64 if (i
< line
.length()) {
66 int space
= line
.indexOf(' ');
68 while (space
> -1 && space
<= i
) {
70 space
= line
.indexOf(' ', space
+ 1);
80 // no dash before space/dash
81 if ((i
+ 1) < line
.length()) {
82 char car
= line
.charAt(i
);
83 char nextCar
= line
.charAt(i
+ 1);
84 if (nextCar
== ' ' || car
== '-' || nextCar
== '-') {
89 // if the space freed by the removed dash allows it, or if
90 // it is the last char, add the next char
91 if (!needDash
|| i
>= line
.length() - 1) {
92 int checkI
= Math
.min(i
+ 1, line
.length());
93 if (checkI
== i
|| checkI
<= width
) {
99 // no dash before parenthesis (but cannot add one more
101 if ((i
+ 1) < line
.length()) {
102 char car
= line
.charAt(i
+ 1);
103 if (car
== '(' || car
== ')') {
109 lines
.add(rightTrim(line
.substring(0, i
)) + "-");
111 lines
.add(rightTrim(line
.substring(0, i
)));
114 // full trim (remove spaces when cutting)
115 line
= line
.substring(i
).trim();
126 * Right-justify a string into a list of lines.
131 * the maximum number of characters in a line
132 * @return the list of lines
134 static List
<String
> right(final String str
, final int n
) {
135 List
<String
> result
= new LinkedList
<String
>();
138 * Same as left(), but preceed each line with spaces to make it n chars
141 List
<String
> lines
= left(str
, n
);
142 for (String line
: lines
) {
143 StringBuilder sb
= new StringBuilder();
144 for (int i
= 0; i
< n
- line
.length(); i
++) {
148 result
.add(sb
.toString());
155 * Center a string into a list of lines.
160 * the maximum number of characters in a line
161 * @return the list of lines
163 static List
<String
> center(final String str
, final int n
) {
164 List
<String
> result
= new LinkedList
<String
>();
167 * Same as left(), but preceed/succeed each line with spaces to make it
170 List
<String
> lines
= left(str
, n
);
171 for (String line
: lines
) {
172 StringBuilder sb
= new StringBuilder();
173 int l
= (n
- line
.length()) / 2;
174 int r
= n
- line
.length() - l
;
175 for (int i
= 0; i
< l
; i
++) {
179 for (int i
= 0; i
< r
; i
++) {
182 result
.add(sb
.toString());
189 * Fully-justify a string into a list of lines.
194 * the maximum number of characters in a line
195 * @return the list of lines
197 static List
<String
> full(final String str
, final int n
) {
198 List
<String
> result
= new LinkedList
<String
>();
201 * Same as left(), but insert spaces between words to make each line n
202 * chars long. The "algorithm" here is pretty dumb: it performs a split
203 * on space and then re-inserts multiples of n between words.
205 List
<String
> lines
= left(str
, n
);
206 for (int lineI
= 0; lineI
< lines
.size() - 1; lineI
++) {
207 String line
= lines
.get(lineI
);
208 String
[] words
= line
.split(" ");
209 if (words
.length
> 1) {
211 for (int i
= 0; i
< words
.length
; i
++) {
212 charCount
+= words
[i
].length();
214 int spaceCount
= n
- charCount
;
215 int q
= spaceCount
/ (words
.length
- 1);
216 int r
= spaceCount
% (words
.length
- 1);
217 StringBuilder sb
= new StringBuilder();
218 for (int i
= 0; i
< words
.length
- 1; i
++) {
220 for (int j
= 0; j
< q
; j
++) {
228 for (int j
= 0; j
< r
; j
++) {
231 sb
.append(words
[words
.length
- 1]);
232 result
.add(sb
.toString());
237 if (lines
.size() > 0) {
238 result
.add(lines
.get(lines
.size() - 1));
245 * Trim the given {@link String} on the right only.
248 * the source {@link String}
249 * @return the right-trimmed String or Empty if it was NULL
251 static private String
rightTrim(String data
) {
255 return ("|" + data
).trim().substring(1);