Beispiel #1
0
    def getTableSvg(self, glyphs, maxValue):
        glyphs.sort(lambda glyph0, glyph1:cmp(glyph0.codePoint, glyph1.codePoint))
        glyphs = glyphs[:1000]

        from tfs.common.TFSSvg import TFSSvg, TFSSvgPath, blendArgbColors

        CANVAS_BACKGROUND_COLOR = 0xffffffff
        CANVAS_BORDER_COLOR = 0x07fbfbfbf
        fiSvg = TFSSvg().withBackground(CANVAS_BACKGROUND_COLOR).withBorder(CANVAS_BORDER_COLOR)

        glyphCount = len(glyphs)
        margin = 10
        hSpacing = vSpacing = 10
        cellWidth = cellHeight = 10
        glyphsPerRow = 13
        width = 2 * margin + (glyphsPerRow * cellWidth) + ((glyphsPerRow - 1) * hSpacing)
        rowCount = int(math.ceil(glyphCount / glyphsPerRow))
        height = 2 * margin + (rowCount * cellHeight) + ((rowCount - 1) * vSpacing)

        for index, glyph in enumerate(glyphs):
            x = index % glyphsPerRow
            y = int(math.floor(index / glyphsPerRow))
            corner = TFSPoint(margin + x * (cellWidth + hSpacing),
                              margin + y * (cellHeight + vSpacing))
            path = polygonWithPoints(corner,
                                     corner.right(cellWidth),
                                     corner.right(cellWidth).up(cellHeight),
                                     corner.up(cellHeight))
            path = path.applyScaleXY(1.0, -1.0)
            minGlyphColor = 0xffafafff
            maxGlyphColor = 0xff0f0f5f
            phase = glyph.count / float(maxValue)
            glyphColor = blendArgbColors(minGlyphColor, maxGlyphColor, phase)
            fiSvg.addItem(TFSSvgPath(path).addFill(glyphColor))

        return fiSvg.renderRaw(None, width, height)
Beispiel #2
0
    def dumpMetrics(self):

        print
        print 'fonts scanned:', len(self.processedPostscriptNames)
        print 'fonts processed:', len(self.nonEmptyPostscriptNames)
        print 'glyphs observed:', len(self.glyphCountMap)
        maxGlyphCount = reduce(max, self.glyphCountMap.values())
        print 'Highest glyph frequency:', maxGlyphCount

        glyphs = []
        for codePoint, count in self.glyphCountMap.iteritems():
            glyph = TFSMap()
            glyph.codePoint = codePoint
            glyph.count = count
            glyphs.append(glyph)

        def glyphCountSort(glyph0, glyph1):
            # Negate; we want reverse order for count.
            result = -cmp(glyph0.count, glyph1.count)
            if result != 0:
                return result

            return cmp(glyph0.codePoint, glyph1.codePoint)

        glyphs.sort(glyphCountSort)

        print 'Most common glyph:', glyphs[0]
        print 'Least common glyph:', glyphs[-1]

        locale.setlocale(locale.LC_ALL, 'en_US')

        pres = []
        for glyph in glyphs[:1000]:
#            pre = {}

            pre = {
#                    'glyphHex': hex(glyph.codePoint),
                    'glyphHex': '%04X' % glyph.codePoint,
                    'glyphName': getUnicodeLongName(glyph.codePoint,
                                                    ignoreUnknown=True,
                                                    skipValidation=True),
                   'glyphCount': locale.format("%d", glyph.count, grouping=True),
                   'glyphPercent': '%0.2f%%' % (100.0 * glyph.count / float(len(self.nonEmptyPostscriptNames))),
#                   'glyphColor': '%06X' % glyphColor,
                   }

#            pre['value'] = '%s %s: %d (%0.2f%%)' % ( hex(glyph.codePoint),
#                                            getUnicodeLongName(glyph.codePoint,
#                                                                    ignoreUnknown=True,
#                                                                    skipValidation=True),
#                                            glyph.count,
#                                            glyph.count / float(len(self.nonEmptyPostscriptNames)),
#                                            )
#            print 'pre', pre
            pres.append(pre)


        glyphs.sort(lambda glyph0, glyph1:cmp(glyph0.codePoint, glyph1.codePoint))
        headerPres = []
        for glyph in glyphs[:10000]:

            minGlyphColor = 0xffafafff
            maxGlyphColor = 0xff0f0f5f
            phase = glyph.count / float(maxGlyphCount)
            from tfs.common.TFSSvg import blendArgbColors
            glyphColor = 0xffffff & blendArgbColors(minGlyphColor, maxGlyphColor, phase)

            pre = {
#                    'glyphHex': hex(glyph.codePoint),
                    'glyphHex': '%X' % glyph.codePoint,
                    'glyphName': getUnicodeLongName(glyph.codePoint,
                                                    ignoreUnknown=True,
                                                    skipValidation=True),
                   'glyphCount': locale.format("%d", glyph.count, grouping=True),
                   'glyphPercent': '%0.2f%%' % (100.0 * glyph.count / float(len(self.nonEmptyPostscriptNames))),
                   'glyphColor': '%06X' % glyphColor,
                   }
#            pre['value'] = '%s %s: %d (%0.2f%%)' % ( hex(glyph.codePoint),
#                                            getUnicodeCharacterName(glyph.codePoint,
#                                                                    ignoreUnknown=True,
#                                                                    skipValidation=True),
#                                            glyph.count,
#                                            glyph.count / float(len(self.nonEmptyPostscriptNames)),
#                                            )
#            print 'pre', pre
            headerPres.append(pre)

#        tableSvg = self.getTableSvg(glyphs, maxGlyphCount)


        self.writeMustacheLog('gia_pre_template.txt',
                              'most_common_glyphs.html',
                              mustacheVars = { 'pres': pres,
                                              'headerPres': headerPres,
                                              'pageTitle': '1,000 Most Commonly Implemented Font Glyphs',
                                              'headerTitle': 'Glyph Implementation Statistics',
                                              'statsTitle': 'Statistics',
                                              'stats': (
                                                        { 'key': 'Total Fonts Scanned', 'value': locale.format("%d", len(self.processedPostscriptNames), grouping=True), },
                                                        { 'key': 'Distinct Code Points Observed', 'value': locale.format("%d", len(self.glyphCountMap), grouping=True), },
                                                        ),
                                              }
#                              replaceMap = {'<!-- SVG Graph Placeholder -->': tableSvg,
#                                            }
                              )