Search in sources :

Example 91 with TextLayout

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

the class TestLineBreakWithFontSub method checkMeasurer.

/**
     * Iterate through measurer and check that every line is
     * not too long and not too short, but just right.
     */
private void checkMeasurer(LineBreakMeasurer measurer, float wrappingWidth, float sequenceAdvance, int endPosition) {
    do {
        TextLayout layout = measurer.nextLayout(wrappingWidth);
        float visAdvance = layout.getVisibleAdvance();
        if (visAdvance > wrappingWidth) {
            // line is too long for given wrapping width
            throw new Error("layout is too long");
        }
        if (measurer.getPosition() < endPosition) {
            if (visAdvance <= wrappingWidth - sequenceAdvance) {
                // another word would have fit
                throw new Error("room for more words on line.  diff=" + (wrappingWidth - sequenceAdvance - visAdvance));
            }
        }
    } while (measurer.getPosition() != endPosition);
}
Also used : TextLayout(java.awt.font.TextLayout)

Example 92 with TextLayout

use of java.awt.font.TextLayout in project JMRI by JMRI.

the class ToolTip method paint.

public void paint(Graphics2D g2d, double scale) {
    if (_tip == null || _tip.trim().length() == 0) {
        return;
    }
    Color color = g2d.getColor();
    Font font = g2d.getFont();
    TextLayout tl = new TextLayout(_tip, _tFont, g2d.getFontRenderContext());
    Rectangle2D bds = tl.getBounds();
    int x0 = Math.max((int) (bds.getX() + _tx - bds.getWidth() / 2 - 9), 0);
    bds.setRect(x0, _ty + (bds.getHeight() - 9) / scale, bds.getWidth() + 9, bds.getHeight() + 8);
    g2d.setColor(_backgroundColor);
    g2d.fill(bds);
    g2d.setColor(_borderColor);
    g2d.draw(bds);
    g2d.setColor(_fontColor);
    tl.draw(g2d, x0 + 3f, (float) (_ty + bds.getHeight() - 4));
    g2d.setColor(color);
    g2d.setFont(font);
}
Also used : Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) Font(java.awt.Font) TextLayout(java.awt.font.TextLayout)

Example 93 with TextLayout

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

the class CompositionArea method setText.

/**
     * Sets the text and caret to be displayed in this composition area.
     * Shows the window if it contains text, hides it if not.
     */
void setText(AttributedCharacterIterator composedText, TextHitInfo caret) {
    composedTextLayout = null;
    if (composedText == null) {
        // there's no composed text to display, so hide the window
        compositionWindow.setVisible(false);
        this.caret = null;
    } else {
        /* since we have composed text, make sure the window is shown.
               This is necessary to get a valid graphics object. See 6181385.
            */
        if (!compositionWindow.isVisible()) {
            compositionWindow.setVisible(true);
        }
        Graphics g = getGraphics();
        if (g == null) {
            return;
        }
        try {
            updateWindowLocation();
            FontRenderContext context = ((Graphics2D) g).getFontRenderContext();
            composedTextLayout = new TextLayout(composedText, context);
            Rectangle2D bounds = composedTextLayout.getBounds();
            this.caret = caret;
            // Resize the composition area to just fit the text.
            FontMetrics metrics = g.getFontMetrics();
            Rectangle2D maxCharBoundsRec = metrics.getMaxCharBounds(g);
            int newHeight = (int) maxCharBoundsRec.getHeight() + HEIGHT_MARGIN;
            int newFrameHeight = newHeight + compositionWindow.getInsets().top + compositionWindow.getInsets().bottom;
            // If it's a passive client, set the width always to PASSIVE_WIDTH (480px)
            InputMethodRequests req = handler.getClientInputMethodRequests();
            int newWidth = (req == null) ? PASSIVE_WIDTH : (int) bounds.getWidth() + WIDTH_MARGIN;
            int newFrameWidth = newWidth + compositionWindow.getInsets().left + compositionWindow.getInsets().right;
            setPreferredSize(new Dimension(newWidth, newHeight));
            compositionWindow.setSize(new Dimension(newFrameWidth, newFrameHeight));
            // show the composed text
            paint(g);
        } finally {
            g.dispose();
        }
    }
}
Also used : Graphics(java.awt.Graphics) FontMetrics(java.awt.FontMetrics) InputMethodRequests(java.awt.im.InputMethodRequests) Rectangle2D(java.awt.geom.Rectangle2D) FontRenderContext(java.awt.font.FontRenderContext) Dimension(java.awt.Dimension) Point(java.awt.Point) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Example 94 with TextLayout

