Canvas export - character type names, better image handling, indentation, faster library generation

This commit is contained in:
Jindra Petk
2014-04-26 12:39:08 +02:00
parent 824a3d2f96
commit 71a108cf61
6 changed files with 149 additions and 165 deletions

View File

@@ -99,6 +99,8 @@ import com.jpexs.decompiler.flash.tags.base.CharacterTag;
import com.jpexs.decompiler.flash.tags.base.Container;
import com.jpexs.decompiler.flash.tags.base.ContainerItem;
import com.jpexs.decompiler.flash.tags.base.DrawableTag;
import com.jpexs.decompiler.flash.tags.base.FontTag;
import com.jpexs.decompiler.flash.tags.base.ImageTag;
import com.jpexs.decompiler.flash.tags.base.MorphShapeTag;
import com.jpexs.decompiler.flash.tags.base.PlaceObjectTypeTag;
import com.jpexs.decompiler.flash.tags.base.RemoveTag;
@@ -160,6 +162,7 @@ import java.util.EmptyStackException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
@@ -178,6 +181,7 @@ import java.util.zip.InflaterInputStream;
import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageOutputStream;
import javax.imageio.stream.ImageOutputStream;
import javax.xml.bind.DatatypeConverter;
import net.kroo.elliot.GifSequenceWriter;
import org.monte.media.VideoFormatKeys;
import org.monte.media.avi.AVIWriter;
@@ -1337,6 +1341,28 @@ public final class SWF implements TreeItem, Timelined {
writer.close();
}
}
private static String getTypePrefix(CharacterTag c){
if(c instanceof ShapeTag){
return "shape";
}
if(c instanceof MorphShapeTag){
return "morphshape";
}
if(c instanceof DefineSpriteTag){
return "sprite";
}
if(c instanceof TextTag){
return "text";
}
if(c instanceof FontTag){
return "font";
}
if(c instanceof ImageTag){
return "image";
}
return "character";
}
public List<File> exportFrames(AbortRetryIgnoreHandler handler, String outdir, int containerId, List<Integer> frames, final FramesExportSettings settings) throws IOException {
final List<File> ret = new ArrayList<>();
@@ -1408,6 +1434,7 @@ public final class SWF implements TreeItem, Timelined {
if (settings.mode == FramesExportMode.CANVAS) {
final Timeline ftim = tim;
final Color fbackgroundColor = backgroundColor;
final SWF fswf = this;
new RetryTask(new RunnableIOEx() {
@Override
public void run() throws IOException {
@@ -1417,15 +1444,43 @@ public final class SWF implements TreeItem, Timelined {
int height = (int) (ftim.displayRect.getHeight() / SWF.unitDivisor);
try (FileOutputStream fos = new FileOutputStream(f)) {
fos.write(Utf8Helper.getBytes(CanvasShapeExporter.getJsPrefix()));
fos.write(Utf8Helper.getBytes("var frames = [];"));
Map<Integer, String> library = new HashMap<>();
framesToHtmlCanvas(SWF.unitDivisor, library, null, ftim, fframes, 0, null, 0, ftim.displayRect, new ColorTransform(), fbackgroundColor, 0);
Set<Integer> library = new HashSet<>();
getNeededCharacters(timeline, fframes, library);
for (int c : library.keySet()) {
fos.write(Utf8Helper.getBytes("function character" + c + "(ctx,frame,ratio){\r\n"));
fos.write(Utf8Helper.getBytes(library.get(c)));
fos.write(Utf8Helper.getBytes("}\r\n"));
for (int c : library) {
CharacterTag ch = fswf.characters.get(c);
if(ch instanceof ImageTag){
ImageTag image=(ImageTag)ch;
String format = image.getImageFormat();
InputStream imageStream = image.getImageData();
byte[] imageData;
if (imageStream != null) {
imageData = Helper.readStream(image.getImageData());
} else {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(image.getImage().getBufferedImage(), format.toUpperCase(Locale.ENGLISH), baos);
} catch (IOException ex) {
}
imageData = baos.toByteArray();
}
String base64ImgData = DatatypeConverter.printBase64Binary(imageData);
fos.write(Utf8Helper.getBytes("var image"+c+" = document.createElement(\"img\");\r\nimage"+c+".src=\"data:image/" + format + ";base64," + base64ImgData + "\";\r\n"));
}else{
fos.write(Utf8Helper.getBytes("function " + getTypePrefix(ch)+c + "(ctx,frame,ratio){\r\n"));
if(ch instanceof DrawableTag){
fos.write(Utf8Helper.getBytes(((DrawableTag)ch).toHtmlCanvas(1)));
}
fos.write(Utf8Helper.getBytes("}\r\n"));
}
}
String currentName = timeline.id == 0?"main":getTypePrefix(fswf.characters.get(timeline.id))+timeline.id;
fos.write(Utf8Helper.getBytes("function "+currentName + "(ctx,frame,ratio){\r\n"));
fos.write(Utf8Helper.getBytes(framesToHtmlCanvas(unitDivisor, ftim, fframes, 0, null, 0, displayRect, new ColorTransform(), fbackgroundColor)));
fos.write(Utf8Helper.getBytes("}\r\n"));
fos.write(Utf8Helper.getBytes("var frame = 0;\r\n"));
fos.write(Utf8Helper.getBytes("var frames = [];\r\n"));
@@ -1435,18 +1490,18 @@ public final class SWF implements TreeItem, Timelined {
fos.write(Utf8Helper.getBytes("function nextFrame(ctx){\r\n"));
fos.write(Utf8Helper.getBytes("\tctx.clearRect(0,0," + width + "," + height + ");\r\n"));
fos.write(Utf8Helper.getBytes("\tframe = (frame+1)%frames.length;\r\n"));
fos.write(Utf8Helper.getBytes("\tcharacter" + timeline.id + "(ctx,frames[frame],0);\r\n"));
fos.write(Utf8Helper.getBytes("\t"+currentName + "(ctx,frames[frame],0);\r\n"));
fos.write(Utf8Helper.getBytes("}\r\n"));
fos.write(Utf8Helper.getBytes("window.setInterval(function(){nextFrame(ctx)}," + (int) (1000.0 / timeline.swf.frameRate) + ");\r\n"));
}
/* if (Configuration.packJavaScripts.get()) {
if (Configuration.packJavaScripts.get()) {
JPacker.main(new String[]{"-q", "-b", "62", "-o", fmin.getAbsolutePath(), f.getAbsolutePath()});
f.delete();
} else {*/
} else {
f.renameTo(fmin);
//}
}
File fh = new File(foutdir + File.separator + "frames.html");
try (FileOutputStream fos = new FileOutputStream(fh); FileInputStream fis = new FileInputStream(fmin)) {
@@ -1456,7 +1511,9 @@ public final class SWF implements TreeItem, Timelined {
while ((cnt = fis.read(buf)) > 0) {
fos.write(buf, 0, cnt);
}
fos.write(Utf8Helper.getBytes(";"));
if(Configuration.packJavaScripts.get()){
fos.write(Utf8Helper.getBytes(";"));
}
fos.write(Utf8Helper.getBytes(CanvasShapeExporter.getHtmlSuffix()));
}
fmin.delete();
@@ -2090,7 +2147,7 @@ public final class SWF implements TreeItem, Timelined {
return ret;
}
private static void getUsedRatios(Timeline timeline, List<Integer> frames, Map<Integer, List<Integer>> usedRatios, HashSet<CharacterTag> processedCharacterIds) {
private static void getNeededCharacters(Timeline timeline, List<Integer> frames, Set<Integer> usedCharacters) {
if (frames == null) {
frames = new ArrayList<>();
for (int i = 0; i < timeline.getFrameCount(); i++) {
@@ -2103,30 +2160,17 @@ public final class SWF implements TreeItem, Timelined {
DepthState layer = frameObj.layers.get(depth);
if (!timeline.swf.characters.containsKey(layer.characterId)) {
continue;
}
if (layer.ratio > -1) {
List<Integer> usedRatiosForCharacter = usedRatios.get(layer.characterId);
if (usedRatiosForCharacter == null) {
usedRatiosForCharacter = new ArrayList<>();
usedRatios.put(layer.characterId, usedRatiosForCharacter);
}
}
}
usedCharacters.add(layer.characterId);
CharacterTag character = timeline.swf.characters.get(layer.characterId);
if (character instanceof DefineSpriteTag && !processedCharacterIds.contains(character)) {
processedCharacterIds.add(character);
DefineSpriteTag sp = (DefineSpriteTag) character;
getUsedRatios(sp.getTimeline(), null, usedRatios, processedCharacterIds);
}
usedCharacters.add(layer.characterId);
usedCharacters.addAll(character.getNeededCharacters());
}
}
}
public static String framesToHtmlCanvas(double unitDivisor, Map<Integer, String> library, Map<Integer, List<Integer>> usedRatios, Timeline timeline, List<Integer> frames, int time, DepthState stateUnderCursor, int mouseButton, RECT displayRect, ColorTransform colorTransform, Color backGroundColor, int level) throws IOException {
if (usedRatios == null) {
usedRatios = new HashMap<>();
getUsedRatios(timeline, frames, usedRatios, new HashSet<CharacterTag>());
}
public static String framesToHtmlCanvas(double unitDivisor, Timeline timeline, List<Integer> frames, int time, DepthState stateUnderCursor, int mouseButton, RECT displayRect, ColorTransform colorTransform, Color backGroundColor) {
StringBuilder sb = new StringBuilder();
if (frames == null) {
frames = new ArrayList<>();
@@ -2171,53 +2215,8 @@ public final class SWF implements TreeItem, Timelined {
DefineSpriteTag sp = (DefineSpriteTag) character;
Timeline tim = sp.getTimeline();
f = layer.time % tim.getFrameCount();
}
if (!library.containsKey(layer.characterId)) {
if (character instanceof DefineSpriteTag) {
DefineSpriteTag sp = (DefineSpriteTag) character;
Timeline tim = sp.getTimeline();
framesToHtmlCanvas(1, library, usedRatios, tim, null, 0, stateUnderCursor, mouseButton, displayRect, colorTransform, backGroundColor, level + 1);
//placeMatrix = new Matrix();
charStr = null;
} else {
if (character instanceof DrawableTag) {
charStr = ((DrawableTag) character).toHtmlCanvas(1);
}
}
/*if (character instanceof ShapeTag) {
ShapeTag sh = (ShapeTag) character;
CanvasShapeExporter cse = new CanvasShapeExporter(1,timeline.swf, sh.getShapes(), colorTransform, 0, 0);
cse.export();
charStr = cse.getShapeData();
} else if (character instanceof DefineSpriteTag) {
DefineSpriteTag sp = (DefineSpriteTag) character;
Timeline tim = sp.getTimeline();
framesToHtmlCanvas(1, library, usedRatios, tim, null, 0, stateUnderCursor, mouseButton, displayRect, colorTransform, backGroundColor, level + 1);
//placeMatrix = new Matrix();
charStr = null;
} else if (character instanceof MorphShapeTag){
MorphShapeTag ms=(MorphShapeTag)character;
StringBuilder mb=new StringBuilder();
if(usedRatios.containsKey(layer.characterId)){
for(int r:usedRatios.get(layer.characterId)){
mb.append("\tif(ratio == ").append(r).append("){\r\n");
CanvasShapeExporter cse = new CanvasShapeExporter(1,timeline.swf, ms.getShapeAtRatio(r), colorTransform, 0, 0);
cse.export();
mb.append("\t\t"+cse.getShapeData().replaceAll("\n(.)", "\n\t\t$1"));
mb.append("\t}\r\n");
}
}
charStr = mb.toString();
}*/
if (charStr != null) {
library.put(layer.characterId, charStr);
}
}
if (library.containsKey(layer.characterId)) {
sb.append("\t\t\tctx.save();\r\n");
}
sb.append("\t\t\tctx.save();\r\n");
sb.append("\t\t\tctx.transform(").append(placeMatrix.scaleX).append(",");
sb.append(placeMatrix.rotateSkew0).append(",");
sb.append(placeMatrix.rotateSkew1).append(",");
@@ -2225,15 +2224,13 @@ public final class SWF implements TreeItem, Timelined {
sb.append(placeMatrix.translateX).append(",");
sb.append(placeMatrix.translateY);
sb.append(");\r\n");
sb.append("\t\t\tcharacter").append(layer.characterId).append("(ctx,").append(f).append(",").append(layer.ratio).append(");\r\n");
sb.append("\t\t\t").append(getTypePrefix(character)).append(layer.characterId).append("(ctx,").append(f).append(",").append(layer.ratio).append(");\r\n");
sb.append("\t\t\tctx.restore();\r\n");
}
}
sb.append("\t\t\tbreak;\r\n");
}
sb.append("\t}\r\n");
library.put(timeline.id, sb.toString());
return null;
return sb.toString();
}
public static void frameToSvg(Timeline timeline, int frame, int time, DepthState stateUnderCursor, int mouseButton, SVGExporter exporter, ColorTransform colorTransform, int level) throws IOException {

View File

@@ -135,7 +135,7 @@ public class CanvasMorphShapeExporter extends MorphShapeExporterBase {
@Override
public void beginFill(RGB color, RGB colorEnd) {
finalizePath();
fillData += "ctx.fillStyle="+useRatioColor(color,colorEnd)+";\r\n";
fillData += "\tctx.fillStyle="+useRatioColor(color,colorEnd)+";\r\n";
}
@Override
@@ -163,7 +163,7 @@ public class CanvasMorphShapeExporter extends MorphShapeExporterBase {
end2.x += deltaX;
end2.y += deltaY;
fillData += "var grd=ctx.createLinearGradient(" + useRatioPos(start.x, start2.x) + "," + useRatioPos(start.y, start2.y) + "," + useRatioPos(end.x, end2.x) + "," + useRatioPos(end.y, end2.y) + ");\r\n";
fillData += "\tvar grd=ctx.createLinearGradient(" + useRatioPos(start.x, start2.x) + "," + useRatioPos(start.y, start2.y) + "," + useRatioPos(end.x, end2.x) + "," + useRatioPos(end.y, end2.y) + ");\r\n";
} else {
matrix.translateX /= unitDivisor;
matrix.translateY /= unitDivisor;
@@ -189,7 +189,7 @@ public class CanvasMorphShapeExporter extends MorphShapeExporterBase {
fillMatrixEnd = matrixEnd;
fillData += "var grd=ctx.createRadialGradient("+focalPointRatio*16384+",0,0,0,0," + (16384 + 32768 * repeatCnt) + ");\r\n";
fillData += "\tvar grd=ctx.createRadialGradient("+focalPointRatio*16384+",0,0,0,0," + (16384 + 32768 * repeatCnt) + ");\r\n";
}
int repeatTotal = repeatCnt * 2 + 1;
double oneHeight = 1.0 / repeatTotal;
@@ -205,16 +205,16 @@ public class CanvasMorphShapeExporter extends MorphShapeExporterBase {
for(int j=0;j<gradientRecords.length;j++){
GRADRECORD r = gradientRecords[j];
GRADRECORD r2 = gradientRecordsEnd[j];
fillData += "var s = "+useRatioDouble(
fillData += "\tvar s = "+useRatioDouble(
pos + (oneHeight * (revert ? 255 - r.ratio : r.ratio) / 255.0),
pos + (oneHeight * (revert ? 255 - r2.ratio : r2.ratio) / 255.0)
)+";\r\nif(s<0) s = 0;\r\nif(s>1) s = 1;\r\n";
fillData += "grd.addColorStop(s," + useRatioColor(r.color,r2.color) + ");\r\n";
)+";\r\n\tif(s<0) s = 0;\r\n\tif(s>1) s = 1;\r\n";
fillData += "\tgrd.addColorStop(s," + useRatioColor(r.color,r2.color) + ");\r\n";
lastRadColor = useRatioColor(r.color,r2.color);
}
pos += oneHeight;
}
fillData += "ctx.fillStyle = grd;\r\n";
fillData += "\tctx.fillStyle = grd;\r\n";
}
@Override
@@ -247,7 +247,7 @@ public class CanvasMorphShapeExporter extends MorphShapeExporterBase {
}
imageData = baos.toByteArray();
}
String base64ImgData = DatatypeConverter.printBase64Binary(imageData);
//String base64ImgData = DatatypeConverter.printBase64Binary(imageData);
if (matrix != null) {
fillMatrix = matrix.clone();
fillMatrix.translateX /= unitDivisor;
@@ -272,9 +272,9 @@ public class CanvasMorphShapeExporter extends MorphShapeExporterBase {
}
fillData += "var img = document.createElement(\"img\");\r\nimg.src=\"data:image/" + format + ";base64," + base64ImgData + "\";\r\n";
fillData += "var pat=ctx.createPattern(img,\"repeat\");\r\n";
fillData += "ctx.fillStyle = pat;\r\n";
//fillData += "\tvar img = document.createElement(\"img\");\r\nimg.src=\"data:image/" + format + ";base64," + base64ImgData + "\";\r\n";
fillData += "\tvar pat=ctx.createPattern(image"+bitmapId+",\"repeat\");\r\n";
fillData += "\tctx.fillStyle = pat;\r\n";
}
}
}
@@ -290,29 +290,29 @@ public class CanvasMorphShapeExporter extends MorphShapeExporterBase {
thickness /= unitDivisor;
thicknessEnd /= unitDivisor;
strokeData += "ctx.strokeStyle=" + useRatioColor(color,colorEnd) + ";\r\n";
strokeData += "ctx.lineWidth="+useRatioDouble(thickness,thicknessEnd)+";\r\n";
strokeData += "\tctx.strokeStyle=" + useRatioColor(color,colorEnd) + ";\r\n";
strokeData += "\tctx.lineWidth="+useRatioDouble(thickness,thicknessEnd)+";\r\n";
switch (startCaps) {
case LINESTYLE2.NO_CAP:
strokeData += "ctx.lineCap=\"butt\";\r\n";
strokeData += "\tctx.lineCap=\"butt\";\r\n";
break;
case LINESTYLE2.SQUARE_CAP:
strokeData += "ctx.lineCap=\"square\";\r\n";
strokeData += "\tctx.lineCap=\"square\";\r\n";
break;
default:
strokeData += "ctx.lineCap=\"round\";\r\n";
strokeData += "\tctx.lineCap=\"round\";\r\n";
break;
}
switch (joints) {
case LINESTYLE2.BEVEL_JOIN:
strokeData += "ctx.lineJoin=\"bevel\";\r\n";
strokeData += "\tctx.lineJoin=\"bevel\";\r\n";
break;
case LINESTYLE2.ROUND_JOIN:
strokeData += "ctx.lineJoin=\"round\";\r\n";
strokeData += "\tctx.lineJoin=\"round\";\r\n";
break;
default:
strokeData += "ctx.lineJoin=\"miter\";\r\n";
strokeData += "ctx.miterLimit=" + Integer.toString(miterLimit) + ";\r\n";
strokeData += "\tctx.lineJoin=\"miter\";\r\n";
strokeData += "\tctx.miterLimit=" + Integer.toString(miterLimit) + ";\r\n";
break;
}
}
@@ -328,7 +328,7 @@ public class CanvasMorphShapeExporter extends MorphShapeExporterBase {
y += deltaY;
x2 += deltaX;
y2 += deltaY;
pathData += "ctx.moveTo("
pathData += "\tctx.moveTo("
+ useRatioPos(x,x2) + ","
+ useRatioPos(y,y2) + ");\r\n";
}
@@ -339,7 +339,7 @@ public class CanvasMorphShapeExporter extends MorphShapeExporterBase {
y += deltaY;
x2 += deltaX;
y2 += deltaY;
pathData += "ctx.lineTo("
pathData += "\tctx.lineTo("
+ useRatioPos(x,x2) + ","
+ useRatioPos(y,y2) + ");\r\n";
}
@@ -357,7 +357,7 @@ public class CanvasMorphShapeExporter extends MorphShapeExporterBase {
anchorY2 += deltaY;
pathData += "ctx.quadraticCurveTo(" + useRatioPos(controlX,controlX2) + ","
pathData += "\tctx.quadraticCurveTo(" + useRatioPos(controlX,controlX2) + ","
+ useRatioPos(controlY,controlY2) + ","
+ useRatioPos(anchorX,anchorX2) + ","
+ useRatioPos(anchorY,anchorY2) + ");\r\n";
@@ -365,28 +365,27 @@ public class CanvasMorphShapeExporter extends MorphShapeExporterBase {
protected void finalizePath() {
if (!"".equals(pathData)) {
pathData = "ctx.beginPath();\r\n" + pathData + "ctx.closePath();\r\n" + strokeData;
pathData = "\tctx.beginPath();\r\n" + pathData + "\tctx.closePath();\r\n" + strokeData;
if (fillMatrix != null) {
if (lastRadColor != null) {
pathData += "ctx.fillStyle=" + lastRadColor + ";\r\n ctx.fill(\"evenodd\");\r\n";
pathData += "\tctx.fillStyle=" + lastRadColor + ";\r\n\tctx.fill(\"evenodd\");\r\n";
}
pathData += "ctx.save();\r\n";
pathData += "ctx.clip();\r\n";
pathData += "ctx.transform(" + useRatioDouble(fillMatrix.scaleX,fillMatrixEnd.scaleX) + "," + useRatioDouble(fillMatrix.rotateSkew0,fillMatrixEnd.rotateSkew0) + "," + useRatioDouble(fillMatrix.rotateSkew1,fillMatrixEnd.rotateSkew1) + "," + useRatioDouble(fillMatrix.scaleY,fillMatrixEnd.scaleY) + "," + useRatioDouble(fillMatrix.translateX,fillMatrixEnd.translateX) + "," + useRatioDouble(fillMatrix.translateY,fillMatrixEnd.translateY) + ");\r\n";
pathData += "console.log('m:'+("+useRatioDouble(fillMatrix.scaleX,fillMatrixEnd.scaleX)+")+','+("+useRatioDouble(fillMatrix.scaleY,fillMatrixEnd.scaleY)+")+','+("+useRatioDouble(fillMatrix.rotateSkew0,fillMatrixEnd.rotateSkew0)+")+','+("+useRatioDouble(fillMatrix.rotateSkew1,fillMatrixEnd.rotateSkew1)+"));";
pathData += "\tctx.save();\r\n";
pathData += "\tctx.clip();\r\n";
pathData += "\tctx.transform(" + useRatioDouble(fillMatrix.scaleX,fillMatrixEnd.scaleX) + "," + useRatioDouble(fillMatrix.rotateSkew0,fillMatrixEnd.rotateSkew0) + "," + useRatioDouble(fillMatrix.rotateSkew1,fillMatrixEnd.rotateSkew1) + "," + useRatioDouble(fillMatrix.scaleY,fillMatrixEnd.scaleY) + "," + useRatioDouble(fillMatrix.translateX,fillMatrixEnd.translateX) + "," + useRatioDouble(fillMatrix.translateY,fillMatrixEnd.translateY) + ");\r\n";
pathData += fillData;
pathData += "ctx.fillRect(" + (-16384 - 32768 * repeatCnt) + "," + (-16384 - 32768 * repeatCnt) + "," + (2 * 16384 + 32768 * 2 * repeatCnt) + "," + (2 * 16384 + 32768 * 2 * repeatCnt) + ");\r\n";
pathData += "ctx.restore();\r\n";
pathData += "\tctx.fillRect(" + (-16384 - 32768 * repeatCnt) + "," + (-16384 - 32768 * repeatCnt) + "," + (2 * 16384 + 32768 * 2 * repeatCnt) + "," + (2 * 16384 + 32768 * 2 * repeatCnt) + ");\r\n";
pathData += "\tctx.restore();\r\n";
shapeData += pathData;
} else {
if (!"".equals(fillData)) {
pathData += "ctx.fill(\"evenodd\");\r\n";
pathData += "\tctx.fill(\"evenodd\");\r\n";
}
shapeData += fillData + pathData;
}
if (!"".equals(strokeData)) {
shapeData += "ctx.stroke();\r\n";
shapeData += "\tctx.stroke();\r\n";
}
}

View File

@@ -69,6 +69,9 @@ public class CanvasShapeExporter extends ShapeExporterBase {
public static String getHtmlPrefix(int width,int height) {
return "<!DOCTYPE html>\r\n"
+ "<html>\r\n"
+ "<head>"
+ "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
+ "</head>"
+ "<body>\r\n"
+ "\r\n"
+ "<canvas id=\"myCanvas\" width=\"" + width + "\" height=\"" + height + "\" style=\"border:1px solid #c3c3c3;\">\r\n"
@@ -140,7 +143,7 @@ public class CanvasShapeExporter extends ShapeExporterBase {
if(color == null){
color = basicFill;
}
fillData += "ctx.fillStyle=\"" + color(color) + "\";\r\n";
fillData += "\tctx.fillStyle=\"" + color(color) + "\";\r\n";
}
@Override
@@ -159,7 +162,7 @@ public class CanvasShapeExporter extends ShapeExporterBase {
start.y += deltaY;
end.x += deltaX;
end.y += deltaY;
fillData += "var grd=ctx.createLinearGradient(" + Double.toString(start.x / unitDivisor) + "," + Double.toString(start.y / unitDivisor) + "," + Double.toString(end.x / unitDivisor) + "," + Double.toString(end.y / unitDivisor) + ");\r\n";
fillData += "\tvar grd=ctx.createLinearGradient(" + Double.toString(start.x / unitDivisor) + "," + Double.toString(start.y / unitDivisor) + "," + Double.toString(end.x / unitDivisor) + "," + Double.toString(end.y / unitDivisor) + ");\r\n";
} else {
matrix.translateX /= unitDivisor;
matrix.translateY /= unitDivisor;
@@ -172,7 +175,7 @@ public class CanvasShapeExporter extends ShapeExporterBase {
matrix.translateX += deltaX / unitDivisor;
matrix.translateY += deltaY / unitDivisor;
fillData += "var grd=ctx.createRadialGradient("+focalPointRatio*16384+",0,0,0,0," + (16384 + 32768 * repeatCnt) + ");\r\n";
fillData += "\tvar grd=ctx.createRadialGradient("+focalPointRatio*16384+",0,0,0,0," + (16384 + 32768 * repeatCnt) + ");\r\n";
}
int repeatTotal = repeatCnt * 2 + 1;
double oneHeight = 1.0 / repeatTotal;
@@ -186,12 +189,12 @@ public class CanvasShapeExporter extends ShapeExporterBase {
revert = !revert;
}
for (GRADRECORD r : gradientRecords) {
fillData += "grd.addColorStop(" + Double.toString(pos + (oneHeight * (revert ? 255 - r.ratio : r.ratio) / 255.0)) + ",\"" + color(r.color) + "\");\r\n";
fillData += "\tgrd.addColorStop(" + Double.toString(pos + (oneHeight * (revert ? 255 - r.ratio : r.ratio) / 255.0)) + ",\"" + color(r.color) + "\");\r\n";
lastRadColor = color(r.color);
}
pos += oneHeight;
}
fillData += "ctx.fillStyle = grd;\r\n";
fillData += "\tctx.fillStyle = grd;\r\n";
}
public static String color(Color color) {
@@ -224,21 +227,7 @@ public class CanvasShapeExporter extends ShapeExporterBase {
if (image != null) {
SerializableImage img = image.getImage();
if (img != null) {
colorTransform.apply(img);
String format = image.getImageFormat();
InputStream imageStream = image.getImageData();
byte[] imageData;
if (imageStream != null) {
imageData = Helper.readStream(image.getImageData());
} else {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(img.getBufferedImage(), format.toUpperCase(Locale.ENGLISH), baos);
} catch (IOException ex) {
}
imageData = baos.toByteArray();
}
String base64ImgData = DatatypeConverter.printBase64Binary(imageData);
colorTransform.apply(img);
if (matrix != null) {
matrix.translateX /= unitDivisor;
matrix.translateY /= unitDivisor;
@@ -249,9 +238,9 @@ public class CanvasShapeExporter extends ShapeExporterBase {
fillMatrix = matrix;
}
fillData += "var img = document.createElement(\"img\"); img.src=\"data:image/" + format + ";base64," + base64ImgData + "\";\r\n";
fillData += "var pat=ctx.createPattern(img,\"repeat\");\r\n";
fillData += "ctx.fillStyle = pat;\r\n";
fillData += "\tvar pat=ctx.createPattern(image"+bitmapId+",\"repeat\");\r\n";
fillData += "\tctx.fillStyle = pat;\r\n";
}
}
}
@@ -266,29 +255,29 @@ public class CanvasShapeExporter extends ShapeExporterBase {
finalizePath();
thickness /= unitDivisor;
strokeData += "ctx.strokeStyle=\"" + color(color) + "\";\r\n";
strokeData += "ctx.lineWidth=" + Double.toString(thickness == 0 ? 1 : thickness) + ";\r\n";
strokeData += "\tctx.strokeStyle=\"" + color(color) + "\";\r\n";
strokeData += "\tctx.lineWidth=" + Double.toString(thickness == 0 ? 1 : thickness) + ";\r\n";
switch (startCaps) {
case LINESTYLE2.NO_CAP:
strokeData += "ctx.lineCap=\"butt\";\r\n";
strokeData += "\tctx.lineCap=\"butt\";\r\n";
break;
case LINESTYLE2.SQUARE_CAP:
strokeData += "ctx.lineCap=\"square\";\r\n";
strokeData += "\tctx.lineCap=\"square\";\r\n";
break;
default:
strokeData += "ctx.lineCap=\"round\";\r\n";
strokeData += "\tctx.lineCap=\"round\";\r\n";
break;
}
switch (joints) {
case LINESTYLE2.BEVEL_JOIN:
strokeData += "ctx.lineJoin=\"bevel\";\r\n";
strokeData += "\tctx.lineJoin=\"bevel\";\r\n";
break;
case LINESTYLE2.ROUND_JOIN:
strokeData += "ctx.lineJoin=\"round\";\r\n";
strokeData += "\tctx.lineJoin=\"round\";\r\n";
break;
default:
strokeData += "ctx.lineJoin=\"miter\";\r\n";
strokeData += "ctx.miterLimit=" + Integer.toString(miterLimit) + ";\r\n";
strokeData += "\tctx.lineJoin=\"miter\";\r\n";
strokeData += "\tctx.miterLimit=" + Integer.toString(miterLimit) + ";\r\n";
break;
}
}
@@ -302,7 +291,7 @@ public class CanvasShapeExporter extends ShapeExporterBase {
public void moveTo(double x, double y) {
x += deltaX;
y += deltaY;
pathData += "ctx.moveTo("
pathData += "\tctx.moveTo("
+ (x / unitDivisor) + ","
+ (y / unitDivisor) + ");\r\n";
}
@@ -311,7 +300,7 @@ public class CanvasShapeExporter extends ShapeExporterBase {
public void lineTo(double x, double y) {
x += deltaX;
y += deltaY;
pathData += "ctx.lineTo(" + (x / unitDivisor) + ","
pathData += "\tctx.lineTo(" + (x / unitDivisor) + ","
+ (y / unitDivisor) + ");\r\n";
}
@@ -321,7 +310,7 @@ public class CanvasShapeExporter extends ShapeExporterBase {
anchorX += deltaX;
controlY += deltaY;
anchorY += deltaY;
pathData += "ctx.quadraticCurveTo(" + (controlX / unitDivisor) + ","
pathData += "\tctx.quadraticCurveTo(" + (controlX / unitDivisor) + ","
+ (controlY / unitDivisor) + ","
+ (anchorX / unitDivisor) + ","
+ (anchorY / unitDivisor) + ");\r\n";
@@ -329,27 +318,27 @@ public class CanvasShapeExporter extends ShapeExporterBase {
protected void finalizePath() {
if (!"".equals(pathData)) {
pathData = "ctx.beginPath();\r\n" + pathData + "ctx.closePath();\r\n" + strokeData;
pathData = "\tctx.beginPath();\r\n" + pathData + "\tctx.closePath();\r\n" + strokeData;
if (fillMatrix != null) {
if (lastRadColor != null) {
pathData += "ctx.fillStyle=\"" + lastRadColor + "\";\r\n ctx.fill(\"evenodd\");\r\n";
pathData += "\tctx.fillStyle=\"" + lastRadColor + "\";\r\n ctx.fill(\"evenodd\");\r\n";
}
pathData += "ctx.save();\r\n";
pathData += "ctx.clip();\r\n";
pathData += "ctx.transform(" + fillMatrix.scaleX + "," + fillMatrix.rotateSkew0 + "," + fillMatrix.rotateSkew1 + "," + fillMatrix.scaleY + "," + fillMatrix.translateX + "," + fillMatrix.translateY + ");\r\n";
pathData += "\tctx.save();\r\n";
pathData += "\tctx.clip();\r\n";
pathData += "\tctx.transform(" + fillMatrix.scaleX + "," + fillMatrix.rotateSkew0 + "," + fillMatrix.rotateSkew1 + "," + fillMatrix.scaleY + "," + fillMatrix.translateX + "," + fillMatrix.translateY + ");\r\n";
pathData += fillData;
pathData += "ctx.fillRect(" + (-16384 - 32768 * repeatCnt) + "," + (-16384 - 32768 * repeatCnt) + "," + (2 * 16384 + 32768 * 2 * repeatCnt) + "," + (2 * 16384 + 32768 * 2 * repeatCnt) + ");\r\n";
pathData += "ctx.restore();\r\n";
pathData += "\tctx.fillRect(" + (-16384 - 32768 * repeatCnt) + "," + (-16384 - 32768 * repeatCnt) + "," + (2 * 16384 + 32768 * 2 * repeatCnt) + "," + (2 * 16384 + 32768 * 2 * repeatCnt) + ");\r\n";
pathData += "\tctx.restore();\r\n";
shapeData += pathData;
} else {
if (!"".equals(fillData)) {
pathData += "ctx.fill(\"evenodd\");\r\n";
pathData += "\tctx.fill(\"evenodd\");\r\n";
}
shapeData += fillData + pathData;
}
if (!"".equals(strokeData)) {
shapeData += "ctx.stroke();\r\n";
shapeData += "\tctx.stroke();\r\n";
}
}

View File

@@ -80,5 +80,5 @@ public abstract class ImageTag extends CharacterTag {
protected static Color multiplyAlpha(Color c) {
float multiplier = c.getAlpha() == 0 ? 0 : 255.0f / c.getAlpha();
return new Color(max255(c.getRed() * multiplier), max255(c.getGreen() * multiplier), max255(c.getBlue() * multiplier), c.getAlpha());
}
}
}

View File

@@ -349,14 +349,14 @@ public abstract class TextTag extends CharacterTag implements BoundedTag, Drawab
Matrix mat = (new Matrix(textMatrix).concatenate(Matrix.getTranslateInstance(x - bounds.Xmin, y - bounds.Ymin))).concatenate(Matrix.getScaleInstance(rat));
if (entry.glyphIndex != -1) {
// shapeNum: 1
ret += "ctx.save();\r\n";
ret += "ctx.transform(" + mat.scaleX + "," + mat.rotateSkew0 + "," + mat.rotateSkew1 + "," + mat.scaleY + "," + mat.translateX + "," + mat.translateY + ");\r\n";
ret += "\tctx.save();\r\n";
ret += "\tctx.transform(" + mat.scaleX + "," + mat.rotateSkew0 + "," + mat.rotateSkew1 + "," + mat.scaleY + "," + mat.translateX + "," + mat.translateY + ");\r\n";
SHAPE shape = glyphs.get(entry.glyphIndex);
CanvasShapeExporter exporter = new CanvasShapeExporter(new RGBA(textColor),unitDivisor, swf, shape, colorTransform,0,0);
exporter.export();
ret += exporter.getShapeData();
ret += "ctx.restore();\r\n";
ret += "\tctx.restore();\r\n";
x += entry.glyphAdvance;
}
}

View File

@@ -172,8 +172,7 @@ public class Timeline {
public String toHtmlCanvas(double unitDivisor,List<Integer> frames){
// SWF.framesToHtmlCanvas(unitDivisor, null, null, this, frames, id, null, frameRate, displayRect, null, Color.darkGray, id)r
return null;
return SWF.framesToHtmlCanvas(unitDivisor, this, frames, 0, null, 0, displayRect, new ColorTransform(),null);
}
public void getSounds(int frame, int time, DepthState stateUnderCursor, int mouseButton, List<Integer> sounds, List<String> soundClasses) {