1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-10-03 23:48:07 +03:00

Capture fontTools logs when subsetting fonts

Fix #2169.
This commit is contained in:
Guillaume Ayoub 2024-07-03 21:35:38 +02:00
parent e5fa17ec62
commit 61852c4098
3 changed files with 42 additions and 34 deletions

View File

@ -1,8 +1,6 @@
"""Helpers for tests."""
import contextlib
import functools
import logging
import sys
from pathlib import Path
@ -12,7 +10,7 @@ from weasyprint.css.counters import CounterStyle
from weasyprint.css.targets import TargetCollector
from weasyprint.formatting_structure import boxes, build
from weasyprint.html import HTML5_UA_STYLESHEET
from weasyprint.logger import LOGGER
from weasyprint.logger import capture_logs
from weasyprint.text.fonts import FontConfiguration
from weasyprint.urls import path2url
@ -73,35 +71,6 @@ BASE_URL = path2url(resource_path('<test>'))
TEST_UA_FONT_CONFIG = FontConfiguration()
TEST_UA_STYLESHEET = CSS(resource_path('tests_ua.css'), font_config=TEST_UA_FONT_CONFIG)
class CallbackHandler(logging.Handler):
"""A logging handler that calls a function for every message."""
def __init__(self, callback):
logging.Handler.__init__(self)
self.emit = callback
@contextlib.contextmanager
def capture_logs():
"""Return a context manager that captures all logged messages."""
logger = LOGGER
messages = []
def emit(record):
if record.name == 'weasyprint.progress':
return
messages.append(f'{record.levelname.upper()}: {record.getMessage()}')
previous_handlers = logger.handlers
previous_level = logger.level
logger.handlers = []
logger.addHandler(CallbackHandler(emit))
logger.setLevel(logging.DEBUG)
try:
yield messages
finally:
logger.handlers = previous_handlers
logger.level = previous_level
def assert_no_logs(function):
"""Decorator that asserts that nothing is logged in a function."""

View File

@ -13,6 +13,7 @@ Logging levels are used for specific purposes:
"""
import contextlib
import logging
LOGGER = logging.getLogger('weasyprint')
@ -21,3 +22,35 @@ if not LOGGER.handlers: # pragma: no cover
LOGGER.addHandler(logging.NullHandler())
PROGRESS_LOGGER = logging.getLogger('weasyprint.progress')
class CallbackHandler(logging.Handler):
"""A logging handler that calls a function for every message."""
def __init__(self, callback):
logging.Handler.__init__(self)
self.emit = callback
@contextlib.contextmanager
def capture_logs(logger='weasyprint', level=None):
"""Return a context manager that captures all logged messages."""
logger = logging.getLogger(logger)
messages = []
def emit(record):
if record.name == 'weasyprint.progress':
return
if level is not None and record.levelno < level:
return
messages.append(f'{record.levelname.upper()}: {record.getMessage()}')
previous_handlers = logger.handlers
previous_level = logger.level
logger.handlers = []
logger.addHandler(CallbackHandler(emit))
logger.setLevel(logging.DEBUG)
try:
yield messages
finally:
logger.handlers = previous_handlers
logger.level = previous_level

View File

@ -2,6 +2,7 @@
import io
from hashlib import md5
from logging import WARNING
from math import ceil
import pydyf
@ -9,7 +10,7 @@ from fontTools import subset
from fontTools.ttLib import TTFont, TTLibError, ttFont
from fontTools.varLib.mutator import instantiateVariableFont
from ..logger import LOGGER
from ..logger import LOGGER, capture_logs
from ..text.constants import PANGO_STRETCH_PERCENT
from ..text.ffi import ffi, harfbuzz, harfbuzz_subset, pango, units_to_double
from ..text.fonts import get_hb_object_data, get_pango_font_hb_face
@ -257,7 +258,12 @@ class Font:
# Subset font.
try:
ttfont = TTFont(full_font, fontNumber=self.index)
subsetter.subset(ttfont)
with capture_logs('fontTools', level=WARNING) as logs:
subsetter.subset(ttfont)
for log in logs:
LOGGER.warning(
'fontTools warning when subsetting "%s": %s',
self.family.decode(), log)
except TTLibError:
LOGGER.warning('Unable to subset font with fontTools')
else: