예제 #1
0
def create_layout(text, style, hinting, max_width):
    """Return an opaque Pango layout with default Pango line-breaks.

    :param text: Unicode
    :param style: a :class:`StyleDict` of computed values
    :param hinting: whether to enable text hinting or not
    :param max_width:
        The maximum available width in the same unit as ``style.font_size``,
        or ``None`` for unlimited width.

    """
    if hinting:
        dummy_context = cairo.Context(cairo.ImageSurface(
            cairo.FORMAT_ARGB32, 1, 1))
    else:
        # None as a the target for PDFSurface is new in pycairo 1.8.8.
        # BytesIO() helps with compat with earlier versions:
        dummy_file = BytesIO()
        dummy_context = cairo.Context(cairo.PDFSurface(dummy_file, 1, 1))
    layout = create_pango_layout(dummy_context)
    font = Pango.FontDescription()
    assert not isinstance(style.font_family, basestring), (
        'font_family should be a list')
    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)
    layout.set_wrap(PANGO_WRAP_WORD)
    set_text(layout, text)
    # Make sure that max_width * Pango.SCALE == max_width * 1024 fits in a
    # signed integer. Treat bigger values same as None: unconstrained width.
    if max_width is not None and max_width < 2 ** 21:
        layout.set_width(units_from_double(max_width))
    word_spacing = style.word_spacing
    letter_spacing = style.letter_spacing
    if letter_spacing == 'normal':
        letter_spacing = 0
    if text and (word_spacing != 0 or letter_spacing != 0):
        word_spacing = units_from_double(word_spacing)
        letter_spacing = units_from_double(letter_spacing)
        markup = escape(text).replace(
            ' ', '<span letter_spacing="%i"> </span>' % (
                word_spacing + letter_spacing,))
        markup = '<span letter_spacing="%i">%s</span>' % (
            letter_spacing, markup)
        layout.set_attributes(parse_markup(markup))
    return layout
예제 #2
0
파일: text.py 프로젝트: jdahlin/WeasyPrint
def create_layout(text, style, hinting, max_width):
    """Return an opaque Pango object to be passed to other functions
    in this module.

    :param text: Unicode
    :param style: a :class:`StyleDict` of computed values
    :param hinting: whether to enable text hinting or not
    :param max_width:
        The maximum available width in the same unit as ``style.font_size``,
        or ``None`` for unlimited width.

    """
    layout = create_pango_layout(
        HINTED_DUMMY_CONTEXT if hinting else NON_HINTED_DUMMY_CONTEXT)
    font = Pango.FontDescription()
    assert not isinstance(style.font_family, basestring), (
        'font_family should be a list')
    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)
    layout.set_wrap(PANGO_WRAP_WORD)
    set_text(layout, text)
    # Make sure that max_width * Pango.SCALE == max_width * 1024 fits in a
    # signed integer. Treat bigger values same as None: unconstrained width.
    if max_width is not None and max_width < 2**21:
        layout.set_width(units_from_double(max_width))
    word_spacing = style.word_spacing
    letter_spacing = style.letter_spacing
    if letter_spacing == 'normal':
        letter_spacing = 0
    if text and (word_spacing != 0 or letter_spacing != 0):
        word_spacing = units_from_double(word_spacing)
        letter_spacing = units_from_double(letter_spacing)
        markup = escape(text).replace(
            ' ', '<span letter_spacing="%i"> </span>' % (
                word_spacing + letter_spacing,))
        markup = '<span letter_spacing="%i">%s</span>' % (
            letter_spacing, markup)
        layout.set_attributes(parse_markup(markup))
    return layout
예제 #3
0
def create_layout(text, style, hinting, max_width):
    """Return an opaque Pango object to be passed to other functions
    in this module.

    :param text: Unicode
    :param style: a :class:`StyleDict` of computed values
    :param hinting: whether to enable text hinting or not
    :param max_width:
        The maximum available width in the same unit as ``style.font_size``,
        or ``None`` for unlimited width.

    """
    layout = create_pango_layout(
        HINTED_DUMMY_CONTEXT if hinting else NON_HINTED_DUMMY_CONTEXT)
    font = Pango.FontDescription()
    assert not isinstance(style.font_family,
                          basestring), ('font_family should be a list')
    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)
    layout.set_wrap(PANGO_WRAP_WORD)
    set_text(layout, text)
    # Make sure that max_width * Pango.SCALE == max_width * 1024 fits in a
    # signed integer. Treat bigger values same as None: unconstrained width.
    if max_width is not None and max_width < 2**21:
        layout.set_width(units_from_double(max_width))
    word_spacing = style.word_spacing
    letter_spacing = style.letter_spacing
    if letter_spacing == 'normal':
        letter_spacing = 0
    if text and (word_spacing != 0 or letter_spacing != 0):
        word_spacing = units_from_double(word_spacing)
        letter_spacing = units_from_double(letter_spacing)
        markup = escape(text).replace(
            ' ', '<span letter_spacing="%i"> </span>' %
            (word_spacing + letter_spacing, ))
        markup = '<span letter_spacing="%i">%s</span>' % (letter_spacing,
                                                          markup)
        layout.set_attributes(parse_markup(markup))
    return layout