Example #1
0
 /**
  * This deals with a bug/peculiarity for the default Mac font: several pixels of the ascent are
  * actually empty. This screws up certain measurements which assume the font is actually a few
  * pixels taller than it really is.
  */
 private static int getUnusedAscent(FontMetrics fm, Font font) {
   Integer value = ascentTable.get(font);
   if (value == null) {
     int recordedAscent = fm.getAscent();
     FontRenderContext frc = new FontRenderContext(new AffineTransform(), false, false);
     GlyphVector gv = font.createGlyphVector(frc, "XYZ");
     Rectangle2D bounds = ShapeBounds.getBounds(gv.getOutline());
     int observedAscent = (int) (Math.ceil(bounds.getHeight()) + .5);
     value = new Integer(recordedAscent - observedAscent);
     ascentTable.put(font, value);
   }
   return value.intValue();
 }
    private static BufferedImage createImage(String s, boolean valid) {
      FontRenderContext frc = new FontRenderContext(null, true, true);
      Font font = new Font("dialog", Font.BOLD, 12);

      GlyphVector glyphs = font.createGlyphVector(frc, s);
      Shape shape = glyphs.getOutline();
      Rectangle bounds = shape.getBounds();

      int imageW = bounds.width;
      int imageH = bounds.height;
      BufferedImage image = new BufferedImage(imageW, imageH, BufferedImage.TYPE_INT_ARGB);
      Graphics2D g = (Graphics2D) image.getGraphics();

      g.setColor(valid ? Color.blue : Color.red);
      g.translate(bounds.x, -bounds.y);
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
      g.fill(shape);

      return image;
    }