From 2d812445ea51d8c76f6bafc5eb1139b19ea7fc62 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 15 Nov 2016 10:52:24 +0530 Subject: [PATCH] Some tests for fallback font matching --- src/calibre/ebooks/oeb/polish/embed.py | 10 +++++----- src/calibre/ebooks/oeb/polish/tests/cascade.py | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/calibre/ebooks/oeb/polish/embed.py b/src/calibre/ebooks/oeb/polish/embed.py index c712c5f32f..4ff7be05f2 100644 --- a/src/calibre/ebooks/oeb/polish/embed.py +++ b/src/calibre/ebooks/oeb/polish/embed.py @@ -77,8 +77,8 @@ def filter_by_stretch(fonts, val): candidates = condensed or expanded else: candidates = expanded or condensed - distance_map = [abs(stretch_map[i] - val) for i in candidates] - min_dist = min(distance_map) + distance_map = {i:abs(stretch_map[i] - val) for i in candidates} + min_dist = min(distance_map.itervalues()) return [fonts[i] for i in candidates if distance_map[i] == min_dist] @@ -125,12 +125,12 @@ def filter_by_weight(fonts, val): if 400 in rmap: return [fonts[rmap[400]]] candidates = below or above - distance_map = [abs(weight_map[i] - val) for i in candidates] - min_dist = min(distance_map) + distance_map = {i:abs(weight_map[i] - val) for i in candidates} + min_dist = min(distance_map.itervalues()) return [fonts[i] for i in candidates if distance_map[i] == min_dist] -def find_matching_font(fonts, weight, style, stretch): +def find_matching_font(fonts, weight='normal', style='normal', stretch='normal'): # See https://www.w3.org/TR/css-fonts-3/#font-style-matching # We dont implement the unicode character range testing # We also dont implement bolder, lighter diff --git a/src/calibre/ebooks/oeb/polish/tests/cascade.py b/src/calibre/ebooks/oeb/polish/tests/cascade.py index f54b7731d2..9f301e062a 100644 --- a/src/calibre/ebooks/oeb/polish/tests/cascade.py +++ b/src/calibre/ebooks/oeb/polish/tests/cascade.py @@ -14,6 +14,7 @@ from calibre.constants import iswindows from calibre.ebooks.oeb.base import OEB_STYLES, OEB_DOCS from calibre.ebooks.oeb.polish.cascade import iterrules, resolve_styles, DEFAULTS from calibre.ebooks.oeb.polish.css import remove_property_value +from calibre.ebooks.oeb.polish.embed import find_matching_font from calibre.ebooks.oeb.polish.container import ContainerBase, href_to_name from calibre.ebooks.oeb.polish.stats import StatsCollector, font_keys, normalize_font_properties, prepare_font_rule from calibre.ebooks.oeb.polish.tests.base import BaseTest @@ -213,3 +214,19 @@ class CascadeTest(BaseTest): for prop in style.getProperties(all=True): remove_property_value(prop, lambda val:'png' in val.cssText) self.assertEqual('background: black fixed', style.cssText) + + def test_fallback_font_matching(self): + def cf(id, weight='normal', style='normal', stretch='normal'): + return {'id':id, 'font-weight':weight, 'font-style':style, 'font-stretch':stretch} + fonts = [cf(1, '500', 'oblique', 'condensed'), cf(2, '300', 'italic', 'normal')] + self.assertEqual(find_matching_font(fonts)['id'], 2) + fonts = [cf(1, '500', 'oblique', 'normal'), cf(2, '300', 'italic', 'normal')] + self.assertEqual(find_matching_font(fonts)['id'], 1) + fonts = [cf(1, '500', 'oblique', 'normal'), cf(2, '200', 'oblique', 'normal')] + self.assertEqual(find_matching_font(fonts)['id'], 1) + fonts = [cf(1, '600', 'oblique', 'normal'), cf(2, '100', 'oblique', 'normal')] + self.assertEqual(find_matching_font(fonts)['id'], 2) + fonts = [cf(1, '600', 'oblique', 'normal'), cf(2, '100', 'oblique', 'normal')] + self.assertEqual(find_matching_font(fonts, '500')['id'], 2) + fonts = [cf(1, '600', 'oblique', 'normal'), cf(2, '100', 'oblique', 'normal')] + self.assertEqual(find_matching_font(fonts, '600')['id'], 1)