Search in sources :

Example 51 with FontRenderContext

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

the class Toolkit method getAscent.

/**
   * Synthesized replacement for FontMetrics.getAscent(), which is dreadfully
   * inaccurate and inconsistent across platforms.
   */
public static double getAscent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    FontRenderContext frc = g2.getFontRenderContext();
    //return new TextLayout("H", font, frc).getBounds().getHeight();
    return new TextLayout("H", g.getFont(), frc).getBounds().getHeight();
}
Also used : FontRenderContext(java.awt.font.FontRenderContext) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Example 52 with FontRenderContext

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

the class PFont method getShape.

public PShape getShape(char ch, float detail) {
    Font font = (Font) getNative();
    if (font == null) {
        throw new IllegalArgumentException("getShape() only works on fonts loaded with createFont()");
    }
    PShape s = new PShape(PShape.PATH);
    // six element array received from the Java2D path iterator
    float[] iterPoints = new float[6];
    // array passed to createGylphVector
    char[] textArray = new char[] { ch };
    //Graphics2D graphics = (Graphics2D) this.getGraphics();
    //FontRenderContext frc = graphics.getFontRenderContext();
    @SuppressWarnings("deprecation") FontRenderContext frc = Toolkit.getDefaultToolkit().getFontMetrics(font).getFontRenderContext();
    GlyphVector gv = font.createGlyphVector(frc, textArray);
    Shape shp = gv.getOutline();
    // make everything into moveto and lineto
    PathIterator iter = (detail == 0) ? // maintain curves
    shp.getPathIterator(null) : // convert to line segments
    shp.getPathIterator(null, detail);
    int contours = 0;
    //    boolean contour = false;
    while (!iter.isDone()) {
        int type = iter.currentSegment(iterPoints);
        switch(type) {
            case // 1 point (2 vars) in textPoints
            PathIterator.SEG_MOVETO:
                //        if (!contour) {
                if (contours == 0) {
                    s.beginShape();
                } else {
                    s.beginContour();
                //          contour = true;
                }
                contours++;
                s.vertex(iterPoints[0], iterPoints[1]);
                break;
            case // 1 point
            PathIterator.SEG_LINETO:
                //        System.out.println("lineto");
                //        PApplet.println(PApplet.subset(iterPoints, 0, 2));
                s.vertex(iterPoints[0], iterPoints[1]);
                break;
            case // 2 points
            PathIterator.SEG_QUADTO:
                //        System.out.println("quadto");
                //        PApplet.println(PApplet.subset(iterPoints, 0, 4));
                s.quadraticVertex(iterPoints[0], iterPoints[1], iterPoints[2], iterPoints[3]);
                break;
            case // 3 points
            PathIterator.SEG_CUBICTO:
                //        System.out.println("cubicto");
                //        PApplet.println(iterPoints);
                s.quadraticVertex(iterPoints[0], iterPoints[1], iterPoints[2], iterPoints[3], iterPoints[4], iterPoints[5]);
                break;
            case PathIterator.SEG_CLOSE:
                //        System.out.println("close");
                if (contours > 1) {
                    //        contours--;
                    //        if (contours == 0) {
                    ////          s.endShape();
                    //        } else {
                    s.endContour();
                }
                break;
        }
        //      PApplet.println(iterPoints);
        iter.next();
    }
    s.endShape(CLOSE);
    return s;
}
Also used : GlyphVector(java.awt.font.GlyphVector) PathIterator(java.awt.geom.PathIterator) FontRenderContext(java.awt.font.FontRenderContext)

Example 53 with FontRenderContext

use of java.awt.font.FontRenderContext in project playn by threerings.

the class JavaTextLayout method layoutText.

public static JavaTextLayout layoutText(JavaGraphics gfx, String text, TextFormat format) {
    // we do some fiddling to work around the fact that TextLayout chokes on the empty string
    AttributedString astring = new AttributedString(text.length() == 0 ? " " : text);
    if (format.font != null) {
        astring.addAttribute(TextAttribute.FONT, ((JavaFont) format.font).jfont);
    }
    FontRenderContext frc = format.antialias ? gfx.aaFontContext : gfx.aFontContext;
    return new JavaTextLayout(text, format, new TextLayout(astring.getIterator(), frc));
}
Also used : AttributedString(java.text.AttributedString) FontRenderContext(java.awt.font.FontRenderContext) AbstractTextLayout(playn.core.AbstractTextLayout) TextLayout(java.awt.font.TextLayout)

Example 54 with FontRenderContext

use of java.awt.font.FontRenderContext in project limelight by slagyr.

the class TextPanel method getRenderContext.

public static FontRenderContext getRenderContext() {
    if (staticFontRenderingContext == null) {
        AffineTransform affineTransform = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().getDefaultTransform();
        staticFontRenderingContext = new FontRenderContext(affineTransform, true, false);
    }
    return staticFontRenderingContext;
}
Also used : AffineTransform(java.awt.geom.AffineTransform) FontRenderContext(java.awt.font.FontRenderContext)

Example 55 with FontRenderContext

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

the class DefaultEditorTextRepresentationHelper method updateContext.

public void updateContext() {
    FontRenderContext oldContext = myFontRenderContext;
    myFontRenderContext = FontInfo.getFontRenderContext(myEditor.getContentComponent());
    if (!myFontRenderContext.equals(oldContext))
        clearSymbolWidthCache();
}
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