Search in sources :

Example 11 with FontRenderContext

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

the class MissingCodePointLayoutTest method main.

public static void main(String[] args) {
    Font font = new Font("Tahoma", Font.PLAIN, 12);
    String text = "�";
    FontRenderContext frc = new FontRenderContext(null, false, false);
    TextLayout layout = new TextLayout(text, font, frc);
    layout.getCaretShapes(0);
}
Also used : FontRenderContext(java.awt.font.FontRenderContext) Font(java.awt.Font) TextLayout(java.awt.font.TextLayout)

Example 12 with FontRenderContext

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

the class HelvLtOblTest method drawText.

BufferedImage drawText(boolean doGV) {
    int w = 400;
    int h = 50;
    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bi.createGraphics();
    g.setColor(Color.white);
    g.fillRect(0, 0, w, h);
    g.setColor(Color.black);
    Font f = helvFont.deriveFont(Font.PLAIN, 40);
    g.setFont(f);
    int x = 5;
    int y = h - 10;
    if (doGV) {
        FontRenderContext frc = new FontRenderContext(null, true, true);
        GlyphVector gv = f.createGlyphVector(frc, codes);
        g.drawGlyphVector(gv, 5, y);
    } else {
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        g.drawString(str, x, y);
    }
    return bi;
}
Also used : GlyphVector(java.awt.font.GlyphVector) FontRenderContext(java.awt.font.FontRenderContext) BufferedImage(java.awt.image.BufferedImage) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D)

Example 13 with FontRenderContext

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

the class StyledSpaceAdvance method test.

static void test(int sz) {
    Font reg = new Font(name, Font.PLAIN, sz);
    Font bold = new Font(name, Font.BOLD, sz);
    //System.out.println("reg="+reg);
    //System.out.println("bold="+bold);
    FontRenderContext frc = new FontRenderContext(null, false, false);
    if (reg.getFontName(Locale.ENGLISH).equals(name) && bold.getFontName(Locale.ENGLISH).equals(name)) {
        Rectangle2D rb = reg.getStringBounds(" ", frc);
        Rectangle2D bb = bold.getStringBounds(" ", frc);
        if (bb.getWidth() > rb.getWidth() + 1.01f) {
            System.err.println("reg=" + reg + " bds = " + rb);
            System.err.println("bold=" + bold + " bds = " + bb);
            throw new RuntimeException("Advance difference too great.");
        }
    } else {
        System.out.println("Skipping test because fonts aren't as expected");
    }
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) FontRenderContext(java.awt.font.FontRenderContext) Font(java.awt.Font)

Example 14 with FontRenderContext

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

use of java.awt.font.FontRenderContext in project Activiti by Activiti.

the class DefaultProcessDiagramCanvas method drawLabel.

public void drawLabel(String text, GraphicInfo graphicInfo, boolean centered) {
    float interline = 1.0f;
    // text
    if (text != null && text.length() > 0) {
        Paint originalPaint = g.getPaint();
        Font originalFont = g.getFont();
        g.setPaint(LABEL_COLOR);
        g.setFont(LABEL_FONT);
        int wrapWidth = 100;
        int textY = (int) graphicInfo.getY();
        // TODO: use drawMultilineText()
        AttributedString as = new AttributedString(text);
        as.addAttribute(TextAttribute.FOREGROUND, g.getPaint());
        as.addAttribute(TextAttribute.FONT, g.getFont());
        AttributedCharacterIterator aci = as.getIterator();
        FontRenderContext frc = new FontRenderContext(null, true, false);
        LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
        while (lbm.getPosition() < text.length()) {
            TextLayout tl = lbm.nextLayout(wrapWidth);
            textY += tl.getAscent();
            Rectangle2D bb = tl.getBounds();
            double tX = graphicInfo.getX();
            if (centered) {
                tX += (int) (graphicInfo.getWidth() / 2 - bb.getWidth() / 2);
            }
            tl.draw(g, (float) tX, textY);
            textY += tl.getDescent() + tl.getLeading() + (interline - 1.0f) * tl.getAscent();
        }
        // restore originals
        g.setFont(originalFont);
        g.setPaint(originalPaint);
    }
}
Also used : AttributedString(java.text.AttributedString) LineBreakMeasurer(java.awt.font.LineBreakMeasurer) Rectangle2D(java.awt.geom.Rectangle2D) RoundRectangle2D(java.awt.geom.RoundRectangle2D) Paint(java.awt.Paint) FontRenderContext(java.awt.font.FontRenderContext) Font(java.awt.Font) Point(java.awt.Point) Paint(java.awt.Paint) AttributedCharacterIterator(java.text.AttributedCharacterIterator) TextLayout(java.awt.font.TextLayout)

Aggregations

FontRenderContext (java.awt.font.FontRenderContext)75 Font (java.awt.Font)25 TextLayout (java.awt.font.TextLayout)21 GlyphVector (java.awt.font.GlyphVector)18 Graphics2D (java.awt.Graphics2D)16 Rectangle2D (java.awt.geom.Rectangle2D)15 AttributedString (java.text.AttributedString)14 AffineTransform (java.awt.geom.AffineTransform)13 Paint (java.awt.Paint)8 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)7 AttributedCharacterIterator (java.text.AttributedCharacterIterator)7 LineMetrics (java.awt.font.LineMetrics)5 BufferedImage (java.awt.image.BufferedImage)5 ArrayList (java.util.ArrayList)5 Point (java.awt.Point)4 Shape (java.awt.Shape)4 BasicStroke (java.awt.BasicStroke)3 Color (java.awt.Color)3 FontMetrics (java.awt.FontMetrics)3 RoundRectangle2D (java.awt.geom.RoundRectangle2D)3