Example usage for java.awt.font FontRenderContext FontRenderContext

List of usage examples for java.awt.font FontRenderContext FontRenderContext

Introduction

In this page you can find the example usage for java.awt.font FontRenderContext FontRenderContext.

Prototype

public FontRenderContext(AffineTransform tx, Object aaHint, Object fmHint) 

Source Link

Document

Constructs a FontRenderContext object from an optional AffineTransform and two Object values that determine if the newly constructed object has anti-aliasing or fractional metrics.

Usage

From source file:Main.java

public static boolean isVectorFont(Font font) {
    GlyphVector gv = font.createGlyphVector(new FontRenderContext(null, true, false), "Test");
    Shape fontShape = gv.getOutline();
    Rectangle2D bounds = fontShape.getBounds2D();
    return !(bounds.getWidth() == 0 && bounds.getHeight() == 0);
}

From source file:Main.java

public static int getFontSize(String text, boolean getWidth) {
    if (text != null && text.length() > 0) {
        Font defaultFont = new Font("Montserrat", Font.PLAIN, 13);

        AffineTransform affinetransform = new AffineTransform();
        FontRenderContext frc = new FontRenderContext(affinetransform, true, true);
        int textWidth = (int) (defaultFont.getStringBounds(text, frc) != null
                ? defaultFont.getStringBounds(text, frc).getWidth()
                : 0) + 10;/*ww w . j  a v a2 s.  co  m*/
        int textHeight = (int) (defaultFont.getStringBounds(text, frc) != null
                ? defaultFont.getStringBounds(text, frc).getHeight()
                : 0);
        return getWidth ? textWidth : textHeight;
    }
    return 0;
}

From source file:Main.java

public static int paintMultilineText(Graphics2D g2d, String text, int textX, int textWidth, int textY,
        int maxTextLineCount) {
    FontRenderContext frc = new FontRenderContext(new AffineTransform(), true, false);
    int fa = g2d.getFontMetrics().getAscent();

    if (text.length() == 0)
        return textY;

    int currOffset = 0;
    AttributedString attributedDescription = new AttributedString(text);
    attributedDescription.addAttribute(TextAttribute.FONT, g2d.getFont());
    LineBreakMeasurer lineBreakMeasurer = new LineBreakMeasurer(attributedDescription.getIterator(), frc);
    int lineCount = 0;
    while (true) {
        TextLayout tl = lineBreakMeasurer.nextLayout(textWidth);
        if (tl == null)
            break;

        int charCount = tl.getCharacterCount();
        String line = text.substring(currOffset, currOffset + charCount);

        g2d.drawString(line, textX, textY);

        textY += fa;/*w w  w.ja v a 2s .  c  o  m*/
        currOffset += charCount;

        lineCount++;
        if ((maxTextLineCount > 0) && (lineCount == maxTextLineCount))
            break;
    }

    // textY += fh;

    return textY;
}

From source file:Main.java

/**
 * Returns the bounds of the given string.
 *
 * @param text the string to measure//from   w  w  w  . j  a va 2  s .c  o m
 * @return the bounds of the given string
 */
public static Rectangle2D getDefaultStringSize(String text) {
    Font font = UIManager.getFont("Label.font");
    FontRenderContext frc = new FontRenderContext(null, true, false);
    TextLayout layout = new TextLayout(text, font, frc);

    return layout.getBounds();
}

From source file:Main.java

/**
 * Creates and returns image from the given text.
 * @param text input text//from w w w .jav a  2 s .co  m
 * @param font text font
 * @return image with input text
 */
