diff --git a/src/ca/mcgill/cs/jetuml/viewers/edges/AbstractEdgeViewer.java b/src/ca/mcgill/cs/jetuml/viewers/edges/AbstractEdgeViewer.java index c79bb087d..9bbd56be3 100644 --- a/src/ca/mcgill/cs/jetuml/viewers/edges/AbstractEdgeViewer.java +++ b/src/ca/mcgill/cs/jetuml/viewers/edges/AbstractEdgeViewer.java @@ -29,6 +29,7 @@ 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; @@ -36,7 +37,6 @@ 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. @@ -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; @@ -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 diff --git a/src/ca/mcgill/cs/jetuml/views/StringViewer.java b/src/ca/mcgill/cs/jetuml/views/StringViewer.java index 0469f56dc..bd3f9cfee 100644 --- a/src/ca/mcgill/cs/jetuml/views/StringViewer.java +++ b/src/ca/mcgill/cs/jetuml/views/StringViewer.java @@ -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: @@ -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; @@ -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. @@ -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; } /** @@ -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; @@ -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);