Search in sources :

Example 6 with FontRenderContext

use of java.awt.font.FontRenderContext in project smile by haifengl.

the class Graphics method drawText.

/**
     * Draw a string with given reference point and rotation angle. (0.5, 0.5)
     * is center, (0, 0) is lower left, (0, 1) is upper left, etc. The angle of
     * rotation is in radians. The coordinates are logical coordinates.
     */
public void drawText(String label, double horizontalReference, double verticalReference, double rotation, double[] coord) {
    int[] sc = projection.screenProjection(coord);
    int x = sc[0];
    int y = sc[1];
    AffineTransform transform = g2d.getTransform();
    // Corner offset adjustment : Text Offset is used Here
    FontRenderContext frc = g2d.getFontRenderContext();
    Font font = g2d.getFont();
    double w = font.getStringBounds(label, frc).getWidth();
    double h = font.getSize2D();
    if (rotation != 0) {
        g2d.rotate(rotation, x, y);
    }
    x -= (int) (w * horizontalReference);
    y += (int) (h * verticalReference);
    g2d.drawString(label, x, y);
    g2d.setTransform(transform);
}
Also used : AffineTransform(java.awt.geom.AffineTransform) FontRenderContext(java.awt.font.FontRenderContext) Paint(java.awt.Paint) Font(java.awt.Font)

Example 7 with FontRenderContext

use of java.awt.font.FontRenderContext in project android_frameworks_base by DirtyUnicorns.

the class BidiRenderer method render.

/**
     * Renders the text to the right of the bounds with the given font.
     * @param font The font to render the text with.
     */
private void render(int start, int limit, Font font, int flag, float[] advances, int advancesIndex, boolean draw) {
    FontRenderContext frc;
    if (mGraphics != null) {
        frc = mGraphics.getFontRenderContext();
    } else {
        frc = Toolkit.getDefaultToolkit().getFontMetrics(font).getFontRenderContext();
        // Metrics obtained this way don't have anti-aliasing set. So,
        // we create a new FontRenderContext with anti-aliasing set.
        frc = new FontRenderContext(font.getTransform(), mPaint.isAntiAliased(), frc.usesFractionalMetrics());
    }
    GlyphVector gv = font.layoutGlyphVector(frc, mText, start, limit, flag);
    int ng = gv.getNumGlyphs();
    int[] ci = gv.getGlyphCharIndices(0, ng, null);
    if (advances != null) {
        for (int i = 0; i < ng; i++) {
            int adv_idx = advancesIndex + ci[i];
            advances[adv_idx] += gv.getGlyphMetrics(i).getAdvanceX();
        }
    }
    if (draw && mGraphics != null) {
        mGraphics.drawGlyphVector(gv, mBounds.right, mBaseline);
    }
    // Update the bounds.
    Rectangle2D awtBounds = gv.getLogicalBounds();
    RectF bounds = awtRectToAndroidRect(awtBounds, mBounds.right, mBaseline);
    // coordinates from the bounds as an offset.
    if (Math.abs(mBounds.right - mBounds.left) == 0) {
        mBounds = bounds;
    } else {
        mBounds.union(bounds);
    }
}
Also used : GlyphVector(java.awt.font.GlyphVector) Rectangle2D(java.awt.geom.Rectangle2D) FontRenderContext(java.awt.font.FontRenderContext)

Example 8 with FontRenderContext

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

the class TestLayoutVsICU method show.

