aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@qt.io>2017-10-12 15:24:31 +0200
committerJ-P Nurmi <jpnurmi@qt.io>2017-10-18 19:46:39 +0000
commita4265f2c8e9b99e804b3d065d0419d7f29a2e8cd (patch)
tree2c28c722a5d8c4872b3bc173e703589f88b6356f
parent6dbe20bdc3ae582a885d931519c4356c2ec58285 (diff)
9-patch export plugin for Sketch
This plugin crops upscaled 9-patch PNG assets when exported from Sketch, to ensure that 9-patch borders remain 1px wide when upscaled. Change-Id: Iad46d75f37b76eecf64ac32fa38d2b9e9db8b34d Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
-rw-r--r--src/imports/controls/imagine/design/9-patch-export.sketchplugin/Contents/Sketch/9-patch-export.js24
-rw-r--r--src/imports/controls/imagine/design/9-patch-export.sketchplugin/Contents/Sketch/manifest.json19
-rw-r--r--tests/auto/sanity/tst_sanity.cpp2
3 files changed, 44 insertions, 1 deletions
diff --git a/src/imports/controls/imagine/design/9-patch-export.sketchplugin/Contents/Sketch/9-patch-export.js b/src/imports/controls/imagine/design/9-patch-export.sketchplugin/Contents/Sketch/9-patch-export.js
new file mode 100644
index 00000000..a7f9a459
--- /dev/null
+++ b/src/imports/controls/imagine/design/9-patch-export.sketchplugin/Contents/Sketch/9-patch-export.js
@@ -0,0 +1,24 @@
+// 9-patch export
+//
+// This plugin crops upscaled 9-patch PNG assets when exported from Sketch,
+// to ensure that 9-patch borders remain 1px wide when upscaled.
+//
+function onExportSlices(context) {
+ var exports = context.actionContext.exports;
+ for (var i = 0; i < exports.count(); ++i) {
+ var name = exports[i].request.name();
+ var scale = exports[i].request.scale();
+ if (scale > 1 && name.endsWith(".9"))
+ cropAsset(exports[i].path, scale - 1);
+ }
+}
+
+function cropAsset(path, inset) {
+ var url = NSURL.fileURLWithPath(path);
+ var img = CIImage.imageWithContentsOfURL(url);
+ var rect = NSInsetRect(img.extent(), inset, inset);
+ var cropped = img.imageByCroppingToRect(rect);
+ var rep = NSBitmapImageRep.alloc().initWithCIImage(cropped);
+ var data = rep.PNGRepresentationWithInterlaced(false);
+ data.writeToFile(path);
+}
diff --git a/src/imports/controls/imagine/design/9-patch-export.sketchplugin/Contents/Sketch/manifest.json b/src/imports/controls/imagine/design/9-patch-export.sketchplugin/Contents/Sketch/manifest.json
new file mode 100644
index 00000000..40807aa3
--- /dev/null
+++ b/src/imports/controls/imagine/design/9-patch-export.sketchplugin/Contents/Sketch/manifest.json
@@ -0,0 +1,19 @@
+{
+ "name" : "9-patch export",
+ "description" : "Crops upscaled 9-patch PNG assets when exported from Sketch.",
+ "version" : "0.1",
+ "identifier" : "org.qt-project.sketch.9-patch-export",
+ "author" : "The Qt Project",
+ "commands" : [
+ {
+ "name" : "9-patch export",
+ "identifier" : "9-patch-export",
+ "script" : "9-patch-export.js",
+ "handlers" : {
+ "actions" : {
+ "ExportSlices": "onExportSlices",
+ },
+ },
+ },
+ ],
+}
diff --git a/tests/auto/sanity/tst_sanity.cpp b/tests/auto/sanity/tst_sanity.cpp
index 2c67e86a..96c8eb38 100644
--- a/tests/auto/sanity/tst_sanity.cpp
+++ b/tests/auto/sanity/tst_sanity.cpp
@@ -139,7 +139,7 @@ private:
void tst_Sanity::initTestCase()
{
QDirIterator it(QQC2_IMPORT_PATH, QStringList() << "*.qml" << "*.js", QDir::Files, QDirIterator::Subdirectories);
- const QStringList excludeDirs = QStringList() << QStringLiteral("snippets") << QStringLiteral("designer");
+ const QStringList excludeDirs = QStringList() << QStringLiteral("snippets") << QStringLiteral("designer") << QStringLiteral("Sketch");
while (it.hasNext()) {
it.next();
QFileInfo info = it.fileInfo();