2012-03-22 02:19:27 +04:00
|
|
|
"""
|
|
|
|
weasyprint.text
|
|
|
|
---------------
|
2011-08-19 18:52:56 +04:00
|
|
|
|
2012-03-22 02:19:27 +04:00
|
|
|
Interface with Pango to decide where to do line breaks and to draw text.
|
2011-08-19 18:52:56 +04:00
|
|
|
|
2018-09-20 19:41:37 +03:00
|
|
|
:copyright: Copyright 2011-2018 Simon Sapin and contributors, see AUTHORS.
|
2012-03-22 02:19:27 +04:00
|
|
|
:license: BSD, see LICENSE for details.
|
2011-08-19 18:52:56 +04:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2016-09-26 13:15:12 +03:00
|
|
|
import re
|
|
|
|
|
2012-12-29 02:17:04 +04:00
|
|
|
import cairocffi as cairo
|
2017-03-25 02:33:36 +03:00
|
|
|
import cffi
|
|
|
|
import pyphen
|
2012-06-21 17:10:17 +04:00
|
|
|
|
2016-09-12 01:23:52 +03:00
|
|
|
from .logger import LOGGER
|
2011-10-13 20:23:49 +04:00
|
|
|
|
2018-03-19 00:37:56 +03:00
|
|
|
CAIRO_DUMMY_CONTEXT = {
|
|
|
|
True: cairo.Context(cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1)),
|
|
|
|
False: cairo.Context(cairo.PDFSurface(None, 1, 1))}
|
|
|
|
|
|
|
|
|
2012-12-29 02:17:04 +04:00
|
|
|
ffi = cffi.FFI()
|
|
|
|
ffi.cdef('''
|
2016-10-26 18:17:00 +03:00
|
|
|
// Cairo
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
CAIRO_FONT_TYPE_TOY,
|
|
|
|
CAIRO_FONT_TYPE_FT,
|
|
|
|
CAIRO_FONT_TYPE_WIN32,
|
|
|
|
CAIRO_FONT_TYPE_QUARTZ,
|
|
|
|
CAIRO_FONT_TYPE_USER
|
|
|
|
} cairo_font_type_t;
|
|
|
|
|
|
|
|
|
2016-10-01 21:38:33 +03:00
|
|
|
// Pango
|
|
|
|
|
2016-10-26 19:15:32 +03:00
|
|
|
typedef unsigned int guint;
|
|
|
|
typedef int gint;
|
|
|
|
typedef char gchar;
|
|
|
|
typedef gint gboolean;
|
|
|
|
typedef void* gpointer;
|
|
|
|
typedef ... cairo_t;
|
|
|
|
typedef ... PangoLayout;
|
|
|
|
typedef ... PangoContext;
|
|
|
|
typedef ... PangoFontMap;
|
|
|
|
typedef ... PangoFontMetrics;
|
|
|
|
typedef ... PangoLanguage;
|
|
|
|
typedef ... PangoTabArray;
|
|
|
|
typedef ... PangoFontDescription;
|
|
|
|
typedef ... PangoLayoutIter;
|
|
|
|
typedef ... PangoAttrList;
|
|
|
|
typedef ... PangoAttrClass;
|
|
|
|
|
2012-12-29 02:17:04 +04:00
|
|
|
typedef enum {
|
2013-04-08 19:22:14 +04:00
|
|
|
PANGO_STYLE_NORMAL,
|
|
|
|
PANGO_STYLE_OBLIQUE,
|
|
|
|
PANGO_STYLE_ITALIC
|
2012-12-29 02:17:04 +04:00
|
|
|
} PangoStyle;
|
|
|
|
|
|
|
|
typedef enum {
|
2013-04-08 19:22:14 +04:00
|
|
|
PANGO_WEIGHT_THIN = 100,
|
|
|
|
PANGO_WEIGHT_ULTRALIGHT = 200,
|
|
|
|
PANGO_WEIGHT_LIGHT = 300,
|
|
|
|
PANGO_WEIGHT_BOOK = 380,
|
|
|
|
PANGO_WEIGHT_NORMAL = 400,
|
|
|
|
PANGO_WEIGHT_MEDIUM = 500,
|
|
|
|
PANGO_WEIGHT_SEMIBOLD = 600,
|
|
|
|
PANGO_WEIGHT_BOLD = 700,
|
|
|
|
PANGO_WEIGHT_ULTRABOLD = 800,
|
|
|
|
PANGO_WEIGHT_HEAVY = 900,
|
|
|
|
PANGO_WEIGHT_ULTRAHEAVY = 1000
|
2012-12-29 02:17:04 +04:00
|
|
|
} PangoWeight;
|
|
|
|
|
|
|
|
typedef enum {
|
2013-04-08 19:22:14 +04:00
|
|
|
PANGO_STRETCH_ULTRA_CONDENSED,
|
|
|
|
PANGO_STRETCH_EXTRA_CONDENSED,
|
|
|
|
PANGO_STRETCH_CONDENSED,
|
|
|
|
PANGO_STRETCH_SEMI_CONDENSED,
|
|
|
|
PANGO_STRETCH_NORMAL,
|
|
|
|
PANGO_STRETCH_SEMI_EXPANDED,
|
|
|
|
PANGO_STRETCH_EXPANDED,
|
|
|
|
PANGO_STRETCH_EXTRA_EXPANDED,
|
|
|
|
PANGO_STRETCH_ULTRA_EXPANDED
|
2012-12-29 02:17:04 +04:00
|
|
|
} PangoStretch;
|
2014-04-27 15:29:55 +04:00
|
|
|
|
2013-09-30 22:16:03 +04:00
|
|
|
typedef enum {
|
|
|
|
PANGO_WRAP_WORD,
|
|
|
|
PANGO_WRAP_CHAR,
|
|
|
|
PANGO_WRAP_WORD_CHAR
|
|
|
|
} PangoWrapMode;
|
2012-12-29 02:17:04 +04:00
|
|
|
|
2016-08-20 21:11:03 +03:00
|
|
|
typedef enum {
|
|
|
|
PANGO_TAB_LEFT
|
|
|
|
} PangoTabAlign;
|
|
|
|
|
2012-12-29 17:20:38 +04:00
|
|
|
typedef struct {
|
|
|
|
const PangoAttrClass *klass;
|
|
|
|
guint start_index;
|
|
|
|
guint end_index;
|
|
|
|
} PangoAttribute;
|
2016-10-26 19:15:32 +03:00
|
|
|
|
2012-12-29 02:17:04 +04:00
|
|
|
typedef struct {
|
|
|
|
PangoLayout *layout;
|
|
|
|
gint start_index;
|
|
|
|
gint length;
|
|
|
|
/* ... */
|
|
|
|
} PangoLayoutLine;
|
|
|
|
|
2016-10-26 19:15:32 +03:00
|
|
|
typedef struct {
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
} PangoRectangle;
|
2012-12-29 02:17:04 +04:00
|
|
|
|
2017-10-24 09:47:22 +03:00
|
|
|
typedef struct {
|
|
|
|
guint is_line_break: 1;
|
|
|
|
/* ... */
|
|
|
|
} PangoLogAttr;
|
|
|
|
|
2016-10-26 20:11:21 +03:00
|
|
|
int pango_version (void);
|
|
|
|
|
2016-10-26 19:15:32 +03:00
|
|
|
double pango_units_to_double (int i);
|
|
|
|
int pango_units_from_double (double d);
|
|
|
|
void g_object_unref (gpointer object);
|
|
|
|
void g_type_init (void);
|
2012-12-29 02:17:04 +04:00
|
|
|
|
2012-12-29 17:20:38 +04:00
|
|
|
void pango_layout_set_width (PangoLayout *layout, int width);
|
2016-10-26 19:15:32 +03:00
|
|
|
void pango_layout_set_attributes (
|
2013-03-30 23:55:23 +04:00
|
|
|
PangoLayout *layout, PangoAttrList *attrs);
|
2012-12-29 02:17:04 +04:00
|
|
|
void pango_layout_set_text (
|
|
|
|
PangoLayout *layout, const char *text, int length);
|
2016-08-20 21:11:03 +03:00
|
|
|
void pango_layout_set_tabs (
|
|
|
|
PangoLayout *layout, PangoTabArray *tabs);
|
2012-12-29 17:20:38 +04:00
|
|
|
void pango_layout_set_font_description (
|
|
|
|
PangoLayout *layout, const PangoFontDescription *desc);
|
2013-09-30 22:16:03 +04:00
|
|
|
void pango_layout_set_wrap (
|
|
|
|
PangoLayout *layout, PangoWrapMode wrap);
|
2012-12-29 02:17:04 +04:00
|
|
|
|
2016-10-26 19:15:32 +03:00
|
|
|
PangoLayoutIter * pango_layout_get_iter (PangoLayout *layout);
|
|
|
|
void pango_layout_iter_free (PangoLayoutIter *iter);
|
|
|
|
gboolean pango_layout_iter_next_line (PangoLayoutIter *iter);
|
|
|
|
PangoLayoutLine * pango_layout_iter_get_line_readonly (
|
|
|
|
PangoLayoutIter *iter);
|
|
|
|
int pango_layout_iter_get_baseline (PangoLayoutIter *iter);
|
2012-12-29 02:17:04 +04:00
|
|
|
|
|
|
|
PangoFontDescription * pango_font_description_new (void);
|
|
|
|
void pango_font_description_free (PangoFontDescription *desc);
|
|
|
|
void pango_font_description_set_family (
|
|
|
|
PangoFontDescription *desc, const char *family);
|
|
|
|
void pango_font_description_set_style (
|
|
|
|
PangoFontDescription *desc, PangoStyle style);
|
|
|
|
void pango_font_description_set_stretch (
|
|
|
|
PangoFontDescription *desc, PangoStretch stretch);
|
|
|
|
void pango_font_description_set_weight (
|
|
|
|
PangoFontDescription *desc, PangoWeight weight);
|
|
|
|
void pango_font_description_set_absolute_size (
|
|
|
|
PangoFontDescription *desc, double size);
|
|
|
|
|
2016-10-26 19:15:32 +03:00
|
|
|
PangoFontMetrics * pango_context_get_metrics (
|
|
|
|
PangoContext *context, const PangoFontDescription *desc,
|
|
|
|
PangoLanguage *language);
|
|
|
|
void pango_font_metrics_unref (PangoFontMetrics *metrics);
|
|
|
|
int pango_font_metrics_get_ascent (PangoFontMetrics *metrics);
|
|
|
|
int pango_font_metrics_get_descent (PangoFontMetrics *metrics);
|
|
|
|
int pango_font_metrics_get_approximate_char_width (
|
|
|
|
PangoFontMetrics *metrics);
|
|
|
|
int pango_font_metrics_get_approximate_digit_width (
|
|
|
|
PangoFontMetrics *metrics);
|
|
|
|
int pango_font_metrics_get_underline_thickness (
|
|
|
|
PangoFontMetrics *metrics);
|
|
|
|
int pango_font_metrics_get_underline_position (
|
|
|
|
PangoFontMetrics *metrics);
|
|
|
|
int pango_font_metrics_get_strikethrough_thickness (
|
|
|
|
PangoFontMetrics *metrics);
|
|
|
|
int pango_font_metrics_get_strikethrough_position (
|
|
|
|
PangoFontMetrics *metrics);
|
|
|
|
|
|
|
|
PangoAttrList * pango_attr_list_new (void);
|
|
|
|
void pango_attr_list_unref (PangoAttrList *list);
|
|
|
|
void pango_attr_list_insert (
|
2012-12-29 17:20:38 +04:00
|
|
|
PangoAttrList *list, PangoAttribute *attr);
|
2016-10-26 19:15:32 +03:00
|
|
|
PangoAttribute * pango_attr_font_features_new (const gchar *features);
|
|
|
|
PangoAttribute * pango_attr_letter_spacing_new (int letter_spacing);
|
|
|
|
void pango_attribute_destroy (PangoAttribute *attr);
|
2012-12-29 17:20:38 +04:00
|
|
|
|
2016-10-26 19:15:32 +03:00
|
|
|
PangoTabArray * pango_tab_array_new_with_positions (
|
2016-08-20 21:11:03 +03:00
|
|
|
gint size, gboolean positions_in_pixels, PangoTabAlign first_alignment,
|
|
|
|
gint first_position, ...);
|
|
|
|
void pango_tab_array_free (PangoTabArray *tab_array);
|
2012-12-29 17:20:38 +04:00
|
|
|
|
2016-10-26 19:15:32 +03:00
|
|
|
PangoLanguage * pango_language_from_string (const char *language);
|
|
|
|
PangoLanguage * pango_language_get_default (void);
|
|
|
|
void pango_context_set_language (
|
2016-09-13 06:02:36 +03:00
|
|
|
PangoContext *context, PangoLanguage *language);
|
2016-10-27 18:36:24 +03:00
|
|
|
void pango_context_set_font_map (
|
|
|
|
PangoContext *context, PangoFontMap *font_map);
|
2012-12-29 02:17:04 +04:00
|
|
|
|
|
|
|
void pango_layout_line_get_extents (
|
|
|
|
PangoLayoutLine *line,
|
|
|
|
PangoRectangle *ink_rect, PangoRectangle *logical_rect);
|
|
|
|
|
2016-10-26 19:15:32 +03:00
|
|
|
PangoContext * pango_layout_get_context (PangoLayout *layout);
|
2013-05-17 20:52:05 +04:00
|
|
|
|
2017-10-24 09:47:22 +03:00
|
|
|
void pango_get_log_attrs (
|
|
|
|
const char *text, int length, int level, PangoLanguage *language,
|
|
|
|
PangoLogAttr *log_attrs, int attrs_len);
|
|
|
|
|
2013-05-17 20:52:05 +04:00
|
|
|
|
2016-10-26 18:17:00 +03:00
|
|
|
// PangoCairo
|
2016-09-23 20:00:14 +03:00
|
|
|
|
2016-10-26 18:17:00 +03:00
|
|
|
PangoLayout * pango_cairo_create_layout (cairo_t *cr);
|
2012-12-29 04:00:30 +04:00
|
|
|
void pango_cairo_update_layout (cairo_t *cr, PangoLayout *layout);
|
|
|
|
void pango_cairo_show_layout_line (cairo_t *cr, PangoLayoutLine *line);
|
2012-12-29 02:17:04 +04:00
|
|
|
''')
|
2013-02-26 18:05:53 +04:00
|
|
|
|
2012-06-21 17:10:17 +04:00
|
|
|
|
2013-02-26 18:05:53 +04:00
|
|
|
def dlopen(ffi, *names):
|
2013-03-08 18:09:45 +04:00
|
|
|
"""Try various names for the same library, for different platforms."""
|
2013-02-26 18:05:53 +04:00
|
|
|
for name in names:
|
|
|
|
try:
|
|
|
|
return ffi.dlopen(name)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
# Re-raise the exception.
|
|
|
|
return ffi.dlopen(names[0]) # pragma: no cover
|
2012-05-31 03:14:15 +04:00
|
|
|
|
2012-07-23 20:52:19 +04:00
|
|
|
|
2014-05-20 11:53:06 +04:00
|
|
|
gobject = dlopen(ffi, 'gobject-2.0', 'libgobject-2.0-0', 'libgobject-2.0.so',
|
2014-04-27 15:29:55 +04:00
|
|
|
'libgobject-2.0.dylib')
|
2014-05-20 11:53:06 +04:00
|
|
|
pango = dlopen(ffi, 'pango-1.0', 'libpango-1.0-0', 'libpango-1.0.so',
|
|
|
|
'libpango-1.0.dylib')
|
2014-04-27 15:29:55 +04:00
|
|
|
pangocairo = dlopen(ffi, 'pangocairo-1.0', 'libpangocairo-1.0-0',
|
2014-05-20 11:53:06 +04:00
|
|
|
'libpangocairo-1.0.so', 'libpangocairo-1.0.dylib')
|
2012-07-23 20:52:19 +04:00
|
|
|
|
2013-03-08 18:09:45 +04:00
|
|
|
gobject.g_type_init()
|
|
|
|
|
2012-12-29 02:17:04 +04:00
|
|
|
units_to_double = pango.pango_units_to_double
|
|
|
|
units_from_double = pango.pango_units_from_double
|
|
|
|
|
|
|
|
|
2013-04-08 19:22:14 +04:00
|
|
|
PANGO_STYLE = {
|
|
|
|
'normal': pango.PANGO_STYLE_NORMAL,
|
|
|
|
'oblique': pango.PANGO_STYLE_OBLIQUE,
|
|
|
|
'italic': pango.PANGO_STYLE_ITALIC,
|
|
|
|
}
|
|
|
|
|
|
|
|
PANGO_STRETCH = {
|
|
|
|
'ultra-condensed': pango.PANGO_STRETCH_ULTRA_CONDENSED,
|
|
|
|
'extra-condensed': pango.PANGO_STRETCH_EXTRA_CONDENSED,
|
|
|
|
'condensed': pango.PANGO_STRETCH_CONDENSED,
|
|
|
|
'semi-condensed': pango.PANGO_STRETCH_SEMI_CONDENSED,
|
|
|
|
'normal': pango.PANGO_STRETCH_NORMAL,
|
|
|
|
'semi-expanded': pango.PANGO_STRETCH_SEMI_EXPANDED,
|
|
|
|
'expanded': pango.PANGO_STRETCH_EXPANDED,
|
|
|
|
'extra-expanded': pango.PANGO_STRETCH_EXTRA_EXPANDED,
|
|
|
|
'ultra-expanded': pango.PANGO_STRETCH_ULTRA_EXPANDED,
|
|
|
|
}
|
2012-12-29 02:17:04 +04:00
|
|
|
|
2013-09-30 22:16:03 +04:00
|
|
|
PANGO_WRAP_MODE = {
|
2014-04-27 15:29:55 +04:00
|
|
|
'WRAP_WORD': pango.PANGO_WRAP_WORD,
|
|
|
|
'WRAP_CHAR': pango.PANGO_WRAP_CHAR,
|
|
|
|
'WRAP_WORD_CHAR': pango.PANGO_WRAP_WORD_CHAR
|
2013-09-30 22:16:03 +04:00
|
|
|
}
|
|
|
|
|
2016-09-13 06:02:36 +03:00
|
|
|
# From http://www.microsoft.com/typography/otspec/languagetags.htm
|
|
|
|
LST_TO_ISO = {
|
|
|
|
'aba': 'abq',
|
|
|
|
'afk': 'afr',
|
|
|
|
'afr': 'aar',
|
|
|
|
'agw': 'ahg',
|
|
|
|
'als': 'gsw',
|
|
|
|
'alt': 'atv',
|
|
|
|
'ari': 'aiw',
|
|
|
|
'ark': 'mhv',
|
|
|
|
'ath': 'apk',
|
|
|
|
'avr': 'ava',
|
|
|
|
'bad': 'bfq',
|
|
|
|
'bad0': 'bad',
|
|
|
|
'bag': 'bfy',
|
|
|
|
'bal': 'krc',
|
|
|
|
'bau': 'bci',
|
|
|
|
'bch': 'bcq',
|
|
|
|
'bgr': 'bul',
|
|
|
|
'bil': 'byn',
|
|
|
|
'bkf': 'bla',
|
|
|
|
'bli': 'bal',
|
|
|
|
'bln': 'bjt',
|
|
|
|
'blt': 'bft',
|
|
|
|
'bmb': 'bam',
|
|
|
|
'bri': 'bra',
|
|
|
|
'brm': 'mya',
|
|
|
|
'bsh': 'bak',
|
|
|
|
'bti': 'btb',
|
|
|
|
'chg': 'sgw',
|
|
|
|
'chh': 'hne',
|
|
|
|
'chi': 'nya',
|
|
|
|
'chk': 'ckt',
|
|
|
|
'chk0': 'chk',
|
|
|
|
'chu': 'chv',
|
|
|
|
'chy': 'chy',
|
|
|
|
'cmr': 'swb',
|
|
|
|
'crr': 'crx',
|
|
|
|
'crt': 'crh',
|
|
|
|
'csl': 'chu',
|
|
|
|
'csy': 'ces',
|
|
|
|
'dcr': 'cwd',
|
|
|
|
'dgr': 'doi',
|
|
|
|
'djr': 'dje',
|
|
|
|
'djr0': 'djr',
|
|
|
|
'dng': 'ada',
|
|
|
|
'dnk': 'din',
|
|
|
|
'dri': 'prs',
|
|
|
|
'dun': 'dng',
|
|
|
|
'dzn': 'dzo',
|
|
|
|
'ebi': 'igb',
|
|
|
|
'ecr': 'crj',
|
|
|
|
'edo': 'bin',
|
|
|
|
'erz': 'myv',
|
|
|
|
'esp': 'spa',
|
|
|
|
'eti': 'est',
|
|
|
|
'euq': 'eus',
|
|
|
|
'evk': 'evn',
|
|
|
|
'evn': 'eve',
|
|
|
|
'fan': 'acf',
|
|
|
|
'fan0': 'fan',
|
|
|
|
'far': 'fas',
|
|
|
|
'fji': 'fij',
|
|
|
|
'fle': 'vls',
|
|
|
|
'fne': 'enf',
|
|
|
|
'fos': 'fao',
|
|
|
|
'fri': 'fry',
|
|
|
|
'frl': 'fur',
|
|
|
|
'frp': 'frp',
|
|
|
|
'fta': 'fuf',
|
|
|
|
'gad': 'gaa',
|
|
|
|
'gae': 'gla',
|
|
|
|
'gal': 'glg',
|
|
|
|
'gaw': 'gbm',
|
|
|
|
'gil': 'niv',
|
|
|
|
'gil0': 'gil',
|
|
|
|
'gmz': 'guk',
|
|
|
|
'grn': 'kal',
|
|
|
|
'gro': 'grt',
|
|
|
|
'gua': 'grn',
|
|
|
|
'hai': 'hat',
|
|
|
|
'hal': 'flm',
|
|
|
|
'har': 'hoj',
|
|
|
|
'hbn': 'amf',
|
|
|
|
'hma': 'mrj',
|
|
|
|
'hnd': 'hno',
|
|
|
|
'ho': 'hoc',
|
|
|
|
'hri': 'har',
|
|
|
|
'hye0': 'hye',
|
|
|
|
'ijo': 'ijc',
|
|
|
|
'ing': 'inh',
|
|
|
|
'inu': 'iku',
|
|
|
|
'iri': 'gle',
|
|
|
|
'irt': 'gle',
|
|
|
|
'ism': 'smn',
|
|
|
|
'iwr': 'heb',
|
|
|
|
'jan': 'jpn',
|
|
|
|
'jii': 'yid',
|
|
|
|
'jud': 'lad',
|
|
|
|
'jul': 'dyu',
|
|
|
|
'kab': 'kbd',
|
|
|
|
'kab0': 'kab',
|
|
|
|
'kac': 'kfr',
|
|
|
|
'kal': 'kln',
|
|
|
|
'kar': 'krc',
|
|
|
|
'keb': 'ktb',
|
|
|
|
'kge': 'kat',
|
|
|
|
'kha': 'kjh',
|
|
|
|
'khk': 'kca',
|
|
|
|
'khs': 'kca',
|
|
|
|
'khv': 'kca',
|
|
|
|
'kis': 'kqs',
|
|
|
|
'kkn': 'kex',
|
|
|
|
'klm': 'xal',
|
|
|
|
'kmb': 'kam',
|
|
|
|
'kmn': 'kfy',
|
|
|
|
'kmo': 'kmw',
|
|
|
|
'kms': 'kxc',
|
|
|
|
'knr': 'kau',
|
|
|
|
'kod': 'kfa',
|
|
|
|
'koh': 'okm',
|
|
|
|
'kon': 'ktu',
|
|
|
|
'kon0': 'kon',
|
|
|
|
'kop': 'koi',
|
|
|
|
'koz': 'kpv',
|
|
|
|
'kpl': 'kpe',
|
|
|
|
'krk': 'kaa',
|
|
|
|
'krm': 'kdr',
|
|
|
|
'krn': 'kar',
|
|
|
|
'krt': 'kqy',
|
|
|
|
'ksh': 'kas',
|
|
|
|
'ksh0': 'ksh',
|
|
|
|
'ksi': 'kha',
|
|
|
|
'ksm': 'sjd',
|
|
|
|
'kui': 'kxu',
|
|
|
|
'kul': 'kfx',
|
|
|
|
'kuu': 'kru',
|
|
|
|
'kuy': 'kdt',
|
|
|
|
'kyk': 'kpy',
|
|
|
|
'lad': 'lld',
|
|
|
|
'lah': 'bfu',
|
|
|
|
'lak': 'lbe',
|
|
|
|
'lam': 'lmn',
|
|
|
|
'laz': 'lzz',
|
|
|
|
'lcr': 'crm',
|
|
|
|
'ldk': 'lbj',
|
|
|
|
'lma': 'mhr',
|
|
|
|
'lmb': 'lif',
|
|
|
|
'lmw': 'ngl',
|
|
|
|
'lsb': 'dsb',
|
|
|
|
'lsm': 'smj',
|
|
|
|
'lth': 'lit',
|
|
|
|
'luh': 'luy',
|
|
|
|
'lvi': 'lav',
|
|
|
|
'maj': 'mpe',
|
|
|
|
'mak': 'vmw',
|
|
|
|
'man': 'mns',
|
|
|
|
'map': 'arn',
|
|
|
|
'maw': 'mwr',
|
|
|
|
'mbn': 'kmb',
|
|
|
|
'mch': 'mnc',
|
|
|
|
'mcr': 'crm',
|
|
|
|
'mde': 'men',
|
|
|
|
'men': 'mym',
|
|
|
|
'miz': 'lus',
|
|
|
|
'mkr': 'mak',
|
|
|
|
'mle': 'mdy',
|
|
|
|
'mln': 'mlq',
|
|
|
|
'mlr': 'mal',
|
|
|
|
'mly': 'msa',
|
|
|
|
'mnd': 'mnk',
|
|
|
|
'mng': 'mon',
|
|
|
|
'mnk': 'man',
|
|
|
|
'mnx': 'glv',
|
|
|
|
'mok': 'mdf',
|
|
|
|
'mon': 'mnw',
|
|
|
|
'mth': 'mai',
|
|
|
|
'mts': 'mlt',
|
|
|
|
'mun': 'unr',
|
|
|
|
'nan': 'gld',
|
|
|
|
'nas': 'nsk',
|
|
|
|
'ncr': 'csw',
|
|
|
|
'ndg': 'ndo',
|
|
|
|
'nhc': 'csw',
|
|
|
|
'nis': 'dap',
|
|
|
|
'nkl': 'nyn',
|
|
|
|
'nko': 'nqo',
|
|
|
|
'nor': 'nob',
|
|
|
|
'nsm': 'sme',
|
|
|
|
'nta': 'nod',
|
|
|
|
'nto': 'epo',
|
|
|
|
'nyn': 'nno',
|
|
|
|
'ocr': 'ojs',
|
|
|
|
'ojb': 'oji',
|
|
|
|
'oro': 'orm',
|
|
|
|
'paa': 'sam',
|
|
|
|
'pal': 'pli',
|
|
|
|
'pap': 'plp',
|
|
|
|
'pap0': 'pap',
|
|
|
|
'pas': 'pus',
|
|
|
|
'pgr': 'ell',
|
|
|
|
'pil': 'fil',
|
|
|
|
'plg': 'pce',
|
|
|
|
'plk': 'pol',
|
|
|
|
'ptg': 'por',
|
|
|
|
'qin': 'bgr',
|
|
|
|
'rbu': 'bxr',
|
|
|
|
'rcr': 'atj',
|
|
|
|
'rms': 'roh',
|
|
|
|
'rom': 'ron',
|
|
|
|
'roy': 'rom',
|
|
|
|
'rsy': 'rue',
|
|
|
|
'rua': 'kin',
|
|
|
|
'sad': 'sck',
|
|
|
|
'say': 'chp',
|
|
|
|
'sek': 'xan',
|
|
|
|
'sel': 'sel',
|
|
|
|
'sgo': 'sag',
|
|
|
|
'sgs': 'sgs',
|
|
|
|
'sib': 'sjo',
|
|
|
|
'sig': 'xst',
|
|
|
|
'sks': 'sms',
|
|
|
|
'sky': 'slk',
|
|
|
|
'sla': 'scs',
|
|
|
|
'sml': 'som',
|
|
|
|
'sna': 'seh',
|
|
|
|
'sna0': 'sna',
|
|
|
|
'snh': 'sin',
|
|
|
|
'sog': 'gru',
|
|
|
|
'srb': 'srp',
|
|
|
|
'ssl': 'xsl',
|
|
|
|
'ssm': 'sma',
|
|
|
|
'sur': 'suq',
|
|
|
|
'sve': 'swe',
|
|
|
|
'swa': 'aii',
|
|
|
|
'swk': 'swa',
|
|
|
|
'swz': 'ssw',
|
|
|
|
'sxt': 'ngo',
|
|
|
|
'taj': 'tgk',
|
|
|
|
'tcr': 'cwd',
|
|
|
|
'tgn': 'ton',
|
|
|
|
'tgr': 'tig',
|
|
|
|
'tgy': 'tir',
|
|
|
|
'tht': 'tah',
|
|
|
|
'tib': 'bod',
|
|
|
|
'tkm': 'tuk',
|
|
|
|
'tmn': 'tem',
|
|
|
|
'tna': 'tsn',
|
|
|
|
'tne': 'enh',
|
|
|
|
'tng': 'toi',
|
|
|
|
'tod': 'xal',
|
|
|
|
'tod0': 'tod',
|
|
|
|
'trk': 'tur',
|
|
|
|
'tsg': 'tso',
|
|
|
|
'tua': 'tru',
|
|
|
|
'tul': 'tcy',
|
|
|
|
'tuv': 'tyv',
|
|
|
|
'twi': 'aka',
|
|
|
|
'usb': 'hsb',
|
|
|
|
'uyg': 'uig',
|
|
|
|
'vit': 'vie',
|
|
|
|
'vro': 'vro',
|
|
|
|
'wa': 'wbm',
|
|
|
|
'wag': 'wbr',
|
|
|
|
'wcr': 'crk',
|
|
|
|
'wel': 'cym',
|
|
|
|
'wlf': 'wol',
|
|
|
|
'xbd': 'khb',
|
|
|
|
'xhs': 'xho',
|
|
|
|
'yak': 'sah',
|
|
|
|
'yba': 'yor',
|
|
|
|
'ycr': 'cre',
|
|
|
|
'yim': 'iii',
|
|
|
|
'zhh': 'zho',
|
|
|
|
'zhp': 'zho',
|
|
|
|
'zhs': 'zho',
|
|
|
|
'zht': 'zho',
|
|
|
|
'znd': 'zne',
|
|
|
|
}
|
|
|
|
|
2012-12-29 02:17:04 +04:00
|
|
|
|
2013-03-02 04:33:37 +04:00
|
|
|
def utf8_slice(string, slice_):
|
|
|
|
return string.encode('utf-8')[slice_].decode('utf-8')
|
|
|
|
|
|
|
|
|
2012-12-29 02:17:04 +04:00
|
|
|
def unicode_to_char_p(string):
|
2013-06-19 13:04:37 +04:00
|
|
|
"""Return ``(pointer, bytestring)``.
|
|
|
|
|
|
|
|
The byte string must live at least as long as the pointer is used.
|
|
|
|
|
|
|
|
"""
|
2012-12-29 17:20:38 +04:00
|
|
|
bytestring = string.encode('utf8').replace(b'\x00', b'')
|
|
|
|
return ffi.new('char[]', bytestring), bytestring
|
2012-12-29 02:17:04 +04:00
|
|
|
|
|
|
|
|
2016-08-20 17:23:22 +03:00
|
|
|
def get_size(line, style):
|
2012-12-29 02:17:04 +04:00
|
|
|
logical_extents = ffi.new('PangoRectangle *')
|
|
|
|
pango.pango_layout_line_get_extents(line, ffi.NULL, logical_extents)
|
2016-08-20 17:23:22 +03:00
|
|
|
width, height = (units_to_double(logical_extents.width),
|
|
|
|
units_to_double(logical_extents.height))
|
2017-07-28 14:35:06 +03:00
|
|
|
if style['letter_spacing'] != 'normal':
|
|
|
|
width += style['letter_spacing']
|
2016-08-20 17:23:22 +03:00
|
|
|
return width, height
|
2012-12-29 02:17:04 +04:00
|
|
|
|
|
|
|
|
2013-04-04 04:38:39 +04:00
|
|
|
def get_ink_position(line):
|
|
|
|
ink_extents = ffi.new('PangoRectangle *')
|
|
|
|
pango.pango_layout_line_get_extents(line, ink_extents, ffi.NULL)
|
|
|
|
return (units_to_double(ink_extents.x), units_to_double(ink_extents.y))
|
|
|
|
|
|
|
|
|
2016-05-08 17:44:57 +03:00
|
|
|
def first_line_metrics(first_line, text, layout, resume_at, space_collapse,
|
2016-08-20 16:26:16 +03:00
|
|
|
style, hyphenated=False, hyphenation_character=None):
|
2013-03-02 04:33:37 +04:00
|
|
|
length = first_line.length
|
2016-08-19 17:42:52 +03:00
|
|
|
if hyphenated:
|
|
|
|
length -= len(hyphenation_character.encode('utf8'))
|
|
|
|
elif resume_at:
|
2017-06-03 16:14:27 +03:00
|
|
|
# Set an infinite width as we don't want to break lines when drawing,
|
|
|
|
# the lines have already been split and the size may differ.
|
|
|
|
pango.pango_layout_set_width(layout.layout, -1)
|
2016-05-08 17:44:57 +03:00
|
|
|
# Create layout with final text
|
|
|
|
first_line_text = utf8_slice(text, slice(length))
|
|
|
|
# Remove trailing spaces if spaces collapse
|
|
|
|
if space_collapse:
|
2016-08-19 18:50:24 +03:00
|
|
|
first_line_text = first_line_text.rstrip(u' ')
|
2016-08-19 17:42:52 +03:00
|
|
|
# Remove soft hyphens
|
2016-08-19 18:50:24 +03:00
|
|
|
layout.set_text(first_line_text.replace(u'\u00ad', u''))
|
Don't split a line when drawing it
To draw a text line, the previous behaviour didn't rely only on the text
actually set on the layout, but also relied on the fact that the line
was cut again when drawn. This change removes the line cutting when
drawing, and thus only relies on the line splitting done during the
layout. This fixes a bug causing some words not being displayed at the
end of a text line drawn with hinting, and the actual drawing size with
hinting was bigger than the size calculated during the layout.
The text included in the drawn layout object was sometimes not cut at
the right position, it was longer but cut when actually drawn. This
commit also fixes this, by always setting the right text in the layout
object.
Fixing this bug enables us to remove a hack introduced to fight against
an "accumulation of floating point errors". I now think that "it wasn't
our war"™. I think that the real reasons of this hack were probably:
- a problem with trailing spaces in the shrink-to-fit functions fixed in
commit 3a620db, and
- this line-cutting bug while drawing, fixed now.
I've tried hard to reproduce the shink-to-fit problem without this hack,
with no success. I don't see anymore how it can theorically happen with
the current code of the "preferred" module. The only bug fixed by this
hack that I've been able to reproduce is the hinting problem explained
in the first paragraph, and this bug is now really fixed.
Moreover, this hack used to cause another problem: as the maximum size
allowed to an inline block was actually bigger than the real size
available in the line, an inline block whose size was between the real
and the allowed sizes was put on a new line by the split_inline_box
function. This commit fixes #288, the bug reported about this problem.
2016-01-01 18:48:15 +03:00
|
|
|
first_line = next(layout.iter_lines(), None)
|
|
|
|
length = first_line.length if first_line is not None else 0
|
2016-08-19 17:42:52 +03:00
|
|
|
soft_hyphens = 0
|
2016-08-19 18:50:24 +03:00
|
|
|
if u'\u00ad' in first_line_text:
|
2017-06-03 16:40:13 +03:00
|
|
|
if first_line_text[0] == u'\u00ad':
|
|
|
|
length += 2 # len(u'\u00ad'.encode('utf8'))
|
2016-08-19 17:42:52 +03:00
|
|
|
for i in range(len(layout.text_bytes.decode('utf8'))):
|
|
|
|
while i + soft_hyphens + 1 < len(first_line_text):
|
2016-08-19 18:50:24 +03:00
|
|
|
if first_line_text[i + soft_hyphens + 1] == u'\u00ad':
|
2016-08-19 17:42:52 +03:00
|
|
|
soft_hyphens += 1
|
|
|
|
else:
|
|
|
|
break
|
2016-08-19 18:50:24 +03:00
|
|
|
length += soft_hyphens * 2 # len(u'\u00ad'.encode('utf8'))
|
2016-08-20 17:23:22 +03:00
|
|
|
width, height = get_size(first_line, style)
|
2013-03-02 04:33:37 +04:00
|
|
|
baseline = units_to_double(pango.pango_layout_iter_get_baseline(ffi.gc(
|
|
|
|
pango.pango_layout_get_iter(layout.layout),
|
|
|
|
pango.pango_layout_iter_free)))
|
|
|
|
return layout, length, resume_at, width, height, baseline
|
|
|
|
|
|
|
|
|
2012-12-29 02:17:04 +04:00
|
|
|
class Layout(object):
|
|
|
|
"""Object holding PangoLayout-related cdata pointers."""
|
2016-10-27 18:36:24 +03:00
|
|
|
def __init__(self, context, font_size, style):
|
2018-04-30 23:57:47 +03:00
|
|
|
from .fonts import ZERO_FONTSIZE_CRASHES_CAIRO
|
|
|
|
|
2018-03-31 19:10:51 +03:00
|
|
|
# Cairo crashes with font-size: 0 when using Win32 API
|
|
|
|
# See https://github.com/Kozea/WeasyPrint/pull/599
|
2018-04-30 23:57:47 +03:00
|
|
|
if font_size == 0 and ZERO_FONTSIZE_CRASHES_CAIRO:
|
2018-03-31 19:10:51 +03:00
|
|
|
font_size = 1
|
2016-10-27 18:36:24 +03:00
|
|
|
self.context = context
|
|
|
|
hinting = context.enable_hinting if context else False
|
2013-04-04 06:39:25 +04:00
|
|
|
self.layout = ffi.gc(
|
|
|
|
pangocairo.pango_cairo_create_layout(ffi.cast(
|
2018-03-19 00:37:56 +03:00
|
|
|
'cairo_t *', CAIRO_DUMMY_CONTEXT[hinting]._pointer)),
|
2013-04-04 06:39:25 +04:00
|
|
|
gobject.g_object_unref)
|
2016-10-27 18:36:24 +03:00
|
|
|
pango_context = pango.pango_layout_get_context(self.layout)
|
|
|
|
if context and context.font_config.font_map:
|
|
|
|
pango.pango_context_set_font_map(
|
|
|
|
pango_context, context.font_config.font_map)
|
|
|
|
self.font = ffi.gc(
|
2013-04-04 06:39:25 +04:00
|
|
|
pango.pango_font_description_new(),
|
|
|
|
pango.pango_font_description_free)
|
2017-07-28 14:35:06 +03:00
|
|
|
if style['font_language_override'] != 'normal':
|
2016-09-13 06:02:36 +03:00
|
|
|
lang_p, lang = unicode_to_char_p(LST_TO_ISO.get(
|
2017-07-28 14:35:06 +03:00
|
|
|
style['font_language_override'].lower(),
|
|
|
|
style['font_language_override']))
|
|
|
|
elif style['lang']:
|
|
|
|
lang_p, lang = unicode_to_char_p(style['lang'])
|
2016-09-13 06:02:36 +03:00
|
|
|
else:
|
|
|
|
lang = None
|
|
|
|
self.language = pango.pango_language_get_default()
|
|
|
|
if lang:
|
|
|
|
self.language = pango.pango_language_from_string(lang_p)
|
2016-10-27 18:36:24 +03:00
|
|
|
pango.pango_context_set_language(pango_context, self.language)
|
2016-09-13 06:02:36 +03:00
|
|
|
|
2018-01-14 03:48:17 +03:00
|
|
|
assert not isinstance(style['font_family'], str), (
|
2013-04-04 06:39:25 +04:00
|
|
|
'font_family should be a list')
|
2017-07-28 14:35:06 +03:00
|
|
|
family_p, family = unicode_to_char_p(','.join(style['font_family']))
|
2016-10-27 18:36:24 +03:00
|
|
|
pango.pango_font_description_set_family(self.font, family_p)
|
2013-04-04 06:39:25 +04:00
|
|
|
pango.pango_font_description_set_style(
|
2017-07-28 14:35:06 +03:00
|
|
|
self.font, PANGO_STYLE[style['font_style']])
|
2013-04-04 06:39:25 +04:00
|
|
|
pango.pango_font_description_set_stretch(
|
2017-07-28 14:35:06 +03:00
|
|
|
self.font, PANGO_STRETCH[style['font_stretch']])
|
|
|
|
pango.pango_font_description_set_weight(
|
|
|
|
self.font, style['font_weight'])
|
2013-04-04 06:39:25 +04:00
|
|
|
pango.pango_font_description_set_absolute_size(
|
2016-10-27 18:36:24 +03:00
|
|
|
self.font, units_from_double(font_size))
|
|
|
|
pango.pango_layout_set_font_description(self.layout, self.font)
|
2013-04-04 06:39:25 +04:00
|
|
|
|
2012-12-29 02:17:04 +04:00
|
|
|
def iter_lines(self):
|
|
|
|
layout_iter = ffi.gc(
|
|
|
|
pango.pango_layout_get_iter(self.layout),
|
|
|
|
pango.pango_layout_iter_free)
|
|
|
|
while 1:
|
|
|
|
yield pango.pango_layout_iter_get_line_readonly(layout_iter)
|
|
|
|
if not pango.pango_layout_iter_next_line(layout_iter):
|
|
|
|
return
|
|
|
|
|
|
|
|
def set_text(self, text):
|
2012-12-29 17:20:38 +04:00
|
|
|
text, bytestring = unicode_to_char_p(text)
|
|
|
|
self.text = text
|
|
|
|
self.text_bytes = bytestring
|
2012-12-29 02:17:04 +04:00
|
|
|
pango.pango_layout_set_text(self.layout, text, -1)
|
2012-07-23 20:52:19 +04:00
|
|
|
|
2013-05-17 20:52:05 +04:00
|
|
|
def get_font_metrics(self):
|
|
|
|
context = pango.pango_layout_get_context(self.layout)
|
2016-09-13 06:02:36 +03:00
|
|
|
return FontMetrics(context, self.font, self.language)
|
2014-04-27 15:29:55 +04:00
|
|
|
|
2013-09-30 22:16:03 +04:00
|
|
|
def set_wrap(self, wrap_mode):
|
|
|
|
pango.pango_layout_set_wrap(self.layout, wrap_mode)
|
2013-05-17 20:52:05 +04:00
|
|
|
|
2016-08-20 21:11:03 +03:00
|
|
|
def set_tabs(self, style):
|
2018-01-13 19:05:23 +03:00
|
|
|
if isinstance(style['tab_size'], int):
|
2016-08-20 21:11:03 +03:00
|
|
|
layout = Layout(
|
2018-01-13 19:05:23 +03:00
|
|
|
context=self.context, font_size=style['font_size'],
|
2016-08-20 21:11:03 +03:00
|
|
|
style=style)
|
2018-01-13 19:05:23 +03:00
|
|
|
layout.set_text(' ' * style['tab_size'])
|
2016-08-20 21:11:03 +03:00
|
|
|
line, = layout.iter_lines()
|
|
|
|
width, _ = get_size(line, style)
|
2016-08-21 00:37:24 +03:00
|
|
|
width = int(round(width))
|
2016-08-20 21:11:03 +03:00
|
|
|
else:
|
2018-01-13 19:05:23 +03:00
|
|
|
width = int(style['tab_size'].value)
|
2016-08-20 21:11:03 +03:00
|
|
|
# TODO: 0 is not handled correctly by Pango
|
|
|
|
array = ffi.gc(
|
|
|
|
pango.pango_tab_array_new_with_positions(
|
|
|
|
1, True, pango.PANGO_TAB_LEFT, width or 1),
|
|
|
|
pango.pango_tab_array_free)
|
|
|
|
pango.pango_layout_set_tabs(self.layout, array)
|
|
|
|
|
2013-05-17 20:52:05 +04:00
|
|
|
|
|
|
|
class FontMetrics(object):
|
2016-09-13 06:02:36 +03:00
|
|
|
def __init__(self, context, font, language):
|
2013-05-17 20:52:05 +04:00
|
|
|
self.metrics = ffi.gc(
|
2016-09-13 06:02:36 +03:00
|
|
|
pango.pango_context_get_metrics(context, font, language),
|
2013-06-03 20:35:37 +04:00
|
|
|
pango.pango_font_metrics_unref)
|
2013-05-17 20:52:05 +04:00
|
|
|
|
|
|
|
def __dir__(self):
|
|
|
|
return ['ascent', 'descent',
|
|
|
|
'approximate_char_width', 'approximate_digit_width',
|
|
|
|
'underline_thickness', 'underline_position',
|
|
|
|
'strikethrough_thickness', 'strikethrough_position']
|
|
|
|
|
|
|
|
def __getattr__(self, key):
|
|
|
|
if key in dir(self):
|
|
|
|
return units_to_double(
|
|
|
|
getattr(pango, 'pango_font_metrics_get_' + key)(self.metrics))
|
2013-06-03 20:35:37 +04:00
|
|
|
|
2012-07-23 20:52:19 +04:00
|
|
|
|
2016-10-07 14:42:05 +03:00
|
|
|
def get_font_features(
|
|
|
|
font_kerning='normal', font_variant_ligatures='normal',
|
|
|
|
font_variant_position='normal', font_variant_caps='normal',
|
|
|
|
font_variant_numeric='normal', font_variant_alternates='normal',
|
|
|
|
font_variant_east_asian='normal', font_feature_settings='normal'):
|
2016-09-12 01:23:52 +03:00
|
|
|
"""Get the font features from the different properties in style.
|
|
|
|
|
|
|
|
See https://www.w3.org/TR/css-fonts-3/#feature-precedence
|
|
|
|
|
|
|
|
"""
|
|
|
|
features = {}
|
|
|
|
ligature_keys = {
|
|
|
|
'common-ligatures': ['liga', 'clig'],
|
|
|
|
'historical-ligatures': ['hlig'],
|
|
|
|
'discretionary-ligatures': ['dlig'],
|
|
|
|
'contextual': ['calt']}
|
|
|
|
caps_keys = {
|
|
|
|
'small-caps': ['smcp'],
|
|
|
|
'all-small-caps': ['c2sc', 'smcp'],
|
|
|
|
'petite-caps': ['pcap'],
|
|
|
|
'all-petite-caps': ['c2pc', 'pcap'],
|
|
|
|
'unicase': ['unic'],
|
|
|
|
'titling-caps': ['titl']}
|
|
|
|
numeric_keys = {
|
|
|
|
'lining-nums': 'lnum',
|
|
|
|
'oldstyle-nums': 'onum',
|
|
|
|
'proportional-nums': 'pnum',
|
|
|
|
'tabular-nums': 'tnum',
|
|
|
|
'diagonal-fractions': 'frac',
|
|
|
|
'stacked-fractions': 'afrc',
|
|
|
|
'ordinal': 'ordn',
|
|
|
|
'slashed-zero': 'zero'}
|
|
|
|
east_asian_keys = {
|
|
|
|
'jis78': 'jp78',
|
|
|
|
'jis83': 'jp83',
|
|
|
|
'jis90': 'jp90',
|
|
|
|
'jis04': 'jp04',
|
|
|
|
'simplified': 'smpl',
|
|
|
|
'traditional': 'trad',
|
|
|
|
'full-width': 'fwid',
|
|
|
|
'proportional-width': 'pwid',
|
|
|
|
'ruby': 'ruby'}
|
|
|
|
|
2016-10-07 14:42:05 +03:00
|
|
|
# Step 1: getting the default, we rely on Pango for this
|
2016-10-26 18:17:00 +03:00
|
|
|
# Step 2: @font-face font-variant, done in fonts.add_font_face
|
|
|
|
# Step 3: @font-face font-feature-settings, done in fonts.add_font_face
|
2016-09-12 01:23:52 +03:00
|
|
|
|
|
|
|
# Step 4: font-variant and OpenType features
|
|
|
|
|
2016-10-07 14:42:05 +03:00
|
|
|
if font_kerning != 'auto':
|
|
|
|
features['kern'] = int(font_kerning == 'normal')
|
2016-09-12 01:23:52 +03:00
|
|
|
|
2016-10-07 14:42:05 +03:00
|
|
|
if font_variant_ligatures == 'none':
|
2016-09-12 01:23:52 +03:00
|
|
|
for keys in ligature_keys.values():
|
|
|
|
for key in keys:
|
|
|
|
features[key] = 0
|
2016-10-07 14:42:05 +03:00
|
|
|
elif font_variant_ligatures != 'normal':
|
|
|
|
for ligature_type in font_variant_ligatures:
|
2016-09-12 01:23:52 +03:00
|
|
|
value = 1
|
|
|
|
if ligature_type.startswith('no-'):
|
|
|
|
value = 0
|
|
|
|
ligature_type = ligature_type[3:]
|
|
|
|
for key in ligature_keys[ligature_type]:
|
|
|
|
features[key] = value
|
|
|
|
|
2016-10-07 14:42:05 +03:00
|
|
|
if font_variant_position == 'sub':
|
2016-09-12 01:23:52 +03:00
|
|
|
# TODO: the specification asks for additional checks
|
|
|
|
# https://www.w3.org/TR/css-fonts-3/#font-variant-position-prop
|
|
|
|
features['subs'] = 1
|
2016-10-07 14:42:05 +03:00
|
|
|
elif font_variant_position == 'super':
|
2016-09-12 01:23:52 +03:00
|
|
|
features['sups'] = 1
|
|
|
|
|
2016-10-07 14:42:05 +03:00
|
|
|
if font_variant_caps != 'normal':
|
2016-09-12 01:23:52 +03:00
|
|
|
# TODO: the specification asks for additional checks
|
|
|
|
# https://www.w3.org/TR/css-fonts-3/#font-variant-caps-prop
|
2016-10-07 14:42:05 +03:00
|
|
|
for key in caps_keys[font_variant_caps]:
|
2016-09-12 01:23:52 +03:00
|
|
|
features[key] = 1
|
|
|
|
|
2016-10-07 14:42:05 +03:00
|
|
|
if font_variant_numeric != 'normal':
|
|
|
|
for key in font_variant_numeric:
|
2016-09-12 01:23:52 +03:00
|
|
|
features[numeric_keys[key]] = 1
|
|
|
|
|
2016-10-07 14:42:05 +03:00
|
|
|
if font_variant_alternates != 'normal':
|
2016-09-12 01:23:52 +03:00
|
|
|
# TODO: support other values
|
|
|
|
# See https://www.w3.org/TR/css-fonts-3/#font-variant-caps-prop
|
2016-10-07 14:42:05 +03:00
|
|
|
if font_variant_alternates == 'historical-forms':
|
2016-09-12 01:23:52 +03:00
|
|
|
features['hist'] = 1
|
|
|
|
|
2016-10-07 14:42:05 +03:00
|
|
|
if font_variant_east_asian != 'normal':
|
|
|
|
for key in font_variant_east_asian:
|
2016-09-12 01:23:52 +03:00
|
|
|
features[east_asian_keys[key]] = 1
|
|
|
|
|
2016-10-26 18:17:00 +03:00
|
|
|
# Step 5: incompatible non-OpenType features, already handled by Pango
|
2016-09-12 01:23:52 +03:00
|
|
|
|
|
|
|
# Step 6: font-feature-settings
|
|
|
|
|
2016-10-07 14:42:05 +03:00
|
|
|
if font_feature_settings != 'normal':
|
|
|
|
features.update(dict(font_feature_settings))
|
2016-09-12 01:23:52 +03:00
|
|
|
|
|
|
|
return features
|
|
|
|
|
|
|
|
|
2017-08-01 11:07:36 +03:00
|
|
|
def create_layout(text, style, context, max_width, justification_spacing):
|
2012-11-23 00:21:28 +04:00
|
|
|
"""Return an opaque Pango layout with default Pango line-breaks.
|
2011-08-19 18:52:56 +04:00
|
|
|
|
2012-06-21 17:10:17 +04:00
|
|
|
:param text: Unicode
|
2018-01-13 19:41:08 +03:00
|
|
|
:param style: a style dict of computed values
|
2012-06-21 17:10:17 +04:00
|
|
|
:param hinting: whether to enable text hinting or not
|
|
|
|
:param max_width:
|
2018-01-13 19:05:23 +03:00
|
|
|
The maximum available width in the same unit as ``style['font_size']``,
|
2012-06-21 17:10:17 +04:00
|
|
|
or ``None`` for unlimited width.
|
|
|
|
|
|
|
|
"""
|
2018-01-13 19:05:23 +03:00
|
|
|
text_wrap = style['white_space'] in ('normal', 'pre-wrap', 'pre-line')
|
2017-07-07 15:05:12 +03:00
|
|
|
if not text_wrap:
|
2016-10-07 16:57:56 +03:00
|
|
|
max_width = None
|
|
|
|
|
2018-01-13 19:05:23 +03:00
|
|
|
layout = Layout(context, style['font_size'], style)
|
2013-04-04 06:39:25 +04:00
|
|
|
layout.set_text(text)
|
2016-08-20 21:11:03 +03:00
|
|
|
|
2012-07-23 20:52:19 +04:00
|
|
|
# Make sure that max_width * Pango.SCALE == max_width * 1024 fits in a
|
|
|
|
# signed integer. Treat bigger values same as None: unconstrained width.
|
2012-11-23 00:21:28 +04:00
|
|
|
if max_width is not None and max_width < 2 ** 21:
|
2013-04-08 12:54:59 +04:00
|
|
|
pango.pango_layout_set_width(
|
|
|
|
layout.layout, units_from_double(max_width))
|
2016-08-20 21:11:03 +03:00
|
|
|
|
2016-09-12 01:23:52 +03:00
|
|
|
text_bytes = layout.text_bytes
|
|
|
|
|
2016-08-20 21:11:03 +03:00
|
|
|
# Word and letter spacings
|
2018-01-13 19:05:23 +03:00
|
|
|
word_spacing = style['word_spacing'] + justification_spacing
|
|
|
|
letter_spacing = style['letter_spacing']
|
2012-06-21 17:10:17 +04:00
|
|
|
if letter_spacing == 'normal':
|
|
|
|
letter_spacing = 0
|
|
|
|
if text and (word_spacing != 0 or letter_spacing != 0):
|
2012-07-23 20:52:19 +04:00
|
|
|
letter_spacing = units_from_double(letter_spacing)
|
2012-12-29 17:20:38 +04:00
|
|
|
space_spacing = units_from_double(word_spacing) + letter_spacing
|
|
|
|
attr_list = pango.pango_attr_list_new()
|
|
|
|
|
|
|
|
def add_attr(start, end, spacing):
|
2016-10-28 19:35:26 +03:00
|
|
|
# TODO: attributes should be freed
|
2012-12-29 17:20:38 +04:00
|
|
|
attr = pango.pango_attr_letter_spacing_new(spacing)
|
2016-10-26 19:15:32 +03:00
|
|
|
attr.start_index, attr.end_index = start, end
|
2012-12-29 17:20:38 +04:00
|
|
|
pango.pango_attr_list_insert(attr_list, attr)
|
|
|
|
|
|
|
|
add_attr(0, len(text_bytes) + 1, letter_spacing)
|
|
|
|
position = text_bytes.find(b' ')
|
|
|
|
while position != -1:
|
|
|
|
add_attr(position, position + 1, space_spacing)
|
|
|
|
position = text_bytes.find(b' ', position + 1)
|
2016-10-26 19:15:32 +03:00
|
|
|
|
2013-04-04 06:39:25 +04:00
|
|
|
pango.pango_layout_set_attributes(layout.layout, attr_list)
|
2012-12-29 17:20:38 +04:00
|
|
|
pango.pango_attr_list_unref(attr_list)
|
2016-08-20 21:11:03 +03:00
|
|
|
|
2016-10-07 14:42:05 +03:00
|
|
|
features = get_font_features(
|
2018-01-13 19:05:23 +03:00
|
|
|
style['font_kerning'], style['font_variant_ligatures'],
|
|
|
|
style['font_variant_position'], style['font_variant_caps'],
|
|
|
|
style['font_variant_numeric'], style['font_variant_alternates'],
|
|
|
|
style['font_variant_east_asian'], style['font_feature_settings'])
|
2016-09-12 01:23:52 +03:00
|
|
|
if features:
|
|
|
|
features = ','.join(
|
|
|
|
('%s %i' % (key, value)) for key, value in features.items())
|
2016-10-30 16:04:11 +03:00
|
|
|
|
|
|
|
# TODO: attributes should be freed.
|
|
|
|
# In the meantime, keep a cache to avoid leaking too many of them.
|
2018-03-25 17:20:24 +03:00
|
|
|
attr = context.font_features.get(features)
|
2016-10-30 16:04:11 +03:00
|
|
|
if attr is None:
|
|
|
|
try:
|
2016-10-30 23:17:15 +03:00
|
|
|
attr = pango.pango_attr_font_features_new(
|
|
|
|
features.encode('ascii'))
|
2016-10-30 16:04:11 +03:00
|
|
|
except AttributeError:
|
2017-07-25 14:59:56 +03:00
|
|
|
LOGGER.error(
|
2016-10-30 16:04:11 +03:00
|
|
|
'OpenType features are not available with Pango < 1.38')
|
|
|
|
else:
|
2018-03-25 17:20:24 +03:00
|
|
|
context.font_features[features] = attr
|
2016-10-30 16:04:11 +03:00
|
|
|
if attr is not None:
|
2016-09-12 01:23:52 +03:00
|
|
|
attr_list = pango.pango_attr_list_new()
|
|
|
|
pango.pango_attr_list_insert(attr_list, attr)
|
|
|
|
pango.pango_layout_set_attributes(layout.layout, attr_list)
|
|
|
|
|
2016-08-20 21:11:03 +03:00
|
|
|
# Tabs width
|
2018-01-13 19:05:23 +03:00
|
|
|
if style['tab_size'] != 8: # Default Pango value is 8
|
2016-08-20 21:11:03 +03:00
|
|
|
layout.set_tabs(style)
|
|
|
|
|
2013-04-04 06:39:25 +04:00
|
|
|
return layout
|
2012-06-21 17:10:17 +04:00
|
|
|
|
|
|
|
|
2018-03-05 03:23:32 +03:00
|
|
|
def split_first_line(text, style, context, max_width, justification_spacing,
|
|
|
|
minimum=False):
|
2012-06-21 17:10:17 +04:00
|
|
|
"""Fit as much as possible in the available width for one line of text.
|
|
|
|
|
2012-11-23 00:21:28 +04:00
|
|
|
Return ``(layout, length, resume_at, width, height, baseline)``.
|
2012-06-21 17:10:17 +04:00
|
|
|
|
2012-11-23 00:21:28 +04:00
|
|
|
``layout``: a pango Layout with the first line
|
2012-06-21 17:10:17 +04:00
|
|
|
``length``: length in UTF-8 bytes of the first line
|
|
|
|
``resume_at``: The number of UTF-8 bytes to skip for the next line.
|
|
|
|
May be ``None`` if the whole text fits in one line.
|
|
|
|
This may be greater than ``length`` in case of preserved
|
|
|
|
newline characters.
|
2012-11-23 00:21:28 +04:00
|
|
|
``width``: width in pixels of the first line
|
|
|
|
``height``: height in pixels of the first line
|
|
|
|
``baseline``: baseline in pixels of the first line
|
2011-08-19 18:52:56 +04:00
|
|
|
|
2011-07-29 03:13:07 +04:00
|
|
|
"""
|
2017-07-07 15:05:12 +03:00
|
|
|
# See https://www.w3.org/TR/css-text-3/#white-space-property
|
2018-01-13 19:05:23 +03:00
|
|
|
text_wrap = style['white_space'] in ('normal', 'pre-wrap', 'pre-line')
|
|
|
|
space_collapse = style['white_space'] in ('normal', 'nowrap', 'pre-line')
|
2016-05-08 17:44:57 +03:00
|
|
|
|
2017-07-07 15:05:12 +03:00
|
|
|
if not text_wrap:
|
2016-05-08 17:44:57 +03:00
|
|
|
max_width = None
|
|
|
|
|
2012-11-22 23:55:09 +04:00
|
|
|
# Step #1: Get a draft layout with the first line
|
2017-07-23 23:05:48 +03:00
|
|
|
layout = None
|
2018-01-13 19:05:23 +03:00
|
|
|
if (max_width is not None and max_width != float('inf') and
|
|
|
|
style['font_size']):
|
|
|
|
expected_length = int(max_width / style['font_size'] * 2.5)
|
2017-07-23 23:05:48 +03:00
|
|
|
if expected_length < len(text):
|
|
|
|
# Try to use a small amount of text instead of the whole text
|
|
|
|
layout = create_layout(
|
2017-08-01 11:07:36 +03:00
|
|
|
text[:expected_length], style, context, max_width,
|
|
|
|
justification_spacing)
|
2017-07-23 23:05:48 +03:00
|
|
|
lines = layout.iter_lines()
|
|
|
|
first_line = next(lines, None)
|
|
|
|
second_line = next(lines, None)
|
|
|
|
if second_line is None:
|
|
|
|
# The small amount of text fits in one line, give up and use
|
|
|
|
# the whole text
|
|
|
|
layout = None
|
|
|
|
if layout is None:
|
2017-08-01 11:07:36 +03:00
|
|
|
layout = create_layout(
|
|
|
|
text, style, context, max_width, justification_spacing)
|
2017-07-23 23:05:48 +03:00
|
|
|
lines = layout.iter_lines()
|
|
|
|
first_line = next(lines, None)
|
|
|
|
second_line = next(lines, None)
|
2013-03-02 04:33:37 +04:00
|
|
|
resume_at = None if second_line is None else second_line.start_index
|
2012-11-22 23:55:09 +04:00
|
|
|
|
2017-10-21 20:23:45 +03:00
|
|
|
# Step #2: Don't split lines when it's not needed
|
2013-03-02 04:33:37 +04:00
|
|
|
if max_width is None:
|
|
|
|
# The first line can take all the place needed
|
2016-05-08 17:44:57 +03:00
|
|
|
return first_line_metrics(
|
2016-08-20 16:26:16 +03:00
|
|
|
first_line, text, layout, resume_at, space_collapse, style)
|
2016-08-20 17:23:22 +03:00
|
|
|
first_line_width, _ = get_size(first_line, style)
|
2013-03-02 04:33:37 +04:00
|
|
|
if second_line is None and first_line_width <= max_width:
|
|
|
|
# The first line fits in the available width
|
2016-05-08 17:44:57 +03:00
|
|
|
return first_line_metrics(
|
2016-08-20 16:26:16 +03:00
|
|
|
first_line, text, layout, resume_at, space_collapse, style)
|
2013-03-02 04:33:37 +04:00
|
|
|
|
|
|
|
# Step #3: Try to put the first word of the second line on the first line
|
2017-01-11 15:45:24 +03:00
|
|
|
# https://mail.gnome.org/archives/gtk-i18n-list/2013-September/msg00006
|
|
|
|
# is a good thread related to this problem.
|
2013-03-02 04:33:37 +04:00
|
|
|
if first_line_width <= max_width:
|
|
|
|
# The first line may have been cut too early by Pango
|
|
|
|
second_line_index = second_line.start_index
|
2016-01-01 21:20:45 +03:00
|
|
|
first_line_text = utf8_slice(text, slice(second_line_index))
|
|
|
|
second_line_text = utf8_slice(text, slice(second_line_index, None))
|
2012-11-22 23:55:09 +04:00
|
|
|
else:
|
2013-03-02 04:33:37 +04:00
|
|
|
# The first word is longer than the line, try to hyphenize it
|
2016-01-01 21:20:45 +03:00
|
|
|
first_line_text = ''
|
|
|
|
second_line_text = text
|
2013-03-02 04:33:37 +04:00
|
|
|
|
2016-01-01 21:20:45 +03:00
|
|
|
next_word = second_line_text.split(' ', 1)[0]
|
2017-12-11 03:38:57 +03:00
|
|
|
if next_word:
|
2016-05-08 17:44:57 +03:00
|
|
|
if space_collapse:
|
|
|
|
# next_word might fit without a space afterwards
|
|
|
|
# only try when space collapsing is allowed
|
|
|
|
new_first_line_text = first_line_text + next_word
|
|
|
|
layout.set_text(new_first_line_text)
|
|
|
|
lines = layout.iter_lines()
|
|
|
|
first_line = next(lines, None)
|
|
|
|
second_line = next(lines, None)
|
2016-08-20 17:23:22 +03:00
|
|
|
first_line_width, _ = get_size(first_line, style)
|
2016-11-22 03:11:02 +03:00
|
|
|
if second_line is None and first_line_text:
|
2016-05-08 17:44:57 +03:00
|
|
|
# The next word fits in the first line, keep the layout
|
|
|
|
resume_at = len(new_first_line_text.encode('utf-8')) + 1
|
|
|
|
return first_line_metrics(
|
2016-08-20 16:26:16 +03:00
|
|
|
first_line, text, layout, resume_at, space_collapse, style)
|
2016-11-22 04:37:04 +03:00
|
|
|
elif second_line:
|
2016-11-22 03:11:02 +03:00
|
|
|
# Text may have been split elsewhere by Pango earlier
|
2016-11-22 04:37:04 +03:00
|
|
|
resume_at = second_line.start_index
|
|
|
|
else:
|
2017-12-10 03:01:55 +03:00
|
|
|
# Second line is none
|
2017-12-11 02:20:49 +03:00
|
|
|
resume_at = first_line.length + 1
|
2016-01-01 21:20:45 +03:00
|
|
|
elif first_line_text:
|
|
|
|
# We found something on the first line but we did not find a word on
|
|
|
|
# the next line, no need to hyphenate, we can keep the current layout
|
2016-05-08 17:44:57 +03:00
|
|
|
return first_line_metrics(
|
2016-08-20 16:26:16 +03:00
|
|
|
first_line, text, layout, resume_at, space_collapse, style)
|
2013-03-02 04:33:37 +04:00
|
|
|
|
|
|
|
# Step #4: Try to hyphenize
|
2018-01-13 19:05:23 +03:00
|
|
|
hyphens = style['hyphens']
|
|
|
|
lang = style['lang'] and pyphen.language_fallback(style['lang'])
|
|
|
|
total, left, right = style['hyphenate_limit_chars']
|
2013-09-30 22:16:03 +04:00
|
|
|
hyphenated = False
|
2016-08-19 18:50:24 +03:00
|
|
|
soft_hyphen = u'\u00ad'
|
2013-12-31 19:46:16 +04:00
|
|
|
|
|
|
|
# Automatic hyphenation possible and next word is long enough
|
2014-05-23 12:17:01 +04:00
|
|
|
if hyphens != 'none' and len(next_word) >= total:
|
2016-08-20 17:23:22 +03:00
|
|
|
first_line_width, _ = get_size(first_line, style)
|
2013-09-30 22:16:03 +04:00
|
|
|
space = max_width - first_line_width
|
2018-01-13 19:05:23 +03:00
|
|
|
if style['hyphenate_limit_zone'].unit == '%':
|
|
|
|
limit_zone = max_width * style['hyphenate_limit_zone'].value / 100.
|
2013-09-30 22:16:03 +04:00
|
|
|
else:
|
2018-01-13 19:05:23 +03:00
|
|
|
limit_zone = style['hyphenate_limit_zone'].value
|
2013-12-31 19:46:16 +04:00
|
|
|
|
2013-09-30 22:16:03 +04:00
|
|
|
if space > limit_zone or space < 0:
|
2016-08-19 17:42:52 +03:00
|
|
|
# Manual hyphenation: check that the line ends with a soft hyphen
|
|
|
|
# and add the missing hyphen
|
2014-05-23 12:17:01 +04:00
|
|
|
if hyphens == 'manual':
|
2016-08-19 17:42:52 +03:00
|
|
|
if first_line_text.endswith(soft_hyphen):
|
|
|
|
# The first line has been split on a soft hyphen
|
2016-08-19 18:24:27 +03:00
|
|
|
if u' ' in first_line_text:
|
2016-08-19 17:42:52 +03:00
|
|
|
first_line_text, next_word = (
|
2016-08-19 18:24:27 +03:00
|
|
|
first_line_text.rsplit(u' ', 1))
|
|
|
|
next_word = u' ' + next_word
|
2016-11-02 03:48:58 +03:00
|
|
|
layout.set_text(first_line_text)
|
2016-08-19 17:42:52 +03:00
|
|
|
lines = layout.iter_lines()
|
|
|
|
first_line = next(lines, None)
|
|
|
|
second_line = next(lines, None)
|
2016-08-19 18:24:27 +03:00
|
|
|
resume_at = len(
|
|
|
|
(first_line_text + u' ').encode('utf8'))
|
2014-05-23 12:17:01 +04:00
|
|
|
else:
|
2016-08-19 18:50:24 +03:00
|
|
|
first_line_text, next_word = u'', first_line_text
|
2016-08-19 17:42:52 +03:00
|
|
|
soft_hyphen_indexes = [
|
|
|
|
match.start() for match in
|
|
|
|
re.finditer(soft_hyphen, next_word)]
|
|
|
|
soft_hyphen_indexes.reverse()
|
|
|
|
dictionary_iterations = [
|
|
|
|
next_word[:i + 1] for i in soft_hyphen_indexes]
|
2014-05-23 13:53:05 +04:00
|
|
|
elif hyphens == 'auto' and lang:
|
2016-08-19 17:42:52 +03:00
|
|
|
# The next word does not fit, try hyphenation
|
2014-05-23 12:17:01 +04:00
|
|
|
dictionary_key = (lang, left, right, total)
|
2018-03-25 17:26:39 +03:00
|
|
|
dictionary = context.dictionaries.get(dictionary_key)
|
2014-05-23 12:17:01 +04:00
|
|
|
if dictionary is None:
|
2016-08-19 17:57:01 +03:00
|
|
|
dictionary = pyphen.Pyphen(
|
|
|
|
lang=lang, left=left, right=right)
|
2018-03-25 17:26:39 +03:00
|
|
|
context.dictionaries[dictionary_key] = dictionary
|
2016-08-19 17:42:52 +03:00
|
|
|
dictionary_iterations = [
|
|
|
|
start for start, end in dictionary.iterate(next_word)]
|
|
|
|
else:
|
|
|
|
dictionary_iterations = []
|
|
|
|
|
|
|
|
if dictionary_iterations:
|
|
|
|
for first_word_part in dictionary_iterations:
|
|
|
|
new_first_line_text = first_line_text + first_word_part
|
|
|
|
hyphenated_first_line_text = (
|
2018-01-13 19:05:23 +03:00
|
|
|
new_first_line_text + style['hyphenate_character'])
|
2016-08-19 17:42:52 +03:00
|
|
|
new_layout = create_layout(
|
2017-08-01 11:07:36 +03:00
|
|
|
hyphenated_first_line_text, style, context, max_width,
|
|
|
|
justification_spacing)
|
2016-08-19 17:42:52 +03:00
|
|
|
new_lines = new_layout.iter_lines()
|
|
|
|
new_first_line = next(new_lines, None)
|
|
|
|
new_second_line = next(new_lines, None)
|
2016-08-20 17:23:22 +03:00
|
|
|
new_first_line_width, _ = get_size(new_first_line, style)
|
2016-08-19 17:42:52 +03:00
|
|
|
new_space = max_width - new_first_line_width
|
|
|
|
if new_second_line is None and (
|
|
|
|
new_space >= 0 or
|
|
|
|
first_word_part == dictionary_iterations[-1]):
|
2014-05-23 12:17:01 +04:00
|
|
|
hyphenated = True
|
2016-08-19 17:42:52 +03:00
|
|
|
layout = new_layout
|
|
|
|
first_line = new_first_line
|
|
|
|
second_line = new_second_line
|
|
|
|
resume_at = len(new_first_line_text.encode('utf8'))
|
|
|
|
if text[len(new_first_line_text)] == soft_hyphen:
|
|
|
|
resume_at += len(soft_hyphen.encode('utf8'))
|
2013-09-30 22:16:03 +04:00
|
|
|
break
|
2013-12-31 19:46:16 +04:00
|
|
|
|
2016-08-19 17:42:52 +03:00
|
|
|
if not hyphenated and not first_line_text:
|
|
|
|
# Recreate the layout with no max_width to be sure that
|
|
|
|
# we don't break inside the hyphenate-character string
|
|
|
|
hyphenated = True
|
2016-11-02 03:48:58 +03:00
|
|
|
layout.set_text(hyphenated_first_line_text)
|
|
|
|
pango.pango_layout_set_width(
|
|
|
|
layout.layout, units_from_double(-1))
|
2016-08-19 17:42:52 +03:00
|
|
|
lines = layout.iter_lines()
|
|
|
|
first_line = next(lines, None)
|
|
|
|
second_line = next(lines, None)
|
|
|
|
resume_at = len(new_first_line_text.encode('utf8'))
|
|
|
|
if text[len(first_line_text)] == soft_hyphen:
|
|
|
|
resume_at += len(soft_hyphen.encode('utf8'))
|
|
|
|
|
|
|
|
if not hyphenated and first_line_text.endswith(soft_hyphen):
|
|
|
|
# Recreate the layout with no max_width to be sure that
|
|
|
|
# we don't break inside the hyphenate-character string
|
|
|
|
hyphenated = True
|
|
|
|
hyphenated_first_line_text = (
|
2018-01-13 19:05:23 +03:00
|
|
|
first_line_text + style['hyphenate_character'])
|
2016-11-02 03:48:58 +03:00
|
|
|
layout.set_text(hyphenated_first_line_text)
|
|
|
|
pango.pango_layout_set_width(
|
|
|
|
layout.layout, units_from_double(-1))
|
2016-08-19 17:42:52 +03:00
|
|
|
lines = layout.iter_lines()
|
|
|
|
first_line = next(lines, None)
|
|
|
|
second_line = next(lines, None)
|
|
|
|
resume_at = len(first_line_text.encode('utf8'))
|
2014-05-23 12:17:01 +04:00
|
|
|
|
2013-09-30 22:16:03 +04:00
|
|
|
# Step 5: Try to break word if it's too long for the line
|
2018-01-13 19:05:23 +03:00
|
|
|
overflow_wrap = style['overflow_wrap']
|
2016-08-20 17:23:22 +03:00
|
|
|
first_line_width, _ = get_size(first_line, style)
|
2013-03-02 06:42:36 +04:00
|
|
|
space = max_width - first_line_width
|
2013-09-30 22:16:03 +04:00
|
|
|
# If we can break words and the first line is too long
|
2018-03-05 03:23:32 +03:00
|
|
|
if not minimum and overflow_wrap == 'break-word' and space < 0:
|
2016-01-01 21:20:45 +03:00
|
|
|
# Is it really OK to remove hyphenation for word-break ?
|
|
|
|
hyphenated = False
|
2013-09-30 22:16:03 +04:00
|
|
|
# TODO: Modify code to preserve W3C condition:
|
|
|
|
# "Shaping characters are still shaped as if the word were not broken"
|
2013-12-31 19:46:16 +04:00
|
|
|
# The way new lines are processed in this function (one by one with no
|
|
|
|
# memory of the last) prevents shaping characters (arabic, for
|
|
|
|
# instance) from keeping their shape when wrapped on the next line with
|
2017-10-21 20:23:45 +03:00
|
|
|
# pango layout. Maybe insert Unicode shaping characters in text?
|
2016-11-02 03:48:58 +03:00
|
|
|
layout.set_text(text)
|
|
|
|
pango.pango_layout_set_width(
|
|
|
|
layout.layout, units_from_double(max_width))
|
2017-01-11 15:45:24 +03:00
|
|
|
layout.set_wrap(PANGO_WRAP_MODE['WRAP_CHAR'])
|
2016-11-02 03:48:58 +03:00
|
|
|
temp_lines = layout.iter_lines()
|
2016-08-19 17:57:01 +03:00
|
|
|
next(temp_lines, None)
|
2013-09-30 22:16:03 +04:00
|
|
|
temp_second_line = next(temp_lines, None)
|
2013-12-31 19:46:16 +04:00
|
|
|
temp_second_line_index = (
|
2016-01-01 21:20:45 +03:00
|
|
|
len(text.encode('utf-8')) if temp_second_line is None
|
2013-12-31 19:46:16 +04:00
|
|
|
else temp_second_line.start_index)
|
2017-01-11 15:45:24 +03:00
|
|
|
# TODO: WRAP_CHAR is said to "wrap lines at character boundaries", but
|
|
|
|
# it doesn't. Looks like it tries to split at word boundaries and then
|
|
|
|
# at character boundaries if there's no enough space for a full word,
|
|
|
|
# just as WRAP_WORD_CHAR does. That's why we have to split this text
|
|
|
|
# twice. Find why. It may be related to the problem described in the
|
|
|
|
# link given in step #3.
|
2016-01-01 21:20:45 +03:00
|
|
|
first_line_text = utf8_slice(text, slice(temp_second_line_index))
|
2016-11-02 03:48:58 +03:00
|
|
|
layout.set_text(first_line_text)
|
2013-09-30 22:16:03 +04:00
|
|
|
lines = layout.iter_lines()
|
|
|
|
first_line = next(lines, None)
|
Fix AssertionError in layout.inlines.split_text_box().
Traceback (most recent call last):
[...]
File "/app/env/lib/python3.5/site-packages/weasyprint/layout/inlines.py", line 718, in split_text_box
'Expected nothing or a preserved line break' % (between,))
AssertionError: Got '1,' between two lines. Expected nothing or a preserved line break
The Assertion Error can be triggered with following minimal test case
(Adobe's Source Sans Pro font must be installed):
<style type="text/css">
p {
font-family: 'Source Sans Pro';
font-size: 24pt;
width: 275pt;
overflow-wrap: break-word;
}
</style>
<p>W1D1,W1D7,W2D14,W3D21,W4D28</p>
With the Adobe Source Sans Font the pango line wrapping algorithm
sometimes produces sporadic results. The wrapping seems to be dependent
on the following text, so that a short text doesn't "fit" on a line, but
does if it is followed by more text. This can be worked around in the
split_first_line() function by computing the resume offset at a later
point, so it is in sync with the actual wrapping behaviour.
See https://bugzilla.gnome.org/show_bug.cgi?id=777093.
2017-01-11 13:31:16 +03:00
|
|
|
second_line = next(lines, None)
|
|
|
|
resume_at = (
|
|
|
|
first_line.length if second_line is None
|
|
|
|
else second_line.start_index)
|
2012-11-22 23:55:09 +04:00
|
|
|
|
2018-01-10 01:42:56 +03:00
|
|
|
if resume_at is not None and resume_at >= len(text.encode('utf-8')):
|
2018-01-09 19:05:49 +03:00
|
|
|
resume_at = None
|
2016-05-08 17:44:57 +03:00
|
|
|
return first_line_metrics(
|
2016-08-20 16:26:16 +03:00
|
|
|
first_line, text, layout, resume_at, space_collapse, style, hyphenated,
|
2018-01-13 19:05:23 +03:00
|
|
|
style['hyphenate_character'])
|
2012-06-21 17:10:17 +04:00
|
|
|
|
|
|
|
|
2013-04-08 12:54:59 +04:00
|
|
|
def show_first_line(context, pango_layout, hinting):
|
2012-12-29 02:17:04 +04:00
|
|
|
"""Draw the given ``line`` to the Cairo ``context``."""
|
2013-04-08 12:54:59 +04:00
|
|
|
context = ffi.cast('cairo_t *', context._pointer)
|
2012-12-29 02:17:04 +04:00
|
|
|
if hinting:
|
2013-04-08 12:54:59 +04:00
|
|
|
pangocairo.pango_cairo_update_layout(context, pango_layout.layout)
|
Don't split a line when drawing it
To draw a text line, the previous behaviour didn't rely only on the text
actually set on the layout, but also relied on the fact that the line
was cut again when drawn. This change removes the line cutting when
drawing, and thus only relies on the line splitting done during the
layout. This fixes a bug causing some words not being displayed at the
end of a text line drawn with hinting, and the actual drawing size with
hinting was bigger than the size calculated during the layout.
The text included in the drawn layout object was sometimes not cut at
the right position, it was longer but cut when actually drawn. This
commit also fixes this, by always setting the right text in the layout
object.
Fixing this bug enables us to remove a hack introduced to fight against
an "accumulation of floating point errors". I now think that "it wasn't
our war"™. I think that the real reasons of this hack were probably:
- a problem with trailing spaces in the shrink-to-fit functions fixed in
commit 3a620db, and
- this line-cutting bug while drawing, fixed now.
I've tried hard to reproduce the shink-to-fit problem without this hack,
with no success. I don't see anymore how it can theorically happen with
the current code of the "preferred" module. The only bug fixed by this
hack that I've been able to reproduce is the hinting problem explained
in the first paragraph, and this bug is now really fixed.
Moreover, this hack used to cause another problem: as the maximum size
allowed to an inline block was actually bigger than the real size
available in the line, an inline block whose size was between the real
and the allowed sizes was put on a new line by the split_inline_box
function. This commit fixes #288, the bug reported about this problem.
2016-01-01 18:48:15 +03:00
|
|
|
# Set an infinite width as we don't want to break lines when drawing, the
|
|
|
|
# lines have already been split and the size may differ for example because
|
|
|
|
# of hinting.
|
|
|
|
pango.pango_layout_set_width(pango_layout.layout, -1)
|
2012-12-29 04:00:30 +04:00
|
|
|
pangocairo.pango_cairo_show_layout_line(
|
2013-04-08 12:54:59 +04:00
|
|
|
context, next(pango_layout.iter_lines()))
|
2017-10-24 09:47:22 +03:00
|
|
|
|
|
|
|
|
|
|
|
def can_break_text(text, lang):
|
|
|
|
if not text or len(text) < 2:
|
|
|
|
return False
|
|
|
|
if lang:
|
|
|
|
lang_p, lang = unicode_to_char_p(lang)
|
|
|
|
else:
|
|
|
|
lang = None
|
|
|
|
language = pango.pango_language_get_default()
|
|
|
|
if lang:
|
|
|
|
language = pango.pango_language_from_string(lang_p)
|
2017-12-10 03:01:55 +03:00
|
|
|
# TODO: this should be removed when bidi is supported
|
|
|
|
for char in ('\u202a', '\u202b', '\u202c', '\u202d', '\u202e'):
|
|
|
|
text = text.replace(char, '')
|
2017-10-24 09:47:22 +03:00
|
|
|
text_p, bytestring = unicode_to_char_p(text)
|
|
|
|
length = len(bytestring) + 1
|
|
|
|
log_attrs = ffi.new('PangoLogAttr[]', length)
|
|
|
|
pango.pango_get_log_attrs(
|
|
|
|
text_p, len(bytestring), -1, language, log_attrs, length)
|
|
|
|
return any(attr.is_line_break for attr in log_attrs[1:length - 1])
|