HtmlText.java
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009-2024, Arnaud Roques
*
* Project Info: https://plantuml.com
*
* If you like this project or if you find it useful, you can support us at:
*
* https://plantuml.com/patreon (only 1$ per month!)
* https://plantuml.com/paypal
*
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
*
* Original Author: Arnaud Roques
*
*
*/
package net.sourceforge.plantuml.ngdot.html;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* A text: lines made of spans, each span with its own font changes. A line
* ends with a {@code <BR/>}, whose {@code ALIGN} justifies it; the last line
* has no justification of its own.
*/
public final class HtmlText {
/** The justification of a line: {@code 'l'}, {@code 'r'}, {@code 'c'} or {@code 0} for none. */
public static final class Line {
private final List<Span> spans = new ArrayList<Span>();
private char justification;
public List<Span> getSpans() {
return Collections.unmodifiableList(spans);
}
public char getJustification() {
return justification;
}
/** The text of the line, without any font. */
public String getText() {
final StringBuilder sb = new StringBuilder();
for (Span span : spans)
sb.append(span.getText());
return sb.toString();
}
}
public static final class Span {
private final String text;
private final HtmlFont font;
Span(String text, HtmlFont font) {
this.text = text;
this.font = font;
}
public String getText() {
return text;
}
public HtmlFont getFont() {
return font;
}
}
private final List<Line> lines = new ArrayList<Line>();
HtmlText() {
lines.add(new Line());
}
void add(String text, HtmlFont font) {
if (text.length() > 0)
lines.get(lines.size() - 1).spans.add(new Span(text, font));
}
void endLine(char justification) {
lines.get(lines.size() - 1).justification = justification;
lines.add(new Line());
}
/** {@code true} when there is neither a character nor a line break. */
public boolean isEmpty() {
return lines.size() == 1 && lines.get(0).spans.isEmpty();
}
public List<Line> getLines() {
return Collections.unmodifiableList(lines);
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < lines.size(); i++) {
if (i > 0)
sb.append(" / ");
final Line line = lines.get(i);
for (Span span : line.spans) {
final String font = span.font.toString();
sb.append(font.length() == 0 ? "" : "{" + font + "}").append(span.text);
}
if (line.justification != 0)
sb.append('\\').append(line.justification);
}
return sb.toString();
}
// ------------------------------------------------------------------
// Layout, set by HtmlBlock
double width;
double height;
/** The space given to the text, relative to the top left corner of the label. */
double x;
double y;
double boxWidth;
double boxHeight;
/** The default justification of the lines, from BALIGN. */
char defaultJustification = 'c';
}