Some tests for fallback font matching

This commit is contained in:
Kovid Goyal 2016-11-15 10:52:24 +05:30
parent b17a501697
commit 2d812445ea
2 changed files with 22 additions and 5 deletions

View File

@ -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

View File

@ -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)