Exemple #1
0
 def test_without_indentation(self):
     par = tl.Paragraph(width=12, align=tl.ParagraphAlignment.CENTER)
     set_paragraph_content(par)
     par.place(0, 0)
     for line in par:
         assert line.total_width == 10
         assert line.final_location()[0] == 1
Exemple #2
0
def new_paragraph(
    cells: List,
    ctx: MTextContext,
    cap_height: float,
    line_spacing: float = 1,
    width: float = 0,
    default_stops: Sequence[tl.TabStop] = None,
):
    if cells:
        p = ctx.paragraph
        align = ALIGN.get(p.align, tl.ParagraphAlignment.LEFT)
        left = p.left * cap_height
        right = p.right * cap_height
        first = left + p.indent * cap_height  # relative to left
        _default_stops: Sequence[tl.TabStop] = default_stops or []
        tab_stops = _default_stops
        if p.tab_stops:
            tab_stops = make_tab_stops(
                cap_height, width, p.tab_stops, _default_stops
            )
        paragraph = tl.Paragraph(
            align=align,
            indent=(first, left, right),
            line_spacing=line_spacing,
            tab_stops=tab_stops,
        )
        paragraph.append_content(cells)
    else:
        paragraph = tl.EmptyParagraph(  # type: ignore
            cap_height=ctx.cap_height, line_spacing=line_spacing
        )
    return paragraph
Exemple #3
0
 def test_right_indentation(self):
     par = tl.Paragraph(width=12,
                        indent=(0, 0, 0.5),
                        align=tl.ParagraphAlignment.CENTER)
     set_paragraph_content(par)
     par.place(0, 0)
     for line in par:
         assert line.total_width == 10
         assert line.final_location()[0] == 0.75  # (11.5 - 10) / 2
Exemple #4
0
 def test_move_tab_to_next_line_if_following_content_does_not_fit(self):
     result = []
     par = tl.Paragraph(width=10, tab_stops=[tl.TabStop(4)])
     par.append_content(str2cells("t#t", content=6, result=result))
     # The tab (#) should move the following text to the tab stop
     # in the next line at position 4.
     par.distribute_content()
     par.place(0, 0)
     par.render()
     assert result[0] == "Text(0.0, -1.0, 6.0, 0.0)"
     assert result[1] == "Text(4.0, -2.7, 10.0, -1.7)", "x1 has to be 4.0"
Exemple #5
0
    def test_without_indentation(self):
        par = tl.Paragraph(width=12, align=tl.ParagraphAlignment.JUSTIFIED)
        set_paragraph_content(par)
        par.place(0, 0)
        lines = list(par)
        for line in lines[:-1]:
            assert line.total_width == 12  # expand across paragraph width
            assert line.final_location()[0] == 0

        # last line is not expanded
        last_line = lines[-1]
        assert last_line.total_width == 10
        assert last_line.final_location()[0] == 0
def create_layout(align: tl.ParagraphAlignment, content: Content) -> tl.Layout:
    # Create a flow text paragraph for the content:
    paragraph = tl.Paragraph(align=align)
    paragraph.append_content(content)

    # Start the layout engine and set default column width:
    layout = tl.Layout(
        width=8,  # default column width for columns without define width
        margins=(0.5, ),  # space around the layout
        # The render object of collections like Layout, Column or Paragraph is
        # called before the render objects of the content managed by the
        # collection.
        # This could be used to render a frame or a background:
        renderer=FrameRenderer(color=2),
    )

    # Append the first column with default width and a content height of 12 drawing
    # units. At least the first column has to be created by the client.
    layout.append_column(height=COLUMN_HEIGHT, gutter=1)

    # Append the content. The content will be distributed across the available
    # columns and automatically overflow into adjacent columns if necessary.
    # The layout engine creates new columns automatically if required by
    # cloning the last column.
    layout.append_paragraphs([paragraph])

    # Content- and total size is always up to date, only the final location
    # has to be updated by calling Layout.place().
    print()
    print(f"Layout has {len(layout)} columns.")
    print(f"Layout total width: {layout.total_width}")
    print(f"Layout total height: {layout.total_height}")

    for n, column in enumerate(layout, start=1):
        print()
        print(f"  {n}. column has {len(column)} paragraph(s)")
        print(f"  Column total width: {column.total_width}")
        print(f"  Column total height: {column.total_height}")

    # It is recommended to place the layout at origin (0, 0) and use a
    # transformation matrix to move the layout to the final location in
    # the DXF target layout - the model space in this example.
    # Set final layout location in the xy-plane with alignment:
    layout.place(align=tl.LayoutAlignment.BOTTOM_LEFT)

    # It is possible to add content after calling place(), but place has to be
    # called again before calling the render() method of the layout.
    return layout
Exemple #7
0
 def test_left_indentation(self):
     par = tl.Paragraph(width=12,
                        indent=(0.7, 0.5, 0),
                        align=tl.ParagraphAlignment.LEFT)
     set_paragraph_content(par)
     par.place(0, 0)
     lines = list(par)
     # first line:
     assert par.line_width(True) == 12 - 0.7  # available content space
     assert lines[0].final_location()[0] == 0.7
     assert lines[0].total_width == 10
     # remaining lines:
     for line in lines[1:]:
         assert par.line_width(False) == 12 - 0.5  # available content space
         assert line.total_width == 10
         assert line.final_location()[0] == 0.5
Exemple #8
0
    def test_with_indentation(self):
        par = tl.Paragraph(
            width=12,
            indent=(0.7, 0.5, 0.5),
            align=tl.ParagraphAlignment.JUSTIFIED,
        )
        set_paragraph_content(par)
        par.place(0, 0)
        lines = list(par)
        # first line:
        assert lines[0].total_width == 10.8  # 12 - (0.7 + 0.5)
        assert lines[0].final_location()[0] == 0.7

        # remaining lines:
        for line in lines[1:-1]:
            assert line.total_width == 11  # 12 - (0.5 + 0.5)
            assert line.final_location()[0] == 0.5

        # last line is not expanded:
        assert lines[-1].total_width == 10
        assert lines[-1].final_location()[0] == 0.5
Exemple #9
0
 def par(self):
     # Paragraph alignment is not important for content distribution.
     return tl.Paragraph(width=10, renderer=Rect("PAR"))
Exemple #10
0
 def par(self):
     # Paragraph alignment is not important for content distribution,
     # because the required space is independent from alignment (left,
     # right, center or justified).
     # This may change by implementing regular tabulator support.
     return tl.Paragraph(width=10, renderer=Rect("PAR"))
Exemple #11
0
def test_paragraph_available_line_content_space():
    par = tl.Paragraph(width=12, indent=(0.7, 0.5, 0.9))
    assert par.line_width(first=True) == 12 - 0.7 - 0.9
    assert par.line_width(first=False) == 12 - 0.5 - 0.9