import sys
import os
+class TtfError(Exception):
+ def __init__(self, val=""):
+ self.value = val
+ def __str__(self):
+ return repr(self.value)
+
class TtfReader:
def __init__(self, filehandle, offset=0):
self.fh = filehandle
def read_ttc_head(self):
self.fh.seek(self.offset, os.SEEK_SET)
if self.fh.read(8) != b"ttcf\x00\x01\x00\x00":
- raise "TTCファイルヘッダ読み込みエラー"
+ raise TtfError("TTCファイルヘッダ読み込みエラー")
self.tttable = []
numfn = struct.unpack(">L", self.fh.read(4))[0]
for i in range(numfn):
def read_ttf_head(self):
self.fh.seek(self.offset, os.SEEK_SET)
if self.fh.read(4) != b"\x00\x01\x00\x00":
- raise "TTFファイルヘッダ読み込みエラー"
+ raise TtfError("TTFファイルヘッダ読み込みエラー")
(num_tables, search_range, entry_selector, range_shift) \
= struct.unpack(">HHHH", self.fh.read(8))
self.table_list = {}
elif self.tables["head"]["indexToLocFormat"] == 1:
table_format = struct.Struct(">L")
else:
- raise "headテーブルのindexToLocFormat値が不正。"
+ raise TtfError("headテーブルのindexToLocFormat値が不正。")
self.tables["loca"] = []
for i in range(self.tables["maxp"]["numGlyphs"]+1):
offset = table_format.unpack(self.fh.read(table_format.size))
self.fh.seek(self.table_list["cmap"]["o"], os.SEEK_SET)
(version, num_tables) = struct.unpack(">HH", self.fh.read(4));
if version != 0:
- raise "cmapテーブルのバージョンがあたらしすぎまっす。"
+ raise TtfError("cmapテーブルのバージョンがあたらしすぎまっす。")
encoding_records = []
maxscore = 0
no = -1
"offset": offset
})
if no < 0:
- raise "文字マップのエンコード方式に非対応のものしかありません"
+ raise TtfError("文字マップのエンコード方式に非対応のものしかありません")
encoding = None
if encoding_records[no]["platformID"] == 3:
if encoding_records[no]["encodingID"] == 0:
if encoding_records[no]["encodingID"] == 2:
encoding = "sjis"
if encoding == None:
- raise "文字マップのエンコード方式に非対応のものしかありません"
+ raise TtfError("文字マップのエンコード方式に非対応のものしかありません")
self.fh.seek(self.table_list["cmap"]["o"]
+ encoding_records[no]["offset"], os.SEEK_SET)
maptype = struct.unpack(">H", self.fh.read(2))[0]
code = code.decode(encoding, "replace")
mapdict[code] = (glyph_index + delta) % 65536
else:
- raise "ID{}の文字マップには対応していません".format(maptype)
+ raise TtfError("ID{}の文字マップには対応していません".format(maptype))
self.tables["cmap"] = mapdict
return self.tables["cmap"]
self.tables["maxp"]["numGlyphs"] \
= struct.unpack(">S", self.fh.read(2))[0]
else:
- raise "maxpテーブルのバージョンが非対応なやつです"
+ raise TtfError("maxpテーブルのバージョンが非対応なやつです")
def get_table_hhea(self):
if not "hhea" in self.tables:
if not(font_can_HW and font_can_gothic and
font_can_mincho and font_can_marumoji):
print("適切なフォントをfonts/に入れてください。(README参照)")
- raise "必要なフォントを自動検出できんかったよ"
+ raise TtfError("必要なフォントを自動検出できんかったよ")
hwfont_reader = font_can_HW
fwfont_readers = [font_can_gothic, font_can_mincho, font_can_marumoji]
fwfont_replacers = {