use of java.awt.font.FontRenderContext in project jgnash by ccavanaugh.
the class AWTFontUtilities method getFontRenderContext.
private static synchronized FontRenderContext getFontRenderContext() {
FontRenderContext context = contextReference.get();
if (context == null) {
context = new FontRenderContext(null, true, true);
contextReference = new SoftReference<>(context);
}
return context;
}
use of java.awt.font.FontRenderContext in project jgnash by ccavanaugh.
the class AWTFontUtilities method getStringWidth.
/**
* Calculates the width of the specified {@code String}.
*
* @param text the text to be weighted.
* @param font the font to be weighted
* @return the width of the given string in 1/72" dpi.
*/
private static int getStringWidth(final String text, final Font font) {
final FontRenderContext frc = getFontRenderContext();
// text is padded by one space
final Rectangle2D bounds = font.getStringBounds(text + " ", frc);
return (int) ceil(bounds.getWidth()) + 5;
}
use of java.awt.font.FontRenderContext in project chipKIT32-MAX by chipKIT32.
the class CompositionTextManager method getTextLayout.
private TextLayout getTextLayout(AttributedCharacterIterator text, int committed_count) {
AttributedString composed = new AttributedString(text, committed_count, text.getEndIndex());
Font font = textArea.getPainter().getFont();
FontRenderContext context = ((Graphics2D) (textArea.getPainter().getGraphics())).getFontRenderContext();
composed.addAttribute(TextAttribute.FONT, font);
TextLayout layout = new TextLayout(composed.getIterator(), context);
return layout;
}
use of java.awt.font.FontRenderContext in project gephi by gephi.
the class EdgeLabelRenderer method renderG2D.
public void renderG2D(G2DTarget target, String label, float x, float y, Color color, float outlineSize, Color outlineColor) {
Graphics2D graphics = target.getGraphics();
graphics.setFont(font);
FontMetrics fm = graphics.getFontMetrics();
float posX = x - fm.stringWidth(label) / 2f;
float posY = y + fm.getAscent() / 2f;
Shape outlineGlyph = null;
if (outlineSize > 0) {
FontRenderContext frc = graphics.getFontRenderContext();
GlyphVector gv = font.createGlyphVector(frc, label);
outlineGlyph = gv.getOutline(posX, posY);
graphics.setColor(outlineColor);
graphics.setStroke(new BasicStroke(outlineSize, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
graphics.draw(outlineGlyph);
}
graphics.setColor(color);
if (null == outlineGlyph) {
graphics.drawString(label, posX, posY);
} else {
graphics.fill(outlineGlyph);
}
}
use of java.awt.font.FontRenderContext in project gephi by gephi.
the class NodeLabelRenderer method renderG2D.
public void renderG2D(G2DTarget target, String label, float x, float y, int fontSize, Color color, float outlineSize, Color outlineColor, boolean showBox, Color boxColor) {
Graphics2D graphics = target.getGraphics();
Font font = fontCache.get(fontSize);
graphics.setFont(font);
FontMetrics fm = graphics.getFontMetrics();
float posX = x - fm.stringWidth(label) / 2f;
float posY = y + fm.getDescent();
Shape outlineGlyph = null;
//Box
if (showBox) {
graphics.setColor(boxColor);
Rectangle2D.Float rect = new Rectangle2D.Float();
rect.setFrame(posX - outlineSize / 2f, y - (fm.getAscent() + fm.getDescent()) / 2f - outlineSize / 2f, fm.stringWidth(label) + outlineSize, fm.getAscent() + fm.getDescent() + outlineSize);
graphics.draw(rect);
}
//Outline
if (outlineSize > 0) {
FontRenderContext frc = graphics.getFontRenderContext();
GlyphVector gv = font.createGlyphVector(frc, label);
outlineGlyph = gv.getOutline(posX, posY);
graphics.setColor(outlineColor);
graphics.setStroke(new BasicStroke(outlineSize, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
graphics.draw(outlineGlyph);
}
graphics.setColor(color);
if (null == outlineGlyph) {
graphics.drawString(label, posX, posY);
} else {
graphics.fill(outlineGlyph);
}
}
Aggregations