From 096b7d561c694ce40e4366ecaa0ebac38550a246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jindra=20Pet=C5=99=C3=ADk?= Date: Sun, 21 Mar 2021 18:45:51 +0100 Subject: [PATCH] PDF export - include system fonts when necessary --- .../flash/exporters/FrameExporter.java | 42 ++++++++++++++----- .../decompiler/flash/tags/base/FontTag.java | 15 +++++++ 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/exporters/FrameExporter.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/exporters/FrameExporter.java index 11679bcda..93d177457 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/exporters/FrameExporter.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/exporters/FrameExporter.java @@ -33,6 +33,7 @@ import com.jpexs.decompiler.flash.exporters.settings.FrameExportSettings; import com.jpexs.decompiler.flash.exporters.settings.SpriteExportSettings; import com.jpexs.decompiler.flash.exporters.shape.CanvasShapeExporter; import com.jpexs.decompiler.flash.helpers.BMPFile; +import com.jpexs.decompiler.flash.helpers.FontHelper; import com.jpexs.decompiler.flash.helpers.ImageHelper; import com.jpexs.decompiler.flash.tags.DefineEditTextTag; import com.jpexs.decompiler.flash.tags.DefineSpriteTag; @@ -496,8 +497,6 @@ public class FrameExporter { /*BufferedImage img = frameImages.next(); p.setSize(img.getWidth() + 10, img.getHeight() + 10);*/ - - int pos = 0; RECT rect = tim.displayRect; @@ -645,7 +644,7 @@ public class FrameExporter { } Matrix textMatrix = new Matrix(textTag.getTextMatrix()); - + Matrix mat0 = mat.concatenate(textMatrix); Matrix trans = mat0.preConcatenate(Matrix.getScaleInstance(1 / SWF.unitDivisor)); //trans = trans.preConcatenate(Matrix.getTranslateInstance(5, 5)); @@ -695,16 +694,37 @@ public class FrameExporter { if (existingFonts.containsKey(rec.fontId)) { g2.setExistingTtfFont(existingFonts.get(rec.fontId).deriveFont((float) textHeight)); } else { - FontExporter fe = new FontExporter(); - File tempFile; - try { - tempFile = File.createTempFile("ffdec_font_export_", ".ttf"); - fe.exportFont(font, FontExportMode.TTF, tempFile); + if (font.getCharacterCount() < 1) { + String fontName = font.getFontName(); + File fontFile = FontTag.fontNameToFile(fontName); + if (fontFile == null) { + fontFile = FontTag.fontNameToFile("Times New Roman"); + } + if (fontFile == null) { + fontFile = FontTag.fontNameToFile("Arial"); + } + if (fontFile == null) { + throw new RuntimeException("Font " + fontName + " not found in your system"); + } Font f = new Font("/MYFONT" + rec.fontId, font.getFontStyle(), textHeight); existingFonts.put(rec.fontId, f); - g2.setTtfFont(f, tempFile); - } catch (IOException ex) { - Logger.getLogger(FrameExporter.class.getName()).log(Level.SEVERE, null, ex); + try { + g2.setTtfFont(f, fontFile); + } catch (IOException ex) { + Logger.getLogger(FrameExporter.class.getName()).log(Level.SEVERE, null, ex); + } + } else { + FontExporter fe = new FontExporter(); + File tempFile; + try { + tempFile = File.createTempFile("ffdec_font_export_", ".ttf"); + fe.exportFont(font, FontExportMode.TTF, tempFile); + Font f = new Font("/MYFONT" + rec.fontId, font.getFontStyle(), textHeight); + existingFonts.put(rec.fontId, f); + g2.setTtfFont(f, tempFile); + } catch (IOException ex) { + Logger.getLogger(FrameExporter.class.getName()).log(Level.SEVERE, null, ex); + } } } 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 de036432f..b33186319 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 @@ -147,6 +147,8 @@ public abstract class FontTag extends DrawableTag implements AloneTag { private static Map> installedFontFilesByFamily; + private static Map installedFontFilesByName; + private static Map installedFontsByName; private static Map customFontToFile; @@ -159,6 +161,12 @@ public abstract class FontTag extends DrawableTag implements AloneTag { private static Map>> customFontKerningPairs; + public static File fontNameToFile(String fontName) { + if (installedFontFilesByName.containsKey(fontName)) { + return installedFontFilesByName.get(fontName); + } + return null; + } private static void ensureLoaded() { if (!firstLoaded) { @@ -344,6 +352,7 @@ public abstract class FontTag extends DrawableTag implements AloneTag { installedFontsByFamily = FontHelper.getInstalledFonts(); installedFontFilesByFamily = FontHelper.getInstalledFontFiles(); installedFontsByName = new HashMap<>(); + installedFontFilesByName = new HashMap<>(); customFontToFile = new HashMap<>(); customFontKerningPairs = new HashMap<>(); @@ -353,6 +362,12 @@ public abstract class FontTag extends DrawableTag implements AloneTag { } } + for (String fam : installedFontFilesByFamily.keySet()) { + for (String nam : installedFontFilesByFamily.get(fam).keySet()) { + installedFontFilesByName.put(nam, installedFontFilesByFamily.get(fam).get(nam)); + } + } + if (installedFontsByFamily.containsKey("Times New Roman")) { defaultFontName = "Times New Roman"; } else if (installedFontsByFamily.containsKey("Arial")) {