use of java.awt.font.TextLayout in project poi by apache.

the class XDGFText method draw.

/**
     * When calling this to draw text, it assumes graphics is set properly
     * to draw in the right style.
     */
public void draw(Graphics2D graphics) {
    String textContent = getTextContent();
    if (textContent.length() == 0)
        return;
    Rectangle2D.Double bounds = getTextBounds();
    String[] lines = textContent.trim().split("\n");
    FontRenderContext frc = graphics.getFontRenderContext();
    Font font = graphics.getFont();
    AffineTransform oldTr = graphics.getTransform();
    // visio is in flipped coordinates, so translate the text to be in the
    // right place
    Boolean flipX = _parent.getFlipX();
    Boolean flipY = _parent.getFlipY();
    if (flipY == null || !_parent.getFlipY()) {
        graphics.translate(bounds.x, bounds.y);
        graphics.scale(1, -1);
        graphics.translate(0, -bounds.height + graphics.getFontMetrics().getMaxCharBounds(graphics).getHeight());
    }
    if (flipX != null && _parent.getFlipX()) {
        graphics.scale(-1, 1);
        graphics.translate(-bounds.width, 0);
    }
    Double txtAngle = _parent.getTxtAngle();
    if (txtAngle != null && Math.abs(txtAngle) > 0.01)
        graphics.rotate(txtAngle);
    float nextY = 0;
    for (String line : lines) {
        if (line.length() == 0)
            continue;
        TextLayout layout = new TextLayout(line, font, frc);
        if (layout.isLeftToRight())
            layout.draw(graphics, 0, nextY);
        else
            layout.draw(graphics, (float) (bounds.width - layout.getAdvance()), nextY);
        nextY += layout.getAscent() + layout.getDescent() + layout.getLeading();
    }
    graphics.setTransform(oldTr);
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) AffineTransform(java.awt.geom.AffineTransform) FontRenderContext(java.awt.font.FontRenderContext) Font(java.awt.Font) TextLayout(java.awt.font.TextLayout)

Example 95 with TextLayout

use of java.awt.font.TextLayout in project jgnash by ccavanaugh.

the class PrintableCheckLayout method drawPayee.

private void drawPayee(final Graphics2D g2, final CheckObject object, final float offset, final String payee) {
    float payeeX = object.getX();
    float payeeY = object.getY() + offset;
    if (payee != null && !payee.isEmpty()) {
        TextLayout textLayout = new TextLayout(payee, font, frc);
        textLayout.draw(g2, payeeX, payeeY);
    }
    if (testPrint) {
        TextLayout payeeText = new TextLayout("ORDER OF", testPrintFont, frc);
        double width = payeeText.getBounds().getWidth();
        payeeText.draw(g2, (float) (payeeX - width - space), payeeY);
        LineMetrics metrics = testPrintFont.getLineMetrics("PAY TO THE", frc);
        float y = payeeY - (float) payeeText.getBounds().getHeight() - metrics.getDescent() - metrics.getLeading();
        payeeText = new TextLayout("PAY TO THE", testPrintFont, frc);
        payeeText.draw(g2, (float) (payeeX - width - space), y);
    }
}
Also used : LineMetrics(java.awt.font.LineMetrics) 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