Permalink
Browse files

Add support for font-stretch, fix #7

The value is passed to Pango as-is. It may have no effect if
(for example) the chosen font has no expanded variant.
  • Loading branch information...
1 parent a09eb52 commit 39b553fae792dd64a587afb45cf794d263bccf63 @SimonSapin SimonSapin committed Aug 17, 2012
Showing with 63 additions and 1 deletion.
  1. +8 −0 CHANGES
  2. +2 −0 weasyprint/css/properties.py
  3. +10 −0 weasyprint/css/validation.py
  4. +1 −1 weasyprint/html.py
  5. +19 −0 weasyprint/tests/test_layout.py
  6. +23 −0 weasyprint/text.py
View
@@ -1,6 +1,14 @@
WeasyPrint changelog
====================
+Version 0.15
+------------
+
+Not released yet.
+
+Add support for the ``font-stretch`` property.
+
+
Version 0.14
------------
@@ -62,6 +62,7 @@
'float': 'none',
'font_family': ['serif'], # depends on user agent
'font_size': 16, # Actually medium, but we define medium from this.
+ 'font_stretch': 'normal', # css3-fonts
'font_style': 'normal',
'font_variant': 'normal',
'font_weight': 400,
@@ -190,6 +191,7 @@
font_family
font_size
font_style
+ font_stretch
font_variant
font_weight
letter_spacing
@@ -598,6 +598,16 @@ def font_style(keyword):
@validator()
@single_keyword
+def font_stretch(keyword):
+ """Validation for the ``font-stretch`` property."""
+ return keyword in (
+ 'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed',
+ 'normal',
+ 'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded')
+
+
+@validator()
+@single_keyword
def font_variant(keyword):
"""``font-variant`` property validation."""
return keyword in ('normal', 'small-caps')
View
@@ -206,7 +206,7 @@ def find_base_url(html_document, fallback_base_url):
See http://www.w3.org/TR/html5/urls.html#document-base-url
"""
- first_base_element = next(html_document.iter('base'), None)
+ first_base_element = next(iter(html_document.iter('base')), None)
if first_base_element is not None:
href = first_base_element.get('href', '').strip()
if href:
@@ -4114,3 +4114,22 @@ def test_floats():
line, = anon_block.children
img_2, = line.children
assert outer_area(img_2) == (0, 0, 50, 50)
+
+
+@assert_no_logs
+def test_font_stretch():
+ page, = parse('''
+ <style>p { float: left }</style>
+ <p>Hello, world!</p>
+ <p style="font-stretch: semi-condensed">Hello, world!</p>
+ <p style="font-stretch: semi-expanded">Hello, world!</p>
+ ''')
+ html, = page.children
+ body, = html.children
+ p_1, p_2, p_3 = body.children
+ normal = p_1.width
+ condensed = p_2.width
+ expanded = p_3.width
+ assert condensed < normal
+ # TODO: when @font-face is supported use a font with an expanded variant.
+# assert normal < expanded
View
@@ -45,6 +45,17 @@
'italic': Pango.Style.ITALIC,
'oblique': Pango.Style.OBLIQUE,
}
+ PANGO_STRETCH = {
+ 'ultra-condensed': Pango.Stretch.ULTRA_CONDENSED,
+ 'extra-condensed': Pango.Stretch.EXTRA_CONDENSED,
+ 'condensed': Pango.Stretch.CONDENSED,
+ 'semi-condensed': Pango.Stretch.SEMI_CONDENSED,
+ 'normal': Pango.Stretch.NORMAL,
+ 'semi-expanded': Pango.Stretch.SEMI_EXPANDED,
+ 'expanded': Pango.Stretch.EXPANDED,
+ 'extra-expanded': Pango.Stretch.EXTRA_EXPANDED,
+ 'ultra-expanded': Pango.Stretch.ULTRA_EXPANDED,
+ }
PANGO_WRAP_WORD = Pango.WrapMode.WORD
def set_text(layout, text):
@@ -79,6 +90,17 @@ def show_first_line(cairo_context, pango_layout, hinting):
'italic': Pango.STYLE_ITALIC,
'oblique': Pango.STYLE_OBLIQUE,
}
+ PANGO_STRETCH = {
+ 'ultra-condensed': Pango.STRETCH_ULTRA_CONDENSED,
+ 'extra-condensed': Pango.STRETCH_EXTRA_CONDENSED,
+ 'condensed': Pango.STRETCH_CONDENSED,
+ 'semi-condensed': Pango.STRETCH_SEMI_CONDENSED,
+ 'normal': Pango.STRETCH_NORMAL,
+ 'semi-expanded': Pango.STRETCH_SEMI_EXPANDED,
+ 'expanded': Pango.STRETCH_EXPANDED,
+ 'extra-expanded': Pango.STRETCH_EXTRA_EXPANDED,
+ 'ultra-expanded': Pango.STRETCH_ULTRA_EXPANDED,
+ }
PANGO_WRAP_WORD = Pango.WRAP_WORD
set_text = Pango.Layout.set_text
@@ -136,6 +158,7 @@ def create_layout(text, style, hinting, max_width):
font.set_family(','.join(style.font_family))
font.set_variant(PANGO_VARIANT[style.font_variant])
font.set_style(PANGO_STYLE[style.font_style])
+ font.set_stretch(PANGO_STRETCH[style.font_stretch])
font.set_absolute_size(units_from_double(style.font_size))
font.set_weight(style.font_weight)
layout.set_font_description(font)

0 comments on commit 39b553f

Please sign in to comment.