| 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 | """ |
| 34 | Contains methods for making comparisons using a host of tests. |
| 35 | They are also for modifying a glyph by altering its |
| 36 | serif, stroke , stem thickness, size, italic angle etc. |
| 37 | and then later producing scores by doing bitmap comparison |
| 38 | """ |
| 39 | from fc.BitmapHandler import BitmapCompare |
| 40 | from fc.mockify import MockFont |
| 41 | import shutil |
| 42 | import pkg_resources |
| 43 | thefile = pkg_resources.resource_filename("fc","data/masterspritebold.bmp") |
| 44 | shutil.copy(thefile,"/var/tmp/tmpb.bmp") |
| 45 | thefile = pkg_resources.resource_filename("fc","data/masterspritenormal.bmp") |
| 46 | shutil.copy(thefile,"/var/tmp/tmpn.bmp") |
| 47 | thefile = pkg_resources.resource_filename("fc","data/masterspriteitalic.bmp") |
| 48 | shutil.copy(thefile,"/var/tmp/tmpi.bmp") |
| 49 | class FontCompare(object): |
| 50 | |
| 51 | def font_basiccompare(self, Testfont, mockfont): |
| 52 | final=list() |
| 53 | |
| 54 | mx=max(Testfont.ascent,mockfont.ascent) |
| 55 | mn=min(Testfont.ascent,mockfont.ascent) |
| 56 | score1 = int((1/float(abs(mx-mn)))*10) if (mx-mn)!=0 else 10 |
| 57 | final.append(("Ascent Score: ",score1)) |
| 58 | |
| 59 | mx=max(Testfont.descent,mockfont.descent) |
| 60 | mn=min(Testfont.descent,mockfont.descent) |
| 61 | score2 = int((1/float(abs(mx-mn)))*10) if (mx-mn)!=0 else 10 |
| 62 | final.append(("Descent Score: ",score2)) |
| 63 | |
| 64 | mx=max(Testfont.capHeight,mockfont.capHeight) |
| 65 | mn=min(Testfont.capHeight,mockfont.capHeight) |
| 66 | score3 = int((1/float(abs(mx-mn)))*10) if (mx-mn)!=0 else 10 |
| 67 | final.append(("Cap Height: ",score3)) |
| 68 | |
| 69 | mx=max(Testfont.strokewidth,mockfont.strokewidth) |
| 70 | mn=min(Testfont.strokewidth,mockfont.strokewidth) |
| 71 | score4 = int((1/float(abs(mx-mn)))*10) if (mx-mn)!=0 else 10 |
| 72 | final.append(("Stroke Width Score: ",score4)) |
| 73 | |
| 74 | mx=max(Testfont.upos,mockfont.upos) |
| 75 | mn=min(Testfont.upos,mockfont.upos) |
| 76 | score5 = int((1/float(abs(mx-mn)))*10) if (mx-mn)!=0 else 10 |
| 77 | final.append(("Underline Position Score: ",score5)) |
| 78 | |
| 79 | mx=max(Testfont.uwidth,mockfont.uwidth) |
| 80 | mn=min(Testfont.uwidth,mockfont.uwidth) |
| 81 | score6 = int((1/float(abs(mx-mn)))*10) if (mx-mn)!=0 else 10 |
| 82 | final.append(("Underline Width Score: ",score6)) |
| 83 | |
| 84 | mx=max(Testfont.xHeight,mockfont.xHeight) |
| 85 | mn=min(Testfont.xHeight,mockfont.xHeight) |
| 86 | score7 = int((1/float(abs(mx-mn)))*10) if (mx-mn)!=0 else 10 |
| 87 | final.append(("x Height Score: ",score7)) |
| 88 | |
| 89 | test=Testfont.weight |
| 90 | standard=mockfont.weight |
| 91 | score8 = 10 if test==standard else 0 |
| 92 | final.append(("PostScript weight string",score8)) |
| 93 | |
| 94 | score=score1+score2+score3+score4+score5+score6+score7+score8; |
| 95 | final.append(("Average Basic Score: ",score/8.0)) |
| 96 | Testfont.close() |
| 97 | return final |
| 98 | |
| 99 | def font_facecompare(self, Testfont, mockfont, glyphRange, \ |
| 100 | resolution, ptsize, pixeldepth, fonttype): |
| 101 | spritepath = "/var/tmp/tmpn.bmp" |
| 102 | if fonttype is "bold": |
| 103 | spritepath = "/var/tmp/tmpb.bmp" |
| 104 | for i in range (glyphRange[0],glyphRange[1]): |
| 105 | if i in Testfont: |
| 106 | Testfont[i].changeWeight(50,"auto",0,0,"auto") |
| 107 | if fonttype is "italic": |
| 108 | Testfont.selection.all() |
| 109 | Testfont = Testfont.italicize(-13) |
| 110 | spritepath = "/var/tmp/tmpi.bmp" |
| 111 | scores = list() |
| 112 | comparator = BitmapCompare() |
| 113 | pixelsize = (resolution*ptsize)/72 |
| 114 | print spritepath |
| 115 | for i in range (glyphRange[0],glyphRange[1]): |
| 116 | if i in Testfont: |
| 117 | Testfont[i].export("/var/tmp/tmp.bmp",pixelsize,1) |
| 118 | Testfont[i].export("tmp.bmp",pixelsize,1) |
| 119 | glyphscore = comparator.basicCompare("/var/tmp/tmp.bmp",\ |
| 120 | spritepath,pixelsize,(i-glyphRange[0])*pixelsize) |
| 121 | glyphscore*=100 |
| 122 | else: |
| 123 | glyphscore=0 |
| 124 | scores.append((i,round(glyphscore))) |
| 125 | Testfont.close() |
| 126 | return scores |