Beispiel #1
0
def wmf2eps(wmf_fp, title=None):
    """
    Return a psg.document.dsc.eps_document instance containing a rendition
    of the wmf file pointed fo by 'wmf_fp'.

    @param wmf_fp: File pointer open for reading (seeking is not needed)
    @param title: Title string for the EPS document.
    """
    eps = eps_document(title=title)
    page = eps.page
    
    reader = postscript_reader(wmf_fp)

    # This renders the wmf file in memory. If one wanted to comply
    # with psg's 'lazy' pollicy for wmf files, one could easily do so
    # by writing a special subclass of eps_document that keeps a
    # reference to the reader and then calls save() on write_to().

    outstream = eps.page

    # Colorize the PostScript to be able to tell it apart from
    # debugging info.
    if debug.verbose:
        outstream = color_ps(outstream)

    reader.save(outstream)

    width = float(reader.bounding_box.width()) / float(reader.resolution) *72.0
    height = float(reader.bounding_box.height())/float(reader.resolution) *72.0
    eps.header.bounding_box = ( 0, 0, width, height, )
    eps.header.hires_bounding_box = ( 0, 0, width, height, )

    return eps
def main():
    if len(sys.argv) < 3:
        print "Usage: %s <outline file> <metrics file>" % sys.argv[0]
        sys.exit(-1)


    margin = mm(3)
    font_size = 20
    
    outline_file = open(sys.argv[1])
    metrics_file = open(sys.argv[2])

    # Load the font
    font = type1(outline_file, metrics_file)

    # The text to be set
    # text = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    text = font.full_name

    # Create the EPS document
    eps = eps_document(title=font.full_name)
    page = eps.page
    
    # Register the font with the document
    font_wrapper = page.register_font(font)
    
    # The actual string sits in its own textbox

    # width = the width of the string
    width = font.metrics.stringwidth(text, font_size)
    
    tb = textbox(page, margin, margin, width, font_size)
    tb.set_font(font_wrapper, font_size)
    tb.typeset(unicode(text, "iso-8859-1"))

    page.append(tb)
    
    # Page height = the height of the largest character (=font bbox height).
    fontbb = font.metrics.font_bounding_box()
    page_height = fontbb.ury * float(font_size) # scale to font size, too
    page_bb = bounding_box(0, 0, tb.w() + 2*margin, page_height + 2*margin)
    eps.header.bounding_box = page_bb.as_tuple()
    eps.header.hires_bounding_box = page_bb.as_tuple()

    eps.write_to(sys.stdout)
Beispiel #3
0
def main():
    if len(sys.argv) < 3:
        print "Usage: %s <outline file> <metrics file>" % sys.argv[0]
        sys.exit(-1)


    cellpadding = mm(1)
    page_margin = mm(16)
    
    font_size = 10
    chars = u"""ABCDEFGHIJKLMNOPQRSTUVWXYZ
                abcdefghijklmnopqrstuvwxyz
                äöüÄÖÜß 0123456789
                !\"§$%&/()=?€@ „“ “” »«"""

    
    outline_file = open(sys.argv[1])
    metrics_file = open(sys.argv[2])

    # Load the font
    font = type1(outline_file, metrics_file)

    # Create the EPS document
    eps = eps_document(title=font.full_name)
    page = eps.page
    
    # Register the font with the document
    font_wrapper = page.register_font(font)

    # Ok, we got to find out a number of things: Dimensions of the cells,
    # dimensions of the table
    m = 0
    for char in chars:
        m = max(m, font.metrics.stringwidth(char, font_size))

    td_width = m + 2*cellpadding
    td_height = font_size + 2*cellpadding

    lines = map(strip, split(chars, "\n"))
    lines.reverse()

    m = 0
    for line in lines:
        m = max(m, len(line))

    cols = m
    rows = len(lines)

    table_width = cols * td_width
    table_height = rows * td_height

    # Create a canvas at the coordinates of the page_margins and width
    # the table's size.
    table = canvas(page, page_margin, page_margin, table_width, table_height,
                   border=True)

    # Draw the table grid by drawing row and column boundaries.
    print >> table, "gsave"
    print >> table, "0.4 setgray [] 0.5 setdash"
    for a in range(1, cols):
        print >> table, "newpath"
        print >> table, "%f %f moveto" % ( a * td_width, 0, )
        print >> table, "%f %f lineto" % ( a * td_width, table_height, )
        print >> table, "stroke"

    for a in range(1, rows):
        print >> table, "newpath"
        print >> table, "%f %f moveto" % ( 0, a * td_height, )
        print >> table, "%f %f lineto" % ( table_width, a * td_height, )
        print >> table, "stroke"
        
    print >> table, "grestore"

    print >> table, "/%s findfont" % font_wrapper.ps_name()
    print >> table, "%f scalefont" % font_size
    print >> table, "setfont"
    
    for lc, line in enumerate(lines):
        for cc, char in enumerate(line):
            x = cc * td_width + cellpadding
            y = lc * td_height + cellpadding
            psrep = font_wrapper.postscript_representation(char)
            
            print >> table, "%f %f moveto" % ( x, y, )
            print >> table, "(%s) show" % psrep

    page.append(table)

    page_bb = table.bounding_box()
    eps.header.bounding_box = page_bb.as_tuple()
    eps.header.hires_bounding_box = page_bb.as_tuple()

    eps.write_to(sys.stdout)