made extra tag insertion during write() optional
[swftools.git] / lib / rfxswf.c
index 6a7ba4c..16e0005 100644 (file)
@@ -863,15 +863,18 @@ TAG* swf_CopyTag(TAG*tag, TAG*to_copy)
     return tag;
 }
 
-int swf_DeleteTag(TAG * t)
-{ if (!t) return -1;
+TAG* swf_DeleteTag(SWF*swf, TAG * t)
+{
+  TAG*next = t->next;
 
+  if (swf && swf->firstTag==t) 
+    swf->firstTag = t->next;
   if (t->prev) t->prev->next = t->next;
   if (t->next) t->next->prev = t->prev;
 
   if (t->data) rfx_free(t->data);
   rfx_free(t);
-  return 0;
+  return next;
 }
 
 TAG * swf_ReadTag(reader_t*reader, TAG * prev)
@@ -938,7 +941,12 @@ int swf_WriteTag2(writer_t*writer, TAG * t)
      t->id!=ST_DEFINEBITSJPEG&&t->id!=ST_DEFINEBITSJPEG2&&t->id!=ST_DEFINEBITSJPEG3);
 
   if (writer)
-  { if (short_tag)
+  {
+#ifdef MEASURE
+  int oldpos = writer->pos;
+#endif
+    
+    if (short_tag)
     { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
       if (writer->write(writer,raw,2)!=2)
       {
@@ -981,6 +989,11 @@ int swf_WriteTag2(writer_t*writer, TAG * t)
     #ifdef DEBUG_RFXSWF
       else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
     #endif
+
+#ifdef MEASURE
+  writer->flush(writer);
+  printf("TAG %s costs %d bytes\n", swf_TagGetName(t), writer->pos-oldpos);
+#endif
   }
 
   return t->len+(short_tag?2:6);
@@ -1129,7 +1142,7 @@ void swf_FoldSprite(TAG * t)
     if(t->id == ST_END)
        level--;
     t = swf_NextTag(t);
-    swf_DeleteTag(tmp);
+    swf_DeleteTag(0, tmp);
   } 
   while (t && level);
   if(level)
@@ -1276,6 +1289,8 @@ int swf_ReadSWF(int handle, SWF * swf)
   return swf_ReadSWF2(&reader, swf);
 }
 
+int no_extra_tags = 0;
+
 int  swf_WriteSWF2(writer_t*writer, SWF * swf)     // Writes SWF to file, returns length or <0 if fails
 { U32 len;
   TAG * t;
@@ -1283,47 +1298,66 @@ int  swf_WriteSWF2(writer_t*writer, SWF * swf)     // Writes SWF to file, return
   writer_t zwriter;
   int fileSize = 0;
   int inSprite = 0;
-  int writer_lastpos = 0;
   int ret;
+  writer_t*original_writer = writer;
+  int writer_lastpos = 0;
     
   if (!swf) return -1;
   if (!writer) return -1; // the caller should provide a nullwriter, not 0, for querying SWF size
 
-  if(writer) writer_lastpos = writer->pos;
+  if(original_writer) writer_lastpos = original_writer->pos;
 
   // Insert REFLEX Tag
 
+  if(!no_extra_tags) {
+      /* this block needs a complete rewrite */
+
 #ifdef INSERT_RFX_TAG
 
-  if ((swf->firstTag && swf->firstTag->id != ST_REFLEX) &&
-      (!swf->firstTag->next || (swf->firstTag->next->id != ST_REFLEX &&
-      (!swf->firstTag->next->next || (swf->firstTag->next->next->id!=ST_REFLEX)))))
-  {
-      swf_SetBlock(swf_InsertTagBefore(swf, swf->firstTag,ST_REFLEX),(U8*)"rfx",3);
-  }
+      if ((swf->firstTag && swf->firstTag->id != ST_REFLEX) &&
+         (!swf->firstTag->next || (swf->firstTag->next->id != ST_REFLEX &&
+         (!swf->firstTag->next->next || (swf->firstTag->next->next->id!=ST_REFLEX)))))
+      {
+         swf_SetBlock(swf_InsertTagBefore(swf, swf->firstTag,ST_REFLEX),(U8*)"rfx",3);
+      }
 
 #endif // INSERT_RFX_TAG
 
-  if(swf->fileVersion >= 9) {
-    if ((!swf->firstTag || swf->firstTag->id != ST_SCENEDESCRIPTION) &&
-       (!swf->firstTag || 
-        !swf->firstTag->next || swf->firstTag->next->id != ST_SCENEDESCRIPTION) &&
-       (!swf->firstTag || 
-        !swf->firstTag->next || 
-        !swf->firstTag->next->next || swf->firstTag->next->next->id != ST_SCENEDESCRIPTION))
-    {
-       TAG*scene = swf_InsertTagBefore(swf, swf->firstTag,ST_SCENEDESCRIPTION);
-       swf_SetU16(scene, 1);
-       swf_SetString(scene, (U8*)"Scene 1");
-       swf_SetU8(scene, 0);
-    }
-  }
-  
-  if(swf->fileVersion >= 9) {
-      if (swf->firstTag && swf->firstTag->id != ST_FILEATTRIBUTES)
-      {
-         U32 flags = 0x8; // | 128 = usenetwork, | 16 = Actionscript3 | 8 = hasmetadata
-         swf_SetU32(swf_InsertTagBefore(swf, swf->firstTag,ST_FILEATTRIBUTES),flags);
+      if(swf->fileVersion >= 9) {
+       if ((!swf->firstTag || swf->firstTag->id != ST_SCENEDESCRIPTION) &&
+           (!swf->firstTag || 
+            !swf->firstTag->next || swf->firstTag->next->id != ST_SCENEDESCRIPTION) &&
+           (!swf->firstTag || 
+            !swf->firstTag->next || 
+            !swf->firstTag->next->next || swf->firstTag->next->next->id != ST_SCENEDESCRIPTION))
+       {
+           TAG*scene = swf_InsertTagBefore(swf, swf->firstTag,ST_SCENEDESCRIPTION);
+           swf_SetU16(scene, 1);
+           swf_SetString(scene, (U8*)"Scene 1");
+           swf_SetU8(scene, 0);
+       }
+      }
+      
+      if(swf->fileVersion >= 9) {
+         TAG*tag = swf->firstTag;
+         U32 flags = 0x08; // 16 = has symbolclass tag | 8 = actionscript3 | 1 = usenetwork
+         int has_version_8_action=0;
+         int has_version_9_action=0;
+         while(tag) {
+           /* FIXME: this doesn't find actionscript in buttons */
+           if(tag->id == ST_DOACTION || tag->id == ST_DOINITACTION) 
+             has_version_8_action=1;
+           if(tag->id == ST_DOABC) 
+             has_version_9_action=1;
+           tag = tag->next;
+         }
+         if(has_version_8_action && !has_version_9_action)
+           flags = 0x00;
+
+         if (swf->firstTag && swf->firstTag->id != ST_FILEATTRIBUTES)
+         {
+             swf_SetU32(swf_InsertTagBefore(swf, swf->firstTag,ST_FILEATTRIBUTES),flags);
+         }
       }
   }
 
@@ -1422,7 +1456,7 @@ int  swf_WriteSWF2(writer_t*writer, SWF * swf)     // Writes SWF to file, return
     if(swf->compressed==1 || (swf->compressed==0 && swf->fileVersion>=6) || swf->compressed==8) {
       if(swf->compressed != 8) {
        zwriter.finish(&zwriter);
-       return writer->pos - writer_lastpos;
+       return original_writer->pos - writer_lastpos;
       }
       return (int)fileSize;
     } else {