Method for Kerning calculation

This commit is contained in:
Jindra Petk
2013-09-23 12:48:31 +02:00
parent 167534f477
commit 90e7d28232

View File

@@ -16,6 +16,14 @@
*/
package com.jpexs.decompiler.flash.types;
import java.awt.Font;
import java.awt.font.GlyphVector;
import java.awt.font.TextAttribute;
import java.text.AttributedCharacterIterator;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JPanel;
/**
* Represents 24-bit red, green, blue value
*
@@ -31,4 +39,61 @@ public class KERNINGRECORD {
public String toString() {
return "[KERNINGRECORD fontKerningCode1=" + fontKerningCode1 + ", fontKerningCode2=" + fontKerningCode2 + ", fontKerningAdjustment=" + fontKerningAdjustment + "]";
}
public KERNINGRECORD(int fontKerningCode1, int fontKerningCode2, int fontKerningAdjustment) {
this.fontKerningCode1 = fontKerningCode1;
this.fontKerningCode2 = fontKerningCode2;
this.fontKerningAdjustment = fontKerningAdjustment;
}
public KERNINGRECORD() {
}
public KERNINGRECORD(Font font, char char1, char char2) {
fontKerningCode1 = char1;
fontKerningCode2 = char2;
fontKerningAdjustment = KERNINGRECORD.getSystemFontKerning(font, char1, char2);
}
public KERNINGRECORD(String fontName, int fontStyle, int fontSize, char char1, char char2) {
fontKerningCode1 = char1;
fontKerningCode2 = char2;
fontKerningAdjustment = KERNINGRECORD.getSystemFontKerning(fontName, fontSize, fontStyle, char1, char2);
}
public static int getSystemFontKerning(Font font, char char1, char char2) {
return getSystemFontKerning(font.getFamily(), font.getSize(), font.getStyle(), char1, char2);
}
public static int getSystemFontKerning(String fontName, int fontSize, int fontStyle, char char1, char char2) {
char[] chars = new char[]{char1, char2};
Map<AttributedCharacterIterator.Attribute, Object> withKerningAttrs = new HashMap<>();
withKerningAttrs.put(TextAttribute.FAMILY, fontName);
if ((fontStyle & Font.BOLD) == Font.BOLD) {
withKerningAttrs.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
}
if ((fontStyle & Font.ITALIC) == Font.ITALIC) {
withKerningAttrs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
}
withKerningAttrs.put(TextAttribute.SIZE, (float) fontSize);
withKerningAttrs.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
Font withKerningFont = Font.getFont(withKerningAttrs);
GlyphVector withKerningVector = withKerningFont.layoutGlyphVector((new JPanel()).getFontMetrics(withKerningFont).getFontRenderContext(), chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT);
int withKerningX = withKerningVector.getGlyphLogicalBounds(1).getBounds().x;
Map<AttributedCharacterIterator.Attribute, Object> noKerningAttrs = new HashMap<>();
noKerningAttrs.put(TextAttribute.FAMILY, fontName);
if ((fontStyle & Font.BOLD) == Font.BOLD) {
noKerningAttrs.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
}
if ((fontStyle & Font.ITALIC) == Font.ITALIC) {
noKerningAttrs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
}
noKerningAttrs.put(TextAttribute.SIZE, (float) fontSize);
Font noKerningFont = Font.getFont(noKerningAttrs);
GlyphVector noKerningVector = noKerningFont.layoutGlyphVector((new JPanel()).getFontMetrics(noKerningFont).getFontRenderContext(), chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT);
int noKerningX = noKerningVector.getGlyphLogicalBounds(1).getBounds().x;
return withKerningX - noKerningX;
}
}