Search in sources :

Example 66 with FontRenderContext

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

the class StyledFontLayoutTest method runTest.

private static void runTest() {
    im = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = im.createGraphics();
    g2d.setColor(Color.white);
    g2d.fillRect(0, 0, W, H);
    g2d.setColor(Color.black);
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    char[] chs = "Sample Text.".toCharArray();
    int len = chs.length;
    int x = 50, y = 100;
    FontRenderContext frc = g2d.getFontRenderContext();
    Font plain = new Font("Serif", Font.PLAIN, 48);
    GlyphVector pgv = plain.layoutGlyphVector(frc, chs, 0, len, 0);
    g2d.setFont(plain);
    g2d.drawChars(chs, 0, len, x, y);
    y += 50;
    g2d.drawGlyphVector(pgv, x, y);
    y += 50;
    Rectangle2D plainStrBounds = plain.getStringBounds(chs, 0, len, frc);
    Rectangle2D plainGVBounds = pgv.getLogicalBounds();
    Font bold = new Font("Serif", Font.BOLD, 48);
    GlyphVector bgv = bold.layoutGlyphVector(frc, chs, 0, len, 0);
    Rectangle2D boldStrBounds = bold.getStringBounds(chs, 0, len, frc);
    Rectangle2D boldGVBounds = bgv.getLogicalBounds();
    g2d.setFont(bold);
    g2d.drawChars(chs, 0, len, x, y);
    y += 50;
    g2d.drawGlyphVector(bgv, x, y);
    System.out.println("Plain String Bounds = " + plainStrBounds);
    System.out.println("Bold String Bounds = " + boldStrBounds);
    System.out.println("Plain GlyphVector Bounds = " + plainGVBounds);
    System.out.println("Bold GlyphVector Bounds = " + boldGVBounds);
    if (!plainStrBounds.equals(boldStrBounds) && plainGVBounds.equals(boldGVBounds)) {
        System.out.println("Test failed: Plain GV bounds same as Bold");
        if (!interactive) {
            throw new RuntimeException("Plain GV bounds same as Bold");
        }
    }
}
Also used : GlyphVector(java.awt.font.GlyphVector) Rectangle2D(java.awt.geom.Rectangle2D) FontRenderContext(java.awt.font.FontRenderContext) BufferedImage(java.awt.image.BufferedImage) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D)

Example 67 with FontRenderContext

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

Example 68 with FontRenderContext

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

the class HelvLtOblTest method paintComponent.

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    FontRenderContext frc = new FontRenderContext(null, true, true);
    Font f = helvFont.deriveFont(Font.PLAIN, 40);
    System.out.println("font = " + f.getFontName());
    GlyphVector gv = f.createGlyphVector(frc, codes);
    g.setFont(f);
    g.setColor(Color.white);
    g.fillRect(0, 0, 400, 400);
    g.setColor(Color.black);
    g2.drawGlyphVector(gv, 5, 200);
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    g2.drawString(str, 5, 250);
}
Also used : GlyphVector(java.awt.font.GlyphVector) FontRenderContext(java.awt.font.FontRenderContext) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D)

Example 69 with FontRenderContext

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

the class GraphPane method drawGraph.

