def getMargins(glyph, beam=None):
    '''
    Get left and right margins for a glyph.

    Args:
        glyph (RGlyph): A glyph object.
        beam (int or None): A beam to measure the margins. (optional)

    Returns:
        A tuple with left and right margins, or None if the beam does not intersect any contours.

    >>> glyph = CurrentGlyph()
    >>> sp = CurrentSpaceCenter()
    >>> beam = sp.beam()
    >>> print(glyph.name, beam, getMargins(glyph, beam))

    '''
    if beam is None:
        return glyph.leftMargin, glyph.rightMargin

    else:
        line = (-1000, beam), (glyph.width + 1000, beam)
        intersections = IntersectGlyphWithLine(glyph, line, canHaveComponent=True, addSideBearings=True)
        intersections.sort()

        if not len(intersections) > 2:
            return

        leftMargin = intersections[1][0] - intersections[0][0]
        rightMargin = intersections[-1][0] - intersections[-2][0]

        return leftMargin, rightMargin
Example #2
0
def isSidebearingEqual(glyph1, glyph2, position, height):
    # checking arguments qualities
    assert position == '*left' or position == '*right'
    assert type(height) is types.IntType

    intersections1 = IntersectGlyphWithLine(glyph1, ((0, height),
                                                     (glyph1.width, height)),
                                            canHaveComponent=True,
                                            addSideBearings=True)
    intersections1 = sorted(intersections1, key=lambda coor: coor[0])
    intersections1 = [xx for (xx, yy) in intersections1]
    intersections1.sort()

    intersections2 = IntersectGlyphWithLine(glyph2, ((0, height),
                                                     (glyph2.width, height)),
                                            canHaveComponent=True,
                                            addSideBearings=True)
    intersections2 = [xx for (xx, yy) in intersections2]
    intersections2.sort()

    if position == '*left':
        left1 = int(intersections1[1])
        left2 = int(intersections2[1])
        if left1 != left2:
            return False
        else:
            return True
    else:
        right1 = int(intersections1[-1] - intersections1[-2])
        right2 = int(intersections2[-1] - intersections2[-2])
        if right1 != right2:
            return False
        else:
            return True
Example #3
0
 def stemWidth(self, glyph):
     try:
         val = int(self.w.stemWidthBeamX.get())
     except ValueError:
         self.w.infoOutput.set("Pls enter a Number.")
         return
     BeamerPosition = int(self.w.stemWidthBeamX.get())
     result = IntersectGlyphWithLine(glyph,
                                     ((-10, BeamerPosition),
                                      (glyph.width + 10, BeamerPosition)),
                                     canHaveComponent=True,
                                     addSideBearings=False)
     xValuesFromIntersection = [x[0] for x in result]
     xValuesSorted = sorted(xValuesFromIntersection)
     try:
         stemWidth = xValuesSorted[1] - xValuesSorted[0]
         self.listOfValues.append(int(stemWidth))
     except IndexError:
         return
