Debian Codegraph @

pycontrib/FontCompare/fontcompare

raw
1 """
2 Copyright (C) 2013 Mayank Jha <[email protected]>
3
4 This library is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published
6 by the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 """
17
18 #! /usr/bin/python
19 from PyQt4.QtGui import QMessageBox
20 from PyQt4.QtGui import QMainWindow
21 from PyQt4.QtGui import QApplication
22 from PyQt4.QtGui import QFileDialog
23 from PyQt4.QtCore import *
24 import fontforge
25 import sys
26 import time
27 from fc.FontCompare import FontCompare
28 from fc.GlyphConsistency import GlyphConsistency
29 from fc.mockify import MockFont
30 from fc.DocCompare import DocCompare
31 import shelve
32 import pkg_resources
33 import shutil
34 import os
35 from os.path import basename
36 from os.path import dirname
37 from fc.DocCompare import DocCompare
38 import pkg_resources
39 import shutil
40 # Import the interface class
41 from fc import main_ui
42 class MainApp(QMainWindow, main_ui.Ui_MainWindow):
43 Testfilepath = ""
44 Standardfilepath = "workinprogress"
45
46 def __init__(self, parent=None):
47 super(MainApp, self).__init__(parent)#?????????????
48 # This is because Python does not automatically
49 # call the parent's constructor.
50 self.setupUi(self)
51 self.connectActions()
52
53 def connectActions(self):
54 self.BeginTestButton.clicked.connect(self.BeginTest)
55 self.loadTestpushButton.clicked.connect(lambda: self.\
56 OpenFontFile("test"))
57 self.actionLoadTestFont.triggered.connect(lambda: self.\
58 OpenFontFile("test"))
59 self.actionSaveTestResults.triggered.connect(self.SaveMessage)
60 self.actionabout.triggered.connect(self.about)
61 self.ClearMessageBoxButton.clicked.connect(self.ClearMessage)
62 self.SaveMessageBoxButton.clicked.connect(self.SaveMessage)
63
64 def about(self):
65 QMessageBox.about(self, "About", \
66 " A tool for testing the aesthetic quality of fonts of Indic scripts")
67
68 def BeginTest(self):
69 if self.Testfilepath == "":
70 QMessageBox.about(self, "Error", "No File was Loaded!")
71 else:
72 self.TestFromFont()
73 if self.Testfilepath!="" and self.Standardfilepath!="":
74 self.TestFromFont()
75 else:
76 QMessageBox.about(self, "Error",
77 "Standard or Test Font missing!")
78
79
80 def OpenFontFile(self,filetype):
81 flag=0
82 while flag == 0:
83 filename = QFileDialog.getOpenFileName(self, 'Font File')
84 if filename == "":
85 break
86 try:
87 f=fontforge.open(filename)
88 QMessageBox.about(self, "Success",
89 "The file was loaded successfully!")
90 flag=1
91
92 except:
93 QMessageBox.about(self,
94 "Error", "The file could not be loaded!\n \
95 Please try again")
96 if filetype == "standard":
97 self.Standardfilepath = filename
98 else:
99 self.Testfilepath = filename
100
101 def ClearMessage(self):
102 self.MessageBox.setText("")
103
104 def SaveMessage(self):
105 content = self.MessageBox.toPlainText()
106 savefilename = QFileDialog.getSaveFileName(self, 'Save File')
107 myfile=open(savefilename,'w')
108 myfile.write(content)
109 myfile.close()
110
111 def PrintGlyphrelatedScore(self,scores):
112 total=len(scores)
113 final=0
114 for score in scores:
115 p=unichr(score[0])+" "+str(score[1])
116 final+=score[1]
117 self.PrintMessage(p)
118 if final:
119 final = (final/float(total))/10
120 else:
121 final = 0
122 self.PrintMessage
123 ("No glyphs for Selected Script Unicode characters")
124
125 self.PrintMessage("The total score was "+str(final))
126 return final
127
128 def TestFromFont(self):
129 if not self.Testfilepath or not self.Standardfilepath:
130 QMessageBox.about(self, "Warning!", \
131 "Please load standard or test file")
132 thefile = pkg_resources.resource_filename("fc","data/mockfile.mcy")
133 shutil.copy(thefile,"/var/tmp/tmp.mcy")
134 mock_font = shelve.open("/var/tmp/tmp.mcy")
135 mockfont = mock_font["font"]
136 mock_font.close()
137
138 fc=FontCompare()
139 gc=GlyphConsistency()
140 #consistency test
141 self.PrintMessage("-------------------------------------------")
142 #basic
143 fontA = fontforge.open(self.Testfilepath)
144 score = gc.glyph_basicConsistency(fontA,self.GetScript())
145 total=0
146 for tup in score:
147 self.PrintMessage(unichr(tup[0])+" "+str(tup[1]))
148 total+=tup[1]/10
149 self.BasicConsistencyScoreBar.setValue(total/len(score))
150 self.PrintMessage("Basic Consistency Score: "+str(total/len(score)))
151 self.PrintMessage("----------------------------------------")
152 #numerals
153 score = gc.glyph_basicset_consistency(fontA, (0x960,0x96f))
154 self.PrintMessage("Numeral Consistency Score is "+str(score))
155 self.NumeralScoreBar.setValue(round(score))
156 self.PrintMessage("-------------------------------------------")
157 self.PrintMessage("----------------------------------------")
158 #consonants
159 score = gc.glyph_basicset_consistency(fontA, (0x915,0x939))
160 self.PrintMessage("Consonant Consistency Score is "+str(score))
161 self.ConsonantScoreBar.setValue(round(score))
162 self.PrintMessage("-------------------------------------------")
163 #marks
164 score = gc.glyph_basicset_consistency(fontA, (0x958,0x95f))
165 self.PrintMessage("Mark Consistency Score is "+str(score))
166 self.MarkScoreBar.setValue(round(score))
167 self.PrintMessage("-------------------------------------------")
168 #vowels
169 score = gc.glyph_basicset_consistency(fontA, (0x904,0x914))
170 self.PrintMessage("Vowel Consistency Score is "+str(score))
171 self.VowelScoreBar.setValue(round(score))
172 self.PrintMessage("-------------------------------------------")
173 #rounding
174 score = gc.glyph_round_consistency(fontA, self.GetScript(),100)
175
176 self.PrintMessage("Rounding Consistency Score is "+str(score))
177 self.RoundingScoreBar.setValue(round(score))
178 #font main tests
179 self.PrintMessage("----------------------------------------")
180 #basic properties
181 fontA = fontforge.open(self.Testfilepath)
182 scores = fc.font_basiccompare(fontA,mockfont)
183 for tup in scores:
184 self.PrintMessage(tup[0]+str(tup[1]))
185 self.BasicScoreBar.setValue(scores[len(scores)-1][1])
186 self.PrintMessage("-------------------------------------------")
187 #normal test
188 fontA = fontforge.open(self.Testfilepath)
189 scores = fc.font_facecompare(fontA,mockfont,self.GetScript(),600, \
190 12,1,"normal")
191 score = self.PrintGlyphrelatedScore(scores)
192 self.NormalScoreBar.setValue(round(score))
193 fontA = fontforge.open(self.Testfilepath)
194 self.PrintMessage("-------------------------------------------")
195 #bold test
196 fontA = fontforge.open(self.Testfilepath)
197 scores = fc.font_facecompare(fontA,mockfont,self.GetScript(),600, \
198 12,1,"bold")
199 score = self.PrintGlyphrelatedScore(scores)
200 self.BoldScoreBar.setValue(round(score))
201 fontA = fontforge.open(self.Testfilepath)
202 self.PrintMessage("-------------------------------------------")
203 #italic test
204 fontA = fontforge.open(self.Testfilepath)
205 scores = fc.font_facecompare(fontA,mockfont,self.GetScript(),600, \
206 12,1,"italic")
207 score = self.PrintGlyphrelatedScore(scores)
208 self.ItalicScoreBar.setValue(round(score))
209 self.PrintMessage("-------------------------------------------")
210 #font advanced tests
211 #highres docfile
212 dc = DocCompare()
213 score = dc.basicCompare(self.Testfilepath,mockfont,160)
214 self.highresScoreBar.setValue(round(score/10))
215 self.PrintMessage("High Resolution Score: "+str(score/10))
216
217 def GetScript(self):
218 return (0x900,0x97f)#self.LanguageBox.currentIndex()
219
220 def GetFontFilePaths(self):
221 print "under construction"
222
223 def PrintMessage(self,message):
224 self.MessageBox.append(message)
225
226
227 def main(self):
228 self.show()
229
230 if __name__=='__main__':
231 app = QApplication(sys.argv)
232 mainApp = MainApp()
233 mainApp.main()
234 app.exec_()