Skip to content

Commit 61852c4

Browse files
committed
Capture fontTools logs when subsetting fonts
Fix #2169.
1 parent e5fa17e commit 61852c4

File tree

3 files changed

+42
-34
lines changed

3 files changed

+42
-34
lines changed

tests/testing_utils.py

+1-32
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
"""Helpers for tests."""
22

3-
import contextlib
43
import functools
5-
import logging
64
import sys
75
from pathlib import Path
86

@@ -12,7 +10,7 @@
1210
from weasyprint.css.targets import TargetCollector
1311
from weasyprint.formatting_structure import boxes, build
1412
from weasyprint.html import HTML5_UA_STYLESHEET
15-
from weasyprint.logger import LOGGER
13+
from weasyprint.logger import capture_logs
1614
from weasyprint.text.fonts import FontConfiguration
1715
from weasyprint.urls import path2url
1816

@@ -73,35 +71,6 @@ def resource_path(name):
7371
TEST_UA_FONT_CONFIG = FontConfiguration()
7472
TEST_UA_STYLESHEET = CSS(resource_path('tests_ua.css'), font_config=TEST_UA_FONT_CONFIG)
7573

76-
class CallbackHandler(logging.Handler):
77-
"""A logging handler that calls a function for every message."""
78-
def __init__(self, callback):
79-
logging.Handler.__init__(self)
80-
self.emit = callback
81-
82-
83-
@contextlib.contextmanager
84-
def capture_logs():
85-
"""Return a context manager that captures all logged messages."""
86-
logger = LOGGER
87-
messages = []
88-
89-
def emit(record):
90-
if record.name == 'weasyprint.progress':
91-
return
92-
messages.append(f'{record.levelname.upper()}: {record.getMessage()}')
93-
94-
previous_handlers = logger.handlers
95-
previous_level = logger.level
96-
logger.handlers = []
97-
logger.addHandler(CallbackHandler(emit))
98-
logger.setLevel(logging.DEBUG)
99-
try:
100-
yield messages
101-
finally:
102-
logger.handlers = previous_handlers
103-
logger.level = previous_level
104-
10574

10675
def assert_no_logs(function):
10776
"""Decorator that asserts that nothing is logged in a function."""

weasyprint/logger.py

+33
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
1414
"""
1515

16+
import contextlib
1617
import logging
1718

1819
LOGGER = logging.getLogger('weasyprint')
@@ -21,3 +22,35 @@
2122
LOGGER.addHandler(logging.NullHandler())
2223

2324
PROGRESS_LOGGER = logging.getLogger('weasyprint.progress')
25+
26+
27+
class CallbackHandler(logging.Handler):
28+
"""A logging handler that calls a function for every message."""
29+
def __init__(self, callback):
30+
logging.Handler.__init__(self)
31+
self.emit = callback
32+
33+
34+
@contextlib.contextmanager
35+
def capture_logs(logger='weasyprint', level=None):
36+
"""Return a context manager that captures all logged messages."""
37+
logger = logging.getLogger(logger)
38+
messages = []
39+
40+
def emit(record):
41+
if record.name == 'weasyprint.progress':
42+
return
43+
if level is not None and record.levelno < level:
44+
return
45+
messages.append(f'{record.levelname.upper()}: {record.getMessage()}')
46+
47+
previous_handlers = logger.handlers
48+
previous_level = logger.level
49+
logger.handlers = []
50+
logger.addHandler(CallbackHandler(emit))
51+
logger.setLevel(logging.DEBUG)
52+
try:
53+
yield messages
54+
finally:
55+
logger.handlers = previous_handlers
56+
logger.level = previous_level

weasyprint/pdf/fonts.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
import io
44
from hashlib import md5
5+
from logging import WARNING
56
from math import ceil
67

78
import pydyf
89
from fontTools import subset
910
from fontTools.ttLib import TTFont, TTLibError, ttFont
1011
from fontTools.varLib.mutator import instantiateVariableFont
1112

12-
from ..logger import LOGGER
13+
from ..logger import LOGGER, capture_logs
1314
from ..text.constants import PANGO_STRETCH_PERCENT
1415
from ..text.ffi import ffi, harfbuzz, harfbuzz_subset, pango, units_to_double
1516
from ..text.fonts import get_hb_object_data, get_pango_font_hb_face
@@ -257,7 +258,12 @@ def _fonttools_subset(self, cmap, hinting):
257258
# Subset font.
258259
try:
259260
ttfont = TTFont(full_font, fontNumber=self.index)
260-
subsetter.subset(ttfont)
261+
with capture_logs('fontTools', level=WARNING) as logs:
262+
subsetter.subset(ttfont)
263+
for log in logs:
264+
LOGGER.warning(
265+
'fontTools warning when subsetting "%s": %s',
266+
self.family.decode(), log)
261267
except TTLibError:
262268
LOGGER.warning('Unable to subset font with fontTools')
263269
else:

0 commit comments

Comments
 (0)