Exemple #1
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() != "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
         event.setAccepted(True)
     else:
         super().dropEvent(event)
Exemple #2
0
    def openFile(self, path=None):
        if not path:
            fileFormat = self.tr("UFO Fonts {}")
            if platformSpecific.treatPackageAsFile():
                ext = "(*.ufo)"
            else:
                ext = "(metainfo.plist)"

            path, _ = QFileDialog.getOpenFileName(
                self.activeWindow(), self.tr("Open File"), '',
                fileFormat.format(ext)
            )
            if not path:
                return
        if ".plist" in path:
            path = os.path.dirname(path)
        path = os.path.normpath(path)
        for widget in self.topLevelWidgets():
            if isinstance(widget, FontWindow):
                font = widget.font_()
                if font is not None and font.path == path:
                    widget.raise_()
                    return
        try:
            font = TFont(path)
            window = FontWindow(font)
        except Exception as e:
            msg = self.tr(
                "There was an issue when opening the font at {}.".format(
                    path))
            errorReports.showCriticalException(e, msg)
            return
        window.show()
        self.setCurrentFile(font.path)
Exemple #3
0
    def importFile(self):
        # TODO: systematize this
        fileFormats = (
            self.tr("OpenType Font file {}").format("(*.otf *.ttf)"),
            self.tr("Type1 Font file {}").format("(*.pfa *.pfb)"),
            self.tr("ttx Font file {}").format("(*.ttx)"),
            self.tr("WOFF Font file {}").format("(*.woff)"),
            self.tr("All supported files {}").format(
                "(*.otf *.pfa *.pfb *.ttf *.ttx *.woff)"),
            self.tr("All files {}").format("(*.*)"),
        )

        path, _ = QFileDialog.getOpenFileName(
            self, self.tr("Import File"), None,
            ";;".join(fileFormats), fileFormats[-2])

        if path:
            font = TFont()
            try:
                font.extract(path)
            except Exception as e:
                errorReports.showCriticalException(e)
                return
            window = FontWindow(font)
            window.show()
Exemple #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() != "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
         event.setAccepted(True)
     else:
         super().dropEvent(event)
Exemple #5
0
 def exportFile(self):
     fileFormats = [
         (self.tr("PostScript OT font {}").format("(*.otf)")),
         (self.tr("TrueType OT font {}").format("(*.ttf)")),
     ]
     state = settings.exportFileDialogState()
     # TODO: font.path as default?
     # TODO: store per-font export path in lib
     directory = None if state else QStandardPaths.standardLocations(
         QStandardPaths.DocumentsLocation)[0]
     dialog = QFileDialog(
         self, self.tr("Export File"), directory,
         ";;".join(fileFormats))
     if state:
         dialog.restoreState(state)
     dialog.setAcceptMode(QFileDialog.AcceptSave)
     ok = dialog.exec_()
     settings.setExportFileDialogState(dialog.saveState())
     if ok:
         fmt = "ttf" if dialog.selectedNameFilter(
             ) == fileFormats[1] else "otf"
         path = dialog.selectedFiles()[0]
         try:
             self._font.export(path, fmt)
         except Exception as e:
             errorReports.showCriticalException(e)
Exemple #6
0
 def exportFile(self):
     path, _ = QFileDialog.getSaveFileName(
         self, self.tr("Export File"), None,
         self.tr("OpenType PS font {}").format("(*.otf)"))
     if path:
         try:
             self._font.export(path)
         except Exception as e:
             errorReports.showCriticalException(e)
Exemple #7
0
 def _loadBinary(self, path):
     font = TFont()
     try:
         font.extract(path)
     except Exception as e:
         errorReports.showCriticalException(e)
         return
     window = FontWindow(font)
     window.show()
Exemple #8
0
 def _loadBinary(self, path):
     for widget in self.topLevelWidgets():
         if isinstance(widget, FontWindow):
             font = widget.font_()
             if font is not None and font.binaryPath == path:
                 widget.raise_()
                 return
     font = TFont()
     try:
         font.extract(path)
         self._loadFont(font)
     except Exception as e:
         errorReports.showCriticalException(e)
         return
     self.setCurrentFile(font.binaryPath)
Exemple #9
0
 def _loadUFO(self, path):
     for widget in self.topLevelWidgets():
         if isinstance(widget, FontWindow):
             font = widget.font_()
             if font is not None and font.path == path:
                 widget.raise_()
                 return
     try:
         font = TFont(path)
         self._loadFont(font)
     except Exception as e:
         msg = self.tr("There was an issue opening the font at {}.").format(path)
         errorReports.showCriticalException(e, msg)
         return
     self.setCurrentFile(font.path)
Exemple #10
0
 def exportFile(self):
     params, ok = ExportDialog.getExportParameters(self, self._font)
     if not ok:
         return
     baseName = params["baseName"]
     directory = params["exportDirectory"]
     compression = set(map(str.lower, params["compression"]))
     for format in map(str.lower, params["formats"]):
         fileName = f"{baseName}.{format}"
         path = os.path.join(directory, fileName)
         try:
             self._font.export(path, format, compression=compression)
         except Exception as e:
             msg = (self.tr("This font’s feature file contains an error.")
                    if isinstance(e, FeatureLibError) else None)
             errorReports.showCriticalException(e, message=msg)
Exemple #11
0
 def _loadUFO(self, path):
     for widget in self.topLevelWidgets():
         if isinstance(widget, FontWindow):
             font = widget.font_()
             if font is not None and font.path == path:
                 widget.raise_()
                 return
     try:
         font = TFont(path)
         window = FontWindow(font)
     except Exception as e:
         msg = self.tr("There was an issue opening the font at {}.".format(path))
         errorReports.showCriticalException(e, msg)
         return
     window.show()
     self.setCurrentFile(font.path)
Exemple #12
0
 def exportFile(self):
     params, ok = ExportDialog.getExportParameters(self, self._font)
     if not ok:
         return
     baseName = params['baseName']
     directory = params['exportDirectory']
     compression = set(map(str.lower, params['compression']))
     for format in map(str.lower, params['formats']):
         fileName = "{}.{}".format(baseName, format)
         path = os.path.join(directory, fileName)
         try:
             self._font.export(path, format, compression=compression)
         except Exception as e:
             msg = self.tr("This font’s feature file contains an error."
                           ) if isinstance(e, FeatureLibError) else None
             errorReports.showCriticalException(e, message=msg)
Exemple #13
0
 def _loadBinary(self, path):
     for widget in self.topLevelWidgets():
         if isinstance(widget, FontWindow):
             font = widget.font_()
             if font is not None and font.binaryPath == path:
                 widget.raise_()
                 return
     font = TFont()
     try:
         font.extract(path)
     except Exception as e:
         errorReports.showCriticalException(e)
         return
     window = FontWindow(font)
     window.show()
     self.setCurrentFile(font.binaryPath)
Exemple #14
0
 def openFile(self, path):
     if ".plist" in path:
         path = os.path.dirname(path)
     path = os.path.normpath(path)
     for window in self.topLevelWidgets():
         if isinstance(window, FontWindow):
             font = window.font_()
             if font is not None and font.path == path:
                 window.raise_()
                 return
     try:
         font = TFont(path)
         window = FontWindow(font)
     except Exception as e:
         errorReports.showCriticalException(e)
         return
     window.show()
 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)