public static BufferedImage createImageFromText(String text, Font font) {
    //You may want to change these setting, or make them parameters
    boolean isAntiAliased = true;
    boolean usesFractionalMetrics = false;
    FontRenderContext frc = new FontRenderContext(null, isAntiAliased, usesFractionalMetrics);
    TextLayout layout = new TextLayout(text, font, frc);
    Rectangle2D bounds = layout.getBounds();
    int w = (int) Math.ceil(bounds.getWidth());
    int h = (int) Math.ceil(bounds.getHeight()) + 2;
    BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); //for example;
    Graphics2D g = image.createGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, w, h);
    g.setColor(Color.BLACK);
    g.setFont(font);
    Object antiAliased = isAntiAliased ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON
            : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF;
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, antiAliased);
    Object fractionalMetrics = usesFractionalMetrics ? RenderingHints.VALUE_FRACTIONALMETRICS_ON
            : RenderingHints.VALUE_FRACTIONALMETRICS_OFF;
    g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, fractionalMetrics);
    g.drawString(text, (float) -bounds.getX(), (float) -bounds.getY());
    g.dispose();

    return image;
}

From source file:Main.java

/**
 * Gets the text width./*from  w  w w . ja v  a  2  s  . co m*/
 *
 * @param text
 *            the text
 * @param font
 *            the font
 * @return the text width
 */
public static int getTextWidth(final String text, final Font font) {
    // final JLabel lbl = new JLabel(text);
    // lbl.setBorder(g);
    // lbl.setFont(font);
    // return (int) lbl.getPreferredSize().getWidth();
    AffineTransform affinetransform = new AffineTransform();
    FontRenderContext frc = new FontRenderContext(affinetransform, true, true);
    int textwidth = (int) (font.getStringBounds(text, frc).getWidth());
    int textheight = (int) (font.getStringBounds(text, frc).getHeight());
    return textwidth;
}

From source file:Test.java

public NumericShaperPanel() {
    String text = "java";
    HashMap map = new HashMap();
    Font font = new Font("Mongolian Baiti", Font.PLAIN, 32);
    map.put(TextAttribute.FONT, font);
    map.put(TextAttribute.NUMERIC_SHAPING, NumericShaper.getShaper(NumericShaper.Range.MONGOLIAN));
    FontRenderContext fontRenderContext = new FontRenderContext(null, false, false);
    layout = new TextLayout(text, map, fontRenderContext);
}

From source file:Main.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;

    attribCharIterator = attribString.getIterator();

    FontRenderContext frc = new FontRenderContext(null, false, false);
    LineBreakMeasurer lbm = new LineBreakMeasurer(attribCharIterator, frc);

    int x = 10, y = 20;
    int w = getWidth();

    float wrappingWidth = w - 15;

    while (lbm.getPosition() < text.length()) {
        TextLayout layout = lbm.nextLayout(wrappingWidth);
        y += layout.getAscent();/*w ww.j a  v  a  2  s  . c om*/
        layout.draw(g2, x, y);
        y += layout.getDescent() + layout.getLeading();
    }
}

From source file:com.github.fritaly.dualcommander.Utils.java

public static int getTimestampRenderWidth() {
    final String text = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    final Font font = Utils.getDefaultFont();
    final FontRenderContext context = new FontRenderContext(new AffineTransform(), true, true);

    return (int) font.getStringBounds(text, context).getWidth();
}

From source file:TextLayoutPanel.java

public TextLayoutPanel() {
    setBackground(Color.white);//from  w w  w . j a v  a  2s  .  c  o  m
    setForeground(Color.black);
    setSize(400, 200);
    addMouseListener(new MouseHandler());
    addMouseMotionListener(new MouseMotionHandler());
    w = getWidth();
    h = getHeight();

    String text = "Java Source and Support";
    font = new Font("Arial", Font.PLAIN, 36);
    frc = new FontRenderContext(null, false, false);
    layout = new TextLayout(text, font, frc);

    rx = (float) (w / 2 - layout.getBounds().getWidth() / 2);
    ry = (float) 3 * h / 4;
    rw = (float) (layout.getBounds().getWidth());
    rh = (float) (layout.getBounds().getHeight());
    rect = new Rectangle2D.Float(rx, ry, rw, rh);

    caretColor = getForeground();
}