Example #1
0
def write_sample(font, output_file):
    javascript = contents(
        os.path.join(os.path.dirname(__file__), "make_samples-javascript.js"))
    doc, tag, text = Doc().tagtext()
    with tag('html'):
        with tag('head'):
            with tag('title'):
                text(font['name'])
            with tag('style', type='text/css'):
                text(STYLESHEET)
            with tag('script', type='text/javascript'):
                # We could do this with a link, but I want the HTML file to be
                # self-contained.
                doc.asis("\n// ")
                doc.cdata('\n' + javascript + '\n// ')
                doc.asis("\n")
        with tag('body'):
            with tag('h1'):
                text(font['name'])
            with tag('table', id='glyphs', border='1'):
                index = 0
                for char in font['chars']:
                    with tag('tr'):
                        with tag('td', valign='top', align='right'):
                            # Column 1: indexes, character codes
                            text('%d.' % index)
                            doc.stag('br')
                            text('0x%x' % index)
                            doc.stag('br')
                            doc.asis('ASCII ')
                            text('0x%02x' % (index + 33))
                            doc.stag('br')
                            text('"%c"' % chr(index + 33))
                        with tag('td', ('class', 'metrics'),
                                 valign='top',
                                 align='left'):
                            # Column 2: metrics.  Most are added by javascript code above.n
                            doc.asis("o: ")
                            text('%d' % char['o'])
                            doc.stag('br')
                        with tag('td'):
                            # Column 3: SVG rendering of the glyph
                            with tag('svg',
                                     xmlns='http://www.w3.org/2000/svg'):
                                with tag('g'):
                                    doc.stag('path', ('d', char['d']),
                                             ('fill', 'none'),
                                             ('stroke', 'black'),
                                             ('stroke-width', '1px'))
                        with tag('td', valign='top', align='left'):
                            # Column 4: The SVG path
                            with tag('pre'):
                                text(pretty_path(char['d']))
                    index += 1
    with open(output_file, 'w') as out:
        out.write(indent(doc.getvalue()))