Hello people.

To my HB Tester GUI I just added the facility to save the rendering to
a PNG file (used it for my bug report posted just now).

Again, except for Cibu, I haven't heard from anyone else. I would like
to hear it if anyone is using this little app and/or has found it
useful (or not).

Thanks.

-- 
Shriramana Sharma ஶ்ரீரமணஶர்மா श्रीरमणशर्मा
#! /usr/bin/env python3

# HB Tester -- a simple GUI to test HB's rendering of a particular string/font combination
# (C) Shriramana Sharma, 2013
# Licence: GPLv3

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class MainWindow(QWidget) :
	
	def __init__(self) :
		
		QWidget.__init__(self)
		
		self.setObjectName("mainWindow")
		self.setWindowTitle("HarfBuzz Tester")
		
		self.inputLabel = QLabel("&Input text")
		self.inputTextBox = QPlainTextEdit()
		self.inputLabel.setBuddy(self.inputTextBox)
		
		self.fontFileLabel = QLabel("Font fi&le")
		self.fontFileTextBox = QLineEdit()
		self.fontFileLabel.setBuddy(self.fontFileTextBox)
		
		self.fontFileButton = QPushButton("&Find...")
		
		self.fontSizeLabel = QLabel("&Size")
		self.fontSizeSpin = QSpinBox()
		self.fontSizeSpin.setMinimum(8)
		self.fontSizeSpin.setValue(16)
		self.fontSizeLabel.setBuddy(self.fontSizeSpin)
		
		self.renderButton = QPushButton("&Render")
		self.outputLabel = QLabel()
		self.outputLabel.setAlignment(Qt.AlignCenter)
		self.saveButton = QPushButton("Save &As...")
		
		self.inputLayout = QHBoxLayout()
		self.inputLayout.addWidget(self.inputLabel)
		self.inputLayout.addWidget(self.inputTextBox)
		
		self.fontFileLayout = QHBoxLayout()
		self.fontFileLayout.addWidget(self.fontFileLabel)
		self.fontFileLayout.addWidget(self.fontFileTextBox)
		self.fontFileLayout.addWidget(self.fontFileButton)
		self.fontFileLayout.addWidget(self.fontSizeLabel)
		self.fontFileLayout.addWidget(self.fontSizeSpin)
		
		self.layout = QVBoxLayout()
		self.layout.addLayout(self.inputLayout)
		self.layout.addLayout(self.fontFileLayout)
		self.layout.addWidget(self.renderButton)
		self.layout.addWidget(self.outputLabel)
		self.layout.addWidget(self.saveButton)
		self.setLayout(self.layout)
		
		self.renderedPNGforUser = None
		self.userSavedAsFileName = None
		
		QObject.connect ( self.fontFileButton, SIGNAL("clicked()"), self.selectFontFile )
		QObject.connect ( self.renderButton, SIGNAL("clicked()"), self.renderText )
		QObject.connect ( self.saveButton, SIGNAL("clicked()"), self.saveRenderedPNG )
		
	def selectFontFile(self) :
		fontFileName = self.fontFileTextBox.text()
		path = fontFileName if QFile.exists(fontFileName) else QDir.homePath()
		newFontFileName = QFileDialog.getOpenFileName ( self, "HarfBuzz Tester - Select Font", path, "Fonts (*.ttf *.otf)" )
		if newFontFileName != "" : self.fontFileTextBox.setText(newFontFileName)
		
	def renderText(self) :
		
		if self.inputTextBox.toPlainText() == "" :
			self.outputLabel.clear() ; return
		
		fontFileName = self.fontFileTextBox.text()
		
		if fontFileName == "" :
			QMessageBox.critical ( self, "HarfBuzz Tester - Error", "No font file is specified" )
			self.outputLabel.clear() ; return
		
		if not QFile.exists(fontFileName) :
			QMessageBox.critical ( self, "HarfBuzz Tester - Error", "The specified font file does not exist" )
			self.outputLabel.clear() ; return
		
		if fontFileName[-4:] not in ( ".ttf", ".otf" ) :
			QMessageBox.critical ( self, "HarfBuzz Tester - Error", "The specified font file does not seem to be a font file (only .ttf or .otf allowed)" )
			self.outputLabel.clear() ; return
		
		hbInFile = QTemporaryFile()
		hbInFile.open()
		hbInFileStream = QTextStream(hbInFile)
		hbInFileStream.setCodec("UTF-8")
		hbInFileStream << self.inputTextBox.toPlainText().replace("\t"," ") # replacing tabs because fonts usually do not contain a glyph for the tab character
		hbInFileStream.flush()
		
		hbOutFileTransBkgd = QTemporaryFile()
		hbOutFileTransBkgd.open()
		hbOutFileWhiteBkgd = QTemporaryFile()
		hbOutFileWhiteBkgd.open()
		
		import os
		commonCommand = "hb-view --output-format=png" + \
		                " --font-file=" + fontFileName.replace(" ","\ ") + \
		                " --font-size=" + str(self.fontSizeSpin.value()) + \
		                " --text-file=" + hbInFile.fileName().replace(" ","\ ")
		os.system ( commonCommand + " --output-file=" + hbOutFileTransBkgd.fileName().replace(" ","\ ") + " --background=#00000000" )
		os.system ( commonCommand + " --output-file=" + hbOutFileWhiteBkgd.fileName().replace(" ","\ ") )
		
		self.outputLabel.setPixmap( QPixmap ( hbOutFileTransBkgd.fileName(), "png" ))
		
		hbInFile.close()
		hbOutFileTransBkgd.close()
		hbOutFileWhiteBkgd.close()
		self.renderedPNGforUser = hbOutFileWhiteBkgd
	
	def saveRenderedPNG(self) :
		path = self.userSavedAsFileName if QFile.exists(self.userSavedAsFileName) else QDir.homePath()
		saveAsFileName = QFileDialog.getSaveFileName ( self, "HarfBuzz Tester - Save rendering as...", path, "PNG Image (*.png)" )
		if saveAsFileName != "" :
			if QFile.exists(saveAsFileName) and not QFile.remove(saveAsFileName) :
				QMessageBox.critical ( self, "HarfBuzz Tester - Could not save file", "A file already exists with that name. Could not remove it." )
			else :
				if not self.renderedPNGforUser.copy(saveAsFileName) :
					QMessageBox.critical ( self, "HarfBuzz Tester - Could not save file", "Could not save to the given filename." )
				else : # successfully saved
					self.userSavedAsFileName = saveAsFileName

app = QApplication([])
mainWindow = MainWindow()
mainWindow.show()
app.exec_()
_______________________________________________
HarfBuzz mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/harfbuzz

Reply via email to