From 0d5d786360b64d4e789b4311fe54f1180a6c302a Mon Sep 17 00:00:00 2001 From: James Godfrey-Kittle Date: Fri, 13 Nov 2015 13:37:56 -0800 Subject: [italics] Fix whitespace --- scripts/lib/fontbuild/italics.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'scripts/lib/fontbuild/italics.py') diff --git a/scripts/lib/fontbuild/italics.py b/scripts/lib/fontbuild/italics.py index c889bd5..b1db984 100644 --- a/scripts/lib/fontbuild/italics.py +++ b/scripts/lib/fontbuild/italics.py @@ -33,11 +33,11 @@ def italicizeGlyph(f, g, angle=10, stemWidth=185): m = Transform(1, 0, slope, 1, 0, 0) xoffset, junk = m.transformPoint((0, MEAN_YCENTER)) m = Transform(.97, 0, slope, 1, xoffset, 0) - + if len(glyph) > 0: g2 = italicize(f[g.name], angle, xoffset=xoffset, stemWidth=stemWidth) f.insertGlyph(g2, g.name) - + transformFLGlyphMembers(f[g.name], m) if unic > 0xFFFF: #restore unicode @@ -53,12 +53,12 @@ def italicize(glyph, angle=12, stemWidth=180, xoffset=-50): grad = mapEdges(lambda a,(p,n): normalize(p-a), va, e) cornerWeights = mapEdges(lambda a,(p,n): normalize(p-a).dot(normalize(a-n)), grad, e)[:,0].reshape((-1,1)) smooth = np.ones((n,1)) * CURVE_CORRECTION_WEIGHT - + controlPoints = findControlPointsInMesh(glyph, va, subsegments) smooth[controlPoints > 0] = 1 smooth[cornerWeights < .6] = CORNER_WEIGHT # smooth[cornerWeights >= .9999] = 1 - + out = va.copy() hascurves = False for c in glyph.contours: @@ -80,13 +80,13 @@ def italicize(glyph, angle=12, stemWidth=180, xoffset=-50): centerSkew = skewMesh(center.dot(np.array([[.97,0],[0,1]])), angle * .9) out = outCorrected + (centerSkew - center) out[:,1] = outCorrected[:,1] - + smooth = np.ones((n,1)) * .1 out = alignCorners(glyph, out, subsegments) out = copyMeshDetails(skewMesh(va, angle), out, e, 7, smooth=smooth) # grad = mapEdges(lambda a,(p,n): normalize(p-a), skewMesh(outCorrected, angle*.9), e) # out = recompose(out, grad, e, smooth=smooth) - + out = skewMesh(out, angle * .1) out[:,0] += xoffset # out[:,1] = outCorrected[:,1] @@ -132,6 +132,7 @@ def glyphToMesh(g): offset += len(c) return np.array(points), edges + def meshToGlyph(points, g): g1 = g.copy() j = 0 @@ -144,6 +145,7 @@ def meshToGlyph(points, g): j += 1 return g1 + def quantizeGradient(grad, book=None): if book == None: book = np.array([(1,0),(0,1),(0,-1),(-1,0)]) @@ -153,6 +155,7 @@ def quantizeGradient(grad, book=None): out[i] = normalize(v) return out + def findControlPointsInMesh(glyph, va, subsegments): controlPointIndices = np.zeros((len(va),1)) index = 0 @@ -166,7 +169,6 @@ def findControlPointsInMesh(glyph, va, subsegments): return controlPointIndices - def recompose(v, grad, e, smooth=1, P=None, distance=None): n = len(v) if distance == None: @@ -183,6 +185,7 @@ def recompose(v, grad, e, smooth=1, P=None, distance=None): out[:,i] = cg(P, f[:,i])[0] return out + def mP(v,e): n = len(v) M = np.zeros((n,n)) @@ -193,18 +196,21 @@ def mP(v,e): M[i,i] = 2 return M + def normalize(v): n = np.linalg.norm(v) if n == 0: return v return v/n + def mapEdges(func,v,e,*args): b = v.copy() for i, edges in e.iteritems(): b[i] = func(v[i], [v[j] for j in edges], *args) return b + def getNormal(a,b,c): "Assumes TT winding direction" p = np.roll(normalize(b - a), 1) @@ -214,19 +220,23 @@ def getNormal(a,b,c): # print p, n, normalize((p + n) * .5) return normalize((p + n) * .5) + def edgeNormals(v,e): "Assumes a mesh where each vertex has exactly least two edges" return mapEdges(lambda a,(p,n) : getNormal(a,p,n),v,e) + def rangePrevNext(count): c = np.arange(count,dtype=int) r = np.vstack((c, np.roll(c, 1), np.roll(c, -1))) return r.T + def skewMesh(v,angle): slope = np.tanh([math.pi * angle / 180]) return v.dot(np.array([[1,0],[slope,1]])) + def labelConnected(e): label = 0 labels = np.zeros((len(e),1)) @@ -236,6 +246,7 @@ def labelConnected(e): label += 1 return labels + def copyGradDetails(a,b,e,scale=15): n = len(a) labels = labelConnected(e) @@ -245,6 +256,7 @@ def copyGradDetails(a,b,e,scale=15): out[mask,:] = gaussian(b[mask,:], scale, mode="wrap", axis=0) + a[mask,:] - gaussian(a[mask,:], scale, mode="wrap", axis=0) return out + def copyMeshDetails(va,vb,e,scale=5,smooth=.01): gradA = mapEdges(lambda a,(p,n): normalize(p-a), va, e) gradB = mapEdges(lambda a,(p,n): normalize(p-a), vb, e) @@ -253,8 +265,6 @@ def copyMeshDetails(va,vb,e,scale=5,smooth=.01): return recompose(vb, grad, e, smooth=smooth) - - def condenseGlyph(glyph, scale=.8, stemWidth=185): ga, subsegments = segmentGlyph(glyph, 25) va, e = glyphToMesh(ga) @@ -273,7 +283,7 @@ def condenseGlyph(glyph, scale=.8, stemWidth=185): # cornerWeights = mapEdges(lambda a,(p,n): normalize(p-a).dot(normalize(a-n)), grad, e)[:,0].reshape((-1,1)) # smooth = np.ones((n,1)) * .1 # smooth[cornerWeights < .6] = 10 - # + # # grad2 = quantizeGradient(grad).astype(float) # grad2 = copyGradDetails(grad, grad2, e, scale=10) # grad2 = mapEdges(lambda a,e: normalize(a), grad2, e) -- cgit v1.2.3 From ceb4ae1b523cb0160dbf2ac57b770db0761c68d9 Mon Sep 17 00:00:00 2001 From: James Godfrey-Kittle Date: Fri, 13 Nov 2015 13:39:25 -0800 Subject: [italics] Fix import order, remove unused imports --- scripts/lib/fontbuild/italics.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'scripts/lib/fontbuild/italics.py') diff --git a/scripts/lib/fontbuild/italics.py b/scripts/lib/fontbuild/italics.py index b1db984..83b123f 100644 --- a/scripts/lib/fontbuild/italics.py +++ b/scripts/lib/fontbuild/italics.py @@ -13,12 +13,18 @@ # limitations under the License. +import math + from fontTools.misc.transform import Transform -from robofab.world import RFont -from time import clock import numpy as np -import math -from alignpoints import alignCorners +from numpy.linalg import norm +from scipy.sparse.linalg import cg +from scipy.ndimage.filters import gaussian_filter1d as gaussian +from scipy.cluster.vq import vq, whiten + +from fontbuild.alignpoints import alignCorners +from fontbuild.curveFitPen import fitGlyph, segmentGlyph + def italicizeGlyph(f, g, angle=10, stemWidth=185): unic = g.unicode #save unicode @@ -111,13 +117,7 @@ def transformFLGlyphMembers(g, m, transformAnchors = True): a.x = aa[0] # a.x,a.y = (aa[0] - p[0], aa[1] - p[1]) # a.x = a.x - m[4] - -from curveFitPen import fitGlyph,segmentGlyph -from numpy.linalg import norm -from scipy.sparse.linalg import cg -from scipy.ndimage.filters import gaussian_filter1d as gaussian -from scipy.cluster.vq import vq, kmeans2, whiten def glyphToMesh(g): points = [] -- cgit v1.2.3 From b9db24ac21becece78cdf011921d1416046dc159 Mon Sep 17 00:00:00 2001 From: James Godfrey-Kittle Date: Thu, 19 Nov 2015 15:42:48 -0800 Subject: [italics] Add some comments It's not much, but maybe a start.... --- scripts/lib/fontbuild/italics.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'scripts/lib/fontbuild/italics.py') diff --git a/scripts/lib/fontbuild/italics.py b/scripts/lib/fontbuild/italics.py index 83b123f..51d1f96 100644 --- a/scripts/lib/fontbuild/italics.py +++ b/scripts/lib/fontbuild/italics.py @@ -53,6 +53,8 @@ def italicizeGlyph(f, g, angle=10, stemWidth=185): def italicize(glyph, angle=12, stemWidth=180, xoffset=-50): CURVE_CORRECTION_WEIGHT = .03 CORNER_WEIGHT = 10 + + # decompose the glyph into smaller segments ga, subsegments = segmentGlyph(glyph,25) va, e = glyphToMesh(ga) n = len(va) @@ -79,14 +81,19 @@ def italicize(glyph, angle=12, stemWidth=180, xoffset=-50): # out = copyMeshDetails(va, out, e, 6) else: outCorrected = out + + # create a transform for italicizing normals = edgeNormals(out, e) center = va + normals * stemWidth * .4 if stemWidth > 130: center[:, 0] = va[:, 0] * .7 + center[:,0] * .3 centerSkew = skewMesh(center.dot(np.array([[.97,0],[0,1]])), angle * .9) + + # apply the transform out = outCorrected + (centerSkew - center) out[:,1] = outCorrected[:,1] + # make some corrections smooth = np.ones((n,1)) * .1 out = alignCorners(glyph, out, subsegments) out = copyMeshDetails(skewMesh(va, angle), out, e, 7, smooth=smooth) @@ -101,6 +108,8 @@ def italicize(glyph, angle=12, stemWidth=180, xoffset=-50): # gOut.width *= .97 # gOut.width += 10 # return gOut + + # recompose the glyph into original segments return fitGlyph(glyph, gOut, subsegments) -- cgit v1.2.3