Example #1
0
    def checkPair(self, g1, g2):
        """New checking method contributed by Frederik

        Returns a Boolean if overlapping.
        """

        kern = self.getKerning(g1, g2)
        
        # Check sidebearings first (PvB's idea)
        if self.rsb[g1] + self.lsb[g2] + kern > 0:
            return False

        # get the bounds and check them
        bounds1 = g1.box
        if bounds1 is None:
            return False
        bounds2 = g2.box
        if bounds2 is None:
            return False    

        # shift bounds2
        bounds2 = offsetRect(bounds2, g1.width+kern, 0)
        # check for intersection bounds
        intersectingBounds, _ = sectRect(bounds1, bounds2)
        if not intersectingBounds:
            return False
        # move bounds1 back, moving bounds is faster then moving all coordinates in a glyph
        bounds1 = offsetRect(bounds1, -g2.width-kern, 0)

        # create a pen for g1 with a shifted rect, draw the glyph into the pen
        pen1 = findPossibleOverlappingSegmentsPen.FindPossibleOverlappingSegmentsPen(g1.getParent(), bounds2)
        g1.draw(pen1)

        # create a pen for g2 with a shifted rect and move each found segment with the width and kerning
        pen2 = findPossibleOverlappingSegmentsPen.FindPossibleOverlappingSegmentsPen(g2.getParent(), bounds1, (g1.width+kern, 0))
        # draw the glyph into the pen
        g2.draw(pen2)

        for segment1 in pen1.segments:
            for segment2 in pen2.segments:
                if len(segment1) == 4 and len(segment2) == 4:
                    a1, a2, a3, a4 = segment1
                    b1, b2, b3, b4 = segment2
                    result = intersectCubicCubic(a1, a2, a3, a4, b1, b2, b3, b4)
                elif len(segment1) == 4:
                    p1, p2, p3, p4 = segment1
                    a1, a2 = segment2
                    result = intersectCubicLine(p1, p2, p3, p4, a1, a2)
                elif len(segment2) == 4:
                    p1, p2, p3, p4 = segment2
                    a1, a2 = segment1
                    result = intersectCubicLine(p1, p2, p3, p4, a1, a2)
                else:
                    a1, a2 = segment1
                    b1, b2 = segment2
                    result = intersectLineLine(a1, a2, b1, b2)
                if result.status == "Intersection":
                    return True

        return False
def _getSegmentIntersection(bcp, intersection, curve):
    pt0, pt1, pt2, pt3 = curve
    intersection = bezierTools.intersectCubicLine(pt0, pt1, pt2, pt3, bcp, intersection)
    if intersection:
        point = intersection.points[0]
        return point.x, point.y
    return None
Example #3
0
def _getLineCurveIntersection(line, curve):
    points = curve + line
    intersection = rfBezierTools.intersectCubicLine(*points)
    return intersection
