Search in sources :

Example 86 with TextLayout

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

the class PathGraphics method drawString.

public void drawString(String str, float x, float y) {
    if (str.length() == 0) {
        return;
    }
    TextLayout layout = new TextLayout(str, getFont(), getFontRenderContext());
    layout.draw(this, x, y);
}
Also used : TextLayout(java.awt.font.TextLayout)

Example 87 with TextLayout

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

the class DrawTest method main.

public static void main(String[] args) {
    final JFrame frame = new JFrame();
    frame.getContentPane().setPreferredSize(new Dimension(116, 75));
    frame.pack();
    frame.setVisible(true);
    Insets insets = frame.getInsets();
    System.out.println(insets);
    final JPanel panel = new JPanel() {

        private void drawString(Graphics g, Font font) {
            g.setFont(font);
            g.drawString(txt, 0, 32);
        }

        private void drawTextLayout(Graphics g, Font font) {
            TextLayout tl = new TextLayout(txt, font, g.getFontMetrics(font).getFontRenderContext());
            tl.draw((Graphics2D) g, 0, 65);
        }

        /**
             * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
             */
        @Override
        protected void paintComponent(Graphics g) {
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.BLACK);
            int width;
            TextLayout tl;
            if (testCaseNo == 1) {
                // Ok.
                // For the PLAIN font, the text painted by g.drawString and the text layout are the same.
                testCaseName = "PLAIN";
                errMsg = "plained";
                drawString(g, plain);
                drawTextLayout(g, plain);
            } else {
                // Not Ok.
                // For the BOLD font, the text painted by g.drawString and the text layout are NOT the same.
                testCaseName = "BOLD";
                errMsg = "bolded";
                drawString(g, bold);
                drawTextLayout(g, bold);
            }
        }
    };
    frame.getContentPane().add(panel);
    frame.setVisible(true);
    for (testCaseNo = 1; testCaseNo <= 2; testCaseNo++) {
        BufferedImage paintImage = getScreenShot(panel);
        if (testCaseNo == 2) {
            panel.revalidate();
            panel.repaint();
        }
        paintImage = getScreenShot(panel);
        int width1 = charWidth(paintImage, 0, 9, 116, 23);
        int width2 = charWidth(paintImage, 0, 42, 116, 23);
        if (width1 != width2) {
            System.out.println(testCaseName + " test case FAILED");
            isPassed = false;
        } else
            System.out.println(testCaseName + " test case PASSED");
    }
    frame.dispose();
    if (!isPassed) {
        throw new RuntimeException(errMsg + " logical fonts (Serif) was not correctly handled");
    }
}
Also used : Graphics(java.awt.Graphics) JPanel(javax.swing.JPanel) Insets(java.awt.Insets) JFrame(javax.swing.JFrame) Dimension(java.awt.Dimension) Font(java.awt.Font) BufferedImage(java.awt.image.BufferedImage) TextLayout(java.awt.font.TextLayout)

Example 88 with TextLayout

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

the class LigatureCaretTest method testLamAlef.

/**
     * Ensure proper careting and hit-testing behavior with
     * a lam-alef ligature.
     * If the test fails, an Error is thrown.
     * @exception an Error is thrown if the test fails
     */
