PaoyungJun 17, 2022
ℳ𝒾𝒸𝓇ℴ𝒫𝓎𝓉𝒽ℴ𝓃 隨手記Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
相關主題 👉 微控制器上字庫的選擇
從沒想過自己會和字庫這個主題糾纏這麼久呀!🤷♂️
自前陣子接觸到適合 MicroPython 使用的字型後,陸陸續續做了幾番測試,也查詢到一些資料,例如黑大就寫了不少篇與字型相關的文章,而在「閒聊中文點陣字型授權-黑暗執行緒」的文章中更提到了授權這個頭疼的問題,讓我意會到可使用於商業用途的重要性,然後找啊找就發現了Cubic 俐方體,它允許商業或非商業性質修改及使用,雖然只有 11x11,但放在 SSD1306 這類的小螢幕上還滿搭的,相較於 15x16 的字體可以顯示更多的內容,或許字體稱不上美觀,甚至要用呆萌來形容,但重點是不用再擔心授權的因素而踩到雷,於是馬上就在 github 向作者投上我的星星
作者提供的字庫檔格式為 TTF,字量為 10216 字,相對於陳大 ming.py
有 28922 字來說小得多,按作者提供的收錄字數數據已包含了常用國字標準字體 4808 字和 Big5 第一字面 5401 字,這足以應付一般的應用了。而若要像陳大 ming.py
那樣便於 MicroPython 調用的話,就要把 TTF 格式轉為 Python 的 dict 資料格式,對於字型格式的轉換我並不擅長,但先讓它把字型畫在畫布上再截取 bitmap 倒是滿簡單的,使用 Python 的 PIL 模組跑一下子就可以得到 dict 格式,簡單粗暴的程式碼就不獻醜了。不過比較特殊之處在於它的規格雖然是 11x11,在中文這類方正的格局時沒有問題,但在英文和符號時並非等寬,且 gjpqy 等這些下盤較低的英文字母,考慮到上方留白則高度可達 14 pixel 才能包含整個點陣的範圍,轉換時必需留意處理才不會出現下盤不見的情況。
而按照使用條款,我將其轉換為 Python dict 格式,為其原著作的衍生品即不可再用 cubic 或俐方體之名稱,故我將之取名為「cubicpy」,所以後續的文章和程式碼裡會以 cubicpy 稱之,而提供下載的檔案中也會隨附 SIL Open Font Licese 來發行。
cubicpy.zip 下載 (含 SIL Open Font License 1.1 文檔)
俐方體11號 10216 字清單
在前幾篇字庫的文章中已經有詳細的說明,如有需要可參考之前的內容:
👉 ① 陳大的 ming.py 試玩筆記 ② 陳大的 ming.py 試完筆記 ③ 陳大 ming.py ESP32 字量極限
# name: cubicpy_fetch.py, version: 0.1.0, author: paoyung.chang@gmail.com
# Copyright (c) 2022 Paoyung Chang. See the file LICENSE for copying permission.
# License link: https://gist.github.com/paoyung/7e465ad984a6cf24024508831ec54516
from cubicpy import chinese
def fetch(word_list, output_file):
# collect
words = {}
exclude = []
for w in word_list:
try:
words[w] = chinese[w]
except:
exclude.append(w)
if len(exclude):
print('本字庫不包含:', exclude)
# save file
try:
with open(output_file, 'w') as f:
f.write('words=')
f.write(str(words))
except:
print(f'{output_file} 建立或寫入發生異常!')
else:
print(f'{output_file} 建立完成!')
## example:
## fetch(word_list, output_filename)
# >>> word_list = 'Hello,大家好。向各位介紹免費、開源可商用自由改造及衍生的中文點陣字體 -『俐方體11號』'
# >>> fetch(word_list, 'my_font.py')
# name: cubicpy_demo.py, version: 0.1.0, author: paoyung.chang@gmail.com
# Copyright (c) 2022 Paoyung Chang. See the file LICENSE for copying permission.
# License link: https://gist.github.com/paoyung/7e465ad984a6cf24024508831ec54516
from framebuf import FrameBuffer, MONO_VLSB
from my_font import words
from my_util import get_display #自訂的 ssd1306 設定 function
def write_font(display, sentense, x, y, invert=False):
wordsBuf = [words[each] for each in sentense]
for buf in wordsBuf:
if invert: # 反白
buf = [b^0xff for b in buf]
width = len(buf) // 2 #判斷字寬
fbuf = FrameBuffer(bytearray(buf), width, 16, MONO_VLSB)
display.blit(fbuf, x, y, 0)
x += width
if x > 128:
break
def demo():
# 啟用 SSD1306
disp = get_display()
disp.invert(0)
disp.rotate(True)
# 顯示
write_font(disp, 'Hello,大家好。', 0, 0)
write_font(disp, '向各位介紹免費、開源', 0, 13)
write_font(disp, '可商用自由改造及衍生', 0, 25)
write_font(disp, '的中文點陣字體 --', 0, 37)
write_font(disp, '『俐方體11號』', 20, 51)
disp.show()
字型字庫的學問很多,字寬、字高、間隔、起始位置及如何節省空間都是必須納入考量的細節,初次轉換完成的字庫檔可能還有不足之處需要優化或人工介入微調,但看著字檔和程式能順利的在 MicroPython 中調用還是有幾分喜悅的,目前已在 github 上開源,歡迎 fork 及使用。
Paoyung ChangSun, Jun 19, 2022 1:54 PM
github: 俐方體11號的衍生字庫檔 for MicroPythonImage Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
相關主題 👉 微控制器上字庫的選擇