12864液晶屏定制版字模生成程序

在之前这片短文用Ruby和HZK16字库进行字模提取中提到了如何使用HZK16字库提取逐行扫描的字模。

但是12864这块液晶比较奇怪,是逐列扫描的,所以用普通的字模提取软件提取出来的字模是不行的。

本着没事干写代码权当消遣的想法,用Ruby写了下12864液晶屏特供版的字模提取程序。代码的前半部分和之前用的一样,先提取出来正常的字模。然后用最傻大黑粗的方法直接将16x16的字点阵的数据存到一个16x16的二维数组里面,然后在用逐列输出的方法输出一下即可。如果再碰到其他的奇怪的液晶,也可以采用这种傻大黑粗的方法,虽然不太文雅,不过代码写起来也不用怎么想,抬手就能写,一会就能写完。

废话不多说,代码见下吧。

print "input the Chinese character:"
cstr = gets
str = cstr.encode("gb2312")
arr = []
code = []

str.each_byte {|b| arr<< b}
offset = (arr[0]*94+arr[1]-15295)*32
print "offset in the HZK16 fonts file:"
puts offset

f = File.new("HZK16")
f.seek(offset)
print "file pointer position:"
puts f.pos

if offset==f.pos
puts "file pointer is at the right place!"
end
puts "----------------------------------------"

puts "traditional fonts array:"
for i in 0..31
	code[i] = f.getbyte		#code[i] type: string
	#print code[i].to_s(16)
end
print "{"
for i in 0..30
	if code[i] <= 0x0F
		print "0x0"+code[i].to_s(16)+","
	else
		print "0x"+code[i].to_s(16)+","
	end
end
if code[31] <= 0x0F
	print "0x0"+code[31].to_s(16)
else
	print "0x"+code[31].to_s(16)
end
puts "}"
puts "----------------------------------------"
f.close

#end of original fonts.rb

puts "data matrix (space for 0, # for 1):"
dataMatrix = Array.new(16) { Array.new(16) }
j=0
for i in 0..31
	if (i%2)==0
		for p in 0..7
			dataMatrix[j][p] = (code[i]&(1<<(7-p)))>>(7-p)
		end
	else
		for p in 8..15
			dataMatrix[j][p] = (code[i]&(1<<(15-p)))>>(15-p)
		end
		j+=1
	end
end

for i in 0..15
	for j in 0..15
		if dataMatrix[i][j]==1
			print "# "
		else
			print "  "
		end
	end
	puts ""
end
puts "----------------------------------------"

out = []
for i in 0..31
	out[i]=0
	if i<=15
		for p in 0..7
			out[i] += (dataMatrix[p][i]<<p)
		end
	else
		j = i-16
		for p in 8..15
			out[i] += (dataMatrix[p][j]<<(p-8))
		end
	end
end

f = File.open("fonts output.txt","w+")
puts "fonts in 12864:"
print "{"
f.write("{")
for i in 0..30
	if out[i] <= 0x0F
		print "0x0"+out[i].to_s(16)+","
		f.write("0x0"+out[i].to_s(16)+",")
	else
		print "0x"+out[i].to_s(16)+","
		f.write("0x"+out[i].to_s(16)+",")
	end
end
if out[31] <= 0x0F
	print "0x0"+out[31].to_s(16)
	f.write("0x0"+out[31].to_s(16))
else
	print "0x"+out[31].to_s(16)
	f.write("0x"+out[31].to_s(16))
end
puts "}"
f.write("}")
f.close

puts "press enter to quit:"
while true
	edstr = gets
	if edstr == "n"
		break
	end
end

另外最后写完尝试使用了一下ocra,编译出来了一个exe文件,HZK16也被编译在了里面。妈妈不用再担心字库丢了的问题了!携带更方便!