Debian Codegraph @

pycontrib/FontCompare/fc/GlyphConsistency.py

raw symbols SCIP
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 Copyright (C) 2013 Mayank Jha <[email protected]>
19
20 This library is free software; you can redistribute it and/or modify
21 it under the terms of the GNU Lesser General Public License as published
22 by the Free Software Foundation; either version 2.1 of the License, or
23 (at your option) any later version.
24
25 This program is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 GNU General Public License for more details.
29
30 You should have received a copy of the GNU Lesser General Public License
31 along with this program. If not, see <http://www.gnu.org/licenses/>.
32 """
33 from fc.BitmapHandler import BitmapCompare
34 bitcomp = BitmapCompare()
35 class GlyphConsistency:
36 #computes the number of selfIntersects in the glyph is any across
37 #all layers, it is a self consistency check
38 def glyph_basicConsistency(self, font, glyphrange):
39 scores = list()
40 total = 0
41 for i in range (glyphrange[0],glyphrange[1]):
42 #worth outputting
43 try:
44 t=font[i]
45 if i in font:
46 if font[i].isWorthOutputting():
47 scoreFactor = 1
48 else:
49 scoreFactor = 0
50 score=10
51 if font[i].layers[1].selfIntersects():
52 score=0
53 else:
54 score = score * scoreFactor
55 except:
56 score = 0
57 #no counter intersection
58 total+=score
59 scores.append((i,score))
60 #scores.append(("Basic Consistency Score: ",total/len(scores)))
61 return scores
62
63 def glyph_basicset_consistency(self, font, range_tuple):
64 total = 0
65 #bbox consistency
66 xmin_cords = list()
67 ymin_cords = list()
68 ymax_cords = list()
69 xmax_cords = list()
70 #bearing consistency
71 lbearing = list()
72 rbearing = list()
73 #layer count consistency
74 layer_cnts = list()
75 for i in range (range_tuple[0],range_tuple[1]):
76 if i in font:
77 if not font[i].boundingBox()[0] in xmin_cords:
78 xmin_cords.append(font[i].boundingBox()[0])
79 if not font[i].boundingBox()[1] in ymin_cords:
80 ymin_cords.append(font[i].boundingBox()[1])
81 if not font[i].boundingBox()[2] in xmax_cords:
82 xmax_cords.append(font[i].boundingBox()[2])
83 if not font[i].boundingBox()[3] in ymax_cords:
84 ymax_cords.append(font[i].boundingBox()[3])
85 if not font[i].left_side_bearing in lbearing:
86 rbearing.append(font[i].left_side_bearing)
87 if not font[i].right_side_bearing in rbearing:
88 rbearing.append(font[i].right_side_bearing)
89 if not font[i].layer_cnt in layer_cnts:
90 layer_cnts.append(font[i].layer_cnt)
91 total+=1
92 count=len(xmin_cords)+len(xmax_cords)+len(ymin_cords)+len(ymax_cords)
93 count = count+ len(rbearing) + len(lbearing) + len(layer_cnts)
94 set_basic_score = count/float(total*7)
95 return set_basic_score*10
96
97 def glyph_round_consistency(self, font, glyphrange, pixelsize):
98 #script and rounding consistency
99 set_round_score=0.0
100 total=0
101 net_score = list()
102 for i in range (glyphrange[0],glyphrange[1]):
103 if i in font:
104 score = self.glyph_round_compare(font[i],pixelsize)
105 print score
106 set_round_score+=score
107 total+=1
108 font.close()
109 return (set_round_score/float(total))*10
110
111 def glyph_round_compare(self,glyph,pixelsize):
112 glyph.export("/var/tmp/before.bmp",pixelsize,1)
113 glyph.round()
114 glyph.export("/var/tmp/after.bmp",pixelsize,1)
115 score =(bitcomp.basicCompare("/var/tmp/before.bmp", \
116 "/var/tmp/after.bmp",pixelsize,0,True))
117 if score == 1:
118 return 1
119 else:
120 return 0