コード例 #1
0
ファイル: demo_page.py プロジェクト: nickflink/pyfreetype
def generate_page(fontfile, str="The quick brown fox jumped over the lazy dog", sizes=[72,60,48,44,36,32,30,28,24,22,21,20,18,16,14,12,11,10,9,8]):
	font = pyfreetype.open_font(fontfile)
	
	image = PIL.Image.new("L", (1024,1024))
	
	pen_x = 0
	pen_y = 0
	
	for size in sizes:
		font.set_char_size(size, size, 72, 72)
		
		# move to the baseline
		pen_x = 0
		pen_y += int(font.size.height)
		
		for ch in str:
			bitmap = font.get_char_bitmap(ord(ch))
			metrics = font.get_char_metrics(ord(ch))
			
			ch_bitmap = PIL.Image.new("L", (bitmap.width, bitmap.height))
			ch_bitmap.putdata(bitmap.pixels)
			
			if pen_x + bitmap.left + bitmap.width > 1024:
				pen_x = 0
				pen_y += int(font.size.height)
	
			image.paste(ch_bitmap, (pen_x+bitmap.left, pen_y-bitmap.top), ch_bitmap)
			
			pen_x += int(metrics.advanceX)
			pen_y += int(metrics.advanceY)
			
	return image
コード例 #2
0
ファイル: compare.py プロジェクト: nickflink/pyfreetype
def generate_comparison(fontfiles, str, size=28, width=1024):
	image_width = width
	image_height = 1024
	image = PIL.Image.new("L", (image_width, image_height))
	
	pen_x = 0
	pen_y = 0
	
	for file in fontfiles:
		font = pyfreetype.open_font(file)
		font.set_char_size(size, size, 72, 72)
		
		# move to the baseline
		pen_x = 0
		pen_y += int(font.size.height)
		
		for ch in "%s-%s: %s" % (font.family_name, font.style_name, str):
			bitmap = font.get_char_bitmap(ord(ch))
			metrics = font.get_char_metrics(ord(ch))
			
			ch_bitmap = PIL.Image.new("L", (bitmap.width, bitmap.height))
			ch_bitmap.putdata(bitmap.pixels)
			
			if pen_x + bitmap.left + bitmap.width > image_width:
				pen_x = 0
				pen_y += int(font.size.height)
	
			if pen_y-bitmap.top+bitmap.height >= image_height:
				image_height *= 2
				image_large = PIL.Image.new("L", (image_width, image_height))
				image_large.paste(image, (0, 0))
				image = image_large
	
			image.paste(ch_bitmap, (pen_x+bitmap.left, pen_y-bitmap.top), ch_bitmap)
			
			pen_x += int(metrics.advanceX)
			pen_y += int(metrics.advanceY)
		
		del font
	
	return image.crop((0, 0, image_width, pen_y+64))
コード例 #3
0
def build(fontfile, size, charset, max_texture_width=256, spacing=4):
	font = pyfreetype.open_font(fontfile)
	font.set_pixel_size(size, size)
	
	bitmaps = {}
	
	max_width = 0
	max_height = 0
	
	for ch in charset:
		bitmap = font.get_char_bitmap(ord(unicode(ch)))
		bitmaps[ch] = bitmap
		
		max_width = max(max_width, bitmap.width)
		max_height = max(max_height, bitmap.height)
	
	# adjust for spacing
	max_width += spacing * 2
	max_height += spacing * 2
	
	tex_width = min(max_width * len(charset), max_texture_width)
	tex_height = int(len(charset) / math.floor(tex_width / float(max_width)) * max_height)
	
	tex = PIL.Image.new("L", (tex_width, tex_height))
	
	pen_x = 0
	pen_y = 0
	
	metrics_dict = {}
	
	for ch in charset:
		bitmap = font.get_char_bitmap(ord(unicode(ch)))
		metrics = font.get_char_metrics(ord(unicode(ch)))
		
		metrics_dict[ch] = {
			'width':				metrics.width,
			'horiBearingX':			metrics.horiBearingX,
			'horiBearingY':			metrics.horiBearingY,
			'horiAdvance':			metrics.horiAdvance,
			'vertBearingX':			metrics.vertBearingX,
			'vertBearingY':			metrics.vertBearingY,
			'vertAdvance':			metrics.vertAdvance,
			'linearHoriAdvance':	metrics.linearHoriAdvance,
			'linearVertAdvance':	metrics.linearVertAdvance,
			'advanceX':				metrics.advanceX,
			'advanceY':				metrics.advanceY,
			'bitmapLeft':			pen_x+spacing,
			'bitmapTop':			pen_y+spacing,
			'bitmapWidth':			bitmap.width,
			'bitmapHeight':			bitmap.height,
		}
		
		image = PIL.Image.new("L", (bitmap.width, bitmap.height))
		image.putdata(bitmap.pixels)
		
		tex.paste(image, (pen_x+spacing, pen_y+spacing))
		
		del image
		
		pen_x += max_width
		
		if pen_x + max_width >= tex.size[0]:
			pen_x = 0
			pen_y += max_height
	
	kerning_pairs = []
	
	for left in charset:
		for right in charset:
			x, y = font.get_kerning(ord(unicode(left)), ord(unicode(right)))
			
			if x != 0 or y != 0:
				kerning_pairs.append({
					'left': left,
					'right': right,
					'x': x,
					'y': y,
				})
	
	metrics_dict['kerning_pairs'] = kerning_pairs
	
	del font
	
	return tex, json.dumps(metrics_dict, indent=1)