Ejemplo n.º 1
0
 def _get_clockwise(self):
     if self._clockwise is None:
         pen = AreaPen()
         pen.endPath = pen.closePath
         self.draw(pen)
         self._clockwise = pen.value < 0
     return self._clockwise
Ejemplo n.º 2
0
 def _get_clockwise(self):
     if self._clockwise is None:
         pen = AreaPen()
         pen.endPath = pen.closePath
         self.draw(pen)
         self._clockwise = pen.value < 0
     return self._clockwise
Ejemplo n.º 3
0
def glyphs_surface_area(font):
    """Calculate the surface area of a glyph's ink"""
    glyphs = {}
    glyph_set = font.getGlyphSet()
    area_pen = AreaPen(glyph_set)

    for glyph in glyph_set.keys():
        glyph_set[glyph].draw(area_pen)

        area = area_pen.value
        area_pen.value = 0
        glyphs[glyph] = area
    return glyphs
Ejemplo n.º 4
0
def calculateGlyphCoverage(glyph, font=None, cache=None):
    """
        Area of the glyph / area of the (font.em * glyph.width)
        This calculates the coverage of any glyph.
    """
    if cache is None:
        cache = {}
    if glyph.width == 0:
        return 0
    if font is None:
        font = glyph.font
        if font is None:
            return 0
    if font.info.unitsPerEm == 0:
        return 0
    if glyph.name in cache:
        new = cache.get(glyph.name)
    else:
        new = decomposeRemoveOverlapFactory(glyph, font)
        cache[glyph.name] = new
    if new.bounds is None:
        return 0
    p = AreaPen(font)
    try:
        new.draw(p)
    except NotImplementedError:
        print("caught NotImplementedError in areaPen draw", glyph.name)
        return None
    coverage = p.value / (font.info.unitsPerEm * glyph.width)
    return coverage
Ejemplo n.º 5
0
 def _get_area(self):
     """
     Subclasses may override this method.
     """
     from fontTools.pens.areaPen import AreaPen
     pen = AreaPen(self.layer)
     self.draw(pen)
     return abs(pen.value)
Ejemplo n.º 6
0
def glyphs_modified(font_before, font_after):
    """For glyphs which are shared between the two fonts, return glyphs
    who's surface areas differ and the diff is greater than a
    predetermined threshold"""
    bad_glyphs = []
    glyphs_before = font_before.getGlyphSet()
    glyphs_after = font_after.getGlyphSet()
    glyphs_shared = set(glyphs_before.keys()) & set(glyphs_after.keys())

    upm_before = font_before['head'].unitsPerEm
    upm_after = font_after['head'].unitsPerEm

    pen_before = AreaPen(glyphs_before)
    pen_after = AreaPen(glyphs_after)

    for glyph in glyphs_shared:
        glyphs_before[glyph].draw(pen_before)
        glyphs_after[glyph].draw(pen_after)

        area_before = pen_before.value
        pen_before.value = 0
        area_after = pen_after.value
        pen_after.value = 0

        area_norm_before = (area_before / upm_before) * upm_after
        area_norm_after = (area_after / upm_after) * upm_before

        if area_norm_before != area_norm_after:
            if abs(area_norm_before - area_norm_after) > GLYPH_AREA_THRESHOLD:
                bad_glyphs.append(glyph)

    glyphs_encoded = font_before['cmap'].getcmap(3, 1).cmap
    return [i for i in glyphs_encoded.items() if i[1] in bad_glyphs]
Ejemplo n.º 7
0
def glyph_area(glyphset, glyph):
    """Get the surface area of a glyph"""
    pen = AreaPen(glyphset)
    glyphset[glyph].draw(pen)
    return int(pen.value)
def contourAreaRepresentationFactory(contour):
    pen = AreaPen()
    pen._endPath = pen._closePath
    contour.draw(pen)
    return pen.value
def glyphAreaRepresentationFactory(glyph):
    pen = AreaPen(glyph.layer)
    glyph.draw(pen)
    return abs(pen.value)
Ejemplo n.º 10
0
    def test_openPaths(self):
        pen = AreaPen()
        pen.moveTo((0, 0))
        pen.endPath()
        self.assertEqual(0, pen.value)

        pen.moveTo((0, 0))
        pen.lineTo((1, 0))
        with self.assertRaises(NotImplementedError):
            pen.endPath()
Ejemplo n.º 11
0
 def test_TTcontour_counterclockwise_line_first(self):
     pen = AreaPen(None)
     draw8_(pen)
     self.assertEqual(104602.791667, round(pen.value, precision))
Ejemplo n.º 12
0
 def test_PScontour_counterclockwise_line_first(self):
     pen = AreaPen(None)
     draw4_(pen)
     self.assertEqual(104561.35, round(pen.value, precision))
Ejemplo n.º 13
0
 def _get_clockwise(self):
     from fontTools.pens.areaPen import AreaPen
     pen = AreaPen()
     pen.endPath = pen.closePath
     self.drawToPen(pen)
     return pen.value < 0
Ejemplo n.º 14
0
def contourClockwiseRepresentationFactory(contour):
    pen = AreaPen()
    pen.endPath = pen.closePath
    contour.draw(pen)
    return pen.value < 0
Ejemplo n.º 15
0
def contourClockwiseRepresentationFactory(contour):
    pen = AreaPen()
    pen.endPath = pen.closePath
    contour.draw(pen)
    return pen.value < 0
Ejemplo n.º 16
0
 def _get_area(self):
     pen = AreaPen()
     self.naked().draw(pen)
     return abs(pen.value)
Ejemplo n.º 17
0
    def test_openPaths(self):
        pen = AreaPen()
        pen.moveTo((0, 0))
        pen.endPath()
        self.assertEqual(0, pen.value)

        pen.moveTo((0, 0))
        pen.lineTo((1, 0))
        with self.assertRaises(NotImplementedError):
            pen.endPath()