Skip to content

Commit

Permalink
Use partial font instead of font face to create font cache keys
Browse files Browse the repository at this point in the history
The font face introduced by Pango 1.46 was appealing, but its description seems
to be broken. No easy way to know whether it’s broken in Pango, in FontConfig,
in our custom FontConfig configuration, or elsewhere. As it only happens with
@font-face rules, we can easily blame our FontConfig configuration.

Let’s try something else again! This key changes in each version…

Font descriptions have a nice way to unset values, allowing us to only keep
fields that should be unique by font file. This will probably lead us to other
subtle and frightening problems in the future, but we’ll let our dear users and
customers find them for us.

And we don’t require Pango 1.46 anymore to get smaller PDFs. So cool.

Fix #1778.
  • Loading branch information
liZe committed Dec 23, 2022
1 parent 0e9426f commit e2083e0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
5 changes: 2 additions & 3 deletions docs/first_steps.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ Ubuntu_, Fedora_, Archlinux_, Gentoo_…

If WeasyPrint is not available on your distribution, or if you want to use a
more recent version of WeasyPrint, you have to be sure that Python_ (at least
version 3.7.0) and Pango_ (at least version 1.44.0, 1.46.0 or newer is
preferred to get smaller documents) are installed on your system. You can
verify this by launching::
version 3.7.0) and Pango_ (at least version 1.44.0) are installed on your
system. You can verify this by launching::

python3 --version
pango-view --version
Expand Down
11 changes: 6 additions & 5 deletions weasyprint/pdf/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,12 @@ def set_blend_mode(self, mode):

@lru_cache()
def add_font(self, pango_font):
if pango.pango_version() > 14600:
pango_face = pango.pango_font_get_face(pango_font)
description = pango.pango_font_face_describe(pango_face)
else:
description = pango.pango_font_describe(pango_font)
description = pango.pango_font_describe(pango_font)
mask = (
pango.PANGO_FONT_MASK_SIZE +
pango.PANGO_FONT_MASK_GRAVITY +
pango.PANGO_FONT_MASK_VARIATIONS)
pango.pango_font_description_unset_fields(description, mask)
key = pango.pango_font_description_hash(description)
pango.pango_font_description_free(description)
if key not in self._fonts:
Expand Down
12 changes: 9 additions & 3 deletions weasyprint/text/ffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
typedef ... PangoAttrList;
typedef ... PangoAttrClass;
typedef ... PangoFont;
typedef ... PangoFontFace;
typedef guint PangoGlyph;
typedef gint PangoGlyphUnit;
Expand All @@ -72,6 +71,12 @@
PANGO_WEIGHT_ULTRAHEAVY = 1000
} PangoWeight;
typedef enum {
PANGO_FONT_MASK_SIZE = 1 << 5,
PANGO_FONT_MASK_GRAVITY = 1 << 6,
PANGO_FONT_MASK_VARIATIONS = 1 << 7
} PangoFontMask;
typedef enum {
PANGO_STRETCH_ULTRA_CONDENSED,
PANGO_STRETCH_EXTRA_CONDENSED,
Expand Down Expand Up @@ -249,6 +254,9 @@
const PangoFontDescription* desc);
int pango_font_description_get_size (PangoFontDescription *desc);
void pango_font_description_unset_fields (
PangoFontDescription* desc, PangoFontMask to_unset);
int pango_glyph_string_get_width (PangoGlyphString *glyphs);
char * pango_font_description_to_string (
const PangoFontDescription *desc);
Expand Down Expand Up @@ -280,8 +288,6 @@
void pango_font_get_glyph_extents (
PangoFont *font, PangoGlyph glyph, PangoRectangle *ink_rect,
PangoRectangle *logical_rect);
PangoFontFace* pango_font_get_face (PangoFont* font);
PangoFontDescription* pango_font_face_describe (PangoFontFace* face);
void pango_context_set_round_glyph_positions (
PangoContext *context, gboolean round_positions);
Expand Down

0 comments on commit e2083e0

Please sign in to comment.