Support for font-variant

This commit is contained in:
Kovid Goyal 2007-08-04 21:44:18 +00:00
parent e71d862928
commit e6bcc95150
2 changed files with 42 additions and 6 deletions

View File

@ -113,6 +113,12 @@ class Span(_Span):
ans = 'sans' ans = 'sans'
return ans return ans
def font_variant(val):
ans = None
if 'small-caps' in val.lower():
ans = 'small-caps'
return ans
def font_key(family, style, weight): def font_key(family, style, weight):
key = 'normal' key = 'normal'
if style == 'italic' and weight == 'normal': if style == 'italic' and weight == 'normal':
@ -122,6 +128,8 @@ class Span(_Span):
elif style == 'italic' and weight == 'bold': elif style == 'italic' and weight == 'bold':
key = 'bi' key = 'bi'
return key return key
def font_size(val): def font_size(val):
@ -154,7 +162,7 @@ class Span(_Span):
return ans return ans
t = dict() t = dict()
family, weight, style = 'serif', 'normal', 'normal' family, weight, style, variant = 'serif', 'normal', 'normal', None
for key in d.keys(): for key in d.keys():
val = d[key].lower() val = d[key].lower()
if key == 'font': if key == 'font':
@ -176,6 +184,11 @@ class Span(_Span):
if sz: if sz:
t['fontsize'] = sz t['fontsize'] = sz
break break
for val in vals:
variant = font_variant(val)
if variant:
t['fontvariant'] = variant
break
elif key in ['font-family', 'font-name']: elif key in ['font-family', 'font-name']:
family = font_family(val) family = font_family(val)
elif key == "font-size": elif key == "font-size":
@ -186,6 +199,10 @@ class Span(_Span):
weight = font_weight(val) weight = font_weight(val)
elif key == 'font-style': elif key == 'font-style':
style = font_style(val) style = font_style(val)
elif key == 'font-variant':
variant = font_variant(val)
if variant:
t['fontvariant'] = variant
else: else:
report = True report = True
if memory != None: if memory != None:
@ -200,7 +217,7 @@ class Span(_Span):
t['wordspace'] = 50 t['wordspace'] = 50
return t return t
def __init__(self, ns, css, memory, dpi, fonts, font_delta=0): def __init__(self, ns, css, memory, dpi, fonts, font_delta=0, normal_font_size=100):
src = ns.string if hasattr(ns, 'string') else ns src = ns.string if hasattr(ns, 'string') else ns
src = re.sub(r'\s{2,}', ' ', src) # Remove multiple spaces src = re.sub(r'\s{2,}', ' ', src) # Remove multiple spaces
for pat, repl in Span.rules: for pat, repl in Span.rules:
@ -208,6 +225,20 @@ class Span(_Span):
if not src: if not src:
raise ConversionError('No point in adding an empty string to a Span') raise ConversionError('No point in adding an empty string to a Span')
attrs = Span.translate_attrs(css, dpi, fonts, font_delta=font_delta, memory=memory) attrs = Span.translate_attrs(css, dpi, fonts, font_delta=font_delta, memory=memory)
if 'fontsize' in attrs.keys():
normal_font_size = attrs['fontsize']
variant = attrs.pop('fontvariant', None)
if variant == 'small-caps':
tokens = [ i.upper() for i in src.split()]
spans = []
for i in tokens:
f, r = i[0], i[1:]+' '
spans.append(f)
spans.append(_Span(r, fontsize=normal_font_size-30))
src = _Span(fontsize=normal_font_size)
for i in spans:
src.append(i)
family, key = attrs['fontfacename'] family, key = attrs['fontfacename']
if fonts[family].has_key(key): if fonts[family].has_key(key):
attrs['fontfacename'] = fonts[family][key][1] attrs['fontfacename'] = fonts[family][key][1]
@ -740,7 +771,7 @@ class HTMLConverter(object):
'padding' in test or 'border' in test or 'page-break' in test \ 'padding' in test or 'border' in test or 'page-break' in test \
or test.startswith('mso') or test.startswith('background')\ or test.startswith('mso') or test.startswith('background')\
or test.startswith('line') or test in ['color', 'display', \ or test.startswith('line') or test in ['color', 'display', \
'letter-spacing', 'font-variant', 'position']: 'letter-spacing', 'position']:
css.pop(key) css.pop(key)
return css return css

View File

@ -3,7 +3,7 @@
<style type='text/css'> <style type='text/css'>
.toc { page-break-after: always; text-indent: 0em; } .toc { page-break-after: always; text-indent: 0em; }
.tocpn {text-align: right; } .tocpn {text-align: right; }
.tocchr {text-align: right; font-variant: small-caps;} .tocchr {text-align: right;}
</style> </style>
</head> </head>
<h1>Demo of <span style='font-family:monospace'>html2lrf</span></h1> <h1>Demo of <span style='font-family:monospace'>html2lrf</span></h1>
@ -18,7 +18,7 @@
<li><a href='#text'>Text formatting and ruled lines</a></li> <li><a href='#text'>Text formatting and ruled lines</a></li>
<li><a href='#images'>Inline images</a></li> <li><a href='#images'>Inline images</a></li>
<li><a href='#fonts'>Embedded Fonts</a></li> <li><a href='#fonts'>Embedded Fonts</a></li>
<li><a href='#dropcaps'>Dropcaps</a></li> <li><a href='#dropcaps'>Dropcaps and Small-Caps</a></li>
<li><a href='#recursive'>Recursive link following</a></li> <li><a href='#recursive'>Recursive link following</a></li>
</ul> </ul>
@ -155,7 +155,7 @@
<a href='#toc'>Table of Contents</a> <a href='#toc'>Table of Contents</a>
</p> </p>
<h2><a name='dropcaps'>Dropcaps</a></h2> <h2><a name='dropcaps'>Dropcaps and Small-Caps</a></h2>
<br/> <br/>
<p><img class='libprs500_dropcaps' src='a.png' /> beautiful image based dropcaps to emphasize this <p><img class='libprs500_dropcaps' src='a.png' /> beautiful image based dropcaps to emphasize this
paragraph. Image based dropcaps are specified by adding the <code>class = 'libprs500_dropcaps'</code> paragraph. Image based dropcaps are specified by adding the <code>class = 'libprs500_dropcaps'</code>
@ -165,6 +165,11 @@
<p><big class='libprs500_dropcaps'>T</big>his is a plain text based dropcaps. It <p><big class='libprs500_dropcaps'>T</big>his is a plain text based dropcaps. It
is not nearly as dramatic, but easier to code ;-) is not nearly as dramatic, but easier to code ;-)
</p> </p>
<br/>
<br/>
<p><span style="font-variant: small-caps">This is an example</span> of small-caps.
It can also be used to highlight the start of a paragraph very effectively.
</p>
<p class='toc'> <p class='toc'>
<hr /> <hr />
<a href='#toc'>Table of Contents</a> <a href='#toc'>Table of Contents</a>