Permalink
Browse files

Handle "iOS" option

  • Loading branch information...
1 parent 7095ce4 commit 23e773d3aba224f0727fdac10021dd1bce36631c @andreyvit committed Jan 22, 2014
Showing with 48 additions and 0 deletions.
  1. +45 −0 main.js
  2. +3 −0 test/test-parse-layer-name.js
View
45 main.js
@@ -420,11 +420,56 @@
reportError(component.error);
}
}
+
+ function splitComponent(component) {
+ if (component.ios) {
+ var component1x = Object.create(component);
+ var component2x = Object.create(component);
+ component1x.ios = component2x.ios = false;
+
+ component1x.scale = (component.scale || 1.0) / 2;
+
+ if (component.pad) {
+ component1x.pad = {
+ left: { value: Math.floor(component.pad.left.value / 2) },
+ right: { value: Math.floor(component.pad.right.value / 2) },
+ top: { value: Math.floor(component.pad.top.value / 2) },
+ bottom: { value: Math.floor(component.pad.bottom.value / 2) },
+ };
+ }
+
+ if (component.padto) {
+ component1x.padto = {
+ width: { value: Math.floor(component.padto.width.value / 2) },
+ height: { value: Math.floor(component.padto.height.value / 2) },
+ };
+ }
+
+ component2x.file = component.file.replace(/(\.\w+)$/i, "@2x$1");
+ return [component1x, component2x];
+ }
+ }
+
+ function rearrangeComponents(components) {
+ // the components array is mutated during the iteration, so use a custom loop
+ var i = 0;
+ while (i < components.length) {
+ var replacements = splitComponent(components[i]);
+ if (typeof replacements !== "undefined") {
+ components.splice.apply(components, [i, 1].concat(replacements));
+ } else {
+ ++i;
+ }
+ }
+ return components;
+ }
function analyzeLayerName(layerName) {
var components = typeof(layerName) === "string" ? parseLayerName(layerName) : [],
errors = [];
+ components = rearrangeComponents(components);
+
var validFileComponents = components.filter(function (component) {
if (!component.file) {
return false;
View
3 test/test-parse-layer-name.js
@@ -189,6 +189,9 @@
[{ name: "100x80 foo.png iOS", file: "foo.png", extension: "png",
width: 100, height: 80, ios: true }],
+ "foo.png iOS":
+ [{ name: "foo.png iOS", file: "foo.png", extension: "png", ios: true }],
+
"100x80 foo.png pad=10:20":
[{ name: "100x80 foo.png pad=10:20", file: "foo.png", extension: "png",
width: 100, height: 80,

0 comments on commit 23e773d

Please sign in to comment.