コード例 #1
0
def _get_nix_font_path(self, name, style):
	if style == 'bold' or style == 'italic':
		return _font_cache.get_font_path('%s %s' % (name, style.capitalize()))
	elif style == '':
		return _font_cache.get_font_path(name)
	else:
		return
コード例 #2
0
ファイル: ImageASCIIUI.py プロジェクト: cclauss/ImageToAscii
def _getfont(fontsize):
    '''Return the ImageFont for the font I'm using'''
    try:
        return ImageFont.truetype("DejaVuSansMono", fontsize * 4)
    except IOError:
        import _font_cache
        return ImageFont.truetype(_font_cache.get_font_path('DejaVuSansMono'))
コード例 #3
0
def _getfont(fontsize):
    '''Return the ImageFont for the font I'm using'''
    try:
        return ImageFont.truetype("DejaVuSansMono", fontsize*4)
    except IOError:
        import _font_cache
        return ImageFont.truetype(_font_cache.get_font_path('DejaVuSansMono'))
コード例 #4
0
ファイル: Image2ASCII.py プロジェクト: cclauss/Image2ASCII
def RenderASCII(text, fontsize=5, bgcolor='#EDEDED'):
	'''Create an image of ASCII text'''
	linelist=text.split('\n')
	try:
		font = ImageFont.truetype("DejaVuSansMono", fontsize*4)
	except:
		import _font_cache
		font = ImageFont.truetype(_font_cache.get_font_path('DejaVuSansMono'))
	width, height = font.getsize(linelist[1])

	image = Image.new("RGB", (width, height*len(linelist)), bgcolor)
	draw = ImageDraw.Draw(image)

	for x in range(len(linelist)):
		line = linelist[x]
		draw.text((0, x*height), line, (0, 0, 0), font=font)
	return image
コード例 #5
0
ファイル: Image2ASCII.py プロジェクト: cclauss/Image2ASCII
def RenderASCII(text, fontsize=5, bgcolor='#EDEDED'):
    '''Create an image of ASCII text'''
    linelist = text.split('\n')
    try:
        font = ImageFont.truetype("DejaVuSansMono", fontsize * 4)
    except:
        import _font_cache
        font = ImageFont.truetype(_font_cache.get_font_path('DejaVuSansMono'))
    width, height = font.getsize(linelist[1])

    image = Image.new("RGB", (width, height * len(linelist)), bgcolor)
    draw = ImageDraw.Draw(image)

    for x in range(len(linelist)):
        line = linelist[x]
        draw.text((0, x * height), line, (0, 0, 0), font=font)
    return image
コード例 #6
0
ファイル: ImageFont.py プロジェクト: WardF/XCode-Projects
def truetype(filename, size, index=0, encoding=""):
    "Load a truetype font file."
    try:
        #Patch for Pythonista to use built-in fonts on iOS:
        if not os.path.isfile(filename):
            import _font_cache
            filename = _font_cache.get_font_path(filename)
        return FreeTypeFont(filename, size, index, encoding)
    except IOError:
        if sys.platform == "win32":
            # check the windows font repository
            # NOTE: must use uppercase WINDIR, to work around bugs in
            # 1.5.2's os.environ.get()
            windir = os.environ.get("WINDIR")
            if windir:
                filename = os.path.join(windir, "fonts", filename)
                return FreeTypeFont(filename, size, index, encoding)
        raise
