示例#1
0
    def adjust(self, index, changelen):
        if changelen < 0:
            cache = self.cache
            del cache[index:index - changelen]
            start = end = index
            if index < len(cache):
                # [(0,3), (1,2), <snip> (1,2), (2,1)]

                # clear overlapping range before adjustment
                if index > 0:
                    hit = cache[index - 1]
                    if hit and hit[1] > 1:
                        start = index - (hit[0] + 1)
                        for i in range(hit[0] + 1):
                            if i < 0:
                                # this should never happen
                                start = 0
                                break
                            cache[index - 1 - i] = None

                # clear overlapping range after adjustment
                hit = cache[index]
                if hit and hit[0] > 0:
                    end = index + hit[1]
                    for i in range(hit[1]):
                        cache[index + i] = None

            return NSRange(start, end - start)
        self.cache[index:index] = [None for i in range(changelen)]
        return NSRange(index, changelen)
示例#2
0
    def Blaster( self, wordlist ):

        layout = self.w.radioGroup.get() # 0=stacked 1=sidebyside
        wordlist = self.Sanitise(wordlist)

        repeater = ""
        wlist = ""

        for w in wordlist:

            s=0
            while s<masterlen: 
                if layout==1:
                    repeater = repeater + w + " "  
                else:
                    repeater = repeater + w + "\n"  
                s=s+1
            
            wlist = wlist + repeater + "\n"
            repeater = ""

        Glyphs.currentDocument.windowController().addTabWithString_( wlist ) # add text
        currentEditViewController = Glyphs.currentDocument.windowController().activeEditViewController()
        currentTab = currentEditViewController.graphicView()

        s=0
        location_start = 0
        listlen = len(wordlist)

        while s<listlen:

            myRangeOfGlyphs = NSRange()
            t=0

            while t<masterlen: 

                myRangeOfGlyphs.location = location_start
                myRangeOfGlyphs.length = len(wordlist[s])

                # assign master value to range
                masterID = f.masters[t].id
                Attributes = { "GSLayerIdAttrib": masterID }
                currentTab.textStorage().text().addAttributes_range_( Attributes, myRangeOfGlyphs )

                location_start = location_start + len(wordlist[s]) + 1
                t+=1

            location_start+=1
            s=s+1

        if not self.SaveP( self ):
            print("Could not save preferences." )

        currentEditViewController.forceRedraw()
示例#3
0
 def scan(self, text, setcolor, offset=0):
     info = self.wordinfo
     prevend = offset
     for match in self.regex.finditer(text, offset):
         data = info.get(match.lastgroup)
         if data is None:
             log.error("invalid syntax match: %r", match.groups())
             continue
         thestart = match.start()
         thisend = match.end()
         range_ = NSRange(thestart, thisend - thestart)
         setcolor(data[0], range_, prevend, data[1])
         prevend = range_.location + range_.length
     setcolor(None, NSRange(len(text) - 1, 0), prevend, None)
示例#4
0
class TextField(TextFieldBasedControl, GTextField):

    #_vertical_padding = 5
    _intercept_tab_key = False

    def __init__(self, text = "", font = system_font, 
            multiline = False, password = False, border = True, **kwds):
        ns_textfield = self._create_ns_textfield(editable = True,
            multiline = multiline, password = password,
            text = text, font = font, border = border)
        GTextField.__init__(self, _ns_view = ns_textfield, 
            multiline = multiline, **kwds)

    def get_selection(self):
        ns_editor = self._ns_editor()
        if ns_editor:
            start, length = ns_editor.selectedRange()
            return (start, start + length)
        else:
            return (0, 0)
    
    def set_selection(self, (start, end)):
        self.become_target()
        ns_editor = self._ns_editor()
        if ns_editor:
            ns_editor.setSelectedRange_(NSRange(start, end - start))
