From 17c57fef3ed9da6bf7b1596bc6e898b85fc7a757 Mon Sep 17 00:00:00 2001 From: Shaunak Kishore Date: Wed, 2 Sep 2015 03:33:35 -0400 Subject: [PATCH] Fix stroke extraction and add orientation override --- client/glyph.js | 4 ++++ scripts/stroke_extractor.py | 13 ++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/client/glyph.js b/client/glyph.js index bd31fcf3..528f0ed0 100644 --- a/client/glyph.js +++ b/client/glyph.js @@ -19,6 +19,10 @@ function change_glyph(method, glyph) { }); } +window.get_glyph = function(name) { + change_glyph('get_glyph', name); +} + function to_line(pairs) { return { x1: pairs[0][0], diff --git a/scripts/stroke_extractor.py b/scripts/stroke_extractor.py index b34cb544..aa4b28c3 100644 --- a/scripts/stroke_extractor.py +++ b/scripts/stroke_extractor.py @@ -35,6 +35,11 @@ MAX_CORNER_MERGE_DISTANCE = 16 MIN_CORNER_ANGLE = 0.1*math.pi MIN_CORNER_TANGENT_DISTANCE = 4 +# Some glyphs in the font have strokes that incorrectly curve clockwise. +# To handle these glyphs, we store a list of glyph names and stroke indices that +# should be reversed during the call to split_and_orient_path. +PATH_ORDER_MISTAKES = {'U9BFE': [4, 5, 6]} + def area(path): ''' @@ -46,7 +51,7 @@ def area(path): return int(sum(map(area_under_curve, path))/2) -def split_and_orient_path(path): +def split_and_orient_path(name, path): ''' Takes a non-empty svg.path.Path object that may contain multiple closed loops. Returns a list of svg.path.Path objects that are all minimal closed curve. @@ -71,6 +76,8 @@ def split_and_orient_path(path): max_area = max((abs(area), area) for area in areas)[1] if max_area < 0: paths = map(reverse, paths) + for i in PATH_ORDER_MISTAKES.get(name, []): + paths[i] = reverse(paths[i]) return [svg.path.Path(*path) for path in paths] @@ -200,7 +207,7 @@ class StrokeExtractor(object): def __init__(self, name, d, manual=None): self.name = name self.messages = [] - self.paths = split_and_orient_path(svg.path.parse_path(d)) + self.paths = split_and_orient_path(name, svg.path.parse_path(d)) self.corners = self.get_corners() self.bridges = self.get_bridges() if manual: @@ -268,7 +275,7 @@ class StrokeExtractor(object): def angle(index, bridge): tangent = self.corners[index].tangent1 ratio = (self.corners[bridge].point - self.corners[index].point)/tangent - return abs(math.atan2(ratio.imag, ratio.real)) + return math.atan2(ratio.imag, ratio.real) while True: # Add the current stroke element to the path and advance along it.