class TriggerButton():
    def __init__(self):
        self.colorOne = NSColor.colorWithCalibratedRed_green_blue_alpha_(
            1, 0, 0, .5)
        self.colorTwo = NSColor.colorWithCalibratedRed_green_blue_alpha_(
            0, .2, .8, .3)
        self.active = False
        self.closest = None
        self.ortho = None
        self.mousePt = None
        self.activDraw = 0
        self.keydidUp = 0
        # self.getGlyph()
        addObserver(self, "drawClosest", "draw")
        addObserver(self, "drawClosest", "drawPreview")
        addObserver(self, "drawClosest", "drawInactive")
        addObserver(self, "mouseMoved", "mouseMoved")
        addObserver(self, "changeGlyph", "currentGlyphChanged")
        addObserver(self, "keyDown", "keyDown")
        addObserver(self, "keyUp", "keyUp")

    def getRCJKI(self):
        for o in lib.eventTools.eventManager.allObservers():
            obsClass = o[1]
            if obsClass.__class__.__name__ == 'RoboCJKController':
                return obsClass
        return None

    @property
    def g(self):
        currentGlyph = CurrentGlyph()
        if currentGlyph is None:
            return None
        _glyph = currentGlyph.copy()
        pen = _glyph.getPen()
        RCJKI = self.getRCJKI()
        if RCJKI is None:
            return _glyph
        RCJKI_glyph = RCJKI.currentFont[_glyph.name]
        if RCJKI_glyph.type == "atomicElement":
            return _glyph
        for atomicInstanceGlyph in RCJKI_glyph.preview():
            atomicInstanceGlyph.glyph.draw(pen)
        return _glyph

    def keyDown(self, sender):
        # self.getGlyph()
        if self.g is None: return
        if sender['event'].characters() == "r":
            self.activDraw = 1
            self.keydidUp = 0
        if sender['event'].characters() == "r" and getActiveEventTool(
        ).getModifiers()['commandDown']:
            self.activDraw = 0
        UpdateCurrentGlyphView()

    def keyUp(self, sender):
        self.keydidUp = 1
        UpdateCurrentGlyphView()

    def changeGlyph(self, info):
        if not self.mousePt: return
        self.closest = self.ortho = None
        self.sections = []
        # self.getGlyph()
        if not self.g: return
        self.pen = ClosestPointPen(self.mousePt, self.g.getParent())
        self.g.draw(self.pen)
        self.closest, self.ortho = self.pen.getClosestData()
        UpdateCurrentGlyphView()

    def drawClosest(self, info):
        if not self.activDraw: return
        if self.closest and self.ortho:
            p = self.closest[0], self.closest[1]
            s = info['scale']
            r = 2.5 * s
            rOutline = 5 * s
            rOutline2 = 8 * s
            strokeWidth(.5 * s)

            normOrtho = self.normalize(self.ortho)

            save()
            newPath()
            stroke(.2)
            moveTo((p))
            endOut = p[0] + normOrtho[0] * 2000, p[1] + normOrtho[1] * 2000
            lineTo(endOut)
            closePath()
            drawPath()
            restore()

            save()
            newPath()
            stroke(.2)
            moveTo((p))
            endIn = p[0] - normOrtho[0] * 2000, p[1] - normOrtho[1] * 2000
            lineTo(endIn)
            closePath()
            drawPath()
            restore()

            save()
            fill(None)
            stroke(1, 0, 1)
            oval(p[0] - rOutline, p[1] - rOutline, 2 * rOutline, 2 * rOutline)
            oval(p[0] - rOutline2, p[1] - rOutline2, 2 * rOutline2,
                 2 * rOutline2)
            restore()

            self.sections = IntersectGlyphWithLine(self.g, (endOut, endIn),
                                                   canHaveComponent=True,
                                                   addSideBearings=False)
            self.sections.sort()

            save()
            lens = len(self.sections)
            if lens > 1:
                for i in range(lens - 1):
                    cur = self.sections[i]
                    next = self.sections[(i + 1) % lens]
                    dist = distance(cur, next)
                    fontsize = 9 * s  #*(max(1, min(1.5, 100/dist)))
                    midPt = (cur[0] + next[0]) * .5, (cur[1] + next[1]) * .5

                    if self.g.pointInside(midPt):
                        fillText = 0
                        fillDisc = 1
                    else:
                        fillText = 1
                        fillDisc = 0

                    save()
                    fill(1, 0, 1)
                    stroke(None)
                    oval(cur[0] - r, cur[1] - r, 2 * r, 2 * r)
                    restore()

                    txt = str(int(dist))

                    stroke(None)
                    fill(fillDisc, fillDisc, fillDisc, .8)
                    oval(midPt[0] - fontsize * 1.5, midPt[1] - fontsize * 1.35,
                         2 * fontsize * 1.5, 2 * fontsize * 1.5)
                    font('Menlo-Bold', fontsize)
                    fill(fillText)
                    text(txt, midPt[0] - fontsize * .3 * len(txt),
                         midPt[1] - fontsize * .5)
                save()
                fill(1, 0, 1)
                stroke(None)
                oval(next[0] - r, next[1] - r, 2 * r, 2 * r)
                restore()
            restore()

    def mouseMoved(self, info):
        if not self.activDraw: return
        if self.keydidUp == 1: return
        self.mousePt = (info['point'].x, info['point'].y)
        self.pen = ClosestPointPen(self.mousePt, self.g.getParent())
        self.g.draw(self.pen)
        self.closest, self.ortho = self.pen.getClosestData()
        UpdateCurrentGlyphView()

    def normalize(self, a):
        l = self.lengtha(a)
        return (a[0] / l, a[1] / l)

    def lengtha(self, a):
        return (sqrt(a[0] * a[0] + a[1] * a[1]))
    def drawClosest(self, info):
        if not self.activDraw: return
        if self.closest and self.ortho:
            p = self.closest[0], self.closest[1]
            s = info['scale']
            r = 2.5 * s
            rOutline = 5 * s
            rOutline2 = 8 * s
            strokeWidth(.5 * s)

            normOrtho = self.normalize(self.ortho)

            save()
            newPath()
            stroke(.2)
            moveTo((p))
            endOut = p[0] + normOrtho[0] * 2000, p[1] + normOrtho[1] * 2000
            lineTo(endOut)
            closePath()
            drawPath()
            restore()

            save()
            newPath()
            stroke(.2)
            moveTo((p))
            endIn = p[0] - normOrtho[0] * 2000, p[1] - normOrtho[1] * 2000
            lineTo(endIn)
            closePath()
            drawPath()
            restore()

            save()
            fill(None)
            stroke(1, 0, 1)
            oval(p[0] - rOutline, p[1] - rOutline, 2 * rOutline, 2 * rOutline)
            oval(p[0] - rOutline2, p[1] - rOutline2, 2 * rOutline2,
                 2 * rOutline2)
            restore()

            self.sections = IntersectGlyphWithLine(self.g, (endOut, endIn),
                                                   canHaveComponent=True,
                                                   addSideBearings=False)
            self.sections.sort()

            save()
            lens = len(self.sections)
            if lens > 1:
                for i in range(lens - 1):
                    cur = self.sections[i]
                    next = self.sections[(i + 1) % lens]
                    dist = distance(cur, next)
                    fontsize = 9 * s  #*(max(1, min(1.5, 100/dist)))
                    midPt = (cur[0] + next[0]) * .5, (cur[1] + next[1]) * .5

                    if self.g.pointInside(midPt):
                        fillText = 0
                        fillDisc = 1
                    else:
                        fillText = 1
                        fillDisc = 0

                    save()
                    fill(1, 0, 1)
                    stroke(None)
                    oval(cur[0] - r, cur[1] - r, 2 * r, 2 * r)
                    restore()

                    txt = str(int(dist))

                    stroke(None)
                    fill(fillDisc, fillDisc, fillDisc, .8)
                    oval(midPt[0] - fontsize * 1.5, midPt[1] - fontsize * 1.35,
                         2 * fontsize * 1.5, 2 * fontsize * 1.5)
                    font('Menlo-Bold', fontsize)
                    fill(fillText)
                    text(txt, midPt[0] - fontsize * .3 * len(txt),
                         midPt[1] - fontsize * .5)
                save()
                fill(1, 0, 1)
                stroke(None)
                oval(next[0] - r, next[1] - r, 2 * r, 2 * r)
                restore()
            restore()
