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 / build.py View on Github external
new_box = context.get_running_element_for(page, *value)
            if new_box is None:
                continue
            new_box = new_box.deepcopy()
            new_box.style['position'] = 'static'
            for child in new_box.descendants():
                if child.style['content'] in ('normal', 'none'):
                    continue
                child.children = content_to_boxes(
                    child.style, child, quote_depth, counter_values,
                    get_image_from_uri, target_collector, context=context,
                    page=page)
            boxlist.append(new_box)
    text = ''.join(texts)
    if text:
        boxlist.append(boxes.TextBox.anonymous_from(parent_box, text))
        # Only add CounterLookupItem if the content_list actually produced text
        target_collector.collect_missing_counters(
            parent_box, css_token, parse_again, missing_counters,
            missing_target_counters)

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

    else:
        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)
                children.append(box)

        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)
            box.style['white_space'] = 'pre-wrap'
            children.append(box)

    if not children:
        return

    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 https://drafts.csswg.org/css-pseudo-4/#marker-pseudo
        marker_box.style['position'] = 'absolute'
        if parent_style['direction'] == 'ltr':
            translate_x = properties.Dimension(-100, '%')
        else:
            translate_x = properties.Dimension(100, '%')
        translate_y = computed_values.ZERO_PIXELS
github Kozea / WeasyPrint / weasyprint / formatting_structure / build.py 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:
        children.extend(element_to_box(
            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
            else:
                children.append(text_box)
    children.extend(before_after_to_box(
        element, 'after', state, style_for, get_image_from_uri,
        target_collector))

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

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

    if marker_boxes and len(box.children) == 1:
        # See https://www.w3.org/TR/css-lists-3/#list-style-position-outside
        #
        # "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 / html.py 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.

    See: http://www.w3.org/TR/html5/embedded-content-1.html#the-img-element

    """
    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)]
        else:
            # 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 []
            else:
                assert alt is None
                # TODO: find some indicator that an image is missing.
                # For now, just remove the image.
                return []
    else:
        if alt:
            box.children = [boxes.TextBox.anonymous_from(box, alt)]
            return [box]
        else:
            return []
github Kozea / WeasyPrint / weasyprint / formatting_structure / build.py View on Github external
parent_box.cached_counter_values = copy.deepcopy(counter_values)

    for type_, value in content_list:
        if type_ == 'string':
            texts.append(value)
        elif type_ == 'url' and get_image_from_uri is not None:
            origin, uri = value
            if origin != 'external':
                # Embedding internal references is impossible
                continue
            image = get_image_from_uri(uri)
            if image is not None:
                text = ''.join(texts)
                if text:
                    boxlist.append(
                        boxes.TextBox.anonymous_from(parent_box, text))
                texts = []
                boxlist.append(
                    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()
            texts.append(added_text)
        elif type_ == 'counter()':
            counter_name, counter_style = value
            if need_collect_missing:
                _collect_missing_counter(
                    counter_name, counter_values, missing_counters)
            counter_value = counter_values.get(counter_name, [0])[-1]
            texts.append(counters.format(counter_value, counter_style))