1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-11-15 17:57:10 +00:00

Force Font Size if sepcified instead of Deriving it

This commit is contained in:
Christoph Rüthing 2022-02-28 14:38:18 +01:00
parent d8cbe985ef
commit 10931ae95c
5 changed files with 67 additions and 28 deletions

View File

@ -65,11 +65,12 @@ public class PSystemDitaa extends AbstractPSystem {
private final float scale; private final float scale;
private final boolean transparentBackground; private final boolean transparentBackground;
private final Font font; private final Font font;
private final boolean forceFontSize;
private final boolean performSeparationOfCommonEdges; private final boolean performSeparationOfCommonEdges;
private final boolean allCornersAreRound; private final boolean allCornersAreRound;
public PSystemDitaa(UmlSource source, String data, boolean performSeparationOfCommonEdges, boolean dropShadows, public PSystemDitaa(UmlSource source, String data, boolean performSeparationOfCommonEdges, boolean dropShadows,
boolean allCornersAreRound, boolean transparentBackground, float scale, Font font) { boolean allCornersAreRound, boolean transparentBackground, float scale, Font font, boolean forceFontSize) {
super(source); super(source);
this.data = data; this.data = data;
this.dropShadows = dropShadows; this.dropShadows = dropShadows;
@ -90,11 +91,12 @@ public class PSystemDitaa extends AbstractPSystem {
this.transparentBackground = transparentBackground; this.transparentBackground = transparentBackground;
this.scale = scale; this.scale = scale;
this.font = font; this.font = font;
this.forceFontSize = forceFontSize;
} }
PSystemDitaa add(String line) { PSystemDitaa add(String line) {
return new PSystemDitaa(getSource(), data + line + BackSlash.NEWLINE, performSeparationOfCommonEdges, return new PSystemDitaa(getSource(), data + line + BackSlash.NEWLINE, performSeparationOfCommonEdges,
dropShadows, allCornersAreRound, transparentBackground, scale, font); dropShadows, allCornersAreRound, transparentBackground, scale, font, forceFontSize);
} }
public DiagramDescription getDescription() { public DiagramDescription getDescription() {
@ -127,6 +129,10 @@ public class PSystemDitaa extends AbstractPSystem {
final Method setFont = renderingOptions.getClass().getMethod("setFont", Font.class); final Method setFont = renderingOptions.getClass().getMethod("setFont", Font.class);
setFont.invoke(renderingOptions, font); setFont.invoke(renderingOptions, font);
// renderingOptions.setForceFontSize(font);
final Method setForceFontSize = renderingOptions.getClass().getMethod("setForceFontSize", boolean.class);
setForceFontSize.invoke(renderingOptions, forceFontSize);
// renderingOptions.setScale(scale); // renderingOptions.setScale(scale);
final Method setScale = renderingOptions.getClass().getMethod("setScale", float.class); final Method setScale = renderingOptions.getClass().getMethod("setScale", float.class);
setScale.invoke(renderingOptions, scale); setScale.invoke(renderingOptions, scale);

View File

@ -74,12 +74,16 @@ public class PSystemDitaaFactory extends PSystemBasicFactory<PSystemDitaa> {
if (startLine != null && (startLine.contains("-T") || startLine.contains("--transparent"))) if (startLine != null && (startLine.contains("-T") || startLine.contains("--transparent")))
transparentBackground = true; transparentBackground = true;
boolean forceFontSize = false;
if (startLine != null && startLine.contains("--font-size"))
forceFontSize = true;
final float scale = extractScale(startLine); final float scale = extractScale(startLine);
final Font font = extractFont(startLine); final Font font = extractFont(startLine);
if (getDiagramType() == DiagramType.UML) if (getDiagramType() == DiagramType.UML)
return null; return null;
else if (getDiagramType() == DiagramType.DITAA) else if (getDiagramType() == DiagramType.DITAA)
return new PSystemDitaa(source, "", performSeparationOfCommonEdges, dropShadows, allCornersAreRound, transparentBackground, scale, font); return new PSystemDitaa(source, "", performSeparationOfCommonEdges, dropShadows, allCornersAreRound, transparentBackground, scale, font, forceFontSize);
else else
throw new IllegalStateException(getDiagramType().name()); throw new IllegalStateException(getDiagramType().name());
@ -104,9 +108,13 @@ public class PSystemDitaaFactory extends PSystemBasicFactory<PSystemDitaa> {
if (line.contains("-T") || line.contains("--transparent")) if (line.contains("-T") || line.contains("--transparent"))
transparentBackground = true; transparentBackground = true;
boolean forceFontSize = false;
if (line.contains("--font-size"))
forceFontSize = true;
final float scale = extractScale(line); final float scale = extractScale(line);
final Font font = extractFont(line); final Font font = extractFont(line);
return new PSystemDitaa(source, "", performSeparationOfCommonEdges, dropShadows, allCornersAreRound, transparentBackground, scale, font); return new PSystemDitaa(source, "", performSeparationOfCommonEdges, dropShadows, allCornersAreRound, transparentBackground, scale, font, forceFontSize);
} }
if (system == null) if (system == null)
return null; return null;

View File

@ -45,6 +45,7 @@ public class RenderingOptions {
private Color backgroundColor = Color.white; private Color backgroundColor = Color.white;
private Font font = new Font("Dialog", Font.BOLD, 12); private Font font = new Font("Dialog", Font.BOLD, 12);
private boolean forceFontSize = false;
/** /**
* @return * @return
@ -140,4 +141,13 @@ public class RenderingOptions {
this.font = font; this.font = font;
} }
public boolean getForceFontSize()
{
return forceFontSize;
}
public void setForceFontSize(boolean forceFontSize)
{
this.forceFontSize = forceFontSize;
}
} }

View File

@ -111,7 +111,8 @@ public class Diagram {
this.cellWidth = options.renderingOptions.getCellWidth(); this.cellWidth = options.renderingOptions.getCellWidth();
this.cellHeight = options.renderingOptions.getCellHeight(); this.cellHeight = options.renderingOptions.getCellHeight();
FontMeasurer fontMeasurer = new FontMeasurer(options.renderingOptions.getFont()); FontMeasurer fontMeasurer = new FontMeasurer(
options.renderingOptions.getFont(), options.renderingOptions.getForceFontSize());
width = grid.getWidth() * cellWidth; width = grid.getWidth() * cellWidth;
height = grid.getHeight() * cellHeight; height = grid.getHeight() * cellHeight;

View File

@ -34,11 +34,13 @@ import java.awt.image.BufferedImage;
public class FontMeasurer { public class FontMeasurer {
private final Font baseFont; private final Font baseFont;
private boolean forceFontSize;
private FontRenderContext fakeRenderContext; private FontRenderContext fakeRenderContext;
private Graphics2D fakeGraphics; private Graphics2D fakeGraphics;
public FontMeasurer(Font font){ public FontMeasurer(Font font, boolean forceFontSize){
baseFont = font; this.baseFont = font;
this.forceFontSize = forceFontSize;
BufferedImage image = new BufferedImage(1,1, BufferedImage.TYPE_INT_RGB); BufferedImage image = new BufferedImage(1,1, BufferedImage.TYPE_INT_RGB);
fakeGraphics = image.createGraphics(); fakeGraphics = image.createGraphics();
@ -83,30 +85,42 @@ public class FontMeasurer {
} }
public Font getFontFor(final int maxWidth, final String string){ public Font getFontFor(final int maxWidth, final String string){
FontPredicate predicate = new FontPredicate() { if (forceFontSize)
@Override {
public boolean test(Font font) return baseFont;
{ }
int width = getWidthFor(string, font); else
return width > maxWidth; {
} FontPredicate predicate = new FontPredicate() {
}; @Override
public boolean test(Font font)
return deriveFont(predicate, 1.0f); {
int width = getWidthFor(string, font);
return width > maxWidth;
}
};
return deriveFont(predicate, 1.0f);
}
} }
public Font getFontFor(final int pixelHeight){ public Font getFontFor(final int pixelHeight){
FontPredicate predicate = new FontPredicate() { if (forceFontSize)
@Override {
public boolean test(Font font) return baseFont;
{ }
//ascent is the distance between the baseline and the tallest character else
int ascent = getAscent(font); {
return ascent > pixelHeight; FontPredicate predicate = new FontPredicate() {
} @Override
}; public boolean test(Font font)
{
return deriveFont(predicate, 0.5f); //ascent is the distance between the baseline and the tallest character
int ascent = getAscent(font);
return ascent > pixelHeight;
}
};
return deriveFont(predicate, 0.5f);
}
} }
private Font deriveFont(FontPredicate predicate, float sizeDelta) private Font deriveFont(FontPredicate predicate, float sizeDelta)