1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-05-28 22:20:49 +00:00
plantuml/src/net/sourceforge/plantuml/creole/StripeSimple.java

259 lines
9.0 KiB
Java
Raw Normal View History

2013-12-10 19:36:50 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2019-01-16 18:34:41 +00:00
* (C) Copyright 2009-2020, 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
*
*
*/
package net.sourceforge.plantuml.creole;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
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;
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;
2019-01-16 18:34:41 +00:00
import net.sourceforge.plantuml.graphic.HtmlColor;
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;
2019-08-26 17:07:21 +00:00
import net.sourceforge.plantuml.sprite.Sprite;
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;
2013-12-10 19:36:50 +00:00
final private List<Atom> atoms = new ArrayList<Atom>();
2016-01-30 12:20:07 +00:00
final private List<Command> commands = new ArrayList<Command>();
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();
}
2019-01-16 18:34:41 +00:00
public Atom getHeader() {
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;
// class Splitter
this.commands.add(CommandCreoleStyle.createCreole(FontStyle.BOLD));
this.commands.add(CommandCreoleStyle.createLegacy(FontStyle.BOLD));
this.commands.add(CommandCreoleStyle.createLegacyEol(FontStyle.BOLD));
this.commands.add(CommandCreoleStyle.createCreole(FontStyle.ITALIC));
this.commands.add(CommandCreoleStyle.createLegacy(FontStyle.ITALIC));
this.commands.add(CommandCreoleStyle.createLegacyEol(FontStyle.ITALIC));
2016-12-14 21:01:03 +00:00
this.commands.add(CommandCreoleStyle.createLegacy(FontStyle.PLAIN));
this.commands.add(CommandCreoleStyle.createLegacyEol(FontStyle.PLAIN));
2015-07-11 09:32:49 +00:00
if (modeSimpleLine == CreoleMode.FULL) {
2015-04-07 18:18:37 +00:00
this.commands.add(CommandCreoleStyle.createCreole(FontStyle.UNDERLINE));
}
2013-12-10 19:36:50 +00:00
this.commands.add(CommandCreoleStyle.createLegacy(FontStyle.UNDERLINE));
this.commands.add(CommandCreoleStyle.createLegacyEol(FontStyle.UNDERLINE));
this.commands.add(CommandCreoleStyle.createCreole(FontStyle.STRIKE));
this.commands.add(CommandCreoleStyle.createLegacy(FontStyle.STRIKE));
this.commands.add(CommandCreoleStyle.createLegacyEol(FontStyle.STRIKE));
this.commands.add(CommandCreoleStyle.createCreole(FontStyle.WAVE));
this.commands.add(CommandCreoleStyle.createLegacy(FontStyle.WAVE));
this.commands.add(CommandCreoleStyle.createLegacyEol(FontStyle.WAVE));
this.commands.add(CommandCreoleStyle.createLegacy(FontStyle.BACKCOLOR));
this.commands.add(CommandCreoleStyle.createLegacyEol(FontStyle.BACKCOLOR));
this.commands.add(CommandCreoleSizeChange.create());
this.commands.add(CommandCreoleSizeChange.createEol());
this.commands.add(CommandCreoleColorChange.create());
2015-04-07 18:18:37 +00:00
this.commands.add(CommandCreoleColorChange.createEol());
2013-12-10 19:36:50 +00:00
this.commands.add(CommandCreoleColorAndSizeChange.create());
this.commands.add(CommandCreoleColorAndSizeChange.createEol());
this.commands.add(CommandCreoleExposantChange.create(FontPosition.EXPOSANT));
this.commands.add(CommandCreoleExposantChange.create(FontPosition.INDICE));
this.commands.add(CommandCreoleImg.create());
2018-08-26 12:09:50 +00:00
this.commands.add(CommandCreoleQrcode.create());
2019-01-16 18:34:41 +00:00
this.commands.add(CommandCreoleOpenIcon.create(skinParam.getIHtmlColorSet()));
2016-12-14 21:01:03 +00:00
final double scale = skinParam.getDpi() / 96.0;
this.commands.add(CommandCreoleMath.create(scale));
this.commands.add(CommandCreoleLatex.create(scale));
2019-01-16 18:34:41 +00:00
this.commands.add(CommandCreoleSprite.create(skinParam.getIHtmlColorSet()));
2015-04-07 18:18:37 +00:00
this.commands.add(CommandCreoleSpace.create());
2013-12-10 19:36:50 +00:00
this.commands.add(CommandCreoleFontFamilyChange.create());
this.commands.add(CommandCreoleFontFamilyChange.createEol());
2015-09-06 17:28:59 +00:00
this.commands.add(CommandCreoleMonospaced.create(skinParam.getMonospacedFamily()));
2013-12-10 19:36:50 +00:00
this.commands.add(CommandCreoleUrl.create(skinParam));
2015-04-07 18:18:37 +00:00
this.commands.add(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
2019-01-16 18:34:41 +00:00
if (this.header != null) {
this.atoms.add(this.header);
2013-12-10 19:36:50 +00:00
}
}
public List<Atom> getAtoms() {
2015-04-07 18:18:37 +00:00
if (atoms.size() == 0) {
atoms.add(AtomText.create(" ", fontConfiguration));
}
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) {
if (line == null) {
throw new IllegalArgumentException();
}
2017-06-05 11:27:21 +00:00
if (line.contains("" + BackSlash.hiddenNewLine())) {
throw new IllegalArgumentException(line);
}
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) {
if (order == 0) {
fontConfiguration = fontConfiguration.bigger(4).bold();
} else if (order == 1) {
fontConfiguration = fontConfiguration.bigger(2).bold();
} else if (order == 2) {
fontConfiguration = fontConfiguration.bigger(1).bold();
} else {
fontConfiguration = fontConfiguration.italic();
}
return fontConfiguration;
}
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) {
2018-10-21 19:44:14 +00:00
atoms.add(AtomText.createUrl(url, fontConfiguration, skinParam));
2013-12-10 19:36:50 +00:00
}
2019-01-16 18:34:41 +00:00
public void addSprite(String src, double scale, HtmlColor color) {
2013-12-10 19:36:50 +00:00
final Sprite sprite = skinParam.getSprite(src);
if (sprite != null) {
2019-01-16 18:34:41 +00:00
atoms.add(new AtomSprite(color, scale, fontConfiguration, sprite, null));
2015-04-07 18:18:37 +00:00
}
}
2019-01-16 18:34:41 +00:00
public void addOpenIcon(String src, double scale, HtmlColor color) {
2015-04-07 18:18:37 +00:00
final OpenIcon openIcon = OpenIcon.retrieve(src);
if (openIcon != null) {
2019-01-16 18:34:41 +00:00
atoms.add(new AtomOpenIcon(color, scale, openIcon, fontConfiguration, null));
2013-12-10 19:36:50 +00:00
}
}
2016-12-14 21:01:03 +00:00
public void addMath(ScientificEquationSafe math, double scale) {
2018-09-23 12:15:14 +00:00
atoms.add(new AtomMath(math, fontConfiguration.getColor(), fontConfiguration.getExtendedColor(), scale,
skinParam.getColorMapper()));
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) {
if (pending.length() == 0) {
return;
}
atoms.add(AtomText.create(pending.toString(), fontConfiguration));
pending.setLength(0);
}
private Command searchCommand(String line) {
for (Command cmd : commands) {
final int i = cmd.matchingSize(line);
if (i != 0) {
return cmd;
}
}
return null;
}
}