Skip to content

Commit 1b7f405

Browse files
committed
fix: format
1 parent b87c77d commit 1b7f405

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

.github/workflows/python-build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
- name: Check code format
2626
run: |
2727
black --check --diff --color pdf2zh/*.py
28-
flake8
28+
flake8 --ignore E203,E261,E501,W503,E741
2929
3030
- name: Build package
3131
run: python -m build

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ repos:
1010
language: python
1111
- id: flake8
1212
name: flake8
13-
entry: flake8
13+
entry: flake8 --ignore E203,E261,E501,W503,E741
1414
language: python

pdf2zh/converter.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ def vflag(font, char): # 匹配公式(和角标)字体
511511
pstk[-1][6] = True
512512
else: # 根据当前字符构建一个新的段落
513513
sstk.append("")
514-
pstk.append([child.y0,child.x0,child.x0,child.x0,child.size,child.font,False,])
514+
pstk.append([child.y0, child.x0, child.x0, child.x0, child.size, child.font, False])
515515
if not cur_v: # 文字入栈
516516
if ( # 根据当前字符修正段落属性
517517
child.size > pstk[-1][4] / 0.79 # 1. 当前字符显著比段落字体大
@@ -565,7 +565,7 @@ def vflag(font, char): # 匹配公式(和角标)字体
565565
varf.append(vfix)
566566
log.debug("\n==========[VSTACK]==========\n")
567567
for id, v in enumerate(var): # 计算公式宽度
568-
l = max([vch.x1 for vch in v]) - v[0].x0 # noqa: E741
568+
l = max([vch.x1 for vch in v]) - v[0].x0
569569
log.debug(f'< {l:.1f} {v[0].x0:.1f} {v[0].y0:.1f} {v[0].cid} {v[0].fontname} {len(varl[id])} > $v{id}$ = {"".join([ch.get_text() for ch in v])}')
570570
vlen.append(l)
571571

@@ -574,6 +574,7 @@ def vflag(font, char): # 匹配公式(和角标)字体
574574
log.debug("\n==========[SSTACK]==========\n")
575575
hash_key = cache.deterministic_hash("PDFMathTranslate")
576576
cache.create_cache(hash_key)
577+
577578
@retry(wait=wait_fixed(1))
578579
def worker(s): # 多线程翻译
579580
try:
@@ -604,6 +605,7 @@ def raw_string(fcur, cstk): # 编码字符串
604605
return "".join(["%04x" % ord(c) for c in cstk])
605606
else:
606607
return "".join(["%02x" % ord(c) for c in cstk])
608+
607609
_x, _y = 0, 0
608610
for id, new in enumerate(news):
609611
tx = x = pstk[id][1] # 段落初始横坐标
@@ -633,9 +635,7 @@ def raw_string(fcur, cstk): # 编码字符串
633635
adv = vlen[vid]
634636
except Exception:
635637
continue # 翻译器可能会自动补个越界的公式标记
636-
if len(var[vid]) == 1 and unicodedata.category(
637-
var[vid][0].get_text()[0]
638-
) in ["Lm","Mn","Sk",]: # 文字修饰符
638+
if len(var[vid]) == 1 and unicodedata.category(var[vid][0].get_text()[0]) in ["Lm", "Mn", "Sk"]: # 文字修饰符
639639
mod = True
640640
else: # 加载文字
641641
ch = new[ptr]
@@ -667,21 +667,21 @@ def raw_string(fcur, cstk): # 编码字符串
667667
cstk = ""
668668
if lb and x + adv > rt + 0.1 * size: # 到达右边界且原文段落存在换行
669669
x = lt
670-
lang_space = {"zh-CN": 1.4,"zh-TW": 1.4,"ja": 1.1,"ko": 1.2,"en": 1.2} # CJK
670+
lang_space = {"zh-CN": 1.4, "zh-TW": 1.4, "ja": 1.1, "ko": 1.2, "en": 1.2} # CJK
671671
y -= size * lang_space.get(self.translator.lang_out, 1.1) # 小语种大多适配 1.1
672672
if vy_regex: # 插入公式
673673
fix = 0
674674
if fcur is not None: # 段落内公式修正纵向偏移
675675
fix = varf[vid]
676676
for vch in var[vid]: # 排版公式字符
677677
vc = chr(vch.cid)
678-
ops += f"/{self.fontid[vch.font]} {vch.size:f} Tf 1 0 0 1 {x + vch.x0 - var[vid][0].x0:f} {fix + y + vch.y0 - var[vid][0].y0:f} Tm [<{raw_string(self.fontid[vch.font], vc)}>] TJ " # noqa: E501
678+
ops += f"/{self.fontid[vch.font]} {vch.size:f} Tf 1 0 0 1 {x + vch.x0 - var[vid][0].x0:f} {fix + y + vch.y0 - var[vid][0].y0:f} Tm [<{raw_string(self.fontid[vch.font], vc)}>] TJ "
679679
if log.isEnabledFor(logging.DEBUG):
680-
lstk.append(LTLine(0.1,(_x, _y),(x + vch.x0 - var[vid][0].x0,fix + y + vch.y0 - var[vid][0].y0,)))
680+
lstk.append(LTLine(0.1, (_x, _y), (x + vch.x0 - var[vid][0].x0, fix + y + vch.y0 - var[vid][0].y0)))
681681
_x, _y = x + vch.x0 - var[vid][0].x0, fix + y + vch.y0 - var[vid][0].y0
682-
for l in varl[vid]: # 排版公式线条 # noqa: E741
682+
for l in varl[vid]: # 排版公式线条
683683
if l.linewidth < 5: # hack 有的文档会用粗线条当图片背景
684-
ops += f"ET q 1 0 0 1 {l.pts[0][0] + x - var[vid][0].x0:f} {l.pts[0][1] + fix + y - var[vid][0].y0:f} cm [] 0 d 0 J {l.linewidth:f} w 0 0 m {l.pts[1][0] - l.pts[0][0]:f} {l.pts[1][1] - l.pts[0][1]:f} l S Q BT " # noqa: E501
684+
ops += f"ET q 1 0 0 1 {l.pts[0][0] + x - var[vid][0].x0:f} {l.pts[0][1] + fix + y - var[vid][0].y0:f} cm [] 0 d 0 J {l.linewidth:f} w 0 0 m {l.pts[1][0] - l.pts[0][0]:f} {l.pts[1][1] - l.pts[0][1]:f} l S Q BT "
685685
else: # 插入文字缓冲区
686686
if not cstk: # 单行开头
687687
tx = x
@@ -698,9 +698,9 @@ def raw_string(fcur, cstk): # 编码字符串
698698
if log.isEnabledFor(logging.DEBUG):
699699
lstk.append(LTLine(0.1, (_x, _y), (x, y)))
700700
_x, _y = x, y
701-
for l in lstk: # 排版全局线条 # noqa: E741
701+
for l in lstk: # 排版全局线条
702702
if l.linewidth < 5: # hack 有的文档会用粗线条当图片背景
703-
ops += f"ET q 1 0 0 1 {l.pts[0][0]:f} {l.pts[0][1]:f} cm [] 0 d 0 J {l.linewidth:f} w 0 0 m {l.pts[1][0] - l.pts[0][0]:f} {l.pts[1][1] - l.pts[0][1]:f} l S Q BT " # noqa: E501
703+
ops += f"ET q 1 0 0 1 {l.pts[0][0]:f} {l.pts[0][1]:f} cm [] 0 d 0 J {l.linewidth:f} w 0 0 m {l.pts[1][0] - l.pts[0][0]:f} {l.pts[1][1] - l.pts[0][1]:f} l S Q BT "
704704
ops = f"BT {ops}ET "
705705
return ops
706706

0 commit comments

Comments
 (0)