Example #4
0
    def checkPair(self, g1, g2):
        """New checking method contributed by Frederik

        Returns a Boolean if overlapping.
        """

        kern = self.getKerning(g1, g2)

        # Check sidebearings first (PvB's idea)
        if self.rsb[g1] + self.lsb[g2] + kern > 0:
            return False

        # get the bounds and check them
        bounds1 = g1.box
        if bounds1 is None:
            return False
        bounds2 = g2.box
        if bounds2 is None:
            return False

        # shift bounds2
        bounds2 = offsetRect(bounds2, g1.width + kern, 0)
        # check for intersection bounds
        intersectingBounds, _ = sectRect(bounds1, bounds2)
        if not intersectingBounds:
            return False
        # move bounds1 back, moving bounds is faster then moving all coordinates in a glyph
        bounds1 = offsetRect(bounds1, -g2.width - kern, 0)

        # create a pen for g1 with a shifted rect, draw the glyph into the pen
        pen1 = findPossibleOverlappingSegmentsPen.FindPossibleOverlappingSegmentsPen(
            g1.getParent(), bounds2)
        g1.draw(pen1)

        # create a pen for g2 with a shifted rect and move each found segment with the width and kerning
        pen2 = findPossibleOverlappingSegmentsPen.FindPossibleOverlappingSegmentsPen(
            g2.getParent(), bounds1, (g1.width + kern, 0))
        # draw the glyph into the pen
        g2.draw(pen2)

        for segment1 in pen1.segments:
            for segment2 in pen2.segments:
                if len(segment1) == 4 and len(segment2) == 4:
                    a1, a2, a3, a4 = segment1
                    b1, b2, b3, b4 = segment2
                    result = intersectCubicCubic(a1, a2, a3, a4, b1, b2, b3,
                                                 b4)
                elif len(segment1) == 4:
                    p1, p2, p3, p4 = segment1
                    a1, a2 = segment2
                    result = intersectCubicLine(p1, p2, p3, p4, a1, a2)
                elif len(segment2) == 4:
                    p1, p2, p3, p4 = segment2
                    a1, a2 = segment1
                    result = intersectCubicLine(p1, p2, p3, p4, a1, a2)
                else:
                    a1, a2 = segment1
                    b1, b2 = segment2
                    result = intersectLineLine(a1, a2, b1, b2)
                if result.status == "Intersection":
                    return True

        return False
Example #5
0
def checkIfPairOverlaps(g1, g2):
    assert g1.getParent() is g2.getParent(), 'the two glyphs do not belong to the same font'

    """Checking method from Touche!
    Returns a Boolean if overlapping.
    """

    kern = g1.getParent().naked().flatKerning.get((g1.name, g2.name), 0)

    # Check sidebearings first (PvB's idea)
    if g1.rightMargin + g2.leftMargin + kern > 0:
        return False

    # get the bounds and check them
    if version[0] == '2':
        bounds1 = g1.bounds
    else:
        bounds1 = g1.box
    if bounds1 is None:
        return False
    if version[0] == '2':
        bounds2 = g2.bounds
    else:
        bounds2 = g2.box
    if bounds2 is None:
        return False

    # shift bounds2
    bounds2 = offsetRect(bounds2, g1.width+kern, 0)
    # check for intersection bounds
    intersectingBounds, _ = sectRect(bounds1, bounds2)
    if not intersectingBounds:
        return False
    # move bounds1 back, moving bounds is faster then moving all coordinates in a glyph
    bounds1 = offsetRect(bounds1, -g2.width-kern, 0)

    # create a pen for g1 with a shifted rect, draw the glyph into the pen
    pen1 = FindPossibleOverlappingSegmentsPen(g1.getParent(), bounds2)
    g1.draw(pen1)

    # create a pen for g2 with a shifted rect and move each found segment with the width and kerning
    pen2 = FindPossibleOverlappingSegmentsPen(g2.getParent(), bounds1, (g1.width+kern, 0))
    # draw the glyph into the pen
    g2.draw(pen2)

    for segment1 in pen1.segments:
        for segment2 in pen2.segments:
            if len(segment1) == 4 and len(segment2) == 4:
                a1, a2, a3, a4 = segment1
                b1, b2, b3, b4 = segment2
                result = intersectCubicCubic(a1, a2, a3, a4, b1, b2, b3, b4)
            elif len(segment1) == 4:
                p1, p2, p3, p4 = segment1
                a1, a2 = segment2
                result = intersectCubicLine(p1, p2, p3, p4, a1, a2)
            elif len(segment2) == 4:
                p1, p2, p3, p4 = segment2
                a1, a2 = segment1
                result = intersectCubicLine(p1, p2, p3, p4, a1, a2)
            else:
                a1, a2 = segment1
                b1, b2 = segment2
                result = intersectLineLine(a1, a2, b1, b2)
            if result.status == "Intersection":
                return True

    return False
Example #6
0
def _getLineCurveIntersection(line, curve):
    points = curve + line
    intersection = rfBezierTools.intersectCubicLine(*points)
    return intersection