Java Code Examples for java.awt.font.TextLayout#getPixelBounds()

The following examples show how to use java.awt.font.TextLayout#getPixelBounds() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ArabicDiacriticTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static void measureText() {
    Font font = new Font(FONT, Font.PLAIN, 36);
    if (!font.getFamily(Locale.ENGLISH).equals(FONT)) {
        return;
    }
    FontRenderContext frc = new FontRenderContext(null, false, false);
    TextLayout tl1 = new TextLayout(STR1, font, frc);
    TextLayout tl2 = new TextLayout(STR2, font, frc);
    Rectangle r1 = tl1.getPixelBounds(frc, 0f, 0f);
    Rectangle r2 = tl2.getPixelBounds(frc, 0f, 0f);
    if (r1.height > r2.height) {
        System.out.println(font);
        System.out.println(r1);
        System.out.println(r2);
        throw new RuntimeException("BAD BOUNDS");
    }
}
 
Example 2
Source File: ResizedFontLabel.java    From mars-sim with GNU General Public License v3.0 6 votes vote down vote up
private BufferedImage createImage(String label) {
    Font font = new Font(Font.SANS_SERIF, Font.BOLD, SIZE);
    FontRenderContext frc = new FontRenderContext(null, true, true);
    TextLayout layout = new TextLayout(label, font, frc);
    Rectangle r = layout.getPixelBounds(null, 0, 0);
    //System.out.println(r);
    BufferedImage bi = new BufferedImage(
        r.width + 1, r.height + 1, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = (Graphics2D) bi.getGraphics();
    g2d.setRenderingHint(
        RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setColor(getBackground());
    g2d.fillRect(0, 0, bi.getWidth(), bi.getHeight());
    g2d.setColor(getForeground());
    layout.draw(g2d, 0, -r.y);
    g2d.dispose();
    return bi;
}
 
Example 3
Source File: TextLayoutUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
     * Compute a most appropriate width of the given text layout.
     */
    public static float getWidth(TextLayout textLayout, String textLayoutText, Font font) {
        // For italic fonts the textLayout.getAdvance() includes some extra horizontal space.
        // On the other hand index2X() for TL.getCharacterCount() is width along baseline
        // so when TL ends with e.g. 'd' char the end of 'd' char is cut off.
        float width;
        int tlLen = textLayoutText.length();
        if (!font.isItalic() ||
            tlLen == 0 ||
            Character.isWhitespace(textLayoutText.charAt(tlLen - 1)) ||
            Bidi.requiresBidi(textLayoutText.toCharArray(), 0, textLayoutText.length()))
        {
            width = textLayout.getAdvance();
            if (LOG.isLoggable(Level.FINE)) {
                LOG.fine("TLUtils.getWidth(\"" + CharSequenceUtilities.debugText(textLayoutText) + // NOI18N
                        "\"): Using TL.getAdvance()=" + width + // NOI18N
//                        textLayoutDump(textLayout) + 
                        '\n');
            }
        } else {
            // Compute pixel bounds (with frc being null - means use textLayout's frc; and with default bounds)
            Rectangle pixelBounds = textLayout.getPixelBounds(null, 0, 0);
            width = (float) pixelBounds.getMaxX();
            // On Mac OS X with retina displays the TL.getPixelBounds() give incorrect results. Luckily
            // TL.getAdvance() gives a correct result in that case.
            // Therefore use a minimum of both values (on all platforms).
            float tlAdvance = textLayout.getAdvance();
            if (LOG.isLoggable(Level.FINE)) {
                LOG.fine("TLUtils.getWidth(\"" + CharSequenceUtilities.debugText(textLayoutText) + // NOI18N
                        "\"): Using minimum of TL.getPixelBounds().getMaxX()=" + width + // NOI18N
                        " or TL.getAdvance()=" + tlAdvance +
                        textLayoutDump(textLayout) +
                        '\n');
            }
            width = Math.min(width, tlAdvance);
        }
        
        // For RTL text the hit-info of the first char is above the hit-info of ending char.
        // However textLayout.isLeftToRight() returns true in case of mixture of LTR and RTL text
        // in a single textLayout.
        
        // Ceil the width to avoid rendering artifacts.
        width = (float) Math.ceil(width);
        return width;
    }
 
Example 4
Source File: SlantedSymbol.java    From audiveris with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected MyParams getParams (MusicFont font)
{
    MyParams p = new MyParams();

    p.layouts = new SmartLayout[codes.length];

    // Union of all rectangles
    Rectangle2D rect = null;

    // Current abscissa
    float x = 0;

    for (int i = 0; i < codes.length; i++) {
        int code = codes[i];
        TextLayout layout = font.layout(code);
        Rectangle2D r = layout.getBounds();

        // Abscissa reduction because of slanted characters
        // Its value depends on whether we have a 'f' or not
        float dx;
        int c = code - MusicFont.CODE_OFFSET;

        if ((c == 102) || // F
                (c == 196) || // FF
                (c == 236) || // FFF
                (c == 83) || // SF
                (c == 90)) { // FZ
            dx = (float) r.getHeight() * 0.215f; // Measured
        } else {
            dx = (float) r.getHeight() * 0.075f; // Measured
        }

        p.layouts[i] = new SmartLayout(layout, dx);

        if (i > 0) {
            x -= dx;
        }

        if (i == 0) {
            rect = layout.getPixelBounds(null, x, 0);
        } else {
            Rectangle2D.union(rect, layout.getPixelBounds(null, x, 0), rect);
        }

        x += (r.getWidth() - dx);
    }

    p.rect = new Rectangle(
            (int) Math.floor(rect.getX()),
            (int) Math.floor(rect.getY()),
            (int) Math.ceil(rect.getWidth()),
            (int) Math.ceil(rect.getHeight()));

    return p;
}
 
Example 5
Source File: SlantedSymbol.java    From libreveris with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
protected MyParams getParams (MusicFont font)
{
    MyParams p = new MyParams();

    p.layouts = new SmartLayout[codes.length];

    // Union of all rectangles
    Rectangle2D rect = null;

    // Current abscissa
    float x = 0;

    for (int i = 0; i < codes.length; i++) {
        int code = codes[i];
        TextLayout layout = font.layout(code);
        Rectangle2D r = layout.getBounds();

        // Abscissa reduction because of slanted characters
        // It's value depends on whether we have a 'f' or not
        float dx;
        int c = code - MusicFont.CODE_OFFSET;

        if ((c == 102) || // F
                (c == 196) || // FF
                (c == 236) || // FFF
                (c == 83) || // SF
                (c == 90)) { // FZ
            dx = (float) r.getHeight() * 0.215f; // Measured
        } else {
            dx = (float) r.getHeight() * 0.075f; // Measured
        }

        p.layouts[i] = new SmartLayout(layout, dx);

        if (i > 0) {
            x -= dx;
        }

        if (i == 0) {
            rect = layout.getPixelBounds(null, x, 0);
        } else {
            Rectangle2D.union(
                    rect,
                    layout.getPixelBounds(null, x, 0),
                    rect);
        }

        x += (r.getWidth() - dx);
    }

    p.rect = new Rectangle(
            (int) Math.floor(rect.getX()),
            (int) Math.floor(rect.getY()),
            (int) Math.ceil(rect.getWidth()),
            (int) Math.ceil(rect.getHeight()));

    return p;
}
 
Example 6
Source File: SinglePointRenderer.java    From mil-sym-java with Apache License 2.0 4 votes vote down vote up
private ShapeInfo CreateEchelonShapeInfo(String echelon, FontRenderContext frc, Rectangle bounds, Color textColor, Color textBackgroundColor)
{
    ShapeInfo siEchelon = null;

    int x = 0;
    int y = 0;
    String echelonText;

    int bufferY = 1;

    double ratio = 3.5;//chose 3.5 because I want 10pt font at 35x35 pixels
    //double ratio = 3;//chose 3 because I want 12pt font at 35x35 pixels

    int fontSize = 12;
    
    boolean scaleEchelon = RendererSettings.getInstance().getScaleEchelon();
    
    fontSize = RendererSettings.getInstance().getLabelFontSize();
    
    if(scaleEchelon)
        fontSize = (int)Math.round(bounds.getWidth() / ratio);

    

    try
    {
        if(echelon != null && !echelon.equals(""))
        {

            echelonText = GetEchelonText(echelon);
            if(!echelonText.equals(""))
            {
                TextLayout text = new TextLayout(echelonText, new Font("Arial", Font.BOLD, fontSize), frc);
                Float descent = text.getDescent();
                Rectangle labelBounds = text.getPixelBounds(null, 0, 0);

                x = bounds.x + (bounds.width/2) - (labelBounds.width / 2);
                y = bounds.y - descent.intValue();// - bufferY;

                if(RendererSettings.getInstance().getTextBackgroundMethod() == RendererSettings.TextBackgroundMethod_OUTLINE)
                {
                    y = y - (RendererSettings.getInstance().getTextOutlineWidth()/2);
                }
                else if(RendererSettings.getInstance().getTextBackgroundMethod() == RendererSettings.TextBackgroundMethod_OUTLINE_QUICK)
                {
                    y = y - RendererSettings.getInstance().getTextOutlineWidth();
                }
                else if(RendererSettings.getInstance().getTextBackgroundMethod() == RendererSettings.TextBackgroundMethod_COLORFILL)
                {
                    y = y - 1;
                }

                siEchelon = SymbolDraw.CreateModifierShapeInfo(text, echelonText, x, y, textColor, textBackgroundColor);
                siEchelon.setShapeType(ShapeInfo.SHAPE_TYPE_UNIT_ECHELON);

            }
        }

    }
    catch(Exception exc)
    {
        ErrorLogger.LogException(_className, "CreateEchelonShapeInfo()", exc);
    }

    return siEchelon;
}