How to use the weasyprint.formatting_structure.boxes.TextBox.anonymous_from function in weasyprint

To help you get started, we’ve selected a few weasyprint examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Kozea / WeasyPrint / weasyprint / formatting_structure / View on Github external
new_box = context.get_running_element_for(page, *value)
            if new_box is None:
            new_box = new_box.deepcopy()
  ['position'] = 'static'
            for child in new_box.descendants():
                if['content'] in ('normal', 'none'):
                child.children = content_to_boxes(
          , child, quote_depth, counter_values,
                    get_image_from_uri, target_collector, context=context,
    text = ''.join(texts)
    if text:
        boxlist.append(boxes.TextBox.anonymous_from(parent_box, text))
        # Only add CounterLookupItem if the content_list actually produced text
            parent_box, css_token, parse_again, missing_counters,

    return boxlist if (texts or boxlist) else None
github Kozea / WeasyPrint / weasyprint / formatting_structure / View on Github external

        if image_type == 'url':
            # image may be None here too, in case the image is not available.
            image = get_image_from_uri(image)
            if image is not None:
                box = boxes.InlineReplacedBox.anonymous_from(box, image)

        if not children and style['list_style_type'] != 'none':
            counter_value = counter_values.get('list-item', [0])[-1]
            # TODO: rtl numbered list has the dot on the left
            marker_text = counters.format_list_marker(
                counter_value, style['list_style_type'])
            box = boxes.TextBox.anonymous_from(box, marker_text)
  ['white_space'] = 'pre-wrap'

    if not children:

    if parent_style['list_style_position'] == 'outside':
        marker_box = boxes.BlockBox.anonymous_from(box, children)
        # We can safely edit everything that can't be changed by user style
        # See['position'] = 'absolute'
        if parent_style['direction'] == 'ltr':
            translate_x = properties.Dimension(-100, '%')
            translate_x = properties.Dimension(100, '%')
        translate_y = computed_values.ZERO_PIXELS
github Kozea / WeasyPrint / weasyprint / formatting_structure / View on Github external
# to get the spec-conform counter_values we must do it here,
    # after the ::before is parsed and before the ::after is
    if style['anchor']:
        target_collector.store_target(style['anchor'], counter_values, box)

    text = element.text
    if text:
        children.append(boxes.TextBox.anonymous_from(box, text))

    for child_element in element:
            child_element, style_for, get_image_from_uri, base_url,
            target_collector, state))
        text = child_element.tail
        if text:
            text_box = boxes.TextBox.anonymous_from(box, text)
            if children and isinstance(children[-1], boxes.TextBox):
                children[-1].text += text_box.text
        element, 'after', state, style_for, get_image_from_uri,

    # Scopes created by this element’s children stop here.
    for name in counter_scopes.pop():
        if not counter_values[name]:

    box.children = children
    # calculate string-set and bookmark-label
github Kozea / WeasyPrint / weasyprint / formatting_structure / View on Github external
set_content_lists(element, box, style, counter_values, target_collector)

    if marker_boxes and len(box.children) == 1:
        # See
        # "The size or contents of the marker box may affect the height of the
        #  principal block box and/or the height of its first line box, and in
        #  some cases may cause the creation of a new line box; this
        #  interaction is also not defined."
        # We decide here to add a zero-width space to have a minimum
        # height. Adding text boxes is not the best idea, but it's not a good
        # moment to add an empty line box, and the specification lets us do
        # almost what we want, so…
        if style['list_style_position'] == 'outside':
            box.children.append(boxes.TextBox.anonymous_from(box, '​'))

    # Specific handling for the element. (eg. replaced element)
    return html.handle_element(element, box, get_image_from_uri, base_url)
github Kozea / WeasyPrint / weasyprint / View on Github external
def handle_img(element, box, get_image_from_uri, base_url):
    """Handle ``<img>`` elements, return either an image or the alt-text.


    src = get_url_attribute(element, 'src', base_url)
    alt = element.get('alt')
    if src:
        image = get_image_from_uri(src)
        if image is not None:
            return [make_replaced_box(element, box, image)]
            # Invalid image, use the alt-text.
            if alt:
                box.children = [boxes.TextBox.anonymous_from(box, alt)]
                return [box]
            elif alt == '':
                # The element represents nothing
                return []
                assert alt is None
                # TODO: find some indicator that an image is missing.
                # For now, just remove the image.
                return []
        if alt:
            box.children = [boxes.TextBox.anonymous_from(box, alt)]
            return [box]
            return []
github Kozea / WeasyPrint / weasyprint / formatting_structure / View on Github external
parent_box.cached_counter_values = copy.deepcopy(counter_values)

    for type_, value in content_list:
        if type_ == 'string':
        elif type_ == 'url' and get_image_from_uri is not None:
            origin, uri = value
            if origin != 'external':
                # Embedding internal references is impossible
            image = get_image_from_uri(uri)
            if image is not None:
                text = ''.join(texts)
                if text:
                        boxes.TextBox.anonymous_from(parent_box, text))
                texts = []
                    boxes.InlineReplacedBox.anonymous_from(parent_box, image))
        elif type_ == 'content()':
            added_text = TEXT_CONTENT_EXTRACTORS[value](parent_box)
            # Simulate the step of white space processing
            # (normally done during the layout)
            added_text = added_text.strip()
        elif type_ == 'counter()':
            counter_name, counter_style = value
            if need_collect_missing:
                    counter_name, counter_values, missing_counters)
            counter_value = counter_values.get(counter_name, [0])[-1]
            texts.append(counters.format(counter_value, counter_style))