exporter optimizer twicks

This commit is contained in:
2016-09-30 03:21:12 +07:00
parent 5ae09addae
commit c0a7121f60

View File

@@ -34,7 +34,7 @@ if (!Function.prototype.bind) {
var ft = {
profile_mode : false,
verbose_mode : true,
verbose_mode : false,
optimize_static_items : true,
optimize_single_graphics : true
};
@@ -93,7 +93,8 @@ if (!Function.prototype.bind) {
ft.profile_function_stack.push({
msg: msg,
level: ft.profile_function_level,
time : 0});
time: 0
});
++ft.profile_function_level;
var func_time = ft.get_call_function_time(func);
--ft.profile_function_level;
@@ -244,6 +245,7 @@ if (!Function.prototype.bind) {
};
ftdoc.full_exit_edit_mode = function (doc) {
ft.type_assert(doc, Document);
for (var i = 0; i < 100; ++i) {
doc.exitEditMode();
}
@@ -314,6 +316,18 @@ if (!Function.prototype.bind) {
return item.itemType == "graphic" || item.itemType == "movie clip";
};
ftlib.find_item_by_name = function (library, item_name) {
ft.type_assert(library, Library);
ft.type_assert(item_name, 'string');
for (var i = 0; i < library.items.length; ++i) {
var item = library.items[i];
if (item.name == item_name) {
return item;
}
}
return null;
};
ftlib.edit_all_items = function (doc, library, func, filter) {
ft.type_assert(doc, Document);
ft.type_assert(library, Library);
@@ -332,7 +346,7 @@ if (!Function.prototype.bind) {
ft.type_assert(library, Library);
ft.type_assert(func, Function);
ftlib.edit_all_items(doc, library, func, ftlib.is_symbol_item);
}
};
ftlib.unlock_all_timelines = function (doc, library) {
ft.type_assert(doc, Document);
@@ -349,11 +363,16 @@ if (!Function.prototype.bind) {
var replaces = {};
ft.array_reverse_foreach(library.items, function (item) {
var new_item_name = ft.gen_unique_name();
ftlib.bake_symbol_item(doc, library, item.name, new_item_name, 0);
if (ftlib.bake_symbol_item(doc, library, item.name, new_item_name, 0)) {
replaces[item.name] = new_item_name;
if (ft.verbose_mode) {
ft.trace_fmt("Optimize static item: '{0}'", item.name);
}
} else {
if (ft.verbose_mode) {
ft.trace_fmt("NOT Optimize static item: '{0}'", item.name);
}
}
}, function (item) {
return ftlib.is_symbol_item(item) && fttim.is_static(item.timeline);
});
@@ -371,9 +390,37 @@ if (!Function.prototype.bind) {
ft.type_assert(new_item_name, 'string');
ft.type_assert(first_frame, 'number');
if (!library.itemExists(new_item_name)) {
library.addNewItem("graphic", new_item_name);
if (library.editItem(new_item_name)) {
if (library.itemExists(new_item_name)) {
return true;
}
var item = ftlib.find_item_by_name(library, item_name);
if (!item) {
return false;
}
var item_frame_area = fttim.calculate_frame_area(item.timeline, first_frame);
var item_elems_area = fttim.calculate_elems_area(item.timeline, first_frame);
if (ft.verbose_mode) {
ft.trace_fmt(
"Library item: '{0}'\n- frame area: {1}\n- elems area: {2}",
item_name, item_frame_area, item_elems_area);
}
if (item_frame_area >= item_elems_area) {
return false;
}
if (!library.addNewItem("graphic", new_item_name)) {
return false;
}
if (!library.editItem(new_item_name)) {
library.deleteItem(new_item_name);
return false;
}
if (library.addItemToDocument({x: 0, y: 0}, item_name)) {
var new_item_elem = doc.selection[0];
new_item_elem.symbolType = "graphic";
@@ -382,9 +429,11 @@ if (!Function.prototype.bind) {
new_item_elem.transformX = 0;
new_item_elem.transformY = 0;
doc.convertSelectionToBitmap();
}
return true;
} else {
doc.exitEditMode();
}
library.deleteItem(new_item_name);
return false;
}
};
@@ -446,6 +495,7 @@ if (!Function.prototype.bind) {
fttim.is_keyframe = function (frame, frame_index) {
ft.type_assert(frame, Frame);
ft.type_assert(frame_index, 'number');
return frame.startFrame == frame_index;
};
@@ -457,6 +507,46 @@ if (!Function.prototype.bind) {
});
};
fttim.calculate_elems_area = function (timeline, frame_index) {
ft.type_assert(timeline, Timeline);
ft.type_assert(frame_index, 'number');
return ft.array_foldl(timeline.layers, function (layer, acc) {
if (frame_index >= 0 && frame_index < layer.frames.length) {
return ft.array_foldl(layer.frames[frame_index].elements, function (elem, acc2) {
return acc2 + Math.round(elem.width) * Math.round(elem.height);
}, acc);
} else {
return acc;
}
}, 0);
};
fttim.calculate_frame_area = function (timeline, frame_index) {
ft.type_assert(timeline, Timeline);
ft.type_assert(frame_index, 'number');
var bounds = ft.array_foldl(timeline.layers, function (layer, acc) {
if (frame_index >= 0 && frame_index < layer.frames.length) {
return ft.array_foldl(layer.frames[frame_index].elements, function (elem, acc2) {
acc2.left = Math.min(acc2.left, elem.left);
acc2.right = Math.max(acc2.right, elem.left + elem.width);
acc2.top = Math.min(acc2.top, elem.top);
acc2.bottom = Math.max(acc2.bottom, elem.top + elem.height);
return acc2;
}, acc);
} else {
return acc;
}
}, {
left: Number.POSITIVE_INFINITY,
right: Number.NEGATIVE_INFINITY,
top: Number.POSITIVE_INFINITY,
bottom: Number.NEGATIVE_INFINITY
});
var frame_width = Math.max(0, bounds.right - bounds.left);
var frame_height = Math.max(0, bounds.bottom - bounds.top);
return Math.round(frame_width) * Math.round(frame_height);
}
fttim.replace_baked_symbols = function (doc, timeline, replaces) {
ft.type_assert(doc, Document);
ft.type_assert(timeline, Timeline);
@@ -484,13 +574,11 @@ if (!Function.prototype.bind) {
ft.array_foreach(frame.elements, function (elem) {
var lib_item_name = elem.libraryItem.name;
var lib_item_cache_name = "ft_cache_name_" + lib_item_name + "_" + elem.firstFrame;
ftlib.bake_symbol_item(doc, doc.library, lib_item_name, lib_item_cache_name, elem.firstFrame);
if (ftlib.bake_symbol_item(doc, doc.library, lib_item_name, lib_item_cache_name, elem.firstFrame)) {
if (ft.verbose_mode) {
ft.trace_fmt("Optimize single graphic '{0}' for frame '{1}' in '{2}'",
lib_item_name, elem.firstFrame, timeline.name);
}
if (opt_item == null || doc.library.editItem(opt_item.name)) {
if (timeline.currentFrame != frame_index) {
timeline.currentFrame = frame_index;
@@ -501,6 +589,12 @@ if (!Function.prototype.bind) {
doc.selection[0].firstFrame = 0;
doc.exitEditMode();
}
} else {
if (ft.verbose_mode) {
ft.trace_fmt("NOT Optimize single graphic '{0}' for frame '{1}' in '{2}'",
lib_item_name, elem.firstFrame, timeline.name);
}
}
}, function (elem) {
return fttim.is_symbol_graphic_single_frame_instance(elem) && !fttim.is_static(elem.libraryItem.timeline);
});
@@ -516,9 +610,7 @@ if (!Function.prototype.bind) {
return ft.array_foldl(timeline.layers, function (layer, acc) {
return ft.array_foldl(layer.frames, function (frame, acc2) {
return ft.array_foldl(frame.elements, function (elem, acc3) {
return acc3 && fttim.is_symbol_instance(elem)
? elem.blendMode != "erase" && (fttim.is_symbol_graphic_single_frame_instance(elem) || fttim.is_static(elem.libraryItem.timeline))
: acc3;
return acc3 && fttim.is_symbol_instance(elem) ? elem.blendMode != "erase" && (fttim.is_symbol_graphic_single_frame_instance(elem) || fttim.is_static(elem.libraryItem.timeline)) : acc3;
}, acc2);
}, acc);
}, true);