예제 #1
0
 def run(self):
     """Core logic of the application"""
     if self.format == 'svg':
         font = None # Embed no font info for SVGs, SVGs use font-family attribute
         visitor = WidthHeightSVGOutputVisitor
     else:
         # Be specific about fonts with PNGs as font files are otherwise platform specific
         if self.proportional:
             font = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
                                 "LiberationSans-Regular.ttf")
         else:
             font = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
                                 "LiberationMono-Regular.ttf")
         visitor = aafigure.pil.PILOutputVisitor
     aafigure.process(unicode('\n'.join(self.lines), 'utf-8'), visitor,
                      options={'file_like': self.outfile,
                               'proportional': self.proportional,
                               'textual': self.textual,
                               'line_width': self.linewidth,
                               'scale': self.scaling,
                               'aspect': self.aspect,
                               'fill': self.fill,
                               'foreground': self.foreground,
                               'background': self.background,
                               'format':self.format,
                               'font': font
                               })
예제 #2
0
파일: aafig2img.py 프로젝트: kghost/config
 def run(self):
     """Core logic of the application"""
     outfile = os.path.abspath(self.options.outfile)
     outdir = os.path.dirname(outfile)
     if not os.path.isdir(outdir):
         raise AppError, 'directory does not exist: %s' % outdir
     skip = False
     if self.infile == '-':
         source = sys.stdin.read()
         checksum = md5.new(source).digest()
         f = os.path.splitext(outfile)[0] + '.md5'
         if self.options.modified:
             if os.path.isfile(f) and os.path.isfile(outfile) and \
                     checksum == open(f, 'rb').read():
                 skip = True
             open(f, 'wb').write(checksum)
     else:
         if not os.path.isfile(self.infile):
             raise AppError, 'input file does not exist: %s' % self.infile
         if self.options.modified and os.path.isfile(outfile) and \
                 os.path.getmtime(self.infile) <= os.path.getmtime(outfile):
             skip = True
         source = open(self.infile).read()
     if skip:
         print_verbose('Skipped: no change: %s' % outfile)
         return
     if self.options.format == 'svg':
         font = None # Embed no font info for SVGs, SVGs use font-family attribute
         visitor = WidthHeightSVGOutputVisitor
     else:
         # Be specific about fonts with PNGs as font files are otherwise platform specific
         if self.options.proportional:
             font = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
                                 "LiberationSans-Regular.ttf")
         else:
             font = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
                                 "LiberationMono-Regular.ttf")
         visitor = aafigure.pil.PILOutputVisitor
     aafigure.process(unicode(source, 'utf-8'), visitor,
                      options={'file_like': open(outfile, "wb"),
                               'proportional': self.options.proportional,
                               'textual': self.options.textual,
                               'line_width': self.options.linewidth,
                               'scale': self.options.scale,
                               'aspect': self.options.aspect,
                               'fill': self.options.fill,
                               'foreground': self.options.foreground,
                               'background': self.options.background,
                               'format':self.options.format,
                               'font': font
                               })
     # To suppress asciidoc 'no output from filter' warnings.
     if self.infile == '-':
         sys.stdout.write(' ')
