Search in sources :

Example 6 with TextAttribute

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

the class OSXLigatureTest method main.

public static void main(String[] args) {
    if (!System.getProperty("os.name").startsWith("Mac")) {
        return;
    }
    String ligStr = "ffi";
    int w = 50, h = 50;
    BufferedImage bi1 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D bi1Graphics = bi1.createGraphics();
    bi1Graphics.setColor(Color.white);
    bi1Graphics.fillRect(0, 0, w, h);
    bi1Graphics.setColor(Color.black);
    Font noLigFont = new Font("Gill Sans", Font.PLAIN, 30);
    bi1Graphics.setFont(noLigFont);
    bi1Graphics.drawString(ligStr, 10, 40);
    BufferedImage bi2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D bi2Graphics = bi2.createGraphics();
    bi2Graphics.setColor(Color.white);
    bi2Graphics.fillRect(0, 0, w, h);
    bi2Graphics.setColor(Color.black);
    Map<TextAttribute, Object> attributes = new HashMap<>();
    attributes.put(TextAttribute.LIGATURES, TextAttribute.LIGATURES_ON);
    Font ligFont = noLigFont.deriveFont(attributes);
    bi2Graphics.setFont(ligFont);
    bi2Graphics.drawString(ligStr, 10, 40);
    boolean same = true;
    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            int c1 = bi1.getRGB(x, y);
            int c2 = bi2.getRGB(x, y);
            same &= (c1 == c2);
        }
        if (!same) {
            break;
        }
    }
    if (same) {
        throw new RuntimeException("Images do not differ - no ligature");
    }
}
Also used : HashMap(java.util.HashMap) TextAttribute(java.awt.font.TextAttribute) BufferedImage(java.awt.image.BufferedImage) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D)

Aggregations

TextAttribute (java.awt.font.TextAttribute)6 Font (java.awt.Font)3 Paint (java.awt.Paint)2 AffineTransform (java.awt.geom.AffineTransform)2 HashMap (java.util.HashMap)2 FontMetrics (java.awt.FontMetrics)1 Graphics2D (java.awt.Graphics2D)1 MouseEvent (java.awt.event.MouseEvent)1 FontRenderContext (java.awt.font.FontRenderContext)1 GlyphVector (java.awt.font.GlyphVector)1 LineMetrics (java.awt.font.LineMetrics)1 Point2D (java.awt.geom.Point2D)1 Rectangle2D (java.awt.geom.Rectangle2D)1 RoundRectangle2D (java.awt.geom.RoundRectangle2D)1 BufferedImage (java.awt.image.BufferedImage)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Hashtable (java.util.Hashtable)1 JLabel (javax.swing.JLabel)1 MouseInputAdapter (javax.swing.event.MouseInputAdapter)1