From cccfc2e14dfbe87cb285df729b3345d79cc41b02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jindra=20Pet=C5=99=C3=ADk?= Date: Mon, 7 Mar 2016 23:39:10 +0100 Subject: [PATCH] #1193 FLA export - correct font advance in some cases --- .../decompiler/flash/tags/base/FontTag.java | 3 ++- .../decompiler/flash/tags/base/TextTag.java | 25 +++++++++++++++---- .../decompiler/flash/xfl/XFLConverter.java | 5 +++- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/FontTag.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/FontTag.java index b1f094327..344a85864 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/FontTag.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/FontTag.java @@ -246,7 +246,8 @@ public abstract class FontTag extends CharacterTag implements AloneTag, Drawable } public static float getSystemFontAdvance(Font aFont, Character character, Character nextCharacter) { - GlyphVector gv = aFont.createGlyphVector(new FontRenderContext(aFont.getTransform(), true, true), "" + character + (nextCharacter == null ? "" : nextCharacter)); + String chars = "" + character + (nextCharacter == null ? "" : nextCharacter); + GlyphVector gv = aFont.layoutGlyphVector(new FontRenderContext(aFont.getTransform(), true, true), chars.toCharArray(), 0, chars.length(), Font.LAYOUT_LEFT_TO_RIGHT); GlyphMetrics gm = gv.getGlyphMetrics(0); return gm.getAdvanceX(); } diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/TextTag.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/TextTag.java index 9c47e26a3..012e3270b 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/TextTag.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/TextTag.java @@ -26,6 +26,7 @@ import com.jpexs.decompiler.flash.exporters.modes.FontExportMode; import com.jpexs.decompiler.flash.exporters.shape.BitmapExporter; import com.jpexs.decompiler.flash.exporters.shape.CanvasShapeExporter; import com.jpexs.decompiler.flash.exporters.shape.SVGShapeExporter; +import com.jpexs.decompiler.flash.helpers.FontHelper; import com.jpexs.decompiler.flash.helpers.HighlightedText; import com.jpexs.decompiler.flash.importers.TextImportResizeTextBoundsMode; import com.jpexs.decompiler.flash.tags.text.JustifyAlignGlyphEntry; @@ -60,6 +61,7 @@ import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.Shape; import java.awt.font.LineMetrics; +import java.awt.font.TextAttribute; import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.HashMap; @@ -276,6 +278,13 @@ public abstract class TextTag extends CharacterTag implements DrawableTag { if (!font.hasLayout()) { String fontName = FontTag.getFontNameWithFallback(font.getFontNameIntag()); aFont = new Font(fontName, font.getFontStyle(), (int) (textHeight / SWF.unitDivisor)); + + Map attr = new HashMap<>(); + + attr.put(TextAttribute.KERNING, TextAttribute.KERNING_ON); + attr.put(TextAttribute.LIGATURES, TextAttribute.LIGATURES_ON); + aFont = aFont.deriveFont(attr); + fontMetrics = graphics.getFontMetrics(aFont); LineMetrics lm = fontMetrics.getLineMetrics("A", graphics); ascent = lm.getAscent(); @@ -314,7 +323,7 @@ public abstract class TextTag extends CharacterTag implements DrawableTag { continue; } - int letterSpacing = 0; + int letterSpacing = Integer.MAX_VALUE; for (int e = 0; e < rec.glyphEntries.size(); e++) { GLYPHENTRY entry = rec.glyphEntries.get(e); GLYPHENTRY nextEntry = null; @@ -336,13 +345,19 @@ public abstract class TextTag extends CharacterTag implements DrawableTag { if (nextEntry != null) { kerningAdjustment = font.getGlyphKerningAdjustment(entry.glyphIndex, nextEntry.glyphIndex); } - defaultAdvance = (int) (Math.round(textHeight * (font.getGlyphAdvance(entry.glyphIndex) + kerningAdjustment) / 1024.0) / font.getDivider()); + defaultAdvance = (int) (Math.round(textHeight * (font.getGlyphAdvance(entry.glyphIndex) + kerningAdjustment) / (1024.0 * font.getDivider()))); } else { defaultAdvance = (int) Math.round(SWF.unitDivisor * FontTag.getSystemFontAdvance(aFont, font.glyphToChar(entry.glyphIndex), nextEntry == null ? null : font.glyphToChar(nextEntry.glyphIndex))); } - letterSpacing = adv - defaultAdvance; - - x += adv / (font.getDivider()); + int newLetterSpacing = adv - defaultAdvance; + if (e == 0 || e == rec.glyphEntries.size() - 1) { + if (rec.glyphEntries.size() == 1) { + letterSpacing = 0; + } + } else if (newLetterSpacing < letterSpacing) { + letterSpacing = newLetterSpacing; + } + x += adv; } allLetterSpacings.add(letterSpacing); } diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/xfl/XFLConverter.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/xfl/XFLConverter.java index dffe8cd8b..1831cfc67 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/xfl/XFLConverter.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/xfl/XFLConverter.java @@ -2473,8 +2473,11 @@ public class XFLConverter { ret.append(" instanceName=\"").append(Helper.escapeHTML(instanceName)).append("\""); } ret.append(antiAlias); + if (((CharacterTag) tag).getCharacterId() == 650) { + System.err.println("=========================AAAAAAAAAAA"); + } Map attrs = TextTag.getTextRecordsAttributes(textRecords, swf); - + System.err.println("///////////============="); ret.append(" width=\"").append(tag.getBounds().getWidth() / 2).append("\" height=\"").append(tag.getBounds().getHeight()).append("\" autoExpand=\"true\" isSelectable=\"false\">"); ret.append(matStr);