原文来自 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
暂无关于此日志的评论。