Skip to content

Commit

Permalink
Handle small-caps synthesis
Browse files Browse the repository at this point in the history
Fix #123.
  • Loading branch information
liZe committed Jan 11, 2025
1 parent ccbfac5 commit 4f08647
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
6 changes: 4 additions & 2 deletions weasyprint/draw/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions weasyprint/text/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions weasyprint/text/ffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 (
Expand Down
6 changes: 4 additions & 2 deletions weasyprint/text/fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4f08647

Please sign in to comment.