From 545af877b6b6ce003801e6a19de3f68a1c0fbc4a Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Sun, 16 Mar 2014 19:41:51 -0400 Subject: Fix header and copyright licence (bzr r13158) --- share/extensions/render_barcode.py | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) (limited to 'share') diff --git a/share/extensions/render_barcode.py b/share/extensions/render_barcode.py index 7d4c671c0..c3b809c9e 100755 --- a/share/extensions/render_barcode.py +++ b/share/extensions/render_barcode.py @@ -1,21 +1,25 @@ #!/usr/bin/env python -''' -Copyright (C) 2007 Martin Owens - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -''' +# +# Copyright (C) 2007 Martin Owens +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +""" +Inkscape's general barcode extension. Run from within inkscape or use the +Barcode module provided for outside or scripting. +""" import inkex import sys -- cgit v1.2.3 From 8c06bb6d291a85f89ee0f09953211e07c4aeaf60 Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Mon, 17 Mar 2014 10:30:46 -0400 Subject: New extension: Merge styles, take the common styles of the selected elements and create a css style from them. (bzr r13161) --- share/extensions/merge_styles.inx | 21 ++++++ share/extensions/merge_styles.py | 134 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 share/extensions/merge_styles.inx create mode 100755 share/extensions/merge_styles.py (limited to 'share') diff --git a/share/extensions/merge_styles.inx b/share/extensions/merge_styles.inx new file mode 100644 index 000000000..02da12221 --- /dev/null +++ b/share/extensions/merge_styles.inx @@ -0,0 +1,21 @@ + + + <_name>Merge Styles into CSS + org.inkscape.stylesheet.merge + inkex.py + merge_styles.py + + <_param name="introduction" type="description">All selected nodes will be grouped together and their common style attributes will create a new class, this class will replace the existing inline style attributes. Please use a name which best describes the kinds of objects and their common context for best effect. + + class1 + + + all + + + + + + diff --git a/share/extensions/merge_styles.py b/share/extensions/merge_styles.py new file mode 100755 index 000000000..1872e7f3b --- /dev/null +++ b/share/extensions/merge_styles.py @@ -0,0 +1,134 @@ +#!/usr/bin/env python +# +# Copyright (C) 2014 Martin Owens +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +""" +Merges styles into class based styles and removes. +""" + +import inkex +import sys + +from collections import defaultdict + +class Style(dict): + """Controls the style/css mechanics for this effect""" + def __init__(self, attr=None): + self.weights = defaultdict(int) + self.total = [] + if attr: + self.parse(attr) + + def parse(self, attr): + for name,value in [ a.split(':',1) for a in attr.split(';') if ':' in a ]: + self[name.strip()] = value.strip() + + def entries(self): + return [ "%s:%s;" % (n,v) for (n,v) in self.iteritems() ] + + def to_str(self, sep="\n "): + return " " + "\n ".join(self.entries()) + + def __str__(self): + return self.to_str() + + def css(self, cls): + return ".%s {\n%s\n}" % (cls, str(self)) + + def remove(self, keys): + for key in keys: + self.pop(key, None) + + def add(self, c, el): + self.total.append( (c, el) ) + for name,value in c.iteritems(): + if not self.has_key(name): + self[name] = value + if self[name] == value: + self.weights[name] += 1 + + def clean(self, threshold): + """Removes any elements that aren't the same using a weighted threshold""" + for attr in self.keys(): + if self.weights[attr] < len(self.total) - threshold: + common.pop(attr) + + def all_matches(self): + """Returns an iter for each added element who's style matches this style""" + for (c, el) in self.total: + if self == c: + yield (c, el) + + def __eq__(self, o): + """Not equals, prefer to overload 'in' but that doesn't seem possible""" + for arg in self.keys(): + if o.has_key(arg) and self[arg] != o[arg]: + return False + return True + + +def get_styles(document): + nodes = [] + for node in document.getroot().iterchildren(): + if node.tag == inkex.addNS('style', 'svg'): + return node + nodes.append(node) + ret = inkex.etree.SubElement(document.getroot(), 'style', {}) + # Reorder to make the style element FIRST + for node in nodes: + document.getroot().append(node) + return ret + + +class MergeStyles(inkex.Effect): + def __init__(self): + inkex.Effect.__init__(self) + self.OptionParser.add_option("-n", "--name", + action="store", type="string", + dest="name", default='', + help="Name of selected element's common class") + + def effect(self): + newclass = self.options.name + elements = self.selected.values() + common = Style() + threshold = 1 + + for el in elements: + common.add(Style(el.attrib['style']), el) + common.clean(threshold) + + if not common: + raise KeyError("There are no common styles between these elements.") + + styles = get_styles(self.document) + styles.text = (styles.text or "") + "\n" + common.css( newclass ) + + for (st, el) in common.all_matches(): + st.remove(common.keys()) + el.attrib['style'] = st.to_str("") + + olds = el.attrib.has_key('class') and el.attrib['class'].split() or [] + if newclass not in olds: + olds.append(newclass) + el.attrib['class'] = ' '.join(olds) + + +if __name__ == '__main__': + e = MergeStyles() + e.affect() + -- cgit v1.2.3 From d19b75151578aaedd4350a819b61f96bbe225753 Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Tue, 18 Mar 2014 08:51:31 -0400 Subject: Fix weighted removal of attributes from style in merge extension. (bzr r13164) --- share/extensions/merge_styles.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'share') diff --git a/share/extensions/merge_styles.py b/share/extensions/merge_styles.py index 1872e7f3b..f028bf4ce 100755 --- a/share/extensions/merge_styles.py +++ b/share/extensions/merge_styles.py @@ -65,7 +65,7 @@ class Style(dict): """Removes any elements that aren't the same using a weighted threshold""" for attr in self.keys(): if self.weights[attr] < len(self.total) - threshold: - common.pop(attr) + self.pop(attr) def all_matches(self): """Returns an iter for each added element who's style matches this style""" -- cgit v1.2.3