Ejemplo n.º 1
0
 def _loadFromGLIF(self, glifData):
     try:
         readGlyphFromString(aString=glifData,
                             glyphObject=self.naked(),
                             pointPen=self.getPointPen())
     except GlifLibError:
         raise FontPartsError("Not valid glif data")
Ejemplo n.º 2
0
 def paste(self):
     # TODO: refactor copy/paste code somewhere
     clipboard = QApplication.clipboard()
     mimeData = clipboard.mimeData()
     if mimeData.hasFormat("application/x-trufont-glyph-data"):
         data = pickle.loads(mimeData.data(
             "application/x-trufont-glyph-data"))
         selection = self.glyphCellView.selection()
         glyphs = self.glyphCellView.glyphsForIndexes(selection)
         if len(data) == len(glyphs):
             for pickled, glyph in zip(data, glyphs):
                 # XXX: prune
                 glyph.prepareUndo()
                 glyph.deserialize(pickled)
     elif mimeData.hasText():
         selection = self.glyphCellView.selection()
         if len(selection) == 1:
             glyph = self.glyphCellView.glyphsForIndexes(selection)[0]
             otherGlyph = glyph.__class__()
             text = mimeData.text()
             try:
                 readGlyphFromString(
                     text, otherGlyph, otherGlyph.getPointPen())
             except:
                 return
             glyph.prepareUndo()
             glyph.clear()
             otherGlyph.drawPoints(glyph.getPointPen())
Ejemplo n.º 3
0
 def glifToPy(self, glif):
     glif = stripText(glif)
     glif = "<?xml version=\"1.0\"?>\n" + glif
     glyph = Glyph()
     readGlyphFromString(glif,
                         glyphObject=glyph,
                         pointPen=glyph,
                         validate=True)
     return glyph.py()
Ejemplo n.º 4
0
 def dropEvent(self, event):
     mimeData = event.mimeData()
     if mimeData.hasUrls():
         paths = mimeData.urls()
         # pick just one image
         path = paths[0].toLocalFile()
         fileName = os.path.basename(path)
         with open(path, "rb") as imgFile:
             data = imgFile.read()
         ext = os.path.splitext(path)[1][1:]
         # TODO: make sure we cleanup properly when replacing an image with
         # another
         if ext.lower() == "glif":
             otherGlyph = self._glyph.__class__()
             try:
                 readGlyphFromString(
                     data, otherGlyph, otherGlyph.getPointPen())
             except Exception as e:
                 errorReports.showCriticalException(e)
                 return
             self._glyph.beginUndoGroup()
             otherGlyph.drawPoints(self._glyph.getPointPen())
             self._glyph.endUndoGroup()
             return
         if ext.lower() == "svg":
             try:
                 svgPath = SVGPath.fromstring(data)
             except Exception as e:
                 errorReports.showCriticalException(e)
                 return
             self._glyph.beginUndoGroup()
             svgPath.draw(self._glyph.getPen())
             self._glyph.endUndoGroup()
             return
         if ext.lower() != "png":
             # convert
             img = QImage(path)
             data = QByteArray()
             buffer = QBuffer(data)
             buffer.open(QIODevice.WriteOnly)
             img.save(buffer, 'PNG')
             # format
             data = bytearray(data)
             fileName = "%s.png" % os.path.splitext(fileName)[0]
         imageSet = self._glyph.font.images
         try:
             imageSet[fileName] = data
         except Exception as e:
             errorReports.showCriticalException(e)
             return
         image = self._glyph.instantiateImage()
         image.fileName = fileName
         self._glyph.image = image
         event.setAccepted(True)
     else:
         super().dropEvent(event)
Ejemplo n.º 5
0
 def draw(self):
     try:
         save()
         g = self.ui.glyph
         scale(self.scale, self.scale)
         translate(((self.canvasWidth / self.scale) - 1000) * .5, 250)
         translate(self.translateX, self.translateY)
         if g is None:
             with open(RoboCJKIconPath, "r") as file:
                 iconText = file.read()
             icon = RGlyph()
             pen = icon.getPointPen()
             readGlyphFromString(iconText, icon, pen)
             drawGlyph(icon)
         else:
             if not len(
                     g
             ) and not "deepComponentsGlyph" in g.lib and g.unicode and not self.preview:
                 fill(0, 0, 0, .1)
                 rect(-1000, -1000, 10000, 10000)
                 # fill(.5, 0, .3, .5)
                 fill(0, 0, .8, .2)
                 translate(0, -150)
                 fontSize(1000)
                 text(chr(g.unicode), (0, 0))
                 stroke(0)
                 strokeWidth(100 * self.scale)
                 newPath()
                 moveTo((0, 0))
                 lineTo((1100, 1100))
                 drawPath()
             else:
                 fill(0, 0, 0, 1)
                 drawGlyph(g)
                 if self.ui.onOff_designFrame and not self.preview:
                     DesignFrameDrawer(self.ui).draw(
                         glyph=g,
                         mainFrames=self.ui.showMainFrames,
                         secondLines=self.ui.showSecondLines,
                         customsFrames=self.ui.showCustomsFrames,
                         proximityPoints=self.ui.showproximityPoints,
                         translate_secondLine_X=self.ui.
                         translate_secondLine_X,
                         translate_secondLine_Y=self.ui.
                         translate_secondLine_Y,
                         scale=self.scale)
                 f = self.ui.font2Storage[self.ui.font]
                 fill(.2, 0, 1, .5)
                 if self.preview:
                     fill(0, 0, 0, 1)
                 DeepComponentDrawer(self.ui, g, f)
         restore()
     except Exception as e:
         print(e)