private void show(String arg) throws ParserConfigurationException, SAXException, IOException {
    id = "<none>";
    File xmlFile = new File(arg);
    if (!xmlFile.exists()) {
        throw new FileNotFoundException("Can't open input XML file " + arg);
    }
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    if (OPT_VERBOSE) {
        System.out.println("# Parsing " + xmlFile.getAbsolutePath());
    }
    Document doc = db.parse(xmlFile);
    Element e = doc.getDocumentElement();
    if (!XML_LAYOUT_TESTS.equals(e.getNodeName())) {
        throw new IllegalArgumentException("Document " + xmlFile.getAbsolutePath() + " does not have <layout-tests> as its base");
    }
    NodeList testCases = e.getElementsByTagName(XML_TEST_CASE);
    for (int caseNo = 0; caseNo < testCases.getLength(); caseNo++) {
        final Node testCase = testCases.item(caseNo);
        final Map<String, String> testCaseAttrs = attrs(testCase);
        id = testCaseAttrs.get(XML_ID);
        final String script = testCaseAttrs.get(XML_SCRIPT);
        String testText = null;
        Integer[] expectGlyphs = null;
        Integer[] expectIndices = null;
        Map<String, String> fontAttrs = null;
        if (OPT_VERBOSE) {
            System.out.println("#" + caseNo + " id=" + id + ", script=" + script);
        }
        NodeList children = testCase.getChildNodes();
        for (int sub = 0; sub < children.getLength(); sub++) {
            Node n = children.item(sub);
            if (n.getNodeType() != Node.ELEMENT_NODE)
                continue;
            String nn = n.getNodeName();
            if (nn.equals(XML_TEST_FONT)) {
                fontAttrs = attrs(n);
            } else if (nn.equals(XML_TEST_TEXT)) {
                testText = n.getTextContent();
            } else if (nn.equals(XML_RESULT_GLYPHS)) {
                String hex = n.getTextContent();
                expectGlyphs = parseHexArray(hex);
            } else if (nn.equals(XML_RESULT_INDICES)) {
                String hex = n.getTextContent();
                expectIndices = parseHexArray(hex);
            } else if (OPT_VERBOSE) {
                System.out.println("Ignoring node " + nn);
            }
        }
        if (fontAttrs == null) {
            throw new IllegalArgumentException(id + " Missing node " + XML_TEST_FONT);
        }
        if (testText == null) {
            throw new IllegalArgumentException(id + " Missing node " + XML_TEST_TEXT);
        }
        String fontName = fontAttrs.get(XML_NAME);
        Font f = getFont(fontName, fontAttrs);
        if (f == null) {
            if (OPT_FAILMISSING) {
                throw new MissingResourceException("Missing font,  abort test", Font.class.getName(), fontName);
            }
            System.out.println("Skipping " + id + " because font is missing: " + fontName);
            skipped++;
            continue;
        }
        FontRenderContext frc = new FontRenderContext(null, true, true);
        TextLayout tl = new TextLayout(testText, f, frc);
        final List<GlyphVector> glyphs = new ArrayList<GlyphVector>();
        Graphics2D myg2 = new Graphics2D() {

            @Override
            public void draw(Shape s) {
            // TODO Auto-generated method stub
            }

            @Override
            public boolean drawImage(Image img, AffineTransform xform, ImageObserver obs) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public void drawImage(BufferedImage img, BufferedImageOp op, int x, int y) {
            // TODO Auto-generated method stub
            }

            @Override
            public void drawRenderedImage(RenderedImage img, AffineTransform xform) {
            // TODO Auto-generated method stub
            }

            @Override
            public void drawRenderableImage(RenderableImage img, AffineTransform xform) {
            // TODO Auto-generated method stub
            }

            @Override
            public void drawString(String str, int x, int y) {
            // TODO Auto-generated method stub
            }

            @Override
            public void drawString(String str, float x, float y) {
            // TODO Auto-generated method stub
            }

            @Override
            public void drawString(AttributedCharacterIterator iterator, int x, int y) {
            // TODO Auto-generated method stub
            }

            @Override
            public void drawString(AttributedCharacterIterator iterator, float x, float y) {
            // TODO Auto-generated method stub
            }

            @Override
            public void drawGlyphVector(GlyphVector g, float x, float y) {
                if (x != 0.0 || y != 0.0) {
                    throw new InternalError("x,y should be 0 but got " + x + "," + y);
                }
                //System.err.println("dGV : " + g.toString() + " @ "+x+","+y);
                glyphs.add(g);
            }

            @Override
            public void fill(Shape s) {
            // TODO Auto-generated method stub
            }

            @Override
            public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public GraphicsConfiguration getDeviceConfiguration() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public void setComposite(Composite comp) {
            // TODO Auto-generated method stub
            }

            @Override
            public void setPaint(Paint paint) {
            // TODO Auto-generated method stub
            }

            @Override
            public void setStroke(Stroke s) {
            // TODO Auto-generated method stub
            }

            @Override
            public void setRenderingHint(Key hintKey, Object hintValue) {
            // TODO Auto-generated method stub
            }

            @Override
            public Object getRenderingHint(Key hintKey) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public void setRenderingHints(Map<?, ?> hints) {
            // TODO Auto-generated method stub
            }

            @Override
            public void addRenderingHints(Map<?, ?> hints) {
            // TODO Auto-generated method stub
            }

            @Override
            public RenderingHints getRenderingHints() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public void translate(int x, int y) {
            // TODO Auto-generated method stub
            }

            @Override
            public void translate(double tx, double ty) {
            // TODO Auto-generated method stub
            }

            @Override
            public void rotate(double theta) {
            // TODO Auto-generated method stub
            }

            @Override
            public void rotate(double theta, double x, double y) {
            // TODO Auto-generated method stub
            }

            @Override
            public void scale(double sx, double sy) {
            // TODO Auto-generated method stub
            }

            @Override
            public void shear(double shx, double shy) {
            // TODO Auto-generated method stub
            }

            @Override
            public void transform(AffineTransform Tx) {
            // TODO Auto-generated method stub
            }

            @Override
            public void setTransform(AffineTransform Tx) {
            // TODO Auto-generated method stub
            }

            @Override
            public AffineTransform getTransform() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public Paint getPaint() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public Composite getComposite() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public void setBackground(Color color) {
            // TODO Auto-generated method stub
            }

            @Override
            public Color getBackground() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public Stroke getStroke() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public void clip(Shape s) {
            // TODO Auto-generated method stub
            }

            @Override
            public FontRenderContext getFontRenderContext() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public Graphics create() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public Color getColor() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public void setColor(Color c) {
            // TODO Auto-generated method stub
            }

            @Override
            public void setPaintMode() {
            // TODO Auto-generated method stub
            }

            @Override
            public void setXORMode(Color c1) {
            // TODO Auto-generated method stub
            }

            @Override
            public Font getFont() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public void setFont(Font font) {
            // TODO Auto-generated method stub
            }

            @Override
            public FontMetrics getFontMetrics(Font f) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public Rectangle getClipBounds() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public void clipRect(int x, int y, int width, int height) {
            // TODO Auto-generated method stub
            }

            @Override
            public void setClip(int x, int y, int width, int height) {
            // TODO Auto-generated method stub
            }

            @Override
            public Shape getClip() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public void setClip(Shape clip) {
            // TODO Auto-generated method stub
            }

            @Override
            public void copyArea(int x, int y, int width, int height, int dx, int dy) {
            // TODO Auto-generated method stub
            }

            @Override
            public void drawLine(int x1, int y1, int x2, int y2) {
            // TODO Auto-generated method stub
            }

            @Override
            public void fillRect(int x, int y, int width, int height) {
            // TODO Auto-generated method stub
            }

            @Override
            public void clearRect(int x, int y, int width, int height) {
            // TODO Auto-generated method stub
            }

            @Override
            public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
            // TODO Auto-generated method stub
            }

            @Override
            public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
            // TODO Auto-generated method stub
            }

            @Override
            public void drawOval(int x, int y, int width, int height) {
            // TODO Auto-generated method stub
            }

            @Override
            public void fillOval(int x, int y, int width, int height) {
            // TODO Auto-generated method stub
            }

            @Override
            public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
            // TODO Auto-generated method stub
            }

            @Override
            public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
            // TODO Auto-generated method stub
            }

            @Override
            public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints) {
            // TODO Auto-generated method stub
            }

            @Override
            public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) {
            // TODO Auto-generated method stub
            }

            @Override
            public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) {
            // TODO Auto-generated method stub
            }

            @Override
            public boolean drawImage(Image img, int x, int y, ImageObserver observer) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public void dispose() {
            // TODO Auto-generated method stub
            }
        };
        tl.draw(myg2, 0, 0);
        if (glyphs.size() != 1) {
            err("drew " + glyphs.size() + " times - expected 1");
            total++;
            bad++;
            continue;
        }
        boolean isBad = false;
        GlyphVector gv = glyphs.get(0);
        // GLYPHS
        int[] gotGlyphs = gv.getGlyphCodes(0, gv.getNumGlyphs(), new int[gv.getNumGlyphs()]);
        // go up to this count
        int count = Math.min(gotGlyphs.length, expectGlyphs.length);
        for (int i = 0; i < count; i++) {
            if (gotGlyphs[i] != expectGlyphs[i]) {
                err("@" + i + " - got \tglyph 0x" + Integer.toHexString(gotGlyphs[i]) + " wanted 0x" + Integer.toHexString(expectGlyphs[i]));
                isBad = true;
                break;
            }
        }
        // INDICES
        int[] gotIndices = gv.getGlyphCharIndices(0, gv.getNumGlyphs(), new int[gv.getNumGlyphs()]);
        for (int i = 0; i < count; i++) {
            if (gotIndices[i] != expectIndices[i]) {
                err("@" + i + " - got \tindex 0x" + Integer.toHexString(gotGlyphs[i]) + " wanted 0x" + Integer.toHexString(expectGlyphs[i]));
                isBad = true;
                break;
            }
        }
        // COUNT
        if (gotGlyphs.length != expectGlyphs.length) {
            System.out.println("Got " + gotGlyphs.length + " wanted " + expectGlyphs.length + " glyphs");
            isBad = true;
        } else {
            if (OPT_VERBOSE) {
                System.out.println(">> OK: " + gotGlyphs.length + " glyphs");
            }
        }
        if (isBad) {
            bad++;
            System.out.println("* FAIL: " + id + "  /\t" + fontName);
        } else {
            System.out.println("* OK  : " + id + "  /\t" + fontName);
        }
        total++;
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Shape(java.awt.Shape) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) MissingResourceException(java.util.MissingResourceException) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) Rectangle(java.awt.Rectangle) Document(org.w3c.dom.Document) RenderableImage(java.awt.image.renderable.RenderableImage) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) RenderedImage(java.awt.image.RenderedImage) Font(java.awt.Font) BufferedImage(java.awt.image.BufferedImage) RenderableImage(java.awt.image.renderable.RenderableImage) BufferedImageOp(java.awt.image.BufferedImageOp) Stroke(java.awt.Stroke) GlyphVector(java.awt.font.GlyphVector) Composite(java.awt.Composite) NodeList(org.w3c.dom.NodeList) ImageObserver(java.awt.image.ImageObserver) Color(java.awt.Color) Paint(java.awt.Paint) Paint(java.awt.Paint) TextLayout(java.awt.font.TextLayout) Graphics2D(java.awt.Graphics2D) AttributedCharacterIterator(java.text.AttributedCharacterIterator) DocumentBuilder(javax.xml.parsers.DocumentBuilder) AffineTransform(java.awt.geom.AffineTransform) FontRenderContext(java.awt.font.FontRenderContext) RenderedImage(java.awt.image.RenderedImage) File(java.io.File) Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap) TreeMap(java.util.TreeMap) Key(java.awt.RenderingHints.Key)

