1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-01 08:00:48 +00:00
plantuml/src/net/sourceforge/plantuml/creole/legacy/StripeSimple.java

334 lines
12 KiB
Java
Raw Normal View History

2013-12-10 19:36:50 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2013-12-10 19:36:50 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2013-12-10 19:36:50 +00:00
*
2017-03-15 19:13:31 +00:00
* If you like this project or if you find it useful, you can support us at:
*
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
*
2013-12-10 19:36:50 +00:00
* 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
*
*
*/
2020-05-17 21:15:50 +00:00
package net.sourceforge.plantuml.creole.legacy;
2013-12-10 19:36:50 +00:00
import java.util.ArrayList;
import java.util.Collections;
2021-11-30 18:33:37 +00:00
import java.util.HashMap;
2013-12-10 19:36:50 +00:00
import java.util.List;
2021-11-30 18:33:37 +00:00
import java.util.Map;
2021-05-09 21:14:40 +00:00
import java.util.Objects;
2013-12-10 19:36:50 +00:00
2017-06-05 11:27:21 +00:00
import net.sourceforge.plantuml.BackSlash;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.ISkinSimple;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.Url;
2020-05-17 21:15:50 +00:00
import net.sourceforge.plantuml.creole.CreoleContext;
import net.sourceforge.plantuml.creole.CreoleHorizontalLine;
import net.sourceforge.plantuml.creole.CreoleMode;
import net.sourceforge.plantuml.creole.Stripe;
import net.sourceforge.plantuml.creole.StripeStyle;
import net.sourceforge.plantuml.creole.StripeStyleType;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.creole.atom.Atom;
2021-12-07 18:15:43 +00:00
import net.sourceforge.plantuml.creole.atom.AtomEmoji;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.creole.atom.AtomImg;
import net.sourceforge.plantuml.creole.atom.AtomMath;
import net.sourceforge.plantuml.creole.atom.AtomOpenIcon;
import net.sourceforge.plantuml.creole.atom.AtomSpace;
import net.sourceforge.plantuml.creole.atom.AtomSprite;
import net.sourceforge.plantuml.creole.command.Command;
import net.sourceforge.plantuml.creole.command.CommandCreoleColorAndSizeChange;
import net.sourceforge.plantuml.creole.command.CommandCreoleColorChange;
2021-12-07 18:15:43 +00:00
import net.sourceforge.plantuml.creole.command.CommandCreoleEmoji;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.creole.command.CommandCreoleExposantChange;
import net.sourceforge.plantuml.creole.command.CommandCreoleFontFamilyChange;
import net.sourceforge.plantuml.creole.command.CommandCreoleImg;
import net.sourceforge.plantuml.creole.command.CommandCreoleLatex;
import net.sourceforge.plantuml.creole.command.CommandCreoleMath;
import net.sourceforge.plantuml.creole.command.CommandCreoleMonospaced;
import net.sourceforge.plantuml.creole.command.CommandCreoleOpenIcon;
import net.sourceforge.plantuml.creole.command.CommandCreoleQrcode;
import net.sourceforge.plantuml.creole.command.CommandCreoleSizeChange;
import net.sourceforge.plantuml.creole.command.CommandCreoleSpace;
import net.sourceforge.plantuml.creole.command.CommandCreoleSprite;
import net.sourceforge.plantuml.creole.command.CommandCreoleStyle;
import net.sourceforge.plantuml.creole.command.CommandCreoleSvgAttributeChange;
import net.sourceforge.plantuml.creole.command.CommandCreoleUrl;
2021-12-07 18:15:43 +00:00
import net.sourceforge.plantuml.emoji.Emoji;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.graphic.FontConfiguration;
import net.sourceforge.plantuml.graphic.FontPosition;
import net.sourceforge.plantuml.graphic.FontStyle;
2016-12-01 20:29:25 +00:00
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.graphic.ImgValign;
2016-12-14 21:01:03 +00:00
import net.sourceforge.plantuml.math.ScientificEquationSafe;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.openiconic.OpenIcon;
2020-05-30 15:20:23 +00:00
import net.sourceforge.plantuml.security.SecurityUtils;
2019-08-26 17:07:21 +00:00
import net.sourceforge.plantuml.sprite.Sprite;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColor;
2021-12-06 18:58:08 +00:00
import net.sourceforge.plantuml.ugraphic.color.NoSuchColorException;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.utils.CharHidder;
2013-12-10 19:36:50 +00:00
public class StripeSimple implements Stripe {
2019-01-16 18:34:41 +00:00
final private Atom header;
2021-05-14 08:42:57 +00:00
final private List<Atom> atoms = new ArrayList<>();
2021-11-30 18:33:37 +00:00
final private Map<Character, List<Command>> commands = new HashMap<>();
2016-12-01 20:29:25 +00:00
private HorizontalAlignment align = HorizontalAlignment.LEFT;
public void setCellAlignment(HorizontalAlignment align) {
this.align = align;
}
public HorizontalAlignment getCellAlignment() {
return align;
}
2013-12-10 19:36:50 +00:00
private FontConfiguration fontConfiguration;
final private StripeStyle style;
2015-04-07 18:18:37 +00:00
final private ISkinSimple skinParam;
2013-12-10 19:36:50 +00:00
2016-12-14 21:01:03 +00:00
@Override
public String toString() {
return super.toString() + " " + atoms.toString();
}
2020-05-17 21:15:50 +00:00
public Atom getLHeader() {
2019-01-16 18:34:41 +00:00
return header;
}
2013-12-10 19:36:50 +00:00
public StripeSimple(FontConfiguration fontConfiguration, StripeStyle style, CreoleContext context,
2015-07-11 09:32:49 +00:00
ISkinSimple skinParam, CreoleMode modeSimpleLine) {
2013-12-10 19:36:50 +00:00
this.fontConfiguration = fontConfiguration;
this.style = style;
this.skinParam = skinParam;
2021-11-30 18:33:37 +00:00
addCommand(CommandCreoleStyle.createCreole(FontStyle.BOLD));
addCommand(CommandCreoleStyle.createLegacy(FontStyle.BOLD));
addCommand(CommandCreoleStyle.createLegacyEol(FontStyle.BOLD));
2021-01-10 20:52:19 +00:00
2021-11-30 18:33:37 +00:00
addCommand(CommandCreoleStyle.createCreole(FontStyle.ITALIC));
addCommand(CommandCreoleStyle.createLegacy(FontStyle.ITALIC));
addCommand(CommandCreoleStyle.createLegacyEol(FontStyle.ITALIC));
addCommand(CommandCreoleStyle.createLegacy(FontStyle.PLAIN));
addCommand(CommandCreoleStyle.createLegacyEol(FontStyle.PLAIN));
2021-12-07 18:15:43 +00:00
if (modeSimpleLine == CreoleMode.FULL)
2021-11-30 18:33:37 +00:00
addCommand(CommandCreoleStyle.createCreole(FontStyle.UNDERLINE));
2021-12-07 18:15:43 +00:00
2021-11-30 18:33:37 +00:00
addCommand(CommandCreoleStyle.createLegacy(FontStyle.UNDERLINE));
addCommand(CommandCreoleStyle.createLegacyEol(FontStyle.UNDERLINE));
addCommand(CommandCreoleStyle.createCreole(FontStyle.STRIKE));
addCommand(CommandCreoleStyle.createLegacy(FontStyle.STRIKE));
addCommand(CommandCreoleStyle.createLegacyEol(FontStyle.STRIKE));
addCommand(CommandCreoleStyle.createCreole(FontStyle.WAVE));
addCommand(CommandCreoleStyle.createLegacy(FontStyle.WAVE));
addCommand(CommandCreoleStyle.createLegacyEol(FontStyle.WAVE));
addCommand(CommandCreoleStyle.createLegacy(FontStyle.BACKCOLOR));
addCommand(CommandCreoleStyle.createLegacyEol(FontStyle.BACKCOLOR));
addCommand(CommandCreoleSizeChange.create());
addCommand(CommandCreoleSizeChange.createEol());
addCommand(CommandCreoleColorChange.create());
addCommand(CommandCreoleColorChange.createEol());
addCommand(CommandCreoleColorAndSizeChange.create());
addCommand(CommandCreoleColorAndSizeChange.createEol());
addCommand(CommandCreoleExposantChange.create(FontPosition.EXPOSANT));
addCommand(CommandCreoleExposantChange.create(FontPosition.INDICE));
addCommand(CommandCreoleImg.create());
addCommand(CommandCreoleQrcode.create());
addCommand(CommandCreoleOpenIcon.create());
2021-12-07 18:15:43 +00:00
addCommand(CommandCreoleEmoji.create());
// ::comment when WASM
2021-11-30 18:33:37 +00:00
addCommand(CommandCreoleMath.create());
addCommand(CommandCreoleLatex.create());
// ::done
2021-11-30 18:33:37 +00:00
addCommand(CommandCreoleSprite.create());
addCommand(CommandCreoleSpace.create());
addCommand(CommandCreoleFontFamilyChange.create());
addCommand(CommandCreoleFontFamilyChange.createEol());
addCommand(CommandCreoleMonospaced.create());
addCommand(CommandCreoleUrl.create());
2021-12-07 18:15:43 +00:00
if (SecurityUtils.allowSvgText())
2021-11-30 18:33:37 +00:00
addCommand(CommandCreoleSvgAttributeChange.create());
2013-12-10 19:36:50 +00:00
2019-01-16 18:34:41 +00:00
this.header = style.getHeader(fontConfiguration, context);
2013-12-10 19:36:50 +00:00
2021-12-07 18:15:43 +00:00
if (this.header != null)
2019-01-16 18:34:41 +00:00
this.atoms.add(this.header);
2021-12-07 18:15:43 +00:00
2013-12-10 19:36:50 +00:00
}
2021-11-30 18:33:37 +00:00
private void addCommand(Command cmd) {
final String starters = cmd.startingChars();
for (int i = 0; i < starters.length(); i++) {
final char ch = starters.charAt(i);
List<Command> localList = commands.get(ch);
if (localList == null) {
localList = new ArrayList<Command>();
commands.put(ch, localList);
}
localList.add(cmd);
}
}
2013-12-10 19:36:50 +00:00
public List<Atom> getAtoms() {
2021-12-07 18:15:43 +00:00
if (atoms.size() == 0)
2020-11-21 17:33:24 +00:00
atoms.add(AtomTextUtils.createLegacy(" ", fontConfiguration));
2021-12-07 18:15:43 +00:00
2013-12-10 19:36:50 +00:00
return Collections.unmodifiableList(atoms);
}
public FontConfiguration getActualFontConfiguration() {
return fontConfiguration;
}
public void setActualFontConfiguration(FontConfiguration fontConfiguration) {
this.fontConfiguration = fontConfiguration;
}
public void analyzeAndAdd(String line) {
2021-12-07 18:15:43 +00:00
if (Objects.requireNonNull(line).contains("" + BackSlash.hiddenNewLine()))
2017-06-05 11:27:21 +00:00
throw new IllegalArgumentException(line);
2021-12-07 18:15:43 +00:00
2015-04-07 18:18:37 +00:00
line = CharHidder.hide(line);
2013-12-10 19:36:50 +00:00
if (style.getType() == StripeStyleType.HEADING) {
2019-09-22 17:20:16 +00:00
fontConfiguration = fontConfigurationForHeading(fontConfiguration, style.getOrder());
modifyStripe(line);
2013-12-10 19:36:50 +00:00
} else if (style.getType() == StripeStyleType.HORIZONTAL_LINE) {
atoms.add(CreoleHorizontalLine.create(fontConfiguration, line, style.getStyle(), skinParam));
} else {
modifyStripe(line);
}
}
2019-09-22 17:20:16 +00:00
private static FontConfiguration fontConfigurationForHeading(FontConfiguration fontConfiguration, int order) {
2021-12-07 18:15:43 +00:00
switch (order) {
case 0:
return fontConfiguration.bigger(4).bold();
case 1:
return fontConfiguration.bigger(2).bold();
case 2:
return fontConfiguration.bigger(1).bold();
default:
return fontConfiguration.italic();
2019-09-22 17:20:16 +00:00
}
}
2016-04-04 19:05:10 +00:00
public void addImage(String src, double scale) {
2019-06-26 19:24:49 +00:00
atoms.add(AtomImg.create(src, ImgValign.TOP, 0, scale, null));
2013-12-10 19:36:50 +00:00
}
2018-08-26 12:09:50 +00:00
public void addQrcode(String src, double scale) {
atoms.add(AtomImg.createQrcode(src, scale));
}
2015-04-07 18:18:37 +00:00
public void addSpace(int size) {
atoms.add(AtomSpace.create(size));
}
2013-12-10 19:36:50 +00:00
public void addUrl(Url url) {
2020-11-21 17:33:24 +00:00
atoms.add(AtomTextUtils.createUrl(url, fontConfiguration, skinParam));
2013-12-10 19:36:50 +00:00
}
2020-03-18 10:50:02 +00:00
public void addSprite(String src, double scale, HColor color) {
2013-12-10 19:36:50 +00:00
final Sprite sprite = skinParam.getSprite(src);
2021-12-02 17:39:20 +00:00
if (sprite != null)
2022-09-20 20:35:41 +00:00
atoms.add(new AtomSprite(color, scale, fontConfiguration, sprite, null));
2015-04-07 18:18:37 +00:00
}
2020-03-18 10:50:02 +00:00
public void addOpenIcon(String src, double scale, HColor color) {
2015-04-07 18:18:37 +00:00
final OpenIcon openIcon = OpenIcon.retrieve(src);
2021-12-02 17:39:20 +00:00
if (openIcon != null)
2019-01-16 18:34:41 +00:00
atoms.add(new AtomOpenIcon(color, scale, openIcon, fontConfiguration, null));
2021-12-02 17:39:20 +00:00
}
2021-12-07 18:15:43 +00:00
public void addEmoji(String emojiName, String forcedColor) {
final Emoji emoji = Emoji.retrieve(emojiName);
if (emoji == null)
return;
2021-12-06 18:58:08 +00:00
2021-12-07 18:15:43 +00:00
HColor col = null;
if (forcedColor == null)
col = null;
else if (forcedColor.equals("#0") || forcedColor.equals("#000") || forcedColor.equals("#black"))
col = fontConfiguration.getColor();
else
try {
2022-09-18 17:08:06 +00:00
col = skinParam.getIHtmlColorSet().getColor(forcedColor);
2021-12-07 18:15:43 +00:00
} catch (NoSuchColorException e) {
col = null;
}
2021-12-02 17:39:20 +00:00
2021-12-07 18:15:43 +00:00
atoms.add(new AtomEmoji(emoji, 1, fontConfiguration.getSize2D(), col));
2013-12-10 19:36:50 +00:00
}
// ::comment when WASM
2020-06-07 10:03:18 +00:00
public void addMath(ScientificEquationSafe math) {
2022-09-20 20:35:41 +00:00
atoms.add(new AtomMath(math, fontConfiguration.getColor(), fontConfiguration.getExtendedColor()));
2016-12-01 20:29:25 +00:00
}
// ::done
2016-12-01 20:29:25 +00:00
2013-12-10 19:36:50 +00:00
private void modifyStripe(String line) {
final StringBuilder pending = new StringBuilder();
while (line.length() > 0) {
final Command cmd = searchCommand(line);
if (cmd == null) {
pending.append(line.charAt(0));
line = line.substring(1);
} else {
addPending(pending);
line = cmd.executeAndGetRemaining(line, this);
}
}
addPending(pending);
}
private void addPending(StringBuilder pending) {
2021-12-07 18:15:43 +00:00
if (pending.length() == 0)
2013-12-10 19:36:50 +00:00
return;
2021-12-07 18:15:43 +00:00
2020-11-21 17:33:24 +00:00
atoms.add(AtomTextUtils.createLegacy(pending.toString(), fontConfiguration));
2013-12-10 19:36:50 +00:00
pending.setLength(0);
}
private Command searchCommand(String line) {
2021-11-30 18:33:37 +00:00
final List<Command> localList = commands.get(line.charAt(0));
if (localList != null)
2021-12-07 18:15:43 +00:00
for (Command cmd : localList)
if (cmd.matchingSize(line) != 0)
2021-11-30 18:33:37 +00:00
return cmd;
2021-12-07 18:15:43 +00:00
2013-12-10 19:36:50 +00:00
return null;
}
2021-11-30 18:33:37 +00:00
public ISkinSimple getSkinParam() {
return skinParam;
}
2013-12-10 19:36:50 +00:00
}