Example #6
0
def get_stem1_of_reference_glyphs(f):
    result = IntersectGlyphWithLine(f[referenceGlyph], ((-2100, BeamPos), (f[referenceGlyph].width+2500, BeamPos)), canHaveComponent=True, addSideBearings=False)
    xValuesFromIntersection = [int(round(x[0])) for x in result]
    xValuesSorted = sorted(xValuesFromIntersection)
    stem1 = (xValuesSorted[1]-xValuesSorted[0])
    return (f,stem1)
Example #7
0
class Ruler():

    def __init__(self, interface):
        self.ui = interface
        self.colorOne = NSColor.colorWithCalibratedRed_green_blue_alpha_(1,0,0,.5)
        self.colorTwo = NSColor.colorWithCalibratedRed_green_blue_alpha_(0,.2,.8,.3)
        self.active = False
        self.closest = None
        self.ortho = None
        self.mousePt = None
        self.activDraw = 0
        self.keydidUp = 0
        self.glyph = self.ui.glyph
        self.ui.w.bind('close', self.windowWillClose)
        self.observer()

    def windowWillClose(self, sender):
        self.observer(remove=True)
        
    def observer(self, remove = False):
        if not remove:
            addObserver(self, "draw", "draw")
            addObserver(self, "draw", "drawPreview")
            addObserver(self, "mouseMoved", "mouseMoved")
            addObserver(self, "currentGlyphChanged", "currentGlyphChanged")
            addObserver(self, "keyDown", "keyDown")
            addObserver(self, "keyUp", "keyUp")
            return
        removeObserver(self, "draw")
        removeObserver(self, "drawPreview")
        removeObserver(self, "mouseMoved")
        removeObserver(self, "currentGlyphChanged")
        removeObserver(self, "keyDown")
        removeObserver(self, "keyUp")

    def keyDown(self, sender):
        self.glyph = CurrentGlyph()
        if self.glyph is None or not len(self.glyph): return
        command = extractNSEvent(sender['event'])["commandDown"]
        if sender['event'].characters() == "r":
            self.activDraw = 1
            self.keydidUp = 0
        if sender['event'].characters() == "r" and command:
            self.activDraw = 0
        UpdateCurrentGlyphView()

    def keyUp(self, sender):
        self.keydidUp = 1
        UpdateCurrentGlyphView()
        
    def currentGlyphChanged(self, info):
        if not self.mousePt: return
        self.closest = self.ortho = None
        self.sections = []
        if not self.glyph: return
        self.pen = ClosestPointPen(self.mousePt, self.glyph.getParent())
        self.glyph.draw(self.pen)
        self.closest, self.ortho = self.pen.getClosestData()
        UpdateCurrentGlyphView()
        
    def draw(self, info):
        if not self.activDraw: return
        if self.closest and self.ortho:
            p = self.closest[0], self.closest[1]
            s = info['scale']
            r = 2.5*s
            rOutline = 5*s
            rOutline2 = 8*s
            strokeWidth(.5*s)
            
            normOrtho = self.normalize(self.ortho)

            save()
            newPath()
            stroke(.2)
            endOut = p[0] + normOrtho[0]*1000, p[1] + normOrtho[1]*1000
            moveTo(endOut)
            endIn = p[0] - normOrtho[0]*1000, p[1] - normOrtho[1]*1000
            lineTo(endIn)
            closePath()
            drawPath()
            restore()
            
            save()
            fill(None)
            stroke(1, 0, 1)
            oval(p[0]-rOutline, p[1]-rOutline, 2*rOutline, 2*rOutline)
            oval(p[0]-rOutline2, p[1]-rOutline2, 2*rOutline2, 2*rOutline2)
            restore()
            
            self.sections = IntersectGlyphWithLine(self.glyph, 
                    (endOut, endIn), 
                    canHaveComponent=True, 
                    addSideBearings=False)
            self.sections.sort()
            
            save()
            lens = len(self.sections)
            if lens > 1:
                for i in range(lens-1):

                    cur = self.sections[i]           
                    next = self.sections[(i+1)%lens]
                    dist = distance(cur, next)
                    fontsize = 9*s#*(max(1, min(1.5, 100/dist)))
                    midPt = (cur[0]+next[0])*.5, (cur[1]+next[1])*.5 

                    if self.glyph.pointInside(midPt):
                        fillText = 0
                        fillDisc = 1              
                    else:
                        fillText = 1
                        fillDisc = 0

                    save()
                    fill(1, 0, 1)
                    stroke(None)
                    oval(cur[0]-r, cur[1]-r, 2*r, 2*r)
                    restore()

                    txt = str(int(dist))  
                    stroke(None)                  
                    fill(fillDisc, fillDisc, fillDisc, .8)

                    oval(midPt[0]-fontsize*1.5, midPt[1]-fontsize*1.35, 2*fontsize*1.5, 2*fontsize*1.5)
                    font('Menlo-Bold', fontsize)
                    
                    fill(fillText)
                    text(txt, midPt[0]-fontsize*.3*len(txt), midPt[1]-fontsize*.5)
                save()
                fill(1, 0, 1)
                stroke(None)
                oval(next[0]-r, next[1]-r, 2*r, 2*r)
                restore()
            restore()
            
    def mouseMoved(self, info):
        if not self.activDraw: return
        if self.keydidUp == 1: return
        self.mousePt = (info['point'].x, info['point'].y)
        self.pen = ClosestPointPen(self.mousePt, self.glyph.getParent())
        self.glyph.draw(self.pen)
        self.closest, self.ortho = self.pen.getClosestData()
        UpdateCurrentGlyphView()
        
    def normalize(self, a):
        l = self.lengtha(a)
        return(a[0]/l, a[1]/l)
    
    def lengtha(self, a):
        return(sqrt(a[0]*a[0]+a[1]*a[1]))