protected void drawGraph(Graphics g) {
    if (!(g instanceof Graphics2D)) {
        throw new IllegalArgumentException("Graphics object passed is not the correct type");
    }
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    int w = getWidth();
    int h = getHeight();
    // Draw ordinate (y-axis).
    g2.draw(new Line2D.Double(PAD, PAD, PAD, h - PAD));
    // Draw abcissa (x-axis).
    g2.draw(new Line2D.Double(PAD, h - PAD, w - PAD, h - PAD));
    // Draw labels.
    Font font = g2.getFont();
    FontRenderContext frc = g2.getFontRenderContext();
    LineMetrics lm = font.getLineMetrics("0", frc);
    float[] dash1 = { 1.0f };
    BasicStroke dashed = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
    BasicStroke plain = new BasicStroke(1.0f);
    float sh = lm.getAscent() + lm.getDescent();
    // Ordinate (y-axis) label.
    float sy = PAD + ((h - 2 * PAD) - yLabel.length() * sh) / 2 + lm.getAscent();
    g2.setPaint(Color.green.darker());
    for (int i = 0; i < yLabel.length(); i++) {
        String letter = String.valueOf(yLabel.charAt(i));
        float sw = (float) font.getStringBounds(letter, frc).getWidth();
        float sx = (PAD / 2 - sw) / 2;
        g2.drawString(letter, sx, sy);
        sy += sh;
    }
    // Abcissa (x-axis) label.
    sy = h - PAD / 2 + (PAD / 2 - sh) / 2 + lm.getAscent();
    float sw = (float) font.getStringBounds(xLabel, frc).getWidth();
    float sx = (w - sw) / 2;
    g2.drawString(xLabel, sx, sy);
    // find the maximum of all profiles
    float maxSpeed = 0;
    for (int i = 0; i < _sp.length; i++) {
        maxSpeed = Math.max(_sp[i].getMax(), maxSpeed);
    }
    // Used to scale values into drawing area
    float scale = (h - 2 * PAD) / maxSpeed;
    // space between values along the ordinate (y-axis)
    // start with an increment of 1
    // Plot a grid line every two
    // Plot a label every ten
    float yInc = scale;
    int yMod = 10;
    int gridMod = 2;
    if (units == Speed.MPH) {
        // need inverse transform here
        yInc = Speed.mphToKph(yInc);
    }
    if ((units == Speed.KPH) && (maxSpeed > 100) || (units == Speed.MPH) && (maxSpeed > 160)) {
        log.debug("Adjusting Y axis spacing for max speed");
        yMod *= 2;
        gridMod *= 2;
    }
    String ordString;
    // Draw lines
    for (int i = 0; i <= (h - 2 * PAD) / yInc; i++) {
        g2.setPaint(Color.green.darker());
        g2.setStroke(plain);
        float y1 = h - PAD - i * yInc;
        if ((i % yMod) == 0) {
            g2.draw(new Line2D.Double(7 * PAD / 8, y1, PAD, y1));
            ordString = Integer.toString(i);
            sw = (float) font.getStringBounds(ordString, frc).getWidth();
            sx = 7 * PAD / 8 - sw;
            sy = y1 + lm.getAscent() / 2;
            g2.drawString(ordString, sx, sy);
        }
        if (_grid && (i > 0) && ((i % gridMod) == 0)) {
            // Horizontal grid lines
            g2.setPaint(Color.LIGHT_GRAY);
            if ((i % yMod) != 0) {
                g2.setStroke(dashed);
            }
            g2.draw(new Line2D.Double(PAD, y1, w - PAD, y1));
        }
    }
    if (_grid) {
        // Close the top
        g2.setPaint(Color.LIGHT_GRAY);
        g2.setStroke(dashed);
        g2.draw(new Line2D.Double(PAD, PAD, w - PAD, PAD));
    }
    // The space between values along the abcissa (x-axis).
    float xInc = (float) (w - 2 * PAD) / (_sp[0].getLength() - 1);
    String abString;
    // for each point in a profile
    for (int i = 0; i < _sp[0].getLength(); i++) {
        g2.setPaint(Color.green.darker());
        g2.setStroke(plain);
        float x1 = 0.0F;
        // for each profile in the array
        for (int j = 0; j < _sp.length; j++) {
            x1 = PAD + i * xInc;
            float y1 = h - PAD - scale * _sp[j].getPoint(i);
            float x2 = PAD + (i + 1) * xInc;
            float y2 = h - PAD - scale * _sp[j].getPoint(i + 1);
            // if it's a valid data point
            if (i <= _sp[j].getLast() - 1) {
                g2.draw(new Line2D.Double(x1, y1, x2, y2));
            }
        }
        // tick marks along abcissa
        g2.draw(new Line2D.Double(x1, h - 7 * PAD / 8, x1, h - PAD));
        if (((i % 5) == 0) || (i == _sp[0].getLength() - 1)) {
            // abcissa labels every 5 ticks
            abString = Integer.toString(i);
            sw = (float) font.getStringBounds(abString, frc).getWidth();
            sx = x1 - sw / 2;
            sy = h - PAD + (PAD / 2 - sh) / 2 + lm.getAscent();
            g2.drawString(abString, sx, sy);
        }
        if (_grid && (i > 0)) {
            // Verical grid line
            g2.setPaint(Color.LIGHT_GRAY);
            if ((i % 5) != 0) {
                g2.setStroke(dashed);
            }
            g2.draw(new Line2D.Double(x1, PAD, x1, h - PAD));
        }
    }
    g2.setStroke(plain);
    // for each point in a profile
    for (int i = 0; i <= _sp[0].getLength(); i++) {
        // for each profile in the array
        for (int j = 0; j < _sp.length; j++) {
            g2.setPaint(colors[j]);
            float x = PAD + i * xInc;
            float y = h - PAD - scale * _sp[j].getPoint(i);
            // if it's a valid data point
            if (i <= _sp[j].getLast()) {
                g2.fill(new Ellipse2D.Double(x - 2, y - 2, 4, 4));
            }
        }
    }
}
Also used : BasicStroke(java.awt.BasicStroke) Line2D(java.awt.geom.Line2D) Font(java.awt.Font) Ellipse2D(java.awt.geom.Ellipse2D) Graphics2D(java.awt.Graphics2D) FontRenderContext(java.awt.font.FontRenderContext) LineMetrics(java.awt.font.LineMetrics)

Example 70 with FontRenderContext

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

the class ExtendedTextSourceLabel method createGV.

protected StandardGlyphVector createGV() {
    FontRenderContext frc = source.getFRC();
    int flags = source.getLayoutFlags();
    char[] context = source.getChars();
    int start = source.getStart();
    int length = source.getLength();
    // !!! no custom layout engines
    GlyphLayout gl = GlyphLayout.get(null);
    // ??? use textsource
    gv = gl.layout(font, frc, context, start, length, flags, null);
    GlyphLayout.done(gl);
    return gv;
}
Also used : FontRenderContext(java.awt.font.FontRenderContext)

Aggregations

FontRenderContext (java.awt.font.FontRenderContext)79 Font (java.awt.Font)27 TextLayout (java.awt.font.TextLayout)25 GlyphVector (java.awt.font.GlyphVector)18 Graphics2D (java.awt.Graphics2D)17 Rectangle2D (java.awt.geom.Rectangle2D)17 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 Color (java.awt.Color)5 Shape (java.awt.Shape)5 LineMetrics (java.awt.font.LineMetrics)5 BufferedImage (java.awt.image.BufferedImage)5 ArrayList (java.util.ArrayList)5 Point (java.awt.Point)4 BasicStroke (java.awt.BasicStroke)3 FontMetrics (java.awt.FontMetrics)3 Rectangle (java.awt.Rectangle)3