https://www.gys.cn/buy/5420348615.html
字体是由坐标画出来的,关键词:rmoveto, rlineto, rrcurveto等
""""
<CharString name="uni100d1">
126 15 -1 rmoveto
0 0 rlineto
-2 7 rlineto
43 15 25 25 7 35 rrcurveto
0 0 rlineto
-11 -11 -12 -6 -13 0 rrcurveto
0 0 rlineto
-27 3 -15 18 -2 33 rrcurveto
0 0 rlineto
1 41 17 21 33 1 rrcurveto
0 0 rlineto
35 -1 18 -25 1 -49 rrcurveto
0 0 rlineto
0 -59 -33 -36 -66 -12 rrcurveto
75 95 rmoveto
0 0 rlineto
2 53 -10 26 -22 -1 rrcurveto
0 0 rlineto
-19 0 -9 -16 0 -32 rrcurveto
0 0 rlineto
-1 -33 10 -16 21 1 rrcurveto
0 0 rlineto
11 1 9 6 8 11 rrcurveto
endchar
</CharString>
"""
import ddddocr
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches
# 定义路径命令
commands = [
("rmoveto", 126, 15, -1),
("rlineto", 0, 0),
("rlineto", -2, 7),
("rrcurveto", 43, 15, 25, 25, 7, 35),
("rlineto", 0, 0),
("rrcurveto", -11, -11, -12, -6, -13, 0),
("rlineto", 0, 0),
("rrcurveto", -27, 3, -15, 18, -2, 33),
("rlineto", 0, 0),
("rrcurveto", 1, 41, 17, 21, 33, 1),
("rlineto", 0, 0),
("rrcurveto", 35, -1, 18, -25, 1, -49),
("rlineto", 0, 0),
("rrcurveto", 0, -59, -33, -36, -66, -12),
("rmoveto", 75, 95),
("rlineto", 0, 0),
("rrcurveto", 2, 53, -10, 26, -22, -1),
("rlineto", 0, 0),
("rrcurveto", -19, 0, -9, -16, 0, -32),
("rlineto", 0, 0),
("rrcurveto", -1, -33, 10, -16, 21, 1),
("rlineto", 0, 0),
("rrcurveto", 11, 1, 9, 6, 8, 11),
("endchar",),
]
# 初始化起始点
x, y = 0, 0
# 创建路径数据
path_data = []
for command in commands:
if command[0] == "rmoveto":
x += command[1]
y += command[2]
path_data.append((Path.MOVETO, (x, y)))
elif command[0] == "rlineto":
x += command[1]
y += command[2]
path_data.append((Path.LINETO, (x, y)))
elif command[0] == "rrcurveto":
x1, y1 = x + command[1], y + command[2]
x2, y2 = x1 + command[3], y1 + command[4]
x3, y3 = x2 + command[5], y2 + command[6]
path_data.append((Path.CURVE4, (x1, y1)))
path_data.append((Path.CURVE4, (x2, y2)))
path_data.append((Path.CURVE4, (x3, y3)))
x, y = x3, y3
# 创建路径
codes, verts = zip(*path_data)
path = Path(verts, codes)
# 创建图形
fig, ax = plt.subplots()
patch = patches.PathPatch(path, facecolor="black", lw=2) # 使用黑色填充
ax.add_patch(patch)
ax.set_aspect("equal")
ax.autoscale()
# 保存图形
plt.savefig("charstring.png")
# 使用 ddddocr 识别图像中的字符
ocr = ddddocr.DdddOcr()
with open("charstring.png", "rb") as f:
img_bytes = f.read()
result = ocr.classification(img_bytes)
print("识别结果:", result)
画出来的结果:
ddddocr识别的结果:
完整代码示例:
import io
import ddddocr
import base64
from fontTools.ttLib import TTFont
from fontTools.pens.basePen import BasePen
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches
ocr = ddddocr.DdddOcr()
class MatplotlibPen(BasePen):
def __init__(self, glyphSet):
super().__init__(glyphSet)
self.path_data = []
def _moveTo(self, p0):
self.path_data.append((Path.MOVETO, p0))
def _lineTo(self, p1):
self.path_data.append((Path.LINETO, p1))
def _curveToOne(self, p1, p2, p3):
self.path_data.append((Path.CURVE4, p1))
self.path_data.append((Path.CURVE4, p2))
self.path_data.append((Path.CURVE4, p3))
def _closePath(self):
self.path_data.append((Path.CLOSEPOLY, self.path_data[0][1]))
def get_font_data(font_str):
"""获取解密字体"""
font_data = base64.b64decode(font_str)
font = TTFont(io.BytesIO(font_data))
font.saveXML("font.xml")
result = {}
# 获取字符的路径数据
glyph_set = font.getGlyphSet()
glyph_names = font.getGlyphNames()
for glyph_name in glyph_names:
glyph = glyph_set[glyph_name]
# 使用 MatplotlibPen 解析路径数据
pen = MatplotlibPen(glyph_set)
glyph.draw(pen)
# 检查 path_data 是否为空
if not pen.path_data:
continue
# 创建路径
codes, verts = zip(*pen.path_data)
path = Path(verts, codes)
# 创建图形
fig, ax = plt.subplots()
patch = patches.PathPatch(path, facecolor="black", lw=2) # 使用黑色填充
ax.add_patch(patch)
ax.set_aspect("equal")
ax.autoscale()
# 保存图形
plt.savefig("charstring.png")
# 使用 ddddocr 识别图像中的字符
with open("charstring.png", "rb") as fr:
img_bytes = fr.read()
ret = ocr.classification(img_bytes)
print("识别结果:", glyph_name, ret)
result[glyph_name] = ret.upper()
print(result)
return result
if __name__ == "__main__":
font_str = "T1RUTwAJAIAAAwAQQ0ZGIIpi0aIAAATIAAADjk9TLzJlk+AaAAABAAAAAGBjbWFwACgMUgAABAQAAACkaGVhZCZ5+TwAAACcAAAANmhoZWEA3wBMAAAA1AAAACRobXR4B3IAAAAACFgAAAAgbWF4cAAIUAAAAAD4AAAABm5hbWUKXYQxAAABYAAAAqNwb3N0AAMAAAAABKgAAAAgAAEAAAABAACUNrWcXw889QADAQAAAAAA43NdSAAAAADjc11IAAT//wB4ALUAAAADAAIAAAAAAAAAAQAAAN3/xQAAAH4AAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAgAAFAAAAgAAAADAH4B9AAFAAACigK7AAAAjAKKArsAAAHfADEBAgAAAAAAAAAAAAAAAIAAAAEAAAAAAAAAAAAAAABYWFhYAEAAywDRAN3/xQAAALUAAQAAAAEAAAAAAG8AtQAAAAAAAAAAACIBngABAAAAAAAAAAEAQgABAAAAAAABAAwAAAABAAAAAAACAAYAJAABAAAAAAADABUAxgABAAAAAAAEABMANgABAAAAAAAFAAsApQABAAAAAAAGABIAbwABAAAAAAAHAAEAQgABAAAAAAAIAAEAQgABAAAAAAAJAAEAQgABAAAAAAAKAAEAQgABAAAAAAALAAEAQgABAAAAAAAMAAEAQgABAAAAAAANAAEAQgABAAAAAAAOAAEAQgABAAAAAAAQAAwAAAABAAAAAAARAAYAJAADAAEECQAAAAIAYQ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𐃏𐃏𐃐𐃐𐃑"
txt = "𐃋𐃌𐃍𐃋𐃎𐃏𐃏𐃏𐃐𐃐𐃑"
for key, value in result.items():
key = key.replace("uni", "&#x").lower() + ";"
txt = txt.replace(key, value)
print(txt)
输出结果: