Java examples for 2D Graphics:Font
get Font Kerning Pairs One Char
/*// w ww.j a v a2 s. co m * Copyright (C) 2010-2015 JPEXS, All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3.0 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. */ import java.awt.Font; import java.awt.FontFormatException; import java.awt.GraphicsEnvironment; import java.awt.font.FontRenderContext; import java.awt.font.GlyphVector; import java.awt.font.TextAttribute; import java.io.EOFException; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.text.AttributedCharacterIterator; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import javax.swing.JPanel; public class Main{ private static List<KerningPair> getFontKerningPairsOneChar( List<Character> availableChars, Font font, char firstChar) { List<KerningPair> ret = new ArrayList<>(); char[] chars = new char[availableChars.size() * 2]; for (int i = 0; i < availableChars.size(); i++) { chars[i * 2] = firstChar; chars[i * 2 + 1] = availableChars.get(i); } Map<AttributedCharacterIterator.Attribute, Object> withKerningAttrs = new HashMap<>(); withKerningAttrs.put(TextAttribute.FONT, font); withKerningAttrs.put(TextAttribute.KERNING, TextAttribute.KERNING_ON); Font withKerningFont = Font.getFont(withKerningAttrs); GlyphVector withKerningVector = withKerningFont.layoutGlyphVector( getFontRenderContext(withKerningFont), chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT); int withKerningX[] = new int[availableChars.size()]; for (int i = 0; i < availableChars.size(); i++) { withKerningX[i] = withKerningVector.getGlyphLogicalBounds( i * 2 + 1).getBounds().x; } Map<AttributedCharacterIterator.Attribute, Object> noKerningAttrs = new HashMap<>(); noKerningAttrs.put(TextAttribute.FONT, font); noKerningAttrs.put(TextAttribute.KERNING, 0); Font noKerningFont = Font.getFont(noKerningAttrs); GlyphVector noKerningVector = noKerningFont.layoutGlyphVector( getFontRenderContext(noKerningFont), chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT); for (int i = 0; i < availableChars.size(); i++) { int noKerningX = noKerningVector.getGlyphLogicalBounds( i * 2 + 1).getBounds().x; int kerning = withKerningX[i] - noKerningX; if (kerning > 0) { ret.add(new KerningPair(firstChar, availableChars.get(i), kerning)); } } return ret; } private static FontRenderContext getFontRenderContext(Font font) { return (new JPanel()).getFontMetrics(font).getFontRenderContext(); } }