Skip to content

Commit

Permalink
feat(fontmap): add progress monitoring and stage name for font mapping
Browse files Browse the repository at this point in the history
- introduce stage_name attribute in FontMapper
- add progress monitoring to track font insertion process
- update high_level.py to include FontMapper stage in progress monitor

refactor(fontmap): simplify font list creation

- streamline the creation of font list by removing unnecessary line breaks
  • Loading branch information
awwaawwa committed Jan 21, 2025
1 parent b8f096f commit f5e20d1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
22 changes: 13 additions & 9 deletions yadt/document_il/utils/fontmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@


class FontMapper:
stage_name = "添加字体"

def __init__(self, translation_config: TranslationConfig):
self.font_names = [
"source-han-serif-cn.ttf",
Expand Down Expand Up @@ -66,21 +68,23 @@ def add_font(self, doc_zh: pymupdf.Document, il: il_version_1.Document):
font_list.extend(
[
(
os.path.basename(file_name)
.split(".")[0]
.replace("-", "")
.lower(),
os.path.basename(file_name).split(".")[0].replace("-", "").lower(),
get_cache_file_path(file_name),
)
for file_name in self.font_names
]
)
font_id = {}
for i, page in enumerate(doc_zh):
if not self.translation_config.should_translate_page(i + 1):
continue
for font in font_list:
font_id[font[0]] = page.insert_font(font[0], font[1])
with self.translation_config.progress_monitor.stage_start(
self.stage_name, len(doc_zh)
) as pbar:
for i, page in enumerate(doc_zh):
if not self.translation_config.should_translate_page(i + 1):
pbar.advance()
continue
for font in font_list:
font_id[font[0]] = page.insert_font(font[0], font[1])
pbar.advance()
xreflen = doc_zh.xref_length()
for xref in range(1, xreflen):
for label in ["Resources/", ""]: # 可能是基于 xobj 的 res
Expand Down
3 changes: 2 additions & 1 deletion yadt/high_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from yadt.document_il.backend.pdf_creater import PDFCreater
from yadt.translation_config import TranslationConfig
from yadt.progress_monitor import ProgressMonitor

from yadt.document_il.utils.fontmap import FontMapper
import logging

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -159,6 +159,7 @@ def translate(translation_config: TranslationConfig):
StylesAndFormulas.stage_name,
ILTranslator.stage_name,
Typesetting.stage_name,
FontMapper.stage_name,
PDFCreater.stage_name,
],
) as pm:
Expand Down

0 comments on commit f5e20d1

Please # to comment.