--!movie
--!encoding=utf-8
----------------------------------------
-- Exports vectorShape member as SWF file
-- Usage:
-- ok = exportSWF( member("MyVectorShape"), _movie.path&"export.swf" )
--
-- @param {member} aVectorMember
-- @param {string} aSwfFile
-- @return {bool} success
--
-- @requires clipboard xtra
-- @requires fileIO xtra
----------------------------------------
on exportSWF (aVectorMember, aSwfFile)
if aVectorMember.type<>#vectorShape then return FALSE
-- copy vectorShape member to clipboard
aVectorMember.copyToClipBoard()
-- get Director's custom data for members in clipboard (as byteArray)
cx = xtra("clipboard").new()
if the platform contains "win" then
ba = cx.clipGetData("DIRECTOR:1005")
else
ba = cx.clipGetData(1005)
end if
if voidP(ba) then return FALSE
-- find offset of "FWS" (=start of SWF data) in byteArray
swfStart = bytesOffset("FWS", ba)
if swfStart=0 then return FALSE
-- get length of SWF data
ba.position = swfStart+4
swfLen = ba.readInt32()
-- extract the SWF data, and save it to file
ba.position = swfStart
ba = ba.readByteArray(swfLen)
return putBytes(aSwfFile, ba)
end
----------------------------------------
-- Finds position of string (or byte list) in byteArray
-- Note: case-sensitive!
--
-- @param {string|list} aNeedle
-- @param {byteArray} aHaystack
-- @return {integer} position
----------------------------------------
on bytesOffset (aNeedle, aHaystack)
if stringP(aNeedle) then
s = aNeedle
aNeedle = []
len = s.length
repeat with i = 1 to len
aNeedle[i] = chartonum(s.char[i])
end repeat
end if
cnt = aHaystack.length - aNeedle.count + 1
cnt2 = aNeedle.count
repeat with i = 1 to cnt
ok = TRUE
repeat with j = 1 to cnt2
if aHaystack[i+j-1]<>aNeedle[j] then
ok = FALSE
exit repeat
end if
end repeat
if ok then return i
end repeat
return 0
end
----------------------------------------
-- Saves byteArray to file
--
-- @param {string} aFile
-- @param {byteArray} aByteArray
-- @return {bool} success
--
-- @requires fileIO xtra
----------------------------------------
on putBytes (aFile, aByteArray)
fp = xtra("fileIO").new()
fp.openFile(aFile, 2)
err = fp.status()
if not (err) then fp.delete()
else if (err and not (err = -37)) then return FALSE
fp.createFile(aFile)
err = fp.status()
if (err) then return FALSE
fp.openFile(aFile, 2)
err = fp.status()
if (err) then return FALSE
fp.writeByteArray(aByteArray)
fp.closeFile()
return TRUE
end