diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b8a2e2fb..9f621451a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ All notable changes to this project will be documented in this file. ## [Unreleased] ### Added -- #1561 Font editing - import kerning when adding characters +- #1561 Font editing - import ascent, descent, leading, kerning ### Fixed - #1623 Right side marker (gray line) in P-code diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/helpers/FontHelper.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/helpers/FontHelper.java index 8fb2b993f..2fef035b5 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/helpers/FontHelper.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/helpers/FontHelper.java @@ -28,6 +28,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.io.RandomAccessFile; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; @@ -38,6 +39,7 @@ import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.TreeMap; import java.util.function.BiPredicate; /** diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineFont2Tag.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineFont2Tag.java index ec53da337..7bc543f4e 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineFont2Tag.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineFont2Tag.java @@ -621,4 +621,41 @@ public class DefineFont2Tag extends FontTag { } return kerningAdjustment; } + + @Override + public void setAscent(int ascent) { + if (fontFlagsHasLayout) { + fontAscent = ascent; + } + } + + @Override + public void setDescent(int descent) { + if (fontFlagsHasLayout) { + fontDescent = descent; + } + } + + @Override + public void setLeading(int leading) { + if (fontFlagsHasLayout) { + fontLeading = leading; + } + } + + @Override + public void setHasLayout(boolean hasLayout) { + fontFlagsHasLayout = hasLayout; + if (hasLayout) { + if (fontAdvanceTable == null) { + fontAdvanceTable = new ArrayList<>(); + } + if (fontBoundsTable == null) { + fontBoundsTable = new ArrayList<>(); + } + if (fontKerningTable == null) { + fontKerningTable = new ArrayList<>(); + } + } + } } diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineFont3Tag.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineFont3Tag.java index 9fd6127e9..1d86419c2 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineFont3Tag.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineFont3Tag.java @@ -637,4 +637,42 @@ public class DefineFont3Tag extends FontTag { } return kerningAdjustment; } + + @Override + public void setAscent(int ascent) { + if (fontFlagsHasLayout) { + fontAscent = ascent; + } + } + + @Override + public void setDescent(int descent) { + if (fontFlagsHasLayout) { + fontDescent = descent; + } + } + + @Override + public void setLeading(int leading) { + if (fontFlagsHasLayout) { + fontLeading = leading; + } + } + + @Override + public void setHasLayout(boolean hasLayout) { + fontFlagsHasLayout = hasLayout; + + if (hasLayout) { + if (fontAdvanceTable == null) { + fontAdvanceTable = new ArrayList<>(); + } + if (fontBoundsTable == null) { + fontBoundsTable = new ArrayList<>(); + } + if (fontKerningTable == null) { + fontKerningTable = new ArrayList<>(); + } + } + } } diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineFontTag.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineFontTag.java index c60619083..441dc9325 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineFontTag.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineFontTag.java @@ -12,7 +12,8 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library. */ + * License along with this library. + */ package com.jpexs.decompiler.flash.tags; import com.jpexs.decompiler.flash.SWF; @@ -378,4 +379,20 @@ public class DefineFontTag extends FontTag { public int getCharKerningAdjustment(char c1, char c2) { return 0; } + + @Override + public void setAscent(int ascent) { + } + + @Override + public void setDescent(int descent) { + } + + @Override + public void setLeading(int leading) { + } + + public void setHasLayout(boolean hasLayout) { + + } } 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 2d430fbd6..05dcf30c9 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 @@ -32,6 +32,7 @@ import com.jpexs.decompiler.flash.types.SHAPE; import com.jpexs.decompiler.flash.types.TEXTRECORD; import com.jpexs.decompiler.flash.types.shaperecords.SHAPERECORD; import com.jpexs.helpers.ByteArrayRange; +import com.jpexs.helpers.Reference; import com.jpexs.helpers.SerializableImage; import java.awt.Color; import java.awt.Font; @@ -42,6 +43,7 @@ import java.awt.font.GlyphMetrics; import java.awt.font.GlyphVector; import java.awt.geom.Area; import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -109,6 +111,14 @@ public abstract class FontTag extends DrawableTag implements AloneTag { public abstract int getLeading(); + public abstract void setAscent(int ascent); + + public abstract void setDescent(int descent); + + public abstract void setLeading(int leading); + + public abstract void setHasLayout(boolean hasLayout); + public String getFontName() { DefineFontNameTag fontNameTag = getFontNameTag(); if (fontNameTag == null) { diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/gfx/DefineCompactedFont.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/gfx/DefineCompactedFont.java index 14f11a6d0..10d948bf9 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/gfx/DefineCompactedFont.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/gfx/DefineCompactedFont.java @@ -12,7 +12,8 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library. */ + * License along with this library. + */ package com.jpexs.decompiler.flash.tags.gfx; import com.jpexs.decompiler.flash.SWF; @@ -455,4 +456,23 @@ public final class DefineCompactedFont extends FontTag { public boolean hasLayout() { return true; } + + @Override + public void setAscent(int ascent) { + fonts.get(0).ascent = ascent; + } + + @Override + public void setDescent(int descent) { + fonts.get(0).descent = descent; + } + + @Override + public void setLeading(int leading) { + fonts.get(0).leading = leading; + } + + @Override + public void setHasLayout(boolean hasLayout) { + } } diff --git a/src/com/jpexs/decompiler/flash/gui/FontEmbedDialog.java b/src/com/jpexs/decompiler/flash/gui/FontEmbedDialog.java index c65c0679e..ab68b7c2e 100644 --- a/src/com/jpexs/decompiler/flash/gui/FontEmbedDialog.java +++ b/src/com/jpexs/decompiler/flash/gui/FontEmbedDialog.java @@ -21,6 +21,7 @@ import com.jpexs.decompiler.flash.tags.base.FontTag; import com.jpexs.decompiler.flash.tags.font.CharacterRanges; import com.jpexs.helpers.Helper; import java.awt.BorderLayout; +import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; @@ -80,6 +81,8 @@ public class FontEmbedDialog extends AppDialog { private final JCheckBox allCheckbox; + private final JCheckBox importAscentDescentLeadingCheckBox; + public Font getSelectedFont() { if (ttfFileRadio.isSelected() && customFont != null) { return customFont; @@ -87,6 +90,10 @@ public class FontEmbedDialog extends AppDialog { return ((FontFace) faceSelection.getSelectedItem()).font; } + public boolean isImportAscentDescentLeading() { + return importAscentDescentLeadingCheckBox.isSelected(); + } + public Set getSelectedChars() { Set chars = new TreeSet<>(); Font f = getSelectedFont(); @@ -128,7 +135,7 @@ public class FontEmbedDialog extends AppDialog { faceSelection.setModel(FontPanel.getFaceModel((FontFamily) familyNamesSelection.getSelectedItem())); } - public FontEmbedDialog(FontFace selectedFace, String selectedChars) { + public FontEmbedDialog(boolean hasLayout, FontFace selectedFace, String selectedChars) { setSize(900, 600); setDefaultCloseOperation(HIDE_ON_CLOSE); setTitle(translate("dialog.title")); @@ -248,6 +255,12 @@ public class FontEmbedDialog extends AppDialog { cnt.add(specialPanel); cnt.add(individialSample); + importAscentDescentLeadingCheckBox = new JCheckBox(translate("ascentdescentleading")); + importAscentDescentLeadingCheckBox.setAlignmentX(Component.CENTER_ALIGNMENT); + if (hasLayout) { + cnt.add(importAscentDescentLeadingCheckBox); + } + JPanel buttonsPanel = new JPanel(new FlowLayout()); JButton okButton = new JButton(AppStrings.translate("button.ok")); okButton.addActionListener(this::okButtonActionPerformed); diff --git a/src/com/jpexs/decompiler/flash/gui/FontPanel.java b/src/com/jpexs/decompiler/flash/gui/FontPanel.java index 633e695a0..6ca9bffc8 100644 --- a/src/com/jpexs/decompiler/flash/gui/FontPanel.java +++ b/src/com/jpexs/decompiler/flash/gui/FontPanel.java @@ -24,11 +24,13 @@ import com.jpexs.decompiler.flash.tags.base.FontTag; import com.jpexs.decompiler.flash.tags.base.TextTag; import com.jpexs.decompiler.flash.treeitems.TreeItem; import com.jpexs.helpers.Helper; +import com.jpexs.helpers.Reference; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.FontFormatException; +import java.awt.FontMetrics; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ComponentAdapter; @@ -542,15 +544,29 @@ public class FontPanel extends JPanel { TreeItem item = mainPanel.tagTree.getCurrentTreeItem(); if (item instanceof FontTag) { FontTag ft = (FontTag) item; - FontEmbedDialog fed = new FontEmbedDialog((FontFace) fontFaceSelection.getSelectedItem(), fontAddCharactersField.getText()); + FontEmbedDialog fed = new FontEmbedDialog(ft.hasLayout() || ft.getCharacterCount() == 0, (FontFace) fontFaceSelection.getSelectedItem(), fontAddCharactersField.getText()); if (fed.showDialog() == AppDialog.OK_OPTION) { Set selChars = fed.getSelectedChars(); - if (!selChars.isEmpty()) { + if (!selChars.isEmpty() || fed.isImportAscentDescentLeading()) { + if (ft.getCharacterCount() == 0) { + ft.setHasLayout(true); + } Font selFont = fed.getSelectedFont(); fontFamilyNameSelection.setSelectedItem(new FontFamily(selFont)); fontFaceSelection.setSelectedItem(new FontFace(selFont)); fontAddChars(ft, selChars, selFont); + if (fed.isImportAscentDescentLeading()) { + Font adlFont = selFont; + if (selFont.getSize() != 1024) { + adlFont = selFont.deriveFont(1024f); + } + ft.setAscent((int) (ft.getDivider() * this.getFontMetrics(adlFont).getAscent())); + ft.setDescent((int) (ft.getDivider() * this.getFontMetrics(adlFont).getDescent())); + int leading = this.getFontMetrics(adlFont).getAscent() + this.getFontMetrics(adlFont).getDescent() - 1024; + ft.setLeading((int) (ft.getDivider() * leading)); + } fontAddCharactersField.setText(""); + ft.setModified(true); mainPanel.reload(true); } } diff --git a/src/com/jpexs/decompiler/flash/gui/locales/FontEmbedDialog.properties b/src/com/jpexs/decompiler/flash/gui/locales/FontEmbedDialog.properties index 196cd3409..236cf6d6c 100644 --- a/src/com/jpexs/decompiler/flash/gui/locales/FontEmbedDialog.properties +++ b/src/com/jpexs/decompiler/flash/gui/locales/FontEmbedDialog.properties @@ -24,3 +24,4 @@ installed = Installed: ttffile.noselection = TTF file: