Graphic OLED 100x16 pixels interface

Dependents:   mbed_nicovideo_search_api mbed_recent_nicovideo_display_pub

misaki2cpp

美咲フォントからC++コードの作成プログラム。

/media/uploads/va009039/misaki2cpp.py

misaki2cpp.py

#coding: UTF-8
# misaki2cpp.py 2014/8/5

def r90(bitmap): # 右に90度回転
    bitmap2 = [0x00] * 8
    mask2 = 0x01
    for bit in bitmap:
        mask = 0x80
        for n in range(8):
            if bit & (mask>>n):
                bitmap2[n] |= mask2
        mask2 <<= 1
    return bitmap2

def cpp_bitmap(encoding, bitmap):
    bitmap_r90 = r90(bitmap)
    s = "{0x%04x,{" % encoding
    for i in range(7):
        s += "0x%02x" % bitmap[i]
        if i <= 5:
            s += ","
    s += "}},"
    utf8_str = unichr(encoding).encode('utf-8')
    s += " // "
    for b in utf8_str:
        s += "\\x%02x" % ord(b)
    return s

def view_bitmap(bitmap):
    s = ""
    for bit in bitmap:
        s += "// "
        for x in range(8):
            if (bit<<x) & 0x80:
                s += u"■"
            else:
                s += u"□"
        s += "\n"
    return s

def get_bdf(f):
    bitmap_start = False
    chars_count = 0
    while 1:
        line = f.readline()
        if line == None:
            break
        if chars_count == 0: print "// " + line,
        s = line.split()
        if s[0] == "CHARS":
            chars = int(s[1])
        elif s[0] == "ENDFONT":
            assert chars == chars_count
            break
        elif s[0] == "ENCODING":
            encoding = int(s[1])
        elif s[0] == "BITMAP":
            bitmap_start = True
            bitmap = []
        elif s[0] == "ENDCHAR":
            chars_count += 1
            assert len(bitmap) == 8
            yield (encoding, bitmap)
            bitmap_start = False
        elif bitmap_start:
            bitmap.append(int(s[0], 16))
    return

def hexstr(str):
    s = "len=%d " % len(str)
    for c in str:
        s += "[%02x]" % ord(c)
    return s

if __name__ == "__main__":
    import argparse
    import tarfile

    parser = argparse.ArgumentParser()
    parser.add_argument('-f', dest='filename', nargs=1, default="misaki_bdf_2012-06-03.tar.gz")
    parser.add_argument('-e', dest='extract', nargs=1, default="misaki_gothic.bdf")
    parser.add_argument('-l', dest='list', action='store_true')
    args = parser.parse_args()

    fh = tarfile.open(args.filename, 'r')
    if args.list:
        fh.list()
    f = fh.extractfile(args.extract)

    bitmap_by_unicode = {}
    for (encoding,bitmap) in get_bdf(f):
        jis_str = "\x1b$B" + chr(encoding>>8) + chr(encoding & 255) + "\x1b(B"
        try:
            uni_str = unicode(jis_str, 'iso2022_jp')
        except UnicodeDecodeError: # 機種依存コード
            pass
        else:
            assert uni_str.encode('iso2022_jp') == jis_str
            utf8_str = uni_str.encode('utf-8')
            assert not ord(uni_str) in bitmap_by_unicode
            bitmap_by_unicode[ord(uni_str)] = bitmap

    unicode_lists = sorted(bitmap_by_unicode)

    print """\
#include "GraphicOLED.h"

struct stfont {
    uint16_t unicode;
    uint8_t bitmap[8];
};

static struct stfont misaki_gothic[] = {"""

    for k in unicode_lists:
        bitmap = bitmap_by_unicode[k]
        print cpp_bitmap(k, r90(bitmap))

    print """\
};"""


All wikipages