1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-11-26 06:46:45 +00:00

Decouple StringBounderDebug from any OS

This commit is contained in:
Arnaud Roques 2021-03-22 14:20:24 +01:00
parent 1a5bbd36ae
commit 2efa343e38
23 changed files with 88 additions and 102 deletions

View File

@ -177,7 +177,7 @@ public enum FileFormat {
}
static private Dimension2DDouble getJavaDimension(UFont font, String text) {
final Font javaFont = font.getFont();
final Font javaFont = font.getUnderlayingFont();
final FontMetrics fm = gg.getFontMetrics(javaFont);
final Rectangle2D rect = fm.getStringBounds(text, gg);
return new Dimension2DDouble(rect.getWidth(), rect.getHeight());

View File

@ -541,5 +541,15 @@ public class StringUtils {
return s.replace("\\t", "\t");
}
public static long seed(String string) {
long h = 1125899906842597L; // prime
final int len = string.length();
for (int i = 0; i < len; i++) {
h = 31 * h + string.charAt(i);
}
return h;
}
// http://docs.oracle.com/javase/tutorial/i18n/format/dateFormat.html
}

View File

@ -173,14 +173,7 @@ final public class UmlSource {
}
public long seed() {
long h = 1125899906842597L; // prime
final String string = getPlainString();
final int len = string.length();
for (int i = 0; i < len; i++) {
h = 31 * h + string.charAt(i);
}
return h;
return StringUtils.seed(getPlainString());
}
public String getLine(LineLocation n) {

View File

@ -35,7 +35,6 @@
*/
package net.sourceforge.plantuml.creole.legacy;
import java.awt.font.LineMetrics;
import java.awt.geom.Dimension2D;
import java.util.ArrayList;
import java.util.Arrays;
@ -55,7 +54,6 @@ import net.sourceforge.plantuml.creole.atom.Atom;
import net.sourceforge.plantuml.graphic.FontConfiguration;
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.graphic.TextBlock;
import net.sourceforge.plantuml.graphic.TextBlockUtils;
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.UText;
import net.sourceforge.plantuml.ugraphic.UTranslate;
@ -158,7 +156,7 @@ public final class AtomText extends AbstractAtom implements Atom {
// final int ypos = fontConfiguration.getSpace();
final Dimension2D rect = ug.getStringBounder().calculateDimension(fontConfiguration.getFont(), text);
final double descent = getDescent();
final double descent = getDescent(ug.getStringBounder());
final double ypos = rect.getHeight() - descent;
double x = 0;
@ -209,9 +207,8 @@ public final class AtomText extends AbstractAtom implements Atom {
return " ";
}
private double getDescent() {
final LineMetrics fm = TextBlockUtils.getLineMetrics(fontConfiguration.getFont(), text);
return fm.getDescent();
private double getDescent(StringBounder stringBounder) {
return stringBounder.getDescent(fontConfiguration.getFont(), text);
}
private double getTabSize(StringBounder stringBounder) {

View File

@ -103,9 +103,10 @@ public class StripeCode implements Stripe, Atom {
double y = 0;
for (String s : raw) {
final UText shape = new UText(s, fontConfiguration);
final Dimension2D dim = ug.getStringBounder().calculateDimension(fontConfiguration.getFont(), s);
final StringBounder stringBounder = ug.getStringBounder();
final Dimension2D dim = stringBounder.calculateDimension(fontConfiguration.getFont(), s);
y += dim.getHeight();
ug.apply(UTranslate.dy(y - shape.getDescent())).draw(shape);
ug.apply(UTranslate.dy(y - shape.getDescent(stringBounder))).draw(shape);
}
}

View File

@ -43,4 +43,6 @@ public interface StringBounder {
public Dimension2D calculateDimension(UFont font, String text);
public double getDescent(UFont font, String text);
}

View File

@ -35,9 +35,12 @@
*/
package net.sourceforge.plantuml.graphic;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.geom.Dimension2D;
import net.sourceforge.plantuml.Dimension2DDouble;
import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.text.RichText;
import net.sourceforge.plantuml.text.StyledString;
import net.sourceforge.plantuml.ugraphic.UFont;
@ -61,4 +64,11 @@ public abstract class StringBounderRaw implements StringBounder {
protected abstract Dimension2D calculateDimensionInternal(UFont font, String text);
public double getDescent(UFont font, String text) {
final FontRenderContext frc = FileFormat.gg.getFontRenderContext();
final LineMetrics lineMetrics = font.getUnderlayingFont().getLineMetrics(text, frc);
final double descent = lineMetrics.getDescent();
return descent;
}
}

View File

@ -202,14 +202,6 @@ public class TextBlockUtils {
return FileFormat.gg.getFontRenderContext();
}
public static LineMetrics getLineMetrics(UFont font, String text) {
return font.getLineMetrics(FileFormat.gg, text);
}
public static FontMetrics getFontMetrics(Font font) {
return FileFormat.gg.getFontMetrics(font);
}
public static TextBlock fullInnerPosition(final TextBlock bloc, final String display) {
return new TextBlock() {
@ -233,8 +225,6 @@ public class TextBlockUtils {
return null;
}
};
}

View File

@ -85,7 +85,7 @@ public class UnusedSpace {
private UnusedSpace(UFont font, char c) {
final BufferedImage im = new BufferedImage(2 * HALF_SIZE, 2 * HALF_SIZE, BufferedImage.TYPE_INT_RGB);
final Graphics2D g2d = im.createGraphics();
g2d.setFont(font.getFont());
g2d.setFont(font.getUnderlayingFont());
g2d.drawString("" + c, HALF_SIZE, HALF_SIZE);
int minI = Integer.MAX_VALUE;

View File

@ -105,7 +105,7 @@ public class FontChecker {
}
public String getCharDesc(char c) {
final TextLayout t = new TextLayout("" + c, font.getFont(), TextBlockUtils.getFontRenderContext());
final TextLayout t = new TextLayout("" + c, font.getUnderlayingFont(), TextBlockUtils.getFontRenderContext());
final Shape sh = t.getOutline(null);
final double current[] = new double[6];
final PathIterator it = sh.getPathIterator(null);
@ -123,7 +123,7 @@ public class FontChecker {
}
public String getCharDescVerbose(char c) {
final TextLayout t = new TextLayout("" + c, font.getFont(), TextBlockUtils.getFontRenderContext());
final TextLayout t = new TextLayout("" + c, font.getUnderlayingFont(), TextBlockUtils.getFontRenderContext());
final Shape sh = t.getOutline(null);
final double current[] = new double[6];
final PathIterator it = sh.getPathIterator(null);

View File

@ -36,18 +36,11 @@
package net.sourceforge.plantuml.ugraphic;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.util.HashSet;
import java.util.Set;
import net.sourceforge.plantuml.StringUtils;
import net.sourceforge.plantuml.graphic.FontConfiguration;
import net.sourceforge.plantuml.graphic.TextBlockUtils;
import net.sourceforge.plantuml.ugraphic.color.HColor;
public class UFont {
@ -119,16 +112,10 @@ public class UFont {
this.family = family;
}
public final Font getFont() {
public final Font getUnderlayingFont() {
return font;
}
@Deprecated
public FontConfiguration toFont2(HColor color, boolean useUnderlineForHyperlink, HColor hyperlinkColor,
int tabSize) {
return new FontConfiguration(this, color, hyperlinkColor, useUnderlineForHyperlink, tabSize);
}
public UFont scaled(double scale) {
if (scale == 1) {
return this;
@ -207,13 +194,4 @@ public class UFont {
return this.font.equals(((UFont) obj).font);
}
public LineMetrics getLineMetrics(Graphics2D gg, String text) {
final FontRenderContext frc = gg.getFontRenderContext();
return font.getLineMetrics(text, frc);
}
public FontMetrics getFontMetrics() {
return TextBlockUtils.getFontMetrics(getFont());
}
}

View File

@ -35,10 +35,8 @@
*/
package net.sourceforge.plantuml.ugraphic;
import java.awt.font.LineMetrics;
import net.sourceforge.plantuml.graphic.FontConfiguration;
import net.sourceforge.plantuml.graphic.TextBlockUtils;
import net.sourceforge.plantuml.graphic.StringBounder;
public class UText implements UShape {
@ -74,10 +72,8 @@ public class UText implements UShape {
return font;
}
public double getDescent() {
final LineMetrics fm = TextBlockUtils.getLineMetrics(font.getFont(), text);
final double descent = fm.getDescent();
return descent;
public double getDescent(StringBounder stringBounder) {
return stringBounder.getDescent(font.getFont(), text);
}
public final int getOrientation() {

View File

@ -35,8 +35,10 @@
package net.sourceforge.plantuml.ugraphic.debug;
import java.awt.geom.Dimension2D;
import java.util.Random;
import net.sourceforge.plantuml.Dimension2DDouble;
import net.sourceforge.plantuml.StringUtils;
import net.sourceforge.plantuml.graphic.StringBounderRaw;
import net.sourceforge.plantuml.ugraphic.UFont;
@ -44,10 +46,19 @@ public class StringBounderDebug extends StringBounderRaw {
@Override
protected Dimension2D calculateDimensionInternal(UFont font, String text) {
final Random rnd = new Random(StringUtils.seed(text));
// We want a random factor between 80% et 130%
final double factor = 0.8 + 0.5 * rnd.nextDouble();
final double size = font.getSize2D();
final double height = size;
final double width = size * text.length();
final double width = size * text.length() * factor;
return new Dimension2DDouble(width, height);
}
@Override
public double getDescent(UFont font, String text) {
final double descent = font.getSize2D() / 4.5;
return descent;
}
}

View File

@ -57,7 +57,7 @@ public class DriverCenteredCharacterEps implements UDriver<EpsGraphics> {
final double xpos = x - unusedSpace.getCenterX() - 0.5;
final double ypos = y - unusedSpace.getCenterY() - 0.5;
final TextLayout t = new TextLayout("" + c, font.getFont(), TextBlockUtils.getFontRenderContext());
final TextLayout t = new TextLayout("" + c, font.getUnderlayingFont(), TextBlockUtils.getFontRenderContext());
eps.setStrokeColor(mapper.toColor(param.getColor()));
DriverTextEps.drawPathIterator(eps, xpos, ypos, t.getOutline(null));

View File

@ -93,7 +93,7 @@ public class DriverTextEps implements UDriver<EpsGraphics> {
final FontConfiguration fontConfiguration = shape.getFontConfiguration();
final UFont font = fontConfiguration.getFont();
final TextLayout textLayout = new TextLayout(shape.getText(), font.getFont(), fontRenderContext);
final TextLayout textLayout = new TextLayout(shape.getText(), font.getUnderlayingFont(), fontRenderContext);
// System.err.println("text=" + shape.getText());
MinMax dim = null;

View File

@ -57,7 +57,7 @@ public class DriverCenteredCharacterG2d implements UDriver<Graphics2D> {
final double xpos = x - unusedSpace.getCenterX();
final double ypos = y - unusedSpace.getCenterY() - 0.5;
g2d.setFont(font.getFont());
g2d.setFont(font.getUnderlayingFont());
g2d.drawString("" + c, (float) xpos, (float) ypos);
}

View File

@ -98,9 +98,9 @@ public class DriverTextAsPathG2d implements UDriver<Graphics2D> {
visible.ensureVisible(x, y - dimBack.getHeight() + 1.5);
visible.ensureVisible(x + dimBack.getWidth(), y + 1.5);
g2d.setFont(font.getFont());
g2d.setFont(font.getUnderlayingFont());
g2d.setColor(mapper.toColor(fontConfiguration.getColor()));
final TextLayout t = new TextLayout(shape.getText(), font.getFont(), fontRenderContext);
final TextLayout t = new TextLayout(shape.getText(), font.getUnderlayingFont(), fontRenderContext);
g2d.translate(x, y);
g2d.fill(t.getOutline(null));
g2d.translate(-x, -y);
@ -130,7 +130,7 @@ public class DriverTextAsPathG2d implements UDriver<Graphics2D> {
}
if (fontConfiguration.containsStyle(FontStyle.STRIKE)) {
final Dimension2D dim = calculateDimension(FileFormat.PNG.getDefaultStringBounder(TikzFontDistortion.getDefault()), font, shape.getText());
final FontMetrics fm = g2d.getFontMetrics(font.getFont());
final FontMetrics fm = g2d.getFontMetrics(font.getUnderlayingFont());
final int ypos = (int) (y - fm.getDescent() - 0.5);
final HColor extended = fontConfiguration.getExtendedColor();
if (extended != null) {

View File

@ -100,7 +100,7 @@ public class DriverTextG2d implements UDriver<Graphics2D> {
if (orientation == 90) {
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setFont(font.getFont());
g2d.setFont(font.getUnderlayingFont());
g2d.setColor(mapper.toColor(fontConfiguration.getColor()));
final AffineTransform orig = g2d.getTransform();
g2d.translate(x, y);
@ -133,7 +133,7 @@ public class DriverTextG2d implements UDriver<Graphics2D> {
visible.ensureVisible(x + dimBack.getWidth(), y + 1.5);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setFont(font.getFont());
g2d.setFont(font.getUnderlayingFont());
g2d.setColor(mapper.toColor(fontConfiguration.getColor()));
g2d.drawString(text, (float) x, (float) y);
@ -163,7 +163,7 @@ public class DriverTextG2d implements UDriver<Graphics2D> {
if (fontConfiguration.containsStyle(FontStyle.STRIKE)) {
final Dimension2D dim = calculateDimension(
FileFormat.PNG.getDefaultStringBounder(TikzFontDistortion.getDefault()), font, text);
final FontMetrics fm = g2d.getFontMetrics(font.getFont());
final FontMetrics fm = g2d.getFontMetrics(font.getUnderlayingFont());
final int ypos = (int) (y - fm.getDescent() - 0.5);
if (extended != null) {
g2d.setColor(mapper.toColor(extended));

View File

@ -57,7 +57,7 @@ public class DriverCenteredCharacterSvg implements UDriver<SvgGraphics> {
final double xpos = x - unusedSpace.getCenterX() - 0.5;
final double ypos = y - unusedSpace.getCenterY() - 0.5;
final TextLayout t = new TextLayout("" + c, font.getFont(), TextBlockUtils.getFontRenderContext());
final TextLayout t = new TextLayout("" + c, font.getUnderlayingFont(), TextBlockUtils.getFontRenderContext());
svg.setFillColor(mapper.toRGB(param.getColor()));
svg.drawPathIterator(xpos, ypos, t.getOutline(null).getPathIterator(null));

View File

@ -69,7 +69,7 @@ public class DriverTextAsPathSvg implements UDriver<SvgGraphics> {
final FontConfiguration fontConfiguration = shape.getFontConfiguration();
final UFont font = fontConfiguration.getFont();
final TextLayout t = new TextLayout(shape.getText(), font.getFont(), fontRenderContext);
final TextLayout t = new TextLayout(shape.getText(), font.getUnderlayingFont(), fontRenderContext);
svg.drawPathIterator(x, y, t.getOutline(null).getPathIterator(null));
}

View File

@ -57,7 +57,7 @@ public class DriverCenteredCharacterTikz implements UDriver<TikzGraphics> {
final double xpos = x - unusedSpace.getCenterX() - 0.5;
final double ypos = y - unusedSpace.getCenterY() - 0.5;
final TextLayout t = new TextLayout("" + c, font.getFont(), TextBlockUtils.getFontRenderContext());
final TextLayout t = new TextLayout("" + c, font.getUnderlayingFont(), TextBlockUtils.getFontRenderContext());
tikz.setStrokeColor(mapper.toColor(param.getColor()));
tikz.drawPathIterator(xpos, ypos, t.getOutline(null).getPathIterator(null));

View File

@ -82,7 +82,7 @@ public class UGraphicTxt extends AbstractCommonUGraphic implements ClipContainer
// final UClip clip = getClip();
if (shape instanceof UText) {
final UText txt = (UText) shape;
final int y = ((int) (getTranslateY() + txt.getDescent())) / 10;
final int y = ((int) (getTranslateY() + txt.getDescent(getStringBounder()))) / 10;
if (txt.getFontConfiguration().containsStyle(FontStyle.WAVE)) {
charArea.drawHLine('^', y, getDx(), txt.getText().length());
charArea.drawStringLR(txt.getText(), getDx(), y + 1);

View File

@ -80,7 +80,7 @@ class SimpleSequenceDiagramTest {
final double value1 = Double.parseDouble(double1);
final double value2 = Double.parseDouble(double2);
final double diff = Math.abs(value1 - value2);
final boolean result = diff < 0.005;
final boolean result = diff < 0.001;
if (result == false) {
System.err.println("sameDouble:Non null diff=" + diff);
}
@ -95,26 +95,25 @@ class SimpleSequenceDiagramTest {
}
private String getExpectedResult() {
return packString( //
"DPI: 96", //
return packString("DPI: 96", //
"", //
"LINE:", //
" pt1: [ 49.0000 ; 38.0000 ]", //
" pt2: [ 49.0000 ; 85.0000 ]", //
" pt1: [ 50.0000 ; 38.0000 ]", //
" pt2: [ 50.0000 ; 85.0000 ]", //
" stroke: 5.0-5.0-1.0", //
" shadow: 0", //
" color: ffa80036", //
"", //
"LINE:", //
" pt1: [ 138.0000 ; 38.0000 ]", //
" pt2: [ 138.0000 ; 85.0000 ]", //
" pt1: [ 156.8135 ; 38.0000 ]", //
" pt2: [ 156.8135 ; 85.0000 ]", //
" stroke: 5.0-5.0-1.0", //
" shadow: 0", //
" color: ffa80036", //
"", //
"RECTANGLE:", //
" pt1: [ 5.0000 ; 5.0000 ]", //
" pt2: [ 89.0000 ; 33.0000 ]", //
" pt2: [ 92.9573 ; 33.0000 ]", //
" xCorner: 0", //
" yCorner: 0", //
" stroke: 0.0-0.0-1.5", //
@ -124,14 +123,14 @@ class SimpleSequenceDiagramTest {
"", //
"TEXT:", //
" text: Alice", //
" position: [ 12.0000 ; 22.9238 ]", //
" position: [ 12.0000 ; 22.8889 ]", //
" orientation: 0", //
" font: SansSerif.plain/14 []", //
" color: ffa80036", //
"", //
"RECTANGLE:", //
" pt1: [ 5.0000 ; 84.0000 ]", //
" pt2: [ 89.0000 ; 112.0000 ]", //
" pt2: [ 92.9573 ; 112.0000 ]", //
" xCorner: 0", //
" yCorner: 0", //
" stroke: 0.0-0.0-1.5", //
@ -141,14 +140,14 @@ class SimpleSequenceDiagramTest {
"", //
"TEXT:", //
" text: Alice", //
" position: [ 12.0000 ; 101.9238 ]", //
" position: [ 12.0000 ; 101.8889 ]", //
" orientation: 0", //
" font: SansSerif.plain/14 []", //
" color: ffa80036", //
"", //
"RECTANGLE:", //
" pt1: [ 108.0000 ; 5.0000 ]", //
" pt2: [ 164.0000 ; 33.0000 ]", //
" pt1: [ 130.8135 ; 5.0000 ]", //
" pt2: [ 180.5185 ; 33.0000 ]", //
" xCorner: 0", //
" yCorner: 0", //
" stroke: 0.0-0.0-1.5", //
@ -158,14 +157,14 @@ class SimpleSequenceDiagramTest {
"", //
"TEXT:", //
" text: Bob", //
" position: [ 115.0000 ; 22.9238 ]", //
" position: [ 137.8135 ; 22.8889 ]", //
" orientation: 0", //
" font: SansSerif.plain/14 []", //
" color: ffa80036", //
"", //
"RECTANGLE:", //
" pt1: [ 108.0000 ; 84.0000 ]", //
" pt2: [ 164.0000 ; 112.0000 ]", //
" pt1: [ 130.8135 ; 84.0000 ]", //
" pt2: [ 180.5185 ; 112.0000 ]", //
" xCorner: 0", //
" yCorner: 0", //
" stroke: 0.0-0.0-1.5", //
@ -175,36 +174,35 @@ class SimpleSequenceDiagramTest {
"", //
"TEXT:", //
" text: Bob", //
" position: [ 115.0000 ; 101.9238 ]", //
" position: [ 137.8135 ; 101.8889 ]", //
" orientation: 0", //
" font: SansSerif.plain/14 []", //
" color: ffa80036", //
"", //
"POLYGON:", //
" points:", //
" - [ 126.0000 ; 63.0000 ]", //
" - [ 136.0000 ; 67.0000 ]", //
" - [ 126.0000 ; 71.0000 ]", //
" - [ 130.0000 ; 67.0000 ]", //
" - [ 145.6660 ; 63.0000 ]", //
" - [ 155.6660 ; 67.0000 ]", //
" - [ 145.6660 ; 71.0000 ]", //
" - [ 149.6660 ; 67.0000 ]", //
" stroke: 0.0-0.0-1.0", //
" shadow: 0", //
" color: ffa80036", //
" backcolor: ffa80036", //
"", //
"LINE:", //
" pt1: [ 49.0000 ; 67.0000 ]", //
" pt2: [ 132.0000 ; 67.0000 ]", //
" pt1: [ 50.9786 ; 67.0000 ]", //
" pt2: [ 151.6660 ; 67.0000 ]", //
" stroke: 0.0-0.0-1.0", //
" shadow: 0", //
" color: ffa80036", //
"", //
"TEXT:", //
" text: Hello", //
" position: [ 56.0000 ; 62.1436 ]", //
" position: [ 57.9786 ; 62.1111 ]", //
" orientation: 0", //
" font: SansSerif.plain/13 []", //
" color: ffa80036" //
);
}