示例#5
0
 def get(self, index):
     try:
         value = self.cache[index]
     except IndexError:
         return (None, None)
     if value is None:
         return (None, None)
     return NSRange(index - value[0], value[0] + value[1]), value[2]
示例#6
0
    def color_text(self, ts, minrange=None):
        if ts.editedMask() == ak.NSTextStorageEditedAttributes:
            return  # we don't care if only attributes changed
        sdef = self._syntaxdef
        if sdef is None:
            return
        text = ts.string()
        tlen = ts.length()

        if minrange is not None:
            adjrange = self.adjust(minrange.location, ts.changeInLength())
            minrange = NSUnionRange(minrange, adjrange)
            i = minrange.location - 1
            if i < 0:
                i = 0
            else:
                while i > 0 and text[i] != "\n":
                    i -= 1
            prerange, info = self.get(i)
            if prerange is None:
                prerange = NSRange(i, 0)
            minrange = NSUnionRange(minrange, prerange)
            minstart = minrange.location
            minend = minrange.location + minrange.length
        else:
            minstart = 0
            minend = tlen

        def setcolor(color,
                     range_,
                     prevend,
                     info,
                     ts=ts,
                     cache=self,
                     minend=minend):
            prevcache = cache.get(range_.location)
            prevend = max(min(prevend, range_.location), 0)
            rangelen = max((range_.location - prevend) + range_.length, 0)
            prevrange = NSRange(prevend, rangelen)
            cache.clear(prevrange)
            ts.removeAttribute_range_(ak.NSForegroundColorAttributeName,
                                      prevrange)
            if color is not None:
                ts.addAttribute_value_range_(ak.NSForegroundColorAttributeName,
                                             color, range_)
                cache.set(range_, info)
            if range_.location + range_.length > minend and (
                    range_, info) == prevcache:
                raise StopHighlight()

        ts.beginEditing()
        try:
            sdef.scan(text, setcolor, minstart)
        except StopHighlight:
            pass
        finally:
            ts.endEditing()
示例#7
0
 def setcolor(color,
              range_,
              prevend,
              info,
              ts=ts,
              cache=self,
              minend=minend):
     prevcache = cache.get(range_.location)
     prevend = max(min(prevend, range_.location), 0)
     rangelen = max((range_.location - prevend) + range_.length, 0)
     prevrange = NSRange(prevend, rangelen)
     cache.clear(prevrange)
     ts.removeAttribute_range_(ak.NSForegroundColorAttributeName,
                               prevrange)
     if color is not None:
         ts.addAttribute_value_range_(ak.NSForegroundColorAttributeName,
                                      color, range_)
         cache.set(range_, info)
     if range_.location + range_.length > minend and (
             range_, info) == prevcache:
         raise StopHighlight()
#MenuTitle: Show Masters of Previous Glyph
# -*- coding: utf-8 -*-
from __future__ import division, print_function, unicode_literals
__doc__ = """
Shows all masters for the previous glyph.
"""

from Foundation import NSRange
from PyObjCTools.AppHelper import callAfter

zeroPosition = NSRange()
zeroPosition.location = 0
zeroPosition.length = 0


def showAllMastersOfGlyphInCurrentTab(thisGlyphName):
    try:
        escapedGlyphName = "/" + thisGlyphName
        thisDoc = Glyphs.currentDocument
        thisWindow = thisDoc.windowController()
        thisEditView = thisWindow.activeEditViewController()  # current tab

        if thisEditView is None:
            # opens new Edit tab if none was open:
            callAfter(thisWindow.addTabWithString_, escapedGlyphName)
        else:
            thisGraphicView = thisEditView.graphicView(
            )  # current display string
            thisGraphicView.setDisplayString_(
                escapedGlyphName)  # set the display string to this glyph
            thisGraphicView.setSelectedRange_(
示例#9
0
 def scan(self, text, setcolor, offset=0):
     if offset == 0:
         setcolor(None, NSRange(len(text) - 1, 0), 0, None)