python字体描边_使用 python 将文泉驿字体导出为 fnt 格式的bitmap font
原文來自 BITCA.CN 的【關于低分辨率像素游戲下顯示非防鋸齒中文 / 漢字的研究】文章
廢話少說,直接上代碼 調皮的我,被你發現了 ^_^
# -*- coding: UTF-8 -*-
#coding=utf-8
import pygame # Pygame 游戲模塊
from pygame import freetype # 處理矢量字庫的 Pygame 模塊
import codecs # 處理 unicode 所需模塊
import json # 輸出 json 格式 所需模塊
# 保存的文件名稱
saveImgFileName = "pixel-hz.png"
saveCharsetFileName = "charset.txt"
saveFntFileName = "pix-font.fnt"
saveConstructFileName = "construct-spriteFont-spaceData.json"
# 等寬部分的字符表
fixWCharset = codecs.open("chinese-words.txt", "r", "UTF-8").read() # 讀取3500個常用漢字的表
fixWCharset = fixWCharset + u"哦罹" # 加入常用字中沒有的字
# 需要記錄寬度信息的字符表
varWCharset = codecs.open("ascii.txt", "r", "UTF-8").read() # 加入常用的 ascii 字符 表
varWCharset = varWCharset + u",。;“”、:?《》" # 加入拳腳的漢字標點
gridW = 14 # 每個字符輸出區域的寬度
gridH = 14 # 每個字符輸出區域的高度
outColNum = 90 # 每行輸出的字符數
outRowNum = 42 # 一共輸出的行數
textureW = gridW * outColNum # 最終輸出的貼圖寬度
textureH = gridH * outRowNum # 最終輸出的貼圖高度
pygame.init() # 初始化游戲引擎
pygame.display.set_caption("像素點陣漢字生成") # 窗口的標題
screen = pygame.display.set_mode((textureW, textureH)) # 打開的窗口大小
buffer = pygame.Surface((textureW, textureH), pygame.SRCALPHA) # 建立一個透明貼圖大小的緩沖區,貼圖先
# 因為非等寬字體還要需要處理基線的問題,所以同一個字體載入到兩個變量之中,可以進行不同的設置
fixWFont = pygame.freetype.Font('wqy-bitmapsong/wenquanyi_9pt.pcf') # 等寬字符所用字體
varWFont = pygame.freetype.Font('wqy-bitmapsong/wenquanyi_9pt.pcf') # 非等寬字體所用字體
# 關掉防鋸齒
fixWFont.antialiased = False
varWFont.antialiased = False
varWFont.origin = True # 使用基線方式渲染字體
varWFontSize = 12 # 非等寬字體的固定輸出為 12 像素
baseLine = 10 # 設定從頂部往下 10 個像素為基線
x = 0 # 字符輸出的行坐標
y = 0 # 字符輸出的列坐標
fontColor = ( 255,255,255 ) # 字體顏色
outlineColor = ( 0,0,0 ) # 描邊顏色
charList = {} # 記錄所有字符的字典
def addCharObj(code, x, y, xadvance):
charList[code] = {
"x": x,
"y": y,
"xadvance": xadvance
}
for i in range( 0, len(fixWCharset) ): # 遍歷常用漢字表
fx = x * gridW # 字符輸出的像素坐標 x
fy = y * gridH # 字符輸出的像素坐標 y
char = fixWCharset[i]
# 渲染字符描邊
fixWFont.render_to( buffer, (fx+1, fy+0), char, outlineColor )
fixWFont.render_to( buffer, (fx+1, fy+2), char, outlineColor )
fixWFont.render_to( buffer, (fx+0, fy+1), char, outlineColor )
fixWFont.render_to( buffer, (fx+2, fy+1), char, outlineColor )
# 渲染字符
fixWFont.render_to( buffer, (fx+1, fy+1), char, fontColor )
# 行列遞增
x = x + 1
if (x>=outColNum):
x = 0
y = y + 1
addCharObj(ord(char), fx, fy, gridW)
widthDict = {} # 記錄寬度的字典
for enIndex in range(0, len(varWCharset)):
fx = x * gridW # 字符輸出的像素坐標 x
fy = y * gridH # 字符輸出的像素坐標 y
char = varWCharset[enIndex]
# 渲染字符描邊
varWFont.render_to( buffer, (fx+1, baseLine+fy+0), char, outlineColor, size=varWFontSize )
varWFont.render_to( buffer, (fx+0, baseLine+fy+1), char, outlineColor, size=varWFontSize )
varWFont.render_to( buffer, (fx+2, baseLine+fy+1), char, outlineColor, size=varWFontSize )
varWFont.render_to( buffer, (fx+1, baseLine+fy+2), char, outlineColor, size=varWFontSize )
# 渲染字符
varWFont.render_to( buffer, (fx+1, baseLine + fy+1), char, fontColor, size=varWFontSize )
# 記錄字符寬度
m = varWFont.get_metrics( char, size=varWFontSize )
lineX = fx + m[0][1]
charW = m[0][1] + 3
if not charW in widthDict : widthDict[charW] = []
widthDict[charW].append( char )
# 行列遞增
x = x + 1
if ( x >= outColNum ):
x = 0
y = y + 1
addCharObj(ord(char), fx, fy, charW)
# 輸出 construct 3 所需的寬度 json 文件
outputList = []
for wKey in widthDict:
charStr = ""
for char in widthDict[wKey] : charStr = charStr + char
outputList.append( [wKey,charStr] )
outJson = json.dumps( outputList )
print( "Json String For Construct : " )
print( outJson )
file = open( saveConstructFileName, "w" )
file.write( outJson )
file.close()
print( "Construct saved to : " + saveConstructFileName )
# 輸出 fnt 文件
outputList = ""
for code in charList:
charStr = ""
info = charList[code]
# for char in charList[code] : charStr = charStr + char
header = 'info face={face} size={size} bold={bold} italic={italic} charset={charset} unicode={unicode} stretchH={stretchH} smooth={smooth} aa={aa} padding={padding} spacing={spacing} outline={outline}\ncommon lineHeight={lineHeight} base={base} scaleW={scaleW} scaleH={scaleH} pages={pages} packed=0\npage id=0 file="{file}"\nchars count={count}\n'.format(
face="pixfont",
size=14,
bold=0,
italic=0,
charset="",
unicode="",
stretchH=100,
smooth=0,
aa=0,
padding="0,0,0,0",
spacing="0,0",
outline=0,
lineHeight=14,
base=baseLine,
scaleW=textureW,
scaleH=textureH,
pages=1,
file=saveImgFileName,
count=len(charList),
)
outputList += "char id={0} x={1} y={2} width={3} height={4} xoffset={5} yoffset={6} xadvance={7} page={8} chnl={9}\n".format(
code, info["x"], info["y"], gridW, gridH, 0, 0, info["xadvance"], 0, 15
)
outJson = header + outputList#json.dumps( outputList )
print( "Json String For fnt : " )
print( outJson )
file = open( saveFntFileName, "w" )
file.write( outJson )
file.close()
print( "fnt saved to : " + saveFntFileName )
# 輸出整體字符集文件
charset = fixWCharset + varWCharset
file = codecs.open( saveCharsetFileName, "w", "utf-8" )
file.write( charset )
file.close()
print( "charset saved to : " + saveCharsetFileName )
# 保存貼圖文件
pygame.image.save( buffer, saveImgFileName )
print( "texture saved to : " + saveImgFileName )
# 主循環
running = True
while running:
# 在窗口中顯示貼圖
screen.blit( buffer, (0, 0) )
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
總結
以上是生活随笔為你收集整理的python字体描边_使用 python 将文泉驿字体导出为 fnt 格式的bitmap font的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深度学习——模型的压缩和加速
- 下一篇: 验证python安装_Python环境搭