summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFelipe Corr??a da Silva Sanches <juca@members.fsf.org>2011-05-20 04:28:36 +0000
committerFelipe C. da S. Sanches <juca@members.fsf.org>2011-05-20 04:28:36 +0000
commite0c6a44e2cf0b5834d246fa4765039fd759a6984 (patch)
treee6ba335426a5da902ba5bd7d776d8afacaea4113
parentextension to add a glyph layer. It is rather simple now, but may be more usef... (diff)
downloadinkscape-e0c6a44e2cf0b5834d246fa4765039fd759a6984.tar.gz
inkscape-e0c6a44e2cf0b5834d246fa4765039fd759a6984.zip
Extension that generates an SVGFont from a set of layers with id="GlyphLayer-<unicode char>"
(bzr r10214)
-rw-r--r--share/extensions/Makefile.am2
-rw-r--r--share/extensions/layers2svgfont.inx16
-rw-r--r--share/extensions/layers2svgfont.py102
3 files changed, 120 insertions, 0 deletions
diff --git a/share/extensions/Makefile.am b/share/extensions/Makefile.am
index 4beb0e663..a4d41b836 100644
--- a/share/extensions/Makefile.am
+++ b/share/extensions/Makefile.am
@@ -95,6 +95,7 @@ extensions = \
jessyInk_video.py \
jessyInk_view.py \
launch_webbrowser.py \
+ layers2svgfont.py \
lindenmayer.py \
lorem_ipsum.py \
markers_strokepaint.py \
@@ -265,6 +266,7 @@ modules = \
jessyInk_uninstall.inx \
jessyInk_video.inx \
jessyInk_view.inx \
+ layers2svgfont.inx \
lindenmayer.inx \
lorem_ipsum.inx \
markers_strokepaint.inx \
diff --git a/share/extensions/layers2svgfont.inx b/share/extensions/layers2svgfont.inx
new file mode 100644
index 000000000..06b0a9a67
--- /dev/null
+++ b/share/extensions/layers2svgfont.inx
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
+ <_name>Convert Glyph Layers to SVG Font</_name>
+ <id>org.inkscape.typography.layers2svgfont</id>
+ <dependency type="executable" location="extensions">inkex.py</dependency>
+ <dependency type="executable" location="extensions">layers2svgfont.py</dependency>
+ <effect>
+ <object-type>all</object-type>
+ <effects-menu>
+ <submenu _name="Typography"/>
+ </effects-menu>
+ </effect>
+ <script>
+ <command reldir="extensions" interpreter="python">layers2svgfont.py</command>
+ </script>
+</inkscape-extension>
diff --git a/share/extensions/layers2svgfont.py b/share/extensions/layers2svgfont.py
new file mode 100644
index 000000000..b53cf241c
--- /dev/null
+++ b/share/extensions/layers2svgfont.py
@@ -0,0 +1,102 @@
+'''
+Copyright (C) 2011 Felipe Correa da Silva Sanches
+
+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
+'''
+
+import inkex
+import sys
+
+logfile = open("/tmp/inkscape-ext-logfile", "wa")
+def LOG(s):
+ logfile.write(s+"\n")
+
+class Layers2SVGFont(inkex.Effect):
+ def __init__(self):
+ inkex.Effect.__init__(self)
+
+ def guideline_value(self, label, index):
+ namedview = self.svg.find(inkex.addNS('namedview', 'sodipodi'))
+ guides = namedview.findall(inkex.addNS('guide', 'sodipodi'))
+ for guide in guides:
+ l=guide.get(inkex.addNS('label', 'inkscape'))
+ if l==label:
+ return int(guide.get("position").split(",")[index])
+ return 0
+
+ def get_or_create(self, parentnode, nodetype):
+ node = parentnode.find(nodetype)
+ if node is None:
+ node = inkex.etree.SubElement(parentnode, nodetype)
+ return node
+
+ def get_or_create_glyph(self, font, unicode_char):
+ glyphs = font.findall(inkex.addNS('glyph', 'svg'))
+ for glyph in glyphs:
+ if unicode_char == glyph.get("unicode"):
+ return glyph
+ return inkex.etree.SubElement(font, inkex.addNS('glyph', 'svg'))
+
+ def effect(self):
+ # Get access to main SVG document element
+ self.svg = self.document.getroot()
+ self.defs = self.get_or_create(self.svg, inkex.addNS('defs', 'svg'))
+
+ setwidth = int(self.svg.get("width"))
+ baseline = self.guideline_value("baseline", 1)
+ ascender = self.guideline_value("ascender", 1) - baseline
+ caps = self.guideline_value("caps", 1) - baseline
+ xheight = self.guideline_value("xheight", 1) - baseline
+ descender = self.guideline_value("descender", 1) - baseline
+ lbearing = self.guideline_value("lbearing", 0)
+ rbearing = setwidth - self.guideline_value("rbearing", 0)
+
+ LOG("setwidth: " + str(setwidth))
+ LOG("baseline: " + str(baseline))
+ LOG("ascender: " + str(ascender))
+ LOG("descender: " + str(descender))
+ LOG("caps: " + str(caps))
+ LOG("xheight: " + str(xheight))
+ LOG("lbearing: " + str(lbearing))
+ LOG("rbearing: " + str(rbearing))
+
+ font = self.get_or_create(self.defs, inkex.addNS('font', 'svg'))
+ font.set("horiz-adv-x", str(setwidth))
+
+ fontface = self.get_or_create(font, inkex.addNS('fontface', 'svg'))
+ fontface.set("units-per-em", str(setwidth))
+ fontface.set("cap-height", str(caps))
+ fontface.set("x-height", str(xheight))
+ fontface.set("ascent", str(ascender))
+ fontface.set("descent", str(descender))
+ #TODO: should we somehow store sidebearing values?
+
+ groups = self.svg.findall(inkex.addNS('g', 'svg'))
+ for group in groups:
+ label = group.get(inkex.addNS('label', 'inkscape'))
+ if "GlyphLayer-" in label:
+ unicode_char = label.split("GlyphLayer-")[1]
+ glyph = self.get_or_create_glyph(font, unicode_char)
+ glyph.set("unicode", unicode_char)
+ use = self.get_or_create(glyph, inkex.addNS('use', 'svg'))
+ use.set(inkex.addNS('href', 'xlink'), "#"+group.get("id"))
+ #TODO: This code creates nodes but they do not render on svg fonts dialog. why?
+
+if __name__ == '__main__':
+ LOG("\n\n"+"*"*80+"\n\n")
+ e = Layers2SVGFont()
+ e.affect()
+ logfile.close()
+