Skip to content

Commit

Permalink
Merge pull request #110 from funstory-ai/ft/imposition
Browse files Browse the repository at this point in the history
feat: Change bilingual comparison to side-by-side display.
  • Loading branch information
awwaawwa authored Feb 21, 2025
2 parents 877b5ee + c645094 commit 67863ac
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 13 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.10"
__version__ = "0.1.11"
56 changes: 48 additions & 8 deletions babeldoc/document_il/backend/pdf_creater.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,53 @@ def write(self, translation_config: TranslationConfig) -> TranslateResult:
f"{basename}{debug_suffix}.{translation_config.lang_out}.dual.pdf",
)
translation_config.raise_if_cancelled()
dual = pymupdf.open(self.original_pdf_path)
original_pdf = pymupdf.open(self.original_pdf_path)
translated_pdf = pdf

# Create a new PDF for side-by-side pages
dual = pymupdf.open()
page_count = min(original_pdf.page_count, translated_pdf.page_count)

for page_id in range(page_count):
# Get pages from both PDFs
orig_page = original_pdf[page_id]
trans_page = translated_pdf[page_id]

# Calculate total width and use max height
total_width = orig_page.rect.width + trans_page.rect.width
max_height = max(orig_page.rect.height, trans_page.rect.height)

# Create new page with combined width
dual_page = dual.new_page(width=total_width, height=max_height)

# Define rectangles for left and right sides
left_width = (
orig_page.rect.width
if not translation_config.dual_translate_first
else trans_page.rect.width
)
rect_left = pymupdf.Rect(0, 0, left_width, max_height)
rect_right = pymupdf.Rect(left_width, 0, total_width, max_height)

# Show pages according to dual_translate_first setting
if translation_config.dual_translate_first:
# Show translated page on left and original on right
rect_left, rect_right = rect_right, rect_left

# Show original page on left and translated on right (default)
dual_page.show_pdf_page(
rect_left,
original_pdf,
page_id,
keep_proportion=True,
)
dual_page.show_pdf_page(
rect_right,
translated_pdf,
page_id,
keep_proportion=True,
)

if translation_config.debug:
translation_config.raise_if_cancelled()
try:
Expand All @@ -395,13 +441,7 @@ def write(self, translation_config: TranslationConfig) -> TranslateResult:
"Failed to write debug info to dual PDF",
exc_info=True,
)
dual.insert_file(pdf)
page_count = pdf.page_count
for page_id in range(page_count):
if translation_config.dual_translate_first:
dual.move_page(page_count + page_id, page_id * 2)
else:
dual.move_page(page_count + page_id, page_id * 2 + 1)

dual.save(
dual_out_path,
garbage=3,
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.10"
__version__ = "0.1.11"


def create_parser():
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.10"
version = "0.1.11"
description = "Yet Another Document Translator"
license = "AGPL-3.0"
readme = "README.md"
Expand Down Expand Up @@ -124,7 +124,7 @@ dev = [
]

[bumpver]
current_version = "0.1.10"
current_version = "0.1.11"
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 67863ac

Please # to comment.