[symfont] Change GlyphStatistics object into StatisticsPen
It's a simple pen now. To be moved to fontTools.pens next.
This commit is contained in:
parent
c14aad81f4
commit
5cbfaa2792
@ -9,7 +9,7 @@ from __future__ import print_function, division, absolute_import
|
|||||||
from fontTools.misc.py23 import *
|
from fontTools.misc.py23 import *
|
||||||
|
|
||||||
from fontTools.pens.basePen import BasePen
|
from fontTools.pens.basePen import BasePen
|
||||||
from symfont import GlyphStatistics
|
from symfont import StatisticsPen
|
||||||
import itertools
|
import itertools
|
||||||
|
|
||||||
class PerContourOrComponentPen(BasePen):
|
class PerContourOrComponentPen(BasePen):
|
||||||
@ -134,7 +134,8 @@ def test(glyphsets, glyphs=None, names=None):
|
|||||||
contourVectors = []
|
contourVectors = []
|
||||||
allVectors.append(contourVectors)
|
allVectors.append(contourVectors)
|
||||||
for contour in contourPens:
|
for contour in contourPens:
|
||||||
stats = GlyphStatistics(contour, glyphset=glyphset)
|
stats = StatisticsPen(glyphset=glyphset)
|
||||||
|
contour.draw(stats)
|
||||||
size = abs(stats.area) ** .5 * .5
|
size = abs(stats.area) ** .5 * .5
|
||||||
vector = (
|
vector = (
|
||||||
int(size),
|
int(size),
|
||||||
|
@ -192,61 +192,48 @@ MomentXYPen = partial(GreenPen, func=x*y)
|
|||||||
# Glyph statistics object
|
# Glyph statistics object
|
||||||
#
|
#
|
||||||
|
|
||||||
class GlyphStatistics(object):
|
class StatisticsPen(MomentsPen):
|
||||||
|
|
||||||
def __init__(self, glyph, transform=None, glyphset=None):
|
|
||||||
self._glyph = glyph
|
|
||||||
self._glyphset = glyphset
|
|
||||||
self._transform = transform
|
|
||||||
|
|
||||||
Pen = MomentsPen
|
|
||||||
pen = transformer = Pen(glyphset=self._glyphset)
|
|
||||||
if self._transform:
|
|
||||||
transformer = TransformPen(pen, self._transform)
|
|
||||||
self._glyph.draw(transformer)
|
|
||||||
self.m = m = pen
|
|
||||||
|
|
||||||
self.area = area = m.area
|
|
||||||
self.momentX = m.momentX
|
|
||||||
self.momentY = m.momentY
|
|
||||||
self.momentXX = m.momentXX
|
|
||||||
self.momentXY = m.momentXY
|
|
||||||
self.momentYY = m.momentYY
|
|
||||||
|
|
||||||
if not area:
|
|
||||||
self.meanX = 0.
|
|
||||||
self.meanY = 0.
|
|
||||||
self.varianceX = 0.
|
|
||||||
self.varianceY = 0.
|
|
||||||
self.stddevX = 0.
|
|
||||||
self.stddevY = 0.
|
|
||||||
self.covariance = 0.
|
|
||||||
self.correlation = 0.
|
|
||||||
self.slant = 0.
|
|
||||||
return
|
|
||||||
|
|
||||||
# Center of mass
|
# Center of mass
|
||||||
# https://en.wikipedia.org/wiki/Center_of_mass#A_continuous_volume
|
# https://en.wikipedia.org/wiki/Center_of_mass#A_continuous_volume
|
||||||
self.meanX = self.momentX / area
|
@property
|
||||||
self.meanY = self.momentY / area
|
def meanX(self):
|
||||||
|
return self.momentX / self.area if self.area else 0
|
||||||
|
@property
|
||||||
|
def meanY(self):
|
||||||
|
return self.momentY / self.area if self.area else 0
|
||||||
|
|
||||||
# Var(X) = E[X^2] - E[X]^2
|
# Var(X) = E[X^2] - E[X]^2
|
||||||
self.varianceX = self.momentXX / area - self.meanX**2
|
@property
|
||||||
self.varianceY = self.momentYY / area - self.meanY**2
|
def varianceX(self):
|
||||||
|
return self.momentXX / self.area - self.meanX**2 if self.area else 0
|
||||||
|
@property
|
||||||
|
def varianceY(self):
|
||||||
|
return self.momentYY / self.area - self.meanY**2 if self.area else 0
|
||||||
|
|
||||||
self.stddevX = math.copysign(abs(self.varianceX)**.5, self.varianceX)
|
@property
|
||||||
self.stddevY = math.copysign(abs(self.varianceY)**.5, self.varianceY)
|
def stddevX(self):
|
||||||
|
return math.copysign(abs(self.varianceX)**.5, self.varianceX)
|
||||||
|
@property
|
||||||
|
def stddevY(self):
|
||||||
|
return math.copysign(abs(self.varianceY)**.5, self.varianceY)
|
||||||
|
|
||||||
# Covariance(X,Y) = ( E[X.Y] - E[X]E[Y] )
|
# Covariance(X,Y) = ( E[X.Y] - E[X]E[Y] )
|
||||||
self.covariance = self.momentXY / area - self.meanX*self.meanY
|
@property
|
||||||
|
def covariance(self):
|
||||||
|
return self.momentXY / self.area - self.meanX*self.meanY if self.area else 0
|
||||||
|
|
||||||
# Correlation(X,Y) = Covariance(X,Y) / ( stddev(X) * stddev(Y)) )
|
# Correlation(X,Y) = Covariance(X,Y) / ( stddev(X) * stddev(Y) )
|
||||||
# https://en.wikipedia.org/wiki/Pearson_product-moment_correlation_coefficient
|
# https://en.wikipedia.org/wiki/Pearson_product-moment_correlation_coefficient
|
||||||
correlation = self.covariance / (self.stddevX * self.stddevY)
|
@property
|
||||||
self.correlation = correlation if abs(correlation) > 1e-3 else 0
|
def correlation(self):
|
||||||
|
correlation = self.covariance / (self.stddevX * self.stddevY) if self.area else 0
|
||||||
|
return correlation if abs(correlation) > 1e-3 else 0
|
||||||
|
|
||||||
slant = self.covariance / self.varianceY
|
@property
|
||||||
self.slant = slant if abs(slant) > 1e-3 else 0
|
def slant(self):
|
||||||
|
slant = self.covariance / self.varianceY if self.area else 0
|
||||||
|
return slant if abs(slant) > 1e-3 else 0
|
||||||
|
|
||||||
|
|
||||||
def test(glyphset, upem, glyphs):
|
def test(glyphset, upem, glyphs):
|
||||||
@ -256,10 +243,12 @@ def test(glyphset, upem, glyphs):
|
|||||||
print()
|
print()
|
||||||
print("glyph:", glyph_name)
|
print("glyph:", glyph_name)
|
||||||
glyph = glyphset[glyph_name]
|
glyph = glyphset[glyph_name]
|
||||||
stats = GlyphStatistics(glyph, transform=Scale(1./upem), glyphset=glyphset)
|
pen = StatisticsPen(glyphset=glyphset)
|
||||||
for item in dir(stats):
|
transformer = TransformPen(pen, Scale(1./upem))
|
||||||
|
glyph.draw(transformer)
|
||||||
|
for item in ['area', 'momentX', 'momentY', 'momentXX', 'momentYY', 'momentXY', 'meanX', 'meanY', 'varianceX', 'varianceY', 'stddevX', 'stddevY', 'covariance', 'correlation', 'slant']:
|
||||||
if item[0] == '_': continue
|
if item[0] == '_': continue
|
||||||
print ("%s: %g" % (item, getattr(stats, item)))
|
print ("%s: %g" % (item, getattr(pen, item)))
|
||||||
|
|
||||||
|
|
||||||
def main(args):
|
def main(args):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user