biskuteri-cafe-JKomasto2/RichTextPane.java
Snowyfox 695d1057a2 Fixed RichTextPane somewhat.
Switched to BreakIterator.
2022-04-15 13:07:22 -04:00

273 lines
5.6 KiB
Java

import javax.swing.JComponent;
import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.awt.FontMetrics;
import java.awt.Image;
import java.awt.Color;
import java.util.List;
import java.util.LinkedList;
import java.util.ListIterator;
class
RichTextPane extends JComponent {
private List<Segment>
text;
// ---%-@-%---
public void
setText(List<Segment> text) { this.text = text; }
// - -%- -
protected void
paintComponent(Graphics g)
{
g.setFont(getFont());
FontMetrics fm = g.getFontMetrics(getFont());
g.clearRect(0, 0, getWidth(), getHeight());
for (Segment segment: text)
{
if (segment.image != null) {
int ow = segment.image.getIconWidth();
int oh = segment.image.getIconHeight();
int h = fm.getHeight();
int w = h * ow / oh;
int x = segment.x, y = segment.y;
Image img = segment.image.getImage();
g.drawImage(img, x, y - h, w, h, this);
continue;
}
if (segment.link != null) g.setColor(Color.BLUE);
g.drawString(segment.text, segment.x, segment.y);
g.setColor(getForeground());
}
}
// - -%- -
public static List<Segment>
layout(List<Segment> text, FontMetrics fm, int width)
{
List<Segment> copy = new LinkedList<>();
for (Segment segment: text) copy.add(segment.clone());
text = copy;
ListIterator<Segment> cursor = text.listIterator();
int x = 0, y = fm.getAscent();
int dy = fm.getHeight();
while (cursor.hasNext())
{
Segment curr = cursor.next();
int dx;
if (curr.image != null) {
int ow = curr.image.getIconWidth();
int oh = curr.image.getIconHeight();
dx = dy * ow / oh;
}
else if (curr.text != null) {
dx = fm.stringWidth(curr.text);
}
else if (curr.link != null) {
curr.text = curr.link;
dx = fm.stringWidth(curr.link);
}
else {
assert false;
dx = 0;
}
boolean fits = x + dx < width;
if (fits || curr.spacer)
{
curr.x = x;
curr.y = y;
x += dx;
if (curr.spacer && curr.text.equals("\n")) {
y += dy;
x = 0;
}
continue;
}
boolean tooLong = dx > width;
boolean canFitChar = width >= fm.getMaxAdvance();
boolean splittable = curr.image == null;
/*
* A bit of redundancy in my conditions, but the point is
* to exactly express the triggers in my mental model.
* The conditions should read more like English.
*/
if (!tooLong || (tooLong && !splittable))
{
curr.x = 0;
curr.y = y += dy;
x = dx;
continue;
}
assert tooLong && splittable;
String s = curr.text;
int splitOffset;
for (splitOffset = 0; splitOffset < s.length(); ++splitOffset)
{
String substring = s.substring(0, splitOffset + 1);
if (fm.stringWidth(substring) > width) break;
}
if (splitOffset == 0) splitOffset = 1;
/*
* I force a split even if our width supports no characters.
* Because if I don't split, the only alternatives to infinitely
* looping downwards is to emplace this segment or ignore it.
*/
Segment fitted = new Segment();
fitted.text = s.substring(0, splitOffset);
fitted.link = curr.link;
fitted.x = x;
fitted.y = y;
cursor.add(fitted);
curr.text = s.substring(splitOffset);
y += dy;
x = 0;
cursor.add(curr); cursor.previous();
/*
* I had to use a stack and return a new list because,
* splitting can turn a long segment into several spread
* over different lines. Here curr becomes the "after-split"
* and I push it back to the stack.
*
* If #layout wasn't a separate method, but rather only for
* graphical painting. Then I don't need a stack nor return
* a new list. I iterate over the given one, filling the
* nodes' geometric information, if a split occurs I save the
* "after-split" in a variable, which in the next iteration
* I use as curr instead of list.next(). The caller doesn't
* need to know the geometry of these intermediate segments.
*/
continue;
}
return text;
}
// ---%-@-%---
public static class
Segment {
public ImageIcon
image;
public String
link;
public String
text;
public boolean
spacer;
public int
x, y;
// -=%=-
public String
toString()
{
StringBuilder b = new StringBuilder();
b.append(getClass().getName() + "[");
b.append("image=" + image);
b.append(",link=" + link);
b.append(",text=" + text);
b.append(",x=" + x);
b.append(",y=" + y);
b.append("]");
return b.toString();
}
public Segment
clone()
{
Segment segment = new Segment();
segment.image = this.image;
segment.link = this.link;
segment.text = this.text;
segment.spacer = this.spacer;
segment.x = this.x;
segment.y = this.y;
return segment;
}
}
public static class
Builder {
private List<Segment>
returnee;
// -=%=-
public
Builder() { returnee = new LinkedList<>(); }
public Builder
image(ImageIcon image, String text)
{
Segment segment = new Segment();
segment.image = image;
segment.text = text;
returnee.add(segment);
return this;
}
public Builder
link(String link, String text)
{
Segment segment = new Segment();
segment.link = link;
segment.text = text;
returnee.add(segment);
return this;
}
public Builder
text(String text)
{
Segment segment = new Segment();
segment.text = text;
returnee.add(segment);
return this;
}
public Builder
spacer(String text)
{
Segment segment = new Segment();
segment.text = text;
segment.spacer = true;
returnee.add(segment);
return this;
}
public List<Segment>
finish() { return returnee; }
}
// ---%-@-%---
RichTextPane()
{
text = new LinkedList<>();
}
}