From 93cfc3e4052cba64aac9e47f69a483fe23cb215d Mon Sep 17 00:00:00 2001 From: jamchamb Date: Fri, 6 Feb 2015 22:51:24 -0500 Subject: [PATCH 1/3] Specify exact output dimensions (Inkscape) --- README.md | 7 +++++-- plugin.py | 39 ++++++++++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index bf743b4..cf32308 100644 --- a/README.md +++ b/README.md @@ -98,9 +98,12 @@ You can use wildcards (as well as other special characters supported by Python's [glob module](https://docs.python.org/3.1/library/glob.html)) to specify groups of images under a path. -Since Illustrator accepts a 'scaling' property for rendering and not a DPI, +Since Illustrator accepts a `scaling` property for rendering and not a DPI, it is assumed your files were created with a DPI of 72. If they weren't, -use the 'srcdpi' to specify the source dpi. +use the `srcdpi` to specify the source dpi. + +Inkscape accepts `width` and `height` properties for exact output dimensions. +These properties override `dpi` - they're mutually exclusive. If you create `images/Icon.svg` at 57x57 *points* in inkscape, the above group called `appicons` diff --git a/plugin.py b/plugin.py index 3e883cd..8d5748f 100644 --- a/plugin.py +++ b/plugin.py @@ -58,15 +58,36 @@ def illustrator (infile, outfile, outputConfig, pluginConfig): def inkscape (infile, outfile, outputConfig, pluginConfig): """Render an image with inkscape""" - print 'Rendering', infile, 'to', outfile, 'at dpi', outputConfig['dpi'], 'with Inkscape.' - command = [ - "inkscape", - "-d", - str(outputConfig['dpi']), - "-e", - outfile, - infile - ] + command = ["inkscape"] + + # Use width and height properties + if 'width' in outputConfig and 'height' in outputConfig: + + # Don't allow dpi and width/height to be set + if 'dpi' in outputConfig: + raise Exception("DPI and dimension properties for Inkscape export are mutually exclusive.") + + print 'Rendering', infile, 'to', outfile, 'at', str(outputConfig['width']), 'x', str(outputConfig['height']), 'px with Inkscape.' + command.extend([ + "-w", + str(outputConfig['width']), + "-h", + str(outputConfig['height']) + ]) + + # Use DPI property + elif 'dpi' in outputConfig: + print 'Rendering', infile, 'to', outfile, 'at dpi', outputConfig['dpi'], 'with Inkscape.' + command.extend([ + "-d", + str(outputConfig['dpi']) + ]) + + else: + raise Exception("Invalid Inkscape export configuration for " + outfile) + + command.extend(["-e", outfile, infile]) + launch(command) def checkProps (names, pluginConfig, outputConfig): From 11e803e15856727dd003c6d0d40ed352439efa66 Mon Sep 17 00:00:00 2001 From: James Chambers Date: Mon, 9 Feb 2015 16:35:10 -0500 Subject: [PATCH 2/3] Specify density-independent pixel dimensions (Inkscape) This change allows you to use 'width-dp' and 'height-dp' properties to specify density-independent pixel dimensions targeting specific screen DPIs ('screen-dpi'). --- README.md | 9 +++++++-- plugin.py | 37 +++++++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index cf32308..d329470 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,9 @@ icons: - path: Resources/images/android prepend: 'ic_' - dpi: 82 + screen-dpi: 480 + width-dp: 30 + height-dp: 30 images: - icons/foo.ai @@ -102,7 +104,10 @@ Since Illustrator accepts a `scaling` property for rendering and not a DPI, it is assumed your files were created with a DPI of 72. If they weren't, use the `srcdpi` to specify the source dpi. -Inkscape accepts `width` and `height` properties for exact output dimensions. +Inkscape accepts pixel and density-independent dimension properties. +To specify output dimensions in pixels, use `width-px` and `height-px`. +To specify output dimensions in density-independent pixels, use `width-dp`, +`height-dp`, and `screen-dpi`, where `screen-dpi` is the target device screen DPI. These properties override `dpi` - they're mutually exclusive. If you create `images/Icon.svg` at 57x57 *points* in inkscape, the above group diff --git a/plugin.py b/plugin.py index 8d5748f..1a36ead 100644 --- a/plugin.py +++ b/plugin.py @@ -1,6 +1,7 @@ #!/usr/bin/env python import os, subprocess, inspect, yaml, shutil, sys, glob +from math import ceil class ModCheck (object): """handles checking of modification times to see if we need to @@ -36,6 +37,9 @@ def launch (command): print unicode(err, errors='ignore') raise Exception('Command failed, check output') +def dp_to_px (dp, screen_dpi): + return int(ceil(dp * (screen_dpi/float(160)))) + def illustrator (infile, outfile, outputConfig, pluginConfig): """Render an image with illustrator""" print 'Rendering', infile, 'to', outfile, 'at dpi', outputConfig['dpi'], 'with Illustrator.' @@ -60,19 +64,28 @@ def inkscape (infile, outfile, outputConfig, pluginConfig): """Render an image with inkscape""" command = ["inkscape"] - # Use width and height properties - if 'width' in outputConfig and 'height' in outputConfig: + # Use pixel dimension properties (width-px, height-px) + if {'width-px', 'height-px'} <= set(outputConfig): - # Don't allow dpi and width/height to be set - if 'dpi' in outputConfig: - raise Exception("DPI and dimension properties for Inkscape export are mutually exclusive.") - - print 'Rendering', infile, 'to', outfile, 'at', str(outputConfig['width']), 'x', str(outputConfig['height']), 'px with Inkscape.' + print 'Rendering', infile, 'to', outfile, 'at', str(outputConfig['width-px']), 'x', str(outputConfig['height-px']), 'px with Inkscape.' command.extend([ "-w", - str(outputConfig['width']), + str(outputConfig['width-px']), "-h", - str(outputConfig['height']) + str(outputConfig['height-px']) + ]) + + # Use density-independent pixel dimension properties (width-dp, height-dp, screen-dpi) + elif {'width-dp', 'height-dp', 'screen-dpi'} <= set(outputConfig): + + print 'Rendering', infile, 'to', outfile, 'at', str(outputConfig['width-dp']), 'x', str(outputConfig['height-dp']), 'dp for', str(outputConfig['screen-dpi']), 'DPI screen with Inkscape.' + width_px = dp_to_px(outputConfig['width-dp'], outputConfig['screen-dpi']) + height_px = dp_to_px(outputConfig['height-dp'], outputConfig['screen-dpi']) + command.extend([ + "-w", + str(width_px), + "-h", + str(height_px) ]) # Use DPI property @@ -86,7 +99,11 @@ def inkscape (infile, outfile, outputConfig, pluginConfig): else: raise Exception("Invalid Inkscape export configuration for " + outfile) - command.extend(["-e", outfile, infile]) + command.extend([ + "-e", + outfile, + infile + ]) launch(command) From 5be817448fd19bc089d6994f8347cc9f7cc6a5b6 Mon Sep 17 00:00:00 2001 From: James Chambers Date: Thu, 2 Apr 2015 01:38:11 -0400 Subject: [PATCH 3/3] Comment for dp_to_px, remove unnecessary str --- plugin.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugin.py b/plugin.py index 1a36ead..774a1e3 100644 --- a/plugin.py +++ b/plugin.py @@ -38,6 +38,7 @@ def launch (command): raise Exception('Command failed, check output') def dp_to_px (dp, screen_dpi): + """Translate density-independent pixels to pixels for a given screen DPI.""" return int(ceil(dp * (screen_dpi/float(160)))) def illustrator (infile, outfile, outputConfig, pluginConfig): @@ -67,7 +68,7 @@ def inkscape (infile, outfile, outputConfig, pluginConfig): # Use pixel dimension properties (width-px, height-px) if {'width-px', 'height-px'} <= set(outputConfig): - print 'Rendering', infile, 'to', outfile, 'at', str(outputConfig['width-px']), 'x', str(outputConfig['height-px']), 'px with Inkscape.' + print 'Rendering', infile, 'to', outfile, 'at', outputConfig['width-px'], 'x', outputConfig['height-px'], 'px with Inkscape.' command.extend([ "-w", str(outputConfig['width-px']), @@ -78,7 +79,7 @@ def inkscape (infile, outfile, outputConfig, pluginConfig): # Use density-independent pixel dimension properties (width-dp, height-dp, screen-dpi) elif {'width-dp', 'height-dp', 'screen-dpi'} <= set(outputConfig): - print 'Rendering', infile, 'to', outfile, 'at', str(outputConfig['width-dp']), 'x', str(outputConfig['height-dp']), 'dp for', str(outputConfig['screen-dpi']), 'DPI screen with Inkscape.' + print 'Rendering', infile, 'to', outfile, 'at', outputConfig['width-dp'], 'x', outputConfig['height-dp'], 'dp for', outputConfig['screen-dpi'], 'DPI screen with Inkscape.' width_px = dp_to_px(outputConfig['width-dp'], outputConfig['screen-dpi']) height_px = dp_to_px(outputConfig['height-dp'], outputConfig['screen-dpi']) command.extend([