small refactorings

This commit is contained in:
2014-12-07 12:41:55 +01:00
parent 1e9da7fd40
commit cdae56d128
36 changed files with 154 additions and 108 deletions
@@ -118,14 +118,14 @@ public class FontHelper {
withKerningAttrs.put(TextAttribute.FONT, font);
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);
GlyphVector withKerningVector = withKerningFont.layoutGlyphVector(getFontRenderContext(withKerningFont), 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.FONT, font);
noKerningAttrs.put(TextAttribute.KERNING, 0);
Font noKerningFont = Font.getFont(noKerningAttrs);
GlyphVector noKerningVector = noKerningFont.layoutGlyphVector((new JPanel()).getFontMetrics(noKerningFont).getFontRenderContext(), chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT);
GlyphVector noKerningVector = noKerningFont.layoutGlyphVector(getFontRenderContext(noKerningFont), chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT);
int noKerningX = noKerningVector.getGlyphLogicalBounds(1).getBounds().x;
return withKerningX - noKerningX;
}
@@ -162,6 +162,18 @@ public class FontHelper {
return ret;
}
public static float getFontAdvance(Font font, char ch) {
return createGlyphVector(font, ch).getGlyphMetrics(0).getAdvanceX();
}
public static GlyphVector createGlyphVector(Font font, char ch) {
return font.createGlyphVector(getFontRenderContext(font), new char[]{ch});
}
private static FontRenderContext getFontRenderContext(Font font) {
return (new JPanel()).getFontMetrics(font).getFontRenderContext();
}
private static List<KerningPair> getFontKerningPairsOneChar(List<Character> availableChars, Font font, char firstChar) {
List<KerningPair> ret = new ArrayList<>();
@@ -177,7 +189,7 @@ public class FontHelper {
withKerningAttrs.put(TextAttribute.FONT, font);
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);
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;
@@ -187,7 +199,7 @@ public class FontHelper {
noKerningAttrs.put(TextAttribute.FONT, font);
noKerningAttrs.put(TextAttribute.KERNING, 0);
Font noKerningFont = Font.getFont(noKerningAttrs);
GlyphVector noKerningVector = noKerningFont.layoutGlyphVector((new JPanel()).getFontMetrics(noKerningFont).getFontRenderContext(), chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT);
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;
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2010-2014 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.
*/
package com.jpexs.decompiler.flash.helpers;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
/**
*
* @author JPEXS
*/
public class ImageHelper {
public static BufferedImage read(InputStream input) throws IOException {
return ImageIO.read(input);
}
public static void write(BufferedImage image, String formatName, OutputStream output) throws IOException {
ImageIO.write(image, formatName, output);
}
public static void write(BufferedImage image, String formatName, ByteArrayOutputStream output) {
try {
ImageIO.write(image, formatName, output);
} catch (IOException ex) {
Logger.getLogger(ImageHelper.class.getName()).log(Level.SEVERE, null, ex);
}
}
}