Search in sources :

Example 11 with GlyphVector

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

the class ClassCastExceptionForInvalidSurface method main.

public static void main(final String[] args) throws InterruptedException {
    // Catch all uncaught exceptions and treat them as test failure
    Thread.setDefaultUncaughtExceptionHandler((t, e) -> failed = e);
    // Data for rendering
    BufferedImage bi = new BufferedImage(10, 10, TYPE_INT_ARGB);
    FontRenderContext frc = new FontRenderContext(null, false, false);
    Font font = new Font("Serif", Font.PLAIN, 12);
    GlyphVector gv = font.createGlyphVector(frc, new char[] { 'a', '1' });
    Thread t1 = new Thread(() -> {
        while (!isComplete()) {
            vi = gc.createCompatibleVolatileImage(10, 10);
            if (!list.offer(vi)) {
                vi.flush();
            }
        }
        list.forEach(Image::flush);
    });
    Thread t2 = new Thread(() -> {
        while (!isComplete()) {
            VolatileImage vi = list.poll();
            if (vi != null) {
                vi.flush();
            }
        }
    });
    Thread t3 = new Thread(() -> {
        while (!isComplete()) {
            vi.createGraphics().drawImage(bi, 1, 1, null);
        }
    });
    Thread t4 = new Thread(() -> {
        while (!isComplete()) {
            vi.createGraphics().drawGlyphVector(gv, 0, 0);
            vi.createGraphics().drawOval(0, 0, 10, 10);
            vi.createGraphics().drawLine(0, 0, 10, 10);
            vi.createGraphics().drawString("123", 1, 1);
            vi.createGraphics().draw(new Rectangle(0, 0, 10, 10));
            vi.createGraphics().fillOval(0, 0, 10, 10);
            final Graphics2D graphics = vi.createGraphics();
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            graphics.fillPolygon(new int[] { 0, 10, 10, 0 }, new int[] { 0, 0, 10, 10 }, 4);
        }
    });
    t1.start();
    t2.start();
    t3.start();
    t4.start();
    t1.join();
    t2.join();
    t3.join();
    t4.join();
    if (failed != null) {
        System.err.println("Test failed");
        failed.printStackTrace();
    }
}
Also used : GlyphVector(java.awt.font.GlyphVector) VolatileImage(java.awt.image.VolatileImage) Rectangle(java.awt.Rectangle) FontRenderContext(java.awt.font.FontRenderContext) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) VolatileImage(java.awt.image.VolatileImage) BufferedImage(java.awt.image.BufferedImage) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D)

Example 12 with GlyphVector

use of java.awt.font.GlyphVector in project intellij-community by JetBrains.

the class ComplexTextFragmentTest method assertCaretPositionsForGlyphVector.

private static void assertCaretPositionsForGlyphVector(MyGlyphVector gv, int... expectedPositions) {
    FontLayoutService.setInstance(new MockFontLayoutService(TEST_CHAR_WIDTH, TEST_LINE_HEIGHT, TEST_DESCENT) {

        @NotNull
        @Override
        public GlyphVector layoutGlyphVector(@NotNull Font font, @NotNull FontRenderContext fontRenderContext, @NotNull char[] chars, int start, int end, boolean isRtl) {
            return gv;
        }
    });
    try {
        int length = gv.getNumChars();
        char[] text = new char[length];
        FontInfo fontInfo = new FontInfo(Font.MONOSPACED, 1, Font.PLAIN, false, new FontRenderContext(null, false, false));
        ComplexTextFragment fragment = new ComplexTextFragment(text, 0, length, (gv.getLayoutFlags() & GlyphVector.FLAG_RUN_RTL) != 0, fontInfo);
        int[] charPositions = new int[length];
        for (int i = 0; i < length; i++) {
            charPositions[i] = (int) fragment.visualColumnToX(0, i + 1);
        }
        assertArrayEquals(expectedPositions, charPositions);
    } finally {
        FontLayoutService.setInstance(null);
    }
}
Also used : GlyphVector(java.awt.font.GlyphVector) AbstractMockGlyphVector(com.intellij.testFramework.AbstractMockGlyphVector) MockFontLayoutService(com.intellij.testFramework.MockFontLayoutService) FontRenderContext(java.awt.font.FontRenderContext) NotNull(org.jetbrains.annotations.NotNull) FontInfo(com.intellij.openapi.editor.impl.FontInfo)

Example 13 with GlyphVector

use of java.awt.font.GlyphVector in project intellij-community by JetBrains.

the class UIUtil method getHighestGlyphHeight.

/**
   * @param string {@code String} to examine
   * @param font {@code Font} that is used to render the string
   * @param graphics {@link Graphics} that should be used to render the string
   * @return height of the tallest glyph in a string. If string is empty, returns 0
   */
public static int getHighestGlyphHeight(@NotNull String string, @NotNull Font font, @NotNull Graphics graphics) {
    FontRenderContext frc = ((Graphics2D) graphics).getFontRenderContext();
    GlyphVector gv = font.createGlyphVector(frc, string);
    int maxHeight = 0;
    for (int i = 0; i < string.length(); i++) {
        maxHeight = Math.max(maxHeight, (int) gv.getGlyphMetrics(i).getBounds2D().getHeight());
    }
    return maxHeight;
}
Also used : GlyphVector(java.awt.font.GlyphVector) FontRenderContext(java.awt.font.FontRenderContext)

Example 14 with GlyphVector

use of java.awt.font.GlyphVector 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 15 with GlyphVector

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

the class VerticalTextIcon method calculateSize.

private void calculateSize() {
    font = UIManager.getFont("Button.font");
    FontRenderContext frc = new FontRenderContext(null, true, true);
    GlyphVector glyphs = font.createGlyphVector(frc, text);
    width = (int) glyphs.getLogicalBounds().getWidth();
    height = (int) glyphs.getLogicalBounds().getHeight();
}
Also used : GlyphVector(java.awt.font.GlyphVector) FontRenderContext(java.awt.font.FontRenderContext)

Aggregations

GlyphVector (java.awt.font.GlyphVector)36 FontRenderContext (java.awt.font.FontRenderContext)18 Font (java.awt.Font)8 Graphics2D (java.awt.Graphics2D)8 Rectangle (java.awt.Rectangle)7 Rectangle2D (java.awt.geom.Rectangle2D)7 BufferedImage (java.awt.image.BufferedImage)5 Shape (java.awt.Shape)3 AffineTransform (java.awt.geom.AffineTransform)3 BasicStroke (java.awt.BasicStroke)2 FontMetrics (java.awt.FontMetrics)2 Image (java.awt.Image)2 Paint (java.awt.Paint)2 TextLayout (java.awt.font.TextLayout)2 Point2D (java.awt.geom.Point2D)2 Map (java.util.Map)2 Texture (com.badlogic.gdx.graphics.Texture)1 FontInfo (com.intellij.openapi.editor.impl.FontInfo)1 AbstractMockGlyphVector (com.intellij.testFramework.AbstractMockGlyphVector)1 MockFontLayoutService (com.intellij.testFramework.MockFontLayoutService)1