Skip to content

Commit

Permalink
refactor: Improve PDF generation and progress monitoring
Browse files Browse the repository at this point in the history
- Update stage names in PDFCreater
- Adjust progress monitoring in PDF creation process
- Minor refinements to stage tracking and progress reporting
  • Loading branch information
awwaawwa committed Feb 18, 2025
1 parent 8f15746 commit 3c7d11a
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion babeldoc/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.8"
__version__ = "0.1.9"
18 changes: 15 additions & 3 deletions babeldoc/document_il/backend/pdf_creater.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@

logger = logging.getLogger(__name__)

SUBSET_FONT_STAGE_NAME = "Subset font"
SAVE_PDF_STAGE_NAME = "Save PDF"


class PDFCreater:
stage_name = "Create PDF file"
stage_name = "Generate drawing instructions"

def __init__(
self,
Expand Down Expand Up @@ -249,7 +252,7 @@ def write(self, translation_config: TranslationConfig) -> TranslateResult:
self.font_mapper.add_font(pdf, self.docs)
with self.translation_config.progress_monitor.stage_start(
self.stage_name,
len(self.docs.page) + 2,
len(self.docs.page),
) as pbar:
for page in self.docs.page:
translation_config.raise_if_cancelled()
Expand Down Expand Up @@ -346,9 +349,18 @@ def write(self, translation_config: TranslationConfig) -> TranslateResult:
pdf.update_stream(op_container, draw_op.tobytes())
pdf[page.page_number].set_contents(op_container)
pbar.advance()
translation_config.raise_if_cancelled()
translation_config.raise_if_cancelled()
with self.translation_config.progress_monitor.stage_start(
SUBSET_FONT_STAGE_NAME,
1,
) as pbar:
if not translation_config.skip_clean:
pdf.subset_fonts(fallback=False)
pbar.advance()
with self.translation_config.progress_monitor.stage_start(
SAVE_PDF_STAGE_NAME,
2,
) as pbar:
if not translation_config.no_mono:
if translation_config.debug:
translation_config.raise_if_cancelled()
Expand Down
4 changes: 4 additions & 0 deletions babeldoc/high_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from babeldoc.const import CACHE_FOLDER
from babeldoc.const import get_cache_file_path
from babeldoc.converter import TranslateConverter
from babeldoc.document_il.backend.pdf_creater import SAVE_PDF_STAGE_NAME
from babeldoc.document_il.backend.pdf_creater import SUBSET_FONT_STAGE_NAME
from babeldoc.document_il.backend.pdf_creater import PDFCreater
from babeldoc.document_il.frontend.il_creater import ILCreater
from babeldoc.document_il.midend.add_debug_information import AddDebugInformation
Expand Down Expand Up @@ -48,6 +50,8 @@
Typesetting.stage_name,
FontMapper.stage_name,
PDFCreater.stage_name,
SUBSET_FONT_STAGE_NAME,
SAVE_PDF_STAGE_NAME,
]

resfont_map = {
Expand Down
2 changes: 1 addition & 1 deletion babeldoc/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from babeldoc.translation_config import TranslationConfig

logger = logging.getLogger(__name__)
__version__ = "0.1.8"
__version__ = "0.1.9"


def create_parser():
Expand Down
2 changes: 1 addition & 1 deletion babeldoc/progress_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def calculate_current_progress(self, stage=None):
def stage_update(self, stage, n: int):
with self.lock:
report_time_delta = time.time() - self.last_report_time
if report_time_delta < self.report_interval:
if report_time_delta < self.report_interval and stage.total > 3:
return
if self.progress_change_callback:
self.progress_change_callback(
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "BabelDOC"
version = "0.1.8"
version = "0.1.9"
description = "Yet Another Document Translator"
license = "AGPL-3.0"
readme = "README.md"
Expand Down Expand Up @@ -117,7 +117,7 @@ dev = [
]

[bumpver]
current_version = "0.1.8"
current_version = "0.1.9"
version_pattern = "MAJOR.MINOR.PATCH[.PYTAGNUM]"

[bumpver.file_patterns]
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3c7d11a

Please sign in to comment.