Ejemplo n.º 6
0
 def paste(self):
     isGlyphTab = self.isGlyphTab()
     widget = self.stackWidget.currentWidget()
     if isGlyphTab:
         glyphs = (widget.activeGlyph(), )
     else:
         selection = self.glyphCellView.selection()
         glyphs = widget.glyphsForIndexes(selection)
     clipboard = QApplication.clipboard()
     mimeData = clipboard.mimeData()
     if mimeData.hasFormat("application/x-trufont-glyph-data"):
         data = pickle.loads(
             mimeData.data("application/x-trufont-glyph-data"))
         if len(data) == len(glyphs):
             for pickled, glyph in zip(data, glyphs):
                 if isGlyphTab:
                     pasteGlyph = glyph.__class__()
                     pasteGlyph.deserialize(pickled)
                     # TODO: if we serialize selected state, we don't need
                     # to do this
                     pasteGlyph.selected = True
                     if len(pasteGlyph) or len(pasteGlyph.components) or \
                             len(pasteGlyph.anchors):
                         glyph.beginUndoGroup()
                         glyph.holdNotifications()
                         pen = glyph.getPointPen()
                         # contours, components
                         pasteGlyph.drawPoints(pen)
                         # anchors
                         for anchor in pasteGlyph.anchors:
                             glyph.appendAnchor(dict(anchor))
                         # guidelines
                         for guideline in pasteGlyph.guidelines:
                             glyph.appendGuideline(dict(guideline))
                         glyph.releaseHeldNotifications()
                         glyph.endUndoGroup()
                 else:
                     glyph.deserialize(pickled)
     elif mimeData.hasText():
         if len(glyphs) == 1:
             glyph = glyphs[0]
             otherGlyph = glyph.__class__()
             text = mimeData.text()
             try:
                 readGlyphFromString(text, otherGlyph,
                                     otherGlyph.getPointPen())
             except:
                 return
             glyph.beginUndoGroup()
             if not isGlyphTab:
                 glyph.clear()
             otherGlyph.drawPoints(glyph.getPointPen())
             glyph.endUndoGroup()
Ejemplo n.º 7
0
    def testRoundTrip(self):
        glyph = _Glyph()
        glyph.name = "a"
        glyph.unicodes = [0x0061]

        s1 = writeGlyphToString(glyph.name, glyph)

        glyph2 = _Glyph()
        readGlyphFromString(s1, glyph2)
        self.assertEqual(glyph.__dict__, glyph2.__dict__)

        s2 = writeGlyphToString(glyph2.name, glyph2)
        self.assertEqual(s1, s2)
    def applyCallback(self, sender):
        if self.currentGlyph is not None:
            xml = self.w.xml.get()

            dummyGlyph = RGlyph()
            try:
                readGlyphFromString(str(xml), dummyGlyph,
                                    dummyGlyph.getPointPen())
            except:
                import traceback
                error = "A problem occured when trying to parse the glif plist."
                suggestion = traceback.format_exc(5)
                self.showMessage(error, suggestion)
                return

            self.currentGlyph.clear()
            readGlyphFromString(str(xml), self.currentGlyph,
                                self.currentGlyph.getPointPen())
Ejemplo n.º 9
0
	def glifToPy(self, glif):
		glif = stripText(glif)
		glif = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + glif
		glyph = Glyph()
		readGlyphFromString(glif, glyphObject=glyph, pointPen=glyph)
		return glyph.py()
Ejemplo n.º 10
0
 def _readGlyphFromString(self, glifData):
     try:
         readGlyphFromString(glifData, glyphObject=self.naked(), pointPen=self.getPointPen())
     except:
         raise FontPartsError("Not valid glif data")
Ejemplo n.º 11
0
    subjectPaths = contoursToZs(subjectContours)
    clippingPaths = contoursToZs(clippingContours)
    result = shapeops.intersection(subjectPaths, clippingPaths, **kwargs)
    drawZsWithPointPen(result, outPen, guessSmooth=guessSmooth)


def xor(subjectContours, clippingContours, outPen, guessSmooth=True,
        **kwargs):
    subjectPaths = contoursToZs(subjectContours)
    clippingPaths = contoursToZs(clippingContours)
    result = shapeops.xor(subjectPaths, clippingPaths, **kwargs)
    drawZsWithPointPen(result, outPen, guessSmooth=guessSmooth)


if __name__ == "__main__":
    import sys
    from defcon import Glyph
    from ufoLib.glifLib import readGlyphFromString, writeGlyphToString

    data = sys.stdin.read()

    glyph = Glyph()
    readGlyphFromString(data, glyph, glyph.getPointPen())

    contours = list(glyph)
    glyph.clearContours()
    union(contours, glyph.getPointPen())

    output = writeGlyphToString(glyph.name, glyph, glyph.drawPoints)
    sys.stdout.write(output)
Ejemplo n.º 12
0
 def glifToPy(self, glif):
     glif = stripText(glif)
     glif = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + glif
     glyph = Glyph()
     readGlyphFromString(glif, glyphObject=glyph, pointPen=glyph)
     return glyph.py()