def ttfont_for(font): if font.path: path, index = parse_fontpath(font.path) if index: ttfont = ImageFont.truetype(path, font.size, index=index) else: ttfont = ImageFont.truetype(path, font.size) else: ttfont = None return ttfont
def set_font(self, font): if font.path is None: msg = "Could not detect fonts, use --font opiton\n" raise RuntimeError(msg) if font.path not in self.fonts: path, index = parse_fontpath(font.path) if index: ttfont = TTFont(font.path, path, subfontIndex=index) else: ttfont = TTFont(font.path, path) pdfmetrics.registerFont(ttfont) self.fonts[font.path] = ttfont self.canvas.setFont(font.path, font.size)
def detectfont(options): import glob fontdirs = [ '/usr/share/fonts', '/Library/Fonts', '/System/Library/Fonts', 'c:/windows/fonts', '/usr/local/share/font-*', ] fontfiles = [ 'ipagp.ttf', 'ipagp.otf', 'VL-PGothic-Regular.ttf', 'Hiragino Sans GB W3.otf', 'AppleGothic.ttf', 'msgothic.ttf', 'msgoth04.ttf', 'msgothic.ttc', ] fontpath = None if options.font: for path in options.font: _path, _ = parse_fontpath(path) if os.path.isfile(_path): fontpath = path break else: msg = 'fontfile is not found: %s' % options.font raise RuntimeError(msg) if fontpath is None: globber = (glob.glob(d) for d in fontdirs) for fontdir in sum(globber, []): for root, _, files in os.walk(fontdir): for font in fontfiles: if font in files: fontpath = os.path.join(root, font) break return fontpath
def _detectfont(font): fontdirs = [ '/usr/share/fonts', '/Library/Fonts', '/System/Library/Fonts', 'c:/windows/fonts', '/usr/local/share/font-*', ] fontfiles = [ 'ipagp.ttf', 'ipagp.otf', 'VL-PGothic-Regular.ttf', 'Hiragino Sans GB W3.otf', 'AppleGothic.ttf', 'msgothic.ttf', 'msgoth04.ttf', 'msgothic.ttc', ] fontpath = None if font: from blockdiag.utils.fontmap import parse_fontpath for path in font: _path, _ = parse_fontpath(path) if os.path.isfile(_path): fontpath = path break else: msg = 'fontfile is not found: %s' % font raise RuntimeError(msg) if fontpath is None: globber = (glob.glob(d) for d in fontdirs) for fontdir in sum(globber, []): for root, _, files in os.walk(fontdir): for font_ in fontfiles: if font_ in files: fontpath = os.path.join(root, font_) break return fontpath