Search in sources :

Example 46 with TextLayout

use of java.awt.font.TextLayout in project jdk8u_jdk by JetBrains.

the class SunGraphics2D method drawString.

public void drawString(AttributedCharacterIterator iterator, int x, int y) {
    if (iterator == null) {
        throw new NullPointerException("AttributedCharacterIterator is null");
    }
    if (iterator.getBeginIndex() == iterator.getEndIndex()) {
        return;
    /* nothing to draw */
    }
    TextLayout tl = new TextLayout(iterator, getFontRenderContext());
    tl.draw(this, (float) x, (float) y);
}
Also used : TextLayout(java.awt.font.TextLayout)

Example 47 with TextLayout

use of java.awt.font.TextLayout in project jdk8u_jdk by JetBrains.

the class Font method getStringBounds.

/**
     * Returns the logical bounds of the specified array of characters
     * in the specified <code>FontRenderContext</code>.  The logical
     * bounds contains the origin, ascent, advance, and height, which
     * includes the leading.  The logical bounds does not always enclose
     * all the text.  For example, in some languages and in some fonts,
     * accent marks can be positioned above the ascent or below the
     * descent.  To obtain a visual bounding box, which encloses all the
     * text, use the {@link TextLayout#getBounds() getBounds} method of
     * <code>TextLayout</code>.
     * <p>Note: The returned bounds is in baseline-relative coordinates
     * (see {@link java.awt.Font class notes}).
     * @param chars an array of characters
     * @param beginIndex the initial offset in the array of
     * characters
     * @param limit the end offset in the array of characters
     * @param frc the specified <code>FontRenderContext</code>
     * @return a <code>Rectangle2D</code> that is the bounding box of the
     * specified array of characters in the specified
     * <code>FontRenderContext</code>.
     * @throws IndexOutOfBoundsException if <code>beginIndex</code> is
     *         less than zero, or <code>limit</code> is greater than the
     *         length of <code>chars</code>, or <code>beginIndex</code>
     *         is greater than <code>limit</code>.
     * @see FontRenderContext
     * @see Font#createGlyphVector
     * @since 1.2
     */
public Rectangle2D getStringBounds(char[] chars, int beginIndex, int limit, FontRenderContext frc) {
    if (beginIndex < 0) {
        throw new IndexOutOfBoundsException("beginIndex: " + beginIndex);
    }
    if (limit > chars.length) {
        throw new IndexOutOfBoundsException("limit: " + limit);
    }
    if (beginIndex > limit) {
        throw new IndexOutOfBoundsException("range length: " + (limit - beginIndex));
    }
    // this code should be in textlayout
    // quick check for simple text, assume GV ok to use if simple
    boolean simple = values == null || (values.getKerning() == 0 && values.getLigatures() == 0 && values.getBaselineTransform() == null);
    if (simple) {
        simple = !FontUtilities.isComplexText(chars, beginIndex, limit);
    }
    if (simple) {
        GlyphVector gv = new StandardGlyphVector(this, chars, beginIndex, limit - beginIndex, frc);
        return gv.getLogicalBounds();
    } else {
        // need char array constructor on textlayout
        String str = new String(chars, beginIndex, limit - beginIndex);
        TextLayout tl = new TextLayout(str, this, frc);
        return new Rectangle2D.Float(0, -tl.getAscent(), tl.getAdvance(), tl.getAscent() + tl.getDescent() + tl.getLeading());
    }
}
Also used : StandardGlyphVector(sun.font.StandardGlyphVector) GlyphVector(java.awt.font.GlyphVector) StandardGlyphVector(sun.font.StandardGlyphVector) TextLayout(java.awt.font.TextLayout)

Example 48 with TextLayout

use of java.awt.font.TextLayout in project sulky by huxi.

the class MemoryStatus method paintMemoryStatus.

