温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Python怎么对点阵字体进行读取

发布时间:2021-02-26 15:21:41 来源:亿速云 阅读:428 作者:戴恩恩 栏目:开发技术

本文章向大家介绍使用Python怎么对点阵字体进行读取,主要包括使用Python怎么对点阵字体进行读取的使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

python可以做什么

Python是一种编程语言,内置了许多有效的工具,Python几乎无所不能,该语言通俗易懂、容易入门、功能强大,在许多领域中都有广泛的应用,例如最热门的大数据分析,人工智能,Web开发等。

使用Python读取并显示的过程如下:

根据中文字符获取GB2312编码

通过GB2312编码计算该汉字在点阵字库中的区位和码位

通过区位和码位计算在点阵字库中的偏移量

基于偏移量获取该汉字的32个像素存储字节

解析像素字节获取点阵坐标信息

在对应的坐标显示信息位。如该像素点是否显示点亮

使用该代码前提:下载点阵字体库到本地,这里默认使用的是hzk16点阵字库

代码如下:

#!/usr/bin/python
#encoding: utf-8
import binascii
 
RECT_HEIGHT = 16
RECT_WIDTH = 16
BYTE_COUNT_PER_ROW = RECT_WIDTH / 8
BYTE_COUNT_PER_FONT = BYTE_COUNT_PER_ROW * RECT_HEIGHT
 
KEYS = [0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01]
 
class FontRender(object):
 def __init__(self, font_file,
  rect_height=RECT_HEIGHT, rect_width=RECT_WIDTH, byte_count_per_row=BYTE_COUNT_PER_ROW):
 self.font_file = font_file
 self.rect_height = rect_height
 self.rect_width = rect_width
 self.byte_count_per_row = byte_count_per_row
 
 self.__init_rect_list__()
 
 def __init_rect_list__(self):
 self.rect_list = [] * RECT_HEIGHT
 
 for i in range(RECT_HEIGHT):
 self.rect_list.append([] * RECT_WIDTH)
 
 def get_font_area_index(self, txt, encoding='utf-8'):
 if not isinstance(txt, unicode):
 txt = txt.decode(encoding)
 
 gb2312 = txt.encode('gb2312')
 hex_str = binascii.b2a_hex(gb2312)
 
 area = eval('0x' + hex_str[:2]) - 0xA0
 index = eval('0x' + hex_str[2:]) - 0xA0
 
 return area, index
 
 def get_font_rect(self, area, index):
 offset = (94 * (area-1) + (index-1)) * BYTE_COUNT_PER_FONT
 btxt = None
 
 with open(self.font_file, "rb") as f:
 f.seek(offset)
 btxt = f.read(BYTE_COUNT_PER_FONT)
 
 return btxt
 
 def convert_font_rect(self, font_rect, ft=1, ff=0):
 for k in range(len(font_rect) / self.byte_count_per_row):
 row_list = self.rect_list[k]
 for j in range(self.byte_count_per_row):
 for i in range(8):
  asc = binascii.b2a_hex(font_rect[k * self.byte_count_per_row + j])
  asc = eval('0x' + asc)
  flag = asc & KEYS[i]
  row_list.append(flag and ft or ff)
 
 def render_font_rect(self, rect_list=None):
 if not rect_list:
 rect_list = self.rect_list
 
 for row in rect_list:
 for i in row:
 if i:
  print '■',
 else:
  print '○',
 print
 
 def convert(self, text, ft=None, ff=None, encoding='utf-8'):
 if not isinstance(text, unicode):
 text = text.decode(encoding)
 
 for t in text:
 area, index = self.get_font_area_index(t)
 font_rect = self.get_font_rect(area, index)
 
 self.convert_font_rect(font_rect, ft=ft, ff=ff)
 
 def get_rect_info(self):
 return self.rect_list
 
if '__main__' == __name__:
 text = u'同创伟业'
 fr = FontRender('./font/16x16/hzk16h')
 fr.convert(text, ft='/static/*', ff=0)
 # print fr.get_rect_info()
 fr.render_font_rect()

到此这篇关于使用Python怎么对点阵字体进行读取的文章就介绍到这了,更多相关的内容请搜索亿速云以前的文章或继续浏览下面的相关文章希望大家以后多多支持亿速云!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI