Skip to content

Instantly share code, notes, and snippets.

@adrientetar
Last active June 19, 2016 06:31
Show Gist options
  • Save adrientetar/90e9f62c09ba15cd2ac550dd74f9a223 to your computer and use it in GitHub Desktop.
Save adrientetar/90e9f62c09ba15cd2ac550dd74f9a223 to your computer and use it in GitHub Desktop.
Painting glyph with points into an SVG.
from defconQt.tools import drawing
from PyQt5.QtCore import QRect, QSize, Qt
from PyQt5.QtGui import QPainter
from PyQt5.QtSvg import QSvgGenerator
from PyQt5.QtWidgets import QApplication, QFileDialog
import os
def _currentFontSelection():
"""
We should probably have font.selection sometimes, but for now this will do.
"""
app = QApplication.instance()
window = app.currentMainWindow()
if window is None:
return set()
widget = window.glyphCellView
return widget.glyphsForIndexes(widget.selection())
selection = _currentFontSelection()
if selection:
path = QFileDialog.getExistingDirectory()
cache = [] # gc
if path:
file = open(os.path.join(path, "nam.txt"), "wt")
for glyph in selection:
if None not in (glyph, glyph.bounds):
pixmap = glyph.getRepresentation("defconQt.GlyphCell", width=512, height=512, drawHeader=False, drawMetrics=False)
pixmap.save(os.path.join(path, "%s.png" % glyph.name))
cache.append(pixmap)
print(glyph.name, file=file)
file.close()
from defconQt.tools import drawing
from PyQt5.QtCore import QRect, QSize
from PyQt5.QtGui import QPainter
from PyQt5.QtSvg import QSvgGenerator
from PyQt5.QtWidgets import QFileDialog
glyph = CurrentGlyph()
if None not in (glyph, glyph.bounds):
path, _ = QFileDialog.getSaveFileName(None, "Save As SVG","", "SVG file (*.svg)")
if path:
generator = QSvgGenerator()
generator.setFileName(path)
font = glyph.font
width = glyph.width
if font is not None:
ascender = font.info.ascender
height = font.info.unitsPerEm
else:
_, top, _, bottom = glyph.bounds
ascender = height = top - bottom
generator.setSize(QSize(width, height))
generator.setViewBox(QRect(0, 0, width, height))
painter = QPainter()
painter.begin(generator)
painter.translate(0, ascender)
painter.scale(1, -1)
drawing.drawGlyphFillAndStroke(painter, glyph, 1.0, (0, 0, 0, 0))
drawing.drawGlyphPoints(painter, glyph, 1.0, (0, 0, 0, 0))
painter.end()
from defconQt.tools import drawing
from PyQt5.QtCore import QRect, QSize, Qt
from PyQt5.QtGui import QPainter
from PyQt5.QtSvg import QSvgGenerator
from PyQt5.QtWidgets import QApplication, QFileDialog
import os
def _currentFontSelection():
"""
We should probably have font.selection sometimes, but for now this will do.
"""
app = QApplication.instance()
window = app.currentMainWindow()
if window is None:
return set()
widget = window.glyphCellView
return widget.glyphsForIndexes(widget.selection())
selection = _currentFontSelection()
if selection:
path = QFileDialog.getExistingDirectory()
if path:
for glyph in selection:
if None not in (glyph, glyph.bounds):
generator = QSvgGenerator()
generator.setFileName(os.path.join(path, "%s.svg" % glyph.name))
font = glyph.font
width = glyph.width
if font is not None:
ascender = font.info.ascender
height = font.info.unitsPerEm
else:
_, top, _, bottom = glyph.bounds
ascender = height = top - bottom
generator.setSize(QSize(width, height))
generator.setViewBox(QRect(0, 0, width, height))
painter = QPainter()
painter.begin(generator)
painter.translate(0, ascender)
painter.scale(1, -1)
drawing.drawGlyphFillAndStroke(painter, glyph, 1.0, (0, 0, 0, 0), drawStroke=False, contourFillColor=Qt.black)
#drawing.drawGlyphPoints(painter, glyph, 1.0, (0, 0, 0, 0))
painter.end()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment