Skip to content

Commit

Permalink
[prmr#512] Added italic markup feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
jkcoding7 committed Feb 9, 2024
1 parent 571ece4 commit 61c3a76
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
40 changes: 36 additions & 4 deletions src/org/jetuml/rendering/StringRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import javafx.geometry.VPos;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.TextAlignment;

Expand Down Expand Up @@ -101,10 +102,11 @@ private boolean isRight()
* Various text decorations.
*/
public enum TextDecoration
{ BOLD, UNDERLINED, PADDED }
{ BOLD, ITALIC, UNDERLINED, PADDED }

private Alignment aAlign = Alignment.CENTER_CENTER;
private final boolean aBold;
private final boolean aItalic;
private final boolean aUnderlined;
private int aHorizontalPadding = DEFAULT_HORIZONTAL_TEXT_PADDING;
private int aVerticalPadding = DEFAULT_VERTICAL_TEXT_PADDING;
Expand All @@ -118,6 +120,7 @@ private StringRenderer(Alignment pAlign, EnumSet<TextDecoration> pDecorations)
}
aAlign = pAlign;
aBold = pDecorations.contains(TextDecoration.BOLD);
aItalic = pDecorations.contains(TextDecoration.ITALIC);
aUnderlined = pDecorations.contains(TextDecoration.UNDERLINED);
}

Expand Down Expand Up @@ -242,12 +245,12 @@ public void draw(String pString, GraphicsContext pGraphics, Rectangle pRectangle
{
final VPos oldVPos = pGraphics.getTextBaseline();
final TextAlignment oldAlign = pGraphics.getTextAlign();
int textX = 0;
int textY = 0;

pGraphics.setTextAlign(getTextAlignment());
pGraphics.setTextBaseline(getTextBaseline());

int textX = 0;
int textY = 0;
if( aAlign.isHorizontallyCentered() )
{
textX = pRectangle.getWidth()/2;
Expand All @@ -263,7 +266,18 @@ public void draw(String pString, GraphicsContext pGraphics, Rectangle pRectangle
}

pGraphics.translate(pRectangle.getX(), pRectangle.getY());
CANVAS_FONT.drawString(pGraphics, textX, textY, pString.trim(), aBold);
if( aBold && aItalic )
{
RenderingUtils.drawText(pGraphics, textX, textY, pString.trim(), CANVAS_FONT.getBoldItalic());
}
else if( aItalic )
{
RenderingUtils.drawText(pGraphics, textX, textY, pString.trim(), CANVAS_FONT.getItalic());
}
else
{
CANVAS_FONT.drawString(pGraphics, textX, textY, pString.trim(), aBold);
}

if(aUnderlined && pString.trim().length() > 0)
{
Expand Down Expand Up @@ -300,8 +314,12 @@ private static final class CanvasFont implements IntegerPreferenceChangeHandler

private Font aFont;
private Font aFontBold;
private Font aFontItalic;
private Font aFontBoldItalic;
private FontMetrics aFontMetrics;
private FontMetrics aFontBoldMetrics;
private FontMetrics aFontItalicMetrics;
private FontMetrics aFontBoldItalicMetrics;

private CanvasFont()
{
Expand All @@ -317,6 +335,16 @@ private Font getFont(boolean pBold)
}
return aFont;
}

public Font getItalic()
{
return aFontItalic;
}

public Font getBoldItalic()
{
return aFontBoldItalic;
}

private FontMetrics getFontMetrics(boolean pBold)
{
Expand Down Expand Up @@ -385,8 +413,12 @@ private void refreshAttributes()
{
aFont = Font.font("System", UserPreferences.instance().getInteger(IntegerPreference.fontSize));
aFontBold = Font.font(aFont.getFamily(), FontWeight.BOLD, aFont.getSize());
aFontItalic = Font.font(aFont.getFamily(), FontPosture.ITALIC, aFont.getSize());
aFontBoldItalic = Font.font(aFont.getFamily(), FontWeight.BOLD, FontPosture.ITALIC, aFont.getSize());
aFontMetrics = new FontMetrics(aFont);
aFontBoldMetrics = new FontMetrics(aFontBold);
aFontItalicMetrics = new FontMetrics(aFontItalic);
aFontBoldItalicMetrics = new FontMetrics(aFontBoldItalic);
}

}
Expand Down
33 changes: 32 additions & 1 deletion src/org/jetuml/rendering/nodes/TypeNodeRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ public class TypeNodeRenderer extends AbstractNodeRenderer
protected static final int DEFAULT_HEIGHT = 60;
protected static final int TOP_INCREMENT = 20;
private static final StringRenderer NAME_VIEWER = StringRenderer.get(Alignment.CENTER_CENTER, TextDecoration.BOLD, TextDecoration.PADDED);
private static final StringRenderer ITALIC_NAME_VIEWER = StringRenderer.get(
Alignment.CENTER_CENTER, TextDecoration.BOLD, TextDecoration.ITALIC, TextDecoration.PADDED);
private static final StringRenderer STRING_VIEWER = StringRenderer.get(Alignment.TOP_LEFT, TextDecoration.PADDED);
private static final StringRenderer UNDERLINING_STRING_VIEWER = StringRenderer.get(
Alignment.TOP_LEFT, TextDecoration.PADDED, TextDecoration.UNDERLINED);
private static final StringRenderer ITALIC_STRING_VIEWER = StringRenderer.get(
Alignment.TOP_LEFT, TextDecoration.PADDED, TextDecoration.ITALIC);
/**
* @param pParent The renderer for the parent diagram.
*/
Expand All @@ -76,7 +80,8 @@ public void draw(DiagramElement pElement, GraphicsContext pGraphics)
final int nameHeight = nameBoxHeight(node, attributeHeight, methodHeight);

RenderingUtils.drawRectangle(pGraphics, bounds);
NAME_VIEWER.draw(getNameText(node), pGraphics, new Rectangle(bounds.getX(), bounds.getY(), bounds.getWidth(), nameHeight));
drawName(node, bounds, bounds.getY(), nameHeight, pGraphics);
//NAME_VIEWER.draw(getNameText(node), pGraphics, new Rectangle(bounds.getX(), bounds.getY(), bounds.getWidth(), nameHeight));

if( attributeHeight > 0 )
{
Expand All @@ -96,6 +101,24 @@ else if( methodHeight > 0 )
RenderingUtils.drawLine(pGraphics, bounds.getX(), splitY, bounds.getMaxX(), splitY, LineStyle.SOLID);
drawMethod(node, bounds, splitY, methodHeight, pGraphics);
}
}

private static void drawName(TypeNode pNode, Rectangle pBounds, int pSplitY, int pNameBoxHeight, GraphicsContext pGraphics)
{
String name = pNode.getName();
if( name.length() > 2 && name.startsWith("/") && name.endsWith("/") )
{
StringBuilder underline = new StringBuilder(name);
underline.deleteCharAt(0);
underline.deleteCharAt(underline.length() - 1);
ITALIC_NAME_VIEWER.draw(underline.toString(), pGraphics,
new Rectangle(pBounds.getX(), pSplitY, pBounds.getWidth(), pNameBoxHeight));
}
else
{
NAME_VIEWER.draw(name, pGraphics,
new Rectangle(pBounds.getX(), pSplitY, pBounds.getWidth(), pNameBoxHeight));
}
}

private static void drawAttribute(TypeNode pNode, Rectangle pBounds, int pSplitY, int pAttributeBoxHeight, GraphicsContext pGraphics)
Expand Down Expand Up @@ -139,6 +162,14 @@ private static void drawMethod(TypeNode pNode, Rectangle pBounds, int pSplitY, i
UNDERLINING_STRING_VIEWER.draw(underline.toString(), pGraphics,
new Rectangle(pBounds.getX(), pSplitY + lineSpacing, pBounds.getWidth(), pMethodBoxHeight));
}
else if( method.length() > 2 && method.startsWith("/") && method.endsWith("/") )
{
StringBuilder underline = new StringBuilder(method);
underline.deleteCharAt(0);
underline.deleteCharAt(underline.length() - 1);
ITALIC_STRING_VIEWER.draw(underline.toString(), pGraphics,
new Rectangle(pBounds.getX(), pSplitY + lineSpacing, pBounds.getWidth(), pMethodBoxHeight));
}
else
{
STRING_VIEWER.draw(method, pGraphics,
Expand Down

0 comments on commit 61c3a76

Please sign in to comment.