예제 #3
0
 def run(self):
     """Core logic of the application"""
     outfile = os.path.abspath(self.options.outfile)
     outdir = os.path.dirname(outfile)
     if not os.path.isdir(outdir):
         raise AppError, 'directory does not exist: %s' % outdir
     skip = False
     if self.infile == '-':
         source = sys.stdin.read()
         checksum = md5.new(source).digest()
         f = os.path.splitext(outfile)[0] + '.md5'
         if self.options.modified:
             if os.path.isfile(f) and os.path.isfile(outfile) and \
                     checksum == open(f, 'rb').read():
                 skip = True
             open(f, 'wb').write(checksum)
     else:
         if not os.path.isfile(self.infile):
             raise AppError, 'input file does not exist: %s' % self.infile
         if self.options.modified and os.path.isfile(outfile) and \
                 os.path.getmtime(self.infile) <= os.path.getmtime(outfile):
             skip = True
         source = open(self.infile).read()
     if skip:
         print_verbose('Skipped: no change: %s' % outfile)
         return
     if self.options.format == 'svg':
         font = None # Embed no font info for SVGs, SVGs use font-family attribute
         visitor = WidthHeightSVGOutputVisitor
     else:
         # Be specific about fonts with PNGs as font files are otherwise platform specific
         if self.options.proportional:
             font = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
                                 "LiberationSans-Regular.ttf")
         else:
             font = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
                                 "LiberationMono-Regular.ttf")
         visitor = aafigure.pil.PILOutputVisitor
     aafigure.process(unicode(source, 'utf-8'), visitor,
                      options={'file_like': open(outfile, "wb"),
                               'proportional': self.options.proportional,
                               'textual': self.options.textual,
                               'line_width': self.options.linewidth,
                               'scale': self.options.scale,
                               'aspect': self.options.aspect,
                               'fill': self.options.fill,
                               'foreground': self.options.foreground,
                               'background': self.options.background,
                               'format':self.options.format,
                               'font': font
                               })
     # To suppress asciidoc 'no output from filter' warnings.
     if self.infile == '-':
         sys.stdout.write(' ')
 def gen_flowable(self, style_options):
     options = dict(style_options)
     # explicit :option: always precedes
     options.update(self.options)
     visitor = aafigure.process('\n'.join(self.content),
                                aafigure.pdf.PDFOutputVisitor,
                                options=options)
     return renderPDF.GraphicsFlowable(visitor.drawing)
 def gen_flowable(self, style_options):
     options = dict(style_options)
     # explicit :option: always precedes
     options.update(self.options)
     visitor = aafigure.process(
         '\n'.join(self.content),
         aafigure.pdf.PDFOutputVisitor,
         options=options
     )
     return renderPDF.GraphicsFlowable(visitor.drawing)
예제 #6
0
파일: demo.py 프로젝트: garyvdm/aafigure
print ascii_art
# Parse the image.
aaimg = aafigure.AsciiArtImage(ascii_art)
aaimg.recognize()

# For fun, output the ASCII version in the console.
print " output ".center(78, '=')
aav = aafigure.aa.AsciiOutputVisitor({'file_like':sys.stdout, 'scale':2})
aav.visit_image(aaimg)
print "="*78

# Writing an SVG file would be possible in a similar way, but there is the much
# easier render function for that.

# A stringIO object is returned for the output when the output parameter is not
# given. If it were, the output would be directly written to that object.
visitor, output = aafigure.render(ascii_art, options={'format':'svg'})

# The process method can be used for a lower level access. The visitor class
# has to be specified by the user in this case.  To get output, a file like
# object has to be passed in the options:
# {'file_like' = open("somefile.svg", "wb")}
import aafigure.svg
import StringIO
fl = StringIO.StringIO()
visitor = aafigure.process(
    ascii_art,
    aafigure.svg.SVGOutputVisitor,
    options={'file_like': fl}
)
예제 #7
0
 def test_process_api(self):
     output = BytesIO()
     visitor = aafigure.process(ascii_art,
                                aafigure.svg.SVGOutputVisitor,
                                options={'file_like': output})
     self.assertTrue(b'<svg' in output.getvalue())
예제 #8
0
파일: demo.py 프로젝트: return42/aafigure
# Show what we're parsing.
print " input ".center(78, '=')
print ascii_art
# Parse the image.
aaimg = aafigure.AsciiArtImage(ascii_art)
aaimg.recognize()

# For fun, output the ASCII version in the console.
print " output ".center(78, '=')
aav = aafigure.aa.AsciiOutputVisitor({'file_like': sys.stdout, 'scale': 2})
aav.visit_image(aaimg)
print "=" * 78

# Writing an SVG file would be possible in a similar way, but there is the much
# easier render function for that.

# A stringIO object is returned for the output when the output parameter is not
# given. If it were, the output would be directly written to that object.
visitor, output = aafigure.render(ascii_art, options={'format': 'svg'})

# The process method can be used for a lower level access. The visitor class
# has to be specified by the user in this case.  To get output, a file like
# object has to be passed in the options:
# {'file_like' = open("somefile.svg", "wb")}
import aafigure.svg
import StringIO
fl = StringIO.StringIO()
visitor = aafigure.process(ascii_art,
                           aafigure.svg.SVGOutputVisitor,
                           options={'file_like': fl})