コード例 #7
0
ファイル: ImageFont.py プロジェクト: fhelmli/homeNOWG2
def truetype(font=None, size=10, index=0, encoding="", filename=None):
    """
    Load a TrueType or OpenType font file, and create a font object.
    This function loads a font object from the given file, and creates
    a font object for a font of the given size.

    This function requires the _imagingft service.

    :param font: A truetype font file. Under Windows, if the file
                     is not found in this filename, the loader also looks in
                     Windows :file:`fonts/` directory.
    :param size: The requested size, in points.
    :param index: Which font face to load (default is first available face).
    :param encoding: Which font encoding to use (default is Unicode). Common
                     encodings are "unic" (Unicode), "symb" (Microsoft
                     Symbol), "ADOB" (Adobe Standard), "ADBE" (Adobe Expert),
                     and "armn" (Apple Roman). See the FreeType documentation
                     for more information.
    :param filename: Deprecated. Please use font instead.
    :return: A font object.
    :exception IOError: If the file could not be read.
    """

    if filename:
        if warnings:
            warnings.warn(
                'filename parameter deprecated, '
                'please use font parameter instead.', DeprecationWarning)
        font = filename

    try:
        if not os.path.isfile(font):
            import _font_cache
            font = _font_cache.get_font_path(font)
        return FreeTypeFont(font, size, index, encoding)
    except IOError:
        ttf_filename = os.path.basename(font)

        dirs = []
        if sys.platform == "win32":
            # check the windows font repository
            # NOTE: must use uppercase WINDIR, to work around bugs in
            # 1.5.2's os.environ.get()
            windir = os.environ.get("WINDIR")
            if windir:
                dirs.append(os.path.join(windir, "fonts"))
        elif sys.platform in ('linux', 'linux2'):
            lindirs = os.environ.get("XDG_DATA_DIRS", "")
            if not lindirs:
                # According to the freedesktop spec, XDG_DATA_DIRS should
                # default to /usr/share
                lindirs = '/usr/share'
            dirs += [
                os.path.join(lindir, "fonts") for lindir in lindirs.split(":")
            ]
        elif sys.platform == 'darwin':
            dirs += [
                '/Library/Fonts', '/System/Library/Fonts',
                os.path.expanduser('~/Library/Fonts')
            ]

        ext = os.path.splitext(ttf_filename)[1]
        first_font_with_a_different_extension = None
        for directory in dirs:
            for walkroot, walkdir, walkfilenames in os.walk(directory):
                for walkfilename in walkfilenames:
                    if ext and walkfilename == ttf_filename:
                        fontpath = os.path.join(walkroot, walkfilename)
                        return FreeTypeFont(fontpath, size, index, encoding)
                    elif not ext and os.path.splitext(
                            walkfilename)[0] == ttf_filename:
                        fontpath = os.path.join(walkroot, walkfilename)
                        if os.path.splitext(fontpath)[1] == '.ttf':
                            return FreeTypeFont(fontpath, size, index,
                                                encoding)
                        if not ext and first_font_with_a_different_extension is None:
                            first_font_with_a_different_extension = fontpath
        if first_font_with_a_different_extension:
            return FreeTypeFont(first_font_with_a_different_extension, size,
                                index, encoding)
        raise
コード例 #8
0
def truetype(font=None, size=10, index=0, encoding="", filename=None):
    """
    Load a TrueType or OpenType font file, and create a font object.
    This function loads a font object from the given file, and creates
    a font object for a font of the given size.

    This function requires the _imagingft service.

    :param font: A truetype font file. Under Windows, if the file
                     is not found in this filename, the loader also looks in
                     Windows :file:`fonts/` directory.
    :param size: The requested size, in points.
    :param index: Which font face to load (default is first available face).
    :param encoding: Which font encoding to use (default is Unicode). Common
                     encodings are "unic" (Unicode), "symb" (Microsoft
                     Symbol), "ADOB" (Adobe Standard), "ADBE" (Adobe Expert),
                     and "armn" (Apple Roman). See the FreeType documentation
                     for more information.
    :param filename: Deprecated. Please use font instead.
    :return: A font object.
    :exception IOError: If the file could not be read.
    """

    if filename:
        if warnings:
            warnings.warn(
                'filename parameter deprecated, '
                'please use font parameter instead.',
                DeprecationWarning)
        font = filename

    try:
        if not os.path.isfile(font):
            import _font_cache
            font = _font_cache.get_font_path(font)
        return FreeTypeFont(font, size, index, encoding)
    except IOError:
        ttf_filename = os.path.basename(font)

        dirs = []
        if sys.platform == "win32":
            # check the windows font repository
            # NOTE: must use uppercase WINDIR, to work around bugs in
            # 1.5.2's os.environ.get()
            windir = os.environ.get("WINDIR")
            if windir:
                dirs.append(os.path.join(windir, "fonts"))
        elif sys.platform in ('linux', 'linux2'):
            lindirs = os.environ.get("XDG_DATA_DIRS", "")
            if not lindirs:
                # According to the freedesktop spec, XDG_DATA_DIRS should
                # default to /usr/share
                lindirs = '/usr/share'
            dirs += [os.path.join(lindir, "fonts") for lindir in lindirs.split(":")]
        elif sys.platform == 'darwin':
            dirs += ['/Library/Fonts', '/System/Library/Fonts',
                     os.path.expanduser('~/Library/Fonts')]

        ext = os.path.splitext(ttf_filename)[1]
        first_font_with_a_different_extension = None
        for directory in dirs:
            for walkroot, walkdir, walkfilenames in os.walk(directory):
                for walkfilename in walkfilenames:
                    if ext and walkfilename == ttf_filename:
                        fontpath = os.path.join(walkroot, walkfilename)
                        return FreeTypeFont(fontpath, size, index, encoding)
                    elif not ext and os.path.splitext(walkfilename)[0] == ttf_filename:
                        fontpath = os.path.join(walkroot, walkfilename)
                        if os.path.splitext(fontpath)[1] == '.ttf':
                            return FreeTypeFont(fontpath, size, index, encoding)
                        if not ext and first_font_with_a_different_extension is None:
                            first_font_with_a_different_extension = fontpath
        if first_font_with_a_different_extension:
            return FreeTypeFont(first_font_with_a_different_extension, size,
                                index, encoding)
        raise