public static void testLamAlef() {
    // lam-alef form a mandantory ligature.
    final String lamAlef = "لا";
    final String ltrText = "abcd";
    // Create a TextLayout with just a lam-alef sequence.  There
    // should only be two valid caret positions:  one at
    // insertion offset 0 and the other at insertion offset 2.
    TextLayout layout = new TextLayout(lamAlef, map, frc);
    TextHitInfo hit;
    hit = layout.getNextLeftHit(0);
    if (hit.getInsertionIndex() != 2) {
        throw new Error("Left hit failed.  Hit:" + hit);
    }
    hit = layout.getNextRightHit(2);
    if (hit.getInsertionIndex() != 0) {
        throw new Error("Right hit failed.  Hit:" + hit);
    }
    hit = layout.hitTestChar(layout.getAdvance() / 2, 0);
    if (hit.getInsertionIndex() != 0 && hit.getInsertionIndex() != 2) {
        throw new Error("Hit-test allowed incorrect caret.  Hit:" + hit);
    }
    // Create a TextLayout with some left-to-right text
    // before the lam-alef sequence.  There should not be
    // a caret position between the lam and alef.
    layout = new TextLayout(ltrText + lamAlef, map, frc);
    final int ltrLen = ltrText.length();
    final int layoutLen = layout.getCharacterCount();
    for (int i = 0; i < ltrLen; i++) {
        hit = layout.getNextRightHit(i);
        if (hit.getInsertionIndex() != i + 1) {
            throw new Error("Right hit failed in ltr text.");
        }
    }
    hit = layout.getNextRightHit(ltrLen);
    if (layoutLen != hit.getInsertionIndex()) {
        throw new Error("Right hit failed at direction boundary.");
    }
    hit = layout.getNextLeftHit(layoutLen);
    if (hit.getInsertionIndex() != ltrLen) {
        throw new Error("Left hit failed at end of text.");
    }
}
Also used : TextHitInfo(java.awt.font.TextHitInfo) TextLayout(java.awt.font.TextLayout)

Example 89 with TextLayout

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

the class LigatureCaretTest method testBidiWithNumbers.

/**
     * Caret through text mixed-direction text and check the results.
     * If the test fails an Error is thrown.
     * @exception an Error is thrown if the test fails
     */
public static void testBidiWithNumbers() {
    String bidiWithNumbers = "abcאבג123abc";
    // visual order for the text:
    // abc123<gimel><bet><aleph>abc
    int[] carets = { 0, 1, 2, 3, 7, 8, 6, 5, 4, 9, 10, 11, 12 };
    TextLayout layout = new TextLayout(bidiWithNumbers, map, frc);
    // Caret through TextLayout in both directions and check results.
    for (int i = 0; i < carets.length - 1; i++) {
        TextHitInfo hit = layout.getNextRightHit(carets[i]);
        if (hit.getInsertionIndex() != carets[i + 1]) {
            throw new Error("right hit failed within layout");
        }
    }
    if (layout.getNextRightHit(carets[carets.length - 1]) != null) {
        throw new Error("right hit failed at end of layout");
    }
    for (int i = carets.length - 1; i > 0; i--) {
        TextHitInfo hit = layout.getNextLeftHit(carets[i]);
        if (hit.getInsertionIndex() != carets[i - 1]) {
            throw new Error("left hit failed within layout");
        }
    }
    if (layout.getNextLeftHit(carets[0]) != null) {
        throw new Error("left hit failed at end of layout");
    }
}
Also used : TextHitInfo(java.awt.font.TextHitInfo) TextLayout(java.awt.font.TextLayout)

Example 90 with TextLayout

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

the class CombiningPerf method main.

public static void main(String[] args) throws Exception {
    System.err.println("start");
    GraphicsEnvironment.getLocalGraphicsEnvironment();
    font = new Font("Lucida Sans Regular", PLAIN, 12);
    frc = new FontRenderContext(null, false, false);
    String ascii = "the characters are critical noodles?";
    String french = "l'aperçu caractère one été créés";
    String frenchX = "l'aperçu caractère one été eréés";
    // warmup
    for (int i = 0; i < 100; ++i) {
        TextLayout tl = new TextLayout(french, font, frc);
        tl = new TextLayout(ascii, font, frc);
        tl = new TextLayout(frenchX, font, frc);
    }
    /**/
    long atime = test(ascii);
    System.err.println("atime: " + (atime / 1000000.0) + " length: " + ascii.length());
    long ftime = test(french);
    System.err.println("ftime: " + (ftime / 1000000.0) + " length: " + french.length());
    long xtime = test(frenchX);
    System.err.println("xtime: " + (xtime / 1000000.0) + " length: " + frenchX.length());
    long limit = xtime * 2 / 3;
    if (atime > limit || ftime > limit) {
        throw new Exception("took too long");
    }
/**/
}
Also used : FontRenderContext(java.awt.font.FontRenderContext) Font(java.awt.Font) 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