Skip to content

Instantly share code, notes, and snippets.

@adrientetar
Last active January 20, 2016 19:48
Show Gist options
  • Save adrientetar/b6696652c03cafa66b9f to your computer and use it in GitHub Desktop.
Save adrientetar/b6696652c03cafa66b9f to your computer and use it in GitHub Desktop.
from fontTools.feaLib.error import FeatureLibError
import traceback
font = CurrentFont()
try:
font.getRepresentation("defconQt.TTFont")
except FeatureLibError as e:
traceback.print_exc()
print(e.location)
else:
print("C'est OKAY!")
from defcon import LayoutEngine
from PyQt5.QtCore import pyqtSignal, Qt
from PyQt5.QtWidgets import QComboBox, QGridLayout, QGroupBox, QLineEdit, QRadioButton, QVBoxLayout, QWidget
class OpenTypeControls(QWidget):
controlsChanged = pyqtSignal()
def __init__(self, parent=None):
super().__init__(parent)
# writing direction
ltrBox = QRadioButton("LTR", self)
ltrBox.toggled.connect(self.controlsChanged)
rtlBox = QRadioButton("RTL", self)
directionGroupBox = QGroupBox("Writing direction", self)
directionLayout = QVBoxLayout(self)
directionLayout.addWidget(ltrBox)
directionLayout.addWidget(rtlBox)
directionGroupBox.setLayout(directionLayout)
directionGroupBox.toggled.connect(self.controlsChanged)
# script & language
self.scriptBox = QComboBox(self)
self.scriptBox.currentTextChanged.connect(self.controlsChanged)
self.languageBox = QComboBox(self)
self.languageBox.currentTextChanged.connect(self.controlsChanged)
# feature list
featGroupBox = QGroupBox("Feature list", self)
featGroupBox.setFlat(True)
self.groupBoxLayout = QVBoxLayout(self)
featGroupBox.setLayout(self.groupBoxLayout)
featGroupBox.toggled.connect(self.controlsChanged)
layout = QVBoxLayout(self)
layout.addWidget(directionGroupBox)
layout.addWidget(self.scriptBox)
layout.addWidget(self.languageBox)
layout.addWidget(featGroupBox)
self.setLayout(layout)
def updateFromEngine(self, engine):
# script & language
currentText = self.scriptBox.currentText()
self.scriptBox.clear()
self.scriptBox.addItems(engine.getScriptList())
self.scriptBox.setCurrentText(currentText)
currentText = self.languageBox.currentText()
self.languageBox.clear()
self.languageBox.addItems(engine.getLanguageList())
self.languageBox.setCurrentText(currentText)
# destroy current checkboxes
for checkBox in self.groupBoxLayout.children():
checkBox.setParent(None)
# populate features
for feature in engine.getFeatureList():
box = QCheckBox(feature, self.groupBoxLayout)
box.setChecked(engine.getFeatureState(feature))
#self.groupBoxLayout.addWidget(box)
class PreviewWindow(QWidget):
def __init__(self, parent=None):
super().__init__(parent, Qt.Window)
self._font = CurrentFont()
self._layoutEngine = LayoutEngine(self._font)
otControls = OpenTypeControls(self)
inputLine = QLineEdit(self)
inputLine.textChanged.connect(self.updateView)
canvas = QWidget(self)
layout = QGridLayout()
layout.addWidget(inputLine, 0, 0, 1, 2)
layout.addWidget(otControls, 1, 0)
layout.addWidget(canvas, 1, 1)
self.setLayout(layout)
def updateView(self, text):
rec = self._layoutEngine.process(text)
print(rec)
window = PreviewWindow()
window.show()
from io import BytesIO
import traceback
font = CurrentFont()
try:
ttf = font.getRepresentation("defconQt.QuadraticTTFont")
stream = BytesIO()
ttf.save(stream)
except:
traceback.print_exc()
from cu2qu.defcon import font_to_quadratic
from fontTools.feaLib.error import FeatureLibError
from io import BytesIO
from PyQt5.QtGui import QFontDatabase
try:
# Chromium
from PyQt5.QtWebEngineWidgets import QWebEngineView
except ImportError:
# WebKit
from PyQt5.QtWebKit import QWebView
QWebEngineView = QWebView
class WebEngineFontView(QWebEngineView):
def __init__(self, parent=None):
super().__init__(parent)
self._font = CurrentFont()
self._font.addObserver(
self, "_fontChanged", "Font.Changed")
self._fontId = None
self.updateFont()
def closeEvent(self, event):
self._font.removeObserver(self, "Font.Changed")
event.accept()
# drawbot, edited
def _extractPSName(self, font):
psName = font["name"].getName(6, 1, 0)
if psName is None:
psName = font["name"].getName(6, 3, 1)
return psName
def _fontChanged(self, notification):
self.updateFont()
def updateFont(self):
font_to_quadratic(self._font)
try:
otf = self._font.getRepresentation(
"defconQt.QuadraticTTFont")
except FeatureLibError as e:
traceback.print_exc()
print(e.location)
return
stream = BytesIO()
otf.save(stream)
id = QFontDatabase.addApplicationFontFromData(
stream.getvalue())
if id == -1:
return
if self._fontId is not None:
QFontDatabase.removeApplicationFont(
self._fontId)
psName = self._extractPSName(otf)
if psName is None:
return
self._fontId = id
self.updateHtml(psName)
def updateHtml(self, psName):
html = """
<head>
<style>
html {
font-family: %s;
}
<style>
</head>
<body>
Grumpy wizards make toxic brew for the evil Queen and Jack. A quick movement of the enemy will jeopardize six gunboats. The job of waxing linoleum frequently peeves chintzy kids. My girl wove six dozen plaid jackets before she quit. Twelve ziggurats quickly jumped a finch box.
</body>
""" % psName
self.setHtml(html)
webView = WebEngineFontView()
webView.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment