From 0005f73ae923d4c150168f52658f70ba9706ca66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jindra=20Pet=C5=99=C3=ADk?= Date: Sat, 14 Oct 2023 10:16:30 +0200 Subject: [PATCH] Fixed calculating needed fill/line bits for SHAPE, SHAPEWITHSTYLE --- .../decompiler/flash/SWFOutputStream.java | 42 +++++++++++++++---- .../decompiler/flash/tags/DefineFont2Tag.java | 4 +- .../decompiler/flash/tags/DefineFont3Tag.java | 4 +- .../decompiler/flash/tags/DefineFontTag.java | 4 +- .../flash/tags/DefineMorphShape2Tag.java | 4 +- .../flash/tags/DefineMorphShapeTag.java | 4 +- 6 files changed, 44 insertions(+), 18 deletions(-) diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/SWFOutputStream.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/SWFOutputStream.java index dd234ad99..46f8dd3ab 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/SWFOutputStream.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/SWFOutputStream.java @@ -1353,20 +1353,47 @@ public class SWFOutputStream extends OutputStream { /** * Writes SHAPE value to the stream * - * @param fillStyleCount - * @param lineStyleCount * @param value SHAPE value * @param shapeNum 1 in DefineShape, 2 in DefineShape2,... * @throws IOException */ - public void writeSHAPE(int fillStyleCount, int lineStyleCount, SHAPE value, int shapeNum) throws IOException { - value.numFillBits = getNeededBitsU(fillStyleCount); - value.numLineBits = getNeededBitsU(lineStyleCount); + public void writeSHAPE(SHAPE value, int shapeNum) throws IOException { + calculateSHAPEFillLineBits(value); writeUB(4, value.numFillBits); writeUB(4, value.numLineBits); writeSHAPERECORDS(value.shapeRecords, value.numFillBits, value.numLineBits, shapeNum); } - + + private void calculateSHAPEFillLineBits(SHAPE value) { + int numFillBits = 0; + int numLineBits = 0; + + for (SHAPERECORD r : value.shapeRecords) { + if (r instanceof StyleChangeRecord) { + StyleChangeRecord scr = (StyleChangeRecord) r; + if (scr.stateNewStyles) { + break; + } + if (scr.stateFillStyle0) { + numFillBits = Math.max(numFillBits, getNeededBitsU(scr.fillStyle0)); + } + if (scr.stateFillStyle1) { + numFillBits = Math.max(numFillBits, getNeededBitsU(scr.fillStyle1)); + } + if (scr.stateLineStyle) { + numLineBits = Math.max(numLineBits, getNeededBitsU(scr.lineStyle)); + } + } + } + if (Configuration._debugCopy.get()) { + numFillBits = Math.max(numFillBits, value.numFillBits); + numLineBits = Math.max(numLineBits, value.numLineBits); + } + + value.numFillBits = numFillBits; + value.numLineBits = numLineBits; + } + /** * Writes SHAPEWITHSTYLE value to the stream * @@ -1377,8 +1404,7 @@ public class SWFOutputStream extends OutputStream { public void writeSHAPEWITHSTYLE(SHAPEWITHSTYLE value, int shapeNum) throws IOException { writeFILLSTYLEARRAY(value.fillStyles, shapeNum); writeLINESTYLEARRAY(value.lineStyles, shapeNum); - value.numFillBits = getNeededBitsU(value.fillStyles.fillStyles.length); - value.numLineBits = getNeededBitsU(shapeNum <= 3 ? value.lineStyles.lineStyles.length : value.lineStyles.lineStyles2.length); + calculateSHAPEFillLineBits(value); writeUB(4, value.numFillBits); writeUB(4, value.numLineBits); writeSHAPERECORDS(value.shapeRecords, value.numFillBits, value.numLineBits, shapeNum); 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 ee1a0593d..a80427596 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 @@ -209,7 +209,7 @@ public class DefineFont2Tag extends FontTag { return; } try { - sos3.writeSHAPE(1, 0, glyphShapeTable.get(i), 1); + sos3.writeSHAPE(glyphShapeTable.get(i), 1); } catch (IOException ex) { //should not happen return; @@ -266,7 +266,7 @@ public class DefineFont2Tag extends FontTag { SWFOutputStream sos3 = new SWFOutputStream(baosGlyphShapes, getVersion(), getCharset()); for (int i = 0; i < numGlyphs; i++) { offsetTable.add((glyphShapeTable.size() + 1/*CodeTableOffset*/) * (fontFlagsWideOffsets ? 4 : 2) + sos3.getPos()); - sos3.writeSHAPE(1, 0, glyphShapeTable.get(i), 1); + sos3.writeSHAPE(glyphShapeTable.get(i), 1); } byte[] baGlyphShapes = baosGlyphShapes.toByteArray(); for (Long offset : offsetTable) { 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 c329bed13..e41ff7d67 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 @@ -196,7 +196,7 @@ public class DefineFont3Tag extends FontTag { return; } try { - sos3.writeSHAPE(1, 0, glyphShapeTable.get(i), 1); + sos3.writeSHAPE(glyphShapeTable.get(i), 1); } catch (IOException ex) { //should not happen return; @@ -239,7 +239,7 @@ public class DefineFont3Tag extends FontTag { int numGlyphs = glyphShapeTable.size(); for (int i = 0; i < numGlyphs; i++) { offsetTable.add(sos3.getPos()); - sos3.writeSHAPE(1, 0, glyphShapeTable.get(i), 1); + sos3.writeSHAPE(glyphShapeTable.get(i), 1); } byte[] baGlyphShapes = baosGlyphShapes.toByteArray(); 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 f5c0669c7..0f925a56f 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 @@ -114,7 +114,7 @@ public class DefineFontTag extends FontTag { SWFOutputStream sos2 = new SWFOutputStream(baos2, getVersion(), getCharset()); for (SHAPE shape : glyphShapeTable) { offsetTable.add(glyphShapeTable.size() * 2 + (int) sos2.getPos()); - sos2.writeSHAPE(1, 0, shape, 1); + sos2.writeSHAPE(shape, 1); } for (int offset : offsetTable) { sos.writeUI16(offset); @@ -323,7 +323,7 @@ public class DefineFontTag extends FontTag { return false; } try { - sos2.writeSHAPE(1, 0, shape, 1); + sos2.writeSHAPE(shape, 1); } catch (IOException ex) { //should not happen } diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineMorphShape2Tag.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineMorphShape2Tag.java index 9e8a38d66..c3b3fb594 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineMorphShape2Tag.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineMorphShape2Tag.java @@ -127,11 +127,11 @@ public class DefineMorphShape2Tag extends MorphShapeTag { SWFOutputStream sos2 = new SWFOutputStream(baos2, getVersion(), getCharset()); sos2.writeMORPHFILLSTYLEARRAY(morphFillStyles, 2); sos2.writeMORPHLINESTYLEARRAY(morphLineStyles, 2); - sos2.writeSHAPE(morphFillStyles.fillStyles.length, morphLineStyles.lineStyles2.length, startEdges, 2); + sos2.writeSHAPE(startEdges, 2); byte[] ba2 = baos2.toByteArray(); sos.writeUI32(ba2.length); sos.write(ba2); - sos.writeSHAPE(morphFillStyles.fillStyles.length, morphLineStyles.lineStyles2.length, endEdges, 2); + sos.writeSHAPE(endEdges, 2); } @Override diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineMorphShapeTag.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineMorphShapeTag.java index 2b4a2c0e1..c3af75766 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineMorphShapeTag.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineMorphShapeTag.java @@ -99,11 +99,11 @@ public class DefineMorphShapeTag extends MorphShapeTag { SWFOutputStream sos2 = new SWFOutputStream(baos2, getVersion(), getCharset()); sos2.writeMORPHFILLSTYLEARRAY(morphFillStyles, 1); sos2.writeMORPHLINESTYLEARRAY(morphLineStyles, 1); - sos2.writeSHAPE(morphFillStyles.fillStyles.length, morphLineStyles.lineStyles.length, startEdges, 1); + sos2.writeSHAPE(startEdges, 1); byte[] ba2 = baos2.toByteArray(); sos.writeUI32(ba2.length); sos.write(ba2); - sos.writeSHAPE(morphFillStyles.fillStyles.length, morphLineStyles.lineStyles.length, endEdges, 1); + sos.writeSHAPE(endEdges, 1); } @Override