本文整理汇总了Python中fontforge.open方法的典型用法代码示例。如果您正苦于以下问题:Python fontforge.open方法的具体用法?Python fontforge.open怎么用?Python fontforge.open使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fontforge
的用法示例。
在下文中一共展示了fontforge.open方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: save
# 需要导入模块: import fontforge [as 别名]
# 或者: from fontforge import open [as 别名]
def save(self):
start_str = format_codepoint(self.start)
end_str = format_codepoint(self.last_codepoint)
base_name = 'tofu_{}_{}'.format(start_str, end_str)
save_name_tmp = '{}.sfd'.format(base_name)
with open(save_name_tmp, 'w') as file:
file.write('\n'.join(self.data))
font = fontforge.open(save_name_tmp)
os.remove(save_name_tmp)
font.familyname = 'Tofu'
font.fontname = 'Tofu'
font.fullname = 'Tofu {} - {}'.format(start_str, end_str)
font.comment = 'The complete opposite of a font'
font.version = '0.2'
font.copyright = FONT_LICENSE
self.needs_save = False
return font
示例2: display_unicode_utf8
# 需要导入模块: import fontforge [as 别名]
# 或者: from fontforge import open [as 别名]
def display_unicode_utf8(ctx, fd=sys.stdout):
"""display unicode characters as UTF-8
:param MergingContext ctx: a merging font context
:param file fd: display target"""
font = fontforge.open(ctx.filename)
unicode_range = ctx.unicode_range
start = 0
end = 0
if len(unicode_range) >= 1:
start = int(unicode_range[0], 16)
if len(unicode_range) >= 2:
end = int(unicode_range[1], 16)
remap_start_point = ctx.remap_start_point
delta = 0
if remap_start_point is not None:
delta = int(remap_start_point, 16) - start
if start is 0:
font.selection.all()
else:
font.selection.select(('ranges', 'unicode'), start, end)
length = 0
line = ''
fd.write('{0:-^80}\n'.format(' ' + ctx.id + ' '))
for glyph in list(font.selection.byGlyphs):
line += unichr(glyph.encoding + delta)
info = get_glyph_size_info(glyph)
if info.width <= font.em / 2:
# half width
line += ' '
length += 1
if length is 40:
fd.write(line.encode('utf-8') + '\n')
length = 0
line = ''
if length > 0:
fd.write(line.encode('utf-8') + '\n')
fd.flush()
font.close()
示例3: __init__
# 需要导入模块: import fontforge [as 别名]
# 或者: from fontforge import open [as 别名]
def __init__(self, path):
self.log = getLogger()
self.base_font_path = path
font = fontforge.open(path)
if font.iscid:
raise RuntimeError('CID font is not supported yet.')
font.encoding = 'UnicodeFull'
self.base_font = font
self._hints = None
示例4: OpenFont
# 需要导入模块: import fontforge [as 别名]
# 或者: from fontforge import open [as 别名]
def OpenFont(fontPath):
obj = fontforge.open(fontPath)
if __DEBUG__:
print "OpenFont", fontPath
print "result:", obj
return RFont(obj)
示例5: processGlyphList
# 需要导入模块: import fontforge [as 别名]
# 或者: from fontforge import open [as 别名]
def processGlyphList():
for line in open(glyphlist):
li=line.strip()
if not li.startswith("#"):
char = line.split(";")
if char[0][0].isupper():
path = glyphPath+"upper/"
else:
path = glyphPath+"lower/"
if os.path.isfile(path+char[0]+".svg"):
createNamedChar(char[0], path+char[0]+".svg")
示例6: center_glyphs
# 需要导入模块: import fontforge [as 别名]
# 或者: from fontforge import open [as 别名]
def center_glyphs(src_font_path, dst_font_path, dst_name):
fnt = trim_font(fontforge.open(src_font_path))
size = max(max(g.width for g in fnt.glyphs()),
max(glyph_height(g) for g in fnt.glyphs()))
fnt.ascent, fnt.descent = size, 0
for glyph in fnt.glyphs():
scale_single_glyph(glyph, size, size)
fnt.sfnt_names = []
fnt.fontname = fnt.familyname = fnt.fullname = dst_name
fnt.generate(dst_font_path)
示例7: merge_one
# 需要导入模块: import fontforge [as 别名]
# 或者: from fontforge import open [as 别名]
def merge_one(self, ctx):
self.log.debug('"%s" merging...', ctx.filename)
font = fontforge.open(ctx.filename)
unicode_range = ctx.unicode_range
glyph_scale = ctx.scale
ext_start = 0
ext_end = 0
if len(unicode_range) >= 1:
ext_start = int(unicode_range[0], 16)
if len(unicode_range) >= 2:
ext_end = int(unicode_range[1], 16)
base_start = ext_start
remap_start_point = ctx.remap_start_point
if remap_start_point is not None:
base_start = int(remap_start_point, 16)
base_font_height = get_height(self.base_font)
font_height = get_height(font)
# scale transform
scale_ratio_x = round(float(self.base_font.em) / font.em, 3)
scale_ratio_y = round(float(base_font_height) / font_height, 3)
scale = psMat.scale(scale_ratio_x * glyph_scale, scale_ratio_y * glyph_scale)
if ext_start is 0:
font.selection.all()
else:
font.selection.select(('ranges', 'unicode'), ext_start, ext_end)
# copy and transform glyphs
for glyph in list(font.selection.byGlyphs):
index = base_start + (glyph.encoding - ext_start)
font.selection.select(glyph.encoding)
font.copy()
self.base_font.selection.select(index)
self.base_font.paste()
_glyph = self.base_font[index]
_glyph.transform(scale)
_glyph.glyphname = glyph.glyphname
info = get_glyph_size_info(_glyph)
move_x = 0
move_y = 0
if self.hints.ascent < info.ymax:
move_y = self.hints.ascent - info.ymax
if glyph_scale < 1.0 and ctx.adjust_position:
move_x += info.width * (1.0 - glyph_scale) / 2.0
move_y -= info.height * (1.0 - glyph_scale) / 2.0
info = get_glyph_size_info(_glyph)
if info.width + move_x < self.base_font.em:
_glyph.left_side_bearing += move_x
hint = self.get_hint(info.width)
if hint.glyph_width < info.width and ctx.adjust_position:
delta = info.width - hint.glyph_width
move_x += 1.0 - (delta / hint.glyph_width)
_glyph.transform(psMat.translate(move_x, move_y))
_glyph.width = hint.glyph_width
_glyph.vwidth = hint.glyph_vwidth
# add copyright
if font.copyright and font.copyright not in self.base_font.copyright:
self.base_font.copyright += '\n' + font.copyright
font.close()
self.log.debug('"%s" merged.', ctx.filename)
示例8: dumpFontForgeAPI
# 需要导入模块: import fontforge [as 别名]
# 或者: from fontforge import open [as 别名]
def dumpFontForgeAPI(testFontPath, printModule=False,
printFont=False, printGlyph=False,
printLayer=False, printContour=False, printPoint=False):
def printAPI(item, name):
print
print "-"*80
print "API of", item
names = dir(item)
names.sort()
print
if printAPI:
for n in names:
print
print "%s.%s"%(name, n)
try:
print getattr(item, n).__doc__
except:
print "# error showing", n
# module
if printModule:
print "module file:", fontforge.__file__
print "version:", fontforge.version()
print "module doc:", fontforge.__doc__
print "has User Interface:", fontforge.hasUserInterface()
print "has Spiro:", fontforge.hasSpiro()
printAPI(fontforge, "fontforge")
# font
fontObj = fontforge.open(testFontPath)
if printFont:
printAPI(fontObj, "font")
# glyph
glyphObj = fontObj["A"]
if printGlyph:
printAPI(glyphObj, "glyph")
# layer
layerObj = glyphObj.foreground
if printLayer:
printAPI(layerObj, "layer")
# contour
contourObj = layerObj[0]
if printContour:
printAPI(contourObj, "contour")
# point
if printPoint:
pointObj = contourObj[0]
printAPI(pointObj, "point")
# other objects
penObj = glyphObj.glyphPen()
printAPI(penObj, "glyphPen")
# use your own paths here.
示例9: main
# 需要导入模块: import fontforge [as 别名]
# 或者: from fontforge import open [as 别名]
def main():
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('font_file')
arg_parser.add_argument('chart_png_filename')
arg_parser.add_argument('--font-size', type=int, default=16)
arg_parser.add_argument('--bit-depth', choices=[1, 8], default=8, type=int)
args = arg_parser.parse_args()
font = fontforge.open(args.font_file)
print("# fontname =", font.fontname)
print("# familyname =", font.familyname)
print("# fullname = ", font.fullname)
font_images = {}
temp_dir = tempfile.mkdtemp()
try:
for glyph in font.glyphs():
print(glyph.unicode,
'U+{:04x}'.format(glyph.unicode) if glyph.unicode > 0 else '',
glyph.glyphname,
sep='\t'
)
if glyph.unicode > 0:
path = os.path.join(temp_dir, '{:04x}.png'.format(glyph.unicode))
glyph.export(path, args.font_size, args.bit_depth)
with open(path, 'rb') as file:
font_images[glyph.unicode] = file.read()
finally:
shutil.rmtree(temp_dir)
cols = 32
rows = int(math.ceil(len(font_images) / cols))
col_width = 32
row_height = 32
draw_font = PIL.ImageFont.load_default()
image = PIL.Image.new('L', (cols * col_width, rows * row_height), color=255)
draw_context = PIL.ImageDraw.Draw(image)
for index, char_code in enumerate(sorted(font_images.keys())):
image_data = font_images[char_code]
char_image = PIL.Image.open(io.BytesIO(image_data))
col = index % cols
row = index // cols
image.paste(char_image, (col * col_width, row * row_height + 12))
draw_context.text((col * col_width, row * row_height),
'{:02x}'.format(char_code), font=draw_font)
image.save(args.chart_png_filename)