diff --git a/weasyprint/draw/text.py b/weasyprint/draw/text.py index 84c1a1ef4..71f58a8f7 100644 --- a/weasyprint/draw/text.py +++ b/weasyprint/draw/text.py @@ -78,8 +78,7 @@ def draw_first_line(stream, textbox, text_overflow, block_ellipsis, matrix): if not textbox.text.strip(): return [] - font_size = textbox.style['font_size'] - if font_size < 1e-6: # default float precision used by pydyf + if textbox.style['font_size'] < 1e-6: # default float precision used by pydyf return [] pango.pango_layout_set_single_paragraph_mode(textbox.pango_layout.layout, True) @@ -143,6 +142,9 @@ def draw_first_line(stream, textbox, text_overflow, block_ellipsis, matrix): # Add font file content. pango_font = glyph_item.item.analysis.font + description = ffi.gc( + pango.pango_font_describe(pango_font), pango.pango_font_description_free) + font_size = pango.pango_font_description_get_size(description) * FROM_UNITS font = stream.add_font(pango_font) # Get positions of the glyphs in the UTF-8 string. diff --git a/weasyprint/text/constants.py b/weasyprint/text/constants.py index 8462abc5f..5095793b8 100644 --- a/weasyprint/text/constants.py +++ b/weasyprint/text/constants.py @@ -38,6 +38,15 @@ 'WRAP_CHAR': pango.PANGO_WRAP_CHAR, 'WRAP_WORD_CHAR': pango.PANGO_WRAP_WORD_CHAR } +PANGO_VARIANT = { + 'normal': pango.PANGO_VARIANT_NORMAL, + 'small-caps': pango.PANGO_VARIANT_SMALL_CAPS, + 'all-small-caps': pango.PANGO_VARIANT_ALL_SMALL_CAPS, + 'petite-caps': pango.PANGO_VARIANT_PETITE_CAPS, + 'all-petite-caps': pango.PANGO_VARIANT_ALL_PETITE_CAPS, + 'unicase': pango.PANGO_VARIANT_UNICASE, + 'titling-caps': pango.PANGO_VARIANT_TITLE_CAPS, +} # Language system tags # From https://docs.microsoft.com/typography/opentype/spec/languagetags diff --git a/weasyprint/text/ffi.py b/weasyprint/text/ffi.py index b6577541c..a3c41f46c 100644 --- a/weasyprint/text/ffi.py +++ b/weasyprint/text/ffi.py @@ -145,6 +145,16 @@ PANGO_WRAP_WORD_CHAR } PangoWrapMode; + typedef enum { + PANGO_VARIANT_NORMAL, + PANGO_VARIANT_SMALL_CAPS, + PANGO_VARIANT_ALL_SMALL_CAPS, + PANGO_VARIANT_PETITE_CAPS, + PANGO_VARIANT_ALL_PETITE_CAPS, + PANGO_VARIANT_UNICASE, + PANGO_VARIANT_TITLE_CAPS, + } PangoVariant; + typedef enum { PANGO_TAB_LEFT } PangoTabAlign; @@ -291,6 +301,8 @@ PangoFontDescription *desc, double size); void pango_font_description_set_variations ( PangoFontDescription* desc, const char* variations); + void pango_font_description_set_variant ( + PangoFontDescription* desc, PangoVariant variant); PangoStyle pango_font_description_get_style (const PangoFontDescription *desc); const char* pango_font_description_get_variations ( diff --git a/weasyprint/text/fonts.py b/weasyprint/text/fonts.py index 1fb305600..f7717f7c0 100644 --- a/weasyprint/text/fonts.py +++ b/weasyprint/text/fonts.py @@ -14,8 +14,8 @@ from ..urls import FILESYSTEM_ENCODING, fetch from .constants import ( # isort:skip - CAPS_KEYS, EAST_ASIAN_KEYS, FONTCONFIG_STRETCH, FONTCONFIG_STYLE, - FONTCONFIG_WEIGHT, LIGATURE_KEYS, NUMERIC_KEYS, PANGO_STRETCH, PANGO_STYLE) + CAPS_KEYS, EAST_ASIAN_KEYS, FONTCONFIG_STRETCH, FONTCONFIG_STYLE, FONTCONFIG_WEIGHT, + LIGATURE_KEYS, NUMERIC_KEYS, PANGO_STRETCH, PANGO_STYLE, PANGO_VARIANT) from .ffi import ( # isort:skip TO_UNITS, ffi, fontconfig, gobject, harfbuzz, pango, pangoft2, unicode_to_char_p) @@ -319,6 +319,8 @@ def get_font_description(style): pango.pango_font_description_set_weight(font_description, font_weight) font_size = int(style['font_size'] * TO_UNITS) pango.pango_font_description_set_absolute_size(font_description, font_size) + font_variant = PANGO_VARIANT[style['font_variant_caps']] + pango.pango_font_description_set_variant(font_description, font_variant) if style['font_variation_settings'] != 'normal': string = ','.join( f'{key}={value}' for key, value in