Skip to content

Commit

Permalink
Capture fontTools logs when subsetting fonts
Browse files Browse the repository at this point in the history
Fix #2169.
  • Loading branch information
liZe committed Jul 3, 2024
1 parent e5fa17e commit 61852c4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 34 deletions.
33 changes: 1 addition & 32 deletions tests/testing_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""Helpers for tests."""

import contextlib
import functools
import logging
import sys
from pathlib import Path

Expand All @@ -12,7 +10,7 @@
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

Expand Down Expand Up @@ -73,35 +71,6 @@ def resource_path(name):
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."""
Expand Down
33 changes: 33 additions & 0 deletions weasyprint/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"""

import contextlib
import logging

LOGGER = logging.getLogger('weasyprint')
Expand All @@ -21,3 +22,35 @@
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
10 changes: 8 additions & 2 deletions weasyprint/pdf/fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import io
from hashlib import md5
from logging import WARNING
from math import ceil

import pydyf
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
Expand Down Expand Up @@ -257,7 +258,12 @@ def _fonttools_subset(self, cmap, hinting):
# 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:
Expand Down

0 comments on commit 61852c4

Please sign in to comment.