java.awt.font.TextLayout#getPixelBounds ( )源码实例Demo

下面列出了java.awt.font.TextLayout#getPixelBounds ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: openjdk-jdk9   文件: ArabicDiacriticTest.java
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");
    }
}
 
源代码2 项目: mars-sim   文件: ResizedFontLabel.java
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;
}
 
源代码3 项目: netbeans   文件: TextLayoutUtils.java
/**
     * 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;
    }
 
源代码4 项目: audiveris   文件: SlantedSymbol.java
@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;
}
 
源代码5 项目: libreveris   文件: SlantedSymbol.java
@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;
}
 
源代码6 项目: mil-sym-java   文件: SinglePointRenderer.java
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;
}