Fixed calculating needed fill/line bits for SHAPE, SHAPEWITHSTYLE

This commit is contained in:
Jindra Petřík
2023-10-16 09:36:06 +02:00
parent ae63d3c527
commit 0005f73ae9
6 changed files with 44 additions and 18 deletions
@@ -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);
@@ -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) {
@@ -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();
@@ -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
}
@@ -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
@@ -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