Example 9 with FontRenderContext

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

the class TranslatedOutlineTest method main.

public static void main(String[] a) {
    /* prepare blank image */
    BufferedImage bi = new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = (Graphics2D) bi.getGraphics();
    g2.setColor(Color.WHITE);
    g2.fillRect(0, 0, 50, 50);
    /* draw outline somethere in the middle of the image */
    FontRenderContext frc = new FontRenderContext(null, false, false);
    g2.setColor(Color.RED);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
    GlyphVector gv = g2.getFont().createGlyphVector(frc, "test");
    g2.fill(gv.getOutline(20, 20));
    /* Check if anything was drawn.
         * If y direction is not correct then image is still blank and
         * test will fail.
         */
    int bgcolor = Color.WHITE.getRGB();
    for (int i = 0; i < bi.getWidth(); i++) {
        for (int j = 0; j < bi.getHeight(); j++) {
            if (bi.getRGB(i, j) != bgcolor) {
                System.out.println("Test passed.");
                return;
            }
        }
    }
    throw new RuntimeException("Outline was not detected");
}
Also used : GlyphVector(java.awt.font.GlyphVector) FontRenderContext(java.awt.font.FontRenderContext) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 10 with FontRenderContext

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

the class GetGlyphCharIndexTest method main.

public static void main(String[] args) {
    Font font = new Font(Font.MONOSPACED, Font.PLAIN, 12);
    FontRenderContext frc = new FontRenderContext(null, false, false);
    GlyphVector gv = font.layoutGlyphVector(frc, "abc".toCharArray(), 1, 3, Font.LAYOUT_LEFT_TO_RIGHT);
    int idx0 = gv.getGlyphCharIndex(0);
    if (idx0 != 0) {
        throw new RuntimeException("Expected 0, got " + idx0);
    }
}
Also used : GlyphVector(java.awt.font.GlyphVector) FontRenderContext(java.awt.font.FontRenderContext) Font(java.awt.Font)

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