private void paintMemoryStatus(Graphics g, Rectangle paintingBounds) {
    MemoryInfo info = this.memoryInfo;
    Graphics2D g2 = (Graphics2D) g;
    // g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setComposite(AlphaComposite.Clear);
    g2.fillRect(0, 0, paintingBounds.width, paintingBounds.height);
    g2.setComposite(AlphaComposite.SrcOver);
    if (info != null) {
        if (!usingTotal) {
            double usedFraction = (((double) info.getUsed() / (double) info.getMax()));
            double totalFraction = (((double) info.getTotal() / (double) info.getMax()));
            int usedWidth = (int) (paintingBounds.width * usedFraction + 0.5);
            int totalWidth = (int) (paintingBounds.width * totalFraction + 0.5);
            drawBar(g2, 0, usedWidth, paintingBounds.height, USED_COLOR);
            // g.setColor(new Color(255, 255, 0,192));
            // g.fillRect(usedWidth, 0, totalWidth-usedWidth, paintingBounds.height);
            drawBar(g2, usedWidth, totalWidth, paintingBounds.height, TOTAL_COLOR);
        } else {
            double usedFraction = (((double) info.getUsed() / (double) info.getTotal()));
            int usedWidth = (int) (paintingBounds.width * usedFraction + 0.5);
            drawBar(g2, 0, usedWidth, paintingBounds.height, USED_COLOR);
        }
        // text
        {
            String text = HumanReadable.getHumanReadableSize(info.getUsed(), usingBinaryUnits, true) + "B";
            FontRenderContext frc = g2.getFontRenderContext();
            TextLayout tl = new TextLayout(text, getFont(), frc);
            Shape s = tl.getOutline(null);
            Rectangle textBounds = s.getBounds();
            if (logger.isDebugEnabled())
                logger.debug("textBounds: {}", textBounds);
            textBounds.x = (textBounds.x * -1) + (paintingBounds.width - textBounds.width) / 2;
            textBounds.y = (textBounds.y * -1) + (paintingBounds.height - textBounds.height) / 2;
            g.translate(textBounds.x, textBounds.y);
            if (logger.isDebugEnabled())
                logger.debug("corrected textBounds: {}", textBounds);
            // FontMetrics fm = g.getFontMetrics();
            // Rectangle2D lm = fm.getStringBounds(text, g);
            // int textX=(int) (paintingBounds.width-lm.getWidth());
            // int textBase=halfHeight-fm.getHeight()/2;
            g.setColor(Color.WHITE);
            GraphicsUtilities.drawHighlight(g2, s, GRADIENT_PIXELS, 0.2f);
            g.setColor(Color.BLACK);
            // g.drawString(text,textX,textBase);
            g2.fill(s);
        }
    }
}
Also used : Shape(java.awt.Shape) Rectangle(java.awt.Rectangle) FontRenderContext(java.awt.font.FontRenderContext) GradientPaint(java.awt.GradientPaint) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Example 49 with TextLayout

use of java.awt.font.TextLayout in project drools by kiegroup.

the class PongUI method drawScore.

public void drawScore(Player p, int x) {
    // ui.getTablePanel().getTableG();
    Graphics g = getGraphics();
    int y = (pconf.boundedTop() + 60);
    // background
    g.setColor(Color.BLACK);
    g.fillRect(x, y - 60, 90, 90);
    FontRenderContext frc = ((Graphics2D) g).getFontRenderContext();
    Font f = new Font("Monospaced", Font.BOLD, 70);
    String s = "" + p.getScore();
    TextLayout tl = new TextLayout(s, f, frc);
    g.setColor(Color.WHITE);
    tl.draw(((Graphics2D) g), x, y);
}
Also used : FontRenderContext(java.awt.font.FontRenderContext) EntryPoint(org.kie.api.runtime.rule.EntryPoint) TextLayout(java.awt.font.TextLayout)

Example 50 with TextLayout

use of java.awt.font.TextLayout in project jdk8u_jdk by JetBrains.

the class SunGraphics2D method drawString.

public void drawString(AttributedCharacterIterator iterator, float x, float y) {
    if (iterator == null) {
        throw new NullPointerException("AttributedCharacterIterator is null");
    }
    if (iterator.getBeginIndex() == iterator.getEndIndex()) {
        return;
    /* nothing to draw */
    }
    TextLayout tl = new TextLayout(iterator, getFontRenderContext());
    tl.draw(this, x, y);
}
Also used : TextLayout(java.awt.font.TextLayout)

Aggregations

TextLayout (java.awt.font.TextLayout)108 AttributedString (java.text.AttributedString)32 Graphics2D (java.awt.Graphics2D)25 FontRenderContext (java.awt.font.FontRenderContext)25 Font (java.awt.Font)20 AttributedCharacterIterator (java.text.AttributedCharacterIterator)17 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)16 Point (java.awt.Point)11 Rectangle (java.awt.Rectangle)10 Rectangle2D (java.awt.geom.Rectangle2D)10 Color (java.awt.Color)9 Paint (java.awt.Paint)8 AffineTransform (java.awt.geom.AffineTransform)8 Shape (java.awt.Shape)7 TextLayoutInfo (g4p_controls.StyledString.TextLayoutInfo)5 Dimension (java.awt.Dimension)5 TextHitInfo (java.awt.font.TextHitInfo)4 ArrayList (java.util.ArrayList)4 Graphics (java.awt.Graphics)3 Insets (java.awt.Insets)3