Skip to content

Commit

Permalink
Merge pull request #132 from funstory-ai/ft/better-font-download
Browse files Browse the repository at this point in the history
fix: Fixed watermark becoming bold
  • Loading branch information
awwaawwa authored Mar 2, 2025
2 parents fb2a582 + ea083cd commit 6f16ac1
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 10 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.20"
__version__ = "0.1.21"
5 changes: 4 additions & 1 deletion babeldoc/assets/embedding_assets_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@
"GoNotoKurrent-Regular.ttf",
"GoNotoKurrent-Bold.ttf",
],
"base": ["SourceHanSansCN-Regular.ttf"],
}

HK_FONT_FAMILY = {
Expand All @@ -388,6 +389,7 @@
"GoNotoKurrent-Regular.ttf",
"GoNotoKurrent-Bold.ttf",
],
"base": ["SourceHanSansCN-Regular.ttf"],
}

TW_FONT_FAMILY = {
Expand All @@ -406,6 +408,7 @@
"GoNotoKurrent-Regular.ttf",
"GoNotoKurrent-Bold.ttf",
],
"base": ["SourceHanSansCN-Regular.ttf"],
}

ALL_FONT_FAMILY = {
Expand All @@ -431,7 +434,7 @@ def verify_font_family(font_family: str | dict):
if isinstance(font_family, str):
font_family = ALL_FONT_FAMILY[font_family]
for k in font_family:
if k not in ["script", "normal", "fallback"]:
if k not in ["script", "normal", "fallback", "base"]:
raise ValueError(f"Invalid font family: {font_family}")
for font_file_name in font_family[k]:
if font_file_name not in EMBEDDING_FONT_METADATA:
Expand Down
2 changes: 1 addition & 1 deletion babeldoc/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import subprocess
from pathlib import Path

__version__ = "0.1.20"
__version__ = "0.1.21"

CACHE_FOLDER = Path.home() / ".cache" / "babeldoc"

Expand Down
13 changes: 9 additions & 4 deletions babeldoc/document_il/utils/fontmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(self, translation_config: TranslationConfig):
"normal",
"script",
"fallback",
"base",
):
self.font_file_names.extend(font_family[k])

Expand All @@ -35,6 +36,7 @@ def __init__(self, translation_config: TranslationConfig):
self.fonts[font_file_name] = pymupdf.Font(fontfile=str(font_path))
self.fontid2fontpath[font_file_name] = font_path
self.fonts[font_file_name].font_id = font_file_name
self.fonts[font_file_name].font_path = font_path
self.fonts[font_file_name].ascent_fontmap = font_metadata["ascent"]
self.fonts[font_file_name].descent_fontmap = font_metadata["descent"]
self.fonts[font_file_name].encoding_length = font_metadata[
Expand All @@ -44,8 +46,8 @@ def __init__(self, translation_config: TranslationConfig):
self.normal_font_ids: list[str] = font_family["normal"]
self.script_font_ids: list[str] = font_family["script"]
self.fallback_font_ids: list[str] = font_family["fallback"]

self.fontid2fontpath["base"] = self.fontid2fontpath[self.normal_font_ids[0]]
self.base_font_ids: list[str] = font_family["base"]
self.fontid2fontpath["base"] = self.fontid2fontpath[font_family["base"][0]]

self.fontid2font: dict[str, pymupdf.Font] = {
f.font_id: f for f in self.fonts.values()
Expand All @@ -56,7 +58,7 @@ def __init__(self, translation_config: TranslationConfig):
font.char_lengths,
)

self.fontid2font["base"] = self.fontid2font[self.normal_font_ids[0]]
self.fontid2font["base"] = self.fontid2font[self.base_font_ids[0]]

self.normal_fonts: list[pymupdf.Font] = [
self.fontid2font[font_id] for font_id in self.normal_font_ids
Expand All @@ -68,12 +70,13 @@ def __init__(self, translation_config: TranslationConfig):
self.fontid2font[font_id] for font_id in self.fallback_font_ids
]

self.base_font = self.normal_fonts[0]
self.base_font = self.fontid2font["base"]

self.type2font: dict[str, list[pymupdf.Font]] = {
"normal": self.normal_fonts,
"script": self.script_fonts,
"fallback": self.fallback_fonts,
"base": [self.base_font],
}

self.has_char = functools.lru_cache(maxsize=10240, typed=True)(self.has_char)
Expand Down Expand Up @@ -167,6 +170,8 @@ def add_font(self, doc_zh: pymupdf.Document, il: il_version_1.Document):
xreflen - 1 + len(font_list) + len(il.page) + len(font_list),
) as pbar:
for font in font_list:
if font[0] in font_id:
continue
font_id[font[0]] = doc_zh[0].insert_font(font[0], font[1])
pbar.advance(1)
for xref in range(1, xreflen):
Expand Down
2 changes: 1 addition & 1 deletion babeldoc/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from babeldoc.translation_config import TranslationConfig

logger = logging.getLogger(__name__)
__version__ = "0.1.20"
__version__ = "0.1.21"


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.20"
version = "0.1.21"
description = "Yet Another Document Translator"
license = "AGPL-3.0"
readme = "README.md"
Expand Down Expand Up @@ -132,7 +132,7 @@ pythonpath = [".", "src"]
testpaths = ["tests"]

[bumpver]
current_version = "0.1.20"
current_version = "0.1.21"
version_pattern = "MAJOR.MINOR.PATCH[.PYTAGNUM]"

[bumpver.file_patterns]
Expand Down

0 comments on commit 6f16ac1

Please sign in to comment.