summaryrefslogtreecommitdiffstats
path: root/share
diff options
context:
space:
mode:
authorFelipe Corr??a da Silva Sanches <juca@members.fsf.org>2011-05-25 19:43:58 +0000
committerFelipe C. da S. Sanches <juca@members.fsf.org>2011-05-25 19:43:58 +0000
commit243ca7a004921ca0b845941d16fbe34c3bdeb734 (patch)
treef83f4aac1cca0df6e2f21a5461dd50ed5a18b207 /share
parentAdd GCC support library to files distributed in the Windows bundle. (diff)
downloadinkscape-243ca7a004921ca0b845941d16fbe34c3bdeb734.tar.gz
inkscape-243ca7a004921ca0b845941d16fbe34c3bdeb734.zip
* extension module to parse and encode path descriptions
* flip y-axis when creating an SVG Font via python-code (bzr r10222)
Diffstat (limited to 'share')
-rw-r--r--share/extensions/PathData.py139
-rw-r--r--share/extensions/layers2svgfont.inx1
-rw-r--r--share/extensions/layers2svgfont.py39
3 files changed, 155 insertions, 24 deletions
diff --git a/share/extensions/PathData.py b/share/extensions/PathData.py
new file mode 100644
index 000000000..762f97173
--- /dev/null
+++ b/share/extensions/PathData.py
@@ -0,0 +1,139 @@
+'''
+Copyright (C) 2011 Felipe Correa da Silva Sanches <juca@members.fsf.org>
+
+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
+'''
+
+class PathData():
+ def __init__ (self, d):
+ self.data = self.parse(d)
+
+ def transform_coordinate_values(self, callback, units_per_em, baseline):
+
+ for d in range(len(self.data)):
+ cmd = self.data[d][0]
+ relative = (cmd.lower()==cmd)
+
+ if cmd.lower() in "mtlsqc":
+ for params in range(len(self.data[d][1])):
+ for values in range(len(self.data[d][1][params])):
+
+ if d==0 and params==0 and values==0:
+ self.data[d][1][params][values] = callback(self.data[d][1][params][values], units_per_em, baseline, False)
+ else:
+ self.data[d][1][params][values] = callback(self.data[d][1][params][values], units_per_em, baseline, relative)
+
+ return self.encode()
+
+ def get_coord_pairs(self, num_pairs, tokens, i, multiple=True):
+ pairs = []
+ for _ in range(num_pairs):
+ x,y = tokens[i].split(",")
+ pairs.append((float(x), float(y)))
+ i+=1
+ coords = [pairs]
+
+ while multiple:
+ try:
+ pairs = []
+ for _ in range(num_pairs):
+ x,y = tokens[i].split(",")
+ pairs.append((float(x), float(y)))
+ i+=1
+ coords.append(pairs)
+ except:
+ return coords, i
+ return coords, i
+
+ def get_coords(self, num_values, tokens, i, multiple=True):
+ values = []
+ for _ in range(num_values):
+ values.append(float(tokens[i]))
+ i+=1
+ coords = [values]
+
+ while multiple:
+ try:
+ values = []
+ for _ in range(num_values):
+ values.append(float(tokens[i]))
+ i+=1
+ coords.append(values)
+ except:
+ return coords, i
+ return coords, i
+
+ def get_arc_coords(tokens, i):
+ return [] #TODO
+
+ def get_tokens(self, d):
+ tokens = []
+ for t in d.split(" "):
+ if len(t):
+ if len(t)>1 and t[0].lower() in "hvmtlsqcaz":
+ tokens.append(t[0])
+ tokens.append(t[1:])
+ else:
+ tokens.append(t)
+ return tokens
+
+ def parse(self, d):
+ data = []
+ tokens = self.get_tokens(d)
+ i=0
+ while i < len(tokens):
+ command = tokens[i]
+ i+=1
+ if command in ["z","Z"]:
+ data.append((command, None))
+ i+=1
+ elif command in ["h","H", "v", "V"]:
+ coords, i = self.get_coords(1, tokens, i)
+ data.append((command, coords))
+ elif command in ["a","A"]:
+ coords, i = self.get_arc_coords(tokens, i)
+ data.append((command, coords))
+ elif command in ["m","M", "t", "T", "l", "L"]:
+ coords, i = self.get_coord_pairs(1, tokens, i)
+ data.append((command, coords))
+ elif command in ["s","S", "q", "Q"]:
+ coords, i = self.get_coord_pairs(2, tokens, i)
+ data.append((command, coords))
+ elif command in ["c","C"]:
+ coords, i = self.get_coord_pairs(3, tokens, i)
+ data.append((command, coords))
+ return data
+
+ def encode(self, data=None):
+ if data is None:
+ data = self.data
+
+ d = ""
+ for cmd, coords in data:
+ d += cmd + " "
+ if cmd.lower() in "mtlsqc":
+ for c in coords:
+ for x,y in c:
+ d += str(x) + "," + str(y) + " "
+ elif cmd.lower() == "z":
+ d += cmd
+ elif cmd.lower() in "hv":
+ for values in coords:
+ for v in values:
+ d += str(v) + " "
+ elif cmd.lower() == "a":
+ pass #TODO
+ return d
+
diff --git a/share/extensions/layers2svgfont.inx b/share/extensions/layers2svgfont.inx
index 06b0a9a67..f38c30ca1 100644
--- a/share/extensions/layers2svgfont.inx
+++ b/share/extensions/layers2svgfont.inx
@@ -3,6 +3,7 @@
<_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">PathData.py</dependency>
<dependency type="executable" location="extensions">layers2svgfont.py</dependency>
<effect>
<object-type>all</object-type>
diff --git a/share/extensions/layers2svgfont.py b/share/extensions/layers2svgfont.py
index 7921ec233..49f81852c 100644
--- a/share/extensions/layers2svgfont.py
+++ b/share/extensions/layers2svgfont.py
@@ -1,5 +1,5 @@
'''
-Copyright (C) 2011 Felipe Correa da Silva Sanches
+Copyright (C) 2011 Felipe Correa da Silva Sanches <juca@members.fsf.org>
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
@@ -18,16 +18,7 @@ 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")
-
-def flip_cordinate_system(d, units_per_em, baseline):
- #todo
- return d
-
-bugged_fontforge_compatibility_mode = True
+from PathData import PathData
class Layers2SVGFont(inkex.Effect):
def __init__(self):
@@ -55,6 +46,18 @@ class Layers2SVGFont(inkex.Effect):
return glyph
return inkex.etree.SubElement(font, inkex.addNS('glyph', 'svg'))
+ def flip_cordinate_system(self, d, units_per_em, baseline):
+ pathdata = PathData(d)
+
+ def flip_cordinates(coordinates, units_per_em, baseline, relative):
+ x, y = coordinates
+ if relative:
+ return (x, -y)
+ else:
+ return (x, units_per_em - baseline - y)
+
+ return pathdata.transform_coordinate_values(flip_cordinates, units_per_em, baseline)
+
def effect(self):
# Get access to main SVG document element
self.svg = self.document.getroot()
@@ -70,15 +73,6 @@ class Layers2SVGFont(inkex.Effect):
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))
font.set("horiz-origin-y", str(baseline))
@@ -107,13 +101,10 @@ class Layers2SVGFont(inkex.Effect):
for p in paths:
path = inkex.etree.SubElement(glyph, inkex.addNS('path', 'svg'))
d = p.get("d")
- if not bugged_fontforge_compatibility_mode:
- d = flip_cordinate_system(d, units_per_em, baseline)
+ d = self.flip_cordinate_system(d, units_per_em, baseline)
path.set("d", d)
if __name__ == '__main__':
- LOG("\n\n"+"*"*80+"\n\n")
e = Layers2SVGFont()
e.affect()
- logfile.close()