Skip to content

Commit

Permalink
prmr#413 Utilize FontMetrics class
Browse files Browse the repository at this point in the history
  • Loading branch information
yannsartori authored and Farihatanjin committed Feb 23, 2021
1 parent 78a2edc commit f2f3caa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 40 deletions.
13 changes: 3 additions & 10 deletions src/ca/mcgill/cs/jetuml/viewers/edges/AbstractEdgeViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
import ca.mcgill.cs.jetuml.geom.Point;
import ca.mcgill.cs.jetuml.geom.Rectangle;
import ca.mcgill.cs.jetuml.viewers.nodes.NodeViewerRegistry;
import ca.mcgill.cs.jetuml.views.FontMetrics;
import ca.mcgill.cs.jetuml.views.ToolGraphics;
import javafx.geometry.Bounds;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Shape;
import javafx.scene.text.Text;

/**
* Provides shared services for viewing an edge.
Expand All @@ -46,12 +46,7 @@ public abstract class AbstractEdgeViewer implements EdgeViewer
protected static final int MAX_DISTANCE = 3;
protected static final int BUTTON_SIZE = 25;
protected static final int OFFSET = 3;
private static final Text SIZE_TESTER = new Text();

static
{
SIZE_TESTER.setFont(FONT);
}
private static final FontMetrics SIZE_TESTER = new FontMetrics(FONT);

private static final int DEGREES_180 = 180;

Expand Down Expand Up @@ -80,9 +75,7 @@ protected Shape getShape(Edge pEdge)
*/
protected static Dimension textDimensions( String pText )
{
SIZE_TESTER.setText(pText);
Bounds bounds = SIZE_TESTER.getBoundsInLocal();
return new Dimension((int)bounds.getWidth(), (int)bounds.getHeight());
return SIZE_TESTER.getDimension(pText);
}

@Override
Expand Down
54 changes: 24 additions & 30 deletions src/ca/mcgill/cs/jetuml/views/StringViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@

import ca.mcgill.cs.jetuml.geom.Dimension;
import ca.mcgill.cs.jetuml.geom.Rectangle;
import javafx.geometry.Bounds;
import javafx.geometry.VPos;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.scene.text.TextBoundsType;

/**
* A utility class to view strings with various decorations:
Expand All @@ -41,6 +38,8 @@ public final class StringViewer
{
public static final Font FONT = Font.font("System", 12);
private static final Font FONT_BOLD = Font.font(FONT.getFamily(), FontWeight.BOLD, FONT.getSize());
private static final FontMetrics FONT_METRICS = new FontMetrics(FONT);
private static final FontMetrics FONT_BOLD_METRICS = new FontMetrics(FONT_BOLD);

private static final Dimension EMPTY = new Dimension(0, 0);
private static final int HORIZONTAL_TEXT_PADDING = 7;
Expand Down Expand Up @@ -84,6 +83,15 @@ private Font getFont()
}
}

private FontMetrics getFontMetrics()
{
if ( aBold )
{
return FONT_BOLD_METRICS;
}
return FONT_METRICS;
}

/**
* Gets the width and height required to show pString, including
* padding around the string.
Expand All @@ -98,35 +106,22 @@ public Dimension getDimension(String pString)
{
return EMPTY;
}
Bounds bounds = getLabel(pString).getLayoutBounds();
return new Dimension((int) Math.round(bounds.getWidth() + HORIZONTAL_TEXT_PADDING*2),
(int) Math.round(bounds.getHeight() + VERTICAL_TEXT_PADDING*2));
Dimension dimension = getFontMetrics().getDimension(pString);
return new Dimension((int) Math.round(dimension.width() + HORIZONTAL_TEXT_PADDING*2),
(int) Math.round(dimension.height() + VERTICAL_TEXT_PADDING*2));
}

private Text getLabel(String pString)
{
Text label = new Text();
if (aUnderlined)
{
label.setUnderline(true);
}
label.setFont(getFont());
label.setBoundsType(TextBoundsType.VISUAL);
label.setText(pString);

private TextAlignment getTextAlignment()
{
if(aAlignment == Align.LEFT)
{
label.setTextAlignment(TextAlignment.LEFT);
return TextAlignment.LEFT;
}
else if(aAlignment == Align.CENTER)
{
label.setTextAlignment(TextAlignment.CENTER);
}
else if(aAlignment == Align.RIGHT)
{
label.setTextAlignment(TextAlignment.RIGHT);
return TextAlignment.CENTER;
}
return label;
return TextAlignment.RIGHT;
}

/**
Expand All @@ -137,11 +132,10 @@ else if(aAlignment == Align.RIGHT)
*/
public void draw(String pString, GraphicsContext pGraphics, Rectangle pRectangle)
{
Text label = getLabel(pString);
final VPos oldVPos = pGraphics.getTextBaseline();
final TextAlignment oldAlign = pGraphics.getTextAlign();

pGraphics.setTextAlign(label.getTextAlignment());
pGraphics.setTextAlign(getTextAlignment());

int textX = 0;
int textY = 0;
Expand All @@ -164,19 +158,19 @@ public void draw(String pString, GraphicsContext pGraphics, Rectangle pRectangle
{
int xOffset = 0;
int yOffset = 0;
Bounds bounds = label.getLayoutBounds();
Dimension dimension = getFontMetrics().getDimension(pString);
if(aAlignment == Align.CENTER)
{
xOffset = (int) (bounds.getWidth()/2);
xOffset = (int) (dimension.width()/2);
yOffset = (int) (getFont().getSize()/2) + 1;
}
else if(aAlignment == Align.RIGHT)
{
xOffset = (int) bounds.getWidth();
xOffset = (int) dimension.width();
}

ViewUtils.drawLine(pGraphics, textX-xOffset, textY+yOffset,
(int) (textX-xOffset+bounds.getWidth()), textY+yOffset, LineStyle.SOLID);
(int) (textX-xOffset+dimension.width()), textY+yOffset, LineStyle.SOLID);
}
pGraphics.translate(-pRectangle.getX(), -pRectangle.getY());
pGraphics.setTextBaseline(oldVPos);
Expand Down

0 comments on commit f2f3caa

Please sign in to comment.