Skip to content

Commit

Permalink
Conversion: Add support for the CSS text-transform property when subs…
Browse files Browse the repository at this point in the history
…etting embedded fonts
  • Loading branch information
kovidgoyal committed Apr 1, 2024
1 parent 1365b44 commit 1fbec29
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/calibre/ebooks/oeb/transforms/subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from calibre.utils.fonts.subset import subset
from polyglot.builtins import iteritems

font_properties = ('font-family', 'src', 'font-weight', 'font-stretch', 'font-style', 'text-transform')


def get_font_properties(rule, default=None):
'''
Expand All @@ -24,8 +26,7 @@ def get_font_properties(rule, default=None):
'''
props = {}
s = rule.style
for q in ('font-family', 'src', 'font-weight', 'font-stretch',
'font-style'):
for q in font_properties:
g = 'uri' if q == 'src' else 'value'
try:
val = s.getProperty(q).propertyValue[0]
Expand Down Expand Up @@ -299,13 +300,20 @@ def used_font(self, style):
if matches:
return matches[0]

def find_chars(self, elem):
def find_chars(self, elem, style):
ans = set()
transform = lambda x: x # noqa
tt = style.get('text-transform')
if tt:
if tt in ('uppercase', 'capitalize'):
transform = str.upper
elif tt == 'lowercase':
transform = str.lower
if elem.text:
ans |= set(elem.text)
ans |= set(transform(elem.text))
for child in elem:
if child.tail:
ans |= set(child.tail)
ans |= set(transform(child.tail))
return ans

def find_usage_in(self, elem, inherited_style):
Expand All @@ -314,6 +322,6 @@ def find_usage_in(self, elem, inherited_style):
self.find_usage_in(child, style)
font = self.used_font(style)
if font:
chars = self.find_chars(elem)
chars = self.find_chars(elem, style)
if chars:
font['chars'] |= chars

0 comments on commit 1fbec29

Please sign in to comment.