日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python获取中文字体点阵坐标_Python实现点阵字体读取与转换

發(fā)布時間:2023/12/8 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python获取中文字体点阵坐标_Python实现点阵字体读取与转换 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

點陣字體是指根據(jù)文字的像素點來顯示的字體,效果如下:

使用Python讀取并顯示的過程如下:

根據(jù)中文字符獲取GB2312編碼

通過GB2312編碼計算該漢字在點陣字庫中的區(qū)位和碼位

通過區(qū)位和碼位計算在點陣字庫中的偏移量

基于偏移量獲取該漢字的32個像素存儲字節(jié)

解析像素字節(jié)獲取點陣坐標信息

在對應的坐標顯示信息位。如該像素點是否顯示點亮

使用該代碼前提:下載點陣字體庫到本地,這里默認使用的是hzk16點陣字庫

字體庫下載地址:

代碼如下:

#!/usr/bin/python

#encoding: utf-8

import binascii

BYTE_COUNT_PER_FONT = 32

BYTE_COUNT_PER_ROW = 2

RECT_HEIGHT = 16

RECT_WIDTH = 16

KEYS = [0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01]

class FontRender(object):

def __init__(self, font_file="hzk16s",

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(32)

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*2+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()

fr.convert(text, ft='/static/*', ff=0)

# print fr.get_rect_info()

fr.render_font_rect()

總結(jié)

以上是生活随笔為你收集整理的python获取中文字体点阵坐标_Python实